Mounting LVM partitions from the terminal on Linux
Hello there! Recently I found myself with the interesting task of mounting an LVM partition by hand. It wasn't completely straightforward and there was a bunch of guesswork involved, so I thought I'd document the process here.
For those who aren't aware, LVM stands for the Logical Volume Manager, and it's present on Linux system to make managing partitions easier. It can:
- Move and resize partitions while they are still mounted
- Span multiple disks
....but to my knowledge it doesn't have any redundancy (use Btrfs) or encryption (use LUKS) built in. It is commonly used to manage the partitions on your Linux desktop, as then you don't need to reboot it into a live Linux environment to fiddle with your partitions as much.
LVM works on a layered system. There are 3 layers to it:
- Physical Volumes: Normal physical partitions on the disk.
- Volume Groups: Groups of logical (LVM) partitions.
- Logical Volumes: LVM-managed partitions.
In summary, logical volumes are part of a volume group, which spans 1 or more physical disks.
With this in mind, first list the available physical volumes and their associated volume groups, and identify which is the one you want to mount:
VG Size in the output. Comparing it with the output of
lsblk -o NAME,RO,SIZE,RM,TYPE,MOUNTPOINT,LABEL,VENDOR,MODEL can be helpful to identify which one is which.
I encountered a situation where I had 2 with the same name - one from my host system I was working on, and another from the target disk I was trying to mount. In my situation each disk had it's own volume group assigned to it, so I needed to rename one of the volumes.
To do this, take the value of the
VG UUID field of the volume group you want to rename from the output of
sudo vgdisplay above, and then rename it like this:
sudo vgrename SOME_ID NEW_NAME
...for example, I did this:
sudo vgrename 5o1LoG-jFdv-v1Xm-m0Ca-vYmt-D5Wf-9AAFLm examplename
With that done, we can now locate the logical volume we want to mount. Do this by listing the logical volumes in the volume group you're interested in:
sudo lvdisplay vg_name
Note down the name of the logical volume you want to mount. Now we just need to figure out where it is actually located in
/dev so that we can mount it. Despite the
LV Path field appearing to show us this, it's not actually correct - at least on my system.
Instead, list the contents of
You should see the name of the logical volume that you want to mount in the form
volumegroup-logicalvolumename. Once found, you should be able to mount it like so:
sudo mount /dev/mapper/volumegroup-logicalvolumename path/to/directory
path/to/directory with the path to the (empty) directory you want to mount it to.
If you can't find it, then it is probably because you plugged the drive in question in after you booted up. In this case, it's probable that the volume group is not active. You can check this is the case or not like so:
If it isn't active, then you can activate it like this:
sudo lvchange -a y vg_name
vg_name with the name of the volume group you want to activate. Once done, you can then mount the logical volume as I mentioned above.
Once you are done, unmounting it is a case of reversing these steps. First, unmount the partition:
sudo umount path/to/mount_point
Then, disable the volume group again:
sudo lvchange -a n vg_name
Finally, flush any cached writes to disk, just in case:
Now, you can unplug the device from your machine.
That wraps up this quick tutorial. If you spot any mistakes in this, please do leave a comment below and I'll correct it.