Extending a Logical Volume in Linux

logical volume extend

You might find yourself needing to extend a logical volume for whatever reason. It is important to know what LVM is and how it works before proceeding. If you are not careful, you can corrupt your disks and potentially render your server unusable. Before we start, we must know what LVM is and understand the concept of LVM. To understand this, I suggest you read the following link as it does an extremely good job of explaining LVM:

  • https://www.centos.org/docs/5/html/5.1/Deployment_Guide/s1-lvm-intro-whatis.html
  • If you simply want to know how to add space to a volume group and extend a logical volume, you may skip this section as the portion about adding a disk in vSphere will not be relevant.

    Adding a disk in vSphere

    Do add a disk in vSphere – you must first log into your vSphere client and navigate to the server in which you are going to be adding space too. From the server dashboard you need to click “Settings”, and then navigate to “Edit Settings…”

    vsphere-settings

    Once you have the settings window opened, you need to click on the “New Hard Disk” option. This allows us to add a new disk to the system:
    vsphere-add-disk-device

    Once you have added the new disk, a new field will populate the settings window, and at the bottom you will see the new disk you have added:
    vsphere-new-hard-disk

    You must enter the amount of space that you want to add to the server and click the “ok” button. The settings window will disappear a you should now see your disk added to the server.

    Adding the disk to the Logical Volume Group

    Now that the disk has been added in our hypervisor, we need to pull this disk into LVM. In order to do that we need to get the device path for the disk we just added.

    Issue an fdisk -l to see if your server was able to find the newly added disk.

    fdisk -l

    fdisk-new-device

    Sometimes the machine will not automatically pick up the device and it is necessary to manually rescan the SCSI bus. If the device is not automatically showing up for you, please refer to the troubleshooting section at the end of this post.

    From the output we can see that the disk path is /dev/sdb we need to now “extend” this disk to our volume group so that it is able to see the empty disk. To do this we must use the “vgextend” command. To do this we need to know what volume group we want to add it to, and the path of the disk we are adding. The command will be as follows:

    vgextend VolumeGroupName /path/to/disk

    Where VolumeGroupName is the volume group in which you want to add the disk space to, and /path/to/disk is the path you discovered by running the fdisk command.

    logical volume vgdisplay

    You now can check the status of your volume group to see if the space has been added. It should show up under the “VG Size” when issuing a vgdisplay – You may also issue a “pvscan” which will show you all of the disks and which volume group they are assigned to.

    vgdisplay
    pvscan
    

    Now that we can confirm the disk has been added to the volume group and that we have free space to play with, we can grow the logical volume that needs space. To do this we need to run the lvextend -L+5G /path/to/lv command where the -L option is the size, +# is the number in which you want to increase the logical volume and /path/to/lv is the path of the logical volume you want to increase in size.

    [[email protected] cstanley]# lvextend -L+5G /dev/mapper/rootvg-var_lv
      Size of logical volume rootvg/var_lv changed from 3.00 GiB (96 exten
      Logical volume var_lv successfully resized
    

    Now that we have resized the logical volume we need to resize the file system. To do this we must run resize2fs /path/to/logical/volume

    [[email protected] cstanley]# resize2fs /dev/mapper/rootvg-var_lv
    resize2fs 1.41.12 (17-May-2010)
    Filesystem at /dev/mapper/rootvg-var_lv is mounted on /var; on-line resizing required
    old desc_blocks = 1, new_desc_blocks = 1
    Performing an on-line resize of /dev/mapper/rootvg-var_lv to 2097152 (4k) blocks.
    The filesystem on /dev/mapper/rootvg-var_lv is now 2097152 blocks long.
    

    It is important to remember that even though we have grown the logical volume, we have not yet extended the file system to take up the rest of the space in the logical volume.

    After this is done, you should issue a df -h to make sure that the logical volume that you have extended has been increased in size.

    [[email protected] cstanley]# df -h
    Filesystem            Size  Used Avail Use% Mounted on
    /dev/mapper/rootvg-var_lv
                          7.9G  2.7G  4.8G  36% /var
    

    Commands Issued In-Order

    df -h
    fdisk -l
    vgextend VolumeGroupName /path/to/disk
    vgdisplay
    pvscan
    lvextend -L+SIZE#G /path/to/lv
    resize2fs /path/to/logical/volume
    df -h
    

    Troubleshooting

    If you are having trouble getting the machine to find the newly added disk try manually scanning for the disk. To do this we need to force the SCSI bus to rescan for disks.
    To find out our SCSI host bus number we need to grep for mpt inside the proc_name under our SCSI hosts

    grep mpt /sys/class/scsi_host/host?/proc_name
    

    This should return a result similiar to

    [[email protected] cstanley]# grep mptspi /sys/class/scsi_host/host?/proc_name
    /sys/class/scsi_host/host2/proc_name:mptspi
    

    where host2 is the relevant field.

    echo "- - -" > /sys/class/scsi_host/host2/scan
    

    In the above command the the hyphens represent controller, channel, lun, so the dashes indicate that all of the controllers, channels and luns should be scanned.

    Resizing an Existing Disk

    In the event that you have somehow increased the size of a disk that is already present in the system you can force the system to reload all of the disks information including the sizes.

    echo 1 > /sys/class/scsi_device/DEVICE-ID/rescan
    

    Leave a Comment

    Your email address will not be published. Required fields are marked *