I've created a couple of Debian KVM/QEMU Openwrt x86_64 ext4 VMs, one squashfs and one ext4. I'm trying Docker and Podman with Openwrt as host and so making larger 16GB disks. Both are the UEFI images. From reading several of the help docs to expand the disks offline I am doing:
Download "combined" firmware for ext or squashfs, bios or uefi.
Decompress the zip
Set a variable to the image filename less the img extension eg:
$ OpenWrtImg=openwrt-x86-64-generic-squashfs-combined-efi
Expand the disk to the required size:
$ qemu-img resize -f raw ${OpenWrtImg}.img 16G
Mount to a loop device:
$ loop_device=$(sudo losetup -f)
$ sudo losetup $loop_device ${OpenWrtImg}.img
For any Combined EFI image:
Fix the GPT partition
$ echo -e "OK\nFix" | sudo parted ---pretend-input-tty "$loop_device" print
Increase the root partition size to 100%
Either:
$ sudo cfdisk /dev/loop0 # select partition 2 (About 100MB) resize, then write.
or alternatively:
$ sudo parted "$loop_device" resizepart 2 100%
$ sudo parted "$loop_device" print
Detach loop device:
$ sudo losetup -d /dev/loop0
Convert the disk to qcow2:
$ qemu-img convert -O qcow2 ${OpenWrtImg}.img ${OpenWrtImg}.qcow2
Copy the qcow2 to the KVM QEMU image pool at /var/lib/libvirt/images
$ sudo cp ${OpenWrtImg}.qcow2 /var/lib/libvirt/images/
$ sudo chown libvirt-qemu:libvirt-qemu /var/lib/libvirt/images/${OpenWrtImg}.qcow2
$ sudo chmod 640 /var/lib/libvirt/images/${OpenWrtImg}.qcow2
To check it looks correct.
$ sudo ls -l /var/lib/libvirt/images/
Create VM within the graphical virt-manager. Boot. Make other config changes at the console.
The above appears to work for the squashfs image but not for the ext4 image. I have made it work for ext4 by expanding the image, booting with it and then using this to expand the disk online, but I was expecting to be able to do this all offline. Can someone help me with what I am missing from the above for the Ext4 image. The online version does this:
ROOT_BLK="$(readlink -f /sys/dev/block/"$(awk -e
'$9=="/dev/root"{print $3}' /proc/self/mountinfo)")"
ROOT_DISK="/dev/$(basename "${ROOT_BLK%/}")"
ROOT_PART="${ROOT_BLK##[^0-9]}"
parted -f -s "${ROOT_DISK}"
resizepart "${ROOT_PART}" 100%
mount_root done
touch /etc/rootpt-resize
if [ -e /boot/cmdline.txt ]
then
NEW_UUID=blkid ${ROOT_DISK}p${ROOT_PART} | sed -n 's/.*PARTUUID="\([^"]*\)".*/\1/p'
sed -i "s/PARTUUID=[^ ]*/PARTUUID=${NEW_UUID}/" /boot/cmdline.txt
fi
reboot
fi
exit 1
and then this:
ROOT_BLK="$(readlink -f /sys/dev/block/"$(awk -e
'$9=="/dev/root"{print $3}' /proc/self/mountinfo)")"
ROOT_DEV="/dev/${ROOT_BLK##*/}"
LOOP_DEV="$(awk -e '$5=="/overlay"{print $9}'
/proc/self/mountinfo)"
if [ -z "${LOOP_DEV}" ]
then
LOOP_DEV="$(losetup -f)"
losetup "${LOOP_DEV}" "${ROOT_DEV}"
fi
resize2fs -f "${LOOP_DEV}"
mount_root done
touch /etc/rootfs-resize
reboot
fi
exit 1