Create VMs with KVM on Oracle Linux

6
0
Send lab feedback

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

Validate Environment Supports Virtualization

Note: When using the free lab environment, see Oracle Linux Lab Basics for connection and other usage instructions.

  1. If not already connected, open a terminal and connect via ssh to your instance.

    ssh oracle@<ip_address_of_instance>
  2. 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.

  3. 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.

  4. 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

  1. Install the software packages associated with the Oracle Linux virtualization module.

    sudo dnf module install -y virt

    This command installs the collection of virtualization packages, which include libvirt, qemu-kvm, and other dependencies.

  2. Install virt-install and virt-viewer.

    sudo dnf install -y virt-install virt-viewer

    This completes the install of the required set of packages.

  3. 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.

  4. 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

  1. Install the cockpit-machines extension for managing VMs based on Libvirt.

    sudo dnf install -y cockpit cockpit-machines
  2. 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.

  3. 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.

  4. 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>
  5. Open a web browser on your desktop and browse to https://localhost:9090 .

    Cockpit web console uses a self-signed certificate to enable HTTPS.

    accept_cert

    Click Advanced and accept the connection when you get a warning from the browser.

  6. Login as oracle user with password of oracle.

    login

    The Cockpit Dashboard is displayed.

  7. Click the "Limited Access" button to gain Administrative privileges.

    laccess

  8. Close the Administrative access window.

    claccess

Create Virtual Machine from an ISO

  1. Click the "Virtual Machines" option in the navigation panel on the left.

  2. Click the "Create VM" button.

    vm

  3. 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/u9/x86_64/OracleLinux-R8-U9-x86_64-dvd.iso
    Operating system: Oracle Linux 8.9
    Storage: Create new qcow2 volume
    Size: 20 GiB
    Memory: 16 GiB

    vm

  4. Click "Create and run" to begin.

  5. From the list of Virtual Machines, click the OL-demo link.

    vmlist

  6. 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.

    bootm

  7. 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.

    lang

    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

  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.

  1. Change to the KVM image storage location.

    cd /var/lib/libvirt/images
  2. Download the Oracle Linux VM template.

    sudo curl -O https://yum.oracle.com/templates/OracleLinux/OL8/u9/x86_64/OL8U9_x86_64-kvm-b210.qcow
  3. Create meta-data file.

    cat << 'EOF' | sudo tee ~/meta-data > /dev/null
    instance-id: iid-local01
    local-hostname: vm-01
    EOF
  4. 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
  5. 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.

  6. 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
  7. Generate the ISO image.

    sudo genisoimage -output /var/lib/libvirt/images/vm-01.iso -volid cidata -joliet -rock ~/user-data ~/meta-data
  8. Create the instance.

    sudo cp /var/lib/libvirt/images/OL8U9_x86_64-kvm-b210.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
  9. List the VMs.

    sudo virsh list
  10. 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.

  11. Alternatively you can filter based on the MAC address.

    sudo virsh domiflist vm-01
  12. Copy the MAC address from the previous output and provide in the following command.

    sudo virsh net-dhcp-leases --network default --mac <MAC_address>
  13. 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 ~]$ hostnamectl
       Static hostname: vm-01
             Icon name: computer-vm
               Chassis: vm
            Machine ID: c4889057609a4ad0a20047251cf94211
               Boot ID: e6d758624c6044199b6f9d334aa405f1
        Virtualization: kvm
      Operating System: Oracle Linux Server 8.9
           CPE OS Name: cpe:/o:oracle:linux:8:9:server
                Kernel: Linux 5.15.0-200.131.27.el8uek.x86_64
          Architecture: x86-64

[Optional] View VM from Cockpit Dashboard

Note: Run this next command from a terminal on the lunabox Desktop.

  1. 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>
  2. Open a web browser on your desktop and browse to https://localhost:9090 .

  3. Login as oracle user with password of oracle.

    login

    The Cockpit Dashboard is displayed.

  4. If necessary, click the "Limited Access" button to gain Administrative privileges.

  5. Click the "Virtual Machines" option in the navigation panel on the left.

  6. From the list of Virtual Machines, click the vm-01 link.

  7. 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 with virt-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:

SSR