Btrfs学习笔记

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Btrfs学习笔记相关的知识,希望对你有一定的参考价值。

一、Btrfs文件系统

Btrfs(B-tree, Butter FS, Better FS), Oracle从2007年开始研发的一种新的文件系统, 目标是用于取代ext3和ext4文件系统, 2014年8月发布稳定版本; 采用GPL授权.

1.1 Btrfs文件系统特性

  • 多物理卷支持: Btrfs可由多个底层物理卷组成; 支持RAID, 以联机“添加”、“移除”和“修改”操作;
  • 写时复制更新机制(CoW): 复制、更新及替换指针, 而非就地修改源文件;
  • 数据及元数据校验码: checksum
  • 子卷: sub_volume
  • 快照: 支持快照的快照
  • 透明压缩:

二、Btrfs文件系统的使用

2.1 Btrfs命令使用

# Btrfs命令的用法: btrfs <command> [<args>]
# command: 子命令; args: 参数

# 创建Btrfs文件系统: 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
[[email protected] ~]# mkfs.btrfs -O list-all
Filesystem features available at mkfs time:
mixed-bg            - mixed data and metadata block groups (0x4)
extref              - increased hardlink limit per file to 65536 (0x40, default)
raid56              - raid56 extended format (0x80)
skinny-metadata     - reduced-size metadata extent refs (0x100, default)
no-holes            - no explicit hole extents for files (0x200)

# 创建一个btrfs
[[email protected] ~]# mkfs.btrfs -L ‘mydata00‘ /dev/sdb /dev/sdc 
btrfs-progs v3.19.1
See http://btrfs.wiki.kernel.org 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
adding device /dev/sdc id 2
fs created label mydata00 on /dev/sdb
        nodesize 16384 leafsize 16384 sectorsize 4096 size 4.00GiB

# 查看当前系统btrfs的结构信息: 
# btrfs filesystem show [--mounted|--all-devices|<path>|<uuid>|<device>|<label>]
# 默认显示所有的btrfs信息
[[email protected] ~]# btrfs filesystem show
Label: ‘mydata00‘  uuid: 609254ef-9665-4856-94f4-081643231507
        Total devices 2 FS bytes used 112.00KiB
        devid    1 size 2.00GiB used 437.50MiB path /dev/sdb
        devid    2 size 2.00GiB used 417.50MiB path /dev/sdc

btrfs-progs v3.19.1

# 更改或者查看btrfs文件系统的label:
# btrfs filesystem label [<device>|<mount_point>] [<newlabel>]
[roo[email protected] ~]# btrfs filesystem label /dev/sdb
mydata00

[[email protected] ~]# btrfs filesystem label /dev/sdb ‘mydata01‘
[[email protected] ~]# btrfs filesystem label /dev/sdb
mydata01
[[email protected] ~]# btrfs filesystem label /dev/sdc
mydata01

# 挂载文件系统:
# mount -t btrfs /dev/btrfs_device MOUNT_POINT

# 挂载时透明压缩机制
# mount -o compress={lzo|zlib} -t btrfs /dev/btrfs_device MOUNT_POINT

# 修改btrfs的size
# btrfs filesystem resize [devid:][+/-]<newsize>[kKmMgGtTpPeE]|[devid:]max <path>
[[email protected] ~]# btrfs filesystem show /mydata
Label: ‘mydata01‘  uuid: 609254ef-9665-4856-94f4-081643231507
        Total devices 2 FS bytes used 256.00KiB
        devid    1 size 2.00GiB used 429.50MiB path /dev/sdb
        devid    2 size 2.00GiB used 417.50MiB path /dev/sdc

btrfs-progs v3.19.1

[[email protected] ~]# df -h
Filesystem                               Size  Used Avail Use% Mounted on
...
/dev/sdb                                 4.0G   17M  3.6G   1% /mydata

[[email protected] ~]# btrfs filesystem resize -1G /mydata   
Resize ‘/mydata‘ of ‘-1G‘

[[email protected] ~]# df -h
Filesystem                               Size  Used Avail Use% Mounted on
...
/dev/sdb                                 3.0G   17M  1.6G   2% /mydata

[[email protected] ~]# btrfs filesystem resize +500M /mydata
Resize ‘/mydata‘ of ‘+500M‘
[[email protected] ~]# df -h
Filesystem                               Size  Used Avail Use% Mounted on
...
/dev/sdb                                 3.5G   17M  2.6G   1% /mydata

[[email protected] ~]# btrfs filesystem resize max /mydata  
Resize ‘/mydata‘ of ‘max‘
[[email protected] ~]# df -h
Filesystem                               Size  Used Avail Use% Mounted on
...
/dev/sdb                                 4.0G   17M  3.6G   1% /mydata

# 添加一个磁盘到现有的btrfs中
# btrfs device add [options] <device> [<device>...] <path>
[[email protected] ~]# btrfs device add /dev/sdd /mydata
[[email protected] ~]# df -h
Filesystem                               Size  Used Avail Use% Mounted on
...
/dev/sdb                                 6.0G   17M  5.2G   1% /mydata

