VMFS Recovery™
Recover data from damaged or formatted VMFS disks or VMDK files
Recover data from damaged or formatted VMFS disks or VMDK files
Last updated: Nov 11, 2024

Mount VHDX Linux: A Complete Guide to Accessing Virtual Hard Disk Files in Linux

Virtual Hard Disk (VHD) and Virtual Hard Disk Extended (VHDX) files are commonly used in virtualization platforms like Hyper-V to store virtual machine data. However, accessing and managing these files on a Linux system can be challenging, especially if you're unfamiliar with the necessary tools and commands. Fortunately, Linux offers a range of methods to mount VHD and VHDX files, allowing you to access, modify, and even recover data directly from these virtual drives. In this step-by-step guide, we’ll walk you through the simplest ways to mount VHD and VHDX files on a Linux environment, providing clear instructions and useful tips to make the process straightforward and efficient. Whether you're looking to retrieve specific files or manage virtual storage, this guide will help you seamlessly integrate VHD and VHDX mounting into your Linux workflow.

In this article you will find out:

Are you ready? Let's read!

What is a VHDX File?

VHD (Virtual Hard Disk) and VHDX (Virtual Hard Disk Extended) are file formats used to represent virtual hard disks. Developed by Microsoft, these formats are widely used in virtualization environments, particularly with Hyper-V, a hypervisor that allows multiple virtual machines (VMs) to run on a single physical server. The primary purpose of these formats is to encapsulate and manage the storage needs of virtual machines, creating isolated environments where each VM has its own dedicated storage.

What is VHD? The VHD format was the original standard, offering a maximum storage capacity of 2 TB. However, as the need for larger storage grew, VHDX was introduced as an enhanced format, supporting up to 64 TB of storage and offering improved resilience to data corruption, enhanced performance, and better compatibility with modern workloads.

Use Cases in Virtualization

VHD and VHDX files play an essential role in virtualization environments, particularly with Hyper-V. They allow organizations to create, configure, and manage virtual machines by isolating the operating system, applications, and data within a single file. This capability is invaluable for tasks like server consolidation, where multiple virtual machines share physical resources, reducing hardware costs and improving resource efficiency.

Beyond virtualization, VHD and VHDX files are useful for creating VHD backup, testing configurations, and securely isolating software environments. These files can also be mounted on different systems, making them versatile tools for data transfer and file recovery scenarios.

Differences Between VHD and VHDX

While both formats serve similar purposes, VHDX offers several enhancements over the older VHD format:

  • Capacity: VHD supports up to 2 TB, while VHDX can store up to 64 TB, catering to modern storage requirements.
  • Performance: VHDX is optimized for larger storage capacities and provides better performance in virtualized environments.
  • Data Protection: VHDX includes built-in protection against data corruption, making it a more resilient choice for critical applications.
  • Compatibility: VHD is compatible with older versions of Hyper-V and third-party virtualization tools, while VHDX is typically used with newer Hyper-V versions (Windows Server 2012 and beyond).

Common Scenarios for VHD and VHDX

These virtual disk formats are commonly used in Hyper-V environments, enabling administrators to create flexible and scalable virtual infrastructures. Another popular scenario is file recovery: mounting VHD and VHDX files on a Linux system allows users to access and recover data from virtual disks when issues arise, such as system failures or virtual machine corruption. Additionally, VHD and VHDX files are valuable in development and testing environments, where they offer isolated and easily restorable snapshots for different software setups.

Prerequisites: Preparing Your Linux Environment

Before you can mount VHD and VHDX files on a Linux system, you’ll need to ensure your environment is properly set up with the necessary tools and dependencies. Linux requires specific utilities to manage and interact with these file types, particularly for handling the VHDX format. Here’s how to prepare your Linux environment to seamlessly mount and access VHD and VHDX files.

Step 1: Install qemu-utils and Necessary Dependencies

The qemu-utils package is essential for working with VHD and VHDX files on Linux. This package includes the qemu-nbd (Network Block Device) utility, which enables you to mount virtual disk images, including VHD and VHDX formats, by connecting them as network block devices.

To install qemu-utils, open a terminal and enter the following commands: update your system with sudo apt update, then install qemu-utils with sudo apt install qemu-utils. This installs qemu-nbd along with other dependencies required to handle virtual disk formats.

