Introduction:
In this post, I will explain how to expand disk space on a Linux system using Logical Volume Manager (LVM). Whether you’re dealing with a physical or virtual disk, this step-by-step tutorial will help you make the most of your storage without the need for a system reboot.
Step 1: Increase Disk Size
The first step is to increase the size of the disk that your Linux OS uses. This can be done by expanding the virtual disk in virtualization platforms like VMWare, KVM or Hyper-V. For this post, we’ll extend a 16 GB disk to 30 GB.
My Lab Details:
Hypervisor: VMware ESXi
VM: Ubuntu 22.04.1 LTS
root@ubuntu-test:~# df -h
Filesystem Size Used Avail Use% Mounted on
tmpfs 98M 1.3M 96M 2% /run
/dev/mapper/ubuntu--vg-ubuntu--lv 9.8G 6.7G 2.6G 73% /
tmpfs 486M 0 486M 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
/dev/sda2 1.7G 247M 1.4G 16% /boot
tmpfs 98M 4.0K 98M 1% /run/user/1000
Disk is still not visible/listed
Step 2: Rescan the Disk
After resizing the disk, you may need to prompt Linux to recognize the additional free space. To check for free space, run the cfdisk
command and see if it lists free space. Use “q” to exit when done. If there’s no free space listed, initiate a rescan for /dev/sda
with the command:
echo 1 > /sys/class/block/sda/device/rescan
Stage 2: Modify the partition within the VM
(Note: It is time to stop, if you have not backup all your data, or if you are not sure about what you are doing.)
Step 6: type the following command, find Disk /dev/sda, and confirm the size (of step 3).
$ sudo fdisk -l
Step 7: Run the command
$ sudo fdisk /dev/sda
Step 8: Type 'p' to check the partition info.
(Warning: if you have more than one partitions, STOP using this guide.)
Step 9: Type 'd' to delete Partition 1.
Step 10: Type 'p', and confirm the changes
Step 11: Type 'n', hit 'Enter' 4 times to use defaults ([Partition type], [Partition number], [first sector], and [last sector]), and type 'n'. (You might not need the last 'n'.)
Note: Chose [N]
Partition #3 contains a LVM2_member signature.
Do you want to remove the signature? [Y]es/[N]o: n
Step 12: Type 'p', confirm the changes, and then type 'w'
Step 13: Restart the virtual machine.
Stage 3: Resize the disk space within the VM
Step 14: Run following command
$ sudo resize2fs /dev/sda1
Step 4: Extend the Physical Volume (PV)
With the LVM partition backing up the /dev/sda3
PV extended, it’s time to extend the PV itself.
Run
pvresize /dev/sda3
to accomplish this, and then use pvdisplay
to verify the new size. In our example, the PV has increased from 14.2GB to 28.2G.
Step 5: Check Volume Group (VG) Free Space
Now, check the VG’s free space using vgdisplay
. In our case, there’s 18.25 GiB of free space available within the VG.
Step 6: Extend Logical Volume (LV)
Use lvdisplay
to verify the size of
your upstream Logical Volume (LV). Extend the LV to use all available VG
free space with the below command
lvextend -l +100%FREE /dev/ubuntu-vg/ubuntu-lv
Check the LV one more time with lvdisplay
to ensure it’s been extended.
root@ubuntu-test:~# lvextend -l +100%FREE /dev/ubuntu-vg/ubuntu-lv
Size of logical volume ubuntu-vg/ubuntu-lv changed from 10.00 GiB (2560 extents) to <28.25 GiB (7231 extents).
Logical volume ubuntu-vg/ubuntu-lv successfully resized.
root@ubuntu-test:~# lvdisplay
--- Logical volume ---
LV Path /dev/ubuntu-vg/ubuntu-lv
LV Name ubuntu-lv
VG Name ubuntu-vg
LV UUID xL1oCY-qBRL-cT86-jRRr-0cWK-0P4W-U0Y1Dp
LV Write Access read/write
LV Creation host, time ubuntu-server, 2023-04-12 11:44:17 +0000
LV Status available
# open 1
LV Size <28.25 GiB
Current LE 7231
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
Step 7: Resize the File System
At this point, the block volume supporting your root filesystem has
been expanded, but the filesystem itself needs resizing. Run df -h
to check the current filesystem size. Use the below command to resize it, and run df -h
once more to confirm the new available space.
resize2fs /dev/mapper/ubuntu--vg-ubuntu--lv
Following the above steps, you can successfully expand your Linux Machine/VM physical or virtual disk space using LVM. This allows you to efficiently manage your storage resources and accommodate growing storage needs without disrupting your system. Hope this helps.
Post a Comment
Post a Comment