How can I attach a VHDx or VHD file in Linux? [closed]
Asked Answered
H

1

55

How can I attach a VHDX or VHD file in Linux?

I mean attach the virtual hard disk as a block device, and use external tools to read these devices.

The filesystem inside is not mountable. I do not need to mount the filesystem, but deal with it as if it was on a real hard disk.

I read the manual page of guestfish, but could not find how to do it.

Hurt answered 24/4, 2016 at 5:4 Comment(1)
Another ressource: how2shout.com/linux/…Bioastronautics
E
77

You can use libguestfs-tools to achieve this.


  1. First, you need to install it, on Ubuntu/Debian-like Linux that would be:

    sudo apt-get install libguestfs-tools
    
  2. Then, you can mount almost whatever you wish:

    guestmount --add yourVirtualDisk.vhdx --inspector --ro /mnt/anydirectory
    

    This is just an example of read-only extraction point.


Hints:

  1. Run it as normal user, i.e.:

    guestmount ...
    

    Instead of:

    sudo guestmount ...
    
  2. Switches; citations from man page:

    --add
    

    Add a block device or virtual machine image.

    --inspector
    

    Using virt-inspector(1) code, inspect the disks looking for an operating system and mount filesystems as they would be mounted on the real virtual machine.

    --ro
    

    Add devices and mount everything read-only. Also disallow writes and make the disk appear read-only to FUSE.

Edmon answered 13/6, 2016 at 12:57 Comment(5)
When the vhd file is too big (say 1TB with millions of files), guestmount could successfully mount it but fail in reading files (you can't even ls -1f to see the files). This must be an unsolved bug. Anyone who encounters this problem can try qemu.Dichroic
And guestmount (with no args at all) just segfaults on me (after newly installed here on a Raspbian Jessie).Rutabaga
Thanks for bringing up guestmount which I hoped would allow me to mount my .vhd containing a single partition with a exFAT-volume inside. Sadly, guestmount (v1.38.4) did not accept my specification of the volume within the .vhd and asking me Did you mean to mount one of these filesystems? ... /dev/sda1 (exfat). What I had specified was -m "/dev/sda1" as this was what virt-filesystems had returned. When the volume is NTFS, mounting works. And yes, I've got exfat-utils.x86_64 (v1.2.8, a fuse-fs-driver) installed.Portsalut
I can confirm this works for VHD(x) images downloaded from Azure VMs.Margertmargery
If anyone is trying to do this on Windows via WSL, this web page has the additional info you need to complete the task: blog.codybunch.com/2020/10/16/WSL2-Mount-vhdx-to-WSL2Gearard

© 2022 - 2024 — McMap. All rights reserved.