Btrfs文件系统及应用

、Btrfs文件系统简介

         Btrfs透明压缩文件系统 (B-tree, Butter FS, Better FS)是一种COW(copy-on-write式)文件系统,有着传统文件系统(ext3/4)所没有的一些特性,如支持可写的磁盘快照(snapshots),以及支持递归的快照(snapshots of snapshots),支持内建磁盘阵列(RAID),支持子卷(Subvolumes)的概念,允许在线调整文件系统大小等。

wKioL1YI7vjwY5MRAACsGkU5aZ4174.jpg

 

 

二、核心特性及命令

核心特性:

1、多物理卷支持:btrfs可由多个底层物理卷组成;支持RAID,以联机“添加”、“移除”,“修改”;

2、写时复制更新机制(CoW):复制、更新及替换指针,而非“就地”更新;

3、数据及元数据校验码:checksum

4、子卷:sub_volume,子卷实质上是一个保存文件和目录的命名的B树。它们的inode保存在树根之树中,可以为非根用户和组所有。子卷可选设定块配额。子卷内的所有块和文件区段都有引用计数以便做快照。和虚拟机存储的动态扩展相似,其只按需使用设备空间,消除了许多半满的分区。用户也可用不同的挂载选项挂载子卷,得到更灵活的安全性。

5、快照:支持快照的快照;    命令:btrfs subvolume snapshot

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>

  -O list-all: 列出支持的所有feature;

 

2、文件系统命令管理,包括卷标管理,文件系统大小调整,及碎片管理

命令:btrfs filesystem< subcommand> <args>

 属性查看:btrfs filesystem show

 

3、挂载文件系统:

命令:mount -t btrfs /dev/sdb MOUNT_POINT

 

4、透明压缩机制:

命令:mount -o compress={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

[root@localhost ~]# mkfs.btrfs -L 'B-tree-fs' -f /dev/sd{b,c}

[root@localhost ~]# btrfs filesystem show

Label: 'B-tree-fs'  uuid: b254b565-0418-4601-8317-f9a5306c6f19

Total devices 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、挂载文件系统

[root@localhost ~]# mkdir /btrdata

[root@localhost ~]# mount /dev/sdb /btrdata    

[root@localhost ~]# df -h | grep btrdata

/dev/sdb         40G  1.0M   38G   1% /btrdata

 

3、建立子卷

[root@localhost ~]# btrfs subvolume create /btrdata/mydata

Create subvolume '/btrdata/mydata'

[root@localhost ~]# cp -r /root/* /btrdata/mydata    # 复制数据

[root@localhost ~]# btrfs filesystem show

Label: 'B-tree-fs'  uuid: b254b565-0418-4601-8317-f9a5306c6f19

Total devices 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、扩展文件系统

[root@localhost ~]# btrfs device add /dev/sdd /btrdata

[root@localhost ~]# btrfs filesystem show

Label: 'B-tree-fs'  uuid: b254b565-0418-4601-8317-f9a5306c6f19

Total devices 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

[root@localhost ~]# df -h | grep btrdata

/dev/sdb         60G  772M   56G   2% /btrdata

 

5、重新均衡文件系统

[root@localhost ~]# btrfs balance start /btrdata

Done, had to relocate 6 out of 6 chunks

[root@localhost ~]# btrfs filesystem show

Label: 'B-tree-fs'  uuid: b254b565-0418-4601-8317-f9a5306c6f19

Total devices 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那样需要手动移动数据

[root@localhost ~]# btrfs device delete /dev/sdb /btrdata

[root@localhost ~]# df -h | grep btrdata

/dev/sdc         40G  772M   38G   2% /btrdata

[root@localhost ~]# ls /btrdata/

commdir  mydata

[root@localhost ~]# head /btrdata/mydata/anaconda-ks.cfg

#version=RHEL7

# System authorization information

auth --enableshadow --passalgo=sha512

 

# Use CDROM installation media

cdrom

# Use graphical install

graphical

# Run the Setup Agent on first boot

firstboot --enable

 

7、修改数据或元数据的RAID级别

[root@localhost ~]# btrfs device add /dev/sdb /btrdata

[root@localhost ~]# btrfs filesystem 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

 

[root@localhost ~]# btrfs balance start -mconvert=raid5 /btrdata    # 此处修改元数据RAID级别

Done, had to relocate 2 out of 3 chunks

[root@localhost ~]# btrfs filesystem 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

[root@localhost ~]# btrfs filesystem show

Label: 'B-tree-fs'  uuid: b254b565-0418-4601-8317-f9a5306c6f19

Total devices 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、创建快照

[root@localhost ~]# btrfs subvolume snapshot /btrdata/mydata /btrdata/mydata_snapshot

Create a snapshot of '/btrdata/mydata' in '/btrdata/mydata_snapshot'

[root@localhost ~]# cat /btrdata/mydata/1.txt

1

[root@localhost ~]# echo 'just a test' >> /btrdata/mydata/1.txt

[root@localhost ~]# cat /btrdata/mydata/1.txt

1

just a test

[root@localhost ~]# cat /btrdata/mydata_snapshot/1.txt

1

9、转换

[root@localhost ~]# btrfs balance start -dconvert=single -mconvert=raid1 /btrdata

Done, had to relocate 3 out of 3 chunks

[root@localhost ~]# btrfs device delete /dev/sdd /btrdata

[root@localhost ~]# fdisk /dev/sdd

Welcome to fdisk (util-linux 2.23.2).

 

Changes will remain in memory only, until you decide to write them.

Be careful before using the write command.

 

Device does not contain a recognized partition table

Building a new DOS disklabel 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 Linux and of size 5 GiB is set

 

Command (m for help): w

The partition table has been altered!

 

Calling ioctl() to re-read partition table.

Syncing disks.

[root@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, Stripe width=0 blocks

327680 inodes, 1310720 blocks

65536 blocks (5.00%) reserved for the super user

First data block=0

Maximum filesystem blocks=1342177280

40 block groups

32768 blocks per group, 32768 fragments per group

8192 inodes per group

Superblock backups stored on blocks:

32768, 98304, 163840, 229376, 294912, 819200, 884736

 

Allocating group tables: done                           

Writing inode tables: done                           

Creating journal (32768 blocks): done

Writing superblocks and filesystem accounting information: done

 

[root@localhost ~]# 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@localhost ~]# btrfs-convert /dev/sdd1    # ext4文件系统转换为btrfs

creating btrfs metadata.

creating ext2fs image file.

cleaning up system chunk.

conversion complete.

[root@localhost ~]# btrfs filesystem show

Label: 'B-tree-fs'  uuid: b254b565-0418-4601-8317-f9a5306c6f19

Total devices 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

Total devices 1 FS bytes used 228.42MiB

devid    1 size 5.00GiB used 5.00GiB path /dev/sdd1

 

Btrfs v3.16.2

[root@localhost ~]# btrfs-convert -r /dev/sdd1     # btrfs文件系统回滚原来的文件系统

rollback complete.

[root@localhost ~]# blkid /dev/sdd1

/dev/sdd1: UUID="fbbd653f-fb3f-4715-8c42-8c942564c8f4" TYPE="ext4"


你可能感兴趣的:(btrfs)