Linux virtualization

Virtualization

What is virtualization?

Full virtualization:

  • Hardware virtual machine (HVM)
  • Hypervisor creates emulated device (translate data to real one)
  • Some CPUs support direct access by VMs
  • Quick emulator (QEMU) + Kernel-based virtual machine (KVM)

Paravirtualization:

  • Guest OS must be modified to be aware it’s virtualized
  • Guest can communicate directly with hardware
  • Memory and processor are still emulated
  • QEMU + KVM offer paravirtualizated devices
  • Xen support full paravirtualization

Hypervisors:

  • Xen, Proxmox and ESX - run on the bare metal
  • KVM - part of the Linux kernel

Managing virtual machines:

  • Use QEMU tools directly
  • Use libvirt tools and Virtual Machine Manager
  • Scaling and modifying VMS can be done manually

Creating a virtual machine with QEMU and KVM

Installing "apt install qemu qemu-kvm".

Possible to install headless version of QEMU.

Files for QEMU "ls /usr/bin/qemu*"

Disk image formats:

  • Raw - a file that represents a disk, all space taken up at creation
  • QEMU copy on write (QCOW2)- a file that represent disk, space is taken up as it’s used

Generating disk:

qemu - img create -f qcow2 my-image.qcow2 60G
qemu - img create -f raw my-image.raw 60G

Creating virtual optical drive:

qemu - system x86_64 -cdrom Downloads/ubuntu-16.04.3.iso my-image.qcow2 -m 2G -enable-kvm

Starting new session

qemu - system x86_64 my-image.qcow2 -m 2G -enable-kvm

Modify a QEMU + KVM virtual machine

Possible to do next modifications:

  • Modify memory amount e.g. "-m 2G => -m 4G"
  • Change hardware e.g. "-net none => -net parallel none"
  • Add disks e.g. "-cdrom image.iso -hdb disk.qcow2"
  • Enabling kvm "-enable-kvm"
  • Changing video driver "-vga qxl"

Create a virtual machine with libvirt tools

  • Tools that provide a standard interface to manage VMs
  • Support KVM, Xen, and others
  • Provides virsh shell, virt-install, virt-manager
  • VMs are described by XML files, domain XML contains options and settings
  • Libvirt deamon keeps track of running VMs

Installing "apt install virt-manager".

Starting virtual machine using virt install.

virt-install
 --name my-ubuntu
 --memory 2048
 --disk size, format=qcow2,
     path=/var/lib/libvirt/images/my-ubuntu.qcow2
-- cdrom ~/Downloads/ubuntu.iso

Controlling a virtual machine with virsh

Managing domain state with virsh

start domain => start or boot
stop domain => power off gracefully
suspend domain => pause
resume domain => unpause
shutdown domain => power off gracefully

Modify virtual machine memory

Checking memory with virsh

virsh dominfo my-ubuntu-2
virsh setmem my-ubuntu-2 2048M => temporary set
virsh setmem my-ubuntu02 1G -- config => permanent
virsh setmaxmem my-ubuntu 4G => increase memory limit

Modify virtual machine storage

Checking partition on the disk:

cd /dev
ls
in the output we can see vda + vda1 + vda2 (partitions)

Modifiing the partition:

fdisk /dev/sda
n => create a new partition
w => write

Adding the file system to the partition "sudo mkfs.ext4 /dev/sda1".

Mounting "sudo mkdir /mnt/storage" && "sudo mount /dev/sda1 /mnt/storage".

Adding disk using virsh:

sudo qemu-img create -f qcow2 /var/lib/libvirt/images/disk2.qcow2 40G

virsh attach-disk my-ubuntu-2 --source /var/lib/libvirt/images/disk2.qcow2 --driver qemu --subdriver qcow2 --target vdb --persistent

virsh detach-disk my-ubuntu-2 /var/lib/libvirt/images/disk2.qcow2

Migrate a virtual machine between two hosts

Virtual machine migration:

  • VMs can be migrated between hypervisors
  • VM disks must be on shared storage
  • Migrate transfer running state and RAM contents
  • Allows for resource balancing and maintenance

Explore containers with LXC

Containers:

  • Virtual machines emulate a whole system
  • Containers isolate process and resources
  • Guests in containers share the kernel with the host
  • Make use of namespace

Installing "sudo apt install lxc1".

Creating container "sudo lxc-create -n container1 -t ubuntu".

Start container "sudo lxc-start -n container1".

Connect to the container "sudo lxc-console -n container1".

Connect to the root session "sudo lxc-attach -n container1".

Info "sudo lxc-info -n container1".

List "sudo lxc-ls".

Stoping container "sudo lxc-stop -n container1".

Destroy container "sudo lxc-destroy -n container1".