This guide shows how to work with LVM (Logical Volume Management) on Linux. This guide was originally written by Falko Timme <ft [at] falkotimme [dot] com> (here), but I’ve re-organized and adapted it to be used as more of a reference for those looking for something a little more abstract.
LVM is a logical volume manager for the Linux kernel. It was originally written in 1998 by Heinz Mauelshagen, who based its design on that of the LVM in HP-UX.
I adapted this guide for use on Ubuntu 7.04, so I can’t guarantee it will work exactly as advertised on anything else. With that in mind, if you install all the correct packages, it should work on any distribution.
I know there are formatting problems – but I’ll get to them. It took me nearly 4 months to actually get around to completing the guide, but I hope to fix up the formatting sometime in the near future.
1. Installing necessary tools
sudo apt-get install lvm2 dmsetup mdadm reiserfsprogs xfsprogs
2. Raid Setup
Please refer to some of my other guides regarding setting up various raid levels. You can, and I would highly recommend, setting up your drives in a raid configruation prior to adding them to a logical volume. By doing so, you’ll have properly redundant drives to keep all of your precious data safe.
3. LVM Layout
Very basically an LVM consists of multiple drives, which make up a “Volume Group”. This Volume Group, can then be divided up into Logical Volumes, and expanded / contracted at will. Any Logical Volume can be larger than the physical size of any one disk, but the total size of all Logical Volumes, obviously, cannot exceed the total space your disks afford you.
4. LVM Setup
Lets start by determining what disks are attached to our system:
$ sudo fdisk -l
Disk /dev/hdb: 60.0 GB, 60040544256 bytes 255 heads, 63 sectors/track, 7299 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Device Boot Start End Blocks Id System /dev/hdb1 * 1 6996 56195338+ 83 Linux /dev/hdb2 6997 7299 2433847+ 5 Extended /dev/hdb5 6997 7299 2433816 82 Linux swap / SolarisDisk /dev/sda: 160.0 GB, 160041885696 bytes 255 heads, 63 sectors/track, 19457 cylinders Units = cylinders of 16065 * 512 = 8225280 bytesDisk /dev/sda: 160.0 GB, 160041885696 bytes 255 heads, 63 sectors/track, 19457 cylinders Units = cylinders of 16065 * 512 = 8225280 bytesDisk /dev/sda doesn't contain a valid partition tableDisk /dev/sdb: 160.0 GB, 160041885696 bytes 255 heads, 63 sectors/track, 19457 cylinders Units = cylinders of 16065 * 512 = 8225280 bytesDisk /dev/sdb doesn't contain a valid partition tableDisk /dev/sdc: 500.1 GB, 500107862016 bytes 255 heads, 63 sectors/track, 60801 cylinders Units = cylinders of 16065 * 512 = 8225280 bytesDisk /dev/sdc doesn't contain a valid partition tableDisk /dev/sdd: 500.1 GB, 500107862016 bytes 255 heads, 63 sectors/track, 60801 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes
Disk /dev/sdd doesn’t contain a valid partition table
As you can see, I have 5 disks in the system, one of which is a 60 GB boot drive, and the other four are 2x 160GB drives and 2x 500GB drives.
First thing you’ll notice is that the 4 drives I want to add to my lvm: /dev/sda – /dev/sdd do not contain partition tables. So, the first thing we’ll do is partition the drives to their maximum capacity:
$ sudo fdisk /dev/sda
The number of cylinders for this disk is set to 19457. There is nothing wrong with that, but this is larger than 1024, and could in certain setups cause problems with: 1) software that runs at boot time (e.g., old versions of LILO) 2) booting and partitioning software from other OSs (e.g., DOS FDISK, OS/2 FDISK)Command (m for help): m Command action a toggle a bootable flag b edit bsd disklabel c toggle the dos compatibility flag d delete a partition l list known partition types m print this menu n add a new partition o create a new empty DOS partition table p print the partition table q quit without saving changes s create a new empty Sun disklabel t change a partition's system id u change display/entry units v verify the partition table w write table to disk and exit x extra functionality (experts only)Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 1 First cylinder (1-19457, default 1): enter Using default value 1 Last cylinder or +size or +sizeM or +sizeK (1-19457, default 19457): enter Command (m for help): t Selected partition 1Hex code (type L to list codes): 8e Changed system type of partition 1 to 8e (Linux LVM) Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
Now repeat the process for the other 3 disks:
$ sudo fdisk /dev/sdb $ sudo fdisk /dev/sdc $ sudo fdisk /dev/sdd
Now you’ll want to prepared the partitions for use in out Volume Group:
$ sudo pvcreate /dev/sda1 /dev/sdb1 /dev/sdc1 /dev/sdd1 Physical volume "/dev/sda1" successfully created Physical volume "/dev/sdb1" successfully created Physical volume "/dev/sdc1" successfully created Physical volume "/dev/sdd1" successfully created
$ sudo pvdisplay
$ sudo pvdisplay --- NEW Physical volume --- PV Name /dev/sda1 VG Name PV Size 149.05 GB Allocatable NO PE Size (KByte) 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID ** --- NEW Physical volume --- PV Name /dev/sdb1 VG Name PV Size 149.05 GB Allocatable NO PE Size (KByte) 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID ** --- NEW Physical volume --- PV Name /dev/sdc1 VG Name PV Size 465.76 Allocatable NO PE Size (KByte) 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID ** --- NEW Physical volume --- PV Name /dev/sdd1 VG Name PV Size 465.76 Allocatable NO PE Size (KByte) 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID **
You can now see that our physical volumes are all set up. I removed the PV UUID since they will be different for any and all systems.
Now its time to create our volume group:
$ sudo vgcreate fileserver /dev/sda1 /dev/sdb1 /dev/sdc1 /dev/sdd1
Now you should be able to list the contents of your volume group:
--- Volume group --- VG Name fileserver System ID Format lvm2 Metadata Areas 4 Metadata Sequence No 1 VG Access read/write VG Status resizable MAX LV 0 Cur LV 0 Open LV 0 Max PV 0 Cur PV 4 Act PV 4 VG Size 1229.62 GB PE Size 4.00 MB Total PE 314780 Alloc PE / Size 0 / 0 Free PE / Size 314780 / 1229.62 GB VG UUID
Finally we’ll create some logical volumes on the volume group:
$ sudo lvcreate --name share --size 100G fileserver
$ sudo lvcreate --name backup --size 400G fileserver
$ sudo lvcreate --name filestore --size 400G fileserver
You’ll notice that I didn’t use up the full amount of space on the logical volume. I did this for two reasons, first, its always good to leave yourself some room to grow. Second, I find LVMs tend to get really slow once they’re getting filled up.
Now, lets throw a filesystem on our logical volumes.
$ sudo mkfs.reiserfs /dev/fileserver/share
$ sudo mkfs.reiserfs /dev/fileserver/backup
$ sudo mkfs.reiserfs /dev/fileserver/filestore
Finally, we’re ready to mount those volumes. Obviously you’ll need somewhere to mount them to:
$ sudo mkdir /var/share /var/backup /var/filestore
And now we mount them:
$ sudo mount /dev/fileserver/share /var/share $ sudo mount /dev/fileserver/backup /var/backup $ sudo mount /dev/fileserver/media /var/media
And thats it…