Wsl: how to move the vhdx of wsl2 to other disk

Created on 13 Jun 2019  路  23Comments  路  Source: MicrosoftDocs/WSL

I install wsl2 , i learn that wsl2 use vhdx as store file, because C disk is small, so I want to move the vhdx file to other disk.

please tell me how to do it. thx

Most helpful comment

Here is exactly what I did on 19035.1 and it worked without a reboot or errors.

  1. Run powershell.exe as Administrator
PS C:\WINDOWS\system32> wsl -l
Windows Subsystem for Linux Distributions:
Ubuntu (Default)

# mkdir S:\ISOs\

PS C:\WINDOWS\system32> wsl --export Ubuntu S:\ISOs\ubuntu-wsl.tar

# mkdir w:\VMs

PS C:\WINDOWS\system32> cd w:\VMs
PS W:\VMs> mkdir ubuntu-wsl
PS W:\VMs> wsl --unregister Ubuntu
Unregistering...
PS W:\VMs> wsl --import Ubuntu W:\VMs\ubuntu-wsl S:\ISOs\ubuntu-wsl.tar
PS W:\VMs> wsl -l
Windows Subsystem for Linux Distributions:
Ubuntu (Default)

All 23 comments

You can do this using the wsl --import feature. Here's an example of me doing this setup.

image

If you run into any more issues please reopen this issue, but otherwise I believe your question is solved so I'll close this out for you. Thank you!

You exported Alpine2 but imported Alpine3. Could you please clarify?

@s-usr I exported my current running version of Alpine called Alpine2 and when importing I need to have another name to import, (since Alpine2 is already taken) so I imported the new distro as Alpine3.

Thanks. It will be nice if we could keep the name as is, because this is not a filename; it is a distribution name.

Hello... I got "Access is denied" on import. I checked the "Security" for the 'source' and 'destination'. I have administrator priviliges. There should be a trick to handle this. Please advise if you came across this error. Thanks.

@craigloewen-msft I have compeletely follow the way you provide, however it seems i lost personal files in my WSL2 distro. Is it a expected behavior or just a bug? :(

@craigloewen-msft I have compeletely follow the way you provide, however it seems i lost personal files in my WSL2 distro. Is it a expected behavior or just a bug? :(

I'm sorry, the distro's default user was changed to root, may i bring it back to old user?

@Mrcooder yes there is a way to do this in newer builds. What version of Windows are you using? If you're on the Windows Insiders fast ring then you should have access to this feature as we added it in Build 18980. You can find the details here.

Hello @craigloewen-msft , there is already a way to install Ubuntu distro or any other distro directly on another hard disk, and I have to follow the import method?

Easiest way to do it right now is using the import method. Adding the ability to install to another drive, or move to another drive is something we're tracking here: https://github.com/microsoft/WSL/issues/449

Thank you, but call me a fool. Because once exported, I don't know what to do anymore.

I checked the wsl -l -v versions and now I have 2:

  NAME            STATE           VERSION
* Ubuntu-18.04    Stopped         2
  Ubuntu18        Stopped         2

So I have unregister the previous version (Ubuntu-18.04) with
wsl --unregister Ubuntu-18.04

Now what do I have to do?

I've tried this

wsl.exe --distribution Ubuntu18

but I get the message:

The process does not have access to the file because it is being used by another process.

If I search for Ubuntuin Start Menu and run it, it reinstalls the version I have uninstalled.

So I don't understand very well, can you help me?


Okay, I just rebooted Windows, and now I can get in. I had to install Windows Terminal(Preview) to be able to enter the distro more easily.

Yes you can run wsl -d Ubuntu18 to boot into it directly, or run wsl -s Ubuntu18 to set it to your default distro and then after that just wsl will launch you into it.

Glad that terminal resolved your probelm!

Here is exactly what I did on 19035.1 and it worked without a reboot or errors.

  1. Run powershell.exe as Administrator
PS C:\WINDOWS\system32> wsl -l
Windows Subsystem for Linux Distributions:
Ubuntu (Default)

# mkdir S:\ISOs\

PS C:\WINDOWS\system32> wsl --export Ubuntu S:\ISOs\ubuntu-wsl.tar

# mkdir w:\VMs

PS C:\WINDOWS\system32> cd w:\VMs
PS W:\VMs> mkdir ubuntu-wsl
PS W:\VMs> wsl --unregister Ubuntu
Unregistering...
PS W:\VMs> wsl --import Ubuntu W:\VMs\ubuntu-wsl S:\ISOs\ubuntu-wsl.tar
PS W:\VMs> wsl -l
Windows Subsystem for Linux Distributions:
Ubuntu (Default)

If anybody is interested: I wrote a little bash and batch script for automating these steps.
https://github.com/pxlrbt/move-wsl

If allowed, you can do it with just one reg add command.

The instructions that @mxplusb provided worked really well for me, thanks!

I had to add a wsl --shutdown step before doing the import else it returned an error message saying that the operation timed out because the VM isn't responding. Probably caused by my usage of the WSL2 backend with Docker so that machines were still running even after unregistering.

Also, the default username that WSL uses needs to be reconfigured, otherwise it tries to log in as root with the command ubuntu config --default-user <some-user>

Hoping this gets included in the docs since this is extremely useful for SSDs especially.

I saw a lot of scary answers with many steps and possible errors here, so I decided to just cut and paste the folder to a bigger HD.

Then I told windows to link both of the folders:
mklink /J C:\...\OldFolderName G:\...\GrowAllYouWantHereFolder

The second folder can have any name. No errors. No reboot. My SSD can breath again.

Btw, don't use powershell, use cmd for this one.

wsl --import --export command made a lot of disk io, can I just modify the registry of lxss?
I changed the registry key at

Computer\HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Lxss\{your linux uuid}\BasePath 

from default to \\?\D:\Path\to\ext4.vhdx , then move ext4.vhdx to that path, reboot WSL2, and it seems work well.

A combination of @mxplusb and @Neurrone comments were the solution for me. Thanks

My steps is a combination of @rekaerst, @Neurrone, and @mxplusb

  1. Backup current vhdx file
    from %LOCALAPPDATA%\Packages\<PackageFamilyName>\LocalState\ext4.vhdx
    to D:\ext4.vhdx
    (Most important step. If you can't find correct vhdx file, you shouldn't go ahead)
  2. Remove current distro, like wsl --unregister Ubuntu-20.04
  3. Reinstall distro from Microsoft Store
  4. Export and import
    wsl --export Ubuntu-20.04 D:\wsl.tar
    wsl --unregister Ubuntu-20.04
    wsl --import Ubuntu-20.04 D:\Ubuntu-20.04 D:\wsl.tar
  5. Overwrite original vhdx file
    copy D:\ext4.vhdx D:\Ubuntu-20.04\ext4.vhdx
  6. Set default user
    ubuntu2004.exe config --default-user <username>

@banyh Any side effects or do you lose any data?
I wanna try doing it for my kali-linux dist.

Thanks. It will be nice if we could keep the name as is, because this is not a filename; it is a distribution name.

You can unregister the original distribution before import.

wsl --unregister Ubuntu-18.04

Was this page helpful?
0 / 5 - 0 ratings