This is Linux specific of course. There are three file systems I’ll be listing here. The more common EXT4, newer XFS, and newest GPT file system.
For this example, we have a small image and need to extend it. Additional space has been added to bring the file system to 20 Gigabytes. You just need to expand the space to 20 Gigabytes.
One note here before we continue. Observe that all these examples are a single file system. What that means is extending a partition is pretty easy. But what happens with a normal configuration where there are separate file systems such as root, usr, home, var, etc? If the file system is at the end of the partition table, it’s pretty easy to extend it which is why, typically var is the last file system. But what happens if home needs to be extended? When you’re adding sectors, you add it to the end of a disk. What this means is you need to create a new home file system with the new size, move all the files to the new file system, unmount the old one, fix /etc/fstab, and remount. And now you have the old home directory as a blank spot in your partition table. You might be able to use it to extend usr if it needed extending. Or maybe use it as a /var specific file system such as for mysql. It’s why LVM is so popular.
EXT4 File System
# df -k
Filesystem 1K-blocks Used Available Use% Mounted on
udev 1014152 0 1014152 0% /dev
tmpfs 204548 2948 201600 2% /run
/dev/vda1 2030416 991160 918068 52% /
tmpfs 1022720 0 1022720 0% /dev/shm
tmpfs 5120 0 5120 0% /run/lock
tmpfs 1022720 0 1022720 0% /sys/fs/cgroup
tmpfs 204544 0 204544 0% /run/user/0
Run the fdisk command to confirm space was added and is recognized.
# fdisk -l
Disk /dev/vda: 20 GiB, 21474836480 bytes, 41943040 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: 0xe5c3b0d8
Device Boot Start End Sectors Size Id Type
/dev/vda1 * 2048 4194303 4192256 2G 83 Linux
And there it is, 20 GiB. You’ll delete the existing partition table and add it back in at the new size. Until you actually write to the disk, the changes are in memory. Exiting without saving will prevent the update.
# fdisk /dev/vda
Welcome to fdisk (util-linux 2.33.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): p
Disk /dev/vda: 20 GiB, 21474836480 bytes, 41943040 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: 0xe5c3b0d8
Device Boot Start End Sectors Size Id Type
/dev/vda1 * 2048 4194303 4192256 2G 83 Linux
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): p
Partition number (1-4, default 1): 1
First sector (2048-41943039, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-41943039, default 41943039):
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): p
Disk /dev/vda: 20 GiB, 21474836480 bytes, 41943040 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: 0xe5c3b0d8
Device Boot Start End Sectors Size Id Type
/dev/vda1 2048 41943039 41940992 20G 83 Linux
Command (m for help): w
The partition table has been altered.
Syncing disks.
From here you can either reboot the server or if partprobe is installed, run it to register the new table to the kernel.
# partprobe
Once registered, you simply extend the file system to fit the now available space using resize2fs.
# resize2fs /dev/vda1
resize2fs 1.44.5 (15-Dec-2018)
Filesystem at /dev/vda1 is mounted on /; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 3
[ 4014.025845] EXT4-fs (vda1): resizing filesystem from 524032 to 5242624 blocks
[ 4014.172547] EXT4-fs (vda1): resized filesystem to 5242624
The filesystem on /dev/vda1 is now 5242624 (4k) blocks long.
And done, the file system now registers 20 Gigabytes.
# df -k
Filesystem 1K-blocks Used Available Use% Mounted on
udev 1014152 0 1014152 0% /dev
tmpfs 204548 2948 201600 2% /run
/dev/vda1 20608592 1008764 18723724 6% /
tmpfs 1022720 0 1022720 0% /dev/shm
tmpfs 5120 0 5120 0% /run/lock
tmpfs 1022720 0 1022720 0% /sys/fs/cgroup
tmpfs 204544 0 204544 0% /run/user/0
XFS File Systems
In this case, you won’t need to use fdisk to make the necessary changes. Once the 20 Gig has been added, you just need to run the growpart command to extend the file system.
# growpart /dev/vda 1
CHANGED: partition=1 start=2048 old: size=16384000 end=16386047 new: size=41940992 end=41943039
Then use the xfs_growfs command to extend the file system.
# xfs_growfs -d /dev/vda1
meta-data=/dev/vda1 isize=512 agcount=4, agsize=512000 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=1, rmapbt=0
= reflink=1
data = bsize=4096 blocks=2048000, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0, ftype=1
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 2048000 to 5242624
And the file system is 20 Gigs now.
GPT File Systems
This one is pretty similar to the EXT4 File System updates. The only change is managing partition tables uses gdisk instead of fdisk. Otherwise everything else is the same as EXT4.
# gdisk /dev/vda
GPT fdisk (gdisk) version 1.0.3
Partition table scan:
MBR: protective
BSD: not present
APM: not present
GPT: present
Found valid GPT with protective MBR; using GPT.
Command (? for help): p
Disk /dev/vda: 41943040 sectors, 20.0 GiB
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): E1A6C9DD-012D-4943-8697-0FE02F412F36
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 41943006
Partitions will be aligned on 2048-sector boundaries
Total free space is 37332958 sectors (17.8 GiB)
Number Start (sector) End (sector) Size Code Name
1 227328 4612062 2.1 GiB 8300
14 2048 10239 4.0 MiB EF02
15 10240 227327 106.0 MiB EF00
Command (? for help): d
Partition number (1-15): 1
Command (? for help): n
Partition number (1-128, default 1): 1
First sector (34-41943006, default = 227328) or {+-}size{KMGTP}:
Last sector (227328-41943006, default = 41943006) or {+-}size{KMGTP}:
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300):
Changed type of partition to 'Linux filesystem'
Command (? for help): p
Disk /dev/vda: 41943040 sectors, 20.0 GiB
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): E1A6C9DD-012D-4943-8697-0FE02F412F36
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 41943006
Partitions will be aligned on 2048-sector boundaries
Total free space is 2014 sectors (1007.0 KiB)
Number Start (sector) End (sector) Size Code Name
1 227328 41943006 19.9 GiB 8300 Linux filesystem
14 2048 10239 4.0 MiB EF02
15 10240 227327 106.0 MiB EF00
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/vda.
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.
Again, you’ll use partprobe and resize2fs to extend the file system.
Notes
Just noting that this was done quite some time ago and it’s been updated several times over the years to be more current.