惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Forbes - Security
Forbes - Security
The Hacker News
The Hacker News
V
Vulnerabilities – Threatpost
C
CXSECURITY Database RSS Feed - CXSecurity.com
Spread Privacy
Spread Privacy
P
Proofpoint News Feed
AWS News Blog
AWS News Blog
S
Securelist
S
Security @ Cisco Blogs
Cloudbric
Cloudbric
T
Troy Hunt's Blog
SecWiki News
SecWiki News
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Security Latest
Security Latest
C
Cyber Attacks, Cyber Crime and Cyber Security
AI
AI
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Stack Overflow Blog
Stack Overflow Blog
I
Intezer
I
InfoQ
Attack and Defense Labs
Attack and Defense Labs
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Recent Commits to openclaw:main
Recent Commits to openclaw:main
T
The Exploit Database - CXSecurity.com
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
N
News | PayPal Newsroom
云风的 BLOG
云风的 BLOG
S
Secure Thoughts
T
The Blog of Author Tim Ferriss
Martin Fowler
Martin Fowler
L
LINUX DO - 最新话题
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
D
Darknet – Hacking Tools, Hacker News & Cyber Security
A
About on SuperTechFans
Hacker News - Newest:
Hacker News - Newest: "LLM"
TaoSecurity Blog
TaoSecurity Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Webroot Blog
Webroot Blog
L
LangChain Blog
MyScale Blog
MyScale Blog
Recent Announcements
Recent Announcements
P
Privacy & Cybersecurity Law Blog
Vercel News
Vercel News
Engineering at Meta
Engineering at Meta
C
Cybersecurity and Infrastructure Security Agency CISA
Y
Y Combinator Blog
L
Lohrmann on Cybersecurity
B
Blog RSS Feed
The Last Watchdog
The Last Watchdog

记录点滴 – 魔帆博客

Python包管理的血泪史:从混乱到秩序的漫长征 | 魔帆博客 日文中的衬线、非衬线混排 | 魔帆博客 网页排版中的字体衬线 | 魔帆博客 Git配置:如何优雅的配置多用户并使用 ssh 密钥验证 | 魔帆博客 Git 合并本地两个不同的 Repo 仓库 | 魔帆博客 【记录】使用Golang开发腾讯云函数踩的坑 | 魔帆博客 Git 提示 fatal: unsafe repository is owned by someone else 错误 | 魔帆博客 【笔记】Hydro二次开发总结 前端篇(一) | 魔帆博客 C/C++ 二叉树 | 魔帆博客 Linux 报错Certificate verification failed: The certificate is NOT trusted. | 魔帆博客 Python 数字大小写转换 | 魔帆博客 Windows10 系统盘开启 Bitlocker | 魔帆博客 中兴 CPE (4G) 设置 IPv6 联网 | 魔帆博客 VS Code C/C++ 环境配置 | 魔帆博客 使用 Excel 在地图上标注城市 | 魔帆博客 替换 PHP creat_function() 函数 | 魔帆博客 天下大事,尽在其中——德生 R-9012 收音机 | 魔帆博客 【技巧】干掉第三方抢票!如何以最快的速度抢到火车票 | 魔帆博客 【记录】Deepin 桌面无限转圈(风火轮) | 魔帆博客
Docker WSL1/2 迁移 Linux 发行版目录 | 魔帆博客
野小新 · 2022-02-09 · via 记录点滴 – 魔帆博客

WHY 为什么要迁移 WSL

适用于Linux的Windows子系统(Windows Subsystem for Linux,WSL)是微软开发的一个为在 Windows 上能够原生运行Linux二进制可执行文件(ELF格式)的兼容层。

通过 Windows 商店等途径,可以轻松下载各种发行版,一个系统多层套娃装逼。但是有一个小问题。

