Skip to content

Instantly share code, notes, and snippets.

@PedroCavaleiro
Last active February 23, 2024 16:01
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save PedroCavaleiro/43e4a19a6bec21bc7c587b3bbb966265 to your computer and use it in GitHub Desktop.
Save PedroCavaleiro/43e4a19a6bec21bc7c587b3bbb966265 to your computer and use it in GitHub Desktop.
Mount VMDK on Linux

Read VMDK on Linux

VMDK is a virtual disk file from VMWare, reading without any "special" software is rather useful specially because I didn't want to install VMWare software

It's possible that this works with vhd and vhdx but I didn't test it... If someone does test it let me know

The steps are rather easy

Mounting VMDK

  1. Get Loopback devices

    You first need to see what Loopback devices are available on your system, in my case loop30 is usually available so I use that

    To view which loopback devices are in use just type sudo fdisk -l | grep /dev/loop on your terminal or df -h | grep /dev/loop

  2. Create a mount point

    Just create a mount point, as I usually only work with one VMDK at the time I created the mount point vmdk so the command should be sudo mkdir /mnt/vmdk

  3. Create the loopback device

    In my case the I have loopback devices from 0 to 21 (loop0 - loop21) so I will use loop30 and associalte with the vmdk file. To note that it must be the diskname-flat.vdmk file where diskname is your disk name.

    To create the loopback device type sudo losetup /dev/loop30 /mnt/storage/vm-flat.vmdk where /mnt/storage/vm-flat.vmdk is the path to your vmdk file.

  4. Set the offset

    Instead of performing calculations just get the offset using parted for that follow the following steps

    1. sudo parted /dev/loop30

    2. unit

    3. B

    4. print

    5. quit

    6. Get the offset from the output After the command print you should have something like this

      Model: Loopback device (loopback)
      Disk /dev/loop30: 118111600640B
      Sector size (logical/physical): 512B/512B
      Partition Table: gpt
      Disk Flags: 
      
      Number  Start       End            Size           File system  Name  Flags
       1      1048576B    537919487B     536870912B     fat32              boot, esp
       2      537919488B  118110552063B  117572632576B  ext4
      

      In my case I want to access the second partition so I grab the start value for that partition which is 537919488 remember to remove the B at the end of the number

    7. Finally create a new loopback device for that partition sudo losetup -o 537919488 /dev/loop31 /dev/loop30 where loop30 is the loopback device to which the VMDK is associated and loop31 is the new loopback dive to which the partition will be associated

  5. Mount a new loopback device on the new mount point

Mount a new loopback device, I'll be using loop31 which is associated with the partition and /mnt/vmdk as a mount point.

So to perform the mount type sudo mount /dev/loop31 /mnt/vmdk

And you can now access to your VMDK file

Unmount VMDK

After usage you should perform a unmount and loopback cleanup these steps are even more easy.

  1. sudo umount /mnt/vmdk where /mnt/vmdk is your mount point. Ensure that your system isn't using anything on this mount point
  2. sudo losetup -d /dev/loop31
  3. sudo losetup -d /dev/loop30

On the steps 2 and 3 ensure that you remove the partition loopback first and then the disk loopback. Remember to use your loopXX devices that you used during the mount process.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment