Please enable Javascript to view the contents

RAID 下 Btrfs 运维操作

 ·  ☕ 7 分钟

Btrfs 支持在线扩容、缩容、换盘与 RAID 模式转换;本篇主要介绍在 RAID 0/RAID 1/RAID 10/RAID 6 拓扑下创建文件系统、加盘、换盘、缩容及常用运维操作。

1. Btrfs 配置

  • single:单副本,无冗余,单盘或可多盘聚合。
  • raid0:条带,无冗余,等价 RAID 0。
  • raid1:镜像,至少 2 块盘,等价 RAID 1。
  • raid10:条带 + 镜像,至少 4 块盘,等价 RAID 10。
  • raid5:单校验,至少 3 块盘,允许 1 块盘故障,等价 RAID 5。
  • raid6:双校验,至少 4 块盘,允许 2 块盘故障,等价 RAID 6。

数据与元数据可分别设置配置档(如 -d raid10 -m raid1),元数据一般用更高冗余。

常用概念:

  • degraded:降级,有设备缺失但可读(甚至可写,取决于 profile)。
  • balance:在设备间重新分布数据块,以满足新 profile 或新设备;加盘、改 RAID 后常需 balance。
  • replace:在线用新盘替换旧盘,无需卸载,数据会拷贝到新盘后移除旧盘。

2. RAID 0

2.1 创建 Btrfs 文件系统

  • 使用多块盘创建 RAID 0(数据与元数据均为 raid0)
1
mkfs.btrfs -d raid0 -m raid0 -f /dev/nvme0n1 /dev/nvme1n1
  • 挂载

可只指定一块盘,会识别到其他盘。

1
2
mkdir -p /mnt/btrfs
mount /dev/nvme0n1 /mnt/btrfs
  • 查看设备使用情况
1
btrfs device usage /mnt/btrfs
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
/dev/nvme0n1, ID: 1
   Device size:           745.21GiB
   Device slack:              0.00B
   Data,RAID 0/2:            1.00GiB
   Metadata,RAID 0/2:      512.00MiB
   System,RAID 0/2:          8.00MiB
   Unallocated:           743.70GiB

/dev/nvme1n1, ID: 2
   Device size:           745.21GiB
   Device slack:              0.00B
   Data,RAID 0/2:            1.00GiB
   Metadata,RAID 0/2:      512.00MiB
   System,RAID 0/2:          8.00MiB
   Unallocated:           743.70GiB
  • 查看文件系统使用情况
1
btrfs filesystem df /mnt/btrfs
1
2
3
4
Data, RAID 0: total=2.00GiB, used=0.00B
System, RAID 0: total=16.00MiB, used=16.00KiB
Metadata, RAID 0: total=1.00GiB, used=208.00KiB
GlobalReserve, single: total=3.25MiB, used=0.00B
  • 查看空间
1
df -h /mnt/btrfs
1
2
Filesystem      Size  Used Avail Use% Mounted on
/dev/nvme0n1    1.5T  3.5M  1.5T   1% /mnt/btrfs

两块盘下,raid0 做条带,得盘率 100%。

2.2 加盘扩容

  • 将新盘加入已挂载的文件系统
1
btrfs device add -f /dev/nvme2n1 /mnt/btrfs

-f 会强制覆盖已有的文件系统。

  • 查看设备使用情况
1
btrfs device usage /mnt/btrfs
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
/dev/nvme0n1, ID: 1
   Device size:           745.21GiB
   Device slack:              0.00B
   Data,RAID 0/2:           25.00GiB
   Metadata,RAID 0/2:      512.00MiB
   System,RAID 0/2:          8.00MiB
   Unallocated:           719.70GiB

/dev/nvme1n1, ID: 2
   Device size:           745.21GiB
   Device slack:              0.00B
   Data,RAID 0/2:           25.00GiB
   Metadata,RAID 0/2:      512.00MiB
   System,RAID 0/2:          8.00MiB
   Unallocated:           719.70GiB

