This will probably work in most Linux distributions, but this is how I did it in Debian 8 Jessie.
Install qemu-utils:
sudo apt-get install qemu-utils
Load nbd kernel module:
sudo rmmod nbd (only necessary if nbd is already loaded) sudo modprobe nbd max_part=16 (/dev/nbd0 to /dev/nbd15 will appear after that command)
Create and mount a new VDI image:
qemu-img create -f vdi test.vdi 120G
Connect to the image:
sudo qemu-nbd -c /dev/nbd0 test.vdi sudo partprobe /dev/nbd0
Create a primary partition in the image using fdisk:
sudo fdisk /dev/nbd0 (type: n enter p enter enter enter enter w) (/dev/nbd0p1 will appear after that last command)
Create an ext4 file system on the image’s first partition:
sudo mkfs.ext4 /dev/nbd0p1
Mount the VDI image partition:
sudo mount /dev/nbd0p1 /mnt
Mount an existing VDI image:
Assuming it’s got a formatted partition already.
Connect and mount the image:
sudo qemu-nbd -c /dev/nbd0 test.vdi (/dev/nbd0p1 will appear after that command if the image contains a partition) sudo mount /dev/nbd0p1 /mnt
Dismount and disconnect the image:
sudo umount /mnt sudo qemu-nbd -d /dev/nbd0 (/dev/nbd0p1 will disappear after the last command)