Tuesday, November 5, 2024

How to Install and Setting KVM on Rocky Linux 9


Rocky Linux 9, a robust and reliable Linux distribution, empowers users to embrace the world of virtualization. At its core lies Kernel Virtual Machine (KVM), a powerful, open-source hypervisor that unlocks the ability to run multiple operating systems concurrently on a single physical machine.

This article will guide you through the process of installing and configuring KVM on Rocky Linux 9, empowering you to leverage its capabilities for enhanced resource utilization, cost savings, and flexibility in your system administration endeavors.

Preparing for Virtualization: Essential Prerequisites and Hardware Considerations

Before embarking on the KVM journey, ensure your system meets these prerequisites:

  • A running Rocky Linux 9 system: This guide assumes a properly installed and functioning Rocky Linux 9 distribution.

  • Sudo or root privileges: Administrative privileges are required to perform system configurations and installations.

  • Internet connection: An active internet connection is essential for downloading software packages and resolving dependencies during the setup process.

  • Basic Linux networking knowledge: Familiarity with Linux networking concepts, including network interfaces, bridges, and IP addressing, will facilitate configuring network connectivity for your virtual machines.

  • Hardware virtualization support: KVM relies on hardware virtualization extensions provided by your CPU. To confirm their availability:

    • Intel-based systems: Execute sudo grep -e 'vmx' /proc/cpuinfo.

    • AMD-based systems: Execute sudo grep -e 'svm' /proc/cpuinfo.

If virtualization extensions are not enabled, you can usually enable them from your system's BIOS settings.

Step 1: Installing KVM and Essential Tools

The core KVM components, along with a set of valuable management tools, are readily available in the default repositories of Rocky Linux 9. To install them, use the following command:

      sudo dnf install qemu-kvm libvirt virt-manager virt-install
    
Understanding the Installed Packages:
  • qemu-kvm: This package provides the KVM hypervisor itself, responsible for creating and managing the virtual environments.

  • libvirt: A powerful library and daemon that facilitates interaction between the host system and the hypervisor. It provides a consistent interface for managing virtual machines across different hypervisors.

  • virt-manager: A user-friendly graphical interface for managing virtual machines, offering a visual way to create, start, stop, and configure VMs.

  • virt-install: A command-line tool for creating new virtual machines. This tool provides flexibility in defining VM configurations and is ideal for automated deployments.

Step 2: Enabling the EPEL Repository

To access a wider selection of KVM-related tools, you need to enable the Extra Packages for Enterprise Linux (EPEL) repository. Execute the following commands:

      sudo dnf install epel-release -y
    
Step 3: Installing Additional KVM Management Tools

Once the EPEL repository is enabled, install a collection of utilities that enhance your KVM management capabilities:

      sudo dnf -y install bridge-utils virt-top libguestfs-tools bridge-utils virt-viewer
    
Understanding the Additional Packages:
  • bridge-utils: Provides tools for managing network bridges, which are essential for creating isolated networks for your virtual machines.

  • virt-top: A real-time monitoring tool that displays system resource usage within your virtual machines, offering insights into performance and resource allocation.

  • libguestfs-tools: Enables access to guest operating systems within a virtual machine, allowing you to manage files, manipulate disk partitions, and perform other tasks from the host system.

  • virt-viewer: A graphical tool for connecting to and viewing virtual machines, particularly useful for accessing VMs running graphical environments.

Step 4: Verifying KVM Module Loading

After installing the KVM packages, verify that the KVM module is loaded into your Linux kernel:

      sudo lsmod | grep kvm
    
This command will output information about the loaded KVM module if it's active.

Step 5: Starting and Enabling the libvirtd Daemon

The libvirtd daemon is responsible for handling requests from clients, such as virt-manager and virt-install, to manage the virtual environment. To ensure libvirtd is running, execute the following commands:

sudo systemctl start libvirtd
sudo systemctl enable libvirtd
    
To check the daemon's status:
      sudo systemctl status libvirtd
    
Step 6: Adding Your User to the libvirt Group

To avoid using sudo when managing virtual machines with tools like virt-install, add your user to the libvirt group:

      sudo usermod -aG libvirt $USER
newgrp libvirt
    
Step 7: Creating a Network Bridge for External Connectivity

By default, KVM creates a bridge named virbr0 for network address translation (NAT), which limits external connectivity for virtual machines. For seamless communication with the outside world, we'll create a dedicated network bridge using the Network Manager command-line interface (nmcli).