Step 2: Load the nbd Kernel Module

For qemu-nbd to work correctly, your Linux system needs the Network Block Device (NBD) kernel module. This module allows Linux to treat VHD and VHDX files as block devices, which can then be mounted as if they were physical disks.

To load the nbd module, enter sudo modprobe nbd max_part=8in the terminal. The max_part=8 parameter defines the maximum number of partitions that can be mounted from the image. Adjust this number if your VHDX file has more than eight partitions.

Step 3: Verify File System Support

Linux must support the file system used in your VHD or VHDX file, which could be NTFS, ext4, FAT32, or others. The most common case, especially with Hyper-V VMs, is NTFS. To ensure support for NTFS, you can install ntfs-3g, a popular package for NTFS file systems on Linux. To do this, use sudo apt install ntfs-3g, which enables read and write access to NTFS-formatted partitions.

Step 4: Additional Tools for VHDX Handling

In addition to qemu-utils, having libguestfs-tools installed can be helpful, especially if you need to inspect or modify the content of VHD or VHDX files. This package provides tools like guestmount and virt-inspector, which offer more advanced options for managing virtual disks.

Install libguestfs-tools by entering sudo apt install libguestfs-tools.

Step 5: Verify Dependencies and Compatibility

Once all tools are installed, verify your Linux environment's compatibility by listing the installed modules and confirming that qemu-nbd and nbd are functioning. This setup ensures that your Linux system is ready to handle VHD and VHDX files smoothly.

By completing these steps, your Linux environment will be fully prepared to mount, inspect, and interact with VHD and VHDX files, enabling easy access and management of virtual storage.

Tip: recover VHDX to physical disk

How to Mount VHDX Files in Linux

Mounting a VHDX file on Linux requires a few steps, mainly using the qemu-nbd utility and the Network Block Device (NBD) kernel module. Here’s a clear, step-by-step guide to attaching and mounting a VHDX file, as well as troubleshooting common issues.

Step 1: Attach the VHDX File as a Network Block Device

  1. 1. Load the NBD Kernel Module:


Start by loading the NBD kernel module with the following command to ensure the system can treat the VHDX as a block device. Use sudo modprobe nbd max_part=8. The max_part=8 parameter allows up to eight partitions, which should be sufficient in most cases.

  1. 2. Attach the VHDX File Using qemu-nbd:


Attach the VHDX file to a network block device using the qemu-nbd command. Run sudo qemu-nbd --connect=/dev/nbd0 /path/to/your/file.vhdx, where /dev/nbd0 is the block device being assigned to the VHDX file. Replace /path/to/your/file.vhdx with the actual file path.

Step 2: Scan the Device for Partitions

After attaching the VHDX file, it’s necessary to scan for partitions within it.

  1. 1. Rescan Partitions:


Once the VHDX is connected as a block device, scan it for partitions using the command sudo partprobe /dev/nbd0. This command ensures that Linux recognizes the partitions within the VHDX file and assigns device names to them (e.g., /dev/nbd0p1 for the first partition).

  1. 2. Verify Partitions:


Confirm the partitions are recognized by listing the block devices with lsblk. You should see the nbd0 device along with any partitions listed (e.g., nbd0p1, nbd0p2).

Step 3: Mount the Partition

Once the partitions are recognized, you can mount them as you would with any other disk.

  1. 1. Create a Mount Point:


Create a directory to serve as the mount point, such as sudo mkdir /mnt/vhdx_mount.

  1. 2. Mount the Partition:


Mount the desired partition (e.g., nbd0p1) to the mount point by using sudo mount /dev/nbd0p1 /mnt/vhdx_mount.

You should now have access to the contents of the VHDX file at /mnt/vhdx_mount.

Step 4: Unmount and Disconnect the VHDX File

When you’re done, safely unmount and disconnect the VHDX file.

  1. 1. Unmount the Partition:


Unmount the partition with sudo umount /mnt/vhdx_mount.

  1. 2. Disconnect the Network Block Device:


Detach the VHDX file by disconnecting qemu-nbd using sudo qemu-nbd --disconnect /dev/nbd0.

  1. 3. Unload the NBD Module (Optional):


