KVM (Kernel-based Virtual Machine) is a Linux built-in virtualization technology provided as a kernel module, which offers a nearly native performance VM. QEMU is a full-system emulator. Both of them are open source, which is ideal for system research. KVM/QEMU are used in pair. Let's first have a glance at the packages we are going to install
$ dnf group info virtualization
Repositories loaded.
Id : virtualization
Name : Virtualization
Description : These packages provide a graphical virtualization environment.
Installed : no
Order :
Langonly :
Uservisible : no
Repositories : @System
Mandatory packages : virt-install
Default packages : libvirt-daemon-config-network
: libvirt-daemon-kvm
: qemu-kvm
: virt-manager
: virt-viewer
Optional packages : guestfs-tools
: python3-libguestfs
: virt-top
Install the packages.
$ sudo dnf install @virtualization
After installing packages, start libvirtd
service
$ sudo systemctl start libvirtd
$ sudo systemctl status libvirtd # do this to verify the service is running
Confirm the kernel module is loaded successfully.
$ lsmod | grep kvm
kvm_intel 446464 0
kvm 1449984 1 kvm_intel
Make sure there is "kvm_intel" or "kvm_amd" in the output. At this point, everything is inspired by Fedora's amazing document. Then, we need to install a Linux distro into the VM through virt-install
(thanks to Red Hat's amazing document)
$ virt-install \
> --name ubuntu-20.04.3-lts-scalecache \
> --memory 8192 \
> --vcpus 8 \
> --disk size=60 \
> --cdrom ~/Downloads/ubuntu-20.04.3-desktop-amd64.iso \
> --os-variant ubuntufocal
Starting install...
Allocating 'ubuntu-20.04.3-lts-scalecache.qcow2' | 60 GB 00:00:00
Creating domain... | 00:00:00
Running graphical console command: virt-viewer --connect qemu:///session --wait ubuntu-20.04.3-lts-scalecache
Domain is still running. Installation may be in progress.
You can reconnect to the console to complete the installation process.
Note: that --os-variant/--osinfo OS name is required, we must figure out what parameter fits to our distro best.
$ virt-install --os-variant list
almalinux9
almalinux8
alpinelinux3.21
alpinelinux3.20
alpinelinux3.19
alpinelinux3.18
alpinelinux3.17
alpinelinux3.16
...
ubuntu20.04, ubuntufocal
...
Now, we need to connect to the VM to continue the installation process manually.
$ virt-viewer --connect qemu:///session --wait ubuntu-20.04.3-lts-scalecache
After completing the installation process in VM, before hitting Enter to reboot the VM, detach the mounted iso image from the VM to prevent from booting into the installation process again.
$ virsh domblklist ubuntu-20.04.3-lts-scalecache
Target Source
-----------------------------------------------------------------------------
vda /home/melody/.local/share/libvirt/images/ubuntu-20.04.3-lts-scalecache.qcow2
sda /home/melody/Downloads/ubuntu-20.04.3-desktop-amd64.iso
$ virsh detach-disk ubuntu-20.04.3-lts-scalecache sda --config
Disk detached successfully
Use virsh
to check the status of VM
$ virsh list --all
Id Name State
------------------------------------------------
- ubuntu-20.04.3-lts-scalecache shut off
Launch and connect to the VM again.
$ virsh start ubuntu-20.04.3-lts-scalecache
Domain 'ubuntu-20.04.3-lts-scalecache' started
$ virt-viewer --connect qemu:///session --wait ubuntu-20.04.3-lts-scalecache
To shutdown the VM gracefully, use virsh shutdown
command.
$ virsh shutdown ubuntu-20.04.3-lts-scalecache
Domain 'ubuntu-20.04.3-lts-scalecache' is being shutdown
Comments NOTHING