Introduction
In this article, we will discuss how to persistently mount a VHDX drive in WSL2 (Windows Subsystem for Linux, version 2) for seamless file access. This allows users to easily access their Windows files from within the WSL2 environment, enabling a more integrated development experience.
Background: Windows Subsystem for Linux (WSL)
Windows Subsystem for Linux (WSL) is a compatibility layer for running Linux binary executables natively on Windows 10 and newer. It allows developers to run their preferred Linux tools and applications directly on Windows, without requiring a full virtual machine or dual-boot setup.
Benefits of Using VHDX in WSL2
Virtual Hard Disk (VHD) and Virtual Hard Disk X (VHDX) are disk image formats used by Microsoft for virtual machines and other purposes. By using a VHDX file, you can create a separate, isolated file system inside WSL2, keeping your Linux environment and Windows system files separate. This is particularly useful for:
- Testing and development
- Running Linux applications that require a separate file system
- Managing files and directories independently of your Windows system
Persistently Mounting VHDX in WSL2
In order to mount and access a VHDX file persistently across WSL2 sessions, use the following command:
wsl --mount --vhd "" --bare --name "" Replace <path\_to\_your\_VHDX\_file> with the file path of your VHDX file, and set <desired\_mount\_name> to the desired mount name, which will be used for accessing the drive inside the WSL2 environment. For example:
wsl --mount --vhd "E:\CustomDrive2.vhdx" --bare --name myvhdxUnderstanding the Command Options
Let's examine the various options used in the wsl --mount command:
--vhd: Specifies the path to the VHDX file.--bare: Bypasses the /mnt mount point.--name: Sets the desired mount name, used for accessing the drive inside the WSL2 environment.
Accessing the Mounted VHDX in WSL2
To access the mounted VHDX in WSL2, use the following path:
/mnt/For example, if the VHDX was mounted with the name myvhdx, the path to access the drive in the WSL2 environment would be:
/mnt/myvhdxYou can now use standard Linux commands for file manipulation, such as ls, cd, mkdir, and touch.
Unmounting a VHDX from WSL2
To unmount a VHDX from WSL2, use the following command:
wsl --unmount --mount-point /mnt/For example:
wsl --unmount --mount-point /mnt/myvhdx
This article covered the topic of persistently mounting VHDX drives in WSL2 for seamless file access using the wsl —mount —vhd command. We also touch on the concepts of VHDX and WSL, and provided detailed explanations and examples for understanding the process.