1、支持多物理卷;btrfs可由多个底层物理卷组成
2、支持RAID;btrfs内建raid,可在线增删磁盘设备,可在线扩展和缩减磁盘空间
3、写时复制更新机制(CoW:write and copy);原理是:复制、更新及替换指针,而非“就地”更新(如:每次写入数据时,先将数据写入到新的block,写入成功后,更改旧数据块指针到新数据块,而非更改本身)
4、数据及元数据校验:chechsum
5、子卷(sub volume);可单独挂载子卷
6、快照;支持快照、快照的快照
7、透明压缩;
8、支持件系统的无痛互转
btrfs命令的基本用法
1、创建文件系统
步骤一:分区为创建文件系统做准备。我这里是准备了sd[b,c,d]3块盘
[root@oldboy ~]# fdisk -l | grep "Disk /dev/" Disk /dev/sda: 42.9 GB, 42949672960 bytes, 83886080 sectors Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors Disk /dev/sdc: 21.5 GB, 21474836480 bytes, 41943040 sectors Disk /dev/sdd: 21.5 GB, 21474836480 bytes, 41943040 sectors Disk /dev/mapper/system-root: 33.3 GB, 33281802240 bytes, 65003520 sectors Disk /dev/mapper/system-swap: 8589 MB, 8589934592 bytes, 16777216 sectors [root@oldboy ~]#
步骤二:创建并查看两个分区的btrfs文件系统
创建命令:mkfs.btrfs -L mydata /dev/sdb /dev/sdc
查看命令:btrfs filesystem show 或者 blkid /dev/sdb 【blkid /dev/sdc】
[root@oldboy ~]# mkfs.btrfs -L mydata /dev/sdb /dev/sdc 把两个分区创建为了btrfs文件系统并指明了卷标mydata,后面带了两个参数即两个分区
Label: mydata UUID: ab824eab-9d5c-45b8-9ccd-78857bc9cb26 Node size: 16384 Sector size: 4096 Filesystem size: 40.00GiB [容量] Block group profiles: Data: RAID0 2.00GiB Metadata: RAID1 1.00GiB System: RAID1 8.00MiB SSD detected: no Incompat features: extref, skinny-metadata Number of devices: 2 [由两个子文件系统创建] Devices: ID SIZE PATH 1 20.00GiB /dev/sdb [每个子文件系统的大小] 2 20.00GiB /dev/sdc
[root@oldboy ~]# btrfs filesystem show 使用btrfs内置命令查看创建的文件系统的信息 Label: 'mydata' uuid: ab824eab-9d5c-45b8-9ccd-78857bc9cb26 [新文件系统的UUID号] Total devices 2 FS bytes used 112.00KiB devid 1 size 20.00GiB used 2.01GiB path /dev/sdb devid 2 size 20.00GiB used 2.01GiB path /dev/sdc [root@oldboy ~]# blkid /dev/sdb 普通命令查看的数据,和下面一个命令一样,只不过可以查看自己原有的UUID /dev/sdb: LABEL="mydata" UUID="ab824eab-9d5c-45b8-9ccd-78857bc9cb26" UUID_SUB="b6e560e8-0319-486a-80c4-2032fd64cf40" TYPE="btrfs" [注意:这里有两个UUID,一个是新文件系统的UUID;一个是子文件系统的UUID,即原单个文件系统自带的UUID] [root@oldboy ~]# blkid /dev/sdc /dev/sdc: LABEL="mydata" UUID="ab824eab-9d5c-45b8-9ccd-78857bc9cb26" UUID_SUB="3fb6cf4b-c5a5-43ae-9b3b-5d6b2f36ba68" TYPE="btrfs" [root@oldboy ~]#
步骤三:btrfs文件系统的挂载和卸载
命令:mount -t btrfs /dev/sdb /mydata/ 【或mount -t btrfs /dev/sdb /mydata/】 普通挂载(可以不加-t 选项)
命令:mount -o compress=lzo -L btrfs /btrfs/ 挂载时可以设定透明压缩机制
普通挂载和卸载 [root@oldboy ~]# mkdir /mydata [root@oldboy ~]# mount -t btrfs /dev/sdb /mydata [挂载时,可以指明任意一个文件系统,命令也可以写成:mount -t btrfs /dev/sdc] [root@oldboy ~]# umount /dev/sdc 【注意:但是卸载不行,哪个挂载的必须是哪个卸载】 umount: /dev/sdc: not mounted [root@oldboy ~]# umount /dev/sdb [root@oldboy ~]#
透明压缩机制挂载
[root@oldboy ~]# mount -o compress=lzo /dev/sdb /mydata/
[root@oldboy ~]# btrfs filesystem df /mydata/ 查看文件系统的使用情况
Data, RAID0: total=2.00GiB, used=768.00KiB
System, RAID1: total=8.00MiB, used=16.00KiB
Metadata, RAID1: total=1.00GiB, used=112.00KiB
GlobalReserve, single: total=16.00MiB, used=0.00B
[root@oldboy ~]# df -lh 查看文件系统的使用情况
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/system-root 31G 1.4G 30G 5% /
devtmpfs 901M 0 901M 0% /dev
tmpfs 912M 0 912M 0% /dev/shm
tmpfs 912M 8.7M 904M 1% /run
tmpfs 912M 0 912M 0% /sys/fs/cgroup
/dev/sda2 1014M 143M 872M 15% /boot
tmpfs 183M 0 183M 0% /run/user/0
/dev/sdb 40G 17M 38G 1% /mydata 两个20G的硬盘组合为一个btrfs文件系统的文件系统,大小为40G
步骤三:修改文件系统的大小【注意:这里修改的是btrfs文件系统的逻辑边界,逻辑边界最大不能超过文件系统的物理边界】
命令:btrfs filesystem resize -10G /mydata/
btrfs filesystem resize +5G /mydata/
btrfs filesystem resize max /mydata/
[root@oldboy ~]# btrfs filesystem resize -10G /mydata/ btrfs文件系统逻辑空间减少10G Resize '/mydata/' of '-10G' [root@oldboy ~]# df -lh Filesystem Size Used Avail Use% Mounted on /dev/mapper/system-root 31G 1.4G 30G 5% / devtmpfs 901M 0 901M 0% /dev tmpfs 912M 0 912M 0% /dev/shm tmpfs 912M 8.7M 904M 1% /run tmpfs 912M 0 912M 0% /sys/fs/cgroup /dev/sda2 1014M 143M 872M 15% /boot tmpfs 183M 0 183M 0% /run/user/0 /dev/sdb 30G 17M 18G 1% /mydata # 由原来的40G变为30G [root@oldboy ~]# btrfs filesystem resize +5G /mydata/ # btrfs文件系统逻辑空间增加5G Resize '/mydata/' of '+5G' [root@oldboy ~]# df -lh Filesystem Size Used Avail Use% Mounted on /dev/mapper/system-root 31G 1.4G 30G 5% / devtmpfs 901M 0 901M 0% /dev tmpfs 912M 0 912M 0% /dev/shm tmpfs 912M 8.7M 904M 1% /run tmpfs 912M 0 912M 0% /sys/fs/cgroup /dev/sda2 1014M 143M 872M 15% /boot tmpfs 183M 0 183M 0% /run/user/0 /dev/sdb 35G 17M 28G 1% /mydata # 由原来的30G变为35G [root@oldboy ~]# btrfs filesystem resize +50G /mydata/ #修改的逻辑空间大于物理空间,所以提示空间不足,修改失败 Resize '/mydata/' of '+50G' ERROR: unable to resize '/mydata/': no enough free space [root@oldboy ~]# btrfs filesystem resize max /mydata/ # 使用max参数,把逻辑空间调为和物理空间一样大 Resize '/mydata/' of 'max' [root@oldboy ~]# df -lh Filesystem Size Used Avail Use% Mounted on /dev/mapper/system-root 31G 1.4G 30G 5% / devtmpfs 901M 0 901M 0% /dev tmpfs 912M 0 912M 0% /dev/shm tmpfs 912M 8.6M 904M 1% /run tmpfs 912M 0 912M 0% /sys/fs/cgroup /dev/sda2 1014M 143M 872M 15% /boot tmpfs 183M 0 183M 0% /run/user/0 /dev/sdb 40G 17M 38G 1% /mydata # 40G [root@oldboy ~]#
步骤四:查看btrfs文件系统的使用情况
命令:btrfs filesystem df /mydata [这是btrfs文件系统自带的查看命令]
或 df -lh [这是通用的查看命令]
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
步骤五:向btrfs文件系统上添加一块新硬盘
命令: btrfs device add /dev/sdd /mydata/
[root@oldboy ~]# btrfs filesystem df /mydata/ Data, RAID0: total=2.00GiB, used=768.00KiB System, RAID1: total=8.00MiB, used=16.00KiB Metadata, RAID1: total=1.00GiB, used=112.00KiB GlobalReserve, single: total=16.00MiB, used=0.00B [root@oldboy ~]# df -lh Filesystem Size Used Avail Use% Mounted on /dev/mapper/system-root 31G 1.4G 30G 5% / devtmpfs 901M 0 901M 0% /dev tmpfs 912M 0 912M 0% /dev/shm tmpfs 912M 8.6M 904M 1% /run tmpfs 912M 0 912M 0% /sys/fs/cgroup /dev/sda2 1014M 143M 872M 15% /boot tmpfs 183M 0 183M 0% /run/user/0 /dev/sdb 40G 17M 38G 1% /mydata [root@oldboy ~]# btrfs device add /dev/sdd /mydata/ # 如果是添加两块硬盘的话,则是: btrfs device add /dev/sdd /dev/sde [root@oldboy ~]# df -lh Filesystem Size Used Avail Use% Mounted on /dev/mapper/system-root 31G 1.4G 30G 5% / devtmpfs 901M 0 901M 0% /dev tmpfs 912M 0 912M 0% /dev/shm tmpfs 912M 8.6M 904M 1% /run tmpfs 912M 0 912M 0% /sys/fs/cgroup /dev/sda2 1014M 143M 872M 15% /boot tmpfs 183M 0 183M 0% /run/user/0 /dev/sdb 60G 17M 56G 1% /mydata # 容量由原来的40G变为60G了 [root@oldboy ~]#
步骤六:把原来两块硬盘上面的数据均衡的罗一部分在新硬盘上面来
命令: btrfs balance start /mydata/ 把原来两块硬盘上面的数据均衡的罗一部分在新硬盘上面来
btrfs balance pause /mydata/ 暂停移动【由于业务原因,磁盘需要写入数据,会暂停移动状态】
btrfs balance resume /mydata/ 在业务空闲状态由开始接着移动数据
btrfs balance status /mydata/ 查看当前的移动状态
[root@oldboy ~]# btrfs balance start /mydata/ 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 [root@oldboy ~]# ------------------------------ [root@oldboy ~]# btrfs balance status /mydata/ # 这里由于是新的硬盘,所以移动数据很快,一下数据就移完了,看不到移动状态 No balance found on '/mydata/'
步骤七:从逻辑卷上移除物理卷【这里不同于lvm2不同,我们拆除物理卷的时候,它会自动把物理卷上的数据移走】
命令: btrfs device delete /dev/sdb /mydata
[root@oldboy ~]# btrfs device delete /dev/sdb /mydata/ [后面是/mydata可以省略不写] [root@oldboy ~]# btrfs filesystem show /mydata/ Label: 'mydata' uuid: ab824eab-9d5c-45b8-9ccd-78857bc9cb26 Total devices 2 FS bytes used 896.00KiB devid 2 size 20.00GiB used 2.03GiB path /dev/sdc devid 3 size 20.00GiB used 2.03GiB path /dev/sdd
[root@oldboy ~]#
步骤八:在线修改元数据或数据的j结构(RAID级别)
命令:btrfs balance start -dconvert=raid5 /mydata/ -dconvert修改数据的RAID级别;-mconvert 修改元数据的RAID级别;-sconvert 修改系统对应的组织机制
[root@oldboy ~]# btrfs fi df /mydata/ #查看数据或元数据的信息 Data, RAID5: total=2.00GiB, used=832.00KiB #数据信息 System, RAID1: total=32.00MiB, used=16.00KiB Metadata, RAID1: total=256.00MiB, used=112.00KiB #元数据信息 GlobalReserve, single: total=16.00MiB, used=0.00B [root@oldboy ~]# btrfs balance start -dconvert=raid0 /mydata/ #把数据结构从RAID5 --> RAID1 Done, had to relocate 1 out of 3 chunks [root@oldboy ~]# btrfs fi df /mydata/ Data, RAID0: total=3.00GiB, used=1.31MiB System, RAID1: total=32.00MiB, used=16.00KiB Metadata, RAID1: total=256.00MiB, used=112.00KiB GlobalReserve, single: total=16.00MiB, used=0.00B [root@oldboy ~]# [root@oldboy ~]# btrfs balance start -dconvert=raid10 /mydata/ # 注意:修改的RAID级别的时候需要物理硬盘块数支持才行,这里只有三块硬盘,不支持 RAID5 级别
ERROR: error during balancing '/mydata/': Invalid argument There may be more info in syslog - try dmesg | tail
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
子卷管理(创建和删除,挂载和卸载)
## 创建子卷 [root@oldboy ~]# btrfs subvolume create /mydata/logs #创建子卷就是在父卷的目录下创建一个文件 Create subvolume '/mydata/logs' ## 查看创建子卷个数 [root@oldboy ~]# btrfs subvolume list /mydata/ ID 276 gen 159 top level 5 path logs 【id 下面要使用,注意ID值是267】
##子卷挂载方法一:通过挂载父卷来挂载子卷 [root@oldboy ~]# mount /dev/sdb /mydata/ # 挂载父卷的话就自动会挂载该父卷下的所有子卷,且挂载的子卷目录就是子卷路径 [root@oldboy ~]# [root@oldboy ~]# btrfs subvolume show /mydata/ 【查看父卷的详细信息】 /mydata Name: <FS_TREE> UUID: - Parent UUID: - Received UUID: - Creation time: - Subvolume ID: 5 Generation: 160 Gen at creation: 0 Parent ID: 0 Top level ID: 0 Flags: - Snapshot(s): logs cache [root@oldboy ~]# btrfs subvolume show /mydata/logs/ # 查看子卷的详细信息 /mydata/logs Name: logs UUID: 1e292539-e93c-414d-9c07-5cb02a0a0ba2 Parent UUID: - Received UUID: - Creation time: 2018-06-06 11:42:58 +0800 Subvolume ID: 276 Generation: 159 Gen at creation: 159 Parent ID: 5 Top level ID: 5 Flags: - Snapshot(s):
##子卷挂载方法二:直接单独挂载子卷【注意:单独挂载子卷,父卷就无法访问】 [root@oldboy ~]# mount -o subvol=logs /dev/sdb /mnt/ # 单独挂载子卷 [root@oldboy ~]# btrfs subvolume show /mnt/ # 查看子卷的详细信息 /mnt Name: logs UUID: 1e292539-e93c-414d-9c07-5cb02a0a0ba2 Parent UUID: - Received UUID: - Creation time: 2018-06-06 11:42:58 +0800 Subvolume ID: 276 Generation: 159 Gen at creation: 159 Parent ID: 5 Top level ID: 5 Flags: - Snapshot(s): [root@oldboy ~]#
## 卸载子卷
[root@oldboy ~]# umount /mnt/
###注意:挂载子卷还可以使用ID来实现: mount -o subvolid=267 /dev/sdb /mnt
##子卷删除【前提是需要挂载父卷】
[root@oldboy ~]# btrfs subvolume delete /mydata/logs/
Delete subvolume (no-commit): '/mydata/logs'
[root@oldboy ~]# echo $?
0
[root@oldboy ~]# btrfs subvolume list /mydata/ [查看子卷,没有id号为267的子卷了,证明已经删除]
ID 277 gen 160 top level 5 path cache
子卷创建快照
【注意:快照必须与原卷在同一个卷组中】
命令: btrfs subvolume snapshot /mydata/logs/ /mydata/logs_snapshot 创建快照
命令: btrfs subvolume delete /mydata/logs_snapshot/ 删除快照
[root@oldboy ~]# btrfs subvolume create /mydata/logs #创建子卷logs Create subvolume '/mydata/logs' [root@oldboy ~]# btrfs subvolume list /mydata/ #查看子卷个数和ID号 ID 277 gen 160 top level 5 path cache ID 278 gen 168 top level 5 path logs [root@oldboy ~]# cp /etc/fstab /mydata/logs/ [root@oldboy ~]# btrfs subvolume snapshot /mydata/logs/ /mydata/logs_snapshot #创建子卷的快照 Create a snapshot of '/mydata/logs/' in '/mydata/logs_snapshot' [root@oldboy ~]# btrfs subvolume list /mydata/ ID 277 gen 160 top level 5 path cache ID 278 gen 171 top level 5 path logs ID 279 gen 171 top level 5 path logs_snapshot [root@oldboy ~]#
/// 我们创建好了快照,可以验证一下快照功能,下一段代码就是验证快照功能的
[root@oldboy ~]# vi /mydata/logs/fstab # 编辑子卷里面的文件,向里面添加了内容 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB CCCCCCCCCCCCCCCCCCCCCCCCCC # # /etc/fstab # Created by anaconda on Wed Jun 6 07:08:19 2018 # # 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/system-root / xfs defaults 0 0 UUID=eea47a65-764c-4e60-9753-741a93769f42 /boot xfs defaults 0 0 /dev/mapper/system-swap swap swap defaults 0 0 ~ [root@oldboy ~]# cat /mydata/logs_snapshot/fstab ##查看子卷快照的文件,子卷快照的文件里面的内容没有跟着子卷文件变化,证明快照创建成功 # # /etc/fstab # Created by anaconda on Wed Jun 6 07:08:19 2018 # # 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/system-root / xfs defaults 0 0 UUID=eea47a65-764c-4e60-9753-741a93769f42 /boot xfs defaults 0 0 /dev/mapper/system-swap swap swap defaults 0 0
##删除子卷快照
[root@oldboy ~]# btrfs subvolume delete /mydata/logs_snapshot/
Delete subvolume (no-commit): '/mydata/logs_snapshot'
单个文件快照
命令:cp --reflink fstab fstab.snap
ext系列文件系统和btrfs文件系统之间的转换
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
[root@oldboy ~]# umount /mnt/ ##在转换之前,如果该文件系统是挂载状态,必须修改为 umount 状态 [root@oldboy ~]# 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/327680 files (0.0% non-contiguous), 58463/1310720 blocks ##无损耗才能转
[root@oldboy ~]# btrfs-convert /dev/sdd1 ## ext4 文件系统转换为btrfs文件系统 create btrfs filesystem: blocksize: 4096 nodesize: 16384 features: extref, skinny-metadata (default) creating ext2 image file creating btrfs metadatacopy inodes [o] [ 3/ 12] conversion complete[root@oldboy ~]# btrfs filesystem show Label: 'mydata' uuid: ab824eab-9d5c-45b8-9ccd-78857bc9cb26 Total devices 2 FS bytes used 748.00KiB devid 2 size 20.00GiB used 1.28GiB path /dev/sdc devid 4 size 20.00GiB used 1.28GiB path /dev/sdb Label: none uuid: bfd36278-d7ae-477e-aaca-d66ff96584aa Total devices 1 FS bytes used 228.76MiB devid 1 size 5.00GiB used 485.78MiB path /dev/sdd1 [root@oldboy ~]# mount /dev/sdd1 /mnt/ [root@oldboy ~]# cd /mnt/ [root@oldboy mnt]# ll total 20 drwxr-xr-x. 1 root root 10 Jun 6 13:18 ext2_saved -rw-r--r--. 1 root root 465 Jun 6 13:08 fstab drwx------. 1 root root 0 Jun 6 13:07 lost+found [root@oldboy mnt]# cat fstab # # /etc/fstab # Created by anaconda on Wed Jun 6 07:08:19 2018 # # 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/system-root / xfs defaults 0 0 UUID=eea47a65-764c-4e60-9753-741a93769f42 /boot xfs defaults 0 0 /dev/mapper/system-swap swap swap defaults 0 0 [root@oldboy mnt]# [root@oldboy mnt]# umount /mnt/ umount: /mnt: target is busy. (In some cases useful info about processes that use the device is found by lsof(8) or fuser(1)) [root@oldboy mnt]# cd ~ [root@oldboy ~]# umount /mnt/ [root@oldboy ~]# btrfs-convert -r /dev/sdd1 ##btrfs 转回去(转为ext4文件系统) rollback complete [root@oldboy ~]# nlkid /dev/sdd1 -bash: nlkid: command not found [root@oldboy ~]# blkid /dev/sdd1 /dev/sdd1: UUID="ba723b0d-00c7-4d8a-ae58-2d06ae6d264b" TYPE="ext4" [root@oldboy ~]#