Virtualization solutions on Linux systems - KVM and VirtualBox


https://linuxconfig.org/virtualization-solutions-on-linux-systems-kvm-and-virtualbox#h4-conclusion


1. Introduction

Virtualization packages are means for users to run various operating systems without "bare-metal" hardware - basically, you can run more than one operating system on a single computer without dual-booting or similar approaches. Virtualization software emulates a real machine and "fools" the guest operating system into thinking it's running on a real computer. Besides the more obvious advantages, virtual machines help create a greener and easier to administer computing environment. Looking at the trends in the IT industry, virtualization has seen quite a boom in the last few years, because it fits the concepts of utility computing and/or software as a service. Virtualization can be useful to you if you are an enterprise architect, developer, a home user or basically everything in between. We will begin with a short introduction about virtualization in general, then we will specifically treat VirtualBox and KVM as they seem to be most popular open source full virtualization solutions. You are expected to know your way around Linux systems, how to install a Linux distribution and how to install software on it, although we will show you how to install the two aforementioned virtualization packages on some of the popular Linux distributions.

There are two types of virtualization : one that can run the guest system as-is (as in, unmodified) and another that request a modified kernel on the guest's side in order to run. The first category is named full virtualization, because it emulates a complete hardware environment, the second is named paravirtualization , because it doesn't emulate hardware and hence needs special modifications at guest level, a good example of this type of virtualization being Xen. These are part of a bigger category named hardware virtualization, but there are also other (software, network or storage, amongst others) virtualization types, which we will not detail here. The two pieces of software we will talk about fit into the full virtualization category. Other popular hardware virtualization technologies include QEMU, Bochs, VMware, Parallels, HyperV or OpenVZ.

1.1. When is virtualization useful?

1.1.1. Linux distributions

The Linux world is full of interesting and tempting offers. There are over 600 (!) Linux distributions to choose from, which makes it hard for a person that only has one computer to try them all, or just a few even. LiveCDs aren't always helpful, so one may need to install in order to get the gist of it. Every Linux distribution release brings new and exciting features, and you may feel the thrill and the impulse to install and test it. Enter virtualization. You download the ISO, install the distro in a virtual environment and you're good to go, all in a short time. You don't like it, you delete it. Especially when you're kinda new to the Linux world and you might be confused by the big number of available distributions, this might just be what you need. Also, if you're a developer and need to run the development branch of your distro (think Fedora Rawhide or Debian Sid) but that is too unstable for everyday use, install in a VM and start developing.

1.1.2. Other operating systems

This extends to other operating systems you might need : maybe you have a propgram that runs only on Windows and you don't wanna install Windows just for that one program. Maybe you want to learn Solaris but lack the hardware. Provided you have the appropriate computer configuration that supports virtualization, now you can do it.

1.2. What you will need

Modern processors have special CPU instructions for hardware emulation. You can live without, but you really don't want to, since the host operating system will have to emulate the lacking virtualization instruction and this will slow down your guest(s) significantly. We presume your host OS has Linux installed and your CPU has the necessary virtualization capabilities. The most simple way to check if your CPU has what it takes, do

 $ egrep ‘(vmx|svm)’ /proc/cpuinfo 

and if that returns either vmx (Intel CPUs) or svm (AMD CPUs), you're good to go. But that of course isn't the only request on the hardware side. Check the web page of the system you want to install as guest to check if you meet its' hardware requirements. We recommend at least 20GB free in your home directory and a minimum of 2GB of memory on the host, so you can allocate a mean amount of 768MB to the guest for optimal performance. Of course, should you want to run multiple virtual machines (maybe in parallel), those requirements grow considerably.

1.3. KVM or VirtualBox?

First of all, why would we offer you two virtualization packages? Why not one for all your needs? Well, we believe in the "right tool for the job" concept. KVM offers some features that VirtualBox does not and the other way around. There is no such thing in the IT world as a universal tool, so it's important to use something that fits your needs. The basic idea is : if you want to install a binary Linux distribution as a guest, use KVM. It's faster and its' drivers are included in the official kernel tree. If your guest involves lots of compiling and needs some more advanced features, and/or isn't a Linux system, better go with VirtualBox.

The technical reasons are quite simple : KVM is better integrated with Linux, it's smaller and faster, and while you can use it with other guests besides Linux, we found the experience to be quite troublesome : BSDs tend to have slow I/O and Solaris (OpenIndiana, to be exact) tends to panic immediately after booting the installation ISO. Since we use CURRENT versions of BSD (and compile/update the system from source often) and also need Solaris, we found VirtualBox to be a better option. Another plus for Oracle VirtualBox is the fact that it supports suspend, that is you can save the machine state on the host's hard disk and close VirtualBox and when (re)starting, the system will pick up from where it left. That is why we referred to source compilation : if you have a noisy machine you don't wanna leave on overnight but your Gentoo virtual machine just compiles a new gcc version, suspend the machine state, shut down the host and continue tomorrow.

