我有一个VM模板,用于使用virt-clone / KVM构建其他VM. VM模板为4GB以节省空间.我从中构建的VM的存储位于iSCSI目标或LVM卷(取决于功能),并且它们的文件系统大小因计算机的角色而异.
从模板创建新VM后,如果我正在构建需要超过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% /
解决方法
我已经想出了如何做到这一点.关键是kpartx使得LVM可以在VM外部分开(因此在Hypervisor主机上).然后修改分区大小,然后启动guest虚拟机并增加文件系统.
因此,如果您有一个名为TESTVM的guest虚拟机,其存储位于/ 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
再次重新启动只是为了安全起见.