1

I have a multiboot system where sometimes I boot into Linux and sometimes I boot into Windows and use WSL2.

I just discovered that Linux has tools which can be used on vhdx files, and that from Linux I am able to mount the image and get into the WSL2 instance. It almost seems like a Docker image and not a virtual machine.

How can I export the vhdx file from Linux in order to make a backup of it? Are there any Linux tools that can help with things like shrinking the image or that are otherwise noteworthy? How safe is it to mount the image read-write and save changes to the filesystem?

JSNinja
  • 123

1 Answers1

2

First:

It almost seems like a Docker image and not a virtual machine.

That's an astute observation. WSL2 distributions (I prefer the term "instances") are much more like Docker containers than virtual machines. The WSL2 system itself runs as a virtual machine, then each distribution/instance is executed in its own PID namespace/container, but shares the same kernel and network with the other running WSL2 instances.

I just discovered that Linux has tools which can be used on vhdx files

For the sake of this answer, I'm going to assume you are referring to guestmount et. al. from the guestfs package.

How can I export the vhdx file from Linux in order to make a backup of it? In Windows the command wsl --export will backup a WSL VM to a tar file. This can later be restored and made into a WSL instance. I was wondering if I could do the same thing using Linux tools instead.

While I don't much see the point (unless for something like data recovery), and I haven't tested it myself -- Sure, I can't think of any reason why you wouldn't be able to create a WSL --importable tarball from the files in the VHDX from within Linux. I've created similar tarballs myself from chroot directories. When you are creating the tarball:

  • Keep all owners and permissions the same (so definitely run tar as sudo/root)
  • It's a good idea to use the --xattrs option to tar so that extended attributes are maintained. These can be used in certain distributions for things like capabilities.

How safe is it to mount the image read-write and save changes to the filesystem?

Perhaps a bit too much of an "opinion" question for Super User. My opinion - Probably fairly safe given the maturity of the project, but I still wouldn't trust a good WSL2 VHDX to it myself.

I'm just not sure what the point would be. If you are going that route, I'd just recommend putting any files you need on the NTFS mount of the Windows drive, then copying them in using the "normal" methods from the Windows side of things. They aren't going to be usable in WSL until you dual-boot back to Windows anyway.

Are there any Linux tools that can help with things like shrinking the image or that are otherwise noteworthy?

There's a fairly decent list of tools that come along with the guestfs installation. Again, if you need to shrink the ext.vhdx image, there are Microsoft tools for doing so that I would probably trust more. Regardless, I always recommend a backup before running them.

NotTheDr01ds
  • 28,025