简体   繁体   English

如何在不影响应用程序的情况下调整 qcow2 图像的大小

[英]How to resize the qcow2 image without impact the application

My question is about to resize the qcow2 VM image.我的问题是要调整 qcow2 VM 映像的大小。 I have a image with packages and applications built in. For example when creating the image, the size is 40G, but for now it in reality use about 5G disk space.我有一个内置包和应用程序的映像。例如,在创建映像时,大小是 40G,但现在它实际上使用了大约 5G 的磁盘空间。 So i would like to shrink it.所以我想缩小它。

My method is like below:我的方法如下:

 qemu-img convert -O raw VM1.qcow2 VM1.raw qemu-img resize VM1.raw -20G qemu-img convert -c -O qcow2 VM1.raw VM1.qcow2

But after i did that, although the size is shrunk a lot, the VM could not be able to boot correctly.但是在我这样做之后,虽然大小缩小了很多,但虚拟机无法正常启动。 I am using linux with KVM/libvirt.我在 KVM/libvirt 上使用 linux。

If your guest OS is windows, you can expand the partition from within the guest, while it is booted.如果您的来宾操作系统是 Windows,您可以在引导时从来宾内部扩展分区。 First copy your original image (for safety) and use qemu-img resize to add space to the disk image:首先复制您的原始映像(为了安全)并使用 qemu-img resize 为磁盘映像添加空间:

 cp small_image.qcow2 large_image.qcow2 qemu-img resize large_image.qcow2 +100G

Then boot the windows VM in large_image.qcow2.然后在 large_image.qcow2 中启动 windows VM。 Open the "Disk Management" utility.打开“磁盘管理”实用程序。 Right click on C:, and select either "Extend Volume" or "Shrink Volume"右键单击 C:,然后选择“扩展卷”或“缩小卷”

Your VM guest will now have access to the space added by qemu-img resize.您的 VM 来宾现在可以访问由 qemu-img 调整大小添加的空间。

To shrink a disk, you must do some work on the Guest VM.要缩小磁盘,您必须在来宾 VM 上做一些工作。

An excellent description: http://www.jamescoyle.net/how-to/323-reclaim-disk-space-from-a-sparse-image-file-qcow2-vmdk .一个很好的描述: http : //www.jamescoyle.net/how-to/323-reclaim-disk-space-from-a-sparse-image-file-qcow2-vmdk I've added defrag, and expanding a disk.我添加了碎片整理并扩展了磁盘。

From a windows guest:来自 Windows 来宾:

Fill all free space with 0's.用 0 填充所有可用空间。

c:\sdelete.exe -z c:

From a Linux guest, fill all free space with 0's:在 Linux 客户机中,用 0 填充所有可用空间:

dd if=/dev/zero of=~/mytempfile
rm -f ~/mytempfile

On the libvirt host:在 libvirt 主机上:

mv original_image.qcow2 original_image.qcow2_backup
qemu-img convert -O qcow2 original_image.qcow2_backup original_image.qcow2

To expand a disk, it can all be done on the libvirt host.要扩展磁盘,这一切都可以在 libvirt 主机上完成。

mv original_image.qcow2 original_image.qcow2_backup
truncate -s <desired number of Gigabytes>G original_image.qcow2
qemu-img convert -O qcow2 original_image.qcow2_backup original_image.qcow2

In BOTH cases, check your work.在这两种情况下,请检查您的工作。

qemu-img info original_image.qcow2

Manipulating only from the host side, may leave the guest file system corrupted.仅从主机端进行操作可能会使来宾文件系统损坏。 Thus, before any changes from the host you need to change the VM's storage partitions sizes with partitioning tools like gparted from inside of the guest.因此,在主机进行任何更改之前,您需要使用诸如gparted类的分区工具从来宾内部更改 VM 的存储分区大小。 So that the total size of the virtual disk becomes less than desired size of the virtual storage.使虚拟磁盘的总大小变得小于虚拟存储的所需大小。 After that, you can do converting and resizing and converting back the virtual storage file from the host.之后,您可以进行转换和调整大小并从主机转换回虚拟存储文件。

1) the simplest method: install libguestfs-tools 2) run: virt-sparsify --in-place virtualDisk.qcow2 1)最简单的方法:安装libguestfs-tools 2)运行:virt-sparsify --in-place virtualDisk.qcow2

it does it on file so you don't need extra space.它在文件中进行,因此您不需要额外的空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM