title | description | header | author | footer | keywords | marp | paginate | theme | backgroundImage |
---|---|---|---|---|---|---|---|---|---|
Getting Started with Kernel-based Virtual Machine (KVM) |
Presentation slides for Getting Started with Kernel-based Virtual Machine (KVM) workshop at Open Source Summit Europe 2022. |
**[Getting Started with Kernel-based Virtual Machine (KVM)](https://osseu2022.sched.com/event/15z24)** / [Open Source On-Ramp](https://osseu2022.sched.com/overview/type/Open+Source+On-Ramp) / **[Open Source Summit Europe 2022](https://osseu2022.sched.com/)** |
Leonard Sheng Sheng Lee |
[Leonard Sheng Sheng Lee](https://github.com/sheeeng) / Made with [Marp](https://marp.app/) / Participants Agree to Abide by [Code of Conduct](https://events.linuxfoundation.org/open-source-summit-europe/attend/code-of-conduct/) |
linux,kvm,virtualization,marp,marp-cli,slide |
true |
true |
uncover |
- Want to get started with Kernel-based Virtual Machine (KVM)?
- Want to run a virtual machine on your system using open source technologies?
- Want to interact with KVM virtual machines from command line interface (CLI)?
In this tutorial, Leonard will be teaching people to familiarize themselves with KVM technologies, which allows virtual machines to run with near native performance.
Participants must have a basic knowledge of how the Linux operating system works, and must have a recent Linux based operating system running on a portable computer to join this tutorial.
We will be focusing on tasks such as creating, accessing, modifying, and deleting KVMs, primarily using CLI and if time permits, using a graphical user interface (GUI) too.
At the end of this tutorial, participants are expected to know how to check if KVM is supported on their computer hardware and manage KVMs with confidence.
- Setup Kernel-based Virtual Machine (KVM)
- Manage KVM Using:
- Command Line Interface (CLI)
- Graphical User Interface (GUI)*
- An open source virtualization technology built into Linux®. Turn Linux into a hypervisor.
- Allows a host machine to run multiple, isolated virtual environments called guests or virtual machines (VMs).
- Available from Linux 2.6.20 or newer.
- KVM converts Linux into a type-1 (bare-metal) hypervisor.
- Every VM is implemented as a regular Linux process, scheduled by the standard Linux scheduler, with dedicated virtual hardware components.
- A hypervisor is software that creates and runs virtual machines (VMs).
- It is also called a virtual machine monitor (VMM).
- It isolates the hypervisor operating system and resources from the virtual machines and enables the creation and management of those VMs.
- QEMU (Quick Emulator) is part of the KVM experience being the userspace backend for it, but it also can be used for hardware without virtualization extensions by using its Tiny Code Generator (TCG) mode.
Domain0
is the first domain started by the hypervisor at boot, and will be running a Linux OS. This domain is privileged: it may access the hardware and can run the tools that manage other domains.
- These other domains are referred to as
DomU
s with theU
standing for "user". They are unprivileged, and the equivalent to a guest system or guest virtual machine.
- KVM requires a CPU with virtualization extensions.
- Intel® Virtualization Technology (Intel® VT)
- CPU flag is
vmx
(Virtual Machine Extensions).
- CPU flag is
- AMD virtualization (AMD-V)
- CPU flag is
svm
(Secure Virtual Machine).
- CPU flag is
- Intel® Virtualization Technology (Intel® VT)
egrep --count '^flags.*(vmx|svm)' /proc/cpuinfo
- If output is 0, your system does not support the relevant virtualization extensions or disabled on BIOS. You can still use QEMU/KVM, but the emulator will fall back to software virtualization, which is much slower.
dnf groupinfo virtualization
dnf group install \
virtualization \
--with-optional \
--assumeyes
- See Fedora's Installation Documentation.
# apt-get install \
bridge-utils \
qemu-kvm \
virt-manager
# yum install \
libvirt \
qemu-kvm \
virt-install \
virt-install \
virt-manager
-
The
libvirtd
service is a server side daemon and driver required to manage the virtualization capabilities of the KVM hypervisor. -
Start
libvirtd
service and enable it on boot.
systemctl start libvirtd
systemctl enable libvirtd
- Verify that the KVM kernel modules are properly loaded.
lsmod | egrep 'kvm_*(amd|intel)'
- If output contains kvm_intel or kvm_amd, KVM is properly configured.
- Append current user to
kvm
andlibvirt
groups to create and manage virtual machines.
usermod --append --groups=kvm,libvirt ${USER}
cat /etc/group | egrep "^(kvm|libvirt).*${USER}"
- Log out and log in again to apply this modification.
# cp /etc/libvirt/qemu.conf /etc/libvirt/qemu.conf.original
# sed --in-place \
"s,\#user = \"root\",\#user = \"${USER}\",g" \
/etc/libvirt/qemu.conf
# sed --in-place \
"s,\#group = \"root\",\#group = \"libvirt\",g" \
/etc/libvirt/qemu.conf
# diff --unified \
/etc/libvirt/qemu.conf.original \
/etc/libvirt/qemu.conf
systemctl restart libvirtd
$ virt-install \
--name Debian11 --os-variant debian11 --description 'Debian11' \
--vcpus 2 --ram 2048 \
--location \
https://ftp.debian.org/debian/dists/stable/main/installer-amd64 \
--network bridge=virbr0 \
--graphics vnc,listen=127.0.0.1,port=5901 \
--noreboot --noautoconsole \
--extra-args 'console=ttyS0,115200n8 serial'
$ virt-viewer --connect qemu:///session --wait Debian11
$ virsh console Debian11
Connected to domain 'Debian11'
Escape character is ^] (Ctrl + ])
Several virsh
commands are affected by the type of the guest virtual machine:
Transient
or Persistent
.
During the life cycle of a virtual machine, libvirt
will classify the guest as any of the following states:
Undefined
, Shut off
, Running
, Paused
, Saved
virsh version
virsh version --daemon
virsh connect [hostname-or-URI] [--readonly]
The most commonly used URIs are:
qemu:///system
, qemu:///session
, lxc:///
For example, establish a session to connect to your set of guest virtual machines (VMs), with you as the local user:
virsh connect qemu:///session
virsh list --all
virsh list --inactive
virsh hostname
virsh sysinfo
virsh screenshot $<Domain-{Id,Name,Uuid}> [imagefilepath] [--screen screenID]
Example: virsh screenshot Debian11
virsh start $<Domain-{Id,Name,Uuid}> [--console] [--paused] [--autodestroy] [--bypass-cache] [--force-boot]
Starts the $<Domain-{Id,Name,Uuid}>
that you already created and is currently in the inactive state.
virsh autostart [--disable] $<Domain-{Id,Name,Uuid}>
Example: virsh autostart Debian11
virsh reboot $<Domain-{Id,Name,Uuid}> [--mode <RebootModeName>]
Example: virsh reboot Debian11 --mode initctl
virsh save [--bypass-cache] domain file [--xml string] [--running] [--paused] [--verbose]
Example: virsh save Debian11 Debian11-Configuration.xml --running
virsh define $<Domain-{Id,Name,Uuid}>.xml
Example: virsh define Debian11-Configuration.xml
virsh save-image-dumpxml file --security-info
Example: virsh save-image-dumpxml Debian11-Configuration.xml
virsh save-image-edit <file> [--running] [--paused]
Example: virsh save-image-edit Debian11-Configuration.xml --running
virsh restore <file> [--bypass-cache] [--xml /path/to/file] [--running] [--paused]
Example: virsh restore Debian11-Configuration.xml --running
virsh resume $<Domain-{Id,Name,Uuid}>
virsh domhostname $<Domain-{Id,Name,Uuid}>
virsh dominfo $<Domain-{Id,Name,Uuid}>d/Domain-Name/Uuid}
virsh domid $<Domain-{Id,Name,Uuid}>
virsh domjobabort $<Domain-{Id,Name,Uuid}>
virsh domjobinfo $<Domain-{Id,Name,Uuid}>
virsh domname $<Domain-{Id,Uuid}>
virsh domstate $<Domain-{Id,Name,Uuid}>
virsh domcontrol $<Domain-{Id,Name,Uuid}>
virsh shutdown $<Domain-{Id,Name,Uuid}> [--mode modename]
Example: virsh shutdown Debian11 --mode acpi
virsh suspend $<Domain-{Id,Name,Uuid}>
Example: virsh suspend Debian11
virsh reset $<Domain-{Id,Name,Uuid}>
Example: virsh reset Debian11
virsh managedsave $<Domain-{Id,Name,Uuid}> --bypass-cache --running | --paused | --verbose
Example: virsh managedsave Debian11 --running
qemu-img snapshot [ -l | -a snapshot | -c snapshot | -d snapshot ] filename
virsh undefine $<Domain-{Id,Name,Uuid}> [--managed-save] [storage] [--remove-all-storage] [--wipe-storage] [--snapshots-metadata] [--nvram]
Example: virsh undefine Debian11 --remove-all-storage
virsh destroy $<Domain-{Id,Name,Uuid}>
Example: virsh undefine Debian11 --remove-all-storage
$ virsh shutdown Debian11 # Graceful Shutdown
Domain 'Debian11' is being shutdown
$ virsh destroy Debian11 # Force Shutdown
Domain 'Debian11' destroyed
$ virsh undefine Debian11
Domain 'Debian11' has been undefined
$ virsh nodeinfo
$ virsh edit
$ virt-df
$ virt-top
$ virt-viewer
$ virsh pool-list --all
$ virsh pool-destroy
$ virsh pool-undefine
virt-install --os-variant list
$ virt-install \
--name Ubuntu --os-variant ubuntu22.04 --description 'Ubuntu' \
--vcpus 2 --ram 2048 \
--network bridge=virbr0,model=virtio \
--graphics vnc,listen=127.0.0.1,port=5902 \
--cdrom ~/Downloads/ubuntu-22.04-desktop-amd64.iso \
--noreboot --noautoconsole
$ virt-viewer --connect qemu:///session --wait Ubuntu
$ virt-install \
--name Ubuntu --os-variant ubuntu20.04 --description 'Ubuntu' \
--vcpus 2 --ram 2048 \
--location \
http://archive.ubuntu.com/ubuntu/dists/focal/main/installer-amd64/ \
--network bridge=virbr0,model=virtio \
--graphics vnc,listen=127.0.0.1,port=5902 \
--noreboot --noautoconsole \
--extra-args='console=ttyS0,115200n8 serial edd=off'
$ virt-viewer --connect qemu:///session --wait Ubuntu
$ virsh console Ubuntu
$ virt-install \
--name Fedora --os-variant fedora36 --description 'Fedora' \
--vcpus 2 --ram 2048 \
--network bridge=virbr0,model=virtio \
--graphics vnc,listen=127.0.0.1,port=5904 \
--cdrom ~/Downloads/Fedora-Workstation-Live-x86_64-36-1.5.iso \
--noreboot --noautoconsole
$ virt-viewer --connect qemu:///session --wait Fedora
$ virt-install \
--name Fedora --os-variant fedora36 --description 'Fedora' \
--vcpus 2 --ram 2048 \
--location \
https://download.fedoraproject.org/pub/fedora/linux/releases/36/Server/x86_64/os \
--network bridge=virbr0,model=virtio \
--graphics vnc,listen=127.0.0.1,port=5904 \
--noreboot \
--extra-args='console=ttyS0,115200n8 edd=off'
$ virsh console Fedora
$ virt-install \
--name AlmaLinux --os-variant almalinux9 --description 'AlmaLinux' \
--vcpus 2 --ram 3072 \
--network bridge=virbr0,model=virtio \
--graphics vnc,listen=127.0.0.1,port=5903 \
--cdrom ~/Downloads/AlmaLinux-9.0-x86_64-dvd.iso \
--noreboot --noautoconsole
$ virt-viewer --connect qemu:///session --wait AlmaLinux
$ virt-install \
--name AlmaLinux --os-variant almalinux9 --description 'AlmaLinux' \
--vcpus 2 --ram 3072 \
--location \
https://almalinux.uib.no/9.0/BaseOS/x86_64/os/ \
--network bridge=virbr0,model=virtio \
--graphics vnc,listen=127.0.0.1,port=5905 \
--noreboot \
--extra-args='console=ttyS0,115200n8 edd=off'
$ virsh console AlmaLinux
$ virt-install \
--name CentOS --os-variant centos-stream9 --description 'CentOS' \
--vcpus 2 --ram 3072 \
--network bridge=virbr0,model=virtio \
--graphics vnc,listen=127.0.0.1,port=5902 \
--cdrom ~/Downloads/CentOS-Stream-9-latest-x86_64-dvd1.iso \
--noreboot --noautoconsole
$ virt-viewer --connect qemu:///session --wait CentOS
$ virt-install \
--name CentOS --os-variant centos-stream9 --description 'CentOS' \
--vcpus 2 --ram 3072 \
--location \
https://mirror.netsite.dk/centos-stream/9-stream/BaseOS/x86_64/os/ \
--network bridge=virbr0,model=virtio \
--graphics vnc,listen=127.0.0.1,port=5904 \
--noreboot \
--extra-args='console=ttyS0,115200n8 edd=off'
$ virt-viewer --connect qemu:///session --wait CentOS
$ virsh console CentOS
$ virsh undefine Ubuntu --remove-all-storage
error: Refusing to undefine while domain managed save image exists
$ virsh managedsave-remove Ubuntu
Removed managedsave image for domain 'Ubuntu'
$ virsh undefine Ubuntu
Domain 'Ubuntu' has been undefined
stderr=failed to get mtu of bridge `virbr0': No such device
# systemctl restart libvirtd
$ brctl show
bridge name bridge id STP enabled interfaces
virbr0 8000.525400a87247 yes
Booting from Hard disk....
Probing EDD (edd=off to disable)... ok
$ virt-install \
...
--extra-args='... edd=off'
- Ensure that specified storage pool has correct permissions and path.
$ virsh pool-list --all
$ virsh pool-info default
$ virsh pool-dumpxml default
$ virsh pool-dumpxml default \
| xmlstarlet sel --template --copy-of "/pool/target"
$ virsh pool-dumpxml default \
| xmlstarlet sel --template --value-of "/pool/target/path"
# cp /etc/libvirt/qemu.conf /etc/libvirt/qemu.conf.original
# sed --in-place \
"s,\#user = \"root\",\#user = \"${USER}\",g" \
/etc/libvirt/qemu.conf
# sed --in-place \
"s,\#group = \"root\",\#group = \"libvirt\",g" \
/etc/libvirt/qemu.conf
# systemctl restart libvirtd
$ virsh net-list --all
Name State Autostart Persistent
----------------------------------------
$ sudo virsh net-list --all
Name State Autostart Persistent
--------------------------------------------
default active yes yes
Read this post if default network is still missing.
Use virt-manager
to create, manage, & delete KVMs.
- Preseeding (Debian-based Linux Distributions) or Kickstart (Red Hat-based Linux Distributions) provides a way to set answers to questions asked during the installation process, without having to manually enter the answers while the installation is running.
https://www.linux-kvm.org/page/USB_Host_Device_Assigned_to_Guest