Linux Logical Volume Manager (LVM) Deep Dive Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey guys how's it going Jay here once again with another video and this time it's all about logical volume manager or LVM that's right I'm doing an entire video about LVM so I can show you guys just how awesome it is I'm going to show you why it's awesome how to set it up some of its greatest features and in this video you'll know all of the basics of LVM and maybe even some advanced stuff too so let's go ahead and dive right in so here in my laptop I have two ssh sessions open i have one - a server called noel VM and another to a server cleverly named yes LVM so I guess you could probably tell which one has lbm setup in which one doesn't and the reason why I did this is because I want to show you guys real quick why you want LVM one of the many reasons why I think it's a good idea especially on servers now what I'm going to do is open up T MUX you don't have to follow along with this because this is not a team X tutorial series I have a series on that so if you are curious how to use it T MUX then you can go ahead and check out that video series but the reason why I'm doing this is so I could do what's called a split and it looks like that I have a terminal on the right and a terminal on the left and that's because I want to show you guys the available disk space on this server while I intentionally break it yes that's right I'm going to intentionally break this server I'm going to show you right now exactly how I intend to do that so what I'm going to do is on the right hand side I'm going to do watch DF - H and that's a little bit big so I'm going to lower the font size temporarily here I think that's better for now I just want to make sure you guys are able to see it but the watch commander it's the same command every two seconds and I'm just doing D F - H which is showing me the available space on this server and you can see like right here it's 27 gigabytes that are free right now so we're only using three point eight gigabytes so what I'm going to do is I am going to go ahead and just fill up the hard drive space really fast that's right I'm going to completely saturate the free space on this server again this is not LVM so let's go ahead and see what happens so the command that I'm about to run don't run this guys this is a destructive command it causes problems do not run this if you must run it run it in a VM or something completely disposable that you don't care about but anyway what I'm gonna do is switch to route and then the command I'm about to run is going to fill the hard drive quick so what you want to do is watch this right here on the right the available space on the root filesystem we're gonna watch that drop are you ready so what I'm going to do is cat dev zero and I'm going to redirect that to a file and for the file I want to simulate a real problem that happens a lot and that is a log file getting crazy and just filling up your hard drive it happens I've experienced it I think many others have as well so what I'm gonna do is type that - var log and I'll call it application that log just like that and I'll press ENTER and just watch the right-hand side watch the free space just drop you see that counting down nineteen seventeen fifteen its just continually dropping so this is a bad application that is just logging a bunch of stuff and it's filling up the entire file system right now we are running out of space very quickly and the command aborted and we got this right error there's no more space left on the device so even if I wanted to do something like echo learn Linux TV pipe that to you test file I can't do that there's no space left on the device that's how bad it is I can't even write a text file and this is a scenario that happens a lot now in this case I can obviously clear that file delete that file but if I had LVM I could actually grow the filesystem to get myself some additional space but since I don't have LVM setup i can't expand the filesystem at all it's full for real until I find a way to clear whatever the problem is as far as why it's full now one thing that I can do in this case I guess is I could use a truncate command maybe with the size of zero against the same file that I use to fill the drive with so I'll do var log application dot log just the same file as before and then you'll see the available space just come back pretty quick let's do it and boom we're back to 27 gigs free so I was able to recover very quickly but I did lose all of the logging information now there's other ways I could have recovered I could have even gzip the file but even that wouldn't work because the drive is full but there's more than one way to recover from this situation but one option that we didn't have was the option to actually expand the file system so let's go ahead and see what this looks like if I was using LVM so I switched over to this server right here which does have LVM installed and set up so if I do D F - H just to show you guys you can see that the you know this looks a little weird right I mean it doesn't look like it looks here where it shows the root filesystem you know dev s da one right here and that's where the root filesystem is and basically you can see that there's a longer device name here that's what LVM looks like when it's set up I have a volume group named Ubuntu or VG underscore a boon - and a logical volume LV underscore root I'll explain what that means in just a moment but we can already see some differences I do have less available space here and that's actually because the root file system size is smaller I'll show you why that's the case in just a moment but we definitely have enough free space on the server there's 38 percent that's being used and 4 point 4 gigabytes available I mean not a lot but it's fine the server's fine there's nothing going on here it's running just perfectly normally so we're good but what I'm going to do is go through the same exercise here so I'll open up T MUX again and I'll split the windows here so in this case I think what's probably better is if I split it the other way maybe I should have done that with the first one and I can have the font size bigger - that's pretty cool anyway I'm going to repeat the exact same thing so I'm going to switch to root and again don't follow along this is destructive as I mentioned so I'm going to cat dev 0 and we're going to redirect its output to var log application log and again just watch the free space drop so I'll press ENTER and we can see it's already dropping down and same thing no space left on device we have completely saturated the available space on this server so whatever can we do to recover from this now I could run the same truncate command as I did on the other server for the problem with that again as I lose all of that logging information may be actually needed that data for you know debugging to find out what caused this problem in the first place and what I'm going to need to do is just find another way to recover and I'm going to use LVM to recover in this case to show you guys one of the many ways in which it is awesome now unfortunately I do need some free space in order to actually expand the file system now I don't need much but I can clear maybe a different file or clean up something else what I'm going to try right now is apt clean to see if that's enough to recover and then I'm going to run this command right here Elvia extend and don't worry I'll explain all of these commands later in this video now watch the bottom of the screen it still shows that 100% of the file system is in use so I'll press ENTER and boom look at that not only did I fix the problem I now have more available space on this file system than I even started with look at that that is just awesome I was able to resize the root filesystem without restarting the server and that's important to know because if this was an actual server that had an actual purpose and maybe the users are complaining that there's no space available on the server at all I was able to fix it with one command and without restarting the server which means that the application may have been running maybe not well but it was running and I was able to fix it online without a shutdown or a restart now that is one of the many reasons why LVM is awesome and one take away with this is that if you are setting up a server in my opinion you should always setup LVM on your server now worst case scenario you might set up LVM and never need it never use it never even remember that it's there but if you ever want to benefit from LVM you'll wish that you had set it up that way so I do recommend setting up LVM when you first set up a new installation this video is sponsored by Linode my cloud infrastructure provider for over two years Linode provides linux servers that make it easy and affordable to host your own app site or service live in the cloud whether you're a linux power user or just starting out you can use Linode you can start from scratch and fully customize your server for any application or use Leonard's one-click apps to deploy game servers WordPress sites personal VPNs and much more you can even upload and run your own image servers can be easily scaled up or down so you only pay for what you need and regular backups are also available so you'll never lose your work best of all Linode comes with 24/7 support that is 100% managed by humans by phone support or support ticket to get $20 in free credit when you create your new Linode account sign up at Leno comm slash learn Linux TV the link is in the description I'd like to thank Linode for not only being awesome but also for their continued support of my channel I really appreciate it now let's get back to the video now what I'm going to do right now is I'm going to show you how to install Lubuntu server with LVM and then in this video I'm also going to give you all of the LVM commands to manage it and then I'm also going to show you guys how to setup LVM manually and you know what I might even throw LVM snapshots in here too so what I'm gonna do right now is switch over to my browser and I'm going to set up a new VM and I'll show you the section where you can actually setup LVM during the initial installation so here I am on my proxmox server this is the server that I use for basically most of the VMS in my data center here at home actually have a data center in my house can you believe that it's called a home lab and you know what I've been doing some videos about that if you guys are curious I even have a playlist now I just started doing home lab videos so there's not a whole lot of those right now but what I'm going to do is show you the process that I went through to create a VM so I'll expand this here and we have the two servers right here that I'm using for the purposes of this video so I'm going to create a new one I'll give it an ID of 912 and that's going to keep it right along with these right here now this is an approximate tutorial so I'm going to go ahead and go through this kind of quickly here so I'm going to call this LVM example two I think that's good enough for the OS I'm going to use the Ubuntu server ISO image just speeding through these 32 that's fine I'll have this on my local LVM nd I'm even using LVM on proxmox just to show you guys how much I like it CPU one is fine memories fine that doesn't really matter and the network I have a dedicated network for VMs and let's do it I'll start after created that's the checkbox here finish and this server is being created right now and I'm going to go ahead and install Ubuntu server now I am going to go through this pretty quickly here because I have other tutorials that show you how to setup a boon to server I just want to speed along to the part where you setup LVM so for the hostname I'm going to go ahead and just call it the same thing LVM sample two you now here's where we actually have some options okay we can use the entire disk we can use the entire disk and set up LVM and we can also have encrypted LVM as well now encrypted LVM is not something I'm going to go over in this video that's a little bit more advanced and you might be thinking well here we go here's the option great well no I mean yes that is LVM that does give you an LVM system but that's definitely not the one you want and one of my irritations here and maybe they will fix this in a newer version is that they don't let you choose how much of your hard drive to dedicate to LVM why is that important well because if you are going to want to benefit from snapshots LVM snapshots you'll need some unclaimed space on that device and if you don't have that you can't use snapshots and this is going to use a hundred percent of your drive which means that it basically cancels you out of being able to use LVM snapshots now what I do instead is I go down here to manual and this hard drive has never been used before so I need to initialize it so I'll select it enter and then yes for a new partition table and now we have the option right here configure logical volume manager I'll press ENTER and yes so basically what I'm going to do is setup LVM manually because again I don't want to use a hundred percent of my hard drive straight out the gate I want to have some unclaimed space now if you recall on the second server I was able to expand the filesystem and grow it the reason I was able to do that was because I had some unclaimed space that was not being used if I had a hundred percent of that hard drive used for LVM I would not have been able to do that and when I set up a server even if I don't want to use snapshots I usually reserve a couple of gigabytes unclaimed so that way if my users have an issue with space or something and I need some emergency space in a pinch I have a few gigabytes that I can use when I need to and that's great because that gives me a little bit of a buffer another reason why you don't want to use a hundred percent of your drive when you set it up now before I setup LVM here I want to show you guys exactly what an L VM layout actually looks like in practice and for that I have this browser window open and I want to give credit to the geek diary for this image I didn't create it you can even see in the URL right up here I simply googled for LVM Linux and this was one of the images that came up I thought that this would be a good one so credit to the geek diary com for this image and I'm going to go ahead and explain it now down here we have the lowest layer all the way up to the upper layers here so down here we have one or more physical volumes a physical volume refers to a hard drive and you know what it could be a virtual hard drive a physical hard drive it doesn't matter it's some sort of hard disk that the server has installed now you could have just one hard disk and in fact if I go over to my server here and I look at the hardware I actually only have this one hard drive right here so when I in the console here setup LVM I am creating a physical volume and that's this right here a physical volume in LVM is basically where you get a hard drive you create a LVM physical volume out of it which means I am going to use this for LVM a physical volume means that you have initialized the hard drive so that it can be an LVM physical volume that writes the information to the hard drive that allows it to be used for that purpose on the upper layer you have a volume group and what a volume group actually is is an arbitrary container for logical volumes you can see here we have a logical volume in their example and we have one here as well so for example you can have a logical volume for slash you know the root filesystem another one for slash home if you'd like you have one for /var you can basically create more than one logical volume for multiple purposes and I'm going to explain this in more detail later in this video and then when you create your logical volume you will format it with a file system you know for example maybe ext4 that'll allow linux to work with it as it would any other ext formatted hard drive even if it's just a regular hard drive with you know that's formatted ext4 it's the same thing except the logical volume presents an additional layer on top of the volume group which can reside on one or more physical volumes you might be wondering then okay I have one physical volume why does this example show two more the reason is because you can add additional hard drives to your system so let's just say for example you did decide to use a hundred percent of your hard drive for LVM you have no unclaimed space you can't use snapshots you can't expand it actually you still can you can add another hard drive to your server configure it as a physical volume apply it to your volume group and then you can expand the volume group to be consisting of both of those hard drives so if you have two 16 gig hard drives now you have what is seen as 132 gig hard drive and your logical volumes will still be the same size but you can grow them to make them larger and expand them which is where we get the benefit so back here at our proxmox window I'm going to go ahead and set this up so let's choose the first option display configuration details physical volumes none volume groups none we have nothing set up right now so what we need to do is create a volume group I'll press enter and for the volume group I'm going to just call it VG underscore Lubuntu because that's the distribution that I'm running currently or the one I'm installing but wait a minute we need a physical volume for the volume group if you recall we have this volume group right here but it sits on top of one or more physical volumes we don't even have a physical volume yet yes we have a physical hard drive but it's not configured as a physical volume for LVM and that's what we're doing it's showing me all of the hard drives that I have on the server right now I only have this one slash dev slash SDA so by selecting this I am actually going to be declaring that this hard drive is going to be a physical volume for LVM and it's going to be used for the new volume group that I'm creating so I'll go ahead and continue it's asking me if I want to write the changes yes I do and let's display the configuration details now unallocated physical volumes none if we had additional hard drives or additional physical volumes and maybe I have some physical volumes that are not claimed by this volume group the extra ones will show here I don't have any extra ones I only have the one anyway it's using the physical volume that's this one right here and the volume group is VG Ubuntu so we have effectively set up a physical volume and we created a volume group called VG underscore obutu that's what we've done so far continue now we need to create a logical volume we can have more than one if we'd like but I'm going to go ahead and just create one so I'll press Enter on that and it's going to ask me what volume group I would like to use for that logical volume or or which volume group I would like that logical volume to reside in I only have the one enter logical volume name my naming scheme is LV underscore and then the purpose so I'll call it root for root filesystem and then enter and here's where we get to declare the size it's going to default to all of the space that's available that's not what I want to do though I am actually just going to give it eight gigabytes so you can just simply do 8g and it gives you some examples here and then I'll press enter and let's display configuration details again so we see we have LV root now so what we've done and you know by looking at this example is we've created this logical volume so we're at this part right now so I'll continue and then finish now we're brought back to the partitioning screen and right here we have our LV root volume logical volume right here that we can use for the installation so if I press ENTER it says use as and it defaults to do not use so what I want to do is format it with ext four so that's the filesystem I'm going to go with and that brings us to this I'm going to format the logical volume as ext4 so that the installation can use it so enter the mount point I want it to be the root filesystem and then I'll go down to done and then finish partitioning and write changes to disk then I'll say yes and now it's installing Ubuntu on the LVM on the logical volume that we created which is in the volume group that resides on the physical volume so we should be all set I'm not going to let that finish though because this is the end result right here I already have this set up I'm just going to delete that one off-camera but you get the point that's how you set up LVM on the initial installation so now what I'm going to do is walk you guys through a couple of examples of some additional things that we can do with LVM so if I do LS BLK here on this server we can see that we have a hard disk SDA we have a partition SD a1 and that's our physical volume on that we have a volume group named VG underscore of boon - and the logical volume is LV underscore root now what I'm gonna do is zoom in to this shell right here I'll increase the font size let's run some commands so I'll do PV display and I'm running as root you need to have root privileges to run LVM commands and you can see that PV display gives you information about the physical volume so we can see it's /dev / sta-1 it has volume group VG underscore obutu on it and it's a 32 gigabyte is showing it in gibi bytes but I'll just say gigs just because I'm used to it and right now it's full there's there's nothing available it's just completely used here I cannot expand this anymore it's expanded as much as it can be similarly I can run BG display to display details about the volume group quite a bit of info here huh so I'll make the font size a little bit smaller there we go so volume group VG underscore Ubuntu LV m2 is a format that's pretty much always the case nowadays we get some information about this particular volume group and just like I can run VG display as you can guess I could do LV display as well to view info on the logical volumes that are on this volume group and we have LV root right here that's where the Ubuntu distribution is installed is right there and that's why we can do DF - eh because see where it's mounted there it is and if take a look at the the FS tab file and we can see right here where it's being mounted this is what is being mounted this is where it's being mounted we have ext4 is a file system standard stuff here I'm not going over the FS tab just showing you that it comes full circle because this is how it knows to mount it this was set up during installation it already configured this for me I didn't need to do that now let's do something fun let's expand the root filesystem to be a larger size there's a problem though we don't have any additional space to work with it's maximized so what I'll do is go back here to proxmox and I'm going to go to our LVM server here to hardware and what I'll do is add a hard disk 32 gigs that's fine I'll just go ahead and add it so now we have another hard drive on that server I didn't even need to shut it down so if I do LS BLK legacy now we have s DB we have another hard drive on this server that we can benefit from so let's go ahead and use it so to turn the second hard drive s DB into a physical volume we will use the PV create command you'll need to use sudo or B running his route as I am I prefer to run as su do but you know I was already logged in his route when I went to destroy the filesystem or fill it up so I was still logged in his route it doesn't matter either way you need root privileges in some form or fashion so P V create and then dev s DB be very careful here because if you use P V create against a hard drive that's not the one you wanted to use it on you are wiping out your entire file system so just be sure that and I am so enter and that was quick literally that easy we have converted the second hard drive into a physical volume for L um it's ready to go however that doesn't really give us anything as far as additional space for LVM I could do PV display and we can see dev s DB is a new physical volume volume group name none it's not part of a volume group it's just a physical volume for LVM that's not claimed by LVM let's take care of that right now what we can run is the VG extend command and again we need root this is the last time I warned you about that because you know everything I'm doing basically needs root here so VG extend is the command and what we want to do is type the name of the volume group that we want to extend and now what do we want to extend it with well we just created a physical volume /dev s DB so we are going to extend PG ubuntu by adding this physical volume to it so enter and it's been successfully extended now if we look at the free space its unchanged there's no changes here we're not that far yet so if I was to do VG display we could see something interesting here we have about 32 gigs free that's not allocated so we have some additional room here that we can basically play with now we could use this extra space to create a new logical volume if we wanted to or we could just simply use it to expand the one that we have again we only have the one which is the root filesystem you know this line right here we only have the one logical volume so let's go ahead and expand that one so what I'm going to do is run LV extend to extend the logical volume - uppercase L I'm going to add just ten gigabytes to it I'm not going to use the entire thing now earlier in the video we did use the 100% free it was a small L instead of a big L and I'll have all the commands in the description below in the wiki article so you don't have to rewind the video you could simply just click on that link and scroll up and you'll see that command you could use the 100% free method to give it everything but in this case I just want to give it 10 gigabytes extra because maybe that's all I need and then what I want to extend is dev mapper and this was in the D F - H output V G underscore of boon - - L V underscore route that's the one that I want to expand and let's do D F - H and see the magic and it's the same nothing has changed well why is that well because we have one more step we need to resize the file system so that it can then use the available space that the LVM logical volume now has now for that we can use the resize - FS command and then the I'll just type it out here just like that resize - FS is not specific to LVM but this command will expand the file system to take advantage of all the extra space that it has so enter and here we go now one thing to note here it says file system at then it has the path is mounted okay so it's mounted it says online resizing required well great that's what I wanted anyway I wanted it to be resized online without having to restart the server and now we have more space available it's now a 42 gig volume and we have 33 gigs available to us now so I was able to expand that how cool is that and you know what I'm going to expand it again I'm going to on zoom in T MUX and again I have a whole series on Team X so don't worry too much about how I'm doing this but we can watch the filesystem space on the bottom because I still have that command running now what I'm gonna do right now you know what I'm just gonna give it the remaining space why not so I'm going to run LV extend again but with a different method this time I'm going to do a couple of things I'm going to add the resize FS option to this so it'll automatically resize the logical volume and I don't need to run that extra command - lowercase L this time last time we used an uppercase L plus 100% and then free in all caps and then the path now check this out now watch here at the bottom watch this line when I press ENTER boom look at that now it's 63 gigs and we have available space and that's great we were able to do that online without having to restart the server now I'm gonna show you guys another example I'm going to go ahead back here on my proxmox and i'm going to add another hard disk to this machine so basically the same thing I did before I'll just leave it as a 32 gig volume that's fine we now have three hard drives on this server and I'll prove it because now we have SDC that is completely unconsidered so I'm going to go through the same exercise again PV create we need a physical volume and that's all there is to it and now what I'm going to do is create a new volume group a brand new volume group let's create a new one so the command to do that is this one PG create I'm going to call mine VG extra for extra storage and I'm going to use the new physical volume dev SDC to be the one to use for that and now I'll do VG display and basically have the smaller here it'll show me all of the volume groups but it's already on the one I want and VG extra that's the name we gave it and allocated is zero nothing is being used all of it is free so that's great because we could go ahead and use it for whatever purpose we want so let's create some logical volumes so I'll do LV create then I want to use VG extra that's the volume group I want to use for this logical volume - uppercase L I want it to be five gigs and - n I need to give it a name LV underscore logs maybe I'm going to store log files here or something I don't know just an example then enter then LV display and we have it right here we also have the path for it great now what we need to do is format it before we can use it I'm going to use the mkfs txt for command I want to format it with ext four and then I'll type the path dev mapper VG extra and I just tab completed that enter and now it's formatted now right now you can see that I only have this so what I need to do is actually mount the new logical volume that I created but first I need to create a directory to mount it so what I'm going to do is mkdir - P because I'm going to be creating a couple layers of directories here /mnt extra and then logs and now what I can do is mount dev mapper PG extra and I can mount that to the new directory extra logs enter and sure enough I have now mounted the new logical volume right here and I can go ahead and use it I'm not quite done yet what I want to do now is make sure that when I boot the system that this logical volume will automatically be mounted now there's a new ways we can do this now the way I like to do it I'll run blk ID and I'll run it against dev mapper you guessed it VG extra the same path and it gives me this UUID this is a universally unique identifier I'm going to use that to refer to this new volume now you could use dev map or VG extra you could use that but I want to go ahead and do this now what we want to do is make a backup copy of the FS tap file because we're about to edit that so what I'll do is just copy that's EFS tab and I'll copy it to just be K for backup or something like that that's fine we just want to make a backup of it just in case we make a mistake the next thing we want to do is you mount mount extra logs it's not mounted now we can go ahead and edit the FS tab file so that way it'll automatically be mounted when we reboot the system so nano Etsy FS tab just like that so here we have the FS tab file so what we'll do is just basically add a new line down here UUID equals then I'll paste in the UUID that I copied earlier this will be different for everyone and then we type where we want to mount it to mount extra logs and then the filesystem type is ext4 I'll use defaults for the options then 0 and then to the last digit right here to means I just want it to be the last priority if there's a filesystem check the root filesystem is more important and then ctrl o to save and you could pause the screen if you need to jot down the formatting again I'm going to have this in the description below there's going to be a wiki article anyway but I've essentially added this information to the FS tab file now again this new volume is not mounted so what I want to do now is test the FS tab file because you should absolutely not restart your machine until you have first tested the FS tab file to make sure that there's no problems we could do mount - a because what that is going to do is mount everything in the FS tab file that's not already mounted and we don't have the new volume mounted because I unmounted that so let's press ENTER and it looks like it worked there are no errors so DF - H and sure enough there it is we know the FS tab file is fine we're all set because there were no errors and it was able to mount it so now what I'm going to do is show you guys LVM snapshots and to do that why don't I just go ahead and create yet another logical volume I mean I do have the space for it why not let's go ahead and do that so LD create and I want to create the new logical volume in VG extra just like before and I'll give it 5 gigabytes in this case with a name of LV underscore web just a random example maybe we have a web server and this is the logical volume that stores all of the files that are being served maybe I don't know simple example let's press ENTER and we did create that logical volume so good so far then we need to format that so mkfs ext4 just like last time dev mapper in this case PG extra and web just like that that formatted very quickly we should be good to go now we're going to go ahead and repeat the same exercise again so copy at CFS tab they know that this one works so I can copy over the original backup then I'll run block ID against the new logical volume and here we have it and since I've already made the backup I can go ahead and just edit that file at CFS tab again and I'm going to do the exact same thing I'm just going to do UUID equals I'm going to paste it in mount extra web in this case ext4 and then defaults zero and then two just like the first time and I'm going to save it but there's something I forgot to do pretty obvious but I did forget a step what I need to do is make the directory that it is actually going to be mounted too otherwise it won't work so mkdir /mnt extra web just like that you can see we only have the root filesystem and the logs logical volume we have these two mounted we don't have the new one yet so we should be able to run mount a if there's no errors looks good to me and here we have it so we have that mounted that's awesome now to explain snapshots what I am going to do is create a file on the new logical volume so I'm going to do echo and then LVM is awesome in all caps because LVM is just that awesome and I'm going to direct that into mount extra web and I'm going to call this important underscore file.txt because this is an important file we definitely don't want to lose this Dewey so I'll press ENTER and sure enough let's just make sure the file is actually there it's there we can see the path works out we have that file that's awesome so now what I'm going to do is create a snapshot of the web logical volume so that way I have a recovery point if I ever need to you know recover it basically or just recover a specific file or something like that I just want to snapshot to protect myself in case something gets accidentally deleted so here's how to do that so we're going to run LV create and then for a snapshot what we're going to want to do is type out the path to the logical volume that we want to take a snapshot of even though we want to create a snapshot we're still using the LV create and if you recall the LV create command we use to create a logical volume in the first place but that's also the command that we're going to use to create a snapshot as well so I just typed out the path to the logical volume we need to give it a size and I'll explain what that means and why we need to do that in just a moment I'm going to give it a size of 1 gigabyte so this does require that you do have unclaimed space which I do because I haven't used up all the space just yet we're going to do - s because we are clarifying that this is a snapshot and then we want to give it a name so - N and then you could come up with your own naming scheme but for me I'll type this web snapshot then the date close enough I'll press ENTER and it created a snapshot so if I run the LVS command for example which is yet another command we can use to get some information about logical volumes we can see that we have the new one right here this is the original one but now we have this one and we can see that its origin is set to LV web which is here that's how we know it's a snapshot because it has an origin none of the other ones do just this one and the origin is the name of well it's the name of the logical volume that it is in origin of and data percent we have a percentage right here why is that important so if this gets to a hundred we're in trouble because one downside of LVM snapshots there's ways around it which is you know beyond the scope of this video but the downside is that we have to be careful that we don't let this get full anytime we make changes to this you know this logical volume or the source or whatever it's going to use additional percentage of space to basically you know store the changes basically so what we want to do is make sure this doesn't get to a hundred percent and for that reason we generally don't want to keep snapshots around forever this is not a backup you should have a backup that's off of the server snapshots can be used for example if you want to test a change you could even for example a snapshot your route file system if you have free space that's that's unallocated you can definitely do that you can even run all of your system updates after taking a snapshot and then if your updates break something you can restore it which is awesome but just keep an eye on this we don't we just don't want this to reach a hundred percent so now what I'm going to do is make a change to the system and in fact I'm going to go ahead and just remove that file that I created earlier and it's this one important file I'm going to remove an important file and just to make sure it's gone you know it's not there so I removed an important file Oh No whatever can I do so there's a couple ways we could deal with this now one option is we can actually mount the snapshot and we can mount it to a temporary directory or something like that so we can fetch the file that was deleted so I'll show you that right now but first of all I need to make a directory for where I want to mount that to so I'll just to mount extra snapshot just like that so now I'll just go ahead and mount I'll type out the path dev mapper eg extra and then web that's the snapshot right there and I'm going to mount it to the directory that I just created and then press Enter now you can see that we actually have the snapshot mounted so again if I was to LS against Mount extra web files not there but if I was to do the same thing against the snapshot directory which has the actual snapshot mounted to it we do see the file is there so I could just fetch the file from the snapshot put it where it's supposed to go and then delete the snapshot for example that's a valid way to do that but what I'm gonna do though is I'm going to unmount the snapshot here and show you guys how to recover that logical volume from the snapshot basically restore the snapshot so let's see how to do that now first of all what I'm going to do is on Mount the one that I want to actually recover so now that one's not mounted anymore and then I can use the LV convert command to go ahead and restore the snapshot and the syntax looks something like this I'm going to merge it and I'll just type out the path to the snapshot itself now if you remember the origin is set to the original logical volume so this should actually overwrite the original so I'll press ENTER and we're ready to go so what we'll do now is deactivate the logical volume and then reactivate it so we'll run LV change and we're doing this just because you want to make sure everything is flushed so again LV change and then a n or - a N and then the path to the logical volume the original one just like that I'll press ENTER it's deactivated and then I'll just change the end to a Y to reactivate it and now everything should be flushed so now what we will do is go ahead and remount it now just for comparison you can see that the web logical volume is not mounted I added it to the FS tab you can mount it manually if you like but I should just be able to do mount dash a and we can see that the logical volume LV web has returned to the system moment of truth LS dash L mount extra web and the important file is back we have restored the snapshot how awesome is that and you know what LVM gets even more advanced than this but I think in this video so far I've shown you guys everything I think you need to know to really get going with this I hope this is very helpful because I feel like LVM is just one of these things it's just this complex mysterious thing and you might not know what it's for well hopefully now you know what it's for in my case early in my career I've seen the LVM option presented while installing various distributions and I've always ignored him like I don't know that is maybe I was intimidated by it I don't know I was new and I just never gave it a shot and then one day you know I'm like you know I'm gonna actually dive into this and learn it because maybe this is something I should be using and then I just became fascinated with it and now all of my systems have LVM even my laptops and desktops because you never know maybe I want to create a snapshot and test some software and restore the snapshot or something like that on a laptop or desktop I think this has a lot of use cases now obviously you will want to play around with this on a test system before you put it in production but hopefully everything in this video I've shown you guys will get you going this is all the information that I wish I knew when I was starting out in Linux and hopefully you guys don't discover LVM as late in your career as I did because it's just one of those things that you know I just never bothered with and I wish I have learned about it earlier on so I hope this was helpful for you guys if it was please click that like button that lets YouTube know that you want to see more Linux content just like this and I will see you in another video I have some awesome stuff coming I can't wait so stay tuned and I'll see you in the next video [Music]
Info
Channel: Learn Linux TV
Views: 111,176
Rating: undefined out of 5
Keywords: LearnLinux, Linux, Tutorial, Review, Howto, Guide, Distribution, Distro, Learn Linux, os, open-source, open source, gnu/linux, logical volume manager, lvm, lvm tutorial, linux lvm, partitioning, volume, filesystem, enlarge, resize, logical volume management, physical volume, volume group, lvm linux, linux logical volume, lvm basics, logical volume manager tutorial, lvm in linux, lvm explained, linux tutorial 2020, linux lvm snapshot, linux lvm tutorial, backup, snapshot, logical volume, format
Id: MeltFN-bXrQ
Channel Id: undefined
Length: 53min 12sec (3192 seconds)
Published: Tue Jun 02 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.