Linux btrfs文件系统及管理

linux文件系统,具有写时复制COW(copy-on-write),改善ext3文件系统单文件大小限制,并加入其他特性,如可写快照,快照的快照,内建RAID,子卷(subvloume),专注于容错,修复和易于管理,下面一起来看看什么是linux btrfs文件系统及管理关系吧!

Linux btrfs文件系统及管理

  什么是btrfs?

Btrfs(B-tree文件系统,通常念成Butter FS,Better FS或B-tree FS),linux文件系统,具有写时复制COW(copy-on-write),改善ext3文件系统单文件大小限制,并加入其他特性,如可写快照,快照的快照,内建RAID,子卷(subvloume),专注于容错,修复和易于管理。单文件可达16EB,最大文件数量2^64,最大卷容量16EB,等。

  btrfs功能特性

1,COW:写时复制,每次写入数据时,先将数据写入到新的block,写入成功后,更改旧数据块指针到新数据块,而非更改本身。

2,多物理卷支持,btrfs内建raid,可在线增删磁盘设备,可在线扩展和缩减磁盘空间。

3,数据和元数据校验码,checksum

4,子卷,可单独挂载子卷

5,可写快照,快照的快照,单个文件快照。

6,透明压缩

7,ext3/4和btrfs无痛互转

  btrfs 基本用法:

bash/shell Code复制内容到剪贴板

[root@localhost ~]# btrfs --help #查看帮助可以看到btrfs 有很多子命令,用法也很多,这里只举例常用选项。

usage: btrfs [--help] [--version] [...] []

btrfs subvolume create [-i ] [/] #创建子卷

Create a subvolume

btrfs subvolume delete [options] [...] #删除子卷

Delete subvolume(s)

btrfs subvolume list [options] [-G [+|-]value] [-C [+|-]value] [--sort=gen,ogen,rootid,path] #显示子卷列表

List subvolumes (and snapshots)

btrfs subvolume snapshot [-r] [-i ] |[/] #创建子卷快照

Create a snapshot of the subvolume

btrfs subvolume get-default #获取子卷默认的文件系统

Get the default subvolume of a filesystem

btrfs subvolume set-default #设置默认系统给子卷

Set the default subvolume of a filesystem

btrfs subvolume find-new #列出btrfs文件系统中最近修改的文件,结合find命令

List the recently modified files in a filesystem

btrfs subvolume show #显示更多的子卷信息

Show more information of the subvolume

btrfs subvolume sync [...] #子卷同步,类似mount同步模式,内存数据同步到磁盘,有待查证。

Wait until given subvolume(s) are completely removed from the filesystem.

btrfs filesystem df [options] #显示挂载的文件系统详细信息。

Show space usage information for a mount point

btrfs filesystem show [options] [|||label] #显示创建文件系统的磁盘信息。

Show the structure of a filesystem

btrfs filesystem sync #强制文件系统同步,

Force a sync on a filesystem

btrfs filesystem defragment [options] |

[|...] #碎片整理

Defragment a file or a directory

btrfs filesystem resize [devid:][+/-][kKmMgGtTpPeE]|[devid:]max #btrfs文件系统在线扩展和缩减空间

Resize a filesystem

btrfs filesystem label [|] [] #改变btrfs文件系统卷标

Get or change the label of a filesystem

btrfs filesystem usage [options] [..] #显示文件系统当前的使用信息。

Show detailed information about internal filesystem usage .

btrfs balance start [options] #改变磁盘chunk,在线改 数据和元数据 存储方式,单盘改raid,前提满足raid要求。

Balance chunks across the devices

btrfs balance pause #暂停chunk更改,数据量较大,转换时间较长时,先暂停。

Pause running balance

btrfs balance cancel #取消chunk更改,如上

Cancel running or paused balance

btrfs balance resume #中断balance的操作,如上

Resume interrupted balance

btrfs balance status [-v] #显示balance操作状态 如上

Show status of running or paused balance

btrfs device add [options] [...] #文件系统增加磁盘

Add a device to a filesystem

btrfs device delete [...] #文件系统删除磁盘

Remove a device from a filesystem

btrfs device scan [(-d|--all-devices)| [...]] #文件系统磁盘扫描

Scan devices for a btrfs filesystem

btrfs device ready #猜测是检测加入的'设备有没有被挂载

Check device to see if it has all of its devices in cache for mounting

btrfs device stats [-z] | #显示文件系统的设备状态

Show current device IO stats. -z to reset stats afterwards.

btrfs device usage [options] [..] #显示文件系统内部设备详细使用信息

Show detailed information about internal allocations in devices.

  btrfs文件系统管理

  为分区创建btrfs文件系统

  bash/shell Code复制内容到剪贴板

[root@localhost ~]# fdisk -l | grep "^Disk /dev/sd[a-z]" #准备sd{b,c,d,e}4块20G磁盘,未做任何分区。

Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 sectors

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors

Disk /dev/sdc: 21.5 GB, 21474836480 bytes, 41943040 sectors

