Create and extend an LVM Logical Volume in Ubuntu 12.04

This is one of the most useful things I've ever learned. Physical space in a server environment will eventually finish. In that case, there are three solutions that comes in my mind(with some variants):
Each one has advantages and disadvantages, and each one in some cases is impossible to apply. 

For I don't know what's going to happen to that machine in let's say 3 years, every time I have to install a new machine, I ALWAYS set up LVM to manage the disk. Why? Because if I ever have for any reason to extend disk space, I want to be able to do so. If I don't have LVM configured and I need more space, I have to mount a new disk in a new mountpoint, or to follow a rather complicated procedure to be able to put everything I want in LVM without losing data (like this: SettingUpLVM-WithoutACleanInstall - note the "If you want to put everything under LVM, you should create a separate /boot partition (100MB should be fine) - otherwise you may not be able to boot your system." and the "You're going to need some free space to setup your volume groups. " which is not always possible).

Maybe another time I'm going to study how can I do this (given that I will have to do this procedure ;) I'm still learning!). 

But first: let's say what's LVM. LVM (Logical Volume Management), like many other things, is a level of abstraction between the physical disks and the operating system. As always I try to simplify a little. 
The concept is: you cannot extend a physical disk (even thought you can replace with a bigger one ;) ). Once it's mounted, you can add more disks, but each one will have a static size. But with LVM you can both extend AND reduce an existing volume. Note the difference: a volume is not a disk. volume is a more abstract concept: it's, forgive me, a bunch of space. 

LVM manages space using three layers:
What you format and mount is the logical volume, which the OS sees, to simplify, as a physical volume.

Remember: in a new installation of Ubuntu 12.04, you can choose to use the whole starting disk with LVM. Here a guide: Setting up LVM in Ubuntu . 

Ok let's start with what I use to create a new LVM. 

All commands are intended with sudo.

First, attach the disk in any way. In my situation, I was using VMWare and attached a SCSI disk.
Then, you may restart Ubuntu. Otherwise, if you don't want (or cannot) restart and you have SCSI disks, use the following steps:
apt-get install scsitools
scsitools is a group of tools to manage SCSI drives.
cat /proc/partitions
Note down all the devices.
rescan-scsi-bus
rescan-scsi-bus checks for all the SCSI drives attached and distinguish the old ones from the new ones, then configures Ubuntu to enable working with the newly attached drives.

UPDATE: if your server is a VMWare virtual machine and you want to extend a disk instead of add a new one, you must run a command like this (probably you have to change the 2:0:0:0 part with your real disk ID) :
echo '1' > /sys/class/scsi_disk/2\:0\:0\:0/device/rescanAfter this, running fdisk -l you should be able to see your new disk size.

cat /proc/partitions
Note the new devices. It's gonna be something like /dev/sda or /dev/sdb or /dev/sdc or some other letter in alphabetic order (for a SCSI device).
fdisk /dev/sda
With this command you can partition your disk to be a LVM partition. fdisk opens an internal menu. Note that it doesn't execute anything until you launch the command w (write).These following are the instruction I use:
 n
 p
 <enter>
 <enter>
 t
 8e
 p
 w
If you have more than one disk, repeat the same procedure changing the disk number.

After this starting phase, let's start with the real LVM configuration.
pvcreate /dev/sda1
This command initialize the physical partition to be initialized by LVM
vgcreate vgpool /dev/sda1 
This command creates a volume group with the name vgpool formed by any number of volumes. If you have more than one physical volume, you can put multiple devices path after the volume group name. For example:
vgcreate vgpool /dev/sda1 /dev/sdb1 /dev/sdc1
Then if you want like me to create a single logical volume you have to find out the total size (in GB in these days) of the volume group.
vgdisplay
The output of this command is the configuration of all the volume groups, included size and UUID of the group. Take note of the total size in GB of the volume.

With this size, you can finally create the logical volume:
lvcreate -L 250G -n lvhalen vgpool
Now you have a logical partition of 250G named lvhalen, generally wthin the folder /dev/vgpool/lvhalen.

To use this volume, you need some additional steps. First, you need to format the volume to use a specific filesystem:

mkfs -t ext4 /dev/vgpool/lvhalen
t sets the filesystem type. In this case I used ext4.

Finally, mount the volume to be usable.
mkdir /home/vanhalenhome
mount -t ext4 /dev/vgpool/lvhalen /home/vanhalenhome
Note that you need to create the directory before mounting the filesystem.

If you want to mount the volume every time at startup, you can instead edit /etc/fstab.

Take note of the UUID number of the volume:
partprobe
blkid /dev/sda1
The output of blkid it's the wanted UUID. Then open /etc/fstab.

vi /etc/fstab
And insert the following line:
UUID=XXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXXX /home/vanhalenhome ext4 errors=remount-ro 0 1
I admit that I don't understand everithing of the commands on this line, but it should mount at startup the volume with that UUID in the given directory (if both exists, obviously).

EXTEND VOLUME
If you want to extend a volume, you should follow all the above passages until pvcreate. 
In this way, you are gonna addThen, you need to do something different. You need to ADD the new disk (or, to be more precise, the new physical volume) to the volume group. This is the command:
vgextend vgpool /dev/sdd1
Now, your volume group has 4 physical volumes and it's greater in size. Then you can extend the logical volume (in this case I added a 50G hard drive):
lvextend -L+50G /dev/vgpool/lvhalen
Almost done! But there's a last step to take to be able to use the added space. You need to let the file system know that he can use more space. Note: the command to extend the filesystem is custom for every filesystem, DO NOT EXCHANGE the commands!
In this case I use the ext3/ext4 filesystem command:
resize2fs /dev/vgpool/lvhalen
Et voilĂ ! Now your friend Eddie Van Halen has 300Gb of home directory!

SHRINK VOLUME
First of all, BACKUP every file in the logical volume, for in this process you may lose files.
Then, you just have to use the above steps in the reverse order, use - instead of + in lvextend (or simply use lvreduce) and use vgreduce.



REFERENCES:
LVM Management - a very good tutorial about managing LVM
Expand LVM in a live environment

Labels: ,