Btrfs 文件系统的使用

目前 Btrfs 文件系统是 Fedora 安装时默认使用的文件系统,Btrfs 文件系统相对于以前的 ext3 ext4 xfs 有点不一样,它是对表 ZFS 文件系统,是一种融合文件系统和卷管理器的结合体。

管理文件系统

创建和管理文件系统使用不同的工具。

创建文件系统

使用 mkfs 创建 btrfs 文件系统:

[root@fedora39 ~]# mkfs.btrfs -f -L mydata /dev/vdb /dev/vdc
btrfs-progs v6.6.2
See https://btrfs.readthedocs.io for more information.

Performing full device TRIM /dev/vdc (20.00GiB) ...
Performing full device TRIM /dev/vdb (20.00GiB) ...

Label:              mydata
UUID:               0c1ea6df-f061-4f32-8801-d20f8191ca48
Node size:          16384
Sector size:        4096
Filesystem size:    40.00GiB
Block group profiles:
  Data:             single            8.00MiB
  Metadata:         RAID1           256.00MiB
  System:           RAID1             8.00MiB
SSD detected:       no
Zoned device:       no
Incompat features:  extref, skinny-metadata, no-holes, free-space-tree
Runtime features:   free-space-tree
Checksum:           crc32c
Number of devices:  2
Devices:
   ID        SIZE  PATH    
    1    20.00GiB  /dev/vdb
    2    20.00GiB  /dev/vdc

-L 是指定 label,除此之外,创建文件系统时还有以下常用的选项:

  • -d 数据存储类型 raid0,raid1,raid5,raid6,raid10,single
  • -m 元数据存储类型 raid0,raid1,raid5,raid6,raid10,single,dup
  • -O 特性,使用 -O list-all 列出支持的所有特性
    透明压缩机制 mount -o compress={lzo|zlib} DEVICE Mount_Point

查看 Filesystem:

root@fedora39 ~]# btrfs filesystem show
Label: 'mydata'  uuid: 0c1ea6df-f061-4f32-8801-d20f8191ca48
    Total devices 2 FS bytes used 144.00KiB
    devid    1 size 20.00GiB used 272.00MiB path /dev/vdb
    devid    2 size 20.00GiB used 264.00MiB path /dev/vdc

此时两块磁盘的 UUID 是一致的,但是 UUID_SUB 是不一样的:

[root@fedora39 ~]# blkid /dev/vdb /dev/vdc
/dev/vdb: LABEL="mydata" UUID="0c1ea6df-f061-4f32-8801-d20f8191ca48" UUID_SUB="52e96baa-cf75-4471-a339-f7ab17eaae64" BLOCK_SIZE="4096" TYPE="btrfs"
/dev/vdc: LABEL="mydata" UUID="0c1ea6df-f061-4f32-8801-d20f8191ca48" UUID_SUB="9d7cd584-f516-4d54-bb4d-671e5b204571" BLOCK_SIZE="4096" TYPE="btrfs"

挂载时建议使用 UUID:

[root@fedora39 ~]# mkdir /mydata
[root@fedora39 ~]# mount -t btrfs UUID="0c1ea6df-f061-4f32-8801-d20f8191ca48" /mydata
[root@fedora39 ~]# mount | grep mydata
/dev/vdb on /mydata type btrfs (rw,relatime,seclabel,discard=async,space_cache=v2,subvolid=5,subvol=/)
[root@fedora39 ~]# df -h /mydata
Filesystem      Size  Used Avail Use% Mounted on
/dev/vdb         40G  5.8M   40G   1% /mydata

调整文件系统大小

使用 btrfs filesystem resize 对文件系统进行大小的调整。
缩小文件系统大小:

[root@fedora39 ~]# btrfs filesystem show /mydata
Label: 'mydata'  uuid: 0c1ea6df-f061-4f32-8801-d20f8191ca48
    Total devices 2 FS bytes used 144.00KiB
    devid    1 size 20.00GiB used 272.00MiB path /dev/vdb
    devid    2 size 20.00GiB used 264.00MiB path /dev/vdc