/dev/nvme2n1, ID: 3
   Device size:           745.21GiB
   Device slack:              0.00B
   Unallocated:           745.21GiB
  • 执行 balance,使数据按 raid0 分布到所有设备
1
btrfs balance start -dconvert=raid0 -mconvert=raid0 /mnt/btrfs

命令会 hang 住,需要等待 balance 完成。可以另外开一个终端查看进度。

  • 查看 balance 进度
1
btrfs balance status /mnt/btrfs
1
10 out of about 27 chunks balanced (11 considered),  63% left
  • 查看设备使用情况
1
btrfs device usage /mnt/btrfs
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/dev/nvme0n1, ID: 1
   Device size:           745.21GiB
   Device slack:              0.00B
   Data,RAID 0/3:           17.00GiB
   Metadata,RAID 0/3:      352.00MiB
   System,RAID 0/3:         32.00MiB
   Unallocated:           727.84GiB

/dev/nvme1n1, ID: 2
   Device size:           745.21GiB
   Device slack:              0.00B
   Data,RAID 0/3:           17.00GiB
   Metadata,RAID 0/3:      352.00MiB
   System,RAID 0/3:         32.00MiB
   Unallocated:           727.84GiB

/dev/nvme2n1, ID: 3
   Device size:           745.21GiB
   Device slack:              0.00B
   Data,RAID 0/3:           17.00GiB
   Metadata,RAID 0/3:      352.00MiB
   System,RAID 0/3:         32.00MiB
   Unallocated:           727.84GiB

2.3 减盘缩容

raid0 无冗余,缩容需保证目标盘数下其他盘有足够空间容纳被移走盘上的数据。

  • 移除一块盘

数据会自动迁到其余盘

1
btrfs device remove /dev/nvme2n1 /mnt/btrfs
  • 查看设备列表
1
btrfs device usage /mnt/btrfs
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
/dev/nvme0n1, ID: 1
   Device size:           745.21GiB
   Device slack:              0.00B
   Data,RAID 0/2:           25.00GiB
   Metadata,RAID 0/2:      512.00MiB
   System,RAID 0/2:          8.00MiB
   Unallocated:           719.70GiB

/dev/nvme1n1, ID: 2
   Device size:           745.21GiB
   Device slack:              0.00B
   Data,RAID 0/2:           25.00GiB
   Metadata,RAID 0/2:      512.00MiB
   System,RAID 0/2:          8.00MiB
   Unallocated:           719.70GiB

3. RAID 1

3.1 创建 Btrfs 文件系统

  • 移除已有的文件系统
1
umount /mnt/btrfs
  • 使用 2 块盘创建 RAID 1
1
mkfs.btrfs -d raid1 -m raid1 -f /dev/nvme0n1 /dev/nvme1n1
  • 挂载
1
2
mkdir -p /mnt/btrfs
mount /dev/nvme0n1 /mnt/btrfs
  • 查看设备使用情况
1
btrfs device usage /mnt/btrfs
  • 查看空间
1
df -h /mnt/btrfs
1
2
Filesystem      Size  Used Avail Use% Mounted on
/dev/nvme0n1    746G  3.5M  745G   1% /mnt/btrfs

两块盘下,raid1 做镜像,得盘率 50%。

3.2 单盘替换

  • 清空新盘
1
wipefs -a /dev/nvme2n1
  • 用新盘替换旧盘
1
btrfs replace start /dev/nvme1n1 /dev/nvme2n1 /mnt/btrfs
  • 查看替换进度
1
btrfs replace status /mnt/btrfs
1
6.7% done, 0 write errs, 0 uncorr. read errs

替换期间文件系统可正常读写。替换完成后,旧盘会从文件系统中移除。

  • 若新盘比旧盘大,新空间会自动纳入可用空间
1
btrfs device usage /mnt/btrfs
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
/dev/nvme0n1, ID: 1
   Device size:           745.21GiB
   Device slack:              0.00B
   Data,RAID 1:             51.00GiB
   Metadata,RAID 1:          1.00GiB
   System,RAID 1:           32.00MiB
   Unallocated:           693.18GiB