1.4. What you will learn

We will show you how to install KVM and VirtualBox and how to install and configure a Linux guest on a Linux host. Examples with other Unices are very similar, so we see no need to repeat ourselves.

2. Installation

2.1. KVM

Although KVM advertises Solaris, BSD, ReactOS, Windows and others as supported guests, our experiences were less than fruitful. This isn't a criticism, since we did not, in all honesty, sit down and examine the issue too much. But this is not out of laziness : after talking with other people on the 'Net, we found that we are not the only ones with issues regarding Unix(-like) guests besides Linux, so we just went with VirtualBox for those. As always, your mileage may vary : we too would want to use KVM exclusively because it's more "free", but because of the aforementioned problems, which of course might just be something specific to our setup, we had to use VirtualBox. At the moment, KVM is developed by Red Hat and is easily installable on most Linux distributions. As we did before, we will outline only the installation procedures pertaining to major Linux distributions.

2.1.1. Fedora

Since Red Hat, the sponsor of Fedora, is now developing KVM, Fedora offers a plethora of tools for virtualization. The official documentation, although it's for Fedora 13, is applicable to later versions. You can just install KVM with

 # yum install kvm 

to go the command-line way, or you can install some graphical tools to help you manage your virtual installations :

 # yum install virt-manager libvirt libvirt-python python-virtinst 

You will not need to execute further commands since the installation process on Fedora will mark the kvm module for autoloading. If you don't want to reboot, use modprobe to load the module in-place.

2.1.2. Debian

Debian's wiki comes to the rescue yet again, and if one follows the instructions, one will have KVM installed and ready to go in no time. It's all about

 # apt-get install qemu-kvm libvirt-bin 

to install, setup and have the kvm-(amd|intel) kernel module loaded at boot.

2.1.3. Slackware

Slackbuild for KVM which you can use according to this guide. If you have a modified kernel or just want to make sure your kernel has KVM enabled, type

 $ cat /boot/config |grep -i kvm 

and also check /etc/rc.d/rc.modules to see if the kvm module is set to be loaded at boot.

2.1.4. Arch

The wiki Arch Linux is a good guide for everything KVM-related. Since Arch, like Slackware, is a DYI distribution, you might need to take a few extra steps to start using KVM. The installation is as simple as

 # pacman -S qemu-kvm 

The same steps are to be taken here as on a Slackware machine : add your user, check that your CPU supports virtualization and that KVM is enabled in your kernel.

2.2. VirtualBox

2.2.1. Fedora

The VirtualBox site offers a repo file to be added in /etc/yum.repos.d/ and then perform a

 # yum update

to let yum know about your new repo and index it. Then perform a

 # yum install VirtualBox-4.0 dkms gcc
 # /etc/init.d/vboxdrv setup 
 # usermod -G vboxusers -a username 

and you should be set.

2.2.2. Debian

We don't want to take sides here, but it looks like Debian's documentation is better and more thorough. To install, just type

 # apt-get install linux-headers-$version virtualbox-ose 

which will install all necessary packages, compile the kernel module for you and set it to load at startup.

2.2.3. Slackware

SlackBuilds.org has a VirtualBox (the Open Source edition, which is the only one we will talk about here) entry. For a HOWTO on using slackbuilds, go here. Additional info can be found on linuxquestions.org, info which you might find useful especially if you are on a Slackware 13.37 64-bit machine. Remember to read the README of the slackbuild and it should be ok.

2.2.4. Arch

Arch's documentation is helpful and complete, on par with Debian's, so you should have no issues finding the info you need. All you need to do installation-wise is

 # pacman -S virtualbox qt 

and you're set. You will need Qt for the GUI, if you need just CLI, don't install it.The rest of the configuration steps are resemblant to Fedora's so use the documentation to be able to get started.

3. Usage

3.1. KVM

As you might have noticed, we're against copying blindly from a manual page and trying to substitute said manual. We will outline general and usual options and command-line flags (yes, we recommend the usage of CLI when working with KVM and GUI when working with VirtualBox), but you reading the manual page is essential, especially when dealing with a piece of somewhat complex software like this.

3.1.1. Storage

We recommend you create a separate directory where you will store the virtual disk images and another separate directory that will hold the ISO files used for installation. In the end, only you know best how to organize your files, but this setup, we think, will make your life easier. For the sake of example, let' s say we want to install Fedora. We noticed when using KVM a somewhat smaller bandwidth than that available from our ISP, a problem that VirtualBox doesn't seem to have. So, we install from DVD :

 $ cd ~ && mkdir iso && cd iso/ 
 $ wget -c ftp://ftp.heanet.ie/mirrors/fedora/linux/releases/15/Fedora/\