[root@fedora39 ~]# btrfs filesystem resize 2:-5G /mydata/
Resize device id 2 (/dev/vdc) from 20.00GiB to 15.00GiB
[root@fedora39 ~]# btrfs filesystem show /mydata
Label: 'mydata'  uuid: 0c1ea6df-f061-4f32-8801-d20f8191ca48
    Total devices 2 FS bytes used 144.00KiB
    devid    1 size 20.00GiB used 272.00MiB path /dev/vdb
    devid    2 size 15.00GiB used 264.00MiB path /dev/vdc

缩小文件系统需要注意文件系统实际使用的容量。

扩展文件系统大小:

[root@fedora39 ~]# btrfs filesystem resize 2:+3G /mydata/
Resize device id 2 (/dev/vdc) from 15.00GiB to 18.00GiB

扩展至设备最大可用的空间:

[root@fedora39 ~]# btrfs filesystem resize 2:max /mydata/
Resize device id 2 (/dev/vdc) from 18.00GiB to max
[root@fedora39 ~]# btrfs filesystem show /mydata/
Label: 'mydata'  uuid: 0c1ea6df-f061-4f32-8801-d20f8191ca48
    Total devices 2 FS bytes used 144.00KiB
    devid    1 size 20.00GiB used 272.00MiB path /dev/vdb
    devid    2 size 20.00GiB used 264.00MiB path /dev/vdc

设备管理

使用 btrfs device 命令可对 btrfs 所使用的设备进行管理。

添加设备到文件系统

使用 bttfs device add 将设备添加到文件系统中:

[root@fedora39 ~]# btrfs filesystem show /mydata
Label: 'mydata'  uuid: 0c1ea6df-f061-4f32-8801-d20f8191ca48
    Total devices 2 FS bytes used 144.00KiB
    devid    1 size 20.00GiB used 272.00MiB path /dev/vdb
    devid    2 size 20.00GiB used 264.00MiB path /dev/vdc

[root@fedora39 ~]# btrfs device add /dev/vdd /mydata
Performing full device TRIM /dev/vdd (20.00GiB) ...
[root@fedora39 ~]# btrfs filesystem show /mydata
Label: 'mydata'  uuid: 0c1ea6df-f061-4f32-8801-d20f8191ca48
    Total devices 3 FS bytes used 144.00KiB
    devid    1 size 20.00GiB used 272.00MiB path /dev/vdb
    devid    2 size 20.00GiB used 264.00MiB path /dev/vdc
    devid    3 size 20.00GiB used 0.00B path /dev/vdd

刚添加的磁盘是没有任何数据的,为了能够更加均衡,可以使用 btrfs balance start 命令来使得数据的平衡:

[root@fedora39 ~]# btrfs balance start --full-balance /mydata/
Done, had to relocate 3 out of 3 chunks
[root@fedora39 ~]# btrfs filesystem show /mydata
Label: 'mydata'  uuid: 0c1ea6df-f061-4f32-8801-d20f8191ca48
    Total devices 3 FS bytes used 144.00KiB
    devid    1 size 20.00GiB used 1.03GiB path /dev/vdb
    devid    2 size 20.00GiB used 256.00MiB path /dev/vdc
    devid    3 size 20.00GiB used 288.00MiB path /dev/vdd

从文件系统中移除设备

使用 btrfs device delete 命令从文件系统中移除特定的磁盘设备:

[root@fedora39 ~]# btrfs device delete /dev/vdb /mydata/
[root@fedora39 ~]# btrfs filesystem show /mydata/
Label: 'mydata'  uuid: 0c1ea6df-f061-4f32-8801-d20f8191ca48
    Total devices 2 FS bytes used 144.00KiB
    devid    2 size 20.00GiB used 1.28GiB path /dev/vdc
    devid    3 size 20.00GiB used 288.00MiB path /dev/vdd

修改数据存储模式

当创建文件系统时,不指定任何存储模式,默认的存储模式如下:

[root@fedora39 ~]# btrfs filesystem df /mydata/ | column -t
Data,           single:  total=1.00GiB,    used=0.00B
System,         RAID1:   total=32.00MiB,   used=16.00KiB
Metadata,       RAID1:   total=256.00MiB,  used=128.00KiB
GlobalReserve,  single:  total=5.50MiB,    used=0.00B

