Score:4

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?

us flag

I have a VirtualBox dynamic image of Xubuntu 20.04 of maximum size 70 GB. The current size of the image is 9 GB.

When I try to convert it to a .img file with the command VBoxManage clonehd 'xubuntu.vdi' xubuntu.img --format RAW

the image size keeps growing far beyond 9 GB, and it would probably go on until 70 GB (I stopped the process when it crossed 20 GB).

How to turn it into an image of size 9 GB, so that later on I can write to a 16 GB USB drive as a portable install?

The host OS is Kubuntu 20.04.

Score:5
zw flag

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.

us flag
How do I write the raw image to an USB device? I know how to write an iso or img to USB using `dd` or Etcher.
N0rbert avatar
zw flag
Simplest way is to use GNOME Disks. CLI way is to use `dd` or `ddrescue` (I prefer it, as it shows progress, time estimate and speeds).
us flag
I did not have GNOME disks, I found that Etcher works too. I have edited your answer.
Lorenz Keel avatar
gr flag
just for my curiosity: the number 137 in `/dev/loop137` is just for using a number that at 99.9999% is not still in use, right?
N0rbert avatar
zw flag
Yes, @LorenzKeel , a kind of. System may have many snaps, so it is random big value :)
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.