×
Community Blog How to Install and Configure KVM on Ubuntu 18.04

How to Install and Configure KVM on Ubuntu 18.04

In this tutorial, we will learn how to install and configure KVM on an Alibaba Cloud Elastic Compute Service (ECS) instance with Ubuntu 18.04.

By Hitesh Jethva, Alibaba Cloud Community Blog author.

KVM (Kernel-based Virtual Machine) is an open source full virtualization solution for Linux that allows a host machine to run multiple, isolated virtual environments called guests or virtual machines on Linux. It supports a wide range of guest operating systems and provides Virt-Manager (Virtual Machine Manager) for managing KVM based virtual machines. You can create, edit, start, stop and delete KVM-based virtual machines using Virt-Manager. It has the ability to move a running VM between physical hosts with no service interruption.

In this tutorial, we will learn how to install and configure KVM on Ubuntu 18.04 Desktop with an Alibaba Cloud Elastic Compute Service (ECS) instance.

Requirements

  • A fresh Alibaba Cloud instance with Ubuntu 18.04 desktop installed.
  • A static IP address 192.168.0.100 is set up to your Ubuntu 18.04 instance.
  • A root password is set up to your instance.

Launch Alibaba Cloud ECS Instance

Create a new ECS instance, choosing Ubuntu 18.04 as the operating system with at least 4GB RAM, and connect to your instance as the root user.

Once you are logged into your Ubuntu 18.04 desktop instance, run the following command to update your base system with the latest available packages.

apt-get update -y

Verify Hardware Support

KVM is a virtualization software, so you will need to verify whether your CPU supports virtualization or not.

You can check it with the following command:

egrep -c '(vmx|svm)' /proc/cpuinfo

Output:

2

Note: If output is 0 means that your CPU doesn't support hardware virtualization.

If output is 1 or more it support hardware virtualisation.

Next, you will also need to install cpu-checker tool to determine if your server is capable of running hardware accelerated KVM virtual machines.

You can install it with the following command:

apt-get install cpu-checker

Next, run the following command:

kvm-ok

Output:

INFO: /dev/kvm exists
KVM acceleration can be used

Install KVM

Next, you will need to install KVM and other required dependencies to your system. You can install all of them with the following command:

apt-get install qemu qemu-kvm libvirt-bin bridge-utils virt-manager virt-viewer  -y

Once all the packages are installed, start libvirtd service and enable it to start at boot with the following command:

systemctl start libvirtd
systemctl enable libvirtd

You can check the status of libvirtd with the following command:

service libvirtd status

Output:

¡ñ libvirtd.service - Virtualization daemon
   Loaded: loaded (/lib/systemd/system/libvirtd.service; enabled; vendor preset: enabled)
   Active: active (running) since Wed 2019-01-16 06:24:18 UTC; 8min ago
     Docs: man:libvirtd(8)
           https://libvirt.org
 Main PID: 5047 (libvirtd)
    Tasks: 19 (limit: 32768)
   CGroup: /system.slice/libvirtd.service
           ©À©€5047 /usr/sbin/libvirtd
           ©À©€5586 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/lib/libvirt/libvirt_leaseshelper
           ©ž©€5587 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/lib/libvirt/libvirt_leaseshelper

Jan 16 06:32:22 ubuntu1804 dnsmasq[5586]: reading /etc/resolv.conf
Jan 16 06:32:22 ubuntu1804 dnsmasq[5586]: using nameserver 127.0.0.53#53
Jan 16 06:32:22 ubuntu1804 dnsmasq[5586]: reading /etc/resolv.conf
Jan 16 06:32:22 ubuntu1804 dnsmasq[5586]: using nameserver 127.0.0.53#53
Jan 16 06:32:22 ubuntu1804 dnsmasq[5586]: reading /etc/resolv.conf
Jan 16 06:32:22 ubuntu1804 dnsmasq[5586]: using nameserver 127.0.0.53#53
Jan 16 06:32:24 ubuntu1804 dnsmasq[5586]: reading /etc/resolv.conf
Jan 16 06:32:24 ubuntu1804 dnsmasq[5586]: using nameserver 127.0.0.53#53
Jan 16 06:32:24 ubuntu1804 dnsmasq[5586]: reading /etc/resolv.conf
Jan 16 06:32:24 ubuntu1804 dnsmasq[5586]: using nameserver 127.0.0.53#53

Configure Bridge Networking

A bridged network is a dedicated network card to a virtual machine that allows guest machines to connect outside the network. You can configure Network bridge by editing /etc/netplan/50-cloud-init.yaml file:

nano /etc/netplan/50-cloud-init.yaml

Make the following changes:

network:
  version: 2
  ethernets:
    enp0s3:
      dhcp4: no
      dhcp6: no

  bridges:
    br0:
      interfaces: [enp0s3]
      dhcp4: no
      addresses: [192.168.0.100/24]
      gateway4: 192.168.0.1
      nameservers:
        addresses: [192.168.0.1]

Save and close the file. Then, apply these changes with the following command;

netplan apply

You can now check the status of bridge with the following command:

networkctl status -a

Output:

¡ñ 1: lo
       Link File: /lib/systemd/network/99-default.link
    Network File: n/a
            Type: loopback
           State: carrier (unmanaged)
         Address: 127.0.0.1
                  ::1

