Note:
- This tutorial is available in an Oracle-provided free lab environment.
- It uses example values for Oracle Cloud Infrastructure credentials, tenancy, and compartments. When completing your lab, substitute these values with ones specific to your cloud environment.
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, 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 system with the following configurations:
- a non-root user with sudo permissions
Deploy Oracle Linux
Note: If running in your own tenancy, read the linux-virt-labs
GitHub project README.md and complete the prerequisites before deploying the lab environment.
-
Open a terminal on the Luna Desktop.
-
Clone the
linux-virt-labs
GitHub project.git clone https://github.com/oracle-devrel/linux-virt-labs.git
-
Change into the working directory.
cd linux-virt-labs/ol
-
Install the required collections.
ansible-galaxy collection install -r requirements.yml
-
Deploy the lab environment.
ansible-playbook create_instance.yml -e localhost_python_interpreter="/usr/bin/python3.6" -e instance_ocpus="4" -e instance_memory="64"
The free lab environment requires the extra variable
local_python_interpreter
, which setsansible_python_interpreter
for plays running on localhost. This variable is needed because the environment installs the RPM package for the Oracle Cloud Infrastructure SDK for Python, located under the python3.6 modules.The default deployment shape uses the AMD CPU and Oracle Linux 8. To use an Intel CPU or Oracle Linux 9, add
-e instance_shape="VM.Standard3.Flex"
or-e os_version="9"
to the deployment command.Important: Wait for the playbook to run successfully and reach the pause task. At this stage of the playbook, the installation of Oracle Cloud Native Environment is complete, and the instances are ready. Take note of the previous play, which prints the public and private IP addresses of the nodes it deploys and any other deployment information needed while running the lab.
Validate Environment Supports Virtualization
-
Open a terminal and connect via SSH to the ol-node-01 instance.
ssh oracle@<ip_address_of_instance>
-
Run the following command to determine your CPU type.
grep -e 'vendor_id' /proc/cpuinfo | uniq
The
vendor_id
reports eitherAuthenticAMD
for an AMD CPU orGenuinIntel
for an Intel CPU. -
Check if the hardware supports virtualization.
Run the command that matches your CPU type.
- Verify the AMD V CPU extensions exist.
grep -w -o 'svm' /proc/cpuinfo | uniq
- Verify the Intel VT CPU extensions exist.
grep -w -o 'vmx' /proc/cpuinfo | uniq
The existence of one of these flags in the command output indicates this system supports virtualization. You can also use the
lscpu
command and look for theVirtualization
entry in the output. -
Check for the loaded KVM modules.
lsmod | grep kvm
The output displays the KVM kernel module and, depending on the system’s hardware, either the
kvm_amd
orkvm_intel
processor module. On modern Linux systems, these modules should load by default.
Install and Start KVM
-
Check the running version of Oracle Linux.
hostnamectl | grep 'Operating System'
-
Install the associated software packages for Oracle Linux virtualization.
Run the command based on your installed operating system.
Oracle Linux 8:
sudo dnf module install -y virt
Oracle Linux 9:
sudo dnf install -y qemu-kvm libvirt
These commands install the virtualization packages, which include libvirt, qemu-kvm, and other dependencies on Oracle Linux.
-
Install
virt-install
andvirt-viewer
.sudo dnf install -y virt-install virt-viewer
-
Install these packages to enable https protocol support for an image installation souce.
Oracle Linux 8:
This functionality is available by default in this version of Oracle Linux.
Oracle Linux 9:
sudo dnf install -y qemu-kvm-block-curl
-
Validate the host machine is ready and set up to run libvirt VMs.
virt-host-validate
If all checks pass, the system is prepared for creating VMs. If any checks fail, follow the instructions to correct the problem. If any check returns the value of
WARN
, consider following the instructions to improve the virtualization capabilities.In Oracle Linux 9, there is a
WARN
message regarding the cgroup devices controller support. This is expected based on an upstream issue that states,With cgroups v2 there is no devices controller; eBPF should be used instead
. -
Start the Systemd services and sockets, enabling them to start automatically on each boot. Then, check their status to confirm they are up and running.
Oracle Linux 8:
sudo systemctl enable --now libvirtd.service sudo systemctl status libvirtd.service
Oracle Linux 9:
for drv in qemu network nodedev nwfilter secret storage interface do sudo systemctl start virt${drv}d{,-ro,-admin}.socket done
for drv in qemu network nodedev nwfilter secret storage interface do sudo systemctl status virt${drv}d{,-ro,-admin}.socket done
Output shows the service or sockets as 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 -y cockpit cockpit-machines
-
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.
-
Open another terminal on the Luna Desktop and enter the following command to create a local port forward for the Cockpit web console using SSH.
ssh -L 9090:localhost:9090 oracle@<ip_address_of_instance>
-
Open a web browser and enter the URL.
https://localhost:9090
The Cockpit web console uses a self-signed certificate to enable HTTPS. Approve the security warning based on the browser used. For Chrome, click the
Advanced
button and then theProceed to localhost (unsafe)
link. -
Log in as the
oracle
user with the 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 in the Create new virtual machine dialog.
- Installation source:
https://yum.oracle.com/ISOS/OracleLinux/OL8/u10/x86_64/OracleLinux-R8-U10-x86_64-dvd.iso
Oracle provides access to the Oracle Linux Installation Media from https://yum.oracle.com/oracle-linux-isos.html. The disk size of
10 GiB
and memory of1 GiB
are just for this tutorial and are not recommended for real-world use. - Installation source:
-
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 that 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: Check out these free videos demonstrating the Oracle Linux 8 and Oracle Linux 9 installation process.
-
Close the web browser by clicking the ‘X’ in the window’s top-right corner.
Create a Virtual Machine using Oracle Cloud Images
-
Switch to the terminal running your KVM server.
-
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/u10/x86_64/OL8U10_x86_64-kvm-b237.qcow2
-
Create a meta-data file.
cat << 'EOF' | sudo tee ~/meta-data > /dev/null instance-id: iid-local01 local-hostname: vm-01 EOF
-
Create a 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 command writes the key pair to the.ssh
directory in the user’s 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
-
Find the OS variant that matches your image best.
Oracle Linux 8:
osinfo-query os | grep ol8
Oracle Linux 9:
virt-install --os-variant list | grep ol8
-
Create the instance.
Oracle Linux 8:
sudo cp /var/lib/libvirt/images/OL8U10_x86_64-kvm-b237.qcow2 /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.10 \ --virt-type kvm --graphics none \ --network network=default,model=virtio \ --noautoconsole \ --import
Oracle Linux 9:
The
--os-type
option is deprecated and no longer required to create a virtual machine when runningvirt-install
.sudo cp /var/lib/libvirt/images/OL8U10_x86_64-kvm-b237.qcow2 /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-variant ol8.10 \ --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 rerun the command 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 it 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@<ip_address_of_vm-01>
You can verify the version and get additional details about the OS within the virtual machine by running
hostnamectl
.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 ~]$ hostnamectl Static hostname: vm-01 Icon name: computer-vm Chassis: vm Machine ID: 30c9345b511448b681aafb3371de9792 Boot ID: c2d5586b094f4d299a4ba6c05086d004 Virtualization: kvm Operating System: Oracle Linux Server 8.10 CPE OS Name: cpe:/o:oracle:linux:8:10:server Kernel: Linux 5.15.0-206.153.7.1.el8uek.x86_64 Architecture: x86-64
(Optional) View VM from Cockpit Dashboard
The previously opened SSH tunnel should still be available if you did not exit and close that terminal session. If it is not open, create a new tunnel using ssh -L 9090:localhost:9090 oracle@<instance_ip_address>
.
-
Switch to a web browser and enter the URL.
https://localhost:9090
-
Log in as the
oracle
user with the 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 the Virtual Machine’s Overview, Disks, and Network interfaces sections.
Note: The console is inactive within Cockpit as we create the
vm-01
virtual machine using the--graphical none
option withvirt-install
.
Summary
The virtual shell and Cockpit provide two different ways to create and interact with KVM virtual machines. With these basics, you can explore and further enhance your virtualization skillset.
For More Information:
More Learning Resources
Explore other labs on docs.oracle.com/learn or access more free learning content on the Oracle Learning YouTube channel. Additionally, visit education.oracle.com/learning-explorer to become an Oracle Learning Explorer.
For product documentation, visit Oracle Help Center.
Create VMs with KVM on Oracle Linux
F58820-06
July 2024