A Quick Reference to Adding New Storage with LVM24 September 2015 · Filed in Education
This post walks through the process of adding storage capacity to a Linux server using LVM. There’s nothing new, revolutionary, or cutting-edge about this post—honestly, it’s really more for my own reference than anything else. Adding logical volumes is something that I do so infrequently that it’s hard to remember all the commands, so I’m recording them here for when I need them next time.
First, list the physical disks in the system (all commands should be prefaced with
sudo or run as a user with the appropriate permissions):
This will help you identify which (new) disk needs to be added. In my examples, I’ll use
Start partitioning the new disk (replace
/dev/sdb with the appropriate values for your system):
I’m assuming that this isn’t a boot drive and that whatever logical volumes you create will take up the entire disk. Once you get into
fdisk, follow these steps:
nto create a new partition.
pto make this a primary partition.
1to make this the first partition on the disk.
- Press enter twice to accept default start and end cylinders (unless you know you need to change them).
tto change the partition type.
8efor the “Linux LVM” partition type.
pto view the partition info so you can verify it is correct.
- Assuming the information is correct, enter
wto write the changes to the disk and exit
Now, create a new physical volume (replace
/dev/sdb1 with the correct value for your system):
These steps assume you need to create volume group to hold the new physical volume (use
vgextend instead if you’re adding a physical volume to an existing volume group). This example assumes a volume group name of “hdd_vg”:
vgcreate hdd_vg /dev/sdb1
Now create a logical volume in the new volume group. This command creates a 200GB logical volume named “vmstore” in the volume group “hdd_vg”:
lvcreate -L 200G -n vmstore hdd_vg
Before you can use this new logical volume, you’ll need to format it (replace
ext4 with your desired file system and replace
/hdd_vg/vmstore with the correct volume group and logical volume names):
mkfs -t ext4 /dev/hdd_vg/vmstore
blkid command to obtain the volume’s UUID for use in
Make a mount point where this new logical volume will be mounted:
mkdir -p /mnt/vmstore
/etc/fstab to specify the UUID of the new logical volume, the mount point, the file system, and other necessary information. Once you’re done, mount the new storage with
mount -a. All done!
UPDATE: Reader Rutger van Esch pointed out that it’s not necessary to create the partition before creating the physical volume; you can actually create the physical volume directly on the block device using
pvcreate /dev/sdb. This makes it easier to do online resizing later down the road. Thanks Rutger!