qemu - nicktehrany/notes GitHub Wiki

These are some of my qemu notes

Resizing root file system

Resizing the root partition is fairly simple as long as we don't corrupt it.

Resize the qemu image

On the host machine resize the qemu image with

./qemu-img resize ubuntu-22.04.qcow2 +10G

Identify the device

In the VM we now need to update the partition table to increase the partition size. This is done with

user@stosys:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
tmpfs           2.0G 1008K  2.0G   1% /run
/dev/sda2        30G   25G  3.7G  87% /
tmpfs           9.8G     0  9.8G   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           2.0G     0  2.0G   0% /run/user/1000

where here we want to increase the size of /dev/sda2 for the root dir.

Update the partition table

Since we only want to increase the partition size, we can delete its partition and create a new one with the larger size. Since the partition will start at the same location we can do this without corrupting it. This is the entire output for the operation with fdisk. First we can see that it identifies the size mismatch. We then delete the partition, create a new one with all default options which selects the entire size, and ensure to NOT remove the signature. Lastly, we write out the changes. If you mess up anywhere restart this without writing it out, it hangs out in memory until then, hence is not persisted.

user@stosys:~$ sudo fdisk /dev/sda

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

GPT PMBR size mismatch (62914559 != 83886079) will be corrected by write.
The backup GPT table is not on the end of the device. This problem will be corrected by write.
This disk is currently in use - repartitioning is probably a bad idea.
It's recommended to umount all file systems, and swapoff all swap
partitions on this disk.


Command (m for help): p

Disk /dev/sda: 40 GiB, 42949672960 bytes, 83886080 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: gpt
Disk identifier: DBC829B3-89F5-44FF-9F1C-DACEB5859B60

Device     Start      End  Sectors Size Type
/dev/sda1   2048     4095     2048   1M BIOS boot
/dev/sda2   4096 62912511 62908416  30G Linux filesystem

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

Partition 2 has been deleted.

Command (m for help): n
Partition number (2-128, default 2):
First sector (4096-83886046, default 4096):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (4096-83886046, default 83886046):

Created a new partition 2 of type 'Linux filesystem' and of size 40 GiB.
Partition #2 contains a ext4 signature.

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

Command (m for help): p

Disk /dev/sda: 40 GiB, 42949672960 bytes, 83886080 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: gpt
Disk identifier: DBC829B3-89F5-44FF-9F1C-DACEB5859B60

Device     Start      End  Sectors Size Type
/dev/sda1   2048     4095     2048   1M BIOS boot
/dev/sda2   4096 83886046 83881951  40G Linux filesystem

Command (m for help): w
The partition table has been altered.
Syncing disks.

Update File System info

We can then update the file system info by running on the new device.

sudo resize2fs /dev/sda2

Now, we can see the file system is increased in size by the 10G.

user@stosys:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
tmpfs           2.0G 1008K  2.0G   1% /run
/dev/sda2        40G   25G   14G  65% /
tmpfs           9.8G     0  9.8G   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           2.0G     0  2.0G   0% /run/user/1000