Extend harddisk size of Linux

The hypervisor Proxmox gives you the ability to extend virtual harddisks of your virtual machines. This “How To” explains how to extend the virtual disk of a virtualized Linux.

Information

The virtual machine is a Linux Debian 9.6 which uses the filesystem ext4. The disk will be extended from 21 GB to 22 GB.

How To

Get the current partition table

root@vlx-project:/home/sij# fdisk -l
Disk /dev/vda: 22 GiB, 23622320128 bytes, 46137344 sectors
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: 0xfc0f9abd

Device     Boot    Start      End  Sectors Size Id Type
/dev/vda1  *        2048 39847935 39845888  19G 83 Linux
/dev/vda2       39847936 44040191  4192256   2G  5 Extended
/dev/vda5       39849984 44040191  4190208   2G 82 Linux swap / Solari

Edit the partition table

root@vlx-project:/home/sij# fdisk /dev/vda

Welcome to fdisk (util-linux 2.29.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): d
Partition number (1,2,5, default 5):

Partition 5 has been deleted.

Command (m for help): d
Partition number (1,2, default 2):

Partition 2 has been deleted.

Command (m for help): d
Selected partition 1
Partition 1 has been deleted.

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p):

Using default response p.
Partition number (1-4, default 1):
First sector (2048-46137343, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-46137343, default 46137343): +20G

Created a new partition 1 of type 'Linux' and of size 20 GiB.
Partition #1 contains a ext4 signature.

Do you want to remove the signature? [Y]es/[N]o: N

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 (41945088-46137343, default 41945088):
Last sector, +sectors or +size{K,M,G,T,P} (41945088-46137343, default 46137343):

Created a new partition 2 of type 'Extended' and of size 2 GiB.

Command (m for help): n
All space for primary partitions is in use.
Adding logical partition 5
First sector (41947136-46137343, default 41947136):
Last sector, +sectors or +size{K,M,G,T,P} (41947136-46137343, default 46137343):

Created a new partition 5 of type 'Linux' and of size 2 GiB.

Command (m for help): t
Partition number (1,2,5, default 5):
Partition type (type L to list all types): 82

Changed type of partition 'Linux' to 'Linux swap / Solaris'.

Command (m for help): a
Partition number (1,2,5, default 5): 1

The bootable flag on partition 1 is enabled now.

Command (m for help): p
Disk /dev/vda: 22 GiB, 23622320128 bytes, 46137344 sectors
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: 0xfc0f9abd

Device     Boot    Start      End  Sectors Size Id Type
/dev/vda1  *        2048 41945087 41943040  20G 83 Linux
/dev/vda2       41945088 46137343  4192256   2G  5 Extended
/dev/vda5       41947136 46137343  4190208   2G 82 Linux swap / Solaris

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Re-reading the partition table failed.: Das Gerät oder die Ressource ist belegt

The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8).

It is important, that the partition which contains all data, starts at the same block as before. The partitions vda2 and vda5 can be moved, because these contain no data. Is the swap partition.

root@vlx-project:/home/sij# reboot

Reconfigure the swap partition

During the reboot the following error appears:

A start job is running for dev-disk-by\x2duuid-d3acc93e\x2d...b5\x2d4cdd\x2d9915\x2d64882eda7f82.device (11s / 1 min 30s)

This is caused by the change of the uuid of the swap partition. The old uuid, which doesnt exist anymore, is still in the /etc/fstab. To fix this error, we need to initialize the new swap partition and replace the old uuid by the new one.

root@vlx-project:/home/sij# cat /etc/fstab
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
# / was on /dev/vda1 during installation
UUID=4b18fd12-cf9d-4f71-8e86-bfe13c2ff7b4 /               ext4    errors=remount-ro 0       1
# swap was on /dev/vda5 during installation
UUID=d3acc93e-c6b5-4cdd-9915-64882eda7f82 none            swap    sw              0       0
/dev/sr0        /media/cdrom0   udf,iso9660 user,noauto     0       0
root@vlx-project:/home/sij# mkswap /dev/vda5
Setting up swapspace version 1, size = 2 GiB (2145382400 bytes)
no label, UUID=8dacbe0b-f8ea-4939-8eb2-9b919dacf657

The old swap uuid can now be replaced by the new one.

Resize the filesystem

The partition is now bigger, but the filesystem is still 19 GB and not the expected 20 GB.

root@vlx-project:/home/sij# df -h
Dateisystem    Größe Benutzt Verf. Verw% Eingehängt auf
udev            992M       0  992M    0% /dev
tmpfs           201M    3.0M  198M    2% /run
/dev/vda1        19G    4.9G   13G   28% /
tmpfs          1003M       0 1003M    0% /dev/shm
tmpfs           5.0M       0  5.0M    0% /run/lock
tmpfs          1003M       0 1003M    0% /sys/fs/cgroup
tmpfs           201M       0  201M    0% /run/user/1000
root@vlx-project:/home/sij# resize2fs /dev/vda1
resize2fs 1.43.4 (31-Jan-2017)
Dateisystem bei /dev/vda1 ist auf / eingehängt; Online-Größenänderung ist
erforderlich
old_desc_blocks = 3, new_desc_blocks = 3
Das Dateisystem auf /dev/vda1 is nun 5242880 (4k) Blöcke lang.

root@vlx-project:/home/sij# df -h
Dateisystem    Größe Benutzt Verf. Verw% Eingehängt auf
udev            992M       0  992M    0% /dev
tmpfs           201M    3.0M  198M    2% /run
/dev/vda1        20G    4.9G   14G   27% /
tmpfs          1003M       0 1003M    0% /dev/shm
tmpfs           5.0M       0  5.0M    0% /run/lock
tmpfs          1003M       0 1003M    0% /sys/fs/cgroup
tmpfs           201M       0  201M    0% /run/user/1000