Traditionally if one want to use a hard drive (s)he has to partition it. “Since the dawn of time” the problem was – how. Partition cannot be easily moved around (moving 40 GiB chunk of data takes a long time), and it cannot be done while FS is mounted. What is worst repartitioning the disk while any of the partition is mounted prevents Linux from rereading the partition table – which basicly means that I have to do it from Live CD. While it is inconvenient for desktop user it is impossible for servers.
However I thought I do not need it. Well – what are my needs? 10 GiB for /usr for sure is enough, /var etc. on separate partitions with appropriate size. Rest for /home. As many times before live proved me wrong. Recently I started running of the space on /usr – and have plenty of space on /home – which was not only not-optimal but started stopping my system from working.
Please note that in following text I use the term LVM meaning LVM 2. I also take no responsibility for any action resulted from reading this blog.
System with LVM
It will take a long time to move all of those partitions I thought. However I remaind myself about LVM. Why not try it?
To create a system with LVM you need a few things. Firstly you need a kernel module. If you want your / partition in LVM you need it either compiled-in or put into initrd. As we will not communicate directly with module we need userspace tools. Thirdly we need some disk space.
Let’s assume /dev/sda5 is a partition you have created. pvcreate /dev/sda5 changes it into a Physical Volume. PV is a name of a "partition" on which real data is stored (it need to be stored eventually somewhere). However the advantage of LVM is that PV is only Physical Volume – not the Logical.
Next step is to create a Volume Group. Volume Group, as the name implies, groups the volume – both physical and logical. Running vgcreate vg /dev/sda5 creates VG with name vg and the PV /dev/sda5. If you want you may create VG on more PV simply by supplying more arguments. If you’d like to add /dev/sda6 in the future vgextend vg /dev/sda6 is your friend. It is worth to point out that VG is not limited to one disk only and therefore can work in similar fashion to RAID 0.
The final step is to create a Logical Volume. LV is similar to partition is sense that you create a filesystem on it. However it does not have to be continous or even on a single disk. HoweverLVM takes care low-level details. As the LV have no specific order you can name it (please note that the label of partition is something different) – for example usr. lvcreate -L12G -nusr vg creates 12 GB logical volume named usr using VG vg.
Now you can easily format the hard drive with your favourite FS. For example ext4 – mkfs.ext4 /dev/vg/usr.
Why to do it?
Assume that the 12GB is insufficient for /usr – you need to extend it by 2 GB. Ok – what to do? Simply lvextend -L+2G /dev/vg/usr will resize the volume – you don’t have to unmount the FS. In most GNU/Linux FS, such as reiserfs, jfs, xfs or ext2/3/4, it can be done on-line without unmounting the filesystem. In the LVM HOWTO it is stated that the ext2/3/4 online resizing requires additional patch – however no such information is stated in ext4 wiki.
For more informations please read LVM HOWTO