Extend a wic image for use in QEMU
I had a problem the other day of running a .wic
image with QEMU and it could not download any docker images because there was no space. I looked online for ways to make it possible to have a bigger disk image either with wic
or just another format, like wic
to qcow2
maybe, but I could not find anything that matched my case.
So like any other software engineer/hacker I started experimenting and came up with a solution.
Raw disk
First I created a raw disk using the following command:
$ qemu-img create -f raw disk.img 6G
This will create a file called disk.img
that is 6 Gigabytes big, and it will be the extra space we want minus the size of the wic
image. I did not need a lot more, so if you do need more then just increase the 6 to whatever you need.
Merging the two
Then I merged/concatenated the two together. The caveat is the reason I think this worked is because I happened to extend the last partition of the wic
file, and therefore I can join them together like this.
$ dd conv=notrunc if=lmp-factory-image-qemuarm64-secureboot.wic of=disk.img bs=4M iflag=fullblock oflag=direct status=progress
This will make it so that the first x bytes will be written over by the wic
file, in my case around 2Gb. So inside the raw disk.img
it is basically first the wic
followed by raw disk space, unpartitioned free space.
Booting QEMU
With the following command I could start my QEMU again.
$ qemu-system-aarch64 -m 1024 -cpu cortex-a57 -no-acpi -bios flash.bin \
-device virtio-net-device,netdev=net0,mac=52:54:00:12:35:02 -device virtio-serial-device \
-drive id=disk0,file=disk.img,if=none,format=raw \
-device virtio-blk-device,drive=disk0 -netdev user,id=net0,hostfwd=tcp::2222-:22 \
-object rng-random,filename=/dev/urandom,id=rng0 -device virtio-rng-pci,rng=rng0 \
-chardev null,id=virtcon -machine virt,secure=on -nographic
Note the disk.img
being used, and not the wic
image. The other options are not really of interest right now, but just tweaks that add things like the ability to SSH into the device through port-forwarding and such.
Resizing the partition
Now comes the slightly scary part since we are going to live edit the partition inside QEMU. All these commands have to be run inside the QEMU running image.
$ sudo fdisk /dev/vda
# This will get you inside the fdisk menu, the following are pressed inside the menu
$ p
# note the start sector of the last partition, in my case /dev/vda3
$ d
# here press the number of the last partition, in my case 3
$ 3
$ n
# here press the same number of the last partition
$ 3
# change start sector to one noted before
$ <start sector>
# also make sure the end sector is the new biggest value possible
$ w
This will delete the partition in question, and recreate it but this time the start sector is the same, but the end sector is the new end of the disk.img
size. Then one more command is needed:
$ sudo resize2fs /dev/vda3
That will resize the actual partition to be the bigger size.
Have fun testing.