How to Extend Ubuntu Logical Volumes and Resize Filesystem: Step-by-Step Guide for System Administrators

How to Extend Ubuntu Logical Volumes and Resize Filesystem: Step-by-Step Guide for System Administrators

When you install Ubuntu, by default it doesn’t use all the disk space when creating the default logical volume (LV). This requires you to manually add the unused space or disks. This guide provides a step-by-step procedure for extending the default logical volume and resizing the filesystem on Ubuntu 22.04.5 LTS installed on a VMware VM.

After installing Ubuntu, the default logical volume does not utilize all the allocated disk space. For example, a VM with 2TB allocated may only use 98GB for its default logical volume. This guide will help system administrators extend the logical volume to use the full disk space and resize the filesystem accordingly without having to restart their server or losing data or impacting applications already running on the default ubuntu logical volume.

   # df -h

Sample Output: (As you can see, only 98G is being used by the default Ubuntu LV)

   Filesystem                         Size  Used Avail Use% Mounted on
   tmpfs                              6.3G  1.3M  6.3G   1% /run
   /dev/mapper/ubuntu--vg-ubuntu--lv   98G   26G   68G  28% /
   tmpfs                               32G     0   32G   0% /dev/shm
   tmpfs                              5.0M     0  5.0M   0% /run/lock
   /dev/sda2                          2.0G  243M  1.6G  14% /boot
   /dev/sda1                          1.1G  6.1M  1.1G   1% /boot/efi
   tmpfs                              6.3G  4.0K  6.3G   1% /run/user/1000
   overlay                             98G   26G   68G  28% /var/lib/docker/overlay2/9e2396901616c7a13454d3f5a305f4217a22d809b0dd4daeb2f1bab47f7c94c8/merged
   # fdisk -l | grep -i /dev/sd

Sample Output: (As you can see, 2T provisioned to this server, but it’s nowhere in the filesystem output!)

   Disk /dev/sda: 2 TiB, 2199023255552 bytes, 4294967296 sectors
   /dev/sda1     2048    2203647    2201600   1G EFI System
   /dev/sda2  2203648    6397951    4194304   2G Linux filesystem
   /dev/sda3  6397952 4294965247 4288567296   2T Linux filesystem
   # lsblk

Sample Output: (Using “lsblk” we can see all the available disks, “sda3” = 2T is attached to the default Ubuntu Volume Group (VG) and Logical Volume (LV), but only 100G is currently allocated to the LV)

   NAME                      MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
   loop0                       7:0    0 63.4M  1 loop /snap/core20/1974
   loop1                       7:1    0 44.3M  1 loop /snap/snapd/23258
   loop2                       7:2    0   87M  1 loop /snap/lxd/29351
   loop3                       7:3    0 44.4M  1 loop /snap/snapd/23545
   loop5                       7:5    0 63.7M  1 loop /snap/core20/2434
   loop6                       7:6    0 89.4M  1 loop /snap/lxd/31333
   sda                         8:0    0    2T  0 disk 
   ├─sda1                      8:1    0    1G  0 part /boot/efi
   ├─sda2                      8:2    0    2G  0 part /boot
   └─sda3                      8:3    0    2T  0 part 
     └─ubuntu--vg-ubuntu--lv 253:0    0  100G  0 lvm  /
   sr0                        11:0    1 1024M  0 rom  
   # pvs

Sample Output:

   PV         VG        Fmt  Attr PSize  PFree 
   /dev/sda3  ubuntu-vg lvm2 a--  <2.00t <1.90t
   # vgs

Sample Output:

   VG        #PV #LV #SN Attr   VSize  VFree 
   ubuntu-vg   1   1   0 wz--n- <2.00t <1.90t
   # vgdisplay ubuntu-vg

Sample Output:

