[WSL2] Mount vhdx to WSL216 Oct 2020
Sometimes the best blog posts come from Bad Ideas™. Like this one:
While the cleanup from the experimental moving of AppData is the subject of another post, one thing that didn’t come back properly was my default WSL distribution. This post shows how I mounted the
ext4.vhdx to a different WSL distribution to export the data.
Mount additional vhdx to WSL2
Note: Do not use this for anything other than data recovery. Even then, there are likely better methods. Why? This method uses FUSE mounts which are s-l-o-w, and in general works against the seamlessness that WSL aims for.
In order to mount a VHD or VHDX file to WSL, you need to first install some tools, and, likely a Kernel.
$ sudo apt update $ sudo apt install -y libguestfs-tools linux-image-generic
The kernel also needs to be accessible by our user:
$ sudo chmod 0777 /boot/initrd.img-5.4.0-51-generic $ sudo chmod 0777 /boot/vmlinuz-5.4.0-51-generic
Once your tools are installed, have a look at the partitions and filesystems on the
$ virt-filesystems -a /mnt/c/oldUbuntu/ext4.vhdx -l Name Type VFS Label Size Parent /dev/sda filesystem ext4 - 274877906944 -
Cool, as you see from the output, there is one disk
/dev/sda with an
ext4 filesystem on it. Next, we make a spot to mount it to, and then, mount it using
$ cd ~ $ mkdir import $ guestmount \ --add /mnt/c/oldUbuntu/ext4.vhdx \ --ro \ -i ./import/
guestmount command above adds (
--add) an image (
/mnt/c/oldUbuntu/ext4.vhdx), specifies we want it read-only (
--ro), and to inspect it (
-i) for filesystems to mount to our destination (
Note: This failed for me the first few times until I had a kernel installed in
/bootthat was readable by the user.
Now we can check our mounts and confirm that it is indeed mounted, and even browse the filesystem if we’d like:
$ mount ... /dev/fuse on /home/bunchc/import type fuse (rw,nosuid,nodev,relatime,user_id=1000,group_id=1000) $ ls import/ bin dev home lib lost+found mnt proc run snap sys usr boot etc init lib64 media opt root sbin srv tmp var
In this post we used
guestmount to mount a
vhdx into WSL2 for data recovery. While it is possible to do this for mounting and using secondary disk files, in that case you likely just want a Virtual Machine. It will be faster and more stable.