Virtualization in Linux - part 1

dos2unix

Well-Known Member
Joined
May 3, 2019
Messages
3,700
Reaction score
3,533
Credits
32,987
This is another really big subject. I don't know how far I'll get on it. So this will likely take up multiple posts.

Virtualization in Linux refers to the creation of virtual versions of computing resources, such as operating systems, servers, storage devices, and networks. This allows multiple virtual environments to run on a single physical hardware system, improving resource utilization and flexibility.

There are different types of virtualization, and there are different degrees of isolation in virtualization. How is that for confusing? More about that later.

Before we get too far down this road. Is your computer a good candidate for a virtualization host? Well, virtualization takes up resources. More RAM, more CPU, more hard drive space. Most modern CPUs support something called hardware virtualization. It isn't an absolute requirement, but it'll make your life easier and your computer happier if it supports hardware virtualization.

How can I find out if my computer supports hardware virtualization? My first question would be how many CPU cores do you have?
Code:
 cat /proc/cpuinfo | grep cores

This often returns multiple lines of information, but usually you only need 1 line to know. I would recommend 4 cores at the minimum. People do it with 2 cores. But not very efficiently. These days some desktop CPUs have 8, 12 or 16 cores. More is better for virtualization. (The price goes up accordingly). The second things you need to know, is does my CPU support hyperthreading? Almost all modern CPUs do.
Code:
lscpu | grep "Thread(s) per core"

These will almost always be "two". Hyperthreading makes your CPU core look like it's two CPU cores. These are sometimes called "Virtual CPUs" or vcpus. How can you find out how many vcpus you have?
Code:
lscpu | grep "^CPU(s):"

This will usually be the numbers of cores x 2 hyperthreads. So if you have 4 cores, it will show up as 8. If you have 8 cores, it will show up as 16.

Next, OK, I have enough CPU cores, but does my CPU support hardware virtualization?
Code:
grep -E 'vmx|svm' /proc/cpuinfo

Intel CPUs will have a vmx flag. AMD CPUs will have a svm flag.
Another way to check is...
Code:
lscpu | grep Virtualization

This will show either "VT-x" or "AMD-V". I'll let you guess which is Intel, and which is AMD.
If your computer CPU doesn't have these flags, you can still do virtualization. But your CPU will have to emulate it. That takes up even more CPU overhead to emulate these things and it slows down your computer a little. You really need at least 2 vcpus for your host computer, and at least 2 vcpus for your virtual computer.

The second thing is RAM. How much memory does your computer have? There are a number of ways to find this out.
Code:
free -h
I personally don't like the "free" command because rounds my memory down, so it isn't totally accurate.

So we can use...
Code:
 vmstat -s | grep total

This will show your RAM, and your swap space if you're using any. We only really care about memory. Not swap.
Another way to get the same value is...
Code:
 cat /proc/meminfo | grep MemTotal
If you see a number like this... "16081376" that means you have 16GB of RAM. " 65749300 kB" means you have 64GB of RAM.

How much RAM do you need? It depends? First much RAM does your current distro use? In todays Linux, it is usually somewhere between 1.5 and 3.5GB depending on what distro and what desktop you're running. I usually recommend 8GB as the minimum for a Virtualization host. Some people do it with 4GB. But again, this isn't always efficient and often uses swap space.
It's a simple formula, lets say you need 4GB to run Linux on your computer, and lets say you need 4GB to run your VM (virtual machine). That means you need 8GB or RAM. How can you find out how much RAM your computer is currently using?
Code:
free

"top" will also show you. There are multiple ways to find out.

Finally how much hard drive space do you have? How much space do you need? Again it depends on the distro you want to run in your VM. As a rule, I usually recommend 20GB for a Virtual machine disk. This is in addition to whatever your current distro takes up. This isn't quite as bad as it sounds. Most virtualization applications such a VirtualBox and KVM/Qemu use something called "sparse disks". These are very handy, and will often only take up 1GB or 2 GB of disk space, while looking like your VM has 20GB of disk space. As you add more applications to your VM, the disk will grow in size to accommodate the new applications. But eventually the disk will often take up at least 10GB or more or actual disk space.

OK, so now you should know if your computer is a good candidate for a Virtualization host. Does it have enough CPU cores?
Does my CPU support hardware virtualization? Do I have enough RAM? Do I have enough disk space?
If the answers to these questions is yes. We can move forward.