If you won’t be mounting additional network block devices, you can unload the NBD kernel module with sudo rmmod nbd.

Troubleshooting Common Errors

  1. 1. Error: “Failed to Connect to /dev/nbd0”


Ensure the nbd kernel module is loaded. Run sudo modprobe nbd max_part=8 and try again.

  1. 2. Error: “Partition Not Recognized” After qemu-nbd Connect


Use sudo partprobe /dev/nbd0 to rescan the device. If the partition still doesn’t appear, verify the integrity of the VHDX file, as it may be corrupted.

  1. 3. Mounting Issues (NTFS Partitions)


If the VHDX file uses NTFS, ensure ntfs-3g is installed. Run sudo apt install ntfs-3g if needed, then try mounting again.

Note: repair VHD in PowerShell

How to Mount VHD Files in Linux

Mounting VHD files on Linux is similar to mounting VHDX files, with a few minor distinctions. Both VHD and VHDX can be mounted using the qemu-nbd tool, but VHD files are typically smaller and lack some of the advanced features of VHDX, such as greater storage capacity and enhanced data protection. Here’s a step-by-step guide to mounting VHD files on Linux.

Step 1: Attach the VHD File as a Network Block Device

  1. 1. Load the NBD Kernel Module:


Load the NBD kernel module to allow the system to recognize VHD files as block devices. Enter sudo modprobe nbd max_part=8 in the terminal. This sets the system to recognize up to eight partitions on the block device, which is usually more than enough for VHD files.

  1. 2. Attach the VHD File Using qemu-nbd:


Use qemu-nbd to connect the VHD file to a network block device. Enter sudo qemu-nbd --connect=/dev/nbd0 /path/to/your/file.vhd, replacing /path/to/your/file.vhd with the path to your VHD file. This command assigns the VHD file to the /dev/nbd0 device.

Step 2: Scan for Partitions

Once the VHD file is attached, scan for its partitions so that Linux can recognize and mount them.

  1. 1. Rescan Partitions:


Run sudo partprobe /dev/nbd0 to detect the partitions in the VHD file. This command prompts Linux to recognize any partitions in /dev/nbd0, making them accessible as separate device names (e.g., /dev/nbd0p1).

  1. 2. Verify Partitions:


Confirm that the partitions were detected by listing the block devices with lsblk. You should see the nbd0 device, as well as any partitions (such as nbd0p1).

Step 3: Mount the Partition

With the VHD partitions recognized, you can mount them to access the contents.

  1. 1. Create a Mount Point:


Make a directory to use as the mount point, such as sudo mkdir /mnt/vhd_mount.

  1. 2. Mount the Partition:


Mount the desired partition (e.g., nbd0p1) to the mount point by entering sudo mount /dev/nbd0p1 /mnt/vhd_mount. You can now access the VHD’s contents in the /mnt/vhd_mount directory.

Step 4: Unmount and Disconnect the VHD File

When done, you can safely unmount and detach the VHD file.

  1. 1. Unmount the Partition:


Unmount the partition with sudo umount /mnt/vhd_mount.

  1. 2. Disconnect the Network Block Device:


Use sudo qemu-nbd --disconnect /dev/nbd0 to detach the VHD file from the network block device.

  1. 3. Unload the NBD Module (Optional):


If no other network block devices are needed, you can unload the nbd module with sudo rmmod nbd.

Differences Between Mounting VHD and VHDX Files

  • File Structure: VHD files have a simpler structure compared to VHDX. This means they are generally more straightforward to mount, and the likelihood of issues related to unsupported features is lower.
  • Storage Capacity: Unlike VHDX, which supports storage up to 64 TB, VHD files are limited to a maximum size of 2 TB. This difference in size doesn’t impact the mounting process but is useful to consider for larger virtual environments.
  • Compatibility: VHD files are widely compatible with both older and newer virtualization platforms, so they are often easier to mount without additional dependencies, though the same tools (qemu-nbd) can be used.

Troubleshooting Common Errors

  1. 1. Error: “Failed to Connect to /dev/nbd0”


Make sure the nbd kernel module is loaded by running sudo modprobe nbd max_part=8 again, then retry.

  1. 2. Partition Not Recognized


