Creating Virtual Machines with Virsh

Some of you out there will have worked with KVM/QEMU. Because of this, fewer of you might have worked with a program called Virtual Machine Manager (virt-manager) which is great for managing and creating virtual machines… locally.

Where Virtual Machine Manager falls over is quite irritating, and that’s when it comes to creating virtual machines on a remote system.

For example, we here at DMC host a lot of virtual machines at a remote location, and we need to create machines on a regular basis. This can be quite the tedious task if you’re forced to use virt-manager connecting over ssh, as it can take up to ten minutes just to move between creation screens.

There are ways around this of course, as with everything Linux, and one such way it to import a template using ‘virsh’

Virsh

Virsh, stylized “virsh” is a ‘management user interface’ for virtual machines.

You can do a lot with virsh, it’s a very useful and powerful tool for working with virtual machines on the command line, however, for today we will just be focusing on creating the virtual machines.

Creating a Config File

We are going to start with the following example XML, which is generic, but can be tweaked to your individual needs:


 

<domain type='kvm'>
  <name>test</name>
  <memory unit='MB'>512</memory>
  <currentMemory unit='MB'>512</currentMemory>
  <vcpu placement='static'>1</vcpu>
  <os>
    <type arch='x86_64'>hvm</type>
    <boot dev='cdrom'/>
  </os>
  <features>
    <acpi/>
    <apic/>
    <pae/>
  </features>
  <clock offset='utc'/>
  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>restart</on_crash>
  <devices>
    <emulator>/usr/bin/kvm</emulator>
    <disk type='block' device='disk'>
      <driver name='qemu' type='raw' cache='none'/>
      <source dev='/dev/volume-group/test-boot'/>
      <target dev='vda' bus='virtio'/>
    </disk>
    <disk type='block' device='disk'>
      <driver name='qemu' type='raw' cache='writethrough'/>
      <source dev='/dev/volume-group/test-root'/>
      <target dev='vdb' bus='virtio'/>
    </disk>
    <disk type='block' device='disk'>
      <driver name='qemu' type='raw' cache='writeback'/>
      <source dev='/dev/volume-group/test-swap'/>
      <target dev='vdc' bus='virtio'/>
    </disk>
    <disk type='file' device='cdrom'>
      <source file='/var/lib/libvirt/images/CentOS-6.4-x86_64-bin-DVD1.iso'/>
      <target dev='hdc' bus='ide'/>
      <readonly/>
    </disk>
    <interface type='bridge'>
      <mac address='52:54:00:88:88:88'/>
      <source bridge='br0'/>
      <model type='virtio'/>
    </interface>
    <input type='mouse' bus='ps2'/>
    <graphics type='vnc' port='-1' autoport='yes' keymap='en-gb'/>
  </devices>
  <seclabel type='none'/>
</domain>


 

There are some important additions that need to be made to the above example to make it work for you.

Everything in bold may be different, for example, we are using LVM for our disks, and we also have three of them, splitting up root / swap / boot in to three disks. We also use different caching for each.

The name of the VM may be different, the MAC address, the source of the installation medium, etc. All of the options you should need will be on the man page of virsh.

We need to create this entry somewhere on your server, open your favourite editor and copy it in. Save it as ‘vm-config’ or similar.

Creating the Disks

Because we use LVM for our disks, I have to create them first, this is easily done though:

# lvcreate -L 256M -n test-boot volume-group

# lvcreate -L 20G -n test-root volume-group

# lvcreate -L 1G -n test-swap volume-group

Importing the Config

Now that everything is in place, we can import the configuration:

# virsh define vm-config

If we now run:

# virsh list --all

You will see your VM, but it won’t be started.

# virsh start test

That’s it, your VM will start up and should try the installation medium specified first. If you have automated this process with a script then the installation should start straight away.

Connecting to the VM

There are a couple of ways to connect to the VM and check it through the installation, but the easiest is to use virt-manager.

Load up your virt-manager installation, and your VM should now be in the list of the hypervisor you are connected to.

From here you can guide it through the installation manually if required.

You can also connect to the VM from the command line of the hypervisor using virsh, but that’s a complete blog post in itself.

Checking Post Install

Once you have run through the install, and come to the end, you might want to look at what has changed in your config file.

You can dump the file with the following command:

# virsh dump test > vm-config-new

Opening ‘vm-config-new’ will show you your previously written config, but new parts will have been added, such as ‘uuid’ and ‘video’ among others.

This entry was posted in Linux, Virtualization and tagged , , , , , , , . Bookmark the permalink.

Leave a Reply