Why extend a disk?
What is LVM Partition
Check Existing Space
Extend Partition using the Script
Extend Partition Manually

Why extend a disk?

Many of our images (primarily on openstack) do not grow their partition/file system size automatically if you choose a flavor (openstack resource definition) that allocates more disk space to your instance than was available in the original image. It is possible to use tools such as cloud-init to do this automatically. However, in our public images, which we use on multiple environments (openstack, virtualbox, vmware, etc), we find cloud-init can sometimes cause issues. Instead, we have scripts designed to resize the partitions for the user.

What is LVM Partition

Historically, many of our standard images for Openstack and VirtualBox virtual machines use LVM (Linux Logical Volume Manager) to provide the disk volume for the operating system, as well as additional storage volumes (if applicable). LVM makes it easy to resize and re-purpose space within Linux, usually without even requiring a reboot.

You can learn more about LVM here: LVM – UbuntuLVM is available for most Linux distros.

Check Existing Space

Before you can enlarge the disk, first you have to check to see if it is possible:

Check physical disk size versus the partition or LVM size (using fdisk):

  • Run fdisk, to see if there is more space available that could be added to the LVM:
    sudo fdisk -l
  • In most of our basic images, this should show just a single physical device, such as:
    Disk /dev/vda: 32 GiB, 34359738368 bytes, 67108864 sectors32 GB physical disk
  • It will also show the main partition, typically the LAST partition (which could be used by LVM):
    Regular partition example:
    Device Start End Sectors Size Type
    /dev/vda1 2048 4095 2048 1M BIOS boot
    /dev/vda2 4096 16777182 16773087 8G Linux filesystem

    In this case it is an 8 GB partition (/dev/vda2)
    LVM Example:
    Device Boot Start End Sectors Size Id Type
    /dev/vda1 2048 16777215 16775168 8G 8e Linux LVM

    In this case it is an 8 GB partition (/dev/vda1)
  • And finally it will also show the LVM itself if LVM is being used:
    Disk /dev/mapper/COMPbase--vg-root: 8 GiB, 8585740288 bytes, 16769024 sectors

So, in this case, the Physical disk is 32 GB, but only 8 GB are currently allocated to the LVM, and no other partitions are using the rest of the space for anything else. So we can extend it!

Extend Partition using the Script

If you are using one of our standard SCS images, either for OpenStack or for VirtualBox virtual machines, it usually comes with a small script called extend-disk.sh. The script is found in the home directory of the student user under the extend-disk folder. NOTE:Older images may have the script called extend-lvm.sh, found in the extend-lvm folder in the student home directory

To run the script:

  • Open a terminal
  • Go to the directory with the script, usually: cd ~student/extend-disk/
  • Enter: sudo ./extend-disk.sh /dev/vdaNOTE: the /dev/vda may be different on your system. It is the device you found in Check Existing Space above

That’s it! Now when you run fdisk, you should see the last partition is the full size of the physical disk: sudo fdisk -l.

To update or download the script:

  • If you do not have the script, you can get it using git:
    git clone https://git.scs.carleton.ca/git/extend-disk.git
  • If you have the script and it was pulled from the git repository, then you can pull updates:
    cd extend-disk go into the extend-disk git folder
    git pull to pull the updates
    NOTE: If you are unsure if it was pulled from the git repository, then check if there is a .git hidden folder in the extend-disk folder (check for hidden folders with ls -la); If not, then you need to clone the repository using the git clone command above.

Extend Partition Manually

Here are the steps to manually extend the partition and the filesystem, including for the LVM case.

NOTE: We assume the same setup as the above Check Existing Space

  • Extend the physical drive partition:
    • sudo fdisk /dev/vdaEnter the fdisk tool to modify /dev/vda
      NOTE: End each one letter command by pressing [Enter]; if the instructions do not specify a specific response to a question, just press [Enter] to accept the default
    • pp command prints the disk info, same as running fdisk -l /dev/vda
    • dd command delete the last partition (usually /dev/vda1 or /dev/vda2 on our images)
    • n e n command creates a new partition; e makes that an extended partition
    • Optional: tt changes the type of partition. NOTE: This should not be required as fdisk should use the same type as was removed. You can also enter L when prompted to find the list of codes for the types. Make sure to use the same type as was listed by fdisk for the partition you removed in the earlier d step.
    • ww writes the changes to disk and exits fdisk
  • IF using LVM: Modify (extend) the LVM:
    • Tell LVM the physical partition size has changed: sudo pvresize /dev/vda1
    • Find the actual path of the LVM logical volume: sudo lvdisplayThe LV Path is the value needed
    • Tell LVM to extend the logical volume to use all of the new partition size: sudo lvextend -l +100%FREE /dev/COMPbase-vg/root Using the LV Path from above
  • Resize the file system:
    sudo resize2fs /dev/vda2 or for lvm: sudo resize2fs /dev/COMPbase-vg/root

That’s it! Now, when you run sudo fdisk -l, it will show that the LVM is using all of the space on the physical disk!