一、Btrfs文件系统简介 Btrfs透明压缩文件系统(B-tree, Butter FS, Better FS)是一种COW(copy-on-write式)文件系统,有着传统文件系统(ext3/4)所没有的一些特性,如支持可写的磁盘快照(snapshots),以及支持递归的快照(snapshotsof snapshots),支持内建磁盘阵列(RAID),支持子卷(Subvolumes)的概念,允许在线调整文件系统大小等。
二、核心特性及命令 核心特性: 1、多物理卷支持:btrfs可由多个底层物理卷组成;支持RAID,以联机“添加”、“移除”,“修改”; 2、写时复制更新机制(CoW):复制、更新及替换指针,而非“就地”更新; 3、数据及元数据校验码:checksum 4、子卷:sub_volume,子卷实质上是一个保存文件和目录的命名的B树。它们的inode保存在树根之树中,可以为非根用户和组所有。子卷可选设定块配额。子卷内的所有块和文件区段都有引用计数以便做快照。和虚拟机存储的动态扩展相似,其只按需使用设备空间,消除了许多半满的分区。用户也可用不同的挂载选项挂载子卷,得到更灵活的安全性。 5、快照:支持快照的快照; 命令:btrfs subvolumesnapshot 6、透明压缩:分区里的每个文件都被自动压缩,不仅减小了文件的大小,还提高了性能。 7、Btrfs支持在线碎片整理。命令: btrfs filesystem defragment
相关命令: 1、文件系统创建: 命令:mkfs.btrfs 选项:-L 'LABEL' -d<type>: raid0, raid1, raid5, raid6, raid10, single -m<profile>: raid0, raid1, raid5, raid6, raid10, single, dup -O<feature> -Olist-all: 列出支持的所有feature;
2、文件系统命令管理,包括卷标管理,文件系统大小调整,及碎片管理 命令:btrfs filesystem<subcommand> <args> 属性查看:btrfsfilesystem show
3、挂载文件系统: 命令:mount -t btrfs/dev/sdb MOUNT_POINT
4、透明压缩机制: 命令:mount -ocompress={lzo|zlib} DEVICE MOUNT_POINT
5、 管理子卷和快照命令 命令:btrfs subvolume<subcommand> [<args>]
6、设备管理命令 命令:btrfs device<subcommand> <args>
7、块组均衡管理 命令:btrfs [filesystem]balance <subcommand>|<args>
三、实验 1、创建btrfs文件系统 在vmware中新增两块20G磁盘,/dev/sdb与/dev/sdc [iyunv@localhost ~]#mkfs.btrfs -L 'B-tree-fs' -f /dev/sd{b,c} [iyunv@localhost ~]# btrfsfilesystem show Label: 'B-tree-fs' uuid: b254b565-0418-4601-8317-f9a5306c6f19 Totaldevices 2 FS bytes used 112.00KiB devid 1 size 20.00GiB used 2.03GiB path /dev/sdb devid 2 size 20.00GiB used 2.01GiB path /dev/sdc
2、挂载文件系统 [iyunv@localhost ~]# mkdir/btrdata [iyunv@localhost~]# mount /dev/sdb /btrdata [iyunv@localhost~]# df -h | grep btrdata /dev/sdb 40G 1.0M 38G 1% /btrdata
3、建立子卷 [iyunv@localhost ~]# btrfssubvolume create /btrdata/mydata Create subvolume'/btrdata/mydata' [iyunv@localhost~]# cp -r /root/* /btrdata/mydata # 复制数据 [iyunv@localhost ~]# btrfsfilesystem show Label: 'B-tree-fs' uuid: b254b565-0418-4601-8317-f9a5306c6f19 Totaldevices 2 FS bytes used 735.67MiB devid 1 size 20.00GiB used 2.03GiB path /dev/sdb devid 2 size 20.00GiB used 2.01GiB path /dev/sdc
4、扩展文件系统 [iyunv@localhost ~]# btrfsdevice add /dev/sdd /btrdata [iyunv@localhost ~]# btrfsfilesystem show Label: 'B-tree-fs' uuid: b254b565-0418-4601-8317-f9a5306c6f19 Totaldevices 3 FS bytes used 735.67MiB devid 1 size 20.00GiB used 2.03GiB path /dev/sdb devid 2 size 20.00GiB used 2.01GiB path /dev/sdc devid 3 size 20.00GiB used 0.00 path /dev/sdd [iyunv@localhost ~]# df -h| grep btrdata /dev/sdb 60G 772M 56G 2% /btrdata
5、重新均衡文件系统 [iyunv@localhost ~]# btrfsbalance start /btrdata Done, had to relocate 6out of 6 chunks [iyunv@localhost ~]# btrfsfilesystem show Label: 'B-tree-fs' uuid: b254b565-0418-4601-8317-f9a5306c6f19 Totaldevices 3 FS bytes used 736.20MiB devid 1 size 20.00GiB used 1.03GiB path /dev/sdb devid 2 size 20.00GiB used 2.00GiB path /dev/sdc devid 3 size 20.00GiB used 2.03GiB path /dev/sdd
6、移除硬盘 移除硬盘的时候会先把数据移动到其他盘,不需要像LVM那样需要手动移动数据 [iyunv@localhost ~]# btrfsdevice delete /dev/sdb /btrdata [iyunv@localhost ~]# df -h| grep btrdata /dev/sdc 40G 772M 38G 2% /btrdata [iyunv@localhost ~]# ls/btrdata/ commdir mydata [iyunv@localhost ~]# head/btrdata/mydata/anaconda-ks.cfg #version=RHEL7 # System authorizationinformation auth --enableshadow--passalgo=sha512
# Use CDROM installationmedia cdrom # Use graphical install graphical # Run the Setup Agent onfirst boot firstboot --enable
7、修改数据或元数据的RAID级别 [iyunv@localhost ~]# btrfsdevice add /dev/sdb /btrdata [iyunv@localhost ~]# btrfsfilesystem df /btrdata Data, RAID0:total=2.00GiB, used=699.96MiB System, RAID1:total=32.00MiB, used=16.00KiB Metadata, RAID1:total=1.00GiB, used=35.98MiB GlobalReserve, single:total=16.00MiB, used=0.00
[iyunv@localhost~]# btrfs balance start -mconvert=raid5 /btrdata # 此处修改元数据RAID级别 Done, had to relocate 2out of 3 chunks [iyunv@localhost ~]# btrfsfilesystem df /btrdata Data, RAID0:total=2.00GiB, used=700.21MiB System, RAID5:total=64.00MiB, used=16.00KiB Metadata, RAID5:total=1.00GiB, used=35.91MiB GlobalReserve, single:total=16.00MiB, used=0.00 [iyunv@localhost ~]# btrfsfilesystem show Label: 'B-tree-fs' uuid: b254b565-0418-4601-8317-f9a5306c6f19 Totaldevices 3 FS bytes used 735.89MiB devid 2 size 20.00GiB used 1.53GiB path /dev/sdc devid 3 size 20.00GiB used 1.53GiB path /dev/sdd devid 4 size 20.00GiB used 544.00MiB path/dev/sdb
8、创建快照 [iyunv@localhost ~]# btrfssubvolume snapshot /btrdata/mydata /btrdata/mydata_snapshot Create a snapshot of'/btrdata/mydata' in '/btrdata/mydata_snapshot' [iyunv@localhost ~]# cat/btrdata/mydata/1.txt 1 [iyunv@localhost ~]# echo'just a test' >> /btrdata/mydata/1.txt [iyunv@localhost ~]# cat/btrdata/mydata/1.txt 1 just a test [iyunv@localhost ~]# cat/btrdata/mydata_snapshot/1.txt 1 9、转换 [iyunv@localhost ~]# btrfsbalance start -dconvert=single -mconvert=raid1 /btrdata Done, had to relocate 3out of 3 chunks [iyunv@localhost ~]# btrfsdevice delete /dev/sdd /btrdata [iyunv@localhost ~]# fdisk/dev/sdd Welcome to fdisk(util-linux 2.23.2).
Changes will remain inmemory only, until you decide to write them. Be careful before usingthe write command.
Device does not contain arecognized partition table Building a new DOSdisklabel with disk identifier 0xadd41340.
Command (m for help): n Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): p Partition number (1-4,default 1): 1 First sector(2048-41943039, default 2048): Using default value 2048 Last sector, +sectors or+size{K,M,G} (2048-41943039, default 41943039): +5G Partition 1 of type Linuxand of size 5 GiB is set
Command (m for help): w The partition table hasbeen altered!
Calling ioctl() tore-read partition table. Syncing disks. [iyunv@localhost ~]#mke2fs -t ext4 /dev/sdd1 mke2fs 1.42.9(28-Dec-2013) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096(log=2) Stride=0 blocks, Stripewidth=0 blocks 327680 inodes, 1310720blocks 65536 blocks (5.00%)reserved for the super user First data block=0 Maximum filesystemblocks=1342177280 40 block groups 32768 blocks per group,32768 fragments per group 8192 inodes per group Superblock backups storedon blocks: 32768,98304, 163840, 229376, 294912, 819200, 884736
Allocating group tables:done Writing inode tables:done Creating journal (32768blocks): done Writing superblocks andfilesystem accounting information: done
[iyunv@localhost ~]# fsck-f /dev/sdd1 fsck from util-linux2.23.2 e2fsck 1.42.9(28-Dec-2013) Pass 1: Checking inodes,blocks, and sizes Pass 2: Checkingdirectory structure Pass 3: Checkingdirectory connectivity Pass 4: Checkingreference counts Pass 5: Checking groupsummary information /dev/sdd1: 12/327680files (0.0% non-contiguous), 58463/1310720 blocks [iyunv@localhost~]# btrfs-convert /dev/sdd1 # 将ext4文件系统转换为btrfs creating btrfs metadata. creating ext2fs imagefile. cleaning up system chunk. conversion complete. [iyunv@localhost ~]# btrfsfilesystem show Label: 'B-tree-fs' uuid: b254b565-0418-4601-8317-f9a5306c6f19 Totaldevices 2 FS bytes used 735.81MiB devid 2 size 20.00GiB used 2.03GiB path /dev/sdc devid 4 size 20.00GiB used 1.03GiB path /dev/sdb
Label: none uuid: 12170ec7-9847-4a5a-85ee-13215a478a3c Totaldevices 1 FS bytes used 228.42MiB devid 1 size 5.00GiB used 5.00GiB path /dev/sdd1
Btrfs v3.16.2 [iyunv@localhost~]# btrfs-convert -r /dev/sdd1 # 将btrfs文件系统回滚原来的文件系统 rollback complete. [iyunv@localhost ~]# blkid/dev/sdd1 /dev/sdd1:UUID="fbbd653f-fb3f-4715-8c42-8c942564c8f4" TYPE="ext4"
|