Skip to content

antony@notes:~/data-platform$ cat "了解-Btrfs-Filesystem-運作.md"

了解 Btrfs Filesystem 運作

2023-06-08· data-platform

了解 Btrfs Filesystem 運作

What is Copy-on-Write (CoW) and How does it work?

  • Btrfs 檔案系統的所有寫入操作都會使用「Copy on Write(CoW)的機制」
  • 如果你只是寫入一個之前不存在的檔案,那麼資料就會被寫入到空的空間中,並且組成檔案系統的一些 metadata blocks 會被「CoWed」(寫時複製)。
  • 在「一般」的檔案系統中,如果你回頭覆蓋該檔案的一部分,那麼你正在寫入的那部分會直接覆蓋掉它所替換的資料。
  • 在「CoW」檔案系統中,新的資料會被寫入到磁碟上的一個空閒空間,然後才會將檔案的 metadata 更改為指向新的資料。在那個時候,被替換的舊資料可以被釋放,因為沒有任何東西再指向它了。

實作 Copy-on-Write

  1. Create a Btrfs filesystem on a single drive
~> mkfs.btrfs /dev/sdb
btrfs-progs v5.14
See http://btrfs.wiki.kernel.org for more information.

Label:              (null)
UUID:               b2a83758-9bfd-4e48-b079-a988f4cc7cbe
Node size:          16384
Sector size:        4096
Filesystem size:    50.00GiB
Block group profiles:
  Data:             single            8.00MiB
  Metadata:         DUP             256.00MiB
  System:           DUP               8.00MiB
SSD detected:       no
Zoned device:       no
Incompat features:  extref, skinny-metadata
Runtime features:
Checksum:           crc32c
Number of devices:  1
Devices:
   ID        SIZE  PATH
    1    50.00GiB  /dev/sdb
  1. 將一個 sdb 這顆硬碟 mount 到 /mnt/pool1 目錄上
$ mkdir /mnt/pool1
$ mount -t btrfs /dev/sdb /mnt/pool1
$ df -h  | egrep "Filesystem|pool1"
Filesystem               Size  Used Avail Use% Mounted on
/dev/sdb                  50G  3.5M   50G   1% /mnt/pool1
# 檢視根目錄之檔案系統類型
~> df -hT /
Filesystem              Type   Size  Used Avail Use% Mounted on
/dev/mapper/system-root btrfs   32G  2.4G   30G   8% /

# 建立並切換工作目錄
~> mkdir cow/ && cd cow

# 產生 1 G 大小之檔案
~> fallocate -l 1G one

# 使用 copy on write 的方式複製檔案
~> cp --reflink=always one two

# 不使用 copy on write 複製檔案
~> cp --reflink=never one three

# 顯示 btrfs 檔案系統中當前目錄下每個檔案的硬碟使用量
~> btrfs filesystem du .
     Total   Exclusive  Set shared  Filename
   1.00GiB       0.00B           -  ./one
   1.00GiB       0.00B           -  ./two
 991.38MiB   991.38MiB           -  ./three
   2.97GiB   991.38MiB     1.00GiB  .

# 檢視 one、two 和 three 三個檔案 在 儲存裝置上的絕對位置
~> filefrag -v one two three
Filesystem type is: 9123683e
File size of one is 1073741824 (262144 blocks of 4096 bytes)
 ext:     logical_offset:        physical_offset: length:   expected: flags:
   0:        0..  131071:     684781..    815852: 131072:             unwritten,shared
   1:   131072..  262143:     859392..    990463: 131072:     815853: last,unwritten,shared,eof
one: 2 extents found
File size of two is 4 (1 block of 4096 bytes)
 ext:     logical_offset:        physical_offset: length:   expected: flags:
   0:        0..    4095:          0..      4095:   4096:             last,not_aligned,inline,eof
two: 1 extent found
File size of three is 1073741824 (262144 blocks of 4096 bytes)
 ext:     logical_offset:        physical_offset: length:   expected: flags:
   0:        0..   32767:     815853..    848620:  32768:
   1:    32768..  262143:     990464..   1219839: 229376:     848621: last,eof
three: 2 extents found

what is subvolume

  • A BTRFS subvolume is a part of filesystem with its own independent file/directory hierarchy and inode number namespace.
  • Btrfs 的 subvolume 就像是獨立的 POSIX 檔案命名空間一樣。每個 subvolume 可以掛載到獨立的掛載點。
  • subvolume 具有彈性,可以在檔案系統的任何位置建立,也可以建立在其他 subvolume 之內。由於它們的階層性質,包含其他子 subvolume 的 subvolume 在刪除前無法被刪除。
  • 每個 Btrfs 的實作都會包含一個父/主 subvolume 。這是預設掛載的最上層 subvolume 。對於使用者來說, subvolume 看起來就像是目錄和子目錄。