Background
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 APIs are widely used in the orchestration layer of hypervisors in the development of a cloud-based solution.
- Supported Hypervisors for libvirt
-
Relationship between Qemu, KVM and libvirt, virt-manager
- About virt-manager’s supporting tools
virt-manager is a GUI that can be used to create, destroy, stop, start and edit virtual machines and configure the virtual environment (such as virtual networks etc).
virt-install is a command line tool which provides an easy way to provision operating systems into virtual machines.
virt-viewer is a lightweight UI interface for interacting with the graphical display of virtualized guest OS. It can display VNC or SPICE, and uses libvirt to lookup the graphical connection details.
virt-clone is a command line tool for cloning existing inactive guests. It copies the disk images, and defines a config with new name, UUID and MAC address pointing to the copied disks.
virt-xml is a command line tool for easily editing libvirt domain XML using virt-install’s command line options.
virt-bootstrap is a command line tool providing an easy way to setup the root file system for libvirt-based containers.
- About virsh
virsh is a command line interface that can be used to create, destroy, stop start and edit virtual machines and configure the virtual environment (such as virtual networks etc)
-
Good reference wikis
virt-install examples | KVM virtualization commands cheatsheet
Best Practices
- Use virt-install to create a new VM
#~/bin/bash
OS_NAME=sgx-worker-node1
OS_IMAGE=<path-to>/ubuntu-20.04.3-desktop-amd64.iso
DISK_IMAGE=<path-to>/sgx-worker-node1.img
DISK_SIZE=200
virt-install \
--name $OS_NAME \
--memory 8192 \
--vcpus=8 \
--cpu host \
--qemu-commandline='-enable-kvm' \
--qemu-commandline='-cpu host,+sgx-provisionkey,+sgx2,+sgxlc,+sgx-kss' \
--qemu-commandline='-object memory-backend-epc,id=mem1,size=128M,prealloc=on' \
--qemu-commandline='-M sgx-epc.0.memdev=mem1,sgx-epc.0.node=0' \
--disk path=$DISK_IMAGE,size=$DISK_SIZE \
--os-variant ubuntu20.04 \
--location=$OS_IMAGE \
--network type=direct,source=enp6s0f0,source_mode=bridge,model=virtio \
--graphics vnc \
--autostart \
--accelerate \
--hvm &
- Clone VM to new one
#!/bin/sh
OLD_VM=sgx-develop-node1
NEW_VM=sgx-develop-node2
NEW_DISK_IMAGE=<path-to>/sgx-develop-node2.img
virt-clone --original $OLD_VM --name $NEW_VM --file $NEW_DISK_IMAGE
- Rename KVM VM with virsh
The syntax is:
# virsh domrename {domain} {new-name}
# To rename VM from foo to bar, type:
$ virsh shutdown foo
> Domain foo is being shutdown
# Now rename a VM, run:
$ virsh domrename foo bar
> Domain successfully renamed
# Start a VM/domain, enter:
$ virsh start bar
- Use virt-viewer to connect the guest VM
The syntax is:
# virt-viewer <domain> or <id>
# List the VM status
virsh list --all
Id Name State
-----------------------------------
8 sgx-worker-node1 running
9 sgx-worker-node2 running
- test-vm shut off
# View the runing vm
virt-viewer sgx-worker-node1