1.调整分区大小以使用原始磁盘上的可用空间

  1. 如果磁盘大于 2 TB,请使用 fdisk 或者 parted 验证磁盘空间和当前分区的大小:
# fdisk -l /dev/xvda
Disk /dev/xvda: 53.7 GB, 53687091200 bytes, 104857600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x00087895
Device Boot Start End Blocks Id System
/dev/xvda1 * 2048 1030143 514048 83 Linux
/dev/xvda2 1030144 9418751 4194304 82 Linux swap / Solaris
/dev/xvda3 9418752 31457279 11019264 83 Linux
[root@host0 ~]# lsblk -f /dev/xvda
NAME FSTYPE LABEL UUID MOUNTPOINT
xvda
├─xvda1 xfs /boot 049e257e-25b1-4f46-80aa-ffaa27a21022 /boot
├─xvda2 swap SWAP-VM 0406d7df-68b6-47cb-8ea9-195ac7ef7497 [SWAP]
└─xvda3 btrfs btr_pool 1e72a30b-d59d-458f-b04f-59bf8bdec35d /
  1. fdisk 不支持调整分区大小,所以你需要删除你想要更改的旧分区并创建一个新分区。
# fdisk /dev/xvda
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.
Command (m for help): p
Disk /dev/xvda: 53.7 GB, 53687091200 bytes, 104857600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x00087895
Device Boot Start End Blocks Id System
/dev/xvda1 * 2048 1030143 514048 83 Linux
/dev/xvda2 1030144 9418751 4194304 82 Linux swap / Solaris
/dev/xvda3 9418752 31457279 11019264 83 Linux
Command (m for help): d
Partition number (1-3, default 3): 3
Partition 3 is deleted
  1. 现在让我们创建一个新分区。
Command (m for help): n
Partition type:
p primary (2 primary, 0 extended, 2 free)
e extended
Select (default p): p
Partition number (3,4, default 3): 3
First sector (9418752-104857599, default 9418752):
Using default value 9418752
Last sector, +sectors or +size{K,M,G} (9418752-104857599, default 104857599):
Using default value 104857599
Partition 3 of type Linux and of size 45.5 GiB is set
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
  1. 使用“fdisk -l”验证新创建的分区。
# fdisk -l /dev/xvda
Disk /dev/xvda: 53.7 GB, 53687091200 bytes, 104857600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x00087895
Device Boot Start End Blocks Id System
/dev/xvda1 * 2048 1030143 514048 83 Linux
/dev/xvda2 1030144 9418751 4194304 82 Linux swap / Solaris
/dev/xvda3 9418752 104857599 47719424 83 Linux
  1. 我们需要让 Linux 内核知道我们使用“partprobe”命令对 /dev/xvda 所做的更改:
# partprobe
Error: Partition(s) 3 on /dev/xvda have been written, but we have been unable to inform the kernel of the change, probably because it/they are in use. As a result, the old partition(s) will remain in use. You should reboot now before making further changes.
  1. 由于正在使用的是根文件系统,因此我们必须重新启动操作系统才能让内核看到更改。
    如果它不是根文件系统,则可以跳过此步骤。
# shutdown -r now

扩展 btfrs 文件系统

  1. 现在可以展开btrfs根文件系统了。
# btrfs filesystem resize +10g /
Resize '/' of '+10g'
  1. 在“df -h”命令输出中验证新的文件系统大小。
# df -h /
Filesystem    Size    Used     Avail    Use%     Mounted on
/dev/xvda3    21G     2.5G     17G      14%      /

如果需要,我们也可以使用磁盘上的所有空间。
将选项“max”与“btrfs filesystem resize”命令一起使用。

# btrfs filesystem resize max /
Resize '/' of 'max'
# df -h /
Filesystem    Size    Used     Avail    Use%     Mounted on
/dev/xvda3    46G     2.5G     42G      6%      /
如何调整/扩展 Btrfs 卷/文件系统的大小

由于 btrfs 在 CentOS/RHEL 7 中广泛使用,并且也作为 Docker 的后端存储驱动程序,因此调整/扩展 btrfs 文件系统的大小是一项常见任务。
这篇文章说明了扩展 btrfs 根卷的 2 个常见程序:

  • 扩展以使用原始磁盘上的可用空间
  • 将新磁盘添加到同一个 btrfs 卷中

注意:由于在 btrfs 平衡操作期间更改现有分区或者意外断电时可能出现错误,这些过程存在风险,请确保数据/系统具有最新的可用备份。

on it road .com

2. 将新磁盘添加到 btrfs 卷

  1. 如果我们正在运行虚拟机,我们可以通过提供新 LUN 或者添加新虚拟磁盘向系统添加新磁盘。
    我们可能需要运行以下脚本来扫描新的 LUN/磁盘。
# rescan-scsi-bus.sh -a

或者重新启动系统以使新磁盘对操作系统可见,

  1. 使用命令“sblk -f”或者“fdisk -l”验证操作系统是否可以看到新磁盘。
    例如,/dev/xvdb 是本文中示例的新磁盘。
# lsblk -f
NAME FSTYPE LABEL UUID MOUNTPOINT
xvda
├─xvda1 xfs /boot 049e257e-25b1-4f46-80aa-ffaa27a21022 /boot
├─xvda2 swap SWAP-VM 0406d7df-68b6-47cb-8ea9-195ac7ef7497 [SWAP]
└─xvda3 btrfs btr_pool 1e72a30b-d59d-458f-b04f-59bf8bdec35d /
xvdb
# fdisk -l /dev/xvdb
Disk /dev/xvdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
  1. 将新磁盘 /dev/xvdb 添加到根卷
# btrfs device add /dev/xvdb /
  1. 将元数据从第一个磁盘 /dev/xvda 分发到第二个磁盘 /dev/xvdb。
# btrfs filesystem balance /
WARNING:
Full balance without filters requested. This operation is very
intense and takes potentially very long. It is recommended to
use the balance filters to narrow down the balanced data.
Use 'btrfs balance start --full-balance' option to skip this
warning. The operation will start in 10 seconds.
Use Ctrl-C to stop it.
10 9 8 7 6 5 4 3 2 1
Starting balance without any filters.
Done, had to relocate 9 out of 9 chunks
  1. 使用“df -h”验证文件系统的新大小。
# df -h /
Filesystem    Size    Used    Avail    Use%    Mounted on
/dev/xvda3    56G     2.5G    52G      5%      /

我们可以看到根文件系统又增加了 10G,即 /dev/xvdb 的大小。

日期:2020-09-17 00:13:37 来源:oir作者:oir