我刚刚在 /dev/sda2 上构建了一个带有 15GB 磁盘和 LVM 的测试虚拟机。然后我将磁盘增加到 20GB,并让 LVM 看到额外的 5GB,而无需添加另一个物理卷。
以下是我遵循的步骤:
- apt-get install gnu-fdisk (或 yum install gnu-fdisk)
我不得不使用gfdisk来完成整个工作。“标准” fdisk 没有运气。
切换到“sectors”为单位(这个很关键!!!)并打印分区表:
Command (m for help): u
Changing display/entry units to sectors
Command (m for help): p
Disk /dev/sda: 21 GB, 21089617920 bytes
255 heads, 63 sectors/track, 2564 cylinders, total 41190660 sectors
Units = sectors of 1 * 512 = 512 bytes
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 499711 257008 83 Linux
Warning: Partition 1 does not end on cylinder boundary.
/dev/sda2 501758 29798632 14643247 8e Linux LVM
Warning: Partition 2 does not end on cylinder boundary.
Command (m for help):
记下“Linux LVM”分区(/dev/vda2)的“开始”扇区。删除分区并使用相同的“开始”扇区 (501758) 和相同的分区类型 (8e) 重新创建它:
Command (m for help): d
Partition number (1-2): 2
Command (m for help): n
Partition type
e extended
p primary partition (1-4)
p
First sector (default 63s): 501758
Last sector or +size or +sizeMB or +sizeKB (default 41190659s):
Command (m for help): p
Disk /dev/sda: 21 GB, 21089617920 bytes
255 heads, 63 sectors/track, 2564 cylinders, total 41190660 sectors
Units = sectors of 1 * 512 = 512 bytes
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 499711 257008 83 Linux
Warning: Partition 1 does not end on cylinder boundary.
/dev/sda2 501758 41190659 20338290 83 Linux
Command (m for help): t
Partition number (1-2): 2
Hex code (type L to list codes): 8e
Changed type of partition 2 to 8e (Linux LVM)
Command (m for help):
警告:请注意,我没有接受分区的默认起始扇区,我手动输入了它,以便它与原始值匹配!我确实接受了“最后一个扇区”的默认值,因为我希望这个分区和磁盘一样大。
验证您是否使用“p”正确执行了所有操作并将新分区表写入磁盘:
Command (m for help): w
重新启动虚拟机。现在登录虚拟机并运行:
root@git:~# pvresize /dev/sda2
Physical volume "/dev/sda2" changed
1 physical volume(s) resized / 0 physical volume(s) not resized
完毕!现在运行 vgdisplay,您将看到额外的 5GB 可用作为空闲扩展区。
注意事项:
- 如果 LVM 不是虚拟机磁盘上的最后一个(或唯一一个)分区,您可能很难扩展分区的大小。甚至可能是不可能的。
- 如果 LVM 在逻辑分区上(这是 Debian 在安装时默认放置的位置),或者换句话说,如果 LVM 在 /dev/sda5 而不是 /dev/sda2 上,您必须记下两个扩展的起始扇区分区(我们称之为/dev/sda2)和逻辑分区(我们称之为/dev/sda5),然后删除两个分区,然后用相同的起始扇区重新创建它们。/dev/sda2 和 /dev/sda5 的最后一个扇区应该是磁盘的最后一个扇区。
- 由于这是一个有风险的过程,我建议在尝试之前对虚拟机进行备份。