rss logo

Resize a GNU/Linux Disk Without Interrupting Service

Tux logo

I had a virtual machine running Ubuntu whose disk I needed to expand because my /var partition was full.

I challenged myself and wanted to do it live, i.e. without downtime.

⚠️ Modifying the partition table can lead to data loss. It is therefore important to make a full backup of your machine before making any changes.⚠️

VM Configuration

Virtual machine properties

  • OS: Ubuntu Server 20.04 LTS
  • Disk: 80G
  • Partitioning scheme: GPT
  • Partitions: LVM
  • Hypervisor: VMware 6.7

Virtual disk

  • To get a graphical representation, my virtual hard drive looked like this:
Partition layout on a 60GB disk using GPT and LVM, showing separate logical volumes for /home, /var, /, /usr, and /tmp before resizing.

As explained, the problem was that my /var partition was full and I had to extend it.

Increase disk size

VMware

VMware Logo

First, we need to expand the virtual disk. In this example, I'm going to increase my virtual disk by 20G with 4G dedicated to my /var partition. I'll keep the remaining 16G as a reserve for the future.

  • From VMware or any other hypervisor, increase the virtual disk size. Here, I'm going from 60G to 80G:
VMware interface showing the virtual disk size being increased to 80 GB in the Edit Settings menu.
  • The virtual disk now looks as follows:
Updated GPT partition layout after virtual disk resize, showing 20 GB added space and planned extension of the /var logical volume by 4 GB.

⚠️ Just in case, I recommend taking a snapshot of the Virtual Machine at this stage.⚠️

Ubuntu

Ubuntu Logo

gdisk

  • As we can see with gdisk tool, the modification is not automatically taken into account:
user@ubuntu:~$ sudo gdisk -l /dev/sda
GPT fdisk (gdisk) version 1.0.5

Partition table scan:
  MBR: protective
  BSD: not present
  APM: not present
  GPT: present