Disk /dev/sde: 21.5 GB, 21474836480 bytes, 41943040 sectors

Disk /dev/sdd: 21.5 GB, 21474836480 bytes, 41943040 sectors

  创建单分区btrfs并查看

  bash/shell Code复制内容到剪贴板

[root@localhost ~]# s -L 'btrfs' /dev/sdb

btrfs-progs v3.19.1

See for more information.

Turning ON incompat feature 'extref': increased hardlink limit per file to 65536

Turning ON incompat feature 'skinny-metadata': reduced-size metadata extent refs

fs created label btrfs on /dev/sdb

nodesize 16384 leafsize 16384 sectorsize 4096 size 20.00GiB

[root@localhost ~]# btrfs filesystem show

Label: 'btrfs' uuid: 2a9f0d3d-f8a0-4757-9f4e-d1efa04a683a

Total devices 1 FS bytes used 112.00KiB

devid 1 size 20.00GiB used 2.04GiB path /dev/sdb

btrfs-progs v3.19.1

[root@localhost ~]# mount -o compress=lzo -L btrfs /btrfs/ 挂载时可以设定透明压缩机制。

[root@localhost ~]# btrfs filesystem df /btrfs

Data, single: total=8.00MiB, used=256.00KiB

System, DUP: total=8.00MiB, used=16.00KiB

System, single: total=4.00MiB, used=0.00B

Metadata, DUP: total=1.00GiB, used=112.00KiB

Metadata, single: total=8.00MiB, used=0.00B

GlobalReserve, single: total=16.00MiB, used=0.00B

  再添加一块磁盘

  bash/shell Code复制内容到剪贴板

[root@localhost ~]# btrfs device add /dev/sdc /btrfs #添加磁盘,删除用btrfs device delete /dev/sdc /btrfs

[root@localhost ~]# btrfs fi sh

Label: 'btrfs' uuid: 2a9f0d3d-f8a0-4757-9f4e-d1efa04a683a

Total devices 2 FS bytes used 384.00KiB

devid 1 size 20.00GiB used 2.04GiB path /dev/sdb

devid 2 size 20.00GiB used 0.00B path /dev/sdc

btrfs-progs v3.19.1

[root@localhost ~]#

在线增加或缩减空间,在线改变空间,可以让lvm坐冷板凳了。

  bash/shell Code复制内容到剪贴板

[root@localhost ~]# btrfs fi resize -10G /btrfs/ #在线缩减空间

Resize '/btrfs/' of '-10G'

[root@localhost ~]# btrfs fi sh

Label: 'btrfs' uuid: 2a9f0d3d-f8a0-4757-9f4e-d1efa04a683a

Total devices 2 FS bytes used 384.00KiB

devid 1 size 10.00GiB used 2.04GiB path /dev/sdb

devid 2 size 20.00GiB used 0.00B path /dev/sdc

btrfs-progs v3.19.1

[root@localhost ~]# btrfs fi resize +7G /btrfs/ #在线增加空间

Resize '/btrfs/' of '+7G'

[root@localhost ~]# btrfs fi sh

Label: 'btrfs' uuid: 2a9f0d3d-f8a0-4757-9f4e-d1efa04a683a

Total devices 2 FS bytes used 384.00KiB

devid 1 size 17.00GiB used 2.04GiB path /dev/sdb

devid 2 size 20.00GiB used 0.00B path /dev/sdc

btrfs-progs v3.19.1

[root@localhost ~]#

  在线更改数据和元数据的结构

  bash/shell Code复制内容到剪贴板

[root@localhost ~]# btrfs fi df /btrfs/

Data, single: total=8.00MiB, used=256.00KiB

System, DUP: total=8.00MiB, used=16.00KiB

System, single: total=4.00MiB, used=0.00B

Metadata, DUP: total=1.00GiB, used=112.00KiB

Metadata, single: total=8.00MiB, used=0.00B

GlobalReserve, single: total=16.00MiB, used=0.00B

[root@localhost ~]# btrfs balance start -mconvert=raid1 /btrfs/ #-mconvert 为改变metadata元数据区的存储结构

Done, had to relocate 4 out of 5 chunks

[root@localhost ~]# btrfs fi sh

Label: 'btrfs' uuid: 2a9f0d3d-f8a0-4757-9f4e-d1efa04a683a

Total devices 2 FS bytes used 192.00KiB

devid 1 size 17.00GiB used 296.00MiB path /dev/sdb

devid 2 size 20.00GiB used 288.00MiB path /dev/sdc

btrfs-progs v3.19.1

[root@localhost ~]# btrfs fi df /btrfs/

Data, single: total=8.00MiB, used=64.00KiB

System, RAID1: total=32.00MiB, used=16.00KiB

Metadata, RAID1: total=256.00MiB, used=112.00KiB #对比上改为了raid1

GlobalReserve, single: total=16.00MiB, used=0.00B

[root@localhost ~]# btrfs balance start -dconvert=raid1 /btrfs/ #-dconvert 为改变data数据区的存储结构

Done, had to relocate 1 out of 3 chunks