Xen Project
Updated: September 10, 2025Categories: Virtualization, Bare Metal
Printed from:
Xen Project Comprehensive Cheatsheet
1. Installation and Setup (Dom0 Configuration)
Prerequisites
Bash
123456789# Update system packages
sudo apt-get update && sudo apt-get upgrade
# Install Xen hypervisor and tools
sudo apt-get install xen-hypervisor-amd64 xen-tools
# Verify Xen installation
sudo xl list
Bootloader Configuration (GRUB)
Bash
1234567891011# Edit GRUB configuration
sudo nano /etc/default/grub
# Add Xen parameters
GRUB_DEFAULT="Xen 4.x"
GRUB_CMDLINE_XEN_DEFAULT="dom0_mem=4G,max:4G"
# Update GRUB
sudo update-grub
sudo update-grub2
2. Xen Hypervisor Architecture Overview
Key Components
- Hypervisor (Xen): Lightweight, bare-metal virtualization layer
- Dom0: Privileged control domain with direct hardware access
- DomU: Unprivileged guest domains (virtual machines)
Virtualization Types
- PV (Paravirtualized): Modified guest OS, direct hypervisor communication
- HVM (Hardware-Assisted): Full virtualization, hardware support required
- PVH: Hybrid model combining PV and HVM advantages
3. Domain Management (DomU Creation and Configuration)
Create Configuration File
Bash
12345678910# Sample PV domain configuration (/etc/xen/domains/example-pv.cfg)
name = "example-pv"
type = "pv"
kernel = "/boot/vmlinuz-linux"
ramdisk = "/boot/initramfs-linux.img"
memory = 2048
vcpus = 2
disk = ['phy:/dev/vg0/example-pv,xvda,w']
vif = ['bridge=xenbr0']
Create HVM Domain Configuration
Bash
123456789# Sample HVM domain configuration (/etc/xen/domains/example-hvm.cfg)
name = "example-hvm"
type = "hvm"
firmware = "bios"
memory = 4096
vcpus = 4
disk = ['file:/var/lib/xen/images/example-hvm.img,xvda,w']
vif = ['bridge=xenbr0,model=virtio']
4. xl Toolstack Commands
Domain Lifecycle Management
Bash
123456789101112131415161718# Create and start a domain
xl create /etc/xen/domains/example-pv.cfg
# List running domains
xl list
# Pause a domain
xl pause example-pv
# Unpause a domain
xl unpause example-pv
# Shutdown a domain
xl shutdown example-pv
# Force stop a domain
xl destroy example-pv
5. Virtual Machine Lifecycle Management
Common Lifecycle Operations
Bash
123456789# Save domain state (suspend)
xl save example-pv /path/to/save/state.img
# Restore domain state
xl restore /path/to/save/state.img
# Reboot domain
xl reboot example-pv
6. Storage Configuration
File-Based Storage
Bash
123456# Create disk image
dd if=/dev/zero of=/var/lib/xen/images/example.img bs=1G count=20
# LVM Storage
sudo lvcreate -L 20G -n example-lv vg0
Storage Configuration in Domain Config
disk = [
'file:/var/lib/xen/images/example.img,xvda,w',
'phy:/dev/vg0/example-lv,xvdb,w'
]
7. Network Configuration
Bridge Setup
Bash
12345678910111213# Install bridge utilities
sudo apt-get install bridge-utils
# Configure bridge (/etc/network/interfaces)
auto xenbr0
iface xenbr0 inet static
address 192.168.1.10
netmask 255.255.255.0
gateway 192.168.1.1
bridge_ports eth0
bridge_stp off
bridge_fd 0
VLAN Configuration
Bash
123# VLAN configuration in domain config
vif = ['bridge=xenbr0.10,vlan=10']
8. PV vs HVM Domain Types
PV Characteristics
- Requires modified OS
- Lower overhead
- Direct hypercall communication
- Better performance for Linux guests
HVM Characteristics
- Full hardware virtualization
- Supports unmodified OSes
- Hardware assist (Intel VT-x, AMD-V)
- Slightly higher performance overhead
9. Resource Allocation
CPU Pinning
Bash
1234# Pin domain to specific CPUs
xl vcpu-pin example-pv 0 0
xl vcpu-pin example-pv 1 1
Memory Management
Bash
123# Dynamic memory allocation
xl mem-set example-pv 4096
10. Live Migration
Prepare Migration
Bash
123456# Migrate domain to another host
xl migrate example-pv destination-host
# Live migration with memory transfer
xl migrate-live example-pv destination-host
11. Configuration Files
Key Configuration Locations
/etc/xen/xl.conf: Global Xen configuration/etc/xen/domains/: Individual domain configurations
Configuration Syntax Example
# Global settings
autoballoon="off"
cpuinfo_max_cpus=4
12. Security and Isolation
Mandatory Access Control
- Use SELinux or AppArmor
- Restrict domain privileges
- Implement strict network filtering
Hardening Recommendations
Bash
123# Disable domain creation for non-root users
xl_create_limit = 0
13. Performance Tuning
Optimization Techniques
- Use PV drivers
- Enable CPU flags:
+vmxfor Intel,+svmfor AMD - Use virtio network and block devices
- Disable unnecessary services in Dom0
14. Monitoring and Logging
Monitoring Tools
Bash
12345678# Xen-specific monitoring
xl top
xl info
xenstore-ls
# System-wide monitoring
xentop
Logging
Bash
1234# Xen hypervisor logs
/var/log/xen/hypervisor.log
/var/log/xen/xend.log
15. Troubleshooting
Common Issues
- Check Xen logs
- Verify domain configuration
- Ensure hardware virtualization is enabled in BIOS
- Check kernel compatibility
Debugging Commands
Bash
1234567# Check Xen version
xl version
# Show domain details
xl info
xl dmesg
16. Cloud Platform Integration
Cloud Management
- OpenStack with Xen
- CloudStack support
- Xen as a backend for cloud platforms
Pro Tips:
- Always keep Xen and guest systems updated
- Use configuration management tools
- Regularly test backup and migration procedures
Continue Learning
Discover more cheatsheets to boost your productivity