Partitioning and mounting a new disk using LVM
As I've been doing my PhD, I've been acquiring quite a lot of data that needs storing. To this end, I have acquired a new 2 TiB hard drive in my Lab PC. Naturally, this necessitates formatting it so that I can use it. Since I've been using LVM (Logical Volume Management) for my OS disk - so I decided to use it for my new disk too.
Unfortunately, I don't currently have GUI access to my Lab PC - instead for the past few months I've been limited to SSH access (which is still much better than nothing at all), so I can't really use any GUI tool to do this for me.
This provided me with a perfect opportunity to get into LVM through the terminal instead. As it turns out, it's not actually that bad. In this post, I'm going to take you through the process of formatting a fresh disk: from creating a partition table to mounting the LVM logical volume.
Let's start by partitioning the disk. For this, we'll use the fdisk
CLI tool (install it on Debian-based systems with sudo apt install fdisk
if it's not available already). It should be obvious, but for this tutorial root access is required to execute pretty much all the commands we'll be using.
Start fdisk
like so:
sudo fdisk /dev/sdX
Replace X
with the index of your disk (try lsblk
- no sudo
required - to disk your disks). fdisk
works a bit like a shell. You enter letters (or short sequences) followed by hitting enter to give it commands. Enter the following sequence of commands:
g
: Create new GPT partition tablen
: Create new partition (allow it to fill the disk)t
: Change partition typeL
: List all known partition types31
: Change to aLinux LVM
partition
p
: Preview final partition setupw
: Write changes to disk and exit
Some commands need additional information - fdisk
will prompt you here.
With our disk partitioned, we now need to get LVM organised. LVM has 3 different key concepts:
- Physical Volumes: The physical disk partitions it should use as a storage area (e.g. we created an appropriate partition type above)
- Volume Groups: Groups of 1 or more Physical Volumes
- Logical Volumes: The volumes that you use, format, and mount - they are stored in Volume Groups.
To go with these, there are also 3 different classes of commands that LVM exposes: pv*
commands for Physical Volumes, vg*
for Volume Groups, and lv*
for Logical Volumes.
With respect to Physical Volumes, these are physical partitions on disk. For legacy MSDOS partition tables, these must have a partition type of 8e
. For newer GPT partition tables (such as the one we created above), these need the partition id 31
(Linux LVM
) - as described above.
Next, we create a new volume group that holds our physical volume:
sudo vgcreate vg-pool-name /dev/sdXY
....replacing /dev/sdXY
with the partition you want to add (again, lsblk
is helpful here). Don't forget to change the name of the Volume Group to something more descriptive than vg-pool-name
- though keeping the vg
prefix is recommended.
If you like, you can display the current Volume Groups with the following command:
sudo vgdisplay
Then, create a new logical volume that uses all of the space in the new volume group:
sudo lvcreate -l 100%FREE -n lv-rocket-blueprints vg-pool-name
Again, replace vg-pool-name
with the name of your Volume Group, and lv-rocket-blueprints
with the desired name of your new logical volume. tldr (for which I review pull requests) has a nice page on lvcreate
. If you have a tldr client installed, simply do this to see it:
tldr lvcreate
With our logical volume created, we can now format it. I'm going to format it as ext4, but you can format it as anything you like.
sudo mkfs.ext4 /dev/vg-pool-name/lv-rocket-blueprints
As before, replace vg-pool-name
and lv-rocket-blueprints
with your Volume Group and Logical Volume names respectively.
Finally, mount the newly formatted partition:
sudo mkdir /mnt/rocket-blueprints
sudo mount /dev/vg-extra-data/lv-rocket-blueprints /mnt/rocket-blueprints
You can mount it anywhere - though I'd recommend mounting it to somewhere in /mnt
.
Auto-mounting LVM logical volumes
A common thing many (myself included) want to do is automatically mount an LVM partition on boot. This is fairly trivial to do with /etc/fstab
.
To start, find the logical volume's id with the following command:
sudo blkid
It should be present as UUID="THE_UUID_HERE"
. Pick out only the UUID of the logical volume you want to automount here. As a side note, using the UUID is generally a better idea than the name, because the name of the partition (whether it's an LVM partition or a physical /dev/sdXY
partition) might change, while the UUID always stays the same.
Before continuing, ensure that the partition is unmounted:
sudo umount /mnt/rocket-blueprints
Now, edit /etc/fstab
(e.g. with sudo nano /etc/fstab
), and append something like this following to the bottom of the file:
UUID=THE_UUID_YOU_FOUND_HERE /mnt/rocket-blueprints ext4 defaults,noauto 0 2
Replace THE_UUID_YOU_FOUND_HERE
with the UUID you located with blkid
, and /mnt/rocket-blueprints
with the path to where you want to mount it to. If an empty directory doesn't already exist at the target mount point, don't forget to create it (e.g. with sudo mkdir /mnt/rocket-blueprints
).
Save and close /etc/fstab
, and then try mounting the partition using the /etc/fstab
definition:
sudo mount /mnt/rocket-blueprints
If it works, edit /etc/fstab
again and replace noauto
with auto
to automatically mount it on boot.
That's everything you need to know to get up and running with LVM. I've included my sources below - in particular check out the howtogeek.com tutorial, as it's not only very detailed, but it also has a cheat sheet containing most of the different LVM commands that are available.
Found this useful? Still having issues? Got a suggestion? Comment below!