/dev/nvme2n1, ID: 2
   Device size:           745.21GiB
   Device slack:              0.00B
   Data,RAID 1:             51.00GiB
   Metadata,RAID 1:          1.00GiB
   System,RAID 1:           32.00MiB
   Unallocated:           693.18GiB

3.3 加盘扩容

  • 清空新盘
1
2
wipefs -a /dev/nvme3n1
wipefs -a /dev/nvme4n1
  • 加两块盘后可转为 raid10
1
btrfs device add /dev/nvme3n1 /dev/nvme4n1 /mnt/btrfs
  • 查看设备使用情况
1
btrfs device usage /mnt/btrfs
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/dev/nvme0n1, ID: 1
   Device size:           745.21GiB
   Device slack:              0.00B
   Data,RAID 1:             51.00GiB
   Metadata,RAID 1:          1.00GiB
   System,RAID 1:           32.00MiB
   Unallocated:           693.18GiB

/dev/nvme2n1, ID: 2
   Device size:           745.21GiB
   Device slack:              0.00B
   Data,RAID 1:             51.00GiB
   Metadata,RAID 1:          1.00GiB
   System,RAID 1:           32.00MiB
   Unallocated:           693.18GiB

/dev/nvme3n1, ID: 3
   Device size:           745.21GiB
   Device slack:              0.00B
   Unallocated:           745.21GiB

/dev/nvme4n1, ID: 4
   Device size:           745.21GiB
   Device slack:              0.00B
   Unallocated:           745.21GiB
  • 再 balance 为 raid10
1
btrfs balance start -dconvert=raid10 -mconvert=raid10 /mnt/btrfs

命令会 hang 住,需要等待 balance 完成。可以另外开一个终端查看进度。

  • 查看 balance 进度
1
btrfs balance status /mnt/btrfs
1
34 out of about 53 chunks balanced (35 considered),  36% left
  • 查看设备使用情况
1
btrfs device usage /mnt/btrfs
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/dev/nvme0n1, ID: 1
   Device size:           745.21GiB
   Device slack:              0.00B
   Data,RAID 10/4:          51.00GiB
   Metadata,RAID 10/4:       1.00GiB
   System,RAID 10/4:        32.00MiB
   Unallocated:           693.18GiB

/dev/nvme2n1, ID: 2
   Device size:           745.21GiB
   Device slack:              0.00B
   Data,RAID 10/4:          51.00GiB
   Metadata,RAID 10/4:       1.00GiB
   System,RAID 10/4:        32.00MiB
   Unallocated:           693.18GiB

/dev/nvme3n1, ID: 3
   Device size:           745.21GiB
   Device slack:              0.00B
   Data,RAID 10/4:          51.00GiB
   Metadata,RAID 10/4:       1.00GiB
   System,RAID 10/4:        32.00MiB
   Unallocated:           693.18GiB

/dev/nvme4n1, ID: 4
   Device size:           745.21GiB
   Device slack:              0.00B
   Data,RAID 10/4:          51.00GiB
   Metadata,RAID 10/4:       1.00GiB
   System,RAID 10/4:        32.00MiB
   Unallocated:           693.18GiB

3.4 减盘缩容

  • 移除一块盘
1
btrfs device remove /dev/nvme4n1 /mnt/btrfs
  • 查看设备
1
btrfs device usage /mnt/btrfs
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
/dev/nvme0n1, ID: 1
   Device size:           745.21GiB
   Device slack:              0.00B
   Data,RAID 10/2:          34.00GiB
   Metadata,RAID 10/2:       1.00GiB
   System,RAID 10/2:        32.00MiB
   Unallocated:           710.18GiB

/dev/nvme2n1, ID: 2
   Device size:           745.21GiB
   Device slack:              0.00B
   Data,RAID 10/2:          34.00GiB
   Metadata,RAID 10/2:       1.00GiB
   Unallocated:           710.21GiB

