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.
As always, you will need access to the root account or an account with sudo
privileges.
Follows these steps to install KVM and launch a virtual machine using cloud image:
$ sudo apt-get install kvm cloud-utils genisoimage bridge-utils
$ 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
$ qemu-img create -f qcow2 -b trusty.img.orig trusty.img
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.
$ cloud-localds my-seed.img user-data
/etc/network/interfaces
as follows:auto eth0 iface eth0 inet manual auto br0 iface br0 inet dhcp bridge_ports eth0
$ sudo service networking restart
$ 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.
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
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.
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:
xenial
with the correct Ubuntu version:$ sudo vi /etc/apt/sources.list deb http://download.virtualbox.org/virtualbox/debian xenial contrib
wget -q https://www.virtualbox.org/download/oracle_vbox_2016.asc -O- | sudo apt-key add -
$ sudo apt-get update && sudo apt-get install virtualbox-5.0