How can I clone Ubuntu WSL to a clean virtual machine
I have been doing my PHP development on Ubuntu WSL but earlier this month, I bought a mac and want to continue to develop on exactly the same configuration. How can I clone the Ubuntu WSL (with configurations such as PHP, nginx, etc.) to a virtual machine running on OSX?
There's probably not a great way to go about this, but you can always try it to see what happens.
Personally, if I was moving to a Mac, I think I'd just embrace macOS and HomeBrew for PHP development. I believe that's going to give you an experience closer to what you have with WSL/Ubuntu on Windows than you will get with an Ubuntu VM.
Of course, if there's some development for which you specifically need a Linux kernel or tools that just aren't available for macOS, then that changes things.
As for getting everything out of WSL and into an Ubuntu VM, I don't know that anyone has ever gone in that direction. I've provided answers going from a virtual or physical machine to WSL, but never the other way around (until now).
There's an Arch Linux guide that I came across in Googling for help here. It's not all that relevant to your question, except for the first line:
Moving into a virtual environment takes a little more effort.
Under WSL, to start with, you can create a tarball of the rootfs of any WSL instance using:
wsl --export <distroname> <backupname>.tar
That will preserve all files, permissions, links, and even Linux capabilities (although I don't believe the Ubuntu distribution uses these out-of-the-box, at least).
Getting that into a VM in a failsafe manner is the challenge. You are probably going to need to start with installing Ubuntu 20.04 into that VM to get the driver support for the virtual devices. That's not something, of course, that will be present in the WSL tarball.
So you have to leave /lib
in place, or at least the drivers that are there, but at the same time you want to merge the /lib
from your WSL backup.
In theory, if you have both systems at the same level (via a simultaneous sudo apt update && sudo apt upgrade -y
) then you could (in theory, to intentionally repeat myself) just extract the contents of the tar
over the existing directories.
I just looked at a wsl --export
d tar, and it does a pretty good job of leaving out any "system" directories that shouldn't be restored (e.g. /proc/
, /boot
, /sys
, etc.). However, there are two exceptions -- You should probably also manually exclude /mnt
and /media
from the extract. There's almost certainly no good reason to overwrite the Ubuntu VM mount points with those from the WSL instance.
So if you really want to try it, I would:
-
wsl --export
your existing WSL -
Transfer the
tar
to the Mac. -
Install your Ubuntu VM, preferably using Ubuntu Minimal
-
Back up your Ubuntu VM (because this certainly may not work)
-
Transfer the
tar
into the root of the VM -
Reboot the VM in rescue mode
-
Extract the tar over the existing directories. Use
tar --xattrs-include="security.capability" --exclude="./mnt" --exclude="./media" -xvf ...
. You may also need to specify the target directory, but I can't test that at the moment. Note that the--xattrs
may not be needed, but it's safer in case you do have any extended attributes. See this answer for the--exclude
, since I'm not quite 100% certain of that syntax. -
Reboot the VM
Other things that could cause problems here:
- WSL doesn't boot Systemd, but your VM will by default. It's unclear just how robust the Systemd installation will be that you get from the WSL image.