If partitions don’t appear after running partprobe, check the VHD file integrity or verify the file type with file /path/to/your/file.vhd to ensure it is a valid VHD.

  1. 3. Read/Write Issues


If the VHD file is formatted with NTFS, ensure ntfs-3g is installed. Use sudo apt install ntfs-3g to install if needed, then attempt mounting again.

Note: Microsoft VHDX repair tool

Mounting VHDX and VHD Files Without qemu-nbd: Using guestmount from libguestfs

If you prefer an alternative to qemu-nbd or are looking for a more user-friendly method, guestmount from the libguestfs suite provides a simpler, streamlined approach to mounting VHDX and VHD files. This tool doesn’t require loading the NBD kernel module, making it especially convenient for less technical users. Here’s how to use guestmount to mount and unmount VHDX and VHD files easily.

Advantages of Using guestmount

  • No Kernel Module Requirement: guestmount doesn’t require loading the NBD kernel module, which simplifies the setup process.
  • User-Friendly Interface: The command structure is straightforward, and guestmount automatically detects partitions, making it easier for users less familiar with Linux internals.
  • Versatility: With support for multiple file systems and virtual disk formats, guestmount is well-suited for working with various disk images and accessing files directly.

Step-by-Step Guide to Mounting VHDX or VHD Files Using guestmount

  1. 1. Install libguestfs-tools:

If libguestfs-tools isn’t already installed, set it up by running sudo apt install libguestfs-tools in the terminal.

  1. 2. Create a Mount Point:

Choose a location to mount the VHD or VHDX file. For example, create a directory by entering sudo mkdir /mnt/vhd_mount.

  1. 3. Mount the VHD or VHDX File:

Use guestmount to mount the file. The command structure is as follows:

sudo guestmount -a /path/to/your/file.vhdx -m /dev/sda1 /mnt/vhd_mount

Replace /path/to/your/file.vhdx with the path to your VHDX or VHD file. -m /dev/sda1 refers to the partition within the file; if unsure, you can use -i instead to let guestmount automatically detect the partition (ideal for beginners):


sudo guestmount -a /path/to/your/file.vhdx -i /mnt/vhd_mount

  1. 4. Access the Mounted Files:

You can now browse the contents of the VHD or VHDX file by navigating to /mnt/vhd_mount. This method provides direct access to files, making it easy to copy, modify, or recover data without extra configurations.

Properly Unmounting the VHD or VHDX File

When you’re done, it’s essential to unmount the VHD or VHDX file to avoid any data corruption.

  1. 1. Unmount the File System:


To unmount, run sudo guestunmount /mnt/vhd_mount. This command safely disconnects the file and releases any system resources it was using.

  1. 2. Verify the Unmount:


Check that the mount point is cleared by navigating to /mnt/vhd_mount. It should be empty, indicating a successful unmount.

Using guestmount is an efficient alternative for accessing VHD and VHDX files on Linux, especially for users seeking a straightforward, low-maintenance approach. It’s an excellent choice for those who want to skip the technical setup of qemu-nbd and mount virtual disks quickly and easily.

Tip: VHD recovery

File Recovery from Corrupted VHDX Files in Linux

In virtualization environments, VHDX files play a crucial role in storing the data of virtual machines (VMs). However, when these files become corrupted due to software errors, sudden shutdowns, or storage issues, it can lead to significant data loss. For IT administrators and businesses relying on virtualized environments, having reliable methods to recover corrupted VHDX files is essential to maintaining data integrity and minimizing downtime.

Corrupt VHDX files can make virtual machines unbootable or prevent access to stored data. Common causes of VHDX corruption include:

  • System Crashes: Unexpected shutdowns can lead to file corruption.
  • Hardware Failures: Disk errors and storage hardware issues can damage the VHDX structure.
  • File System Issues: Fragmented or outdated file systems on the host machine may contribute to file corruption.

When a VHDX file is corrupted, Linux might not recognize it as a valid network block device, making direct access challenging. Specialized recovery solutions become invaluable in these scenarios.

Using DiskInternals VMFS Recovery™ for VHDX File Recovery

