如何使用 Parted 扩展最后一个存在的文件系统分区

本文概述了扩展磁盘最后一个分区及其上的文件系统的步骤。
请记住,您只能调整设备上最后一个分区的大小。

如果这不是最后一个分区,我们将需要备份数据,重建磁盘和分区,然后恢复数据。
这篇文章假设我们使用 GPT 分区表或者使用主分区类型的 msdos 分区表。

注意:存储调整大小命令很危险,可能会导致数据完全丢失。
在尝试按照本文中的步骤操作之前,请先运行备份。

  1. 使用 df 命令检查当前文件系统的大小:
# df -h /test
Filesystem   Size   Used   Avail   Use%   Mounted on
/dev/xvdc1   9.1G   84M    8.5G    1%     /test
  1. 卸载文件系统
# umount /test
  1. 检查parted最后一个分区大小,我们将看到开始和结束扇区:
# parted /dev/xvdc u s p
Model: Xen Virtual Block Device (xvd)
Disk /dev/xvdc: 41943040s
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Number  Start    End       Size         File system   Name     Flags
    1   2048s    19531775s 19529728s    ext4          primary

我们可以看到结束扇区是 19531775,整个磁盘大小是 41943040s。
这表明分区有增长空间。

  1. 现在通过指定分区号使用 parted 删除现有分区在这种情况下,数字为 1.
# parted /dev/xvdc rm 1
Information: You may need to update /etc/fstab.
  1. 验证分区已删除:
# parted /dev/xvdc p
Model: Xen Virtual Block Device (xvd)
Disk /dev/xvdc: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Number Start End Size File system Name Flags
  1. 现在重新创建具有新大小的分区。
    指定与上一个分区相同的起始扇区,并使用我们在本示例中需要的百分比大小我将扩展 80%
# parted -s /dev/xvdc mkpart primary 2048s 80%
Warning: The resulting partition is not properly aligned for best performance.
  1. 验证分区的新大小和新的最后一个扇区大小,我们可以与我们之前的输出进行比较。
# parted /dev/xvdc p
Model: Xen Virtual Block Device (xvd)
Disk /dev/xvdc: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Number   Start   End      Size     File system   Name     Flags
1        1049kB  17.2GB   17.2GB   ext4          primary          ### New size of 80 % will be 17 GB.
# parted /dev/xvdc u s p
Model: Xen Virtual Block Device (xvd)
Disk /dev/xvdc: 41943040s
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Number   Start    End     Size File   system Name  Flags
    1    2048s 33554431s  33552384s      ext4     primary
  1. 对设备运行文件系统检查
# e2fsck /dev/xvdc1
e2fsck 1.43-WIP (20-Jun-2013)
/dev/xvdc1: clean, 21/610800 files, 92508/2441216 blocks
  1. 使用 resize2fs 调整文件系统的大小(ext3 和 ex4 文件系统)
# resize2fs -f /dev/xvdc1
resize2fs 1.43-WIP (20-Jun-2013)
Resizing the filesystem on /dev/xvdc1 to 4194048 (4k) blocks.
The filesystem on /dev/xvdc1 is now 4194048 blocks long.
  1. 挂载文件系统并验证新大小,与旧输出进行比较。
# mount /dev/xvdc1 /test
# df -h /test
Filesystem   Size   Used   Avail   Use%   Mounted on
/dev/xvdc1   16G    89M    15G     1%     /test
日期:2020-09-17 00:13:37 来源:oir作者:oir