Screenshot_20250223_051508.png

If your computer has enough resources, you can actually run multiple virtual machines at the same time. But not to worry, even if you can't run multiple VMs at the same time, you can still have Multiple VMs on your computer (they do take up disk space) but you might only be able to run one at a time.

... to be continued...
 
Last edited:


There are a few different type of Virtualization hosts. Type 1 and Type 2. In a type 1, your whole computer is dedicated to running VMs. You can't really do anything else with it. You can't browse the web, watch videos, listen to music, or play games. This would be Proxmox or VMware ESX or a few others. With type 2. It's an application that you run in regular Linux. Since this is a Linux forum, I will focus on these type of installs.

Assuming your computer met all of the requirements mentioned in the previous post, we can Install our Virtualization Server. I can't do all of them at once, so I am going to start with Qemu/KVM.

To install it looks like this.
Fedora/Redhat/Rocky/Oracle
Code:
dnf install -y libvirt-daemon-kvm qemu-kvm qemu-kvm-core virt-p2v virt-v2v qemu ipxe-roms-qemu libvirt-client-qemu libvirt-daemon-driver-qemu libvirt-daemon-qemu qemu-audio-alsa qemu-audio-dbus qemu-audio-pa qemu-audio-pipewire qemu-block-ssh qemu-common qemu-device-display-qxl qemu-device-display-vhost-user-gpu qemu-device-display-virtio-gpu qemu-device-usb-host qemu-img qemu-system-x86 qemu-tools virt-manager virt-viewer libvirt-daemon-driver-storage-disk

Mint/Ubuntu/Debian/
Code:
sudo apt update<br>sudo apt install -y libvirt-daemon-system qemu-kvm qemu-utils virt-p2v virt-v2v ipxe-qemu libvirt-clients libvirt-daemon-driver-qemu qemu-system qemu-utils qemu-block-extra qemu-system-x86 qemu-utils virt-manager virt-viewer

NOTE: You will have to reboot after installing these things.

Qemu/KVM Virt Manager will let you create disk from within the application, but I don't like to do it that way. Why? Because by default it puts the disks under /var/lib and fills up my /var directory. Instead I like to create my disks in my home directory. Qemu/KVM prefers .qcow2 disks. This is how you create one in your home directory.
Code:
qemu-img create -f qcow2 /home/ray/rocky95.qcow2 20G

Just replace rocky95 with ubuntu24 or fedora41 or whatever makes sense. The name doesn't really matter. It just makes it easier to track which disks goes with which virtual machine. You can make the disk larger or smaller by replacing the 20G with something like 15G or 30G. As I mentioned earlier, this won't actually take up 20GB of space, at least not yet.

Now assuming you installed all the packages mentioned earlier. You should have something like this in your system menu.

virtManager1.png


The other thing you'll need is an installation iso of the distro you want to run in your VM. I'll go ahead and put my opinion in here. The main reason for running a VM is to do things you can't do on your host computer. There's no sense in having a fedora VM if you're already running fedora on your host. Why run an Ubuntu VM if you're already running Ubuntu? I recommend running something different than whatever you are currently running. Don't burn this iso to a USB drive, just download it to your hard drive.

... to be continued.
 
Last edited:
If you've already downloaded the installtion iso, and created a qcow2 disk, you can actually create a VM from the command line like this.

Code:
qemu-system-x86_64 \
  -m 4G \
  -smp 2 \
  -hda /home/user/mydisk.qcow2 \
  -cdrom /home/user/Downloads/installation.iso \
  -boot d \
  -net nic \
  -net user \
  -enable-kvm

This will create a VM with 4GB of RAM, and 2 vcpus. It will use the qcow2 disk we previously created.
It will will a NAT network through my main network interface. Just change the name and location of the qcow2 disk to
whatever you named it. Change the cdrom patch and name to wherever your installation iso is located.

Ahhh.. but we don't want to do this from the command line, we want to use the GUI. I'm getting there.
I just need to take several screen shots. Please be patient.

... to be continued.
 
When you first launch the Virtual Machine Manager, it may ask you for a password. It depends on the distro. You can just type in your login password here.
screen1.png


Next you will see the main screen. Not much happening here yet.

screen2.png


So lets click on "File" and click on "New Virtual Machine".

Screenshot_20250223_082514.png