¡ñ 2: enp0s3
       Link File: /lib/systemd/network/99-default.link
    Network File: /run/systemd/network/10-netplan-enp0s3.network
            Type: ether
           State: carrier (configured)
            Path: pci-0000:00:03.0
          Driver: e1000
          Vendor: Intel Corporation
           Model: 82540EM Gigabit Ethernet Controller (PRO/1000 MT Desktop Adapter)
      HW Address: 08:00:27:58:d6:9b (PCS Systemtechnik GmbH)

¡ñ 3: virbr0
       Link File: /lib/systemd/network/99-default.link
    Network File: n/a
            Type: ether
           State: no-carrier (unmanaged)
          Driver: bridge
      HW Address: 52:54:00:e0:0b:f0
         Address: 192.168.122.1

¡ñ 4: virbr0-nic
       Link File: /lib/systemd/network/99-default.link
    Network File: n/a
            Type: ether
           State: off (unmanaged)
          Driver: tun
      HW Address: 52:54:00:e0:0b:f0

¡ñ 5: br0
       Link File: /lib/systemd/network/99-default.link
    Network File: /run/systemd/network/10-netplan-br0.network
            Type: ether
           State: routable (configured)
          Driver: bridge
      HW Address: 3a:26:a7:35:e0:af
         Address: 192.168.0.100
                  fe80::3826:a7ff:fe35:e0af
         Gateway: 192.168.0.1
                  fe80::225:5eff:fe60:65c (Shanghai Dare Technologies Co.,Ltd.)
             DNS: 192.168.0.1

Create Virtual Machine

You can create virtual machine using virt-manager utility.

Run the following command to start the virt-manager:

virt-manager

You should see the following image:

1

Next, right click on localhost(QEMU) and click on New button. You should see the following page:

2

Next, provide your virtual machine name and choose Local install media. Then, click on the Forward button. You should see the following page:

3

Next, provide your ISO image path, OS type and Version. Then, click on the Forward button. You should see the following page:

4

Next, configure Memory and CPU as per your need. The, click on the Forward button. You should see the following page:

5

Next, provide the amount of storage that you want to assign to a virtual machine. The, click on the Forward button. You should see the following page:

6

Next, verify all the settings and click on the Finish button to create a virtual machine. Once VM is created, Virtual Machine Manager will start the console for OS installation as shown below:

7

You can now start, stop, reset, clone, and migrate virtual machines by right-clicking on the selected virtual machine.

Manage KVM Virtual Machines Using CLI

KVM provides virt-install CLI tool to create and configure virtual machines, virsh CLI tool to start, stop, reset, delete and clone the running virtual machines.

You can list all configured guests using the following command:

virsh list --all

Output:

 Id    Name                           State
----------------------------------------------------
 -     Alibaba-Ubuntu14.04            shut off

You can start the virtual machine with the following command:

virsh start Alibaba-Ubuntu14.04

Output:

Domain Alibaba-Ubuntu14.04 started

Now, list all virtual machines again with the following command:

virsh list --all

You should see the virtual machine as running state in the following output:

 Id    Name                           State
----------------------------------------------------
 6     Alibaba-Ubuntu14.04            running

To pause the running virtual machine, run the following command:

virsh suspend Alibaba-Ubuntu14.04

Output:

Domain Alibaba-Ubuntu14.04- suspended

To resume the paused virtual machine, run the following command:

virsh resume Alibaba-Ubuntu14.04

Output:

Domain Alibaba-Ubuntu14.04 resumed

To shutdown the running virtual machine, run the following command:

virsh shutdown Alibaba-Ubuntu14.04

Output:

Domain Alibaba-Ubuntu14.04 is being shutdown

To create a new virtual machine by cloning an existing one with the following command:

virt-clone --original=Alibaba-Ubuntu14.04 --name=Alibaba-Ubuntu14.04-Clone --file=/var/lib/libvirt/images/Alibaba-Ubuntu14.04-clone.img

Output:

Allocating 'Alibaba-Ubuntu14.04-clone.img'             | 8.0 GB     00:02     

Clone 'Alibaba-Ubuntu14.04-Clone' created successfully.

Now, list all the virtual machine:

virsh list --all

You should see your cloned virtual machine in the following output:

 Id    Name                           State
----------------------------------------------------
 -     Alibaba-Ubuntu14.04            shut off
 -     Alibaba-Ubuntu14.04-Clone      shut off

You can delete your virtual machine with the following command:

virsh undefine Alibaba-Ubuntu14.04-Clone

Output:

Domain Alibaba-Ubuntu14.04-Clone has been undefined

You can also create a new virtual machine using virt-install command line utility. This command needs multiple inputs from us to create a virtual machine such as CPU, Memory, disk, network, installation media location, OS variant and more as shown below:

virt-install --name=Ubuntu-16.10 --vcpus=1 --ram=1024 --cdrom=/Data/ISO/ubuntu-16.10-desktop-amd64.iso --disk path=/var/lib/libvirt/images/ubuntu-16.10.img,size=10 --os-type=linux

After running the above command, virt-install will create a virtual machine and starts virt viewer console for OS installation as shown in the following image:

8

1 0 0
Share on

Hiteshjethva

38 posts | 4 followers

You may also like

Comments

256523442801475348 June 22, 2020 at 11:31 am

Hey, just wonder what is the family type that i can use for this to work. I am looking the instance that is cost effective to me.