/dev/nvme3n1, ID: 3
   Device size:           745.21GiB
   Device slack:              0.00B
   Data,RAID 10/2:          34.00GiB
   System,RAID 10/2:        32.00MiB
   Unallocated:           711.18GiB

4. RAID 10

4.1 创建 Btrfs 文件系统

  • 移除已有的文件系统
1
umount /mnt/btrfs
  • 使用 4 块盘创建 RAID 10
1
mkfs.btrfs -d raid10 -m raid10 -f /dev/nvme0n1 /dev/nvme1n1 /dev/nvme2n1 /dev/nvme3n1

-f 会强制覆盖已有的文件系统。

  • 挂载
1
2
mkdir -p /mnt/btrfs
mount /dev/nvme0n1 /mnt/btrfs
  • 查看设备使用情况
1
btrfs device usage /mnt/btrfs
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
/dev/nvme0n1, ID: 1
   Data,RAID 10/4:           1.00GiB
   Metadata,RAID 10/4:     256.00MiB

/dev/nvme1n1, ID: 2
   Data,RAID 10/4:           1.00GiB
   Metadata,RAID 10/4:     256.00MiB

/dev/nvme2n1, ID: 3
   Data,RAID 10/4:           1.00GiB
   Metadata,RAID 10/4:     256.00MiB

/dev/nvme3n1, ID: 4
   Data,RAID 10/4:           1.00GiB
   Metadata,RAID 10/4:     256.00MiB
  • 查看空间
1
df -h /mnt/btrfs
1
2
Filesystem      Size  Used Avail Use% Mounted on
/dev/nvme0n1    1.5T  3.5M  1.5T   1% /mnt/btrfs

4 块盘下,raid10 为条带+镜像,得盘率 50%。

4.2 单盘替换

  • 清空旧盘
1
wipefs -a /dev/nvme4n1
  • 用新盘替换旧盘
1
btrfs replace start /dev/nvme3n1 /dev/nvme4n1 /mnt/btrfs
  • 查看替换进度
1
btrfs replace status /mnt/btrfs

替换期间文件系统可正常使用。完成后旧盘自动移除,若新盘更大,空间自动扩展。

  • 查看设备使用情况
1
btrfs device usage /mnt/btrfs
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
/dev/nvme0n1, ID: 1
   Data,RAID 10/4:          27.00GiB
   Metadata,RAID 10/4:     256.00MiB

/dev/nvme1n1, ID: 2
   Data,RAID 10/4:          27.00GiB
   Metadata,RAID 10/4:     256.00MiB

/dev/nvme2n1, ID: 3
   Data,RAID 10/4:          27.00GiB
   Metadata,RAID 10/4:     256.00MiB

/dev/nvme4n1, ID: 4
   Data,RAID 10/4:          27.00GiB
   Metadata,RAID 10/4:     256.00MiB

4.3 加盘扩容

  • 清空新盘
1
wipefs -a /dev/nvme5n1
  • 将新盘加入文件系统
1
btrfs device add /dev/nvme5n1 /mnt/btrfs
  • balance 数据到新盘
1
btrfs balance start -dconvert=raid10 -mconvert=raid10 /mnt/btrfs
  • 查看 balance 进度
1
btrfs balance status /mnt/btrfs
  • 查看设备使用情况
1
btrfs device usage /mnt/btrfs
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
/dev/nvme0n1, ID: 1
   Data,RAID 10/4:          21.00GiB
   Metadata,RAID 10/4:     512.00MiB

/dev/nvme1n1, ID: 2
   Data,RAID 10/4:          20.00GiB
   Metadata,RAID 10/4:     512.00MiB

/dev/nvme2n1, ID: 3
   Data,RAID 10/4:          20.00GiB
   Metadata,RAID 10/4:     512.00MiB

/dev/nvme4n1, ID: 4
   Data,RAID 10/4:          21.00GiB
   Metadata,RAID 10/4:     512.00MiB

