Linux Virtualization

Virtualization in Linux

What is Virtualization?

Full Virtualization

  • Hardware Virtual Machine (HVM)
  • Hypervisor creates an emulated device and translates data to real hardware
  • Some CPUs support direct hardware access for VMs
  • Uses QEMU (Quick Emulator) + Kernel-based Virtual Machine (KVM)

Paravirtualization

  • Guest OS must be modified to be aware it’s running on a virtual machine
  • Guest communicates directly with hardware
  • Memory and CPU still require emulation
  • QEMU + KVM provide paravirtualized devices
  • Xen hypervisor supports full paravirtualization

Hypervisors in Linux

  • Xen, Proxmox, and ESX - run directly on hardware
  • KVM - integrated into the Linux kernel

Managing Virtual Machines

  • Use QEMU tools directly
  • Use libvirt tools and Virtual Machine Manager
  • Manual scaling and modification of VMs

Creating a Virtual Machine with QEMU and KVM

Install QEMU and KVM

sudo apt install qemu qemu-kvm

Disk Image Formats

  • Raw: Pre-allocated file representing a disk
  • QCOW2 (QEMU Copy on Write): Expands dynamically as data is used

Creating a Disk Image

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

Booting a Virtual Machine

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

Modify a QEMU + KVM Virtual Machine

  • Modify memory: -m 2G → -m 4G
  • Change network settings: -net none → -net user
  • Add disks: -cdrom image.iso -hdb disk.qcow2
  • Enable KVM: -enable-kvm
  • Change video driver: -vga qxl

Creating a Virtual Machine with Libvirt Tools

Install Libvirt and Virtual Machine Manager

sudo apt install virt-manager

Create a Virtual Machine Using virt-install

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

Managing VMs with virsh

virsh start my-ubuntu
virsh shutdown my-ubuntu
virsh suspend my-ubuntu
virsh resume my-ubuntu

Modifying VM Memory

virsh dominfo my-ubuntu
virsh setmem my-ubuntu 2048M --config
virsh setmaxmem my-ubuntu 4G

Managing Virtual Machine Storage

  • Check partitions on the disk:
ls /dev/vd*
  • Modify partitions using fdisk:
fdisk /dev/vda
n → create new partition
w → save changes
  • Format the partition:
sudo mkfs.ext4 /dev/vda1
  • Mount the partition:
sudo mkdir /mnt/storage
sudo mount /dev/vda1 /mnt/storage
  • Add a new disk to the VM:
sudo qemu-img create -f qcow2 /var/lib/libvirt/images/disk2.qcow2 40G
virsh attach-disk my-ubuntu --source /var/lib/libvirt/images/disk2.qcow2 --driver qemu --subdriver qcow2 --target vdb --persistent
  • Detach the disk:
virsh detach-disk my-ubuntu /var/lib/libvirt/images/disk2.qcow2

Migrating a Virtual Machine Between Hosts

  • VMs can migrate between hypervisors
  • VM disks must be on shared storage
  • Live migration transfers the running state and RAM contents
  • Helps with load balancing and maintenance

Exploring Containers with LXC

What Are Containers?

  • VMs emulate entire operating systems
  • Containers isolate processes and resources
  • Containers share the host’s kernel
  • Uses Linux namespaces for isolation

Installing LXC

sudo apt install lxc1

Creating and Managing Containers

sudo lxc-create -n container1 -t ubuntu
sudo lxc-start -n container1
sudo lxc-console -n container1
sudo lxc-attach -n container1

Checking Container Information

sudo lxc-info -n container1
sudo lxc-ls

Stopping and Destroying Containers

sudo lxc-stop -n container1
sudo lxc-destroy -n container1

Conclusion

This guide provides an overview of virtualization in Linux, covering QEMU, KVM, libvirt, virtual machine management, and LXC containers. Understanding these concepts ensures efficient virtualization on Linux systems.