Score:1

how to move ubuntu in WSL

kr flag

When I started developing in WSL I don't know why, but I installed Ubuntu-20.04 app from Microsoft store instead of just Ubuntu. I know that only difference is that Ubuntu-20.04 will not get automatically updated when new Ubuntu version will be released. All I want to do is to make Ubuntu just like Ubuntu-20.04 without reconfiguring it. I thought of moving all files from Ubuntu-20.04 to Ubuntu, but I don't know if doing that is safe. Does anybody know how to do what I'm trying to do? Also should I do it?

edit: both versions of ubuntu are running on WSL-2 I have checked it using wsl -l -v as @NotTheDr01ds said.

edit 2: I want all of my configuration from Ubuntu-20.04 to Ubuntu including all files and installed packages

Ilia Manjavidze avatar
kr flag
@user68186 of course I have
NotTheDr01ds avatar
vn flag
@user68186 That's explicitly stated in the question, so I'm confused as to why you are even asking that? Edit -- Oh, you are asking if they installed the "plain vanilla" "Ubuntu" distribution from the Store already? You are right, that wasn't clear in the question, but I did assume it myself.
NotTheDr01ds avatar
vn flag
Welcome to Ask Ubuntu. This will be quite easy, I believe if you are running WSL2, but I'll need to think on it some more if you are using WSL1. Can you do a `wsl -l -v` and confirm that both your Ubuntu installations from the Store are running WSL2? Please edit your question with the results of `wsl -l -v` if you would. Thanks!
Ilia Manjavidze avatar
kr flag
@user68186 are you sure that it is safe?
NotTheDr01ds avatar
vn flag
@user68186 I would recommend against that unless the OP really knows what they are doing. Permissions/ownership/etc would need to be carefully handled, and it definitely shouldn't be done with something like Windows Explorer. Under WSL2, at least, there's a much easier/safer way.
NotTheDr01ds avatar
vn flag
@IliaManjavidze Ok, typing up the answer based on WSL2, but will be a bit. I'm pretty detailed :-)
NotTheDr01ds avatar
vn flag
@user68186 Sure, but that only gets *user* configuration from `$HOME`. It sounds to me like the OP wants all configuration (installed packages, `/etc/` configurations, etc.)
NotTheDr01ds avatar
vn flag
@IliaManjavidze Please confirm whether you want *all* configuration or just *user* configuration. user68186's point is much simpler if you just need user files, of course. We can just provide instructions for getting your files from the `$HOME` of one instance to the other.
Ilia Manjavidze avatar
kr flag
@NotTheDr01ds I have edited the question. I want all of my configuration
Score:1
vn flag

Why you may not need to do this at all

Ok, first things first. This is probably pretty unnecessary.

The fact that the "Ubuntu" (without a version number) in the Store automatically updates is ... true, but a bit misleading in reality.

This is a bit difficult to explain, but when you install "Ubuntu" (no version) from the Store, you actually end up with two things:

  • A rootfs package (install.tar.gz) in C:\Program Files\WindowsApps\CanonicalGroup.... You can see this by starting an Administrative PowerShell and running Get-ChildItem -Recurse 'C:\Program Files\WindowsApps\Canonical*' | Where-Object {$_.Name -eq 'install.tar.gz' } | % { $_.DirectoryName }.

  • When run for the first time (via ubuntu.exe), the installer creates your actual WSL instance in your %UserProfile%\AppData\Local\Packages\CanonicalGroup....

When there's a new release on the Store, the only thing that gets updated is the rootfs package. It does not change your installed instance. (Credit and thanks to u/zoredache on Reddit who keeps reminding me of this.)

This is pretty useless for most users unless you:

  1. Unregister the instance (which deletes all configuration) and re-configure it by re-running the ubuntu.exe command.
  2. Want to use that install.tar.gz to create a second WSL/Ubuntu instance (using wsl --import). The newly created instance would then use the Store-updated rootfs.

Neither of these scenarios is very common. Even when the unversioned Store Ubuntu gets updated to (presumably) 22.04, it won't change your installed instance of 20.04, just the rootfs package.

You'll still need to run a sudo do-release-upgrade -d when you eventually want to upgrade to 22.04 (or an interim release).

So, given that ... Since you already have your Ubuntu-20.04 configured the way that you want it, there's no great advantage to you to change it, other than getting a shorter, cleaner Ubuntu named for the WSL instance.

How to do it anyway

That said, the way that WSL2 stores your data, it's not all that difficult to move configurations around. The entire filesystem for a WSL2 instance is stored in a virtual HDD named ext4.vhdx.

Since you have two Ubuntu installations (Ubuntu and Ubuntu-20.04), you'll find two of these ext4.vhdx files under %userprofile%\AppData\Local\Packages\. To get the exact locations, run the following from PowerShell:

Get-ChildItem "$env:USERPROFILE\AppData\Local\Packages\CanonicalGroupLimited*\LocalState\*"

For my installation, that's:

...\CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc\LocalState\ext4.vhdx
...\CanonicalGroupLimited.Ubuntu20.04onWindows_79rhkp1fndgsc\LocalState\ext4.vhdx

The solution may be fairly obvious at this point, but use caution, of course:

  • Exit all of your running WSL instances.

  • Then wsl --shutdown to make sure nothing is currently running.

  • A backup of your existing config wouldn't be a bad idea with wsl --export Ubuntu-20.04 2021-09-26_Ubuntu-20.04_backup.tar (or whatever you want to call the backup file). This basically creates a tarball of the current rootfs, including all files, ownership, and permissions.

  • At that point, simply copy your desired (Ubuntu-20.04) ext4.vhdx over the newly installed (Ubuntu) one. The real trick is simply making sure that you copy the right one. You may want to run your Ubuntu-20.04 distribution and make a quick change so that you can easily see the latter timestamp on the "right" copy.

The only real requirement is that the default user for both instances should be the same.


Another possibility -- Since you backed up your desired config with wsl --export, you could always just wsl --import that into a new instance named Ubuntu.

  • First, you'd need to uninstall the Store Ubuntu.
  • Then pick a directory for your new instance. It can be anywhere and no longer has to live under your AppData directory.
  • wsl --import Ubuntu <directory> path\to\2021-09-26_Ubuntu-20.04_backup.tar --version 2

I keep mine in %userprofile%\Documents\WSL\instances and my tar backups in %userprofile%\Documents\WSL\images. That allows me to quickly spin up new instances via wsl --import. It's nice to be able to test something out without impacting my daily environment.


Note for any WSL1 users reading this. The rootfs for WSL1 is not stored in a virtual HDD file, but as actual files and directories under ...\LocalState\rootfs\. Please do not access these files directly as it can easily cause WSL filesystem corruption (see Microsoft devblog).

If you need to simply access the files from Windows, use the \\wsl$\<distro> path in Windows as noted in that Microsoft blog. But if you need to copy the entire thing over, like in this question, the best bet would be to convert the instance to WSL2 first, via wsl --set-version <distro> 2 (do a wsl --export backup first). After the conversion, the filesystem will be in the WSL2 ext4.vhdx format.

Ilia Manjavidze avatar
kr flag
Thank you for this. after this If only advantage I really get is shorter name and cleaner name I can just change its name in windows terminal. If you don't mind I'm still interested in how it's done. I am beginner yet and I don't understand really much so thank you :)
NotTheDr01ds avatar
vn flag
@IliaManjavidze Sure thing. I've added that to the answer. It's really interesting and a useful thing to know, IMHO.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.