将 WSL(Windows 上的 Bash)根文件系统移动到另一个硬盘驱动器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38779801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:38:46  来源:igfitidea点击:

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

windowswindows-subsystem-for-linux

提问by Hymanson Ming Hu

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.

我刚刚将笔记本电脑中的 Windows 10 升级到 Redstone 1。所以我对 Linux 子系统(也就是 Windows 上的 WSL、LXSS 或 Bash)进行了测试。基本上一切正常,但是存在一个问题,就是WSL的RootFS位于%LocalAppData%路径(例如C:\Users\xyz\AppData\Local)中,这也是我的Windows操作系统所在的地方。它消耗了我笔记本电脑中的大量 SSD 硬盘空间,我希望我可以将它移到我的外置硬盘上。

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.

还有一个问题。由于其他硬盘分区挂载为 DriveFS,它不支持某些 Linux 文件系统功能,例如chmod,并且我的某些 Linux 项目构建脚本仅适用于 VoIFS,而 VoIFS 仅位于//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 mklinkfor 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.

所以我尝试将整个 WSL RootFS 移动到另一个硬盘驱动器。首先,我尝试为C:\Users\xyz\AppData\Local\lxss. 我已经完全卸载了 WSL 并将mklinkRootFS运行到另一个硬盘驱动器,当我再次重新安装 WSL 时,它无法创建新用户,并报告代码为 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!

然后我四处搜索,我得到了这个:移动 Linux 文件系统 - 问题 #449似乎为 WSL RootFS 移动和制作硬链接仅在 Windows 10 Build 14388 之前有效,它在 Windows 10 Build 14393 RTM 中不起作用。唯一的方法是将整个移动%LocalAppData%到外部硬盘驱动器,但我不能这样做,因为我是笔记本电脑用户,我不能到处都带外部硬盘驱动器!

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?

所以我想问一下,有没有其他方法可以将 WSL 移出我的系统分区?如果我不能这样做,我可以在其他一些地方创建一个额外的 VoIFS 工作目录并编译我的东西吗?

Thanks and regards, Hymanson

谢谢和问候,Hyman逊

回答by Jaime

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

如果您使用的是Windows 10版1903(2019年4月更新)或更高版本,你可以尝试用WSL命令行工具exportimport选项。如果您使用的是较旧的 Windows 10 版本,则可以使用lxRunOffline将分发版移动到另一个驱动器。



Using the WSL command line tool

使用 WSL 命令行工具

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

在 Windows 10 版本 1903(2019 年 4 月更新)或更高版本中,您可以使用wsl.exe命令行工具。

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

1. 导出分布。.tar使用要移动的分发创建一个文件wsl.exe --export

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

For instance, to export an Ubuntudistribution, you can use

例如,要导出Ubuntu发行版,您可以使用

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

2. 将发行版导入目标文件夹。然后,您可以将导出的发行版导入到另一个文件夹中

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

For instance, to import the exported Ubuntuinto a new UbuntuCustomdistribution, you can use

例如,要将导出的Ubuntu导入到新的UbuntuCustom发行版中,您可以使用

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

注意:您可以在https://github.com/pxlrbt/move-wsl查看移动使用这些命令的 WSL 发行版的脚本



Using LxRunOffline

使用 LxRunOffline

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

在任何 Windows 10 版本中,您都可以使用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.

1. 设置目标文件夹的权限。首先,我认为您必须为要移动分发的文件夹设置一些权限。您可以使用icacls <dir> /grant "<user>:(OI)(CI)(F)"来设置适当的权限。

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.

注意:除了上述权限之外,我还激活了Windows 中长路径名

2. Move the distribution.Using lxrunoffline move.

2. 移动分布。使用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 wslor the same lxrunoffline

3. 运行分发。移动分发后,您可以使用wsl或相同的方式运行分发lxrunoffline

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

C:\wsl> wsl
user@test:/mnt/c/wsl$ exit
logout

回答by Rich Turner

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.

WSL(当前)不支持将发行版移动或安装到非系统驱动器。但是,这是我们正在为未来的 Windows 版本探索的场景。

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).

请注意,从 Fall Creators Update (FCU) 开始,发行版现在通过 Windows 商店安装,并下载到并扩展到不同的文件夹(即不在 %localappdata%\lxss 下)。

NOte: While we're working to improve this scenario in future Windows releases, please DO NOTspelunk 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/

注意:虽然我们正在努力在未来的 Windows 版本中改进这种情况,但请不要从 Windows 或 Windows 应用程序进入发行版文件夹:https://blogs.msdn.microsoft.com/commandline/2016/11/17 /do-not-change-linux-files-using-windows-apps-and-tools/

回答by gavenkoa

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

看看https://github.com/DDoSolitary/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.)
  • 安装时无需访问 Internet。
  • 将任何 Linux 发行版安装到磁盘上的任何位置。
  • 将现有装置移至其他位置。
  • 复制现有安装。(创建备份。)
  • 注册现有的安装目录。(用于便携式使用。)