/dev/nvme5n1, ID: 5
   Data,RAID 10/4:          22.00GiB
   Metadata,RAID 10/4:     512.00MiB

4.4 减盘缩容

需要保障移除盘之后,盘的数量不低于 RAID 的最低要求,并且剩余盘空间足够容纳被移走盘上的数据。

  • 移除一块盘
1
btrfs device remove /dev/nvme0n1 /mnt/btrfs
  • 查看设备列表
1
btrfs device usage /mnt/btrfs
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
/dev/nvme1n1, ID: 2
   Data,RAID 10/4:          26.00GiB
   Metadata,RAID 10/4:     512.00MiB

/dev/nvme2n1, ID: 3
   Data,RAID 10/4:          26.00GiB
   Metadata,RAID 10/4:     512.00MiB

/dev/nvme4n1, ID: 4
   Data,RAID 10/4:          26.00GiB
   Metadata,RAID 10/4:     512.00MiB

/dev/nvme5n1, ID: 5
   Data,RAID 10/4:          26.00GiB
   Metadata,RAID 10/4:     512.00MiB

5. RAID 6

5.1 创建 Btrfs 文件系统

  • 移除已有的文件系统
1
umount /mnt/btrfs
  • 创建 RAID 6

至少 4 块盘。

1
mkfs.btrfs -d raid6 -m raid6 -f /dev/nvme0n1 /dev/nvme1n1 /dev/nvme2n1 /dev/nvme3n1
  • 挂载
1
2
mkdir -p /mnt/btrfs
mount /dev/nvme0n1 /mnt/btrfs
  • 查看设备与配置
1
btrfs device usage /mnt/btrfs
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
/dev/nvme0n1, ID: 1
   Data,RAID 6/4:            1.00GiB
   Metadata,RAID 6/4:      256.00MiB

/dev/nvme1n1, ID: 2
   Data,RAID 6/4:            1.00GiB
   Metadata,RAID 6/4:      256.00MiB

/dev/nvme2n1, ID: 3
   Data,RAID 6/4:            1.00GiB
   Metadata,RAID 6/4:      256.00MiB

/dev/nvme3n1, ID: 4
   Data,RAID 6/4:            1.00GiB
   Metadata,RAID 6/4:      256.00MiB
  • 查看空间
1
df -h /mnt/btrfs
1
/dev/nvme0n1    3.0T  3.5M  1.5T   1% /mnt/btrfs

4 块盘 RAID 6 得盘率 50%(2 数据 + 2 校验)。

5.2 单盘替换

  • 清空旧盘
1
wipefs -a /dev/nvme4n1
  • 用新盘替换旧盘
1
btrfs replace start /dev/nvme3n1 /dev/nvme4n1 /mnt/btrfs
  • 查看进度
1
btrfs replace status /mnt/btrfs

等待完成。

  • 查看设备使用情况
1
btrfs device usage /mnt/btrfs
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
/dev/nvme0n1, ID: 1
   Data,RAID 6/4:           26.00GiB
   Metadata,RAID 6/4:      256.00MiB

/dev/nvme1n1, ID: 2
   Data,RAID 6/4:           26.00GiB
   Metadata,RAID 6/4:      256.00MiB

/dev/nvme2n1, ID: 3
   Data,RAID 6/4:           26.00GiB
   Metadata,RAID 6/4:      256.00MiB

/dev/nvme4n1, ID: 4
   Data,RAID 6/4:           26.00GiB
   Metadata,RAID 6/4:      256.00MiB

5.3 加盘扩容

  • 添加新盘
1
btrfs device add -f /dev/nvme5n1 /mnt/btrfs
  • balance 数据到新盘
1
btrfs balance start -dconvert=raid6 -mconvert=raid6 /mnt/btrfs
1
2
3
4
5
6
7
WARNING:

        RAID 5/6 support has known problems and is strongly discouraged
        to be used besides testing or evaluation. It is recommended that
        you use one of the other RAID profiles.
        The operation will continue in 10 seconds.
        Use Ctrl-C to stop.

