I had a virtual machine running Ubuntu whose disk I needed to extpand 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.⚠️
As explained, the problem was that my /var partition was full and I had to extend it.
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.
⚠️ Just in case, I recommend taking a snapshot of the Virtual Machine at this stage.⚠️
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
user@ubuntu:~$ echo 1 | sudo tee /sys/block/sda/device/rescan
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
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.
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
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.
user@ubuntu:~$ sudo partprobe
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
user@ubuntu:~$ sudo pvresize /dev/sda3
Physical volume "/dev/sda3" changed
1 physical volume(s) resized or updated / 0 physical volume(s) not 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
Last but not least, we now need to resize our /var partition.
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.
user@ubuntu:~$ df -h | grep var
/dev/mapper/ubuntu--vg-lvvar 7.9G 5.3G 2.2G 71% /var
Contact :