PG1X WIKI

My Knowledge Base

User Tools

Site Tools


tech:server:linux:virtualization:kvm-qemu:qemu-img:qemu-img

This is an old revision of the document!


qemu-img

DO NOT OPERATE ANY MODIFICATION COMMAND(INCLUDING SNAPSHOT OPERATION) ON RUNNING VIRTUAL MACHINE IMAGE!!!!!!! THE OPERATIONS RESULTS IN VIRTUAL MACHINE IMAGE CORRUPTION, LOSE ALL DATA!!!!!!!!!!

MAKE SURE TARGET VM STOPPED THAT CONTAINS THE VM IMAGE.

root@hotaru:/var/lib/libvirt/images# virsh list --inactive | fgrep nextcloud1.example.com
 -     nextcloud1.example.com            shut off
  • list snapshots.
qemu-img snapshot -l nextcloud1.example.com.qcow2

if no snapshots found, result is empty.

  • create a snapshot.
qemu-img snapshot -c "Initial snapshot" nextcloud1.example.com.qcow2

list again.

root@hotaru:/var/lib/libvirt/images# qemu-img snapshot -l nextcloud1.example.com.qcow2
Snapshot list:
ID        TAG                 VM SIZE                DATE       VM CLOCK
1         Initial snapshot          0 2020-01-06 21:53:18   00:00:00.000

snapshot was created successfully.

4 or more snapshots exists, in my experience, VM performance significantly decrease….
  • revert to snapshot
root@hotaru:~# qemu-img snapshot -l /var/lib/libvirt/images/nextcloud1.example.com.qcow2
Snapshot list:
ID        TAG                 VM SIZE                DATE       VM CLOCK
1         Initial snapshot          0 2020-01-06 21:53:18   00:00:00.000
root@hotaru:~# qemu-img snapshot -a 1 /var/lib/libvirt/images/nextcloud1.example.com.qcow2
  • remove snapshot
root@hotaru:~# qemu-img snapshot -l /var/lib/libvirt/images/nextcloud1.example.com.qcow2
Snapshot list:
ID        TAG                 VM SIZE                DATE       VM CLOCK
1         Initial snapshot          0 2020-01-06 21:53:18   00:00:00.000
root@hotaru:~# qemu-img snapshot -c "virt-sysprep" /var/lib/libvirt/images/nextcloud1.example.com.qcow2
root@hotaru:~# qemu-img snapshot -l /var/lib/libvirt/images/nextcloud1.example.com.qcow2
Snapshot list:
ID        TAG                 VM SIZE                DATE       VM CLOCK
1         Initial snapshot          0 2020-01-06 21:53:18   00:00:00.000
2         virt-sysprep              0 2020-01-07 07:05:09   00:00:00.000
root@hotaru:~# qemu-img snapshot -d 1 /var/lib/libvirt/images/nextcloud1.example.com.qcow2
root@hotaru:~# qemu-img snapshot -l /var/lib/libvirt/images/nextcloud1.example.com.qcow2
Snapshot list:
ID        TAG                 VM SIZE                DATE       VM CLOCK
2         virt-sysprep              0 2020-01-07 07:05:09   00:00:00.000

Resize QCOW2 image size

qemu-img resize nextcloud1.example.com.qcow2 20G
resize [--shrink] [--preallocation=prealloc] filename [+ | -]size
    Change the disk image as if it had been created with size.

    Before using this command to shrink a disk image, you MUST use file system and partitioning tools inside the VM to reduce allocated file systems and partition
    sizes accordingly.  Failure to do so will result in data loss!

    When shrinking images, the "--shrink" option must be given. This informs qemu-img that the user acknowledges all loss of data beyond the truncated image's
    end.

    After using this command to grow a disk image, you must use file system and partitioning tools inside the VM to actually begin using the new space on the
    device.

    When growing an image, the "--preallocation" option may be used to specify how the additional image area should be allocated on the host.  See the format
    description in the "NOTES" section which values are allowed.  Using this option may result in slightly more data being allocated than necessary.

References

tech/server/linux/virtualization/kvm-qemu/qemu-img/qemu-img.1578807910.txt.gz · Last modified: 2020/01/12 14:45 by wnoguchi