WSL 一般默认都是安装在C盘(之前如果用户更改了UWP安装目录,还需要修改回系统默认的C盘。

虽然现在很多人已经不分盘了(Windows 逐渐稳定,不用像之前隔年重装)。但很多时候为了重装方便,还是会把 C盘单独分一个盘,当安装多个 WSL 并深度使用后,空间小的C 盘就爆红了(相当于装了N个系统)

为什么不用 LxRunOffline

截至目前 3.5.0 版本(release 中的最新 3.5.0 ),LxRunOffline 是有问题的(今年四月左右就有的):

~
❯ lxrunoffline.exe move -n Debian -d D:\application\wsl\Debian
[ERROR] Couldn't set the case sensitive attribute of the directory "\\?\C:\Users\Paxos\AppData\Local\Packages\TheDebianProject.DebianGNULinux_76v4gfsz19hv4\LocalState\".
Reason: Indicates that the directory trying to be deleted is not empty.

无法迁移,duplicate WSL。

不过有一个不在 release 中的版本修复了这个问题:

但我个人觉得没必要,等 release 中放出了再用吧。所以本篇文章大部分都使用微软的官方命令来实现的。但部分功能为了方便会使用到 LxRunOffline

移动 Windows 商店的 WSL 发行版

使用微软官方提供的 WSL 命令来实现的思路就是先导出,然后注销掉发行版,再重新注册。

缺点就是重新注册后,系统默认的用户会变成 root (uid 1)。

查看电脑上安装的 WSL 发行版可以使用 wsl -l -v

下面用 Debian 来作为例子。

首先把 Debian 导出到指定目录 D:\application\wsl\Debian\Debian.tar (所有数据)

~
❯ wsl --export Debian D:\application\wsl\Debian\Debian.tar

确定导出成功后,就可以注销 Debian 发行版了。

这个命令会删除目前系统中的 Debian 发行版中的所有数据,请确保导出了数据

~ took 18s
❯ wsl --unregister Debian
Unregistering...

注销后,就可以使用导入命令 --import 来导入发行版。

导入的发行版名称还要保持为原来的一样(为了确保 wsl 复制程序能正常工作,如果你不在意也无所谓)

然后指定WSL安装目录到 D:\application\wsl\Debian

安装所用的数据 D:\application\wsl\Debian\Debian.tar (刚刚导出的数据文件)

然后使用 --version 指定版本号,如果使用一代使用 1,二代使用 2

~ took 7s
❯ wsl --import Debian D:\application\wsl\Debian "D:\application\wsl\Debian\Debian.tar" --version 1

当发行版导入完毕后,还需要设定进入时的默认用户,因为用户会变成默认 uid1root

设定初始用户:

~
❯ debian.exe config --default-user paxos

# 如果你没有 debian 应用程序(卸载了的话),可以用 lxrunoffline 设定 uid
~
❯ lxrunoffline.exe su -n Debian -v 1000

# 如果不想安装可以在 WSL 中使用添加 wsl.conf
echo -e "[user]\ndefault=$myUsername" >> /etc/wsl.conf

看上边的命令,可以看出来WSL 发行版的商店应用几乎没啥用。但商店的应用程序没必要卸载,一来占不了多少空间,二来可以利用这个 alias 快速进入WSL 发行版

移动 Docker 创建的 wsl2

docker 默认存在这里:C:\Users\Paxos(你的用户名)\AppData\Local\Docker\wsl

建议不要移动 docker-desktop 这个 wsl2,根据测试,docker for windows 更新方式是直接注销然后重新安装的。

其中 data 文件夹中的 wsl 体积会是比较大的,对我们这种C盘低保户非常不友好。

lxrunoffline 截至目前无法导出 WSL2,直接迁移也有bug,所以还是用微软官方命令来实现。

关闭所有发行版:

wsl --shutdown

导出 docker-desktop-data:

~
❯ wsl --export docker-desktop-data D:\application\wsl\docker\docker-desktop-data\docker-desktop-data.tar

注销 docker-desktop-data

~
❯ wsl --unregister docker-desktop-data

重新注册

~
❯ wsl --import docker-desktop-data(发行版名称) D:\application\wsl\docker\docker-desktop-data(安装位置) "D:\application\wsl\docker\docker-desktop-data\docker-desktop-data.tar"(安装使用的tar文件位置) --version 2(指定版本)

复制 wsl 发行版

wsl 的版本有1也有2,虽然微软提供了命令可以方便的切换,但我就想要两个同样的系统,但wsl版本不同。

lxrunoffline 的 duplicate 有点bug,修复版本在上面。但我觉得没必要,用官方的命令实现即可

~
❯ wsl --export Debian D:\application\wsl\Debian\Debian.tar
~
❯ wsl --import Debian-2 D:\application\wsl\Debian2 "D:\application\wsl\Debian\Debian.tar" --version 2

利用 lxrunoffline 修改默认用户:

~ took 4s
❯ lxrunoffline.exe su -n Debian-2 -v 1000
~
❯ wsl -l -v
  NAME                   STATE           VERSION
* Debian                 Stopped         1
  Debian-2               Stopped         2
  docker-desktop         Stopped         2
  docker-desktop-data    Stopped         2
  Arch                   Stopped         2