KVM

Revision as of 20:07, 16 October 2020 by Mhan (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Kernel-based Virtual Machine. A hypervisor technology featuring tight integration to Linux kernel.

Installation

Creating a RHEL 8.2 VM

  • Last tested on RHEL 8.2 (2020-06-30)
# virt-install --name=iqbal --vcpus=2 --memory=2048 --cdrom=/home/mhan/Downloads/rhel-8.2-x86_64-boot.iso --disk size=5 --os-variant=rhel8.2 --graphics vnc

Checking which port VNC is connected to

# virsh vncdisplay vm-name

Attaching a CD-ROM

# virsh attach-disk iqbal /var/lib/libvirt/boot/rhel-8.2-x86_64-boot.iso --type cdrom --mode readonly --target sda --targetbus sata

Installing on CentOS 7.5

  • Tested on CentOS 7.5.1804

Install packages and start libvirtd

Install required packages:

# yum install qemu-kvm libvirt libvirt-python libguestfs-tools virt-install

Start the libvirtd service:

# systemctl enable libvirtd
# systemctl start libvirtd

Make sure KVM module loaded using lsmod command:

# lsmod | grep -i kvm
kvm_intel             178927  0
kvm                   578558  1 kvm_intel
irqbypass              13503  1 kvm

Configuration

Configure bridged networking

Libvirtd configures a dhcpd-based network bridge by default. You can check in the following way:

# brctl show
bridge name     bridge id               STP enabled     interfaces
virbr0          8000.525400f0c02d       yes             virbr0-nic

# virsh net-list
 Name                 State      Autostart     Persistent
----------------------------------------------------------
 default              active     yes           yes

Default configuration allows all VMs to access other VMs but not the network that the host machine is on. You can check the private network created by default:

# virsh net-dumpxml default
<network>
  <name>default</name>
  <uuid>51beab57-eb0c-40b9-bb5e-957f31c1b489</uuid>
  <forward mode='nat'>
    <nat>
      <port start='1024' end='65535'/>
    </nat>
  </forward>
  <bridge name='virbr0' stp='on' delay='0'/>
  <mac address='52:54:00:f0:c0:2d'/>
  <ip address='192.168.122.1' netmask='255.255.255.0'>
    <dhcp>
      <range start='192.168.122.2' end='192.168.122.254'/>
    </dhcp>
  </ip>
</network>

Add the following line to /etc/sysconfig/network-scripts/ifconfig-enp3s0:

BRIDGE=br0

Create a new file /etc/sysconfig/network-scripts/ifcfg-br0:

DEVICE="br0"
# I am getting ip from DHCP server #
BOOTPROTO="dhcp"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
ONBOOT="yes"
TYPE="Bridge"
DELAY="0"

Restart the networking service:

# systemctl restart NetworkManager

Check with brctl command:

# brctl show

Create and set up a storage pool

Define a storage pool:

# virsh pool-define-as --name libvirt-images --type dir --target /var/lib/libvirt/images --source-format raw
Pool libvirt-images defined

Set it to autostart on boot:

# virsh pool-autostart libvirt-images
Pool libvirt-images marked as autostarted

Start for now:

# virsh pool-start libvirt-images
Pool libvirt-images started

Instantiation

Create a VM

This example creates an Ubuntu VM:

# virt-install --virt-type=kvm --name ubuntu1 --memory 4096  --vcpus 2 --os-variant ubuntu17.04 --os-type Linux --cdrom=/var/lib/libvirt/boot/ubuntu-18.04-desktop-amd64.iso --network bridge=br0,model=virtio --graphics vnc --disk path=/var/lib/libvirt/images/ubuntu1.qcow2,size=40,bus=virtio,format=qcow2
WARNING  Unable to connect to graphical console: virt-viewer not installed. Please install the 'virt-viewer' package.
WARNING  No console to launch for the guest, defaulting to --wait -1

Starting install...
Allocating 'ubuntu1.qcow2'                                                                                              |  40 GB  00:00:00
Domain installation still in progress. Waiting for installation to complete.

Connect via VNC and complete installation

Get port number for VNC:

# virsh dumpxml ubuntu1 | grep vnc
    <graphics type='vnc' port='5900' autoport='yes' listen='127.0.0.1'>

Create a SSH tunnel:

$ ssh mhan@chara -L 5900:127.0.0.1:5900

and then use a VNC client to connect to 127.0.0.1 (aka localhost) with port of 5900.

Mount guest image

Make sure to have right packages:

# yum install libguestfs-tools

Mount to /mnt:

# guestmount -a /var/lib/libvirt/images/ubuntu1.qcow2 -m /dev/sda1 /mnt

Unmount /mnt:

# guestunmount /mnt


Commands

Deleting

Note storage files. Shutdown. And delete.

# virsh dumpxml --domain openbsd | grep 'source file'
# virsh shutdown --domain openbsd
# virsh destroy -- domain openbsd
# virsh undefine -- domain openbsd
# rm -rf /var/lib/libvirt/images/openbsd.qcow2

When there are snapshots.

# virsh snapshot-list --domain openbsd
# virsh snapshot-delete --domain openbsd --snapshotname 3sep2016u1

Removing with all storage.

# virsh undefine --domain mysql-server

Links