Tech Tutorial: 351.4 Libvirt Virtual Machine Management #
Exam Objective:
Candidates should be able to manage virtual machines using libvirt and related tools. This includes creating, modifying, monitoring, and deleting virtual machines.
Key Knowledge Areas:
- Virsh and its common operations
- Understanding and using libvirt XML configuration files
- Modifying running configurations and persistent configurations
- Monitoring VM guest performance and resource utilization
- Snapshot management
Utilities:
- virsh
- virt-install
- virt-clone
- virt-manager
Introduction #
Libvirt is an open-source API, daemon, and management tool for managing platform virtualization. It can be used to manage KVM, Xen, VMware ESXi, QEMU, and other virtualization technologies. These tools and APIs are commonly used for managing virtual machines and their underlying infrastructure in a standardized way across various hardware and software platforms.
In this tutorial, we will focus on managing virtual machines using virsh
, which is a command-line interface tool for managing guests and the hypervisor. We will also cover the use of virt-install
for creating VMs, virt-clone
for cloning existing VMs, and virt-manager
, a graphical tool for VM management.
Step-by-Step Guide #
1. Installing Libvirt and Tools #
First, ensure that libvirt and its tools are installed on your system. On a Debian-based system, you can install them using:
sudo apt-get update
sudo apt-get install qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils virt-manager
For Red Hat-based systems:
sudo yum install qemu-kvm libvirt libvirt-python libguestfs-tools virt-install
2. Starting and Enabling Libvirt #
To start and enable the libvirt daemon:
sudo systemctl start libvirtd
sudo systemctl enable libvirtd
3. Creating Virtual Machines #
You can create VMs using virt-install
. Here’s an example command to create a new VM:
sudo virt-install \
--name ubuntu-vm \
--ram 2048 \
--disk path=/var/lib/libvirt/images/ubuntu-vm.img,size=20 \
--vcpus 2 \
--os-type linux \
--os-variant ubuntu20.04 \
--network bridge=virbr0 \
--graphics none \
--console pty,target_type=serial \
--location 'http://archive.ubuntu.com/ubuntu/dists/focal/main/installer-amd64/' \
--extra-args 'console=ttyS0,115200n8 serial'
4. Managing Virtual Machines with Virsh #
Listing All Virtual Machines #
virsh list --all
Starting a Virtual Machine #
virsh start ubuntu-vm
Shutting Down a Virtual Machine #
virsh shutdown ubuntu-vm
Rebooting a Virtual Machine #
virsh reboot ubuntu-vm
Deleting a Virtual Machine #
virsh undefine ubuntu-vm
5. Modifying VM Configuration #
You can edit the configuration of a VM using virsh’s edit command:
virsh edit ubuntu-vm
This command opens the VM’s XML configuration in your default editor, allowing you to make changes.
6. Monitoring VM Performance #
To monitor VM performance, use:
virsh domstats ubuntu-vm
7. Managing Snapshots #
Creating a snapshot:
virsh snapshot-create-as --domain ubuntu-vm --name "snapshot1" --description "Test snapshot"
Listing snapshots:
virsh snapshot-list ubuntu-vm
Reverting to a snapshot:
virsh snapshot-revert ubuntu-vm snapshot1
Conclusion #
In this tutorial, we covered the basics of managing virtual machines using libvirt and its associated tools. By mastering these commands and utilities, system administrators can efficiently manage virtual environments, ensure resource optimization, and maintain a robust virtual infrastructure. Whether you are preparing for an exam or looking to enhance your professional skills in virtual machine management, understanding and utilizing libvirt is essential.