Friday, 8 March 2013

RHCSA: Logical Volume Management

You can use a collection of disks on a system, group them together into a volume group and then carve this volume group up into logical volumes of varying sizes, not necessarily the same size as the original disks. You can also use the physical disks, convert to a volume group and from this make a single, logical, volume.


On RHEL systems logical volumes can be managed using the CLI or using system-config-lvm. System -config-lvm isn’t installed by default however so it will need to be added from the repo if you wish to use it. Real men use the command line though as we all know, so let’s press on and ignore the GUI.





Rogical Volume Management Steps


The first step is to convert your disk partitions into Linux LVM partitions. Check your disks using:

#fdisk -l

This will list your disks. For this example we will assume we have two 250GB disks at /dev/sdd and /dev/sde. Convert the partitions using:

#fdisk /dev/sdd

Press ‘l’ to list the partition types available. We need to change to Linux LVM, which is listed as type 8e. Press ‘t’ to change the partition type and enter ‘8e’. Press ‘w’ to write changes. Then do the same thing for /dev/sde.


The second step is to convert the LVM disks into “physical volumes”. We do this using the pvcreate command:

#pvcreate /dev/sdd
#pvcreate /dev/sde

We can view our physical volumes with the pvs command or with more detail using pvdisplay.


The third step is to create logical volumes by combining our physical disks. We do this using vgcreate.

#vgcreate vg_new /dev/sdd /dev/sde

You should get a message saying the volume group has been created. You can view volume groups using vgs or vgdisplay.

You will now need to create a logical volume using lvcreate (notice a pattern here?)

#lvcreate -L 400G -n lv_new vg_new
Essentially, this creates a 400GB logical volume named lv_new on the vg_new volume group.

You can check your logical volumes using lvs or lvdisplay (more patterns!). You get far more information with lvdisplay.


Following this you will need to put a filesystem on the volume. You can do this with:

#mkfs.ext4 /dev/vg_new/lv_new

Following this you will need to create a mount point for you new filesystem.

#mkdir /mnt/new_disk

Add an entry to /etc/fstab to ensure that it is brought up at boot time:

#vi /etc/fstab

Append something relevent, eg:

/dev/vg_new/lv_new /mnt/new_disk ext4 defaults 0 0

:wq!

Then mount the new volume:

#mount -a

Test the new volume is operating as required by creating a test volume.


Resizing Logical Volumes

We can resize a logical volume without having to unmount it first using the lvresize command:

#lvresize -L 500G /dev/vg_new/lv_new

Check the logical volume has grown with the lvs command.

To shrink the logical volume, first of all make sure that we have a valid backup of the data because any data that is on the area that is removed will be lost. Assuming we have a backup, we unmount the volume:

#umount /mnt/new_disk

Now we run the file system checker on the logical volume:

#fsck -f /dev/vg_new/lv_new

Next we need to shrink the filesystem with the resize2fs command:

#resize2fs /dev/vg_new/lv_new 400G

Once we have done this, we can use lvresize again to shrink the logical volume:

#lvresize -L 400G /dev/vg_new/lv_new

We get a warning at this point indicating the high risk of data loss, so really make sure you have a good backup. Use lvs once again to verify the new size of the logical volume.

Finally, we can remount the logical volume:

#mount -t ext4 /dev/vg_new/lv_new /mnt/new_disk

We can use the df -h command to see the amount of disk space we have in human readable format.

No comments:

Post a Comment