Since we have an installation iso, we will use the top option here. After you select it, just click on [Forward].
Then you will see the following screen.
Screenshot_20250223_082533.png

Click on the [Browse] button near the top to select your iso image. This is a bit confusing the first time. The following screen pops up, but you don't see your iso image.
Screenshot_20250223_082550.png

Click on [Browse Local] near the bottom.
Screenshot_20250223_082613.png

Now we can see our installation iso image. (I have quite a few here, you likely won't see that many) I will pick linuxmint-22.1-cinnamon-64bit.iso here.

Screenshot_20250223_082716.png

Now it populated the patch to my iso in the top text box. However at the bottom, it says it doesn't recognize Mint Linux as one of the default Linux types. So we will un-check the "Automatically detect from the installation media / source" box here.

Screenshot_20250223_082742.png

You can just type in Generic, and usually the rest of the text will fill in automatically for you. Click on [Forward].

Screenshot_20250223_082813.png

Next, it will ask how much Memeory (RAM) and how many CPUs you want to allocate to this VM. Generally RAM will be a multiple of 1024. I recommend 4096, some distro's work with 2048. For myself, most of my VMs have 8192. The CPUs here, aren't really actually CPU cores. They are hyperthread vcpus. Notice it says I have 24 available. But this computer only has 12 cores, but they are hyperthreaded, so it shows up as 24. After filling these in, click on [Forward]

Screenshot_20250223_082829.png

Next it will ask if you want to create hard drive for your VM. If you select the top option, this works, however it creates the disk in /var/lib. I don't like that, so I will use the disk we created earlier. If you haven't created a qcow2 disk yet. Open a console and type this.
Code:
qemu-img create -f qcow2 /home/ray/mint221.qcow2 20G

Replace "ray" with your username. I recommend about 20GB for Mint 22. You can go larger if you want.
But instead of using the top otion, we will click on the bottom option "Select or create custom storage". Then click on [Forward].
Again, we get this funky screen that is intuitive.
Screenshot_20250223_082848.png
I don't see my qcow2 disk here. So click on [Browse Local] near the bottom.
Screenshot_20250223_082919.png

Navigate to the directory where your qcow disk image was created. Notice even though I created this disk as a 20GB disk. It's currently only 197kB. You can double-click on the disk image to select it.
Screenshot_20250223_082940.png

Now, notice we have bottom option checked, and the path to our qcow disk is now populated. Click on [Forward].
Screenshot_20250223_082958.png

This is kind of the summary screen. You normally don't have to change anything here. We will just click on [Finish].
Screenshot_20250223_083041.png

After a few moments, you will see the familiar Mint grub boot menu screen. I'm not going to go through how to install Linux here. That's another thread.


But you should know what to do from here.

Screenshot_20250223_083215.png

...and now we are in the Mint Linux installer. I will mention one more thing here. You're probably used to seeing disks called called /dev/sda or /dev/sdb, or maybe /dev/nvme0n1. In Qemu/KVM, disks are named "vda". Usually you will see disk /dev/vda.
If you have more than one qcow disk attached, you will see /dev/vdb". /dev/vda is where you want to install to.

If you create more than 1 VM, and you create more than one qcow2 disk to use in your second VM. It will also be called /dev/vda, but don't worry, it's not the same disk. (Just make sure you select a different qcow2 image).
That's it for the instrall. Next, more advanced options in Qemu/KVM.
 
An excellent Thread.
1740438817949.gif


I've always used Virtualbox and have only one VM which has 6GB of Ram as I have 16GB of Ram installed.
1740439070743.gif
 
Anyone know the pros and cons of Virtualbox vs. QEMU/KVM?

It's largely overhead but it's not that much. Also, a GUI is probably easier to understand.
 
Same here! Anyone know the pros and cons of Virtualbox vs. QEMU/KVM?

I have used VirtualBox. I like it. Qemu/KVM uses slightly less resources. But supports more hardware options than VirtualBoxx and supports more hardware pass-thru options than VirtualBox.

Overall I think VirtualBox is easier to use. It does require a VirtualBox Extension Pack, which usually isn't in your vendors repo. You usually have to download it from the Oracle/VirtualBox site.

The other thing is that VirtualBox does have some proprietary code licensed by Oracle. Qemu/KVM is all open-source.
 
Last edited:


Members online


Top