How to mount a virtual hard disk?
Solution 1
According to this wikibooks article:
Linux and other Unix-like hosts can mount images created with the raw format type using a loopback device. From a root login (or using sudo), mount a loopback with an offset of 32,256.
mount -o loop,offset=32256 /path/to/image.img /mnt/mountpoint
For other types of qemu images, you can use qemu-nbd
modprobe nbd max_part=16 qemu-nbd -c /dev/nbd0 image.qcow2 partprobe /dev/nbd0 mount /dev/nbd0p1 /mnt/image
Plus, usually, you can convert image from one format to another.
raw - (default) the raw format is a plain binary image of the disc
image, and is very portable.
On filesystems that support sparse files,
images in this format only use the
space actually used by the data recorded in them.
cloop - Compressed Loop format, mainly used for reading Knoppix
and similar live CD image formats
cow - copy-on-write format, supported for historical reasons only and
not available to QEMU on Windows
qcow - the old QEMU copy-on-write format, supported for
historical reasons and superseded by qcow2
qcow2 - QEMU copy-on-write format with a range of special features,
including the ability to take multiple snapshots, smaller
images on filesystems that don't support sparse files,
optional AES encryption, and optional zlib compression
vmdk - VMware 3 & 4, or 6 image format, for exchanging images
with that product
vdi - VirtualBox 1.1 compatible image format, for exchanging
images with VirtualBox.
I found this solution for (VirtualBox) .VDI when I searched, on this website:
modprobe nbd max_part=16 qemu-nbd -c /dev/nbd0 /path/to/some.vdi mount -o loop /dev/nbd0p1 /mnt # do stuff umount /mnt qemu-nbd -d /dev/nbd0 rmmod nbd
The same as "Qemu's way" commands. No borders!
Solution 2
This is on Ubuntu 16.04.
Install and mount using affuse
:
sudo apt-get install afflib-tools
sudo affuse /path/file.vmdk /mnt/vmdk
Check sector size:
sudo fdisk -l /mnt/vmdk/file.vmdk.raw
# example
Disk file.vmdk.raw: 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: 0x000da525
Device Boot Start End Sectors Size Id Type
/mnt/vmdk/file.vmdk.raw1 * 2048 41943039 41940992 20G 83 Linux
Multiply sectorsize and startsector. In this example it would be 2048*512:
$ echo 2048*512 | bc
1048576
Mount using that offset:
sudo mount -o ro,loop,offset=1048576 /mnt/vmdk/file.raw /mnt/vmdisk
Disk should now be mounted and readable on /mnt/vmdisk
Solution 3
You can also use qemu:
For .vdi
sudo modprobe nbd
sudo qemu-nbd -c /dev/nbd1 ./linux_box/VM/image.vdi
if they are not installe you can install them (on Ubuntu is this comand)
sudo apt install qemu-utils
and then mount it
mount /dev/nbd1p1 /mnt
For .vmdk
sudo modprobe nbd
sudo qemu-nbd -r -c /dev/nbd1 ./linux_box/VM/image.vmdk
notice tha I use the option -r
that's because VMDK version 3 must be read only to be able to be mounted by qemu
and then I mount it
mount /dev/nbd1p1 /mnt
I use nbd1
because nbd0
sometimes gives 'mount: special device /dev/nbd0p1 does not exist'
For .ova
tar -tf image.ova
tar -xvf image.ova
The above will extract the .vmdk
disk and then mount that.
Solution 4
For vmdk
and vhd
files, I only got lucky with the kpartx
command below:
sudo kpartx -a -v <image-flat.vmdk>
Check the output for the losetup
, it should contain loop device /dev/loop0
; also check sudo blkid
for partition /dev/mapper/loop0p1
, then use it in the mount command:
sudo mount -o rw /dev/mapper/loop0p1 /mnt/vmdk
Where /mnt/vmdk is your mount point, to be created with sudo mkdir /mnt/vmdk
if non-existent.
source at commandlinefu.com (kpartx and mount command)
Unmount with:
sudo umount /mnt/vmdk
sudo kpartx -d -v <image-flat.vmdk>
Related videos on Youtube
beginner
Updated on September 18, 2022Comments
-
beginner almost 2 years
I was trying to use basic php to insert data into a mysql table from an html form. i was succesfull initially with basic plain text inputs. However when i was trying to input an email id i keep getting this error :
Could not enter data: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@gmail.com,moohawks )' at line 3
My php code is as follows:
$firstname = $_REQUEST['firstname']; $lastname = $_REQUEST['lastname']; $emailid = $_REQUEST['emailid']; $team_name = $_REQUEST['team_name']; $dbhost = 'localhost'; $dbuser = 'root'; $dbpass = 'incredible'; $conn = mysql_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('Could not connect: ' . mysql_error()); } $sql = "INSERT INTO users (firstname,lastname,emailid,teamname) VALUES ( $firstname,$lastname,$emailid,$team_name )"; mysql_select_db('adb project'); $retval = mysql_query( $sql, $conn ); if(! $retval ) { die('Could not enter data: ' . mysql_error()); } $message2 = "Entered data successfully\n"; mysql_close($conn); ?>
What is wrong with my insert statement. As far I can understand, it looks corrects and fairly simple. Any help?
-
Admin over 11 yearsHave you searched Google? It abounds with guides on mounting VMDK, VDI, VHD, and raw disk image files on Ubuntu.
-
Admin over 11 yearsI have searched google, but I did not find like your result. Thanks :)
-
Admin over 5 yearsUbuntugeek link for VHD above failed.
-
-
cljk over 6 yearsgreat!!! did it for me on Ubuntu 17.10
-
Theodore Murdock almost 6 yearsThis is not working for me on 16.04.5 for my .vmdk...works through the fdisk step, and the main partition of my VM, the one I want to mount, also starts at 2048, but
mount -o ro,loop,offset=1048576 ./foo.raw /mnt/foo
fails withonly root can use "--options" option
. Withsudo
, it fails withfailed to setup loop device: Permission denied
. -
N0rbert over 5 yearsJust tested this method with
vhd
, it works!