如果需要将 Data 的存储方式设置为 raid1 Metadata 的存储方式设置为 raid5,可以使用 btrfs balance start 命令进行操作:

[root@fedora39 ~]# btrfs balance start -mconvert=raid5 /mydata
Starting conversion to RAID5/6.
Done, had to relocate 2 out of 3 chunks
[root@fedora39 ~]# btrfs balance start -dconvert=raid1 /mydata
Done, had to relocate 1 out of 3 chunks
[root@fedora39 ~]# btrfs filesystem df /mydata/ | column -t
Data,           RAID1:   total=2.00GiB,    used=0.00B
System,         RAID5:   total=32.00MiB,   used=16.00KiB
Metadata,       RAID5:   total=256.00MiB,  used=128.00KiB
GlobalReserve,  single:  total=5.50MiB,    used=0.00B

-mconvert 修改 Metadata 存储方式,-dconvert 修改 Data 存储方式。

子卷

Btrfs 文件系统拥有子卷的功能,子卷类似于逻辑卷,可以单独创建并将子卷挂载到特定的挂载点上。

创建子卷

使用 btrfs subvolume create 命令创建子卷:

[root@fedora39 ~]# btrfs subvolume create /mydata/logs
Create subvolume '/mydata/logs'
[root@fedora39 ~]# btrfs subvolume create /mydata/cache
Create subvolume '/mydata/cache'
[root@fedora39 ~]# btrfs subvolume list /mydata
ID 256 gen 7 top level 5 path logs
ID 257 gen 9 top level 5 path cache
[root@fedora39 ~]# ls /mydata/
cache  logs

使用 subvolsubvolid 选项挂载子卷:

[root@fedora39 ~]# umount /mydata/
[root@fedora39 ~]# mkdir /mnt/{logs,cache}
[root@fedora39 ~]# mount -o subvol=logs /dev/vdb /mnt/logs
[root@fedora39 ~]# mount -o subvol=cache /dev/vdb /mnt/cache
[root@fedora39 ~]# df -h | grep mnt
/dev/vdb                  60G  5.9M   58G   1% /mnt/logs
/dev/vdb                  60G  5.9M   58G   1% /mnt/cache

查看子卷的信息:

[root@fedora39 ~]# btrfs subvolume show /mnt/logs
logs
    Name:           logs
    UUID:           927c09ec-1a12-f64c-b2f4-d0a97730a5c0
    Parent UUID:        -
    Received UUID:      -
    Creation time:      2024-04-02 13:34:21 +0000
    Subvolume ID:       256
    Generation:         7
    Gen at creation:    7
    Parent ID:      5
    Top level ID:       5
    Flags:          -
    Send transid:       0
    Send time:      2024-04-02 13:34:21 +0000
    Receive transid:    0
    Receive time:       -
    Snapshot(s):
    Quota group:        n/a

删除子卷

子卷删除前,需要卸载,然后通过 btrfs subvolume delete 从父卷中删除子卷:

[root@fedora39 ~]# mount /dev/vdb /mydata/
[root@fedora39 ~]# umount /mnt/logs/

[root@fedora39 ~]# btrfs subvolume delete /mydata/logs/
Delete subvolume (no-commit): '/mydata/logs'
[root@fedora39 ~]# btrfs subvolume list /mydata/
ID 257 gen 9 top level 5 path cache

子卷快照

子卷的快照可以通过 btrfs subvolume snapshot 进行管理。

创建快照:

[root@fedora39 ~]# cp /var/log/dnf.log  /mnt/logs/
[root@fedora39 ~]# btrfs subvolume snapshot /mydata/logs/ /mydata/logs_snapshot
Create a snapshot of '/mydata/logs/' in '/mydata/logs_snapshot'
[root@fedora39 ~]# diff /mnt/logs/dnf.log /mydata/logs_snapshot/dnf.log

