Linux:Powerful Server Administration
上QQ阅读APP看书,第一时间看更新

Creating virtual machine with KVM

Ubuntu server gives you various options for your virtualization needs. You can choose from KVM, XEN, QEMU, VirtualBox, and various other proprietary and open source tools. KVM, or Kernel virtual machine, is the default hypervisor on Ubuntu. In this recipe, we will set up a virtual machine with the help of KVM. Ubuntu, being a popular cloud distribution provides prebuilt cloud images that can be used to start virtual machines in the cloud. We will use one of these prebuilt images to build our own local virtual machine.

Getting ready

As always, you will need access to the root account or an account with sudo privileges.

How to do it…

Follows these steps to install KVM and launch a virtual machine using cloud image:

  1. To get started, install the required packages:
    $ sudo apt-get install kvm cloud-utils \
    genisoimage bridge-utils
    

    Tip

    Before using KVM, you need to check whether your CPU supports hardware virtualization, which is required by KVM. Check CPU support with the following command:

    $ kvm-ok
    

    You should see output like this:

    INFO: /dev/kvm exists
    

    KVM acceleration can be used.

  2. Next, download the cloud images from the Ubuntu servers. I have selected the Ubuntu 14.04 Trusty image:
    $ wget http://cloud-images.ubuntu.com/releases/trusty/release/ubuntu-14.04-server-cloudimg-amd64-disk1.img -O trusty.img.dist
    

    This image is in a compressed format and needs to be converted into an uncompressed format. This is not strictly necessary but should save on-demand decompression when an image is used. Use the following command to convert the image:

    $ qemu-img convert -O qcow2 trusty.img.dist trusty.img.orig
    
  3. Create a copy-on-write image to protect your original image from modifications:
    $ qemu-img create -f qcow2 -b trusty.img.orig trusty.img
    
  4. Now that our image is ready, we need a cloud-config disk to initialize this image and set the necessary user details. Create a new file called user-data and add the following data to it:
    $ sudo vi user-data
    #cloud-config
    password: password
    chpasswd: { expire: False }
    ssh_pwauth: True
    

    This file will set a password for the default user, ubuntu, and enable password authentication in the SSH configuration.

  5. Create a disk with this configuration written on it:
    $ cloud-localds my-seed.img user-data
    
  6. Next, create a network bridge to be used by virtual machines. Edit /etc/network/interfaces as follows:
    auto eth0
    iface eth0 inet manual
    
    auto br0
    iface br0 inet dhcp
     bridge_ports eth0
    

    Note

    On Ubuntu 16.04, you will need to edit files under the /etc/network/interfaces.d directory. Edit the file for eth0 or your default network interface, and create a new file for br0. All files are merged under /etc/network/interfaces.

  7. Restart the networking service for the changes to take effect. If you are on an SSH connection, your session will get disconnected:
    $ sudo service networking restart
    
  8. Now that we have all the required data, let's start our image with KVM, as follows:
    $ sudo kvm -netdev bridge,id=net0,br=br0 \
    -net user -m 256 -nographic \
    -hda trusty.img -hdb my-seed.img
    

    This should start a virtual machine and route all input and output to your console. The first boot with cloud-init should take a while. Once the boot process completes, you will get a login prompt. Log in with the username ubuntu and the password specified in user-data.

  9. Once you get access to the shell, set a new password for the user ubuntu:
    $ sudo passwd ubuntu
    

    After that, uninstall the cloud-init tool to stop it running on the next boot:

    $ sudo apt-get remove cloud-init
    

    Your virtual machine is now ready to use. The next time you start the machine, you can skip the second disk with the cloud-init details and route the system console to VNC, as follows:

    $ sudo kvm -netdev bridge,id=net0,br=br0 \
    -hda trusty.img \
    -m 256 -vnc 0.0.0.0:1 -daemonize
    

How it works…

Ubuntu provides various options to create and manage virtual machines. The previous recipe covers basic virtualization with KVM and prebuilt Ubuntu Cloud images. KVM is very similar to desktop virtualization tools such as VirtualBox and VMware. It comes as a part of the Qemu emulator and uses hardware acceleration features from the host CPU to boost the performance of virtual machines. Without hardware support, the machines need to run inside the Qemu emulator.

After installing KVM, we have used Ubuntu cloud image as our pre-installed boot disk. Cloud images are prebuilt operating system images that do not contain any user data or system configuration. These images need to be initialized before being used. Recent Ubuntu releases contain a program called cloud-init, which is used to initialize the image at first boot. The cloud-init program looks for the metadata service on the network and queries user-data once the service is found. In our case, we have used a secondary disk to pass user data and initialize the cloud image.

We downloaded the prebuilt image from the Ubuntu image server and converted it to uncompressed format. Then, we created a new snapshot with the backing image set to the original prebuilt image. This should protect our original image from any modifications so that it can be used to create more copies. Whenever you need to restore a machine to its original state, just delete the newly created snapshot images and recreate it. Note that you will need to use the cloud-init process again during such restores.

This recipe uses prebuilt images, but you can also install the entire operating system on virtual machines. You will need to download the required installation medium and attach a blank hard disk to the VM. For installation, make sure you set the VNC connection to follow the installation steps.

There's more…

Ubuntu also provides the virt-manager graphical interface to create and manage KVM virtual machines from a GUI. You can install it as follows:

$ sudo apt-get install virt-manager

Alternatively, you can also install Oracle VirtualBox on Ubuntu. Download the .deb file for your Ubuntu version and install it with dpkg -i, or install it from the package manager as follows:

  1. Add the Oracle repository to your installation sources. Make sure to substitute xenial with the correct Ubuntu version:
    $ sudo vi /etc/apt/sources.list
    deb http://download.virtualbox.org/virtualbox/debian xenial contrib
    
  2. Add the Oracle public keys:
    wget -q https://www.virtualbox.org/download/oracle_vbox_2016.asc -O- | sudo apt-key add -
    
  3. Install VirtualBox:
    $ sudo apt-get update && sudo apt-get install virtualbox-5.0
    

See also