Skip to content

qemu setup commands

Krijn Doekemeijer edited this page Oct 3, 2023 · 2 revisions

Resizing root file system

Resizing the root partition is fairly simple as long as we don't corrupt it. First, we need to resize the backing image on the host machine.

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

Next we can go back into the VM and update the partition table for the device. Here we want to update the table for /dev/sda2 which is holding the root file system.

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

Since we only want to increase the partition size, we can delete its partition and create a new one with the larger size. Because 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 until the w command.

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.

Lastly, we need to update the file system info by running on the corresponding device.

sudo resize2fs /dev/sda2

Now, we can see the root file system has 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

Setup passthrough for NVMe

Bind NVMe drive to vfio-pci

ls -l /sys/block/nvme2n2/device/device
# lrwxrwxrwx 1 root root 0 Feb 17 15:26 /sys/block/nvme2n2/device/device -> ../../../0000:b0:00.0
echo "0000:b0:00.0" | sudo tee /sys/bus/pci/drivers/nvme/unbind
sudo modprobe vfio-pci
lspci -n -s 0000:b0:00.0
# b0:00.0 0108: 1b96:2600
echo 1b96 2600 | sudo tee /sys/bus/pci/drivers/vfio-pci/new_id
# Might error if already exists, ignore
echo "0000:b0:00.0" | sudo tee /sys/bus/pci/drivers/vfio-pci/bind

Add the following to your qemu-system-x86 command:

-device vfio-pci,host=0000:b0:00.0

Setup virtio for paravirtualized NVMe

Add the following to your qemu-system-x86 command:

-blockdev node-name=drive0,driver=host_device,filename=/dev/nvme7n1,cache.direct=on,locking=off -device virtio-blk-pci,drive=drive0

This command works both for NVMe and ZNS.

Clone this wiki locally