Create VMs with KVM on Oracle Linux
Introduction
Kernel-based Virtual Machine (KVM) is an open-source type-1 (bare-metal) hypervisor. This functionality permits a host system, such as Oracle Linux 8, to host multiple virtual machines (VMs) or guests when running on supported hardware.
This tutorial will deploy Oracle Linux Kernel Virtualization Manager (KVM) to create a virtual machine.
Objectives
- Deploy KVM
- Deploy Cockpit for Virtual Machines
- Create Virtual Machine from an ISO
- Create Virtual Machine using Oracle Cloud Images
Prerequisites
Any Oracle Linux 8 or later system with the following configurations:
- a non-root user with sudo permissions
Validate Environment Supports Virtualization
Note: When using the free lab environment, see Oracle Linux Lab Basics for connection and other usage instructions.
If not already connected, open a terminal and connect via ssh to your instance.
ssh oracle@<ip_address_of_instance>
Run the following command to determine your CPU type.
grep -e 'vendor_id' /proc/cpuinfo
Example Output:
[oracle@ol-node01 ~]$ grep -e 'vendor_id' /proc/cpuinfo vendor_id : GenuineIntel vendor_id : GenuineIntel vendor_id : GenuineIntel vendor_id : GenuineIntel vendor_id : GenuineIntel vendor_id : GenuineIntel
This system is Intel-based, shown by the output of GenuineIntel.
For Intel systems, type the following command.
grep -e 'vmx' /proc/cpuinfo
The existence of the vmx flag in the command output indicates this system supports virtualization.
Check for the loaded KVM modules.
lsmod | grep kvm
Example Output:
[oracle@ol-node01 ~]$ lsmod |grep kvm kvm_intel 262144 0 kvm 696320 1 kvm_intel irqbypass 16384 1 kvm
The output displays the KVM kernel module and the kvm_intel processor module based on this system's hardware. These modules should load by default on modern Linux systems.
Install and Start KVM
Install the software packages associated with the Oracle Linux virtualization module.
sudo dnf module install virt -y
This command installs the collection of virtualization packages, which include libvirt, qemu-kvm, and other dependencies.
Install
virt-install
andvirt-viewer
.sudo dnf install virt-install virt-viewer -y
This completes the install of the required set of packages.
Validate the host machine is ready and set up to run libvirt VMs.
virt-host-validate
Example Output:
[oracle@ol-node01 ~]$ virt-host-validate QEMU: Checking for hardware virtualization : PASS QEMU: Checking if device /dev/kvm exists : PASS QEMU: Checking if device /dev/kvm is accessible : PASS QEMU: Checking if device /dev/vhost-net exists : PASS QEMU: Checking if device /dev/net/tun exists : PASS QEMU: Checking for cgroup 'cpu' controller support : PASS QEMU: Checking for cgroup 'cpuacct' controller support : PASS QEMU: Checking for cgroup 'cpuset' controller support : PASS QEMU: Checking for cgroup 'memory' controller support : PASS QEMU: Checking for cgroup 'devices' controller support : PASS QEMU: Checking for cgroup 'blkio' controller support : PASS QEMU: Checking for device assignment IOMMU support : WARN (No ACPI DMAR table found, IOMMU either disabled in BIOS or not supported by this hardware platform) QEMU: Checking for secure guest support : WARN (Unknown if this platform has Secure Guest support)
If all checks PASS, then the system is prepared for created VMs. If any checks FAIL, follow the instruction to correct the problem. If any check returns the value of WARN, then consider following the instructions to improve the virtualization capabilities.
Start the
libvirtd
daemon and enable it to start automatically on each boot. Then check its status to confirm that it is up and running.sudo systemctl enable --now libvirtd.service sudo systemctl status libvirtd.service
Example Output:
[oracle@ol-node01 ~]$ sudo systemctl enable --now libvirtd.service [oracle@ol-node01 ~]$ sudo systemctl status libvirtd.service * libvirtd.service - Virtualization daemon Loaded: loaded (/usr/lib/systemd/system/libvirtd.service; enabled; vendor pr> Active: active (running) since Mon 2022-06-13 21:28:19 GMT; 8s ago Docs: man:libvirtd(8) https://libvirt.org ...
Output indicates the service is enabled and running.
Setup Cockpit Web Console to manage KVM on Oracle Linux
Install the cockpit-machines extension for managing VMs based on Libvirt.
sudo dnf install cockpit cockpit-machines –y
Start the cockpit socket and enable it to auto-start on system boot.
sudo systemctl enable --now cockpit.socket sudo systemctl status cockpit.socket
Example Output:
[oracle@ol-node01 ~]$ sudo systemctl enable --now cockpit.socket Created symlink /etc/systemd/system/sockets.target.wants/cockpit.socket -> /usr/lib/systemd/system/cockpit.socket. [oracle@ol-node01 ~]$ sudo systemctl status cockpit.socket * cockpit.socket - Cockpit Web Service Socket Loaded: loaded (/usr/lib/systemd/system/cockpit.socket; enabled; vendor pres> Active: active (listening) since Mon 2022-06-13 21:39:24 GMT; 10s ago Docs: man:cockpit-ws(8) Listen: [::]:9090 (Stream) ...
Output indicates the service is enabled and listening.
To access this site remotely, enable the firewall port for the cockpit service and reload the firewall configuration to apply the new changes.
sudo firewall-cmd --add-service=cockpit --permanent sudo firewall-cmd --reload
Note: Run this next command from a 2nd terminal on the lunabox Desktop.
Open a 2nd terminal on the lunabox Desktop and enter the following command to create a local port forward for Cockpit using ssh.
ssh -L 9090:localhost:9090 oracle@<instance_ip_address>
Open a web browser on your desktop and browse to https://localhost:9090 .
Cockpit web console uses a self-signed certificate to enable HTTPS.
Click Advanced and accept the connection when you get a warning from the browser.
Login as
oracle
user with password oforacle
.The Cockpit Dashboard is displayed.
Click the "Limited Access" button to gain Administrative privileges.
Close the Administrative access window.
Create Virtual Machine from an ISO
Click the "Virtual Machines" option in the navigation panel on the left.
Click the "Create VM" button.
Provide the following information:
Name: OL-demo Connection: System Installation type: URL (ISO image or distro install tree) Installation source: https://yum.oracle.com/ISOS/OracleLinux/OL8/u8/x86_64/OracleLinux-R8-U8-x86_64-dvd.iso Operating system: Oracle Linux 8.8 Storage: Create new volume Size: 20 GiB Memory: 16 GiB
Click "Create and run" to begin.
From the list of Virtual Machines, click the OL-demo link.
Scroll down to view the VNC Console.
If you're quick enough, you will see a menu with the options to Install Oracle Linux or Test this media and Install. The Test option will be selected by default after a few seconds. The installer will validate the content on the media is good and then start the installer.
Once the installer boots, you are presented with the Welcome to Oracle Linux screen. You begin the installation by selecting your language for the installation process.
This lab does not continue with the installation process.
Note: You can watch a free video demonstrating the Oracle Linux installation process at this video link: Installing Oracle Linux 8
Close the web browser by clicking the 'X' in the top-right corner of the window.
Create Virtual Machine using Oracle Cloud Images
Note: Ensure you enter the following commands from your cloud instance (ol-node01) terminal window.
Change to the KVM image storage location.
cd /var/lib/libvirt/images
Download the Oracle Linux VM template.
sudo curl -O https://yum.oracle.com/templates/OracleLinux/OL8/u8/x86_64/OL8U8_x86_64-kvm-b198.qcow
Create meta-data file.
cat << 'EOF' | sudo tee ~/meta-data > /dev/null instance-id: iid-local01 local-hostname: vm-01 EOF
Create user-data file.
cat << 'EOF' | sudo tee ~/user-data > /dev/null #cloud-config system_info: default_user: name: opc ssh_authorized_keys: - <paste_public_key_here> EOF
Generate an SSH Key Pair
ssh-keygen -t rsa -b 4096
Hit
Enter
to accept each of the defaults. The key pair is written to the.ssh
directory in the users home.Copy the public key into the user-data file.
SSHKEY=$(cat ~/.ssh/id_rsa.pub) sed -i "s|<paste_public_key_here>|${SSHKEY}|g" ~/user-data
Generate the ISO image.
sudo genisoimage -output /var/lib/libvirt/images/vm-01.iso -volid cidata -joliet -rock ~/user-data ~/meta-data
Create the instance.
sudo cp /var/lib/libvirt/images/OL8U8_x86_64-kvm-b198.qcow /var/lib/libvirt/images/vm-01.qcow sudo virt-install --name vm-01 \ --memory 2048 \ --vcpus 2 \ --disk /var/lib/libvirt/images/vm-01.qcow,device=disk,bus=virtio \ --disk /var/lib/libvirt/images/vm-01.iso,device=cdrom \ --os-type linux --os-variant ol8.6 \ --virt-type kvm --graphics none \ --network network=default,model=virtio \ --noautoconsole \ --import
List the VMs.
sudo virsh list
Retrieve the IP address of the
vm-01
virtual machine.sudo virsh net-dhcp-leases --network default
Note: Depending on how quickly the virtual machine starts, you may need to run the command again to display the IP address.
Alternatively you can filter based on the MAC address.
sudo virsh domiflist vm-01
Copy the MAC address from the previous output and provide in the following command.
sudo virsh net-dhcp-leases --network default --mac <MAC_address>
Verify the virtual machine works by connecting with
ssh
.ssh opc@<vm-01-ip_address>
Example Output:
[oracle@ol-node01 images]$ ssh opc@192.168.122.46 The authenticity of host '192.168.122.46 (192.168.122.46)' can't be established. ECDSA key fingerprint is SHA256:xcuVfQdoFDCC72i7plD0OfqDTSBG6QWhOm5ti4HIKEs. Are you sure you want to continue connecting (yes/no/[fingerprint])? yes Warning: Permanently added '192.168.122.46' (ECDSA) to the list of known hosts. [opc@vm-01 ~]$ uname -a Linux ol-node01 5.15.0-101.103.2.1.el8uek.x86_64 #2 SMP Mon May 1 20:11:30 PDT 2023 x86_64 x86_64 x86_64 GNU/Linux
[Optional] View VM from Cockpit Dashboard
Note: Run this next command from a terminal on the lunabox Desktop.
Enter the following command from your lunabox Desktop terminal window to create a local port forward for Cockpit using ssh.
ssh -L 9090:localhost:9090 oracle@<instance_ip_address>
Open a web browser on your desktop and browse to https://localhost:9090 .
Login as
oracle
user with password oforacle
.The Cockpit Dashboard is displayed.
If necessary, click the "Limited Access" button to gain Administrative privileges.
Click the "Virtual Machines" option in the navigation panel on the left.
From the list of Virtual Machines, click the vm-01 link.
Explore as desired the Overview section, Disks section, Network interfaces, etc.
Note: The console is not active within Cockpit as the
vm-01
virtual machine was created using the--graphical none
option withvirt-install
.
This concludes this lab.