Linux: Virtualization
Hypervisors
KVM
= Kernel-based Virtual MachineQEMU
= Quick EMUlator
KVM
KVM is a built-in feature of the the Linux kernel that allows the operating system to act as Type 1 hypervisor. It runs virtual machines with their own kernel. QEMU
and virsh
are tools used to interact with KVM. KVM provides only the virtualization capabilities and we use QEMU
or virtsh
to create and manage the VMs.
QEMU
QEMU is a user space application that can emulate full hardware systems and run virtual machines entirely on its own, even without KVM (but slower). It emulates CPUs, hard drives, USB controllers, network cards, display adapters, ...
VM Architecture
VirtIO
VirtIO
provides a faster, more efficient way for virtual machines to communicate with the hypervisor. To fully use VirtIO, we need paravirtualized Drivers
installed within the guest OS. VirtIO provides:
virtio-net
for virtual networkingvirtio-blk
for virtual block storagevirtio-scsi
for virtual SCSi storagevirtio-fs
for virtual shared storagevirtio-gpu
for virtual GPUvirtio-serial
for high-speed guest to host communication
These drivers allow the guest to interact directly with the virtualized hardware with high performance.
Nested Virtualization
A feature that allows a VM to act as a hypervisor itself, running other virtual machines inside it.
Operations
VM States
Common VM states are:
Running
: actively consuming resourcesPaused
: temporarily haltedShut off
: completely powered downSuspended
: memory contents are saved to disk and the VM can be resumed laterCrashed
: failed VM
We can use virsh
to monitor the states of the VMs.
Disk Image Operations
A disk image is a file that acts as a virtual hard drive for a VM, storing all data of the VM. Disk image can be resized, cloned, snapshotted, or transferred easily between systems.
VM Resources
- CPU: VMs are assigned virtual CPUs (vCPUs)
- RAM: the amount of RAM reserved for the VM
- Storage: the virtual hardware space where the OS, apps, and data are stored. Common storage format are
.qcow2
,raw
,.vmdk
- Network: VMs are assigned one or more virtual NICs (vNICs)
Network Types
NAT
NAT = Network Address Translation
VMs share the host ip when talking to the outside world. Inbound traffic from the network cannot reach the VM.
Bridged
The VM is in the same network as the host. The VM gets its own IP address in the same network as the host.
Host-only
VMs can only talk to the host machine or other VMs in the same Host-only configuration
Routed
VMs have access to other networks through a virtual router
Open
VMs see all traffic on the network and freely interact with anything it can find
VM Tools
libvirt
provides a consistent API for managing common hypervisors. User can interact with libvirt via the command line using virsh
or GUI with virt-manager
.