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
c/c++ 学习互助QQ群:877684253
本人微信:xiaoshitou5854
来源:https://www.cnblogs.com/xiaoshiwang/p/12296336.html