CentOS 7: Install KVM and run virtual machine

This article will describe installing KVM and running virtual machine.

1 Install qemu-kvm and libvirt

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

$ sudo yum install -y qemu-kvm libvirt-client virt-install \
virt-manager virt-viewer bridge-utils

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 without sudo.

$ sudo gpasswd libvirt -a <username>

4 Create iso directory

Create iso directory for sharing iso images with multiple users. Move ubuntu-16.04-desktop-amd64.iso to iso directory.

$ sudo mkdir /var/lib/libvirt/iso
$ sudo mv ubuntu-16.04-desktop-amd64.iso /var/lib/libvirt/iso/
$ sudo chown qemu:qemu /var/lib/libvirt/iso/ubuntu-16.04-desktop-amd64.iso

5 VNC port

Allow VNC port with using vnc-server.xml so that other machine can access virtual machine.

The vnc-server.xml allows only 4 ports. If you need more, please change 5903 to bigger value.

$ sudo cat /usr/lib/firewalld/services/vnc-server.xml
<?xml version="1.0" encoding="utf-8"?>
<service>
  <short>Virtual Network Computing Server (VNC)</short>
  <description>A VNC server provides an external accessible X
session. Enable this option if you plan to provide a VNC server with
direct access. The access will be possible for displays :0 to :3. If
you plan to provide access with SSH, do not open this option and use
the via option of the VNC viewer.</description>
  <port protocol="tcp" port="5900-5903"/>
</service>

Allow VNC port.

$ sudo firewall-cmd --add-service=vnc-server --permanent
$ sudo firewall-cmd --reload

6 Enable kvm_intel nested

If Enable kvm_intel nested, you can create nested virtual machine on virtual machine.

$ sudo su -c \
'echo options kvm_intel nested=1 > /etc/modprobe.d/qemu-system-x86.conf'
$ sudo reboot

7 Create virtual machine with virt-install

Create disk with qemu-img and create virtual machine with virt-install. Please change vnc password.

$ sudo qemu-img create -f raw /var/lib/libvirt/images/ubuntu-16.04-vm.raw 80G
$ sudo virt-install --name ubuntu-16.04-vm --memory 1024 --vcpus 1 \
     --cdrom /var/lib/libvirt/iso/ubuntu-16.04-desktop-amd64.iso \
     --network bridge=br0 --os-type linux --os-variant ubuntu14.04 \
     --file /var/lib/libvirt/images/ubuntu-16.04-vm.raw \
     --graphics vnc,listen=0.0.0.0,password=password,keymap=ja

virt-viewer is running and virtual machine is displayed.

0001_virt-viewer.png

Close virt-viewer. Other machine can access virtual machine via VNC.

$ open vnc://<server>:<port>