Since I rarely need storage tools (it’s so much easier in a virtual environment), I decided to write something about it, to hopefully make it stick in my mind.
Pretty basic stuff probably, but I needed this post. I hope you do too.
For testing purposes, I’ve got a Vagrant box with 3 new, small disks of 1GB each. First I’ll be creating a volume on 2 disks, which I will expand with the third disk later on. This should cover all concepts and commands.
You have to take note of a couple of elements in this setup. From bottom to top:
- You’ve got your physical (or virtual) hard disks
- You’ll create partitions on them with fdisk
- With pvcreate you’ll create your LVM ‘physical volumes’
- Next you’ll need a LVM volume group, which you create with vgcreate
- On top of that you have your LVM logical volume created with lvcreate
- Finally you can create your usable filesystem with mkfs
Let’s get going. Check all available disks:
For me this shows me that my 3 new, empty disks, are sdc, sdd and sde. First, we’ll be formatting this with a GPT layout and a LVM capable partition. For the two first disks (we’re leaving the third disk out on purpose):
# fdisk /dev/sdc # fdisk /dev/sdd
With ‘p’ we can find out that it has currently a DOS layout and no partitions.
Disk /dev/sdc: 1 GiB, 1073741824 bytes, 2097152 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: dos Disk identifier: 0x85b10061
Simply make it GPT by pushing ‘g’. Create a new partition with ‘n’. If you want to use the whole disk, you can keep all the defaults presented here. Next push ‘t’ to change the type, which you should set to Linux LVM. Check which number to put in with ‘L’, it varies depending on your fdisk version.
Finally don’t forget the ‘w’ to write your changes.
Changed type of partition 'Linux filesystem' to 'Linux LVM'. Command (m for help): w The partition table has been altered. Calling ioctl() to re-read partition table. Syncing disks.
When you’ve done this for the 2 first disks, you can check your partitions with:
Now we can create LVM physical volumes which we do not have yet:
Create them (notice that we’re using the partitions rather than the disks) and run the command again:
# pvcreate /dev/sdc1 /dev/sdd1
Next is the volume group which we need to create. Check the (most probably absent) current ones with:
Create the volume group and check again:
# vgcreate vgsmall /dev/sdc1 /dev/sdd1
Up to the logical volume. Check:
Creating it is a bit different than the previous commands:
# lvcreate --name lvsmall --size 2040m vgsmall
And your checks again:
This last one is especially useful, because it shows us some important information like the LV path and size. In any case, we have to create a filesystem (check the correct path with lvdisplay):
# mkfs.ext4 /dev/vgsmall/lvsmall
And check your new filesystem:
# blkid # df -h
Finally we can mount and use it:
# mkdir -p /mnt/smallstorage
# mount /dev/mapper/vgsmall-lvsmall /mnt/smallstorage/
We can check the mount with the last command. If you would like to automount at boot, edit the /etc/fstab and add the UUID that you got with the ‘blkid’ command:
UUID=55c9d0c4-d6a9-4417-9b8b-f07d8bb5de31 /mnt/smallstorage ext4 defaults 0 2
That’s it. Now let’s jump to expanding an existing volume. Remember the third partition disk? We’ll be prepping it to expand our existing disk. In a few faster steps:
# lsblk # fdisk /dev/sde1
With fdisk create a GPT layout and a LVM capable partition, like we did before. Then continue:
# pvcreate /dev/sde1 # vgextend vgsmall /dev/sde1
This made the sde1 partition available for LVM and extended the volume group. Now we need to extend the LVM logical volume and filesystem itself:
# lvextend --size +1020m /dev/vgsmall/lvsmall # resize2fs /dev/vgsmall/lvsmall
Check, check, doublecheck and you should have a resized volume group, logical volume and filesystem:
# pvs # vgs # lvs # lvdisplay # mount # df -f