In Part 1 of this series, we covered how to diagnose disk space using df and du, unpacked the structural hierarchy of block devices, and used fdisk to uncover a classic storage bottleneck: a swap partition (/dev/vda2) physically blocking our main root partition (/dev/vda1) from expanding into newly allocated disk sectors.
In this second installment, we will walk through the exact hands-on procedure to expand the underlying hypervisor disk, force the Linux kernel to recognize new space on the fly, and safely rewrite partition sector boundaries in fdisk without losing a single byte of data.
Step 1: Resizing the Virtual Disk Image
Before your guest OS can see more disk space, you must expand the virtual disk image on the host hypervisor. If you are using QEMU/KVM with Virtual Machine Manager (virt-manager), first locate your VM’s .qcow2 file path under disk settings (typically /var/lib/libvirt/images/).
Run the following command on your host machine terminal to append additional storage (for example, +50GB):
Bash
sudo qemu-img resize /var/lib/libvirt/images/Kali.qcow2 +50G
Plaintext
Image resized.
If you check fdisk inside the running guest VM right away, you might notice something frustrating: the total sector count hasn’t changed! This happens because the guest kernel caches block device geometries upon boot.
Step 2: Triggering a Live Kernel Rescan
Instead of interrupting your workflow with a full VM reboot, you can force the guest Linux kernel to re-read the block device size dynamically.
Inside your guest VM terminal, execute:
Bash
echo 1 | sudo tee /sys/class/block/vda/device/rescan
Now, verify that the kernel recognizes the new underlying physical capacity:
Bash
sudo fdisk -l /dev/vda
Plaintext
Disk /dev/vda: 90 GiB, 96636764160 bytes, 188743680 sectors
The physical block device /dev/vda now spans 188,743,680 sectors (~90 GiB). However, /dev/vda1 is still locked at sector 79,509,503.
Step 3: Clearing the Obstacle (Removing the Blocking Partition)
As established in Part 1, partitions must be contiguous. Because /dev/vda2 (the swap partition) sits immediately after /dev/vda1, we cannot stretch /dev/vda1 until /dev/vda2 is removed.
First, turn off active swap memory so the kernel releases the device lock:
Bash
sudo swapoff -a
Next, open fdisk targeting the main block device:
Bash
sudo fdisk /dev/vda
Inside the interactive fdisk shell, delete the blocking swap container and the root partition:
- Type
dand select partition5(deletes the logical swap partition). - Type
dand select partition2(deletes the extended partition container). - Type
dand select partition1(deletes the primary root partition).
Don’t Panic: Deleting a partition table entry inside
fdiskonly alters the disk’s index map in memory—it does not erase the underlying ext4 data filesystem written on those sectors.
Step 4: Re-creating the Partition Table Entry
Now we will recreate partition 1 using the exact same starting sector, but extending the end sector out to the full boundary of the disk.
Inside the same fdisk prompt:
- Type
nfor a new partition. - Type
pto make it a Primary partition. - Select Partition number
1. - First sector: Type
2048(This must match your original starting sector noted in Part 1 to prevent data corruption!). - Last sector: Press Enter to automatically select the maximum available sector (
188743679).
The Critical Signature Prompt
During this step, fdisk will detect your existing filesystem and ask a crucial question:
Plaintext
Created a new partition 1 of type 'Linux' and of size 87.9 GiB.
Partition #1 contains a ext4 signature.
Do you want to remove the signature? [Y]es/[N]o:
⚠️ Type N (No)!
If you type Y, fdisk will wipe the ext4 filesystem metadata header, resulting in complete data loss. Typing N preserves your existing data while updating the boundary markers.
Finally, write the updated partition table to disk:
- Type
wand press Enter.
Step 5: Informing the Kernel & Resizing ext4
After exiting fdisk, force the kernel to re-read the updated partition table map without rebooting:
Bash
sudo partprobe /dev/vda
Lastly, expand the ext4 filesystem layer to claim the new partition space:
Bash
sudo resize2fs /dev/vda1
Plaintext
resize2fs 1.47.0 (05-Feb-2023)
Filesystem at /dev/vda1 is mounted on /; on-line resizing required
old_desc_blocks = 5, new_desc_blocks = 11
The filesystem on /dev/vda1 is now 23041945 (4k) blocks long.
Verify your new available disk space:
Bash
df -h /
Plaintext
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 87G 35G 48G 43% /
What’s Next?
We have successfully expanded our primary partition from 37.9 GB to 87 GB on a live system without losing any data. However, in deleting /dev/vda5, we removed our system’s swap space entirely.
In Part 3, we will finish modernizing our Linux storage architecture by setting up a lightweight, dynamic Swap File inside /dev/vda1 and cleaning up /etc/fstab to keep system boot times lightning fast.
Stay tuned!