Posted by
ads
on
Thursday, 2025-06-12 Posted in [Linux]
Currently installing a new Grafana system, which runs on a Proxmox server. For now the disk for the virtual machine and the ext4 filesystem is large enough, but maybe I need to expand it in the future. Therefore I intentionally created a smaller disk and tried expanding it on the fly. If the expansion doesn’t work, I can just re-create the VM. This blog posting documents the necessary steps for the disk and ext4 filesystem expansion.
Danger Area!
Be careful with the following steps! This can destroy your root partition. You have been warned. Also make sure you have a backup!
Start with a smaller disk
The virtual machine is created with a 30GB disk:
1
2
3
root@grafana-home:/home/grafana# df -h /
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 31G 1.7G 28G 6% /
During installation I opted for an installation on a single partition, without splitting /var and /home up on separate partitions. The partition structure, as created automatically by the DebianBookworm installer:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
root@grafana-home:/home/grafana# fdisk /dev/sda
Command (m for help): p
Disk /dev/sda: 50 GiB, 53687091200 bytes, 104857600 sectors
Disk model: QEMU HARDDISK
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xbaef081b
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 65107967 65105920 31G 83 Linux
/dev/sda2 65110014 67106815 1996802 975M 5 Extended
/dev/sda5 65110016 67106815 1996800 975M 82 Linux swap / Solaris
Necessary tools
For resizing the partition I need the growpart tool, which is in the gdiskDebian package:
1
2
3
4
5
6
7
8
apt install cloud-guest-utils
The following additional packages will be installed:
gdisk
Suggested packages:
cloud-init
The following NEW packages will be installed:
cloud-guest-utils gdisk
0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.
Resize the disk
This happens in Proxmox. Navigate to the virtual machine.
Click on Hardware
Click on Hard Disk (scsi0)
If you have multiple disks, click the root partition (or the one you want to expand)
Click on the Disk Action select button, and select Resize
A new window Resize disk pops up
Enter the additional amount of disk space you want to add (not the new total amount)
In my case I added 20 for adding 20 GB of new space
Click Resize disk
This will resize the disk, or the file holding the disk.
Resize the partition - attempt one
The disk for the virtual machine is now 20GB larger, but the new disk space is at the end of the disk. Which is not helpful.
Let’s try growing the root partition (partition 1, sda1 on the disk):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
root@grafana-home:/home/grafana# growpart --verbose /dev/sda 1
update-partition set to true
FLOCK: try exec open fd 9, on failure exec exits this program
FLOCK: /dev/sda: obtained exclusive lock
resizing 1 on /dev/sda using resize_sfdisk_dos
104857600 sectors of 512. total size=53687091200 bytes
## sfdisk --unit=S --dump /dev/sda
label: dos
label-id: 0xbaef081b
device: /dev/sda
unit: sectors
sector-size: 512
/dev/sda1 : start= 2048, size= 65105920, type=83, bootable
/dev/sda2 : start= 65110014, size= 1996802, type=5
/dev/sda5 : start= 65110016, size= 1996800, type=82
max_end=65110013 tot=104857600 pt_end=65107967 pt_start=2048 pt_size=65105920
NOCHANGE: partition 1 could only be grown by 2046 [fudge=2048]
FLOCK: /dev/sda: releasing exclusive lock
Bummer: NOCHANGE: partition 1 could only be grown by 2046
The reason why the root partition can’t be resized is that after the root partition there is a swap partition and the new free space is after the swap partition. I need to move the swap to the end of the disk, or recreate the swap partition at the end of the disk.
Drop the swap partition
First turn off the swap. This will ask the operating system to load back any files which it might hold in swap into the system memory:
1
2
swapoff /dev/sda5
swapon --show
Confirm that swap space is no longer used:
1
2
3
4
root@grafana-home:/home/grafana# free -h
total used free shared buff/cache available
Mem: 1.9Gi 243Mi 1.5Gi 472Ki 289Mi 1.7Gi
Swap: 0B 0B 0B
Edit /etc/fstab and comment out the swap partition. The next steps will remove the partition, by commenting it out it can’t be loaded by accident if the system boots.
1
2
# swap was on /dev/sda5 during installation
#UUID=3d199bd2-feb9-40b3-8eb9-757d35acf1bc none swap sw 0 0
root@grafana-home:/home/grafana# fdisk /dev/sda
Command (m for help): p
Disk /dev/sda: 50 GiB, 53687091200 bytes, 104857600 sectors
Disk model: QEMU HARDDISK
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xbaef081b
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 65107967 65105920 31G 83 Linux
/dev/sda2 65110014 67106815 1996802 975M 5 Extended
/dev/sda5 65110016 67106815 1996800 975M 82 Linux swap / Solaris
Command (m for help): d
Partition number (1,2,5, default 5): 5
Partition 5 has been deleted.
Command (m for help): p
Disk /dev/sda: 50 GiB, 53687091200 bytes, 104857600 sectors
Disk model: QEMU HARDDISK
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xbaef081b
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 65107967 65105920 31G 83 Linux
/dev/sda2 65110014 67106815 1996802 975M 5 Extended
Command (m for help): d
Partition number (1,2, default 2): 2
Partition 2 has been deleted.
Command (m for help): p
Disk /dev/sda: 50 GiB, 53687091200 bytes, 104857600 sectors
Disk model: QEMU HARDDISK
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xbaef081b
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 65107967 65105920 31G 83 Linux
Command (m for help): w
The partition table has been altered.
Syncing disks.
Gone. Before moving on, remember the number of blocks for the swap partition. The new partition will be roughly the same size.
The swap partition was 1996802 sectors in size, and from sector 65110016 to sector 67106815. Before resizing, this was almost the end of the disk.
After resizing, the disk is 104857600 sectors in size, and subtract 1996802 from that makes the new start sector 102860798. Add a couple sectors more, for the extended partition.
Recreate the swap partition at the end of the disk
root@grafana-home:/home/grafana# fdisk /dev/sda
Command (m for help): p
Disk /dev/sda: 50 GiB, 53687091200 bytes, 104857600 sectors
Disk model: QEMU HARDDISK
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xbaef081b
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 65107967 65105920 31G 83 Linux
Command (m for help): n
Partition type
p primary (1 primary, 0 extended, 3 free)
e extended (container for logical partitions)
Select (default p): e
Partition number (2-4, default 2):
First sector (65107968-104857599, default 65107968): 102860000
Last sector, +/-sectors or +/-size{K,M,G,T,P} (102860000-104857599, default 104857599):
Created a new partition 2 of type 'Extended' and of size 975.4 MiB.
Command (m for help): n
Partition type
p primary (1 primary, 1 extended, 2 free)
l logical (numbered from 5)
Select (default p): l
Adding logical partition 5
First sector (102862048-104857599, default 102862848): 102862048
Last sector, +/-sectors or +/-size{K,M,G,T,P} (102862048-104857599, default 104857599):
Created a new partition 5 of type 'Linux' and of size 974.4 MiB.
Command (m for help): p
Disk /dev/sda: 50 GiB, 53687091200 bytes, 104857600 sectors
Disk model: QEMU HARDDISK
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xbaef081b
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 65107967 65105920 31G 83 Linux
/dev/sda2 102860000 104857599 1997600 975.4M 5 Extended
/dev/sda5 102862048 104857599 1995552 974.4M 83 Linux
I’m using 102860000 as start sector for the extended partition.
When adding the logical partition for swap, note how fdisk suggests 102862848 as start sector. That is quite a bit into the partition, and instead I’m going with 102862048. This value must be entered manually. For the last sector, just accept the default - that is the end of the disk.
Currently the new partition is a “Linux” partition, type 83. We need this to be a “swap” partition, type 82. One more round of fdisk:
root@grafana-home:/home/grafana# fdisk /dev/sda
Command (m for help): t
Partition number (1,2,5, default 5): 5
Hex code or alias (type L to list all): 82
Changed type of partition 'Linux' to 'Linux swap / Solaris'.
Command (m for help): p
Disk /dev/sda: 50 GiB, 53687091200 bytes, 104857600 sectors
Disk model: QEMU HARDDISK
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xbaef081b
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 65107967 65105920 31G 83 Linux
/dev/sda2 102860000 104857599 1997600 975.4M 5 Extended
/dev/sda5 102862048 104857599 1995552 974.4M 82 Linux swap / Solaris
Command (m for help): w
The partition table has been altered.
Syncing disks.
Resize the root partition
After writing the partition table back to disk, growpart can finally grow sda1 and use the entire free disk space.
root@grafana-home:/home/grafana# growpart --verbose /dev/sda 1
update-partition set to true
FLOCK: try exec open fd 9, on failure exec exits this program
FLOCK: /dev/sda: obtained exclusive lock
resizing 1 on /dev/sda using resize_sfdisk_dos
104857600 sectors of 512. total size=53687091200 bytes
## sfdisk --unit=S --dump /dev/sda
label: dos
label-id: 0xbaef081b
device: /dev/sda
unit: sectors
sector-size: 512
/dev/sda1 : start= 2048, size= 65105920, type=83, bootable
/dev/sda2 : start= 102860000, size= 1997600, type=5
/dev/sda5 : start= 102862048, size= 1995552, type=82
max_end=102859999 tot=104857600 pt_end=65107967 pt_start=2048 pt_size=65105920
resize of /dev/sda returned 0.
CHANGED: partition=1 start=2048 old: size=65105920 end=65107967 new: size=102857952 end=102859999
FLOCK: /dev/sda: releasing exclusive lock
Quick check:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
root@grafana-home:/home/grafana# fdisk /dev/sda
Command (m for help): p
Disk /dev/sda: 50 GiB, 53687091200 bytes, 104857600 sectors
Disk model: QEMU HARDDISK
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xbaef081b
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 102859999 102857952 49G 83 Linux
/dev/sda2 102860000 104857599 1997600 975.4M 5 Extended
/dev/sda5 102862048 104857599 1995552 974.4M 82 Linux swap / Solaris
Partition looks good, size is 50GB now.
Resize the filesystem
However the filesystem is still 30GB.
1
2
3
root@grafana-home:/home/grafana# df -h /
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 31G 1.7G 28G 6% /
This also must be resized. For this I use resize2fs:
1
2
3
4
5
root@grafana-home:/home/grafana# resize2fs /dev/sda1
resize2fs 1.47.0 (5-Feb-2023)
Filesystem at /dev/sda1 is mounted on /; on-line resizing required
old_desc_blocks = 4, new_desc_blocks = 7
The filesystem on /dev/sda1 is now 12857244 (4k) blocks long.
Much better:
1
2
3
root@grafana-home:/home/grafana# df -h /
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 49G 1.7G 45G 4% /
Enable swap partition
What’s left is enabling again the swap partition. We did create the partition itself: