btrfs,它名字挺多:B tree fs;Butter fs;Better fs 開源協議是GPL,2007年由Oracle研發 核心特性: 多物理捲支持,btrfs可由多個物理捲組成;支持RAID,可以聯機狀態下,添加,移除,修改 寫時複製(Cow:copy on write):修改前的文件內容 ...
btrfs,它名字挺多:B-tree fs;Butter fs;Better fs
開源協議是GPL,2007年由Oracle研發
核心特性:
- 多物理捲支持,btrfs可由多個物理捲組成;支持RAID,可以聯機狀態下,添加,移除,修改
- 寫時複製(Cow:copy on write):修改前的文件內容還是保留的,只是讓指針指向修改後的文件
- 數據及元數據校驗碼:checksum
- 支持子捲:sub_volume
- 支持快照:而且支持快照的快照
- 透明壓縮:頂層用戶感覺不到文件是被壓縮了的,當要使用文件時,自動解壓縮,但消耗cpu時鐘。
btrfs有很多以btrfs開頭的命令:
# btrfs
btrfs btrfs-convert btrfs-find-root btrfs-map-logical btrfstune
btrfsck btrfs-debug-tree btrfs-image btrfs-select-super btrfs-zero-log
btrfs有許多子命令,每個命令的幫助都可以使用--help獲得。
管理子捲:btrfs subvolume
btrfs filesystem df
顯示已有的btrfs:btrfs filesystem show
btrfs filesystem sync
動態調整文件系統的大小:btrfs filesystem resize
添加捲標:btrfs filesystem label
調整文件系統空間的分佈:btrfs balance start
當新增物理捲後,把原來物理捲上的數據,調整一些到新增的物理捲上。
添加物理捲:btrfs device add
刪除物理捲:btrfs device delete
擦除數據:btrfs scrub
緊急救援:btrfs rescue chunk-recover
修複:btrfs restore
創建btrfs文件系統:mkfs.btrfs命令(版本:btrfs-progs v4.9.1)
指定捲標:-L "label"
指定數據區的數據存放方式:-d <type>:raid0(預設值),raid1,raid5,raid6,raid10,single
指定元數據區的數據存放方式:-m <profile>:raid0,raid1(預設值),raid5,raid6,raid10,single,dup
指定文件系統的功能:-O <feature>
mkfs.btrfs -O list-all
:列出當前內核所支持的功能。btrfs的feature很多,但是並不是內核都預設支持。
btrfs的應用場景:組合多個硬碟構成一個文件系統。
組合一個硬碟的多個分區成一個btrfs,沒有太大意義。
在centos7 1810上,試驗創建btrfs
1,創建btffs
設備/dev/sdb和/dev/sdc分別是2塊獨立的硬碟。
組合/dev/sdb和/dev/sdc,創建bttfs,捲標是btr01.
-f的作用是:/dev/sdb原來有分區,-f就是強制覆蓋原來分區里的內容。如果分區是乾凈的則不需要-f選項。
# mkfs.btrfs -f -L btr01 /dev/sdb /dev/sdc
btrfs-progs v4.9.1
See http://btrfs.wiki.kernel.org for more information.
Label: btr01
UUID: b1998e72-a16c-4b56-9a6b-8315bb621ff1
Node size: 16384
Sector size: 4096
Filesystem size: 2.00GiB
Block group profiles:
Data: RAID0 204.75MiB
Metadata: RAID1 102.38MiB
System: RAID1 8.00MiB
SSD detected: no
Incompat features: extref, skinny-metadata
Number of devices: 2
Devices:
ID SIZE PATH
1 1.00GiB /dev/sdb
2 1.00GiB /dev/sdc
2,查看創建出來的btffs
# btrfs filesystem show
Label: 'btr01' uuid: b1998e72-a16c-4b56-9a6b-8315bb621ff1
Total devices 2 FS bytes used 112.00KiB
devid 1 size 1.00GiB used 212.75MiB path /dev/sdb
devid 2 size 1.00GiB used 212.75MiB path /dev/sdc
3,用blkid查看,發現UUID是相同的,子捲id(UUID_SUB)是不同的。
# blkid /dev/sdb
/dev/sdb: LABEL="btr01" UUID="b1998e72-a16c-4b56-9a6b-8315bb621ff1" UUID_SUB="beceebee-4b19-4aed-a6df-a2d7464be690" TYPE="btrfs"
# blkid /dev/sdc
/dev/sdc: LABEL="btr01" UUID="b1998e72-a16c-4b56-9a6b-8315bb621ff1" UUID_SUB="7ef84d41-8cb6-4cb9-bb38-265e50283157" TYPE="btrfs"
4,查看已經掛載的btrfs
btrfs filesystem show --mounted
5,通過設備文件查看
# btrfs filesystem show /dev/sdb
Label: 'btr02' uuid: 1f0bca0c-adca-4270-b1a5-670765382d81
Total devices 2 FS bytes used 112.00KiB
devid 1 size 1.00GiB used 212.75MiB path /dev/sdb
devid 2 size 1.00GiB used 212.75MiB path /dev/sdc
# btrfs filesystem show /dev/sdc
Label: 'btr02' uuid: 1f0bca0c-adca-4270-b1a5-670765382d81
Total devices 2 FS bytes used 112.00KiB
devid 1 size 1.00GiB used 212.75MiB path /dev/sdb
devid 2 size 1.00GiB used 212.75MiB path /dev/sdc
6,通過掛載點查看
# btrfs filesystem show /mnt
Label: 'btr02' uuid: 1f0bca0c-adca-4270-b1a5-670765382d81
Total devices 2 FS bytes used 264.00KiB
devid 1 size 1.00GiB used 212.75MiB path /dev/sdb
devid 2 size 1.00GiB used 212.75MiB path /dev/sdc
7,查看btrfs設備文件的捲標:btrfs filesystem label /dev/sdc
8,查看文件系統里使用了多少空間:btrfs filesystem df 掛載點
# btrfs filesystem df /mnt
Data, RAID0: total=204.75MiB, used=136.00KiB
System, RAID1: total=8.00MiB, used=16.00KiB
Metadata, RAID1: total=102.38MiB, used=112.00KiB
GlobalReserve, single: total=16.00MiB, used=0.00B
掛載btrfs
和掛載別的文件系統沒有區別。
# mount /dev/sdb /mnt
添加文件,卸載,再掛載(再掛載的時候,指定磁碟/dev/sdc,效果和指定磁碟/dev/sdb是相同的),發信添加的文件還在
# cd /mnt
[root@localhost mnt]# ls
[root@localhost mnt]# touch a.txt
[root@localhost mnt]# cp /etc/grub2.cfg ./
[root@localhost mnt]# ls
a.txt grub2.cfg
[root@localhost mnt]# cd ..
[root@localhost /]# umount /dev/sdb
[root@localhost /]# ls /mnt
[root@localhost /]# mount /dev/sdb /mnt
[root@localhost /]# ls /mnt
a.txt grub2.cfg
掛載時,啟動透明壓縮功能
透明壓縮有2個演算法:compress={lzo|zlib}
# mount -o compress=lzo /dev/sdc /mnt
擴容和縮減
擴展和縮減不需要事先卸載文件系統
btrfs filesystem resize [<devid>:][+/-]<size>[kKmMgGtTpPeE]|[<devid>:]max <path>
1,縮減
縮減前的狀態:直接用df命令查看是2.0G
# btrfs filesystem df /mnt
Data, RAID0: total=204.75MiB, used=136.00KiB
System, RAID1: total=8.00MiB, used=16.00KiB
Metadata, RAID1: total=102.38MiB, used=112.00KiB
GlobalReserve, single: total=16.00MiB, used=0.00B
# df -lh
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 38G 4.5G 33G 13% /
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 9.5M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/sda1 1014M 179M 836M 18% /boot
/dev/mapper/centos-home 19G 39M 19G 1% /home
tmpfs 379M 4.0K 379M 1% /run/user/42
tmpfs 379M 28K 379M 1% /run/user/1000
tmpfs 379M 0 379M 0% /run/user/0
/dev/sdb 2.0G 17M 1.8G 1% /mnt
縮減後的狀態:直接用df命令查看是1.6G.
用btrfs filesystem df /mnt
看,縮減前後都一樣。
# btrfs filesystem resize -500m /mnt
Resize '/mnt' of '-500m'
# df -lh
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 38G 4.5G 33G 13% /
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 9.5M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/sda1 1014M 179M 836M 18% /boot
/dev/mapper/centos-home 19G 39M 19G 1% /home
tmpfs 379M 4.0K 379M 1% /run/user/42
tmpfs 379M 28K 379M 1% /run/user/1000
tmpfs 379M 0 379M 0% /run/user/0
/dev/sdb 1.6G 17M 826M 2% /mnt
# btrfs filesystem df /mnt
Data, RAID0: total=204.75MiB, used=136.00KiB
System, RAID1: total=8.00MiB, used=16.00KiB
Metadata, RAID1: total=102.38MiB, used=112.00KiB
GlobalReserve, single: total=16.00MiB, used=0.00B
縮減後,看看文件是否有損壞
# ls /mnt
a.txt grub2.cfg
[root@localhost /]# less /mnt/grub2.cfg
2,擴容
擴容前:直接用df命令查看是1.6G.
# df -lh
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 38G 4.5G 33G 13% /
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 9.5M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/sda1 1014M 179M 836M 18% /boot
/dev/mapper/centos-home 19G 39M 19G 1% /home
tmpfs 379M 4.0K 379M 1% /run/user/42
tmpfs 379M 28K 379M 1% /run/user/1000
tmpfs 379M 0 379M 0% /run/user/0
/dev/sdb 1.6G 17M 826M 2% /mnt
# btrfs filesystem df /mnt
Data, RAID0: total=204.75MiB, used=136.00KiB
System, RAID1: total=8.00MiB, used=16.00KiB
Metadata, RAID1: total=102.38MiB, used=112.00KiB
GlobalReserve, single: total=16.00MiB, used=0.00B
擴容後:直接用df命令查看是1.9G.
用btrfs filesystem df /mnt
看,擴容前後都一樣。
# btrfs filesystem resize +300M /mnt
Resize '/mnt' of '+300M'
# df -lh
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 38G 4.5G 33G 13% /
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 9.6M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/sda1 1014M 179M 836M 18% /boot
/dev/mapper/centos-home 19G 39M 19G 1% /home
tmpfs 379M 4.0K 379M 1% /run/user/42
tmpfs 379M 28K 379M 1% /run/user/1000
tmpfs 379M 0 379M 0% /run/user/0
/dev/sdb 1.9G 17M 1.4G 2% /mnt
# btrfs filesystem df /mnt
Data, RAID0: total=204.75MiB, used=136.00KiB
System, RAID1: total=8.00MiB, used=16.00KiB
Metadata, RAID1: total=102.38MiB, used=112.00KiB
GlobalReserve, single: total=16.00MiB, used=0.00B
擴容到邏輯邊界的最大:btrfs filesystem resize max /mnt
如果已經到了邏輯邊界的最大後,還想擴大,則添加物理磁碟。
# btrfs filesystem resize max /mnt
Resize '/mnt' of 'max'
# df -lh
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 38G 4.5G 33G 13% /
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 9.6M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/sda1 1014M 179M 836M 18% /boot
/dev/mapper/centos-home 19G 39M 19G 1% /home
tmpfs 379M 4.0K 379M 1% /run/user/42
tmpfs 379M 28K 379M 1% /run/user/1000
tmpfs 379M 0 379M 0% /run/user/0
/dev/sdb 2.0G 17M 1.8G 1% /mnt
物理擴容:btrfs device add /dev/sdd /mnt
添加磁碟/dev/sdd
# btrfs device add /dev/sdd /mnt
[root@localhost /]# df -lh
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 38G 4.5G 33G 13% /
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 9.6M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/sda1 1014M 179M 836M 18% /boot
/dev/mapper/centos-home 19G 39M 19G 1% /home
tmpfs 379M 4.0K 379M 1% /run/user/42
tmpfs 379M 28K 379M 1% /run/user/1000
tmpfs 379M 0 379M 0% /run/user/0
/dev/sdb 3.0G 17M 2.6G 1% /mnt
添加磁碟後,不會自動把原來磁碟里存的東西,分給新添加的磁碟,要想分給新的磁碟,需要使用 btrfs balance start
# btrfs balance start /mnt
WARNING:
Full balance without filters requested. This operation is very
intense and takes potentially very long. It is recommended to
use the balance filters to narrow down the balanced data.
Use 'btrfs balance start --full-balance' option to skip this
warning. The operation will start in 10 seconds.
Use Ctrl-C to stop it.
10 9 8 7 6 5 4 3 2 1
Starting balance without any filters.
Done, had to relocate 3 out of 3 chunks
如果磁碟里的數據很多,則執行需要很多時間,所以就有了查看狀態,暫停,取消,繼續等命令
- 查看狀態:
btrfs balance status
- 暫停:
btrfs balance pause
- 繼續:
btrfs balance resume
- 取消:
btrfs balance cancel
拆除掉一個物理磁碟:btrfs device delete /dev/sdd /mnt
拆除物理設備前不需要卸載,會自動把要拆掉磁碟里的內容,拷貝到別的磁碟(lvm在拆掉物理磁碟前,需要手動使用pvmove命令,拷貝內容到別的pv里)
# btrfs device delete /dev/sdd /mnt
# df -lh
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 38G 4.5G 33G 13% /
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 9.6M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/sda1 1014M 179M 836M 18% /boot
/dev/mapper/centos-home 19G 39M 19G 1% /home
tmpfs 379M 4.0K 379M 1% /run/user/42
tmpfs 379M 28K 379M 1% /run/user/1000
tmpfs 379M 0 379M 0% /run/user/0
/dev/sdb 2.0G 17M 1.6G 2% /mnt
發現空間從3G變成了2G。
修改RAID級別
修改元數據去的raid級別:btrfs balance start -dconvert=raid1 /mnt
# btrfs filesystem df /mnt
Data, RAID0: total=224.00MiB, used=136.00KiB
System, RAID1: total=32.00MiB, used=16.00KiB
Metadata, RAID1: total=208.00MiB, used=112.00KiB
GlobalReserve, single: total=16.00MiB, used=0.00B
# btrfs balance start -dconvert=raid1 /mnt
Done, had to relocate 1 out of 3 chunks
# btrfs filesystem df /mnt
Data, RAID1: total=208.00MiB, used=200.00KiB
System, RAID1: total=32.00MiB, used=16.00KiB
Metadata, RAID1: total=208.00MiB, used=112.00KiB
GlobalReserve, single: total=16.00MiB, used=0.00B
修改數據區的raid級別:btrfs balance start -mconvert=raid5 /mnt
# btrfs filesystem df /mnt
Data, RAID1: total=208.00MiB, used=200.00KiB
System, RAID1: total=32.00MiB, used=16.00KiB
Metadata, RAID1: total=208.00MiB, used=112.00KiB
GlobalReserve, single: total=16.00MiB, used=0.00B
# btrfs device add /dev/sdd /mnt
# btrfs balance start -mconvert=raid5 /mnt
Done, had to relocate 2 out of 3 chunks
[root@localhost ~]# btrfs filesystem df /mnt
Data, RAID1: total=208.00MiB, used=200.00KiB
System, RAID5: total=64.00MiB, used=16.00KiB
Metadata, RAID5: total=256.00MiB, used=112.00KiB
GlobalReserve, single: total=16.00MiB, used=0.00B
管理子捲
1,創建子捲btrfs subvolume create /mnt/子捲名
。/mnt是掛載點。
直接在掛載點下創建
# btrfs subvolume create /mnt/logs
Create subvolume '/mnt/logs'
# btrfs subvolume create /mnt/cache
Create subvolume '/mnt/cache'
2,查看子捲: btrfs subvolume list /mnt
logs子捲的id是265;cache的id是266
# btrfs subvolume list /mnt
ID 265 gen 104 top level 5 path logs
ID 266 gen 106 top level 5 path cache
3,查看詳細信息:
/mnt是父捲
/mnt/logs和/mnt/cache是子捲
# btrfs subvolume show /mnt
/mnt
Name: <FS_TREE>
UUID: -
Parent UUID: -
Received UUID: -
Creation time: -
Subvolume ID: 5
Generation: 100
Gen at creation: 0
Parent ID: 0
Top level ID: 0
Flags: -
Snapshot(s):
logs
cache
[root@localhost ~]# btrfs subvolume show /mnt/logs/
/mnt/logs
Name: logs
UUID: fa094c8f-b0f3-0749-b885-c1b60c7fa009
Parent UUID: -
Received UUID: -
Creation time: 2020-02-11 15:33:20 +0800
Subvolume ID: 265
Generation: 104
Gen at creation: 99
Parent ID: 5
Top level ID: 5
Flags: -
Snapshot(s):
[root@localhost ~]# btrfs subvolume show /mnt/cache/
/mnt/cache
Name: cache
UUID: ce332381-964e-ff45-a4c0-dff98ed01914
Parent UUID: -
Received UUID: -
Creation time: 2020-02-11 15:33:27 +0800
Subvolume ID: 266
Generation: 106
Gen at creation: 100
Parent ID: 5
Top level ID: 5
Flags: -
Snapshot(s):
3,掛載子捲:mount -o subvol=logs /dev/sdc/ /mnt
子捲可以獨立掛載。父捲掛載後,下麵的子捲會被自動掛載。
# btrfs subvolume create /mnt/logs
Create subvolume '/mnt/logs'
# btrfs subvolume create /mnt/cache
Create subvolume '/mnt/cache'
# cp /var/log/messages /mnt/logs/
# touch /mnt/cache/ca.txt
# umount /mnt
# mount -o subvol=logs /dev/sdc/ /mnt
# ls /mnt
messages
# umount /mnt
# mount -o subvol=cache /dev/sdc/ /mnt
# ls /mnt
ca.txt
# umount /mnt
# mount /dev/sdd/ /mnt
# ls /mnt/{logs,cache}
/mnt/cache:
ca.txt
/mnt/logs:
messages
4,通過子捲id掛載
# btrfs subvolume list /mnt
ID 265 gen 104 top level 5 path logs
ID 266 gen 106 top level 5 path cache
# mount -o subvolid=265 /dev/sdb /mnt
# ls /mnt
messages
5,刪除子捲
# btrfs subvolume list /mnt
ID 265 gen 104 top level 5 path logs
ID 266 gen 106 top level 5 path cache
# btrfs subvolume delete /mnt/logs
Delete subvolume (no-commit): '/mnt/logs'
# btrfs subvolume list /mnt
ID 266 gen 106 top level 5 path cache
快照管理
lvm中,快照必須和被快照的捲在同一個捲組(VG)中;
同理,子捲的快照必須和子捲在同一父捲中。
1,創建快照:btrfs subvolume snapshot /mnt/cache/ /mnt/cache_snapshot
/mnt/cache/是被快照的捲;
/mnt/cache_snapshot是快照捲。
# btrfs subvolume snapshot /mnt/cache/ /mnt/cache_snapshot
Create a snapshot of '/mnt/cache/' in '/mnt/cache_snapshot'
# ls /mnt/
a.txt cache cache_snapshot grub2.cfg
# ls /mnt/cache_snapshot/
ca.txt
# echo "new line" >> /mnt/cache/ca.txt
# cat /mnt/cache/ca.txt
new line
# cat /mnt/cache_snapshot/ca.txt
2,刪除快照捲:
和刪除子捲一樣
# btrfs subvolume delete /mnt/cache_snapshot/
Delete subvolume (no-commit): '/mnt/cache_snapshot'
3,文件的快照:
# cp --reflink ca.txt ca.bak
[root@localhost cache]# cat ca.txt
new line
[root@localhost cache]# cat ca.bak
new line
[root@localhost cache]# echo 222 >> ca.txt
[root@localhost cache]# cat ca.txt
new line
222
[root@localhost cache]# cat ca.bak
new line
如果不是在b tree文件系統里,執行cp --reflink ca.txt ca.bak
命令是報錯誤的。
把ext系列文件系統無損轉換成btrfs
/dev/sdd1是ext4文件系統
# blkid /dev/sdd1
/dev/sdd1: UUID="116b845a-5d35-4972-aa43-f9b67469072b" TYPE="ext4"
# mount /dev/sdd1 /mnt
# cp /etc/fstab /mnt
1,先卸載
# umount /mnt
2,檢查
# fsck -f /dev/sdd1
fsck from util-linux 2.23.2
e2fsck 1.42.9 (28-Dec-2013)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/sdd1: 12/57600 files (0.0% non-contiguous), 8289/230400 blocks
3,轉換成btrfs
# btrfs-convert /dev/sdd1
create btrfs filesystem:
blocksize: 4096
nodesize: 16384
features: extref, skinny-metadata (default)
creating ext2 image file
creating btrfs metadatacopy inodes [o] [ 0/ 12]
conversion complete
# btrfs filesystem show /dev/sdd1
Label: none uuid: 21520cb5-9f79-4199-b1fb-c8e9d093f4d1
Total devices 1 FS bytes used 32.05MiB
devid 1 size 900.00MiB used 227.00MiB path /dev/sdd1
# blkid /dev/sdd1
/dev/sdd1: UUID="21520cb5-9f79-4199-b1fb-c8e9d093f4d1" UUID_SUB="f113dae5-0722-4409-bc76-6d44b7601035" TYPE="btrfs"
4,掛載後,發現確實是無損,文件還在。但多了個ext2_saved目錄,這個目錄不能刪除。有這個目錄,還能從btrfs轉回去。
# mount /dev/sdd1/ /mnt
# ls /mnt
ext2_saved fstab lost+found
# cat /mnt/fstab
#
# /etc/fstab
# Created by anaconda on Fri Nov 29 16:44:28 2019
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root / xfs defaults 0 0
UUID=3d3b316a-529e-484a-9895-e785fdde5365 /boot xfs defaults 0 0
/dev/mapper/centos-home /home xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
5,轉回ext
# btrfs-convert -r /dev/sdd1