RSS

Monthly Archives: June 2015

Kernel Virtual Machine alias KVM on Ubuntu Desktop 15.04

I have recently had some fun with KVM and I thought I would share with you my experiences. So basically a few years ago at the beginning of the University, my Programming teacher made me love the Linux and the Open Source community. It was really good to see people sharing their stuff/programs/etc for free, and nevertheless the greater good. At that time I took a dive into virtualization and since I had not much Idea, other than VirtualBox I thought to myself, I can possibly find something else. There I was looking at the how-tos of the KVM. At first it seemed to me a bit too geeky from all the command line stuff, but later I learned the power and the freedom that comes with the shell 🙂 Now I have a Ubuntu 15.04 (i386) installed on my laptop because it’s a bit old one(HP 6930p), but It has some power in it to run 2-3 KVM machines paralell. In order to install the necessary packages we have to run the following commands in the shell, behold we need root access to install the packages.

sudo su

After this we enter our root password

apt-get install kvm

These will select the necessary dependency packages, and install them. Until it’s done we can have a coffee or tea or whatever.

KVMinstallAfter the installation we should create a folder for our KVM platform, I have created it on my user’s Desktop with the following structure. I have 1 folder where the images will be stored, and another where my start scripts will be held.

structureIn order to install a virtual machine we have to create and “image” file which is similar to the “vhd/vhdx” or the Hyper-V(Microsoft) or the “vmdk” of the VMware platforms. This is the realm of the virtual machine. We can create one with the following command.

qemu-img create <NameOfTheImage.img> -f qcow2 <Size>

Here is how I created a 10G image for my Ubuntu Server.

qemu-img create UbuntuServer32bit.img -f qcow2 10G

Now all we have to do to grab an ISO from the Ubuntu site, download it and install the Operating System to play with.

http://www.ubuntu.com/download/server

In the previous command after the “-f” I have used the “qcow2” because “QEMU image format, the most versatile format…”, check the manual page for relevant informations.

So after we have downloaded the ISO all we have to do is specify the KVM to boot from the ISO and work with the previously created image file.

kvm
    -smp 2
    -m 1024
    -boot d
    -cdrom /home/samcro/Downloads/ubuntu-14.04.2-server-amd64.iso
    -hda ./images/UbuntuServer32bit.img

I broke it down so it’s easier to read, the “kvm” tell’s the shell to start an insance of the virtualization, the “-smp 2” tells it to have 2 vCPU, the “-m 1024” means I have assigned 1GB of memory to it “-boot d” makes it boot from cdom, the “-cdrom …” specifies the path to the ISO, and the “-hda” specifies the path to the image.

UbuseroBasically now all we have to do is go through the configuration steps and install the server as we want it. When it’s done we can create a basic startup script so we don”t have to type always so much to get the server started. Here is how my start script looks like. I have navigated into the “osscripts” folder.

touch StartUbuntuServer32bit

With the above command I have created the file which will hold the following command.

kvm -smp 2 -m 1024 ../images/UbuntuServer32bit.img

This is a bit simplier, however it’s quite functional. We can use “nano” or “gedit” to add these lines to the file! In the end we have to make sure we set the “executable” flag on our file so we can run it.

chmod +x StartUbuntuServer32bit

With this we set the flag, and if we paid attention we can see that the color of the file has changed to green.

chmodIf we did not make a typo I believe the following should appear after we give the “./StartUbuntuServer32bit” command to our shell.

Server
There are many ways to fine-tune the kvm, with lots of switches, and other demand specific features. In the future if I will show you a little bit more easier way to manage the virtual machines running on this platform with the help of the “virt-manager”, which is a graphical tool for this very purpose.

Cheers,
Daniel

 
Leave a comment

Posted by on June 17, 2015 in Uncategorized