Ubuntu 18.04: Install KVM for virtualization

This article will describe installing KVM.

1 Install qemu-kvm and libvirt

Install qemu-kvm package and some packages for virtual machine operation.

$ sudo apt install -y qemu-kvm libvirt0 libvirt-bin \
  virt-manager libguestfs-tools

2 Bridge interface

The bridge interface is needed for accessing virtual machine via network from other machine.

3 libvirt group

The user in libvirt group can run virt-manager and virsh without sudo.

$ sudo gpasswd libvirt -a <username>
$ sudo reboot

4 Remote access via SSH

virt-manager and virsh provdes remote access via SSH. If you uses SSH public key authentication, you can omit inputing password.

Create SSH keys on client which uses remote access via SSH.

$ ssh-keygen -t rsa -f ~/.ssh/id_rsa -N ""
Generating public/private rsa key pair.
Your identification has been saved in /home/hiroom2/.ssh/id_rsa.
Your public key has been saved in /home/hiroom2/.ssh/id_rsa.pub.
<snip>
$ cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB... username@clientname

Add SSH public key entry to ~/.ssh/authorized_keys on server which is installed KVM.

$ cat ~/.ssh/authorized_keys
ssh-rsa AAAAB... username@clientname

Specify server URI with -c option on client.

$ virsh -c qemu+ssh://username@servername/system

Setting server URI to VIRSH_DEFAULT_CONNECT_URI is equal with specifying server URI with -c option. Write VIRSH_DEFAULT_CONNECT_URI settings in your .bashrc if you need.

$ export VIRSH_DEFAULT_CONNECT_URI=qemu+ssh://username@servername/system
$ virsh

In case of virt-manager, click "Add Connections" and specify server.

File -> Add Connection

0001_AddConnection.png

5 Add a pool for ISO file

Download ubuntu-18.04-desktop-amd64.iso from this page and add a pool for ISO file at /var/lib/libvirt/isos.

$ wget http://releases.ubuntu.com/18.04/ubuntu-18.04-desktop-amd64.iso
$ sudo mkdir /var/lib/libvirt/isos
$ sudo mv ubuntu-18.04-desktop-amd64.iso /var/lib/libvirt/isos/
$ sudo chown libvirt-qemu:libvirt \
/var/lib/libvirt/isos/ubuntu-18.04-desktop-amd64.iso

In case of virsh, use the following command.

$ virsh pool-define-as isos dir - - - - /var/lib/libvirt/isos
$ virsh pool-autostart isos
$ virsh pool-start isos

In case of virt-manager, left click on "QEMU/KVM" and right click on "Details".

$ virt-manager

0002_OpenDetails.png

Add a pool with pressing "+" button at the below left. Input "Name" and select "dri:Filesystem Directory" at "Type".

0003_AddANewStoragePoolStep1.png

Input "/var/lib/libvirt/isos" at "Target Path".

0004_AddANewStoragePoolStep2.png

The pool for ISO file is added.

0005_StoragePoolIsAdded.png

6 Create virtual machine with virt-manager

Left click on "QEMU/KVM" and right click "New".

$ virt-manager

0006_CreateANewVirtualMachine.png

Select "Local install media" and click "Forward".

0007_LocalInstallMedia.png

Select "Use ISO image" and click "Browse".

0008_UseISOImage.png

Select ISO file in "isos" pool and click "Choose Volume".

0009_ChooseVolume.png

ISO file is selected.

If "OS type" and "Version" is Unknown, select these manually. In case of Ubuntu 18.04, select Linux as "OS type" and nearest version of Ubuntu as "Version"

0010_IsoImageIsChosen.png

Input memory size and CPU number.

0011_ChooseMemoryAndCPUSettings.png

Create storage. By default, storage will be created at /var/lib/libvirt/images directory which is "default" pool.

0012_EnableStorageForThisVirtualMachine.png

Input virtual machine name.

Check "Customize configuration before install" for changing keyboard layout and storage type.

Select network. If you create bridge interface with nmcli or systemd-networkd, virt-manager does not display bridge interface. But you can use bridge interface with "Specify shared device name".

0013_ReadyToBeginTheInstallation.png

Select "IDE Disk1". Set "VirtIO" as "Disk bus".

0014_VirtualDisk.png

Select "Display Spice". Set your keyboard layout as "Keymap". In case of Japanese, keyboard layout is "ja".

0015_SpiceServer.png

Select "NIC". You can modify MAC address at this stage.

0016_VirtualNetworkInterface.png

Clicking "Begin Installation" starts installation. Install OS with the same way as physical machine.