Linux Hypervisor Setup (libvirt/qemu/kvm)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up everybody today I'm gonna be talking about a Linux hypervisor setup built on the stack of libvirt qmu and KVM this is a stack that I use a lot I use it in my home lab desktop if I have like an old laptop laying around it works well there doesn't require any licenses super simple and yeah overall it's just a really rad set up that you can do with with Linux hosts if you're not sure what a hypervisor is basically think of this like a hosts second host virtual machines so yeah let's get right into it so all the content I'll be talking about today is on my website I'll have the link in the description if you dig the content feel free to give it a thumbs up and that'll help me out and let me know you want more Linux content first thing we're gonna talk about are the tools that make up the stack and I want to not only focus on what the tools are specifically but also what they are as far as like their interaction together I think that's where some of the tutorials don't do such a awesome job as they don't really talk about like what's the relationship here so the three things we're going to start with our k vm q mu and libvirt starting at the very low level here we have k vm or the kernel-based virtual machine so long story short this is gonna be a thing that's in the kernel that's going to allow us to rely on kind of like Hardware almost virtualization where we get near native CPU performance in our virtual machines so there's it's pretty complex when you really unpack that but nonetheless it's a really nice performant way for us to give ourselves certain resource to or access to certain hardware resources like CPU one step up from that we have qmu which is the quick emulator which you can see where the names coming from now and this does hardware emulation so our USB device is storage it can even emulate memory CPU all that good stuff but the relationship that we're gonna have set up here is this Hardware emulator is gonna focus on again storage it's gonna focus on our USB and peripheral peripheral even our our display to a degree kind of hardware emulation and then this is going to I should say resource emulation right this is going to allow us to kind of run chill machines which then qmu will pass certain resources or I may be delegates the right word certain resources to KVM so long story short we can take advantage of Kay vm's ability to do these lower-level things like CPU while still using QM use higher level emulation for some of these Hardware peripherals and things like that and that's basically what's going to host our virtual machines now there is this other thing I'd mentioned called libvirt right now libvirt is an abstraction on top of these lower-level things basically what libvirt does is it provides an API to interact with and then it will do the talking behind the scenes to some of these lower level systems what's great about libvirt is it's attempting to be a generic virtualization abstraction wherein whether we're doing krump qm hue or k vm or whatever it might be we could actually plug something else in like Xen ESX hyper-v on and on and on and the great thing here is folks can build tools that interact with lid vert and then libvirt can be that in-between for the different virtualization technologies now not gonna lie I don't really see people using lib bert too much for hyper-v or or ESX maybe that's just me but nonetheless it's cool that it can do it it makes a lot of sense now this is kind of like the underlying stack and you might be wondering well what do I do as a user how do I interact with all these pieces and that's where our user and client tools come in the ones I'll be showing you today our verse virt-manager Vern install vert viewer it's not super important what they are but the key thing to know out of the gate is that these are tools that are built to interact with that libvirt abstraction we were talking about so be it a GUI a CLI it is going to go through libvirt to then talk to those underlying systems and allow us to basically set up virtual machines so overall pretty cool stuff there are some ancillary tools that I'll be using behind the scenes you won't even note that they're there but I did list them out in my website depending on how and what distribution you're using to install you might already have these you might get these as dependencies that are required for some of the things I've been talking about it all just depends I just wanted to call out what they were and why you might care about them I will be installing on a hypervisor that is based on arch linux but again everything i'm talking about here is still relevant to you it's just arch will be kind of the primary example from a tooling and package management standpoint so first things first let's talk about installing the tools themselves now I have gone ahead and installed the tools so if I do a quick them here and take a look you'll notice basically all the packages and let me back up the package manager is pac-man because its arch linux you might be using yum app that's all totally great just note that you might need to switch your package manager your flags and the package names themselves it is possible that they're different but you gotta consult your distro there nonetheless you'll notice all the packages i've been talking about are listed here - k vm k vm is already part of the kernel so for the most part we shouldn't have to do much the only thing you might need to do is enable kind of the hardware virtualization extensions in your bios your system might already have this enabled most modern systems do have this in their bio so this probably won't be a blocker for you but just note that it is a requirement sometimes or it is a requirement when doing things like k vm so all these packages let's assume i ran this pacman install command so they will then install and be on the system now first things first once we've got this set up is we need to take a look at permissions but what i'm gonna do here is i'm actually gonna skip over the permission section and set some things up to show you why the permissions are important just so you kind of understand why we're making these changes to the permissions in the first place so to do that we're gonna jump ahead and start libvirt d ok so in order to start liverty what we've got to do here is once it's installed we are going to run a sudo systemctl which is the command-line tool for your init system system d some of you might not be running system d but the majority are so let's just assume it's system D and we'll do status and ask for libvirt D for the daemon you can see mine is currently inactive depending on how you your package manager' and stuff set it up maybe it's re running which is which is great but just to be sure you want to check the status and if you're like me and it's not go ahead and start libvirt d and if i run status again i can now see it is active and running so effectively my command-line tools should actually work because libvirt D is alive and able to talk to the backend systems now one thing I'll call out with system D as well if you're not super familiar with it if you end up using libvirt D a lot and you like it you can always run systemctl enable this will ensure that libvirt D starts on startup this is personal preference I use libvirt all the time and I do not enable it I like having to manually enable it so I'm like cognizant that I'm turning something on on my system but again that's totally personal preference all right so now it is on and we know that there are these tools that we're gonna use now we'll deep dive into these tools but I'm actually gonna like you know just kind of jump ahead and use some of them real quick to make a point so don't get too bent out of shape that we haven't covered these tools we will very soon so the first tool that we're gonna just take a quick look at is virt-manager which is the graphical interface for interacting through libvirt so I'm gonna launch virt-manager and I will switch over to my desktop view so you can see it this is the default connection and I'm just gonna click on it I'll talk more about that soon you can see I have a virtual machine here so this is the key thing I wanted to show you my system has a pre-existing virtual machine which is which is important now in my terminal here I'm killing off a config file just to make it feel like we're starting more from scratch so if I then go to my terminal okay ignore that remove command pretend we just installed libvirt if I run verse list all VMs I get no vm's back from the command line perspective now that's really interesting right because like we talked about virt-manager and verse they should be talking to libvirt and you'd think you'd get the same results either way or at least the same virtual machines right why are we getting something different and this is why the permissions bit is a little bit important so now let's move away from tools and talk a bit about permissions themselves so what am i what am i driving out here with permissions well basically what's going on is there are on your machine different I don't know instances the right word but like different versions of QM you running particularly let's just let's just paint these out real quick so QM you to interact with it it has a URI it looks like this there's one called system okay and what you can do what's really cool about this system is like if I was running this hypervisor stack on some laptop in my house I could actually put in the credentials for like SSA Qing and are communicating through SSH so Josh at one nine two one six eight some random IP right so that's that's kind of neat in this case this is kind of like localhost so I'm doing this all on my desktop this is when I spin virtual machines up when I'm working and stuff like that so this is one URI another URI is user and user is a way to interact with this in a like more secure rootless mode where you don't just have access to everything in the system right there are reasons why user makes a lot of sense especially depending on like the specifics of what you're trying to build in most cases when you're testing things out if you're like me and you're just trying to spin up virtual machines and create environments locally for testing and stuff you probably simply want system I'm not super concerned about running as root on my desktop right you know if I was doing some other stuff elsewhere or had some specific use case this might be a different story but I can say with some amount of confidence there's a decent chance you want to use system and the discrepancy that you were seeing here is exactly this problem what's happening is that in verse it is looking at users by default and then inside of virt-manager if we flip over to it real quick where do I have virt-manager it is right here this is actually using system by default because that's what virt-manager does so you can see kind of the interesting discrepancy you end up with now this is actually pretty darn easy to solve and I've got the the commands on my website too if you want to check them out if you go into your file system there is a default config file its Etsy libvirt libvirt conf and you can copy that into your dot config directory and a folder called libvirt this is where libvirt expects this config file to live so I'll copy that over and then of course it's probably important that I give myself ownership of that file I just created so config libvirt libvirt conf and i would need to sudo that so now let's go ahead and edit it so we'll go to config libvirt libvirt conf again I did not put these commented things in they came in with the package when it was installed and the key item here if it's not here you could just type it the key item we want to set here is the URI default so in my case I'm gonna be using system obviously if you were in that case where you wanted to use user you could do that and I hope it's user I don't use it a lot I'm pretty sure it's system and user but nonetheless there's some alternative to system and you would save this up just like that and then the great thing is since this is happening at a client level now we can just go in and run verse list all there you go now I've got the exact same virtual machine I had before cuz I'm communicating with that same level I'm communicating with system in both my graphical interface and in my CLI so another way to think about this is rather than there being a whole nother one of these guys out here and we were kind of delegating to the two different ones depending on the tool right we're back down to just a single one which is exactly what we want all right now there is one detail that it might come to bite you depending on again your distro how you've got things set up all that good stuff is whether you even have access to system now back in the day we used to have to always put in this poll kit configuration that basically was a way of saying hey I'm gonna give the ability for the libvirt manage stuff to someone who's in this group you can still do that today have an example in my website of how to do it the arch wiki which by the way is awesome on this stuff also has details on how to do that but in some more recent commits in fact the heck did this commit happen watch it's probably years ago 2015 there has been some support put in here that enables us to to potentially add ourselves to the liver troop so that's an option too you could add your user to the Lib group and then be given access without all this chaotic pull kit stuff what I do and this is maybe a symptom of arch I don't know maybe your distres the same is that I have a wheel group which is already considered an administrator group and in like VI pseudo and stuff like that I've gone in and I've given wheel you know access to run sudo commands because of this simply the nature that my user is part of Weil means these commands simply work okay now all this being said if you're having problems and it's given you a headache you could also just do this right you could run sudo and then basically run the command as root but I really like the ability to not have to do that and just be able to use my user to interact with it just like I would virt-manager right so that works out pretty well for me so keep that in mind with permissions it's something that isn't that complicated but can totally hang you up for hours I've been there trust me it's not fun so we've taken a look at permissions we have looked at how to start liver pretty cool I think it's time we create a virtual machine what do you say so now we're gonna move on from permissions oops permissions to VM creation and wrap up there now as you know you have many ways to interact with libvirt which means you have many ways to create virtual machines the first way that I'm going to show you is using virt-manager the graphical interface but before I do I want to show you a quick directory in fact you can see I'm there right now it is var Lib Lib vert ok this is a directory that liberabit set up and it has a couple key file directories one of them is images images is where your disc images would go by default so you might remember my Windows 10 virtual machine this is where the file system that is packaged as a disk image is stored whether you continue this pattern is kind of up to you I like keeping it in here though another directory that's important and I've actually created this directory you know some people might have bad feelings about me doing that but I like it is a directory called ISO so if I look inside of ISOs ISOs is a directory I make where I store server installation disks so I use a lot of boom to to do installations and what I do here is I go to the MIT mere for a bun to you can get this off the bun to website I find the live server I so for the version I want and then I just W get it into this directory and I keep the ISOs here because maybe there's some new vm's where I'm not starting from like a fully baked image and I want to go through the installer again so this works out pretty well for me this is by the way the same I so you'd use if say you were doing like a USB flash drive or something like that so just kind of a nice place to keep it keeps it out of my downloads folder too not too bad now let's see if we can fire up our friend virt-manager and get a virtual machine started up so it's a pretty simple flow in virt-manager we just go up to this create new virtual machine button we do the local install media and then we're gonna browse out for that ISO now you'll notice I've got a couple different directories in here by default default is again that VAR Lib Lib vert images directory I talked to you about I also have added my ISOs folder here cuz I come here a lot if you don't want to go through these predefined folders you can also do browse local and point right to the ISO but again the reason I like this ISO bin is I keep all my ices in here usually have a couple more I click on it I choose choose volume or click choose volume and now I've got the ISO set up and it should probably auto detect the version for you to the rest is basically business as usual you know choose your arbitrary memory and CPU size you can change this later choose your disk size which while that is maybe feeling like a permanent change there are ways to expand that disk image size no problem I had to do that on my Windows one recently you're basically going to run a command line tool that will expand it then depending on the operating system you might have to go in and tell the OS to expand into the free space because the VM is gonna feel like it's just unallocated space so we create the disk that all looks good we'll give it a random name which is like octet for testing here and there's some details about network selection now one of the things that you may want to know is that you may set up a default Network and this is a default that virt-manager has set up for me something I've seen trip people up a lot is after you set up the network here you now have a network called default right but if you ever get a message that's like hey the network is not on you may need to run a command like this that starts the network so this is a command that verse will use to start networks in my case starting the default network okay so in this case it's already active don't need to worry about it but just note you may need to do that I don't talk too much about this here just because your network selection can be dictated by what you're trying to do and that's a whole nother conversation maybe for another video let me let me know in the comments if you're ever interested in kind of the different Network options okay so back to virt-manager as I cycle through my windows all right back to virt-manager so we've got this I so we'll hit finish and it's gonna create the virtual machine now what it actually pops up here is a application called vert viewer that I actually had installed and this is gonna go through a tool called like a spice server I don't know how familiar you may or may not be with spice but all basically it means that we're getting this graphical interface right inside of here and in the case of a bun to server it's actually got a a well I guess the server has an unknown graphical it's got like a tee UI or curses base like setup so all we got to do here is now go through we can continue without updating hit done hit done you know these are things you might want to think about but this is just my virtual machine that I'm testing here so I'll hit done again I'll hit continue I'll choose some random stuff here okay oops probably important if those match huh so we'll do that we'll do that oh geez this is not going the way I had planned so we'll do that great all right well install OpenSSH a server this one's pretty important I mean you could go after the fact and come back to this GUI or sorry this this vert viewer application but I'll turn SSH server on so I can access the server after the fact we will do done here okay and let's let this install while we while we talk more so now the now the image is being created and it is installing if we go back to our virtual machine for just a moment I can do an LS into into images and now you can see I have the octet disk image in here too so we've added another image in just like that and again our path was us virt-manager which was libvirt which is this and once it started and all that good stuff that'll be passing through to KVM just just by default for some of the resources so overall that's all it takes to set up a virtual machine which is wood pretty freakin cool like it took just minutes really to tie all the pieces together there now while we're waiting for that to install talk about a couple other pieces and come back to it we've set all this up we've run the install there is also the the CLI so talking a bit about this for a moment the one of the key CLI is you'll typically use in the stack is one called verse which is basically gonna be like a way to interact with the specifications for the virtual machine so in the case of verse I this won't be exhaustive be just because you know it's it's a whole topic in and of itself but you can run verse list which will tell you which virtual machines are running you can do verse shutdown right and give it a give it a domain here or an ID which could be one or maybe even the name would work right so verses gonna basically give you those different interaction points it's even got cool ways for you to like list all the network devices so one thing I used verse for is listing all like the net MAC addresses and stuff and what IP addresses are associated with the VMS so yeah just a really cool really cool command-line tool that acts in a way not too dissimilar from virt-manager itself just being a CLI now another thing I'll show you while that one's installing and we'll just kind of get this teed up here is the idea of a a CLI for making new virtual machines now you can do this with command-line utility around qmu but vert install is a really nice convenient tool that will let you do most of what we just did and you could put it in scripts and things like that so we'll take a quick look at this let's go into my temp directory and make one called vert installed shell and I'll just go ahead and make it executable too if when we get out so vert install that shell okay all right so this is the command that you can't see it can you okay so here's this is where I'm at and I will do just so you know where I put this and I just put it in my temp directory not that it matters and basically we're doing some different parameters here so this will be like octet - all right so we'll do tests - I'd put my RAM in here I'd put my disk path in here this is just the arbitrary name so Oh - and then probably the most important piece is I'm pointing to the cd-rom which is that same ISO I had showed you before so basically what you just saw in virt-manager Verte install is gonna do the same thing but with this command which we could obviously include in our scripts we can include and all kinds of different stuff so not too bad okay so taking a look at virt-manager again let's see how far we've gotten here how you do invert manager okay so yeah it looks pretty good it's doing an update so hopefully I won't regret canceling the update I'm gonna try to cancel the update and reboot the machine here and then we'll be able to go ahead and ask this HN do it and and all that good stuff so while that's going we could actually just fire this one off just to show you real quick so let's run this command and I won't I won't go through another install with you but just to give you an idea of what it does if we do vert install it will say oh the guest name octets is already in use so let's take a quick look at that so vert install Oh that's literally the name of my other one so should be octet stew um so yeah I guess I guess the key thing we should know about vert install is it is actually gonna set up right through libvirt the vm not just like copy the the filesystem over so we'll go ahead and deal with that so now we'll do a virgin stall and you can see it's allocating the disk and it's even smart enough to pop up right away the installer here right and and bring this up so I can choose install Ubuntu server and go through the installation process so again two ways to kind of do the same thing pretty nice and and now I can just cancel out of this and let that go so I'll probably and this will be a good example of verse I'll probably end up with this kind of orphaned oh geez I'm in verse now sorry so we'll do vers les yeah so you can see octet stew is running so we'll go ahead and do a verse shutdown just so you can see that of octet stew hopefully it won't get hung up here there's ways to force the shutdown and all that good stuff so watch British list the list and see if it see if it shuts down in a moment if not I can force it to shut down so we'll go back to our virt-manager now that it is installed and take a quick look so now I'm in virt-manager still but I've got my VM I can quickly log in and if this text is small don't worry it'll we'll be out of it in a second but I'll look in the IP addresses I can see that 192 168 1 2 2 1 2 2 got allocated ok so I'll close out of that ok and we'll come back here right and now we can SSH into my username at 192 168 1 2 2 1 2 2 it'll say yes I can do that and that's it I'm in my virtual machine so now I've got a virtual machine I could install docker on it I could you know basically do whatever I want because it's a fully functional a bunt 2 VM I have the virtual machine again in a bun too so there's no reason why I can't do app updates right and run updates on apt upgrades all that good stuff so overall looking pretty good we've set up a virtual machine no problem at all now the last thing I want to leave you with here now that you've seen how we can do this with a CLI and you've seen how we can do this with a virtual machine let's are sorry with the vert manager so many virtual the names here I'm getting confused let's talk about one last thing that's kind of fun which is to clone a VM so a really common thing that I'll end up doing and there's there's some intricacies to this but for now assume we went into that VM which we're in here right and we did like sudo apt install docker that install docker yeah so do i docker nope sudo apt install and I can't remember what the actual package name is sudo systemctl status docker okay so I didn't let's let's do something easier sudo apt install nginx there we go alright so I install like nginx and a bunch of things in the server and then like I want to create ten of these servers effectively that's the beauty of virtual machines right you can kind of lay a foundation and then just start stamping them out and not have to go through the installer every single time now intricacies aside once I've got the VM in a state that I need it I can actually leverage this command for cloning of VM which is a command-line tool called vert clone now you might think to yourself and you'd be right that if you look inside of our Lib Lib vert it was my someone set my phone to an annoying an annoying alert thing there sorry about that so if we go into lib live verdun look inside and go to images couldn't you just copy this disk image in the answers yeah I mean you could copy the disk image and reattach a virtual machine to it no problem but then thing about vert clone that is pretty cool is it's gonna take it a step further and it's gonna basically enable us to specify a virtual machine we want to use as a base it will find where its file system is and clone it for us and also set up all of like the virtual hardware settings and all that stuff in the VM spec based on what our other virtual machine was so all we've got to know here if we do a verse list and I've got to kill that other one forcefully at some point but but essentially if we do a verse list we can see what our what our different pieces are and I just sorry I'm talking while I'm doing something there we go now it's now it's shut down forcefully so verse list okay octet now let's say octet is what I want to clone from okay so I'll verse shutdown octet just cuz I don't need that thing running for no reason and now this one shut down healthily so we'll do all all right octet says shut down good deal all right so we know it's name is octet so what we're gonna do here is we're gonna specify the original flag which is going to be octet oops and we'll just do this in vim real quick so cloned Sh I'll definitely want to get rid of this file in a moment let's you know I want to put this here we'll do it we'll do it here again so we'll do the vert clone command I just got to remember my backslashes original is going to be octet okay and then we need to specify the name of the new one so the name this will be octet three okay set that up or actually this would be two well technically it's three because the first one wasn't indexed okay so let's let's just keep it like that and then we're gonna go ahead and put in the location we want the the image or file system to use so I'm gonna go ahead and do the file flag and the file flag will go to var Lib Lib vert images and we'll call it o three dot Q cow - alright so now we've got all this in place we'll hit enter the cloning process is going to start it's gonna copy it all over for us set up a new virtual machine and now in theory we should have a new virtual machine that's ready to go so if we do verse list all there's octet three and then we can do start octet three right and then while it's starting from verse just to kind of again show us the many many ways that we can be looking and interacting with libvirt I'll go back here open up the graphical console from libvirt and you can see from the libvirt perspective that it is in fact starting up the virtual machine so just like that we have seen many different ways to create a virtual machine including the cloning of an already existent virtual machine where not only was it way faster because I didn't have to go through the installer but also notice that nginx is inside of here because it was part of my my base image right so this in a nutshell is the libvirt qmu and KVM stack if you've used this before hopefully this maybe gave some clarity about how all the pieces work together I know I used it for a little while and I didn't really understand the mechanics not that we went super deep but you kind of see where things map together and just overall I hope you're pretty stoked to create vm's in a really easy and lightweight way on linux so again if you want this information in text and some of the snippets linked to my website is in the description and if you like this video give it a like and I'll continue to make more Linux videos catch you in the next one see you later
Info
Channel: octetz
Views: 53,599
Rating: 4.9699998 out of 5
Keywords: linux, libvirt, qemu, kvm, arch, hypervisor, virtualization, server, vm
Id: HfNKpT2jo7U
Channel Id: undefined
Length: 32min 11sec (1931 seconds)
Published: Sat May 23 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.