Whoa this is weird.
It's like a machine inside the machine inside the machine inside the machine inside the ma-
[acoustic music playing]
Greetings, and welcome to another exciting episode of Veronica Explains. Today, I'm going
to talk about something incredibly exciting, "Kernel-Based Virtual Machines".
OK, well, I think they're exciting.
Kernel-Based Virtual Machine (or KVM) is a fantastic hypervisor baked into the Linux
kernel since Linux 2.6.20. What is a hypervisor, you ask? Well, according to Twitter:
[Child yelling "One Two Three Four!"]
[punk music]
Don't listen to them. Hypervisors let you run complete, isolated operating systems or
" virtual machines" inside your existing real-life actual-factual computer. If you aren't familiar
with the concept of the hypervisor, and you want me to make a broader video covering that
topic, hit me up in the comments.
Why are virtual machines helpful? There are tons of great reasons to use VMs, but here's
one from my own life as a dev: backup and restore. I can't begin to tell you how many
times I've borked an install of some tool on a Linux server. With virtual machine snapshots,
I can simply take a snapshot of the VM before running the install, and then revert if something
goes awry.
[static sound]
[voice garbled] Borked an install of some tool on a Linux server
Similarly, I can clone a working VM into multiple environments, simplifying testing and bug
fixing.
KVM lets you turn your Linux box into a fantastic hypervisor, without downloading tons of complex
programs for managing the whole thing. Because it's baked into the Linux kernel, instead
of a separate program running on top, it's fantastically efficient.
Other hypervisors include VirtualBox, VMWare, and Hyper-V, but I've always gravitated toward
using KVM because it's open source, baked into Linux, and free to use.
Ready to dive into KVM? First things first- in this video, I'm assuming you're on an x86
machine, like a typical laptop running Linux in 2021. If you're on a Chromebook, a Raspberry
Pi, a cloud machine, or a mainframe, these instructions may not work for you. In addition,
virtual machines take up space and RAM, so you're probably going to want at least 50
free gigabytes of storage on your machine, and at least 8 gigabytes of RAM. In both cases,
more is much, much better.
I'm also assuming you're already running some kind of Linux distribution on your machine.
In my case, I'm running Pop!_OS 21.04, which is based on the most up-to-date Ubuntu at
the time I've made the video. I've tested these instructions on my main Pop machine
as well as my Fedora 34 laptop, but as always, your experience may look a bit different.
The first thing you're going to need to do is to make sure your system has enabled all
of the virtualization tools needed in the firmware. If you're running a desktop or laptop
from the last five years, you probably have what you need, but you need to make sure it's
all ready to go.
Every distro is different, but I find this egrep command works pretty well:
If you want to know what this command does, I'll flash an explanatory title on the screen
for four seconds. Pause to learn more.
[improvised singing sounds]
Right. So, if the number displayed by the egrep command is more than "0", congratulations!
You're all set for KVM! If the number is "0", that means you need to go into your BIOS and
tweak the settings to allow virtualization. My advice is to look up the instructions for
whatever desktop, laptop, or motherboard you're using, and figure out how to enable virtualization
on your machine
Once you're done BIOS-ing, try that egrep command again, and make sure you've got
a number greater than 0. With that, you're ready to install what you need!
Now, the install process is obviously going to be different from distro to distro. My
advice is to look up the specific processes for your distro by using DuckDuckGo to google
your distro and the words "install KVM".
Psst: a quick side note: there's no shame in looking up a "how-to". I don't have every command
memorized all the time. It doesn't make me any less of a Linux expert. Learning to find
new answers is so much more important than memorizing old ones... and I don't know any
other professional sysadmin who doesn't feel the exact same way.
Anywho, in Fedora 34, you can just install the whole virtualization group with `sudo
dnf install @virtualization`. In other distros like Ubuntu, you'll have to pick the preferred
packages personally, preferably precisely, and possibly perfectly.
QEMU is the emulator itself, and the `qemu-kvm` package installs the main bits your operating
system will need.
`libvirt-daemon` is very important- a "daemon" is a program that runs something in the background,
so the `libvirt-daemon` is the program that runs virtualization in the background.
`bridge-utils` is vital for networking your VM to the rest of your network, which may
come up in your install, and `virt-manager` is a graphical program we'll use to manage
the whole thing.
There could be lots of other packages, and differences. That's why the best option is
for you to look up specific instructions for your current distro. Once you finish up with
the install, you're going to want to check these instructions for how to proceed next.
On Ubuntu-based systems, I usually have to add the user to the kvm and libvirt group,
and then restart. In my recent experience, Fedora makes this easy, since they ask for
permissions as you work in the GUI. As usual, consult your distro's instructions for help.
Once the group memberships are sorted, you'll want to make sure the libvirt daemon starts
up, and starts on boot. For that, we'll need to tell our trusty systemd to start the daemon
now, and again on startup, with `sudo systemctl start libvirtd` followed by `sudo systemctl
enable libvirtd`.
With that, we should be ready to go with creating VMs!
Before we continue, I just want to address the fact that I'm doing all of this with the
very, very good graphical program for KVM, which is creatively called "Virtual Machine
Manager" (or "virt-manager" for short). Truthfully, I find this program to be great for interacting
with my VMs. However, there's a TON that can be done with KVM at the command line, using
`virsh`, and if you want more terminal nerditude, let me know in the comments. Perhaps I could
do a whole playlist on KVM tutorials. Is that what they call it? A playlist?
Before we do anything with virt-manager, though, we're going to need an ISO to install. An
ISO file is a disk image which is often used to create boot media to install operating
systems. I'm going to assume you already know how to find the ISO of your preferred distribution.
I'm electing to try out the Fedora 35 Beta running MATE, because nostalgia.
I'm just going to leave the ISO in my downloads folder for now. In a minute, I'll show you
how we can use the Downloads folder within virt-manager.
Anywho, now we need to start Virtual Machine Manager. I'm now using my main machine, which
as a reminder, is currently rocking Pop!_OS 21.04. In Pop, I use the handy-dandy launcher
by hitting the "super" key, but your distro might do this totally differently. However
you launch an app, you want to load virt-manager.
Inside virt-manager, to set up a virtual machine, you select this "new" icon at the top left
corner of the window. When you do this, a five step wizard starts up, and this is an
extremely helpful way to get started.
In our case, we're going to use the "Local Install Media" option, since we have an ISO
we downloaded (don't forget to let me know if you want to learn more about those other
options).
Next, we're going to create a "storage pool" for our ISO files. In KVM, a "storage pool"
is exactly what it sounds like- it's an area of storage set aside for your VMs and disk
images. In our case, I want to use the "downloads" folder as a storage pool for the ISO files
I'm collecting, because Firefox conveniently drops them there when I download them. I can
create the storage pool by selecting the "browse" option, and then hitting the "plus" at the
bottom left of the screen. The "plus" is for "add a storage pool". Logic.
Then, in the "add a new storage pool" pop-up, I name the storage pool ("Downloads" in my
case), and then I browse to the specific location on my machine where I want to keep ISO files
(in this case, the "Downloads" folder). Then I just hit "finish". Now, I can select the
"Downloads" pool and can easily see the ISO files located in that directory. Now, I choose
the Fedora ISO, and we should be almost ready for the next step.
Last bit here is that the virt-manager will look up the OS you're installing, and apply
some sane defaults based on your choice. If it automatically detects the correct operating
system from the ISO name, that's wonderful! Otherwise, you can select an option manually.
Either way, when we have the right choice, we press "forward" to advance to the RAM and
CPU selector.
This section only has two options to set- Memory and CPUs. The "memory" section specifies
how much RAM you're giving the VM. In my case, I have 32GB on the machine, so I can give
a lot. I'm going to specify 4 gig, or 4096 meg. If you have an 8 gig machine, you should
consider only specifying 2 gig, or 2048 meg.
The CPU selector lets you specify the number of CPU cores you're letting the VM use. In
my case, I have plenty, so I'm going to give it two. If it were my laptop, I might consider
giving the VM just one. Once you've made your decision, we can move forward into storage!
The storage section lets you specify the amount of storage available to your VM. If you have
plenty of space on your main drive, you can probably just accept the default and move
forward. However, I'm going to create a separate pool on a second SSD, because it has more
space in my case. That's hard to say.
Basically, these files are big, and *I* want to keep them in a separate location. You might
prefer to do something differently, and that's OK!
If you want to do like I do, select the custom storage option, and then select "manage".
Then we hit the "plus" again, like we did earlier for making the Downloads pool. From
there, you browse to the location you want to use for your VM storage. Once you're there,
you give the pool a good clean name, and then click "finish". Then, we can click on that
new pool, and use the "plus" next to the word "Volumes" to add a new disk.
When creating the storage volume, I like to specify a clear name, as it makes managing
these much easier once you have a lot of them. For now, we're just going to stick with the
qcow2 format, as it's a good default and I'm not looking to make this video any longer.
You can also specify the total storage available to your VM- in my case, I'm going to give
it a little bit more, because why not? I have the room here. From here, you hit finish,
and then choose that volume from the list. After that, we get to move forward to the
last step.
The last step in the wizard is naming your virtual machine, and reviewing what you've
done. Pretty straightforward, but I like to give my VMs a clear name, because when you
end up with 20 "fedoras" it'll get really weird.
I should address "network selection" real quick. If you're using Wi-Fi for the internet
connection on your host machine, you'll likely just want to stick with the default here of
"virtual network". This has a disadvantage of not letting your host communicate over
the network with your VM.
Now, for using VMs to test operating systems, that's probably fine. But, if you're trying
to learn about servers or application development inside a VM, you'll likely want to get creative
with these options, and that's outside of the scope of this video. Again, let me know
if you want to see that in a future video.
Once we're happy with the result, we hit "finish", and just like that, we have a virtual machine
running inside our real machine.
It's super neat!
I'll skip over installing the operating system for now, but it's just like installing any
other operating system- the installer sees the virtual machine just like a real machine.
Which is awesome.
And it looks good, but I want to make the screen a little bit bigger. In my case, I'm
just going to go into Fedora and select a larger screen size, and then resize the window
with View, followed by "Resize to VM".
Now that our window is a bit easier to see, let's talk about two of my favorite features
of KVM: snapshots and USB redirect.
Snapshots are life saving technology which lets you roll back your virtual machine to
an earlier state. This is extremely useful if you're a sysadmin or developer, and need
to roll back some change because your software broke.
KVM makes rollback trivial, and virt-manager makes it just a few mouse clicks away. To
start with, I'm going to write up a file in vim that I'll later change. I save the file
and then exit vim.
Now, we make a snapshot. I hit this snapshot icon, and then hit the plus in the lower left
hand corner. From there, I name the snapshot, and can provide it a description. When I click
"Finish", it takes a moment and creates a snapshot of this live system. I love these
snapshots because they are timestamped, and in virt-manager, you can even see a screenshot
of how it looked when you made the snapshot. Super cool.
Now, we go back into our VM and edit the file. In this case, I'm simply going to add some
additional text, to use as a demonstration.
Pretend somehow this text broke your favorite web app.
To roll back, we click on the snapshot icon again, and hit the "run" icon. It'll ask if
we're sure, and we say "yes". This resets things to the way it was, and if I go back
into the VM, you can see that the text has been reverted.
You can imagine how helpful this is when you're developing an app and you break something.
Or, when testing a new operating system and breaking something. Basically, snapshots help
you recover when you break something. Which happens a lot when you're a developer.
Don't let anybody tell you otherwise.
But snapshots aren't the only trick up KVM's sleeve- we've also got USB redirection. USB
redirection lets you to send a USB device from the host computer into a virtual machine.
In our example, I'm going to use this here flash drive, which exists in the real world,
but I'm going to use it inside our virtual machine.
First off, I plug the USB into my desktop, and then inside virt-manager, I select "Virtual
Machine", followed by "Redirect USB device". Then, you select the device, in my case, it's
this flash drive, and as soon as I select it, the device gets rerouted from the host
to the VM. The VM then sees it, just as though it's plugged in there. Amazing.
From here, we can add a file to it, and when we're finished, I simply eject the drive from
within the VM, and remove the redirection from the VM using the inverse of what we did
earlier. Now, the next time I plug it into the host machine, I can see the files on it.
Magic!
I think KVM is a fantastic hypervisor for most folks. It's quick, the GUI has amazing
USB redirect, and the snapshot features have saved me countless hours on production work.
And we've barely scratched the surface on what we can do: remote VM management, migrations,
complex network connections... there's so much to explore! Leave a comment with what
you want to see next!
The thing I appreciate most about KVM is that it's a fully integrated part of Linux. Of
course it is! Because Linux is awesome. And so are you.
[child singing "One, two, three, four"]
[punk music playing]