# btrfs中的chunk平衡, 用于将已使用的chunk平均的分布在所有的btrfs设备中
# 开启balance
# btrfs balance start [options] <path>
[[email protected] ~]# btrfs balance start /mydata
Done, had to relocate 5 out of 5 chunks

# 暂停balance
# btrfs balance pause <path>

# 取消balance
# btrfs balance cancel <path>

# 重新开始balance
# btrfs balance resume <path>

# 显示正在运行中或暂停的balance状态
# btrfs balance status [-v] <path>

# 从btrfs中移除一块磁盘
# btrfs device delete <device> [<device>...] <path>
[[email protected] ~]# btrfs device delete /dev/sdd /mydata
[[email protected] ~]# df -h
Filesystem                               Size  Used Avail Use% Mounted on
...
/dev/sdb                                 4.0G   17M  3.5G   1% /mydata
[[email protected] ~]# btrfs filesystem show /mydata
Label: ‘mydata01‘  uuid: 609254ef-9665-4856-94f4-081643231507
        Total devices 2 FS bytes used 392.00KiB
        devid    1 size 2.00GiB used 496.00MiB path /dev/sdb
        devid    2 size 2.00GiB used 496.00MiB path /dev/sdc

btrfs-progs v3.19.1

# 修改数据的raid级别; 不同的raid级别需要的设备数量不同
# btrfs balance start -d[<filters>] <path>
[[email protected] ~]# btrfs balance start -dconvert=raid1 /mydata/
Done, had to relocate 1 out of 3 chunks
[[email protected] ~]# btrfs balance start -dconvert=raid0 /mydata/
Done, had to relocate 1 out of 3 chunks

# 修改元数据raid级别; 不同的raid级别需要的设备数量不同
# btrfs balance start -m[<filters>] <path>

# 子卷管理
# 创建子卷:
# btrfs subvolume create [-i <qgroupid>] [<dest>/]<name>
[[email protected] ~]# btrfs subvolume create /mydata/logs
Create subvolume ‘/mydata/logs‘

# 列出子卷和快照:
# btrfs subvolume list [options] [-G [+|-]value] [-C [+|-]value] [--sort=gen,ogen,rootid,path] <path>
[[email protected] ~]# btrfs subvolume list /mydata       
ID 264 gen 84 top level 5 path logs

# 挂载子卷
# mount -o subvol=subvol_name device /MOUNT_POINT
[[email protected] ~]# umount /mydata/
[[email protected] ~]# mount -o subvol=logs /dev/sdb /mnt/logs
[[email protected] ~]# df -h
Filesystem                               Size  Used Avail Use% Mounted on
...
/dev/sdb                                 4.0G   17M  3.5G   1% /mnt/logs

# 显示子卷的信息
# btrfs subvolume show <subvol-path>
[[email protected] ~]# btrfs subvolume show /mnt/logs/
/mnt/logs
        Name:                   logs
        uuid:                   efd3a0ab-5af9-bf43-85bc-0efa369c8795
        Parent uuid:            -
        Creation time:          2018-01-10 15:28:54
        Object ID:              264
        Generation (Gen):       87
        Gen at creation:        84
        Parent:                 5
        Top Level:              5
        Flags:                  -
        Snapshot(s):

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

# 创建快照
# btrfs subvolume snapshot [-r] [-i <qgroupid>] <source> <dest>|[<dest>/]<name>
[[email protected] /]# btrfs subvolume snapshot /mydata/logs /mydata/logs-snapshot
Create a snapshot of ‘/mydata/logs‘ in ‘/mydata/logs-snapshot‘
[[email protected] /]# btrfs subvolume list /mydata
ID 264 gen 89 top level 5 path logs
ID 265 gen 89 top level 5 path logs-snapshot

# 删除快照卷方法同删除子卷一样

# 将ext3或者ext4文件系统转换成btrfs
[[email protected] ~]# 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: 11/131072 files (0.0% non-contiguous), 17964/524032 blocks
[[email protected] ~]# btrfs-convert /dev/sdd1 
creating btrfs metadata.
copy inodes [o] [         0/        11]
creating ext2fs image file.
cleaning up system chunk.
conversion complete.
[[email protected] ~]# btrfs filesystem show
Label: ‘mydata01‘  uuid: 609254ef-9665-4856-94f4-081643231507
        Total devices 2 FS bytes used 456.00KiB
        devid    1 size 2.00GiB used 496.00MiB path /dev/sdb
        devid    2 size 2.00GiB used 496.00MiB path /dev/sdc

Label: none  uuid: a6a7705f-efa7-4cb6-bc96-0024ea49a6b2
        Total devices 1 FS bytes used 70.30MiB
        devid    1 size 2.00GiB used 2.00GiB path /dev/sdd1

btrfs-progs v3.19.1

以上是关于Btrfs学习笔记的主要内容,如果未能解决你的问题,请参考以下文章

btrfs文件系统笔记

学习笔记:python3,代码片段(2017)

btrfs文件系统简单学习

btrfs文件系统学习

btrfs文件系统学习总结

Btrfs文件系统学习及练习