Found valid GPT with protective MBR; using GPT.
Disk /dev/sda: 125829120 sectors, 60.0 GiB
Model: Virtual disk    
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): 76AC5877-F92F-4C0E-BC53-504F7E90466A
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 125829086
Partitions will be aligned on 2048-sector boundaries
Total free space is 4029 sectors (2.0 MiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048         1050623   512.0 MiB   EF00  
   2         1050624         3147775   1024.0 MiB  8300  
   3         3147776       125827071   58.5 GiB    8300
  • For the new size to be taken into account by the system, we need to perform a disk scan:
user@ubuntu:~$ echo 1 | sudo tee /sys/block/sda/device/rescan
  • Still using the gdisk tool, we can check that our new disk size has been taken into account. Note also the starting sector of the sda3 partition:
user@ubuntu:~$ sudo gdisk -l /dev/sda
GPT fdisk (gdisk) version 1.0.5

Partition table scan:
  MBR: protective
  BSD: not present
  APM: not present
  GPT: present

Found valid GPT with protective MBR; using GPT.
Disk /dev/sda: 167772160 sectors, 80.0 GiB
Model: Virtual disk    
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): 76AC5877-F92F-4C0E-BC53-504F7E90466A
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 125829086
Partitions will be aligned on 2048-sector boundaries
Total free space is 4029 sectors (2.0 MiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048         1050623   512.0 MiB   EF00  
   2         1050624         3147775   1024.0 MiB  8300  
   3         3147776       125827071   58.5 GiB    8300
  • Before making any changes, we need to rewrite our secondary gpt header:
user@ubuntu:~$ sudo gdisk /dev/sda
GPT fdisk (gdisk) version 1.0.5

Partition table scan:
  MBR: protective
  BSD: not present
  APM: not present
  GPT: present

Found valid GPT with protective MBR; using GPT.

Command (? for help): w
Warning! Secondary header is placed too early on the disk! Do you want to
correct this problem? (Y/N): Y

Have moved second header and partition table to correct location.

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!

Do you want to proceed? (Y/N): Y
OK; writing new GUID partition table (GPT) to /dev/sda.
Warning: The kernel is still using the old partition table.
The new table will be used at the next reboot or after you
run partprobe(8) or kpartx(8)
The operation has completed successfully.
  • Check free space:
user@ubuntu:~$ sudo gdisk -l /dev/sda
GPT fdisk (gdisk) version 1.0.5

Partition table scan:
  MBR: protective
  BSD: not present
  APM: not present
  GPT: present

Found valid GPT with protective MBR; using GPT.
Disk /dev/sda: 167772160 sectors, 80.0 GiB
Model: Virtual disk    
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): 76AC5877-F92F-4C0E-BC53-504F7E90466A
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 167772126
Partitions will be aligned on 2048-sector boundaries
Total free space is 41947069 sectors (20.0 GiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048         1050623   512.0 MiB   EF00  
   2         1050624         3147775   1024.0 MiB  8300  
   3         3147776       125827071   58.5 GiB    8300
  • At this point, Ubuntu should see the virtual disk like this:
LVM partition layout after disk expansion to 80 GB, showing unchanged logical volumes and available unallocated space within the volume group.
  • We can now use gdisk to extend the sda3 partition to its new size:
    • d: we first delete partition 3
    • n: we create a new partition 3 using the new size boundaries (sector 3147776 to 167772126)
user@ubuntu:~$ sudo gdisk /dev/sda

Command (? for help): d
Partition number (1-3): 3

Command (? for help): n
Partition number (3-128, default 3): 3
First sector (34-125829086, default = 3147776) or {+-}size{KMGTP}: 3147776
Last sector (3147776-125829086, default = 167772126) or {+-}size{KMGTP}: 
Current type is 8300 (Linux filesystem)
Hex code or GUID (L to show codes, Enter = 8300): 
Changed type of partition to 'Linux filesystem'

Command (? for help): w

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!

Do you want to proceed? (Y/N): Y
OK; writing new GUID partition table (GPT) to /dev/sda.
Warning: The kernel is still using the old partition table.
The new table will be used at the next reboot or after you
run partprobe(8) or kpartx(8)
The operation has completed successfully.
  • Use the partprobe command to make the kernel take the new partition table into account:
user@ubuntu:~$ sudo partprobe
  • At this point, Ubuntu should see the virtual disk like this:
LVM layout on an 80 GB disk showing unallocated free space in the volume group after resizing the physical volume but before extending logical volumes.

LVM

Physical Volume
  • With pvdisplay, we can see that our physical volume needs to be resized:
user@ubuntu:~$ sudo pvdisplay /dev/sda3
  --- Physical volume ---
  PV Name               /dev/sda3
  VG Name               ubuntu-vg
  PV Size               <58.50 GiB / not usable 2.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              14975
  Free PE               0
  Allocated PE          14975
  PV UUID               ThZ9VT-3zCa-SQrB-6s9a-ZiWL-DbqP-TqRNxM
  • Run pvresize to extend the physical volume:
user@ubuntu:~$ sudo pvresize /dev/sda3
  Physical volume "/dev/sda3" changed
  1 physical volume(s) resized or updated / 0 physical volume(s) not resized
  • Run pvdisplay to check that volume has been resized correctly:
user@ubuntu:~$ sudo pvdisplay /dev/sda3
  --- Physical volume ---
  PV Name               /dev/sda3
  VG Name               ubuntu-vg
  PV Size               <78.50 GiB / not usable 1.98 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              20095
  Free PE               5120
  Allocated PE          14975
  PV UUID               ThZ9VT-3zCa-SQrB-6s9a-ZiWL-DbqP-TqRNxM
  • From Ubuntu perspective, the virtual disk now looks like this:
Final LVM disk layout on an 80 GB disk after resizing the logical volume for /var, showing the lvvar extended from 4G to 8G.
Logical Volume

Last but not least, we now need to resize our /var partition.

  • We can resize the logical volume and the ext4 partition in a single command with lvextend:
    • -r: resizefs, resizes the underlying filesystem along with the logical volume
    • -L+4G: add 4G to the ubuntu--vg-lvvar logical volume
user@ubuntu:~$ sudo lvextend -r -L+4G /dev/mapper/ubuntu--vg-lvvar 
  Size of logical volume ubuntu-vg/lvvar changed from 4.00 GiB (1024 extents) to 8.00 GiB (2048 extents).
  Logical volume ubuntu-vg/lvvar successfully resized.
resize2fs 1.45.5 (07-Jan-2020)
Filesystem at /dev/mapper/ubuntu--vg-lvvar is mounted on /var; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/mapper/ubuntu--vg-lvvar is now 2097152 (4k) blocks long.
  • That's it, the size of /var is now 8G:
user@ubuntu:~$ df -h | grep var
/dev/mapper/ubuntu--vg-lvvar	7.9G  5.3G  2.2G  71% /var
  • On the Ubuntu side the virtual disk now looks like this:
LVM disk layout on an 80 GB disk with the /var logical volume highlighted and resized to 8G, confirming the successful extension.
Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Contact :

contact mail address