One of the most reliable solutions to repair-VHD is DiskInternals VMFS Recovery™. Known for its powerful data recovery features, DiskInternals VMFS Recovery™ specializes in virtual machine recovery, particularly from VMware’s VMFS file system, but it also supports VHD and VHDX files. This software can effectively recover files from damaged virtual disks, locate lost data within corrupted files, and retrieve essential data without requiring advanced technical knowledge.

Key Features of DiskInternals VMFS Recovery™:

  • Deep Scanning: Thoroughly analyzes VHDX files to recover data from even severely damaged areas.
  • File System Compatibility: Supports multiple file systems, making it versatile for various VM formats.
  • User-Friendly Interface: Provides a straightforward interface, allowing even non-expert users to navigate the recovery process.

Although DiskInternals VMFS Recovery™ is primarily a Windows-based application, it can still be integrated into a Linux-based workflow with some preparation:

  • Access the Corrupt VHDX File on Linux: First, use Linux tools like guestmount or qemu-nbd to attempt mounting the VHDX file. If Linux cannot recognize the file due to corruption, proceed with DiskInternals VMFS Recovery™.
  • Transfer the File to a Windows Environment: Copy the corrupted VHDX file to a Windows system where DiskInternals VMFS Recovery™ is installed. This can be done through network sharing, USB drives, or external storage.
  • Run DiskInternals VMFS Recovery™: Open DiskInternals VMFS Recovery™ and load the VHDX file. The software will initiate a deep scan, locating and recovering any accessible data within the corrupted file.
  • Export Recovered Data: Once recovery is complete, you can export the recovered files to a secure location. If you need to transfer the data back to a Linux environment, you can use a shared folder or external storage device.

By incorporating DiskInternals VMFS Recovery™ into your recovery toolkit, you can efficiently address data loss issues from corrupted VHDX files, even in Linux-based workflows. The software’s advanced scanning capabilities and compatibility with various VM formats make it an invaluable resource for maintaining data continuity in virtualized environments.

Ready to get your data back?

To start recovering your data, documents, databases, images, videos, and other files from your RAID 0, RAID 1, 0+1, 1+0, 1E, RAID 4, RAID 5, 50, 5EE, 5R, RAID 6, RAID 60, RAIDZ, RAIDZ2, and JBOD, press the FREE DOWNLOAD button to get the latest version of DiskInternals RAID RecoveryÂŽ and begin the step-by-step recovery process. You can preview all recovered files absolutely for free. To check the current prices, please press the Get Prices button. If you need any assistance, please feel free to contact Technical Support. The team is here to help you get your data back!

Mounting VHD and VHDX on Other Systems

Mounting VHD and VHDX files can vary depending on the operating system. While Linux and Windows both support mounting these virtual disk formats, the complexity and tools involved differ. Here’s a quick comparison of the mounting process on Linux versus Windows, along with tips for ensuring cross-platform compatibility.

FeatureLinux (with `qemu-nbd` or `guestmount`)Windows (native support)
Tool RequirementsRequires `qemu-nbd` or `guestmount` toolsBuilt-in support in Disk Management
Process ComplexityModerately complex (involves command line)Simple (GUI and command-line options)
File System SupportSupports multiple formats (NTFS, ext4, etc.) with additional tools like `ntfs-3g`Primarily NTFS, as well as FAT32, with native support
PerformanceTypically slower, as it’s handled via virtual disk toolsFaster due to native OS support for VHD/VHDX
UnmountingRequires `qemu-nbd --disconnect` or `guestunmount`Easily unmounts through Disk Management

Key Differences

  • Windows: Natively supports both VHD and VHDX files, making the process relatively simple. Users can mount these files directly through Disk Management, PowerShell, or the File Explorer interface. This simplicity and native integration make Windows ideal for those new to handling virtual disk files.
  • Linux: Mounting requires additional utilities (qemu-nbd or guestmount) to access VHD or VHDX files, especially those formatted with NTFS. Linux offers flexibility but requires familiarity with the command line and kernel modules, making the process slightly more technical.