恢复快照可以从快照的子卷中将需要恢复的文件拷贝回子卷即可,这种方式比较简陋,也可以使用一些快照管理工具来进行快照的恢复和管理,这个留到后面有时间再去研究。

列出快照:

[root@fedora39 ~]# btrfs subvolume show /mydata/logs
logs
    Name:           logs
    UUID:           bf5df43e-74e4-9b48-9846-8b1d8175c797
    Parent UUID:        -
    Received UUID:      -
    Creation time:      2024-04-02 13:43:50 +0000
    Subvolume ID:       258
    Generation:         15
    Gen at creation:    12
    Parent ID:      5
    Top level ID:       5
    Flags:          -
    Send transid:       0
    Send time:      2024-04-02 13:43:50 +0000
    Receive transid:    0
    Receive time:       -
    Snapshot(s):
                logs_snapshot
    Quota group:        n/a

删除快照(其实等同于删除子卷):

[root@fedora39 ~]# btrfs subvolume delete /mydata/logs_snapshot/
Delete subvolume (no-commit): '/mydata/logs_snapshot

文件系统的转换

可以通过 btrfs-convert 对现有的非 Btrfs 文件系统转换为 Btrfs 文件系统,也可以将 Btrfs 文件系统转成其它文件系统。

从其它文件系统转换为 Btrfs 文件系统

使用 btrfs-convert 进行转换,转换前需要将文件系统进行卸载:

[root@fedora39 ~]# mkfs.ext4 /dev/vdd
mke2fs 1.47.0 (5-Feb-2023)
Discarding device blocks: done                            
Creating filesystem with 5242880 4k blocks and 1310720 inodes
Filesystem UUID: 0be4568b-78de-4d9c-b45b-ef9eb1df3d1c
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
    4096000

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done   

[root@fedora39 ~]# btrfs-convert /dev/vdd
btrfs-convert from btrfs-progs v6.6.2

Source filesystem:
  Type:           ext2
  Label:          
  Blocksize:      4096
  UUID:           0be4568b-78de-4d9c-b45b-ef9eb1df3d1c
Target filesystem:
  Label:          
  Blocksize:      4096
  Nodesize:       16384
  UUID:           6a1b1a92-63b4-4201-ae1a-804961f96bb9
  Checksum:       crc32c
  Features:       extref, skinny-metadata, no-holes, free-space-tree (default)
    Data csum:    yes
    Inline data:  yes
    Copy xattr:   yes
Reported stats:
  Total space:     21474836480
  Free space:      20691091456 (96.35%)
  Inode count:         1310720
  Free inodes:         1310708
  Block count:         5242880
Create initial btrfs filesystem
Create ext2 image file
Create btrfs metadata
Copy inodes [o] [         1/        12]
Free space cache cleared
Conversion complete

[root@fedora39 ~]# btrfs filesystem show
Label: 'mydata'  uuid: a18461d0-1a21-4f47-a6a1-d466ec4773f3
    Total devices 2 FS bytes used 176.00KiB
    devid    1 size 20.00GiB used 1.04GiB path /dev/vdb
    devid    2 size 20.00GiB used 1.03GiB path /dev/vdc

Label: none  uuid: 6a1b1a92-63b4-4201-ae1a-804961f96bb9
    Total devices 1 FS bytes used 496.21MiB
    devid    1 size 20.00GiB used 782.30MiB path /dev/vdd

从 Btrfs 文件系统转换为原始文件系统

使用 btrfs-convert -r 进行恢复,-r 代表 rollback 的意思:

[root@fedora39 ~]# btrfs-convert -r /dev/vdd
btrfs-convert from btrfs-progs v6.6.2

Open filesystem for rollback:
  Label:           
  UUID:            6a1b1a92-63b4-4201-ae1a-804961f96bb9
  Restoring from:  ext2_saved/image
Rollback succeeded

[root@fedora39 ~]# file -s /dev/vdd
/dev/vdd: Linux rev 1.0 ext4 filesystem data, UUID=0be4568b-78de-4d9c-b45b-ef9eb1df3d1c (extents) (64bit) (large files) (huge files)

发表评论

您的邮箱地址不会被公开。 必填项已用 * 标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据

滚动至顶部