(In the output below, notice that the default installation already created the Physical Volume (PV), Volume Group (VG) and Logical Volume (LV). The VG is 2.00 TiB but then the LV is 100.00 GiB. And therefore to be able to use all the available 2TB in the file system, we need to extend the LV)

   --- Volume group ---
   VG Name               ubuntu-vg
   System ID             
   Format                lvm2
   Metadata Areas        1
   Metadata Sequence No  2
   VG Access             read/write
   VG Status             resizable
   MAX LV                0
   Cur LV                1
   Open LV               1
   Max PV                0
   Cur PV                1
   Act PV                1
   VG Size               <2.00 TiB
   PE Size               4.00 MiB
   Total PE              523506
   Alloc PE / Size       25600 / 100.00 GiB
   Free  PE / Size       497906 / <1.90 TiB
   VG UUID               M25a3Y-sd7I-VyKL-fErn-cPsE-dXmg-rlCqNf

(You can indicate how much you want to extend the volume or use the “+100%FREE” option to use all the available free space.)

   # lvextend -l +100%FREE /dev/ubuntu-vg/ubuntu-lv

Sample Output:

   Size of logical volume ubuntu-vg/ubuntu-lv changed from 100.00 GiB (25600 extents) to <2.00 TiB (523506 extents).
   Logical volume ubuntu-vg/ubuntu-lv successfully resized.
   # lvdisplay /dev/mapper/ubuntu--vg-ubuntu--lv

Sample Output:

(Checking the LV, you can see it has been extended to 2.00 TiB)

   --- Logical volume ---
   LV Path                /dev/ubuntu-vg/ubuntu-lv
   LV Name                ubuntu-lv
   VG Name                ubuntu-vg
   LV UUID                pl6Jxv-o5oz-qi5j-OBOV-0F5R-lZGk-glUxyT
   LV Write Access        read/write
   LV Creation host, time ubuntu-server, 2024-11-01 20:22:40 +0000
   LV Status              available
   # open                 1
   LV Size                <2.00 TiB
   Current LE             523506
   Segments               1
   Allocation             inherit
   Read ahead sectors     auto
   - currently set to     256
   Block device           253:0

(However, we are not done resizing the filesystem, in fact if you check using “df -h” at this point, you will realize that even if the LV Size is reading 2.00 TiB, the Filesystem disk size is still reading 98G, meaning that the change is not yet in effect at the filesystem level! )

(The last step is to resize the file system on the logical volume using the “resize2fs” command)

   # resize2fs /dev/ubuntu-vg/ubuntu-lv

Sample Output:

   resize2fs 1.46.5 (30-Dec-2021)
   Filesystem at /dev/ubuntu-vg/ubuntu-lv is mounted on /; on-line resizing required
   old_desc_blocks = 13, new_desc_blocks = 256
   The filesystem on /dev/ubuntu-vg/ubuntu-lv is now 536070144 (4k) blocks long.
   # df -h

Sample Output:

Filesystem                         Size  Used Avail Use% Mounted on
tmpfs                              6.3G  1.3M  6.3G   1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv  2.0T   26G  1.9T   2% /
tmpfs                               32G     0   32G   0% /dev/shm
tmpfs                              5.0M     0  5.0M   0% /run/lock
/dev/sda2                          2.0G  243M  1.6G  14% /boot
/dev/sda1                          1.1G  6.1M  1.1G   1% /boot/efi
tmpfs                              6.3G  4.0K  6.3G   1% /run/user/1000
overlay                            2.0T   26G  1.9T   2% /var/lib/docker/overlay2/9e2396901616c7

As you can see from the output above, the disk size has been expanded from 98G to 2.0T without having to restart the server and without losing the data that was on the existing logical volume. That’s the advantage of using logical volumes, the process of extending the LV and resizing the file system is safe and NOT service impacting.

About the Author

Joshua Makuru Nomwesigwa is a seasoned Telecommunications Engineer with vast experience in IP Technologies; he eats, drinks, and dreams IP packets. He is a passionate evangelist of the forth industrial revolution (4IR) a.k.a Industry 4.0 and all the technologies that it brings; 5G, Cloud Computing, BigData, Artificial Intelligence (AI), Machine Learning (ML), Internet of Things (IoT), Quantum Computing, etc. Basically, anything techie because a normal life is boring.

Spread the word:

Leave a Reply