Tips for Cross-Platform Compatibility

  • Choose Compatible File Systems: For maximum compatibility, format VHD or VHDX files with NTFS or FAT32. These file systems are well-supported on both Windows and Linux. If using ext4 or other Linux-specific formats, Windows may not be able to read the contents without additional drivers.
  • Use guestmount for Simplicity on Linux: If you’re new to Linux or prefer a simpler approach, consider using guestmount from libguestfs. This method avoids the need for loading kernel modules and offers a more user-friendly way to access VHD/VHDX files.
  • Consider External Storage for Transfers: If you’re transferring VHD/VHDX files between Windows and Linux, use external storage or a shared network location to avoid formatting or compatibility issues.
  • Test with Smaller Files First: Before relying on large VHD/VHDX files cross-platform, test the mounting and transfer process with smaller files to ensure compatibility and smooth performance.

Automating the Process: Scripts to Mount VHD/VHDX on Boot

Automating the mounting process for VHD and VHDX files on boot can streamline workflows for servers or environments requiring consistent access to virtual disks. By creating a startup script that mounts these files automatically, you ensure they’re available immediately after boot. Below is a guide to creating and configuring these scripts for Linux using qemu-nbd or guestmount, and setting them to run at startup.

Step 1: Create the Mount Script

First, create a script that connects, scans, and mounts the VHD or VHDX file. Here’s an example script for qemu-nbd, which you can modify based on your mount point and file path.

  1. 1. Open a New Script File


Open a terminal and create a new script file in a convenient location, such as /usr/local/bin/mount_vhdx.sh.

  1. 2. Add the Mounting Logic


Add the following steps to the script:

  • Load the NBD Kernel Module to ensure the system can work with VHD/VHDX files.
  • Connect the VHDX File to the network block device, specifying the file path and device.
  • Wait for Device Availability and Scan Partitions to detect any partitions within the VHDX file.
  • Mount the First Partition on your chosen mount point.
  1. 3. Save and Make Executable


Save the script, then make it executable by setting the appropriate permissions with sudo chmod +x followed by the script path.

Step 2: Configure the Script to Run at Startup

To automate the script on boot, create a systemd service to execute it when the system starts.

  1. 1. Create a Systemd Service File


Open a new systemd service file in /etc/systemd/system, naming it something like mount_vhdx.service.

  1. 2. Define the Service


Configure the service file with the following sections:

  • Description and After fields to set the service details and dependencies.
  • ExecStart to specify the script’s path for execution.
  • RemainAfterExit to keep the mount active after the service completes.
  • WantedBy to set the service to run in multi-user mode.
  1. 3. Enable and Start the Service


Enable the service to run on boot with sudo systemctl enable mount_vhdx.service, and start it now to verify it works with sudo systemctl start mount_vhdx.service.

Step 3: Verify the Automation

After a reboot, confirm that the VHDX file mounts automatically by checking your specified mount point. If successful, the service will now run on each boot, attaching and mounting the VHDX file automatically.

Alternative for guestmount

If you’re using guestmount instead of qemu-nbd, adjust the script as follows:

  • Replace the qemu-nbd commands with guestmount to mount the VHDX file.
  • Use -i with guestmount to auto-detect partitions.

Then, update the ExecStart line in the systemd service file to specify the modified script path.

Tips for Automated Mounting

  • Unmount on Shutdown: To unmount the VHDX safely on shutdown, create a corresponding unmount script or add unmount commands to the shutdown sequence.
  • Error Handling: Add logic to handle errors if the VHDX file is unavailable or fails to mount.
  • Testing: Test your script and service thoroughly to ensure they work correctly without user intervention on each boot.

Conclusion: Efficient Virtual Disk Management in Linux

Efficiently managing VHD and VHDX files in Linux is essential for those working with virtualized environments, as these files often contain critical data and configurations. By understanding how to mount, automate, and troubleshoot VHD and VHDX files, you can streamline workflows, enhance data accessibility, and improve system performance. This knowledge is especially valuable in server environments where continuity and quick access to virtual disks are crucial.

In addition to native Linux tools, professional recovery solutions like DiskInternals VMFS Recovery™ play a vital role in ensuring data safety. When dealing with corrupted or inaccessible VHD and VHDX files, DiskInternals provides advanced recovery capabilities that can retrieve essential data, minimizing downtime and preventing data loss. By combining effective management practices with reliable recovery tools, you can maintain a robust, resilient virtual infrastructure that supports your organization’s data needs.

Related articles

FREE DOWNLOADVer 4.23, WinBUY NOWFrom $699

Please rate this article.
51 reviews