Creating the Network Bridge:

  1. Identify Network Interfaces: List the available network interfaces on your system:

          sudo nmcli connection show
        

  2. Delete Existing Connections: If there are any existing connections for the interface you want to use for the bridge, delete them. For example, to delete a connection with ID b0912n-5130-309a-b84d-a3c56dec9ed8:

          sudo nmcli connection delete b0912n-5130-309a-b84d-a3c56dec9ed8
        

  3. Create the Bridge: Create a new bridge with the name br1:

          sudo nmcli connection add type bridge autoconnect yes con-name br1 ifname br1
        

  4. Assign IP Address, Gateway, and DNS: Configure the bridge's IP address, gateway, and DNS servers. Replace the placeholders with your actual network information:

    sudo nmcli connection modify br1 ipv4.addresses 192.168.16.122.1/24 ipv4.method manual
    sudo nmcli connection modify br1 ipv4.gateway 192.168.16.2
    sudo nmcli connection modify br1 ipv4.dns 8.8.8.8 +ipv4.dns 8.8.4.4
        

  5. Add the Interface as Bridge Slave: Add the network interface you want to use as a slave to the bridge (e.g., ens160):

          sudo nmcli connection add type bridge-slave autoconnect yes con-name ens160 ifname ens160 master br1
        

  6. Verify Bridge Creation: List all network connections to confirm the bridge is created successfully:

          sudo nmcli connection show
        

  7. Start the Bridge: Activate the newly created bridge:

          sudo nmcli connection up br1
        

  8. Verify Bridge Status: Check the status of the bridge:

          sudo nmcli connection show br1
        

Enabling KVM to Use the Bridge:

  1. Edit the Configuration File: Open the KVM bridge configuration file:

          sudo vim /etc/qemu-kvm/bridge.conf
        

  2. Add Bridge Information: Add a line to specify the bridge name:

          bridge_name = br1
        

  3. Restart libvirtd: Restart the libvirtd daemon to apply the changes:

          sudo systemctl restart libvirtd
        

Step 8: Creating Your First Virtual Machine

With KVM and the network bridge set up, you're ready to create a virtual machine. You'll need an ISO image file of the operating system you want to install. For this example, we'll use a Rocky Linux 9 ISO image.

Creating a Virtual Machine via the Command Line:

  1. Set Ownership of the libvirt Directory: Ensure appropriate permissions for the libvirt directory:

          sudo chown -R $USER:libvirt /var/lib/libvirt/
        

  2. Execute virt-install: Use the virt-install command to create a virtual machine named "Rocky9" with the specified settings:

    virt-install \
    --name Rocky9 \
    --ram 2048 \
    --vcpus 1 \
    --disk path=/var/lib/libvirt/images/rocky-9.img,size=20 \
    --os-variant centos-stream9 \
    --network bridge=br1,model=virtio \
    --graphics vnc,listen=0.0.0.0 \
    --console pty,target_type=serial \
    --location /home/rocky9/Downloads/Rocky-9.0-x86_64-minimal.iso
        

Understanding the Command Options:

  • --name Rocky9: Assigns the name "Rocky9" to the virtual machine.

  • --ram 2048: Allocates 2048 MB of RAM to the virtual machine.

  • --vcpus 1: Specifies that the virtual machine should have one virtual CPU.

  • --disk path=/var/lib/libvirt/images/rocky-9.img,size=20: Creates a virtual hard disk named "rocky-9.img" with a size of 20 GB in the specified directory.

  • --os-variant centos-stream9: Informs virt-install about the operating system variant, helping to optimize the installation process.

  • --network bridge=br1,model=virtio: Connects the virtual machine to the network bridge "br1" using the virtio network driver.

  • --graphics vnc,listen=0.0.0.0: Enables VNC access to the virtual machine, allowing you to connect remotely using a VNC client. The listen=0.0.0.0 parameter allows connections from any network interface.

  • --console pty,target_type=serial: Provides a serial console for accessing the virtual machine.

  • --location /home/rocky9/Downloads/Rocky-9.0-x86_64-minimal.iso: Specifies the location of your Rocky Linux 9 ISO image file.

Creating a Virtual Machine using virt-manager:

  1. Launch virt-manager: Open the virt-manager application from your system's application menu.

  2. Create a New Virtual Machine: Click the "Create" icon in the virt-manager window to begin the virtual machine creation process.

  3. Select Installation Media: Choose "Local Install Media" and provide the path to your ISO image file.

  4. Configure Memory and CPU: Specify the amount of RAM and the number of virtual CPUs for the virtual machine.

  5. Set Disk Size: Define the size of the virtual hard disk.

  6. Finish Installation: Click the "Finish" button to start the virtual machine installation.

Conclusion

By following this comprehensive guide, you have successfully installed KVM on Rocky Linux 9, setting the stage for powerful virtualization capabilities. You have learned how to create network bridges for external connectivity and how to easily create virtual machines using both the command line and the virt-manager graphical interface.

KVM offers an exceptional platform for running multiple operating systems and applications, enabling greater efficiency, flexibility, and resource optimization. Experiment with different operating systems and configurations to explore the full potential of KVM in your system administration endeavors.

0 comments:

Post a Comment