x86_64/iso/Fedora-15-x86_64-DVD.iso 

Of course, if you prefer curl, kget or other download manager, use it. The same applies to the mirror : use one close to you for faster downloading. Now to set up our virtual disk(s) environment :

 $ cd ~ && mkdir kvmhdd && cd kvmhdd 
 $ kvm-img create fedora15.img 15G 

Before going further we recommend you run kvm-img with no arguments to see its main options and disk formats. What the second line above does : it creates a virtual disk file (img format) named fedora15 and having 15 gigabytes in size.

3.1.2. Installation

Now we're ready to start the installation of Fedora 15 on our newly created disk :

 $ kvm -m 768 -boot d -cdrom ~/iso/Fedora-15-x86_64-DVD.iso -hda fedora15.img & 

What we just did : -m represents the size in megabytes of allocated memory, -boot tells kvm the primary boot device (think Windows drive letters), -cdrom is obvious, -hda is the first hard disk (you can use more than one disk, if you have the space, with -hdb being the second disk, and so on), and we used the ampersand at the end of the command line to regain control of the terminal. Ctrl+Alt will give/take control to/from the virtual machine in terms of mouse capture.

3.1.3. Using the virtual machine

After you install Fedora, in order to boot the newly installed system, change '-boot d' to '-boot c' and eliminate the '-cdrom ...' part if you don't need it, or use the host's CD-ROM drive with -cdrom /dev/sr0, for example :

 $ kvm -m 768 -boot c -hda fedora15.img & 

Suggestion: create a file in the kvmhdd (or the name you chose for the work directory) named fedora15.sh that will contain the above line, make it executable and, when you need it, just run it :

 $ cd ~/kvmhdd && echo "kvm -m 768 -boot c -hda fedora15.img &" > fedora15.sh && \
chmod +x fedora15.sh && ./fedora15.sh 

That's about it. Should you need more options, refer to the kvm manual page as it's well written and will help you for certain.

3.2. VirtualBox

3.2.1. Creating a virtual machine

VirtualBox's GUI takes the wizard approach in order to help you create a new virtual machine and we see the interface as quite a smart piece of software. Press Ctrl + N to create a new VM, then enter the name you want to give it (use something suggestive, like "Fedora15x86_64". Based on such naming, VirtualBox will automatically determine what kind of system you want to set up and adjust itself automatically.


Next, choose the amount of memory you want Fedora to have, and VirtualBox tries to suggest a value depending on the OS and architecture. We recommend you don't go much lower, as your VM might freeze because of out-of-memory problems (by the way, Anaconda will refuse to start the installation in graphical mode with less than 768 MB). Then create the hard disk and enter its size (the recommendation related to suggestions applies here too), allocating it dynamically (not a big problem if you don't, just that we found this method to be a little faster in terms of I/O) and you're set. In the main VirtualBox window you will see your new virtual machine listed and to the right, its' settings. Change them as you see fit, and be careful to boot from DVD. Also, if you have multiple cores and want to use them, the System section is the place to go to.

3.2.2. Running a virtual machine

After installation, halt the VM and change the boot order so you can boot from the hard disk, then start by right-clicking on the entry on the left and, of course, "Start". If everything's alright, you will now boot Fedora 15 in a VirtualBox environment. We said earlier that the guest OS on KVM and VBOX doesn't need alterations. While we were right, the guest experience can be improved in VirtualBox with things like full screen view or shared folders by installing the VirtualBox guest additions. This is basically a kernel module and some Xorg drivers for the guest, which you can install on your new Fedora by following the following steps :

 # yum update kernel && yum install kernel-devel kernel-headers dkms gcc gcc-c++ 

Reboot the VM after installing, then go to the Devices menu, select "Install Guest Additions", which will mount the existing Guest Additions ISO image on the guest (on /media), and, as root, just do

 # cd /media/VBOXADD$version && sh ./VBoxLinuxAdditions.run 

Afterwards, restart again and you will have advanced features of VirtualBox at your disposal.

4. Conclusion

When it comes to choosing virtualization solutions, as it is with editors, desktop environments or even operating systems, everyone has a personal opinion based on experience, reading and what their friends use. This is our experience, and we invite you to experiment and tweak until you find the best path to follow. There are, of course, other virtualization software packages but the limited space wouldn't let us write about all of them. These are the most appropriate solutions for a home user (Open Source, that is), but that does not mean they aren't used at enterprise-level too. We just hope this will help you get to get started.


posted @ 2016-06-13 21:52  张同光  阅读(122)  评论(0编辑  收藏  举报