Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WSL2 Register Command #4762

Closed
rwilliams opened this issue Dec 15, 2019 · 22 comments
Closed

WSL2 Register Command #4762

rwilliams opened this issue Dec 15, 2019 · 22 comments
Labels

Comments

@rwilliams
Copy link

rwilliams commented Dec 15, 2019

Is your feature request related to a problem? Please describe.
#4591 I recently refreshed Windows install after a hardware upgrade so my WSL distros were deleted from the registry but I still had the .vhdx files. I also exported my distro with the plan of importing it when I re-installed windows. When I tried to import with WSL2 as the default type I get #4530
image
after quite a few minutes has elapsed. I had to import as WSL1 and then convert to WSL2 after the import. It took ~2.5 hours for a 50gb image on a fast workstation. Even if the import worked directly with WSL2 the process is still very long.

Describe the solution you'd like
It would be nice if you could just run something like WSL --register with the same options as --import (except it looks for a vhdx and not an archive) and have it properly register the distro image in the registry and do anything else that needs to be done. This would allow just backing up the vhdx directly without the need to use import and export.

Describe alternatives you've considered
Right now import/export is terribly slow and maybe broken on WSL2 so I don't think it's a good alternative.

@benhillis
Copy link
Member

I like this idea, let me play with it a bit.

@rwilliams
Copy link
Author

It might also be cool if you could specify a UID to set the default user when doing this.

@synergies
Copy link

@therealkenc the issue is I don't have the export like @rwilliams to import. How can I "import" from the existing ext4.vhdx file?

@rwilliams
Copy link
Author

rwilliams commented Jan 26, 2020

You can create a registry entry for the vhdx though it's not recommended to mess with the registry.

@rwilliams
Copy link
Author

@synergies sent you an email.

@synergies
Copy link

synergies commented Jan 26, 2020

Thanks @rwilliams for the registry file! For anyone that runs into this issue, below is the registry file I used to "import" the existing ext4.vhdx to show up properly as a WSL distro:

Windows Registry Editor Version 5.00

[HKEY_USERS\[SID]\SOFTWARE\Microsoft\Windows\CurrentVersion\Lxss\{[UUID]}]
"State"=dword:00000001
"DistributionName"="[DISTRO_NAME]"
"Version"=dword:00000002
"BasePath"="\\\\?\\[VHDX_PATH]"
"Flags"=dword:0000000f
"DefaultUid"=dword:000003e8

Below are the items you need to update in the registry script above:

  • [SID]: Open command prompt and run whoami /user to get your SID
  • [UUID]: Go to this generator create a UUID
  • [DISTRO_NAME]: Name of distro you want, for example: Ubuntu
  • [VHDX_PATH]: Path of the distro with escaping slashes, for example: D:\\WSL\\Ubuntu

Hopefully this will help anyone stuck in a similar situation as myself. Good luck!

@erSitzt
Copy link

erSitzt commented Mar 4, 2020

@synergies
Thanks for that... got my wsl back this way

Third time i lost my wsl due to a windows update (i guess)

@mnunezdm
Copy link

mnunezdm commented Apr 27, 2020

Thanks @rwilliams for the registry file! For anyone that runs into this issue, below is the registry file I used to "import" the existing ext4.vhdx to show up properly as a WSL distro:

Windows Registry Editor Version 5.00

[HKEY_USERS\[SID]\SOFTWARE\Microsoft\Windows\CurrentVersion\Lxss\{[UUID]}]
"State"=dword:00000001
"DistributionName"="[DISTRO_NAME]"
"Version"=dword:00000002
"BasePath"="\\\\?\\[VHDX_PATH]"
"Flags"=dword:0000000f
"DefaultUid"=dword:000003e8

Below are the items you need to update in the registry script above:

  • [SID]: Open command prompt and run whoami /user to get your SID
  • [UUID]: Go to this generator create a UUID
  • [DISTRO_NAME]: Name of distro you want, for example: Ubuntu
  • [VHDX_PATH]: Path of the distro with escaping slashes, for example: D:\\WSL\\Ubuntu

Hopefully this will help anyone stuck in a similar situation as myself. Good luck!

In my case, I just have to write the path without escaping... \\?\C:\WSL\ubuntu.

If I escape them I got an error of incorrect path or the one referenced here #4860

I check other distro installations in the same registry folder, and in the other, there was no escaping

@Robula
Copy link

Robula commented Aug 18, 2020

I've just done the same, had to to reset my PC because of DCOM errors. WSL was wiped however I still have the ext4.vhdx file in Windows.old. How do I use wsl --import to get back to where I was?

@rwilliams
Copy link
Author

rwilliams commented Aug 18, 2020

@Robula You can re-register the ext4.vhdx using the following #4762 (comment) , though this method is not officially supported. I don't believe there is a way to use the wsl --import if you don't have an exported backup.

@alan23273850
Copy link

I've found this solution https://www.reddit.com/r/bashonubuntuonwindows/comments/gdiubo/wsl2_import_existing_ext4_image/

It says "Solved this by installing a new Ubuntu instance, removing its EXT4 image, and replacing with the EXT4 image from my old install." and it really worked for me rather than the script mentioned above.

@Jirubizu
Copy link

I've found this solution https://www.reddit.com/r/bashonubuntuonwindows/comments/gdiubo/wsl2_import_existing_ext4_image/

It says "Solved this by installing a new Ubuntu instance, removing its EXT4 image, and replacing with the EXT4 image from my old install." and it really worked for me rather than the script mentioned above.

