尝试分离脚本失败

尝试分离脚本失败

我有一个 VM 模板,用于使用 virt-clone/KVM 构建其他 VM。VM 模板为 4GB,以节省空间。我由此构建的 VM 的存储位于 iSCSI 目标或 LVM 卷上(取决于功能),并且它们的文件系统大小因机器的角色而异。

从模板创建新虚拟机后,如果我要构建需要超过 4GB 磁盘空间的东西,我必须调整根分区的大小。这在以交互方式使用 parted 时工作正常,但不是脚本。当尝试删除文件系统时,系统会询问我是否要继续,尽管使用了“-s”

下面的输出显示了一次失败的脚本尝试和一次为实现此目的而运行的交互式会话。

克隆后调整根分区大小的最佳方法是什么?可以简单地编写脚本吗?

尝试分离脚本失败

克隆后分区

# parted /dev/vda p
Model: Virtio Block Device (virtblk)
Disk /dev/vda: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  4294MB  4293MB  primary  ext4         boot

尝试删除脚本

# parted /dev/vda -s rm 1
Warning: Partition /dev/vda1 is being used. Are you sure you want to continue?
#

删除失败后的分区

# parted /dev/vda p
Model: Virtio Block Device (virtblk)
Disk /dev/vda: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  4294MB  4293MB  primary  ext4         boot

工作交互式调整大小(随后重新启动)

删除前的分区

# parted /dev/vda p
Model: Virtio Block Device (virtblk)
Disk /dev/vda: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  4294MB  4293MB  primary  ext4         boot

删除并创建使用所有磁盘空间的新分区

# parted /dev/vda
(parted) rm 1
Warning: Partition /dev/vda1 is being used. Are you sure you want to continue?
Yes/No? y
Error: Partition(s) 1 on /dev/vda 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.
Ignore/Cancel? I
(parted) mkpart p ext4 1 -1
(parted)

# parted /dev/vda p
Model: Virtio Block Device (virtblk)
Disk /dev/vda: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  10.7GB  10.7GB  primary  ext4

文件系统调整大小

# resize2fs /dev/vda1
resize2fs 1.42.9 (4-Feb-2014)
Filesystem at /dev/vda1 is mounted on /; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/vda1 is now 2621184 blocks long.

# df -kh  .
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda1       9.8G  1.6G  7.9G  17% /

答案1

我已经想出了如何做到这一点。关键是 kpartx 使 LVM 可由 VM 外部的 parted 使用(因此在 Hypervisor 主机上)。然后修改分区大小,然后启动客户机并增加文件系统。

因此,如果您有一个名为 TESTVM 的客户机,其存储在 /dev/VMS/VIRT-TESTVM,则您需要在虚拟机管理程序主机上执行以下操作:

# kpartx -a /dev/VMS/VIRT-TESTVM
# parted /dev/VMS/VIRT-TESTVM rm 1
# parted /dev/VMS/VIRT-TESTVM mkpart -a optimal p ext4 0% 100% 
# kpartx -d /dev/VMS/VIRT-TESTVM

然后只需启动机器,登录并执行

# resize2fs /dev/vda1 

为了安全起见,请重新启动。

相关内容