Virtualization
Table of contents
- Virtualization concepts
- KVM and QEMU
- Management with libvirt
- virsh - Management CLI
- Network and storage
- Practical exercises
1 - Virtualization concepts
Types of virtualization
| Type | Examples | Usage |
|---|---|---|
| Type 1 | VMware ESXi, KVM, Hyper-V | Production |
| Type 2 | VirtualBox, VMware Workstation | Desktop |
KVM vs Containers
| Aspect | KVM (VMs) | Containers |
|---|---|---|
| Isolation | Complete (separate kernel) | Shared kernel |
| Overhead | Higher | Minimal |
| Boot | Minutes | Seconds |
| Security | Strong isolation | Weaker isolation |
| Use case | Different OSes, strong isolation | Microservices, scaling |
Hardware requirements
# Vérifier le support de virtualisation
grep -E 'vmx|svm' /proc/cpuinfo
# vmx = Intel VT-x
# svm = AMD-V
# Charger les modules
lsmod | grep kvm
# kvm_intel ou kvm_amd
🔝 Back to table of contents
2 - KVM and QEMU
Installation
# Debian/Ubuntu
apt install qemu-kvm libvirt-daemon-system \
libvirt-clients bridge-utils virtinst
# RHEL/CentOS
yum install qemu-kvm libvirt virt-install
# Démarrer libvirtd
systemctl enable --now libvirtd
# Ajouter l'utilisateur au groupe
usermod -aG libvirt $USER
Verification
# Vérifier KVM
kvm-ok
# INFO: /dev/kvm exists
# KVM acceleration can be used
# Vérifier libvirt
virsh list --all
# Vérifier la connexion
virsh uri
# qemu:///system
Create a VM with virt-install
# Depuis une ISO
virt-install \
--name ubuntu22 \
--ram 2048 \
--vcpus 2 \
--disk path=/var/lib/libvirt/images/ubuntu22.qcow2,size=20 \
--os-variant ubuntu22.04 \
--network bridge=virbr0 \
--graphics vnc,listen=0.0.0.0 \
--cdrom /var/lib/libvirt/boot/ubuntu-22.04-server.iso
# Depuis une image cloud
virt-install \
--name cloud-vm \
--ram 2048 \
--vcpus 2 \
--import \
--disk /var/lib/libvirt/images/cloud-vm.qcow2 \
--os-variant ubuntu22.04 \
--network bridge=virbr0 \
--noautoconsole
Disk formats
| Format | Advantages | Drawbacks |
|---|---|---|
| qcow2 | Snapshots, compression, thin provisioning | Slightly slower |
| raw | Maximum performance | No snapshots |
# Convertir
qemu-img convert -O qcow2 disk.raw disk.qcow2
# Infos sur un disque
qemu-img info disk.qcow2
# Redimensionner
qemu-img resize disk.qcow2 +10G
🔝 Back to table of contents
3 - Management with libvirt
libvirt architecture
Configuration files
# Configuration libvirt
/etc/libvirt/libvirtd.conf
# Définitions des VMs (XML)
/etc/libvirt/qemu/
# Images disque par défaut
/var/lib/libvirt/images/
# Logs
/var/log/libvirt/
XML definition of a VM
<!-- /etc/libvirt/qemu/myvm.xml -->
<domain type='kvm'>
<name>myvm</name>
<memory unit='MiB'>2048</memory>
<vcpu>2</vcpu>
<os>
<type arch='x86_64'>hvm</type>
<boot dev='hd'/>
</os>
<devices>
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2'/>
<source file='/var/lib/libvirt/images/myvm.qcow2'/>
<target dev='vda' bus='virtio'/>
</disk>
<interface type='network'>
<source network='default'/>
<model type='virtio'/>
</interface>
<graphics type='vnc' port='-1'/>
</devices>
</domain>
virt-manager (GUI)
# Installer
apt install virt-manager
# Lancer
virt-manager
# Connexion distante via SSH
virt-manager -c qemu+ssh://user@host/system
🔝 Back to table of contents
4 - virsh - Management CLI
Basic commands
# Lister les VMs
virsh list --all
# Démarrer/arrêter
virsh start myvm
virsh shutdown myvm # Graceful
virsh destroy myvm # Force
# Redémarrer
virsh reboot myvm
# Pause/Resume
virsh suspend myvm
virsh resume myvm
VM management
# Infos sur une VM
virsh dominfo myvm
# Console
virsh console myvm
# Afficher la config XML
virsh dumpxml myvm
# Éditer la config
virsh edit myvm
# Définir/Supprimer
virsh define myvm.xml
virsh undefine myvm
virsh undefine myvm --remove-all-storage
Snapshots
# Créer un snapshot
virsh snapshot-create-as myvm snap1 "Description"
# Lister les snapshots
virsh snapshot-list myvm
# Revenir à un snapshot
virsh snapshot-revert myvm snap1
# Supprimer
virsh snapshot-delete myvm snap1
Hot resources
# Ajouter de la RAM (si supporté)
virsh setmem myvm 4G --live
# Ajouter des vCPUs
virsh setvcpus myvm 4 --live
# Ajouter un disque
virsh attach-disk myvm /path/to/disk.qcow2 vdb --live
# Détacher
virsh detach-disk myvm vdb --live
Autostart
# Activer le démarrage automatique
virsh autostart myvm
# Désactiver
virsh autostart --disable myvm
# Lister les VMs en autostart
virsh list --autostart
🔝 Back to table of contents
5 - Network and storage
Virtual networks
# Lister les réseaux
virsh net-list --all
# Infos sur default
virsh net-info default
virsh net-dumpxml default
# Créer un réseau bridge
cat > br-network.xml << 'EOF'
<network>
<name>br-internal</name>
<forward mode='bridge'/>
<bridge name='br0'/>
</network>
EOF
virsh net-define br-network.xml
virsh net-start br-internal
virsh net-autostart br-internal
Create a bridge on the host
# /etc/network/interfaces (Debian)
auto br0
iface br0 inet dhcp
bridge_ports eth0
bridge_stp off
bridge_fd 0
# Ou avec netplan (Ubuntu)
# /etc/netplan/01-bridge.yaml
network:
version: 2
renderer: networkd
ethernets:
eth0:
dhcp4: no
bridges:
br0:
interfaces: [eth0]
dhcp4: yes
Storage pools
# Lister les pools
virsh pool-list --all
# Créer un pool de répertoire
virsh pool-define-as mypool dir --target /data/vms
virsh pool-build mypool
virsh pool-start mypool
virsh pool-autostart mypool
# Créer un volume dans le pool
virsh vol-create-as mypool disk1.qcow2 20G --format qcow2
# Lister les volumes
virsh vol-list mypool
VM migration
# Migration live (nécessite stockage partagé)
virsh migrate --live myvm qemu+ssh://dest-host/system
# Migration avec copie du disque
virsh migrate --live --copy-storage-all myvm qemu+ssh://dest-host/system
# Offline migration
virsh migrate --offline myvm qemu+ssh://dest-host/system
🔝 Back to table of contents
6 - Practical exercises
Exercise 1: Create a VM
Create a VM with 2 vCPUs, 2GB RAM and a 10GB disk:
Solution
# Créer le disque
qemu-img create -f qcow2 /var/lib/libvirt/images/test-vm.qcow2 10G
# Créer la VM
virt-install \
--name test-vm \
--ram 2048 \
--vcpus 2 \
--disk path=/var/lib/libvirt/images/test-vm.qcow2 \
--os-variant generic \
--network network=default \
--graphics vnc \
--cdrom /path/to/iso
Exercise 2: Snapshot and restore
Create a snapshot, modify the VM, then restore it:
Solution
# Créer le snapshot
virsh snapshot-create-as test-vm clean-state "État initial"
# Modifier la VM (ex: supprimer des fichiers)
# Restaurer
virsh snapshot-revert test-vm clean-state
# Vérifier
virsh snapshot-list test-vm
Quiz
Q1. Which command displays the XML configuration of a VM?
Answer
virsh dumpxml nom-vm
Q2. Which disk format supports snapshots?
Answer
qcow2
🔝 Back to table of contents
Key takeaways
- KVM = Linux hypervisor built into the kernel
- libvirt = unified management API
- virsh = CLI to manage VMs
- qcow2 = format with snapshots and thin provisioning
virsh list --allto see all VMs- Use bridges for VM networking
- Snapshots let you save the state