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"



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章