Virtualizing your environment allows you to work with IT systems quickly and securely. However, not every application can be containerized and packaged in a Docker container - this is where KVM comes to the rescue. KVM, or Kernel-based Virtual Machine, is an environment on which it is possible to run a virtual machine. The association with VirtualBox is most accurate - the idea is exactly the same. There are, of course, more technologies allowing to create virtual environments - such as VMware or Hyper-V.
The first step is to verify if the processor supports virtualization technology. Use the following command to do this:
grep -Eoc '(vmx|svm)' /proc/cpuinfo
In my case, the command returned 16, which is the number of CPU cores. If it returns 0, you have to check in BIOS settings if virtualization is enabled. There is no one way to do it – it’s worth checking in the manual or on the manufacturer’s website.
The next step is to check whether the processor supports hardware virtualization. This is done with the kvm-ok command, which becomes available after installing the package cpu-checker
:
sudo apt install cpu-checker
If everything is configured correctly, when you run the kvm-ok command, you should see more or less this message:
INFO: /dev/kvm exists
KVM acceleration can be used
The next step to perform is to install the following packages:
sudo apt install qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils virtinst virt-manager
Finally, the user should be added to the groups kvm
and libvirt
:
sudo usermod -aG libvirt $(whoami)
sudo usermod -aG kvm $(whoami)
For the above commands to take effect, you must log out and log back in.
More – https://linuxize.com/post/how-to-install-kvm-on-ubuntu-20-04/
You have successfully prepared the environment, so you can move on to setting up a virtual machine. There are two ways to do this – you can do it manually using virt-manager
or use an automated solution. We will create a virtual machine with Ubuntu Server 20.04, so in this case we can use the autoinstall
tool.
We will start by preparing the “place” where the system will be installed. The command used for this is truncate
.
truncate -s 10G image.img
We will create a 10GB file which will be our virtual disk for the machine. Now it’s time to prepare the automatic installation configuration. There is of course the possibility of using debian preseed (Ubuntu is a mutated Debian), however the autoinstall mentioned above will be more friendly. The minimum configuration necessary for a basic system installation looks like this:
#cloud-config
autoinstall:
version: 1
identity:
hostname: ubuntu-server
password: SOME_HASHED_PASS
username: clurgo
The value SOME_HASHED_PASS, as the name suggests, contains the hashed password that the created user will receive. This can be generated in several ways, but the simplest method is to use the mkpasswd command. If it is not installed by default, it can be found in the package whois
.
sudo apt install whois
And then you can generate a password hash, for example, like this:
echo -ne "clurgo" | mkpasswd --method=SHA-512 --stdin
Don’t worry if this command returns a different result every time – this is the expected behavior (link).
The above configuration should be saved in some directory under the name user-data
. The last stage of preparation is to create a meta-data
file, which in our case will be empty:
touch meta-data
At this point, we should have two files in some working directory – user-data
and meta-data.
They should be issued over HTTP. You won’t need a special server for this, Python 3 will help you with that:
python3 -m http.server 3003
We are almost done – the only thing left is to download the ISO with the image of our server. You can download this file from the Ubuntu website. Once the download is complete, you will need to create a directory where you will mount your ISO. You can do this:
sudo mkdir -p /mnt/ubuntu-server && sudo mount /home/mlas/Pobrane/ubuntu-20.04.2-live-server-amd64.iso /mnt/ubuntu-server
The final step is to run the installation:
kvm -no-reboot -m 4096 \
-drive file=image.img,format=raw,cache=none,if=virtio \
-cdrom /home/mlas/Pobrane/ubuntu-20.04.2-live-server-amd64.iso \
-kernel /mnt/ubuntu-server/casper/vmlinuz \
-initrd /mnt/ubuntu-server/casper/initrd \
-append 'autoinstall ds=nocloud-net;s=http://_gateway:3003/'
If everything has been done correctly, after a few minutes the installation will finish – the -no-reboot parameter makes the VM shut down after the installation process.
We will now use the virtual machine manager in a windowed form. The machine is now ready to use – we have configured virtualization on the system, installed the necessary software, configured automatic installation, and then started the whole process. Now we have an image.img file, which we should run.
So we run our manager – you can do it from the list of applications or from the command line:
virt-manager
Then create a new machine by selecting the monitor icon on the left. We can now see the following window:
Select the last option – we have already created the disk image before and now we can proceed further:
After selecting Browse we will see a window for selecting a volume. You can create such a volume if necessary, but we are interested in finding the image.img file – our virtual machine is located there.
After selecting the image, we return to the previous window where we need to select the operating system – here it will be Ubuntu 20.04. After typing the first few letters, hints appear:
In the next window you can define the amount of memory and the number of processors.
I left the default settings.
On the last screen, you can set the name for the machine and complete the process:
After selecting Finish, the machine is started and we can log in:
We’re a team of experienced and skilled software developers – and people you’ll enjoy working with.
Start Your Project