会出现一个警告,提示 RAID 5/6 不建议用于生产环境,数据不可靠。

  • 查看设备使用情况
1
btrfs device usage /mnt/btrfs
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
/dev/nvme0n1, ID: 1
   Data,RAID 6/5:           18.00GiB
   Metadata,RAID 6/5:      352.00MiB

/dev/nvme1n1, ID: 2
   Data,RAID 6/5:           18.00GiB
   Metadata,RAID 6/5:      352.00MiB

/dev/nvme2n1, ID: 3
   Data,RAID 6/5:           18.00GiB
   Metadata,RAID 6/5:      352.00MiB

/dev/nvme4n1, ID: 4
   Data,RAID 6/5:           18.00GiB
   Metadata,RAID 6/5:      352.00MiB

/dev/nvme5n1, ID: 5
   Data,RAID 6/5:           18.00GiB
   Metadata,RAID 6/5:      352.00MiB

5.4 减盘缩容

  • 移除一块盘
1
btrfs device remove /dev/nvme0n1 /mnt/btrfs

移除设备时,没有出现 warning 提示。

  • 查看设备使用情况
1
btrfs device usage /mnt/btrfs
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
/dev/nvme1n1, ID: 2
   Data,RAID 6/4:           26.00GiB
   Metadata,RAID 6/4:      512.00MiB

/dev/nvme2n1, ID: 3
   Data,RAID 6/4:           26.00GiB
   Metadata,RAID 6/4:      512.00MiB

/dev/nvme4n1, ID: 4
   Data,RAID 6/4:           26.00GiB
   Metadata,RAID 6/4:      512.00MiB

/dev/nvme5n1, ID: 5
   Data,RAID 6/4:           26.00GiB
   Metadata,RAID 6/4:      512.00MiB

6. 降级与恢复

  • 某块盘故障或被拔出时,文件系统可能处于 degraded 状态,仍可读(甚至可写,视 profile 而定)。
  • 查看状态
1
2
btrfs device usage /mnt/btrfs
btrfs filesystem show /mnt/btrfs
  • 恢复方式:
    • 换盘:用 btrfs replace start 旧盘 新盘 /mnt/btrfs 在线替换。
    • 移除盘:若不再使用该盘,可用 btrfs device remove 缺失设备(需在能识别到“缺失”的情况下,或先 btrfs device add 新盘再 balance/remove)。
  • 替换或 balance 期间建议避免再掉盘,以加快恢复。

7. 备份与恢复

7.1 快照与恢复

  • 创建快照
1
btrfs subvolume snapshot /mnt/btrfs /mnt/btrfs/snap_test

创建的快照只包含元数据。

  • 查看快照文件
1
ls /mnt/btrfs/snap_test

非常易用,快照直接以文件目录的形式可以被访问。

  • 删除快照
1
btrfs subvolume delete /mnt/btrfs/snap_test

7.2 数据校验

  • 校验文件系统数据
1
btrfs scrub start /mnt/btrfs
  • 查看校验进度
1
btrfs scrub status /mnt/btrfs
1
2
Bytes scrubbed:   7.84GiB  (15.62%)
Rate:             1.57GiB/s

建议定期执行校验,发现静默错误(位翻转了而没有被发现)。

8. 生成测试数据

  • 4M 顺序写
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
fio \
  --name=4m_seq_write \
  --rw=write \
  --bs=4M \
  --ioengine=libaio \
  --direct=1 \
  --numjobs=1 \
  --iodepth=8 \
  --size=50G \
  --time_based \
  --runtime=60 \
  --group_reporting \
  --directory=/mnt/btrfs

用于生成测试数据。

  • 计算文件的 MD5 值
1
md5sum /mnt/btrfs/4m_seq_write.0.0

用于校验调整之后文件的完整性。


微信公众号
作者
微信公众号