rss logo

GNU/Linux - How to increase disk size without downtime

Tux logo

I had a Ubuntu virtual machine that I needed to extend cause my /var partition was full.

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

⚠️ Making changes to the partition table can result in data loss. It is therefore important to have a full backup of your machine before making any changes.⚠️

Configuration

Virtual machine

Virtual machine properties

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

Virtual disk

  • To get a graphic representation my virtual hard drive looked like this :
GNU/Linux | Disk partition scheme

As explained my /var partition was full so I needed to extend it.

Increase disk size

VMware

VMware Logo

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

  • From VMware or whatever your hypervisor is, increase virtual disk size. Here I go from 60G to 80G :
VMware | Modify disk size
  • The virtual disk now looks like this :
GNU/Linux | Disk partition scheme

⚠️ Just in case, I recommand here to take a snapshot of the Virtual Machine.⚠️

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
odel: Virtual disk    
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): 76AC5877-F92F-4C0E-BC53-504F7E90466A
Partition table holds up to 128 entries
ain 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 run a disk scan :
user@ubuntu:~$ echo 1 | sudo tee /sys/block/sda/device/rescan
  • Still with gdisk tool, we can check that our new disk size has been taken into account. Also take note of the sda3 starting sector :
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
odel: Virtual disk    
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): 76AC5877-F92F-4C0E-BC53-504F7E90466A
Partition table holds up to 128 entries
ain 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 doing any modificaiton we need to rewrite our gpt secondary 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
odel: Virtual disk    
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): 76AC5877-F92F-4C0E-BC53-504F7E90466A
Partition table holds up to 128 entries
ain 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 :
GNU/Linux | Disk partition scheme
  • We can now use gdisk to extend sda3 partition with its new size :
    • d : first we delete partition 3
    • n : then we create a new partition 3 with new 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 partprobe to make kernel using the new partition table :
user@ubuntu:~$ sudo partprobe
  • At this point, Ubuntu should see the virtual disk like this :
GNU/Linux | Disk partition scheme

LVM

Physical Volume
  • With pvdisplay we can see that our physical volume need 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
  • Use pvresize to extend 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 correctly resized :
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 side the virtual disk now looks like this :
GNU/Linux | Disk partition scheme
Logical Volume

Final step, we now need to resize our partition /var.

  • We can resize logical volume and ext4 partition in one command with lvextend :
    • -r : resizefs, Resize underlying filesystem together with the LV
    • -L+4G : Add 4G of the current ubuntu--vg-lvvar.
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 /var size is now 8G :
user@ubuntu:~$ df -h | grep var
/dev/mapper/ubuntu--vg-lvvar	7.9G  5.3G  2.2G  71% /var
  • From Ubuntu side the virtual disk now looks like this :
GNU/Linux | Disk partition scheme
Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Contact :

contact mail address