I struggle with this example. How exactly do you open the ext4.vdhx to then move the files over to the localstate. I am unable to find the new installations ext4.vdhx anywhere

@synergies
Copy link

@Jirubizu I can't speak to the steps on the reddit link. However, the process I commented above worked for me. Did that not work for you?

@andregs
Copy link

andregs commented Jul 16, 2021

In my case, I just have to write the path without escaping... \\?\C:\WSL\ubuntu.

If I escape them I got an error of incorrect path or the one referenced here #4860

I check other distro installations in the same registry folder, and in the other, there was no escaping

The escaping part is confusing because in the .reg file the BasePath must be escaped, but when you check the results in regedit you'll see that value with no escaping.

@securedirective
Copy link

In case anyone else finds this page and has the same question as @Jirubizu... the "hard drives" for WSL distros will be a .vdhx if WSL version 2, but will be a regular directory of files if WSL version 1. So I'd posit that whoever said "Solved this by installing a new Ubuntu instance, removing its EXT4 image, ..." probably has their WSL set to version 2 by default. But on a freshly-installed Windows 10 (as of May 2022), the default is still version 1.

So, do one of the following:

  1. Run wsl --set-default-version 2 before creating the new WSL distro. Then any new distros will start in .vdhx format.
  2. Run wsl --set-version Ubuntu 2 to convert an existing "Ubuntu" distro to .vdhx format.

@neilfranci
Copy link

For newer come to here like me reinstall window 10 but keep files and you still have ext4.vhdx then you can go to register and make smt like this
image

You can follow synergies answer above #4762 (comment)
But the Base Path is not that hard

@theAkito
Copy link

Thanks @rwilliams for the registry file! For anyone that runs into this issue, below is the registry file I used to "import" the existing ext4.vhdx to show up properly as a WSL distro:

Windows Registry Editor Version 5.00

[HKEY_USERS\[SID]\SOFTWARE\Microsoft\Windows\CurrentVersion\Lxss\{[UUID]}]
"State"=dword:00000001
"DistributionName"="[DISTRO_NAME]"
"Version"=dword:00000002
"BasePath"="\\\\?\\[VHDX_PATH]"
"Flags"=dword:0000000f
"DefaultUid"=dword:000003e8

Below are the items you need to update in the registry script above:

* **[SID]:** Open command prompt and run `whoami /user` to get your SID

* **[UUID]:** Go to [this generator](https://www.uuidgenerator.net/) create a UUID

* **[DISTRO_NAME]:** Name of distro you want, for example: `Ubuntu`

* **[VHDX_PATH]:** Path of the distro with escaping slashes, for example: `D:\\WSL\\Ubuntu`

Hopefully this will help anyone stuck in a similar situation as myself. Good luck!

Thanks for this, kind of solved my issue.
As this restoration method seems to have different variations, I'll add mine for future readers.

  1. I had to use the escape back slashes. Did not work without.
  2. The DefaultEnvironment "Multi-String" is missing. Mine looks like this:
HOSTTYPE=x86_64
LANG=en_US.UTF-8
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
TERM=xterm-256color
  1. The \\?\ prefix in the BasePath might be needed or not. I have distributions that feature this and others that don't. If your distribution is not found, remove that prefix and check again.
  2. At first, I did not realise that BasePath is actually the path to the directory, where the ext4.vhdx is supposed to be. If the path to the ext4.vhdx is provided, the installation won't be found, either.

@cool-delete
Copy link

After rebuild windows, I found the ext4.vhdx of the old system but could not mount it normally. The disk manager prompted me to format it. I used the 7zip program to check the dir and saw two img.

image

@wangzhankun
Copy link

Thanks @rwilliams for the registry file! For anyone that runs into this issue, below is the registry file I used to "import" the existing ext4.vhdx to show up properly as a WSL distro:

Windows Registry Editor Version 5.00

[HKEY_USERS\[SID]\SOFTWARE\Microsoft\Windows\CurrentVersion\Lxss\{[UUID]}]
"State"=dword:00000001
"DistributionName"="[DISTRO_NAME]"
"Version"=dword:00000002
"BasePath"="\\\\?\\[VHDX_PATH]"
"Flags"=dword:0000000f
"DefaultUid"=dword:000003e8

Below are the items you need to update in the registry script above:

  • [SID]: Open command prompt and run whoami /user to get your SID
  • [UUID]: Go to this generator create a UUID
  • [DISTRO_NAME]: Name of distro you want, for example: Ubuntu
  • [VHDX_PATH]: Path of the distro with escaping slashes, for example: D:\\WSL\\Ubuntu

Hopefully this will help anyone stuck in a similar situation as myself. Good luck!

I have error "Deny ACCESS", Any Solution for it?

@jamesyinbaare
Copy link

working with ubuntu 20.04.6 on Win 11 host and I didn't have any issue with everything that has been described here. I just replaced .vhdx backup and everything worked seamlessly. I just used the same distro, same name and password I was using on the previews installation.

@Chiichen
Copy link

Chiichen commented Jun 29, 2024

Command wsl --import-in-place <Customized distribution name> <path of vhdx> can be used now to import the distribution in place

@OneBlue
Copy link
Collaborator

OneBlue commented Aug 19, 2024

Indeed this is now possible via wsl --import-in-place.
Closing

@OneBlue OneBlue closed this as completed Aug 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests