Table of Contents
Introduction
VMs are virtualized computing environments that mimic physical computers. They allow users to run multiple operating systems and applications on a single physical machine.
Prerequisites for Creating VMs on Ubuntu 22.04
- Before creating VMs on Ubuntu Server, ensure that you have a compatible hardware setup, sufficient disk space, and adequate system resources. Install Ubuntu Server on your physical machine if you haven’t already done so.
- An account with sudo privileges
- Access to the command line/terminal
- Check Virtualization compatibility on ubuntu 22.04
- Hardware virtualization: To check the hardware virtualization use the following comand
egrep -c '(vmx|svm)' /proc/cpuinfo
In our setup above command yields the following result
ubuntu@ubuntu-test:~$ sudo egrep -c '(vmx|svm)' /proc/cpuinfo 32 ubuntu@ubuntu-test:~$
Also, kcheck if your system’s CPU supports hardware virtualization extensions, which are required for running virtual machines with good performance using KVM (Kernel-based Virtual Machine) on Linux systems:
foofunc@ubuntu-test:~$ sudo kvm-ok sudo: kvm-ok: command not found
If the kvm-ok command yields error install cpu checker package to remove the issue and rerun the kvm-ok command. See the below code section
foofunc@ubuntu-test:~$ sudo apt install cpu-checker ... foofunc@ubuntu-test:~$ sudo kvm-ok INFO: /dev/kvm exists KVM acceleration can be used foofunc@ubuntu-test:~$
Important packages and configurations
Install the prerequisite packages
bridge-utils: Bridge-utils is a collection of utilities used for configuring, managing, and monitoring Ethernet bridges on Linux-based systems. The bridge-utils package includes command-line tools such as brctl, which allows users to create, modify, and delete bridge devices, as well as to manage the forwarding database, which stores MAC (Media Access Control) addresses and associated port information. Network bridging is necessary, such as in virtualization environments, network bonding configurations, or when creating complex network topologies.
libvirt-clients: libvirt-clients simplifies the management and administration of virtualized environments by providing powerful command-line tools that streamline common virtualization tasks and operations. Some of the common utilities provided by libvirt-clients include:
- virsh: This is a command-line tool that provides an interactive shell for managing virtual machines and hypervisors.
- virt-install: This utility allows users to create new virtual machines by specifying various parameters such as the VM name, CPU, memory, disk size, and installation source. We will see this command in details in the following section
- virt-viewer: This tool provides a graphical interface for remotely accessing and viewing the console of virtual machines.
libvirt-daemon-system: it is a component of the libvirt virtualization management library, designed to provide a daemon process that manages and coordinates interactions between virtualization hypervisors (such as QEMU, KVM, Xen) and client applications or tools
qemu: Quick Emulator, is an open-source virtualization and machine emulation tool. It enables users to run operating systems and applications designed for one hardware platform on another hardware platform. QEMU supports various CPU architectures, including x86, ARM, PowerPC, and others, making it a versatile tool for running virtual machines (VMs) and emulating different hardware environments.
qemu-kvm: QEMU-KVM, also known as Kernel-based Virtual Machine, is an open-source virtualization solution for Linux-based systems. It combines the QEMU emulator with the KVM kernel module to provide a high-performance virtualization platform
sudo apt update sudo apt -y install bridge-utils libvirt-clients qemu qemu-kvm ibvirt-daemon-system virtinst
Verifying the installation
The mentioned command is used to list all virtual machines (VMs) managed by the libvirt hypervisor, including those that are currently running and those that are defined but not currently active. We are using this command to just check the libvirt is installed properly, this command won’t yield any output.
virsh list --all
ubuntu@ubuntu-test:~$ virsh list --all Id Name State -------------------- ubuntu@ubuntu-test:~$
The following command is used to check the current status of the libvirtd service on a Linux system. Libvirtd is the daemon responsible for managing virtualization capabilities provided by the libvirt library.
sudo systemctl status libvirtd
If the virtualization daemon is not active, activate it with the following command:
sudo systemctl enable --now libvirtd sudo systemctl start libvirtd
Configuring VM Network Settings:
When you run “virsh net-list” in your terminal or command prompt, it will display a list of active network configurations along with their names, states, and associated bridge interfaces, if any. This information helps administrators to manage and troubleshoot virtualized network environments more effectively.
ubuntu@ubuntu-test:~$ sudo virsh net-list Name State Autostart Persistent -------------------------------------------- default active yes yes ubuntu@ubuntu-test:~$
ubuntu@ubuntu-test:~/work$ virsh net-edit default Select an editor. To change later, run 'select-editor'. 1. /bin/nano <---- easiest 2. /usr/bin/vim.basic 3. /usr/bin/vim.tiny 4. /bin/ed Choose 1-4 [1]: 2
Inorder to edit the default network we will run the virsh net-edit command and here we will after choosing the editor it will open the follwoing file shown in the editor and you can edit the parameter according to your need and save the file
<network> <name>default</name> <uuid>77b523f5-384e-4501-b442-f580d3abf766</uuid> <forward mode='nat'/> <bridge name='virbr0' stp='on' delay='0'/> <mac address='52:54:00:d3:5e:a7'/> <ip address='192.168.122.1' netmask='255.255.255.0'> <dhcp> <range start='192.168.122.2' end='192.168.122.254'/> </dhcp> </ip> </network>
Access to other users
To grant access to other users for managing libvirt and KVM , you can add Users to libvirt and kvm group by using the following commands
sudo adduser ‘username’ libvirt sudo adduser ‘username’ kvm
Other Configrations
In our case we have were getting following issue when running virt-install comand
Starting install... Allocating 'my-vm.qcow2' | 0 B 00:00:00 ... Removing disk 'my-vm.qcow2' | 0 B 00:00:00 ERROR internal error: process exited while connecting to monitor: 2024-02-14T12:22:48.734613Z qemu-system-x86_64: -blockdev {"driver":"file","filename":"/home/ubuntu/iso/Rocky-9.2-x86_64-minimal.iso","node-name":"libvirt-1-storage","auto-read-only":true,"discard":"unmap"}: Could not open '/home/ubuntu/iso/Rocky-9.2-x86_64-minimal.iso': Permission denied Domain installation does not appear to have been successful. If it was, you can restart your domain by running: virsh --connect qemu:///system start my-vm otherwise, please restart your installation.
to resolve this issue we have to change in /etc/libivrt/qemu.conf :
User = "root" group = "root"
Create Virtual Machine
In previous section we have already installed the required package , Now the Download the ISO image of the operating system you want to install. In the article we will install rocky 9.2 I have downloaded the iso images it is in following directory.
ubuntu@ubuntu-test:~$ ls /home/ubuntu/iso/Rocky-9.2-x86_64-minimal.iso /home/ubuntu/iso/Rocky-9.2-x86_64-minimal.iso ubuntu@ubuntu-test:~$
Run the virt-install
command with appropriate options to create the virtual machine. To see a complete list of attributes for an option, enter the following command:
virt install --option=?
In our case we will be using the following command to Create the VM with the given options
sudo virt-install \ --name my-vm \ --ram 2048 \ --vcpus 2 \ --disk size=10 \ --cdrom /home/ubuntu/iso/Rocky-9.2-x86_64-minimal.iso \ --network bridge=virbr0 \ --graphics spice \ --os-type linux \ --os-variant rocky9 \ --virt-type kvm \ --console pty,target_type=serial \ --noautoconsole
Replace the following parameters with your desired values:
--name
: Name of the virtual machine.--ram
: Amount of RAM for the virtual machine in MB.--vcpus
: Number of virtual CPUs.--disk size
: Size of the virtual disk in GB.--cdrom
: Path to the ISO image of the operating system.--network bridge
: Name of the network bridge.--graphics none
: Disables graphics for the virtual machine.--os-type
: Type of operating system.--os-variant
: Variant of the operating system.--virt-type
: Type of virtualization (e.g., kvm).--console pty,target_type=serial
: Specifies console settings.--noautoconsole
: Does not automatically open a console after the install.
ubuntu@ubuntu-test:~$ sudo virt-install --name my-vm --ram 2048 --vcpus 2 --disk size=10 --cdrom /home/ubuntu/iso/Rocky-9.2-x86_64-minimal.iso --network bridge=virbr0 --graphics none --os-type linux --os-variant rocky9 --virt-type kvm --console pty,target_type=serial --noautoconsole WARNING --os-type is deprecated and does nothing. Please stop using it. Starting install... Allocating 'my-vm.qcow2' | 0 B 00:00:00 ... Creating domain... | 0 B 00:00:00 Domain is still running. Installation may be in progress. You can reconnect to the console to complete the installation process. root@ubuntu-test:/home/ubuntu# virsh list --all Id Name State ----------------------- 1 my-vm running
After running the virt-install
command, it will start the virtual machine installation process using the specified ISO image. Follow the installation prompts to complete the setup of your virtual machine.
Conclusion
Creating VMs on Ubuntu Server opens up a world of possibilities for optimizing resource usage and enhancing productivity. With the right tools and knowledge, you can harness the power of virtualization to streamline your IT infrastructure and achieve your business goals. In this article we have seen important packages and then later how we can create the VM in the Ubuntu 22.04.