Linux iTOps Tube

Thursday, 13 December 2012

Installing KVM Guests With virt-install On Ubuntu 12.10 Server

Unlike virt-manager, virt-install is a command line tool that allows you to create KVM guests on a headless server. You may ask yourself: "But I can use vmbuilder to do this, why do I need virt-install?" The difference between virt-install and vmbuilder is that vmbuilder is for creating Ubuntu-based guests, whereas virt-install lets you install all kinds of operating systems (e.g. Linux, Windows, Solaris, FreeBSD, OpenBSD) and distributions in a guest, just like virt-manager. This article shows how you can use it on an Ubuntu 12.10 KVM server.

I do not issue any guarantee that this will work for you!

 

1 Preliminary Note

I'm assuming that KVM is already installed (e.g. as shown here: Virtualization With KVM On Ubuntu 12.10). My KVM host has the IP address 192.168.0.100.

I will show how to install a CentOS 6.3 guest in this tutorial.

We also need an Ubuntu 12.10 desktop so that we can connect to the graphical console of our KVM guests. It doesn't matter if the desktop is installed on the Ubuntu 12.10 KVM server or on a remote system (there are small differences if the desktop is installed on the KVM host compared to a remote desktop, but I will outline these differences, so read carefully).

 

2 Installing virt-install

Ubuntu 12.10 Host:

Open a terminal and install virt-install:

sudo apt-get install virtinst

 

3 Installing virt-manager On Your Ubuntu 12.10 Desktop

Ubuntu 12.10 Desktop:

