LibVirt notes….

This blog posts contains shortcuts for creating VM’s in libvirt. I use these commands all the time and constantly find my sell referring to these notes, so I thought I would post them here for everyone else to use as well. To give credit where credit is due alot of this comes from Rhys Oxenham, Thanks Rhys!!

Step one get an image….

I usually download it to /var/lib/libvirt/images, the default location for libvirt, however you can stick it anywhere. For this example I used the latest (of this writing) rhel7 image form Red Hat.

Make sure the libvirt tools are installed
# dnf install libvirt qemu-kvm virt-manager virt-install libguestfs-tools libguestfs-xfs net-tools -y

I usually rename it something more manageable…
# mv rhel-guest-image-7.0-20140930.0.x86_64.qcow2 rhel7-guest-official.qcow2

Check the details of the image you downloaded:
# qemu-img info rhel7-guest-official.qcow2

Check the details of the image file system:
# virt-filesystems --long -h --all -a rhel7-guest-official.qcow2

Create new image, no data:
# qemu-img create -f qcow2 rhel7-guest.qcow2 40G

Re-size off official to new:
# virt-resize --expand /dev/sda1 rhel7-guest-official.qcow2 rhel7-guest.qcow2

Check that both the partition and the filesystem have been correctly resized
# virt-df -a rhel7-guest.qcow2

Turn this guest image into a backing file so we can use copy on write functionality to be space efficient with our new virtual machine:
# qemu-img create -f qcow2 -b rhel7-guest.qcow2 test-vm.qcow2

set the root password:
# virt-customize -a test-vm.qcow2 --root-password password:test

boot the machine:
# virt-install --ram 16384 --vcpus 4 --os-variant rhel7 \
--disk path=/var/lib/libvirt/images/test-vm.qcow2,device=disk,bus=virtio,format=qcow2 \
--import --noautoconsole --vnc \
--network network:default --name test

** if you want to connect to a bridge

  –network bridge=br-prov –network bridge=br-ext

Check that its running:
# virsh list --all

use arp to find out the IP that it has been allocated:
# VM_IP=$(arp | awk ' /virbr0/ {print $1}')
# echo $VM_IP

creating a passwordless SSH keypair:
# ssh-copy-id -i ~/.ssh/ root@$VM_IP

connect to VM
# ssh root@$VM_IP

Take a snapshot:
# virsh snapshot-create-as test test-snap-1

list all snapshots:
# virsh snapshot-list

If you ever need to restore VM you can execute the following command:
# virsh snapshot-revert --domain test


Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s