我有一个VM模板,用于使用virt-clone / KVM构build其他虚拟机。 VM模板为4GB以节省空间。 由此构build的虚拟机的存储位于iSCSI目标或LVM卷(取决于function),其文件系统大小因机器的angular色而异。
从模板创build一个新的虚拟机后,我必须调整根分区的大小,如果我build立的东西需要超过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
删除和创build使用所有磁盘的新分区
# 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
文件系统resize
# 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% /
我已经想出了如何做到这一点。 关键是kpartx使LVM可以在虚拟机之外使用(在Hypervisor主机上)。 然后修改分区大小,然后启动guest虚拟机并增加文件系统。
所以如果你有一个名为TESTVM的guest虚拟机,它的存储位置是/ dev / VMS / VIRT-TESTVM,你可以在hypervisor主机上执行以下操作:
# 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
然后只需启动机器,login并执行操作
# resize2fs /dev/vda1
重新启动只是为了安全起见。