How to convert a large dynamic virtualbox VDI image to a small image file to write it into a USB drive as a portable installation?
Solution 1:
You have to compact the VDI disk file first by using command below:
VBoxManage modifymedium xubuntu.vdi --compact
See 8.22 VBoxManage modifymedium of http://download.virtualbox.org/virtualbox/6.1.22/UserManual.pdf for details.
Then convert it to RAW using command below:
qemu-img convert xubuntu.vdi -O raw xubuntu.raw
If the resulting RAW file is still big, then you have to shrink it more by using Gparted (move, resize partitions here) using commands below:
sudo losetup -P /dev/loop137 xubuntu.raw
gparted /dev/loop137
After shrinking the partition with GParted, you can unmount the image.
sudo losetup -d /dev/loop137
You can check the actual partitions inside RAW image by running fdisk -l xubuntu.raw
.
Then you can shrink the RAW image to fit its partitions:
qemu-img resize --shrink xubuntu.raw 9216M
Then finally test the image in VM
kvm -m 2048 -drive file=xubuntu.raw,format=raw
and then write it to USB drive using GNOME Disks or Etcher or dd
or ddrescue
(I prefer it over plain dd
, as it shows progress, time estimate and speeds).
Then, open Gparted, and extend the partition in the USB drive to the maximum possible size. Otherwise, the root partition would be stuck at (let's say) 9 GB, even if the size of the USB drive is (let's say) 32 GB, and you won't be able to install new programs despite having free space on the drive.
Note: if USB drive shows missing UUID error (as the UUID may change), then you can boot with the fallback mode in the Advanced option in the GRUB menu.