Move WSL (Bash on Windows) root filesystem to another hard drive?

75

I've just upgraded the Windows 10 in my laptop to Redstone 1. So I had a test of the Linux subsystem (aka. WSL, LXSS or Bash on Windows). Basically, everything is fine, but there is a problem that the RootFS of WSL is located in the %LocalAppData% path (e.g. C:\Users\xyz\AppData\Local), which is also the place where my Windows OS located. It consumes a lot of the SSD hard drive space in my laptop and I hope I can move it to my external hard drive.

There is an another problem. Since the other hard drive partitions are mounted as DriveFS, it does not support some Linux filesystem features like chmod, and some of my Linux project build scripts only works in VoIFS, which only located in / and /home.

So I tried to move the whole WSL RootFS to another hard drive. Firstly I've tried to make a NTFS Junction (hard link) for C:\Users\xyz\AppData\Local\lxss. I've uninstalled the WSL completely and run mklink for the RootFS to another hard drive, and when I reinstall the WSL again, it couldn't create the new user, and it reports error with code 0x80070003.

Then I searched around, and I got this: moving Linux filesystem - issue #449. It seems that moving and making hard link for WSL RootFS only works before Windows 10 Build 14388, it does not work in the Windows 10 Build 14393 RTM. The only way is moving the whole %LocalAppData% to the external hard drive, but I cannot do that since I'm a laptop user and I can't bring an external hard drive everywhere!

So I would like to ask that, is there any other ways to move WSL out of my system partition? If I can't do that, can I create an extra VoIFS work directory in some other places and compile my stuff?

Thanks and regards, Jackson

windows
windows-subsystem-for-linux
asked on Stack Overflow Aug 5, 2016 by Jackson Ming Hu • edited Sep 16, 2016 by Tim

4 Answers

137

If you are using Windows 10 version 1903 (April 2019 Update) or later, you can try the WSL command line tool with the export and import options. If you are using an older Windows 10 version, you can move the distribution to another drive using lxRunOffline.


Using the WSL command line tool

In Windows 10 version 1903 (April 2019 Update) or later, you can use the wsl.exe command line tool.

1. Export the distribution. Create a .tar file with the distribution to move using wsl.exe --export

wsl.exe --export <DistributionName> <Tar-FileName>

For instance, to export an Ubuntu distribution, you can use

C:\> wsl.exe --export Ubuntu c:\data\ubuntu.tar

2. Import the distribution into the target folder. Then, you can import the exported distribution into another folder

wsl.exe --import <DistributionName> <Folder-To-Install> <Tar-FileName>

For instance, to import the exported Ubuntu into a new UbuntuCustom distribution, you can use

C:\> wsl.exe --import UbuntuCustom d:\wsl\UbuntuCustom c:\data\ubuntu.tar

NOTE: You may check an script for moving WSL distros that use these commands at https://github.com/pxlrbt/move-wsl


Using LxRunOffline

In any Windows 10 version, you can move the distribution to another drive using lxRunOffline.

1. Set permissions to the target folder. First, I think you must set some permissions to the folder where the distribution will be moved. You may use icacls <dir> /grant "<user>:(OI)(CI)(F)" to set the proper permissions.

C:\> whoami
test\jaime

C:\> icacls D:\wsl /grant "jaime:(OI)(CI)(F)"

NOTE: In addition to the above permissions, I have activated the long path names in Windows.

2. Move the distribution. Using lxrunoffline move.

C:\wsl> lxrunoffline move -n Ubuntu-18.04 -d d:\wsl\installed\Ubuntu-18.04

You may check the installation folder using

C:\wsl> lxrunoffline get-dir -n Ubuntu-18.04
d:\wsl\installed\Ubuntu-18.04

3. Run the distribution. after moving the distribution, you can run the distribution using wsl or the same lxrunoffline

C:\wsl> lxrunoffline run -n Ubuntu-18.04 -w
user@test:~$ exit
logout

C:\wsl> wsl
user@test:/mnt/c/wsl$ exit
logout
answered on Stack Overflow Aug 9, 2018 by Jaime • edited Mar 30, 2020 by Jaime
8

WSL does not (currently) support moving or installing distro to non-system drives. However, this is a scenario we are exploring for future Windows releases.

Note that as of Fall Creators Update (FCU), distros are now installed via Windows store and are downloaded to, and expanded to different folders (i.e. not under %localappdata%\lxss).

NOte: While we're working to improve this scenario in future Windows releases, please DO NOT spelunk into the distro folders from Windows or Windows apps remains: https://blogs.msdn.microsoft.com/commandline/2016/11/17/do-not-change-linux-files-using-windows-apps-and-tools/

answered on Stack Overflow Oct 24, 2017 by Rich Turner
8

Take a look to https://github.com/DDoSolitary/LxRunOffline

scoop install lxrunoffline
choco install lxrunoffline
  • No Internet access required when installing.
  • Install any Linux distros to any location on your disk.
  • Moving existing installations to other locations.
  • Duplicating existing installations. (To create backups.)
  • Register existing installation directories. (For portable usage.)
answered on Stack Overflow Jul 13, 2018 by gavenkoa • edited Jan 23, 2021 by gavenkoa
0

I have WSL2 Ubuntu20.04 in Windows Home

Comment from @pixelarbeit

The wsl command solution works fine. I wrote a little script to automate these steps: github.com/pxlrbt/move-wsl

The steps I did:

  1. install gitbash for windows to run the command from github
  2. open the distro, add file

/etc/wsl.conf

[user]
default=<yourAccount>
  1. in CMD "wsl.exe --shutdown"
  2. next time I open the distro, my user is the default one and all looks like it was before, great!

THANK YOU @pixelarbeit!

answered on Stack Overflow May 11, 2021 by Daniel Katz

User contributions licensed under CC BY-SA 3.0