iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🔥

Automounting VHDX Files for WSL OS Migration

に公開

Getting Frustrated with ArchLinux

I have been using Manjaro, which is based on ArchLinux, but lately, I have been getting frequent certificate errors during pacman updates.
While it is recoverable, it is a hassle, so I want to switch my OS.

However, since my data size has become enormous, transferring it via Windows would take too long.

(゜-゜) I wonder if I can mount the entire OS as a data drive.

I expect similar situations will occur in the future, so I want to keep a VHDX file for data and make it easy to switch OSs. To achieve this, I need to automate the mounting process as well.

So, let's try this with the assumption of mounting the OS itself onto another OS.

ヾ(・ω<)ノ" 三三三● ⅱⅲ Rolling♪

------------------- ↓ The main part starts here ↓-------------------

Prepare the VHDX file for the OS to be mounted

Assuming the host OS is Ubuntu and the OS to be mounted is Debian:

powershell
wsl --install ubuntu
wsl --install debian
wsl --export Debian --vhd Debian.vhdx
wsl --unregister debian

Mount to the Host OS

Execute the mount on the guest OS side. Call the wsl.exe command from the Linux side.
Assuming the VHDX file is saved at C:\work\Debian.vhdx:

bash
/c/Windows/System32/wsl.exe --mount "C:\work\Debian.vhdx" --name Debian --vhd

Register the mount command in ~/.profile

The mount is released when you shut down. However, using fstab or diskpart is quite tedious. Let's just add it to .profile for a quick and easy solution.

~/.profile
if [ ! -d /mnt/wsl/Debian ]; then
  /c/Windows/System32/wsl.exe --mount "C:\work\Debian.vhdx" --name Debian --vhd
fi

References:
How to automount an external vhdx file in WSL2
Mounting other disks in WSL2

------------------- ↓ Postscript starts here ↓-------------------

How to create a VHDX file

There is a feature in the Control Panel with a long name: Create and format hard disk partitions.
You can handle VHDX files there, but the UI is messy with partitions containing the OS, so I prefer not to use it.

Instead, I use Distrod to create an appropriate guest OS like Alpine, and I use the generated file as a VHDX for storage.

Discussion