We need a means of connecting to the graphical console of our guests - we can use virt-viewer or virt-manager (see KVM Guest Management With Virt-Manager On Ubuntu 8.10) for this. I'm assuming that you're using an Ubuntu 12.10 desktop (it doesn't matter if it is a remote desktop of if the desktop is installed on the Ubuntu 12.10 KVM server!).

I suggest you use virt-manager instead of virt-viewer because virt-manager lets you also create and delete virtual machines and do other tasks. virt-manager can be installed as follows:

sudo apt-get install virt-manager

4 Creating A CentOS 6.3 Guest

Ubuntu 12.10 KVM Host:

Now let's go back to our Ubuntu 12.10 KVM host.

Take a look at

man virt-install

to learn how to use it.

We will create our image-based virtual machines in the directory /var/lib/libvirt/images/ which was created automatically when we installed KVM.

To create a CentOS 6.3 guest (in bridging mode) with the name vm10, 1024MB of RAM, two virtual CPUs, and the disk image /var/lib/libvirt/images/vm10.img(with a size of 12GB), insert the CentOS DVD into the CD drive and run

sudo virt-install --connect qemu:///system -n vm10 -r 1024 --vcpus=2 --disk path=/var/lib/libvirt/images/vm10.img,size=12 -c /dev/cdrom --vnc --noautoconsole --os-type linux --accelerate --network=bridge:br0 --hvm

Of course, you can also create an ISO image of the CentOS DVD (please create it in the /var/lib/libvirt/images/ directory because later on I will show how to create virtual machines through virt-manager from your Ubuntu desktop, and virt-manager will look for ISO images in the /var/lib/libvirt/images/ directory)...

sudo dd if=/dev/cdrom of=/var/lib/libvirt/images/CentOS-6.3-x86_64-bin-DVD1.iso

... and use the ISO image in the virt-install command:

sudo virt-install --connect qemu:///system -n vm10 -r 1024 --vcpus=2 --disk path=/var/lib/libvirt/images/vm10.img,size=12 -c /var/lib/libvirt/images/CentOS-6.3-x86_64-bin-DVD1.iso --vnc --noautoconsole --os-type linux --accelerate --network=bridge:br0 --hvm

The output is as follows:

administrator@server1:~$ sudo virt-install --connect qemu:///system -n vm10 -r 1024 --vcpus=2 --disk path=/var/lib/libvirt/images/vm10.img,size=12 -c /var/lib/libvirt/images/CentOS-6.3-x86_64-bin-DVD1.iso --vnc --noautoconsole --os-type linux --accelerate --network=bridge:br0 --hvm

Starting install...
Allocating 'vm10.img'       |  12 GB     00:00
Creating domain...          |    0 B     00:00
Domain installation still in progress. You can reconnect to
the console to complete the installation process.
administrator@server1:~$


5 Connecting To The Guest

 

Ubuntu 12.10 Desktop:

The KVM guest will now boot from the CentOS 6.3 DVD and start the graphical CentOS installer - that's why we need to connect to the graphical console of the guest. You can do this with virt-manager on the Ubuntu 12.10 desktop (see KVM Guest Management With Virt-Manager On Ubuntu 8.10).

Start Virtual Machine Manager (you can search for virt-manager in Unity to find it):


When you start virt-manager for the first time and no KVM is installed on your Ubuntu desktop (i.e., the KVM host is not identical to your dekstop), you will most likely see the following message (Could not detect a default hypervisor.). You can ignore this because we don't want to connect to the local libvirt daemon, but to the one on our remote Ubuntu 12.10 KVM host.


In virt-manager, go to File > Add Connection... to connect to your Ubuntu 12.10 KVM host. In my virt-manager, I couldn't find the File menu, it was missing, so I had to right-click a certain area just below the cross that is used to close the program, and then the menu opened from where I could select Add Connection...:


Select QEMU/KVM as Hypervisor. If the KVM host is identical to your desktop, you are done now and can click on Connect.

But if the KVM host is on a remote Ubuntu 12.10 server, then check Connect to remote host, select SSH from the Method drop-down menu, fill in root in theUsername field, type in the hostname or IP address (192.168.0.100) of the Ubuntu 12.10 KVM host in the Hostname field, and click on Connect.

(Replace 192.168.0.100 with the IP address or hostname of the KVM host. Please note that the root account must be enabled on the KVM host, and that root logins must be allowed on the KVM host. To enable the root login on an Ubuntu system, run

sudo passwd root

To check if root logins are allowed check the directive PermitRootLogin in /etc/ssh/sshd_config - you might have to restart the SSH daemon afterwards. )


If this is the first connection to the remote KVM server, you must type in yes and click on OK:

Afterwards type in the root password of the Ubuntu 12.10 KVM host:


You should see vm10 as running. Mark that guest and click on the Open button to open the graphical console of the guest:


Type in the root password of the KVM host again:


You should now be connected to the graphical console of the guest and see the CentOS installer:



Now install CentOS as you would normally do on a physical system. Please note that at the end of the installation, the CentOS system needs a reboot. The guest will then stop, so you need to start it again, either with virt-manager or like this on the KVM host's command line:

Ubuntu 12.10 KVM Host:

sudo virsh --connect qemu:///system

start vm10

quit

Afterwards, you can connect to the guest again with virt-manager and configure the guest. You can as well connect to it with an SSH client (such as PuTTY).


Instead of creating a virtual machine from the command line (as shown in chapter 4), you can as well create it from the Ubuntu desktop using virt-manager (of course, the virtual machine will be created on the Ubuntu 12.10 KVM host - in case you ask yourself if virt-manager is able to create virtual machines on remote systems).

To do this, click on the following button:

The New VM dialogue comes up. Fill in a name for the VM (e.g. vm11), select Local install media (ISO image or CDROM), and click on Forward:


Next select Linux in the OS type drop-down menu and Red Hat Enterprise Linux 6 in the Version drop-down menu, then check Use ISO image and click on theBrowse... button:


Select the CentOS-6.3-x86_64-bin-DVD1.iso image that you created in chapter 4 and click on Choose Volume:


Now click on Forward:


Assign memory and the number of CPUs to the virtual machine and click on Forward:


Now we come to the storage. Check Enable storage for this virtual machine, select Create a disk image on the computer's hard drive, specify the size of the hard drive (e.g. 12GB), and check Allocate entire disk now. Then click on Forward:


Now we come to the last step of the New VM dialogue. Go to the Advanced options section. Select Specify shared device name; the Bridge name field will then appear where you fill in the name of your bridge (if you have used the Virtualization With KVM On Ubuntu 12.10 guide to set up the KVM host, this is br0). Click on Finish afterwards:




The disk image for the VM is now being created:


Afterwards, the VM will start. If you use a remote KVM host, type in the root password of the KVM host:

You should now be connected to the graphical console of the guest and see the CentOS installer:

Now install CentOS as you would normally do on a physical system.

 

7 Cloning Guests

Ubuntu 12.10 KVM Host:

The python-virtinst package comes with a second tool, virt-clone, that lets you clone guests. To clone vm10 and name the clone vm12 with the disk image/var/lib/libvirt/images/vm12.img, you simply run (make sure that vm10 is stopped!)

sudo virt-clone --connect qemu:///system -o vm10 -n vm12 -f /var/lib/libvirt/images/vm12.img

Afterwards, you can start vm12 with virt-manager or like this...

sudo virsh --connect qemu:///system

start vm12

quit

... and connect to it using virt-manager.

 

8 Links


No comments:

Post a Comment