How to mount a Virtualbox vdi with LVM

January 20, 2024 - Reading time: 3 minutes

To mount a Virtualbox *.vdi file, you need to convert it to an image first.

Before that you need to find the details of it.

VBoxManage showhdinfo ubuntu.vdi

This will give you an output like below

UUID: f77dd722-5112-4a9c-9961-baca7ec93ae3
Parent UUID: base
State: created
Type: normal (base)
Location: /home/lazycoder/VirtualBoxVMs/Elementary/Elementary.vdi
Storage format: VDI
Format variant: dynamic default
Capacity: 20480 MBytes
Size on disk: 12674MBytes
Encryption: disabled
Property: AllocationBlockSize=1048576

In order now to convert the *.vdi, we need to convert it to an image file *.img

VBoxManage clonehd --format RAW Elementary.vdi Elementary.img

Now you need to find the type of filesystem is in the image you just created.

sudo fdisk -l Elementary.img

The output should be like below, note the filesystem type is Linux, this means it is ext3 or ext4

Disk Documents/Elementary.img: 20 GiB, 21474836480 bytes, 41943040 sectors
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: dos
Disk identifier: 0xf4e707ae
Device Boot Start End Sectors Size Id Type
Documents/Elementary.img1 * 4096 2542966 2538871 1.2G 83 Linux
Documents/Elementary.img2 2542968 41938942 39395975 18.8G 83 Linux

At this point, if you try and mount this image, it will give an error like below:

sudo mount -t ext3 -o loop,rw ./Elementary.img /mnt
mount: /mnt: wrong fs type, bad option, bad superblock on /dev/loop0, missing codepage or helper program, or other error.

So there is something wrong with the image, the way we find out about the image is to run fdisk -l on it and see what is actually in the img file. Run

sudo fdisk -l Elementary.img

So now the output is below...

Disk Elementary.img: 20 GiB, 21474836480 bytes, 41943040 sectors
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: dos
Disk identifier: 0xf4e707ae

Device          Boot   Start      End  Sectors  Size Id Type
Elementary.img1 *       4096  2542966  2538871  1.2G 83 Linux
Elementary.img2      2542968 41938942 39395975 18.8G 83 Linux

You notice there is the Elementary.img1 and Elementary.img2, now we have to mount the bigger of the two because that is the root filesystem, the other is the swap partition. To mount the Elementary.img2 we need to do the below:

So now we know the layout of the image, we can mount the partition Elementary.img2 by setting the offset

We find the offset by the below formula

Sector size * Start = (in the case) 512 * 254968 = 1301999616

So now we set up the loop filesystem as below

sudo losetup -o 28672 /dev/loop0 sda.img

That will create /dev/loop0 for us and now you can operate on it.

LazyCoderOZ

I am a Linux guy, been around for 20+ years using Linux as my daily driver.
This is my blog on my discoveries and notes so I don't forget how I have done things :)