WSL Tips

WSL簡(jiǎn)介

對(duì)于Windows Subsystem for Linux,可以參考Windows Subsystem for Linux (WSL) 原理介紹了解一些基本的背景知識(shí)

文件系統(tǒng)的介紹可以參考:
https://blogs.msdn.microsoft.com/wsl/2016/06/15/wsl-file-system-support/

WSL的文件系統(tǒng).png

VolFs is used to mount the VFS root directory, using %LocalAppData%\lxss\rootfs as the backing storage by default.

我們以u(píng)buntu-18.04為例,通過(guò)的Microsoft Store中的ubuntu的app只能默認(rèn)將ubuntu分發(fā)安裝到系統(tǒng)盤(pán),而且上面提到的using %LocalAppData%lxss\rootfs as the backing storage,因此這里需要考慮到windows系統(tǒng)分區(qū)的影響,/, /root, /home的可用空間受rootfs所在Windows分區(qū)的限制,目前WSL也沒(méi)有提供有效的途徑讓我們可以在其他的Windows分區(qū)創(chuàng)建一個(gè)VolFs分區(qū)來(lái)掛載,因此才有了需要將ubuntu移動(dòng)到非系統(tǒng)分區(qū)的需求。

如何將ubuntu分發(fā)移動(dòng)到非系統(tǒng)盤(pán)

想要達(dá)到我們這里的目的目前有兩種方式:

  1. 通過(guò)LxRunOffline工具直接安裝到任意的位置
    具體方法可以參考how-to-install-wsl-distros-on-external-disks-1d22

  2. 通過(guò)wsl.exe --exportwsl.exe --import移動(dòng)已有的分發(fā)
    在通過(guò)控制面板->啟用或關(guān)閉Windows功能開(kāi)啟適用于Linux的Windows子系統(tǒng)功能后,在C:\Windows\system32目錄下就有了wsl.exe

在以管理員身份運(yùn)行cmd.exe打開(kāi)windows的命令行后,我們以wsl --list --running可以查看正在運(yùn)行的wsl分發(fā),wsl --list是查看所有的分發(fā),最好的做法是通過(guò)wsl --terminate <DistritubtionName>的方式來(lái)先關(guān)閉相關(guān)的分發(fā),以免后續(xù)造成不必要的問(wèn)題。

圖片.png
  1. 這里的Ubuntu-18.04就是DistributionName,我們通過(guò)wsl --export <DistributionName> <FileName>來(lái)導(dǎo)出這個(gè)分發(fā)
  2. 執(zhí)行完后我們?cè)偻ㄟ^(guò)wsl --unregister Ubuntu-18.04來(lái)注銷這個(gè)分發(fā),以便避免存在重復(fù)的分發(fā),當(dāng)然也可以不這樣做,只是下面的命令需要指定一個(gè)不同的DistributionName
  3. 然后通過(guò)wsl --import <DistributionName> <InstallLocation> <FileName>來(lái)導(dǎo)入之前export完的分發(fā)

如何配置wsl -import后的實(shí)例,默認(rèn)以非root賬戶啟動(dòng)

在注冊(cè)表中找到HKEY_USERS\xxx\Software\Microsoft\Windows\CurrentVersion\Lxss

圖片.png

通過(guò)修改Lxss下的實(shí)例配置中的DefaultUid的值來(lái)修改wsl默認(rèn)的用戶
圖片.png

可用的目標(biāo)值可以通過(guò)在wsl中運(yùn)行cat /etc/passwd獲取,我這里用的是1000
圖片.png

通過(guò)右鍵DefaultUid->修改,會(huì)彈出如下的對(duì)話框
圖片.png

這里需要注意進(jìn)制問(wèn)題,1000對(duì)應(yīng)的16進(jìn)制數(shù)是3e8

Windows terminal相關(guān)的配置

{
    "globals" : 
    {
        "alwaysShowTabs" : true,
        "defaultProfile" : "{c6eaf9f4-32a7-5fdc-b5cf-066e8a4b1e40}",//我們把這里修改成下面對(duì)應(yīng)WSL配置中的guid的值,讓windows terminal默認(rèn)啟動(dòng)wsl,而不是cmd或者powershell
        "initialCols" : 120,
        "initialRows" : 30,
        "keybindings" : 
        [
           ...
        ],
        "requestedTheme" : "system",
        "showTabsInTitlebar" : true,
        "showTerminalTitleInTitlebar" : true
    },
    "profiles" : 
    [
        ...,
        {
            "acrylicOpacity" : 0.5,
            "closeOnExit" : true,
            "colorScheme" : "Campbell",
            "commandline" : "wsl.exe -d Ubuntu-18.04",
            "cursorColor" : "#FFFFFF",
            "cursorShape" : "bar",
            "fontFace" : "Consolas",
            "fontSize" : 10,
            "guid" : "{c6eaf9f4-32a7-5fdc-b5cf-066e8a4b1e40}",
            "historySize" : 9001,
            "icon" : "ms-appx:///ProfileIcons/{9acb9455-ca41-5af7-950f-6bca1bc9722f}.png",
            "name" : "Ubuntu-18.04",
            "padding" : "0, 0, 0, 0",
            "snapOnInput" : true,
            "startingDirectory":"http://wsl$/Ubuntu-18.04/home/ubuntu",// 這里指定wsl啟動(dòng)后,默認(rèn)的目錄位置
            "useAcrylic" : false
        }
    ],
    "schemes" : 
    [
       ...
    ]
}

參考鏈接

https://github.com/microsoft/WSL/issues/449

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容