What is LVM Partition
Most 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 – Ubuntu – LVM 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 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 sectors– 32 GB physical disk - It will also show the partition being used for the LVM:
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:
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 LVM 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-lvm.sh. The script is found in the home directory of the student user under the extend-lvm folder. Older images may have it just in the student home directory itself rather than in a sub-folder.
To run the script:
- Open a terminal
- Go to the directory with the script, usually:
cd ~student/extend-lvm/ - Enter:
sudo ./extend-lvm.sh /dev/vda– NOTE: 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 LVM 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-lvm.git - If you have the script and it was pulled from the git repository, then you can pull updates:
cd extend-lvmgo into the extend-lvm git folder
git pullto pull the updates
NOTE: If you are unsure if it was pulled from the git repository, then check if there is a.githidden folder in the extend-lvm folder (check for hidden folders withls -la); If not, then you need to clone the repository using thegit clonecommand above.
Extend LVM manually
Here are the steps to manually extend the LVM.
NOTE: We assume the same setup as the above Check Existing Space
-
- Extend the physical drive partition:
-
sudo fdisk /dev/vda– Enter 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 defaultp– p command prints the disk info, same as runningfdisk -l /dev/vdad– d command delete the last partition (in this case, /dev/vda1)nen command creates a new partition; e makes that an extended partitiont– t changes the type of partition
lvm– Enter lvm as the partition type – or the GUIDE6D6D379-F507-44C2-A23C-238F2A3DF928
NOTE: This assumes, the disk uses GPT. You can also enterLwhen prompted to find the list of codes for the types, including the desired lvm
If you are using an older version of fdisk with MBR/DOS rather than GPT, then in that case, use the8e. If it is a newer version of fdisk, then GUIDE6D6D379-F507-44C2-A23C-238F2A3DF928should work for MBR/DOS as well
-
w– w writes the changes to disk and exits fdisk
- Extend the physical drive partition:
- 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 lvdisplay– The 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
- Tell LVM the physical partition size has changed:
- Resize the file system:
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!
Credits to: https://carleton.ca/scs/2019/extend-lvm-disk-space/