Some KVM-Qemu hints and tips

A short summary of useful KVM-Qemu commands and tricks.

disk images in qcow2 format tend to grow after while if one installs more and more stuff in the VMs. In order to free up space and shrink the qcow2 images do the following:

qemu-img convert -c -O qcow2 image.img shrunk.image.img

Test the new image by booting it the usual way and afterwards delete the original.img

In order to better control what gets onto the images we can use an internal snapshot function like so:

qemu-img create -f qcow2 -b shrunk.image.img snapshot01.shrunk.image.img

This way you can create as many snapshots as you need. I usually create simple bash scripts to load each VM snapshots like this :

Snapshot01
kvm -m 1024 -vga std -drive file=snapshot01.shrunk.image.img,cache=none,if=virtio

Snapshot02
kvm -m 1024 -vga std -drive file=snapshot02.shrunk.image.img,cache=none,if=virtio

etc ...

In case you need to revert back, just delete the snapshot file and create a new one from the original shrunk.image.img

When running Windows guests it is essential to run SAMBA on the host server and dedicate some shared folder so that the guests can access. For guest *nix systems sshd on host is sufficient as most systems ship with scp. When none of these are available or the VM network does not work, you can still try and use raw disk image and use that to copy over files.

Here I will use an example of a small USB flash stick 2 GB.

Lets assume it is formatted with some universally accepted file system (FAT32)

dd if=/dev/sdb1 of=/path/to/kvm/images/disk.raw

once the raw fat32 partition from the USB disk is dumped we can mount it via loop interface locally and copy files to/from.

mount -o loop disk.raw /mnt/usb

When finished, unmount and attach to KVM qemu as secondary IDE/virtio drive

kvm -vga vmware -m 1024 -drive file=snapshot01.shrunk.image.img,cache=none,index=1,if=virtio -drive file=disk.raw,index=2 -boot c

 

Advertisements
Gallery | This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s