Skip to content

Instantly share code, notes, and snippets.

@onomatopellan
Last active July 18, 2024 14:51
Show Gist options
  • Save onomatopellan/90024008a0d8c8a2ed6fa57e8b64df54 to your computer and use it in GitHub Desktop.
Save onomatopellan/90024008a0d8c8a2ed6fa57e8b64df54 to your computer and use it in GitHub Desktop.
How to automount an external vhdx file in WSL2
1. Ubuntu WSL2 must be already installed in C: system drive and user should be able to call windows binaries like wsl.exe from bash.
2. We will install Alpine WSL2 distro in an external partition/disk:
Download Alpine from here:
https://github.com/yuk7/AlpineWSL/releases/download/3.11.5-1/Alpine.zip
Extract the files in an external partition/disk. (for example D:\Alpine)
Make sure WSL2 is enabled by default (wsl.exe --set-default-version 2)
Inside the Alpine folder run Alpine.exe to install the Distro. An ext4.vhdx file will be created in that same folder.
Run Alpine.exe again.
Now from the Alpine terminal we will create same user as Ubuntu. This will create the /home/onoma folder.
adduser onoma
3. On Ubuntu edit the ~/.profile file and add:
# mount external /home folder from Alpine distro
if [ ! -d /mnt/wsl/Alpine ]; then
mkdir /mnt/wsl/Alpine
wsl.exe -d Alpine mount --bind /home/onoma /mnt/wsl/Alpine/
fi
4. We will close the VM with:
wsl.exe --shutdown
5. Next time Ubuntu distro is launched it will automount the /home/onoma folder from Alpine distro to /mnt/wsl/Alpine in Ubuntu with all
the benefits and speed of ext4 mounted folders.
How it works
---------------
WSL2 runs a single lightweight VM that supports multiple distros running on the same Linux kernel.
Every time a distro is launched, its vhdx file is attached automatically to the VM as a /dev/sdc device.
Thanks to the nature of WSL interop we can launch another distro and inmediately close it from inside bash.
The folder /mnt/wsl is an undocumented WSL2 feature which is a tmpfs special folder used by applications as Docker for Desktop.
Everything mounted in that folder will also appear in every running distro under /mnt/wsl/. Just make sure you have file permissions to
access that directory. In my case I'm using user "onoma" on both Ubuntu and Alpine.
@BeenLi
Copy link

BeenLi commented Jan 8, 2021

sorry, I have solved this problem by upgrading Alpine from wsl1 to wsl2. But I encounter another problem, copying the output directory generated by buildroot to my wsl2 ubuntu fails and if I copy the directory to my windows directory such as D:, it becomes very slow and consums me nealy 1 hours. Is this because the Windows and Linux file systems are incompatible?

Any help is appreciated!

@Shyamal-Shah
Copy link

I had an Ubuntu 20.04 LTS installed on WSL2, but due to an error during Windows update I cannot boot using that drive. So, I have installed fresh copy of windows on another drive and copied the contents of original drive. As Ubuntu was in WSL2 I cannot directly access those files. But I want to know if there is any possible way that I use my existing ubuntu ext4 filesystem and at least get my files. To be clear I have the whole folder for containing Ubuntu.

Thank you for any help in advanced.

@mwoodpatrick
Copy link

Worked well for me, definitely simpler than my previous approach. Many thanks

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