LVM and replacing disks

June 15, 2011

I've had my /home, /var, and swap on LVM for a year or two now, but have never had cause to actually use the functionality LVM offers. Yesterday, I decided to replace an old disk with a newer, high capacity one, and it was the first time that LVM really played a useful role in migrating my content.

The old disk, a tired 250Gb was to be replaced with a newer 1Tb drive, which I did as follows.

First I booted into a root shell, and then used fdisk to add one partition to the 1Tb drive, and added it to the volume group:

vgextend my_volume_group /dev/<new drive>

In order to remove a physical volume from a volume group, you want to remove all the data which is on that disk, and move it to any other spare disks within the group. This is achieved with the pvmove command.

pvmove /dev/<old drive>

Once that completes (and it took quite a long time), you can then remove the old physical volume from the volume group:

vgreduce my_volume_group /dev/<old drive>

The old drive can now be removed from the computer.

Finally, I extended my home partition to take up the remaining new space available. This was done by using vgdisplay to discover the number of free physical extents available ('Free PE'), and then the extension of the logical volume.

lvextend -l+<Free PE> /dev/my_volume_group/homevol
fsck -f /dev/my_volume_group/homevol
resize2fs /dev/my_volume_group/homevol

The -l flag takes a number of physical extents, using +extentsnumber adds it to the existing number, -l without the + simply sets the physical extents to the number provided. You will need to use other utilities than resize2fs if your filesystem is not ext*.

Once I rebooted my machine, I had my partitions set up on the new disk, and /home extended to the spare space, without any problems. LVM was definitely worth it in my initial setup!