Windows10 Linux子系統安裝/遷移到非系統盤


一、通過wsl命令遷移、備份Linux分發

操作步驟

wsl(.exe)命令用法:

 wsl -h
命令行選項無效: -h
版權所有 (c) Microsoft Corporation。保留所有權利。

用法: wsl.exe [參數] [選項...] [命令行]

用於運行 Linux 二進制文件的參數:

    如果未提供命令行,wsl.exe 將啟動默認的 shell。

    --exec, -e <命令行>
        執行指定的命令而不使用默認的 Linux shell。

    --
        按原樣傳遞剩余的命令行。

選項:
    --distribution, -d <分發版>
        運行指定的分發。

    --user, -u <用戶名>
        以指定用戶身份運行。

用於管理適用於 Linux 的 Windows 子系統的參數:

    --export <分發版> <文件名>
        將分發導出到 tar 文件。
        對於標准輸出,文件名可以是 ---import <分發版> <安裝位置> <文件名> [選項]
        將指定的 tar 文件作為新分發進行導入。
        對於標准輸入,文件名可以是 -。

        選項:
            --version <版本>
                指定用於新分發的版本。

    --list, -l [選項]
        列出分發。

        選項:
            --all
                列出所有分發,包括當前正在
                安裝或卸載的分發。

            --running
                只列出當前正在運行的分發。

            --quiet, -q
                只顯示分發名稱。

            --verbose, -v
                顯示有關所有分發的詳細信息。

    --set-default, -s <分發版>
        將分發設置為默認值。

    --set-default-version <版本>
        更改新分發的默認安裝版本。

    --set-version <分發版> <版本>
        更改指定分發的版本。

    --shutdown
        立即終止所有正在運行的分發和 WSL 2 輕型工具虛擬機。

    --terminate, -t <分發版>
        終止指定的分發。

    --unregister <分發版>
        注銷分發。

    --help
        顯示用法信息。

在這之前可以通過wsl --terminate <DistritubtionName>的方式來先關閉相關的分發,以免后續造成不必要的問題。

然后通過wsl --export <DistributionName> <FileName>選項導出Linux子系統.:

wsl--export Ubuntu-18.04 d:/Ubuntu-18.04.tar

Tips:此命令等效於:

cd /mnt/c/Users/luoz/AppData/Local/Packages/CanonicalGroupLimited.Ubuntu18.04onWindows_79rhkp1fndgsc/LocalState/rootfs
tar -cf /mnt/d/Ubuntu-18.04.tar ./*

等待數十分鍾,生成tar文件,文件結構如下:

 

執行完后我們再通過wsl --unregister Ubuntu-18.04來注銷這個分發,以便避免存在重復的分發,當然也可以不這樣做,只是下面的命令需要指定一個不同的DistributionNam. 

接下來通過wsl --import <DistributionName> <InstallLocation> <FileName>來導入之前export完的分發:

wsl --import Ubuntu d:/install/tools/Ubuntu d:/Ubuntu-18.04.tar

此時我們就能通過wsl --list看到新導入的Linux分發了。

重新導入后的Linux分發版不再以APP的形式出現於開始菜單中:

這樣操作以后不能像原來一樣在開始菜單點擊圖標啟動此Linux分發,其他並未改變。可以通過wsl -s Ubuntu命令、Windows terminal程序等方式啟動。或者將其設為默認分發版,通過wsl、bash啟動。

另一方面,不能像原來那樣在應用商店里更新。

不存在原來那樣的Ubuntu804.exe這樣的命令。

如何配置wsl -import后的實例,默認以非root賬戶啟動

 在注冊表中找到HKEY_USERS\xxx\Software\Microsoft\Windows\CurrentVersion\Lxss

xxx為你當前用戶在注冊表中的代號:

 

 

S-1-5-18:代表SYSTEM

S-1-5-19:代表LocalService

S-1-5-20:代表NetworkService

接下來的S-1-5-21-.....便是代表當前用戶了。

接下來在Lxss下找到需要更改默認賬戶的分發版:

 

 

 通過修改Lxss下的實例配置中的DefaultUid的值來修改wsl默認的用戶。

 可用的目標值可以通過在wsl中運行cat /etc/passwd獲取,比如我這里用的是1000,換算成對應的16進制數字是3e8。

 

 此時就大功告成,默認啟動不再是root用戶了。

其實也可以通過下面介紹的LxRunOffline實現:

LxRunOffline su -n <systemname> -v 1000

 二、通過LxRunOffline實現

個人分析此命令是通過修改注冊表里面的路徑以實現無損遷移的。

項目地址:https://github.com/DDoSolitary/LxRunOffline

在項目地址里下載得到LxRunOffline命令。

LxRunOffline的功能很多,遷移只是功能之一:

oboth@DESKTOP-BUFOEB1:/mnt/c/Users/luoz$ /mnt/d/LxRunOffline.exe
[ERROR] No action is specified.

Supported actions are:
    l, list            List all installed distributions.
    gd, get-default    Get the default distribution, which is used by bash.exe.
    sd, set-default    Set the default distribution, which is used by bash.exe.
    i, install         Install a new distribution.
    ui, uninstall      Uninstall a distribution.
    rg, register       Register an existing installation directory.
    ur, unregister     Unregister a distribution but not delete the installation directory.
    m, move            Move a distribution to a new directory.
    d, duplicate       Duplicate an existing distribution in a new directory.
    e, export          Export a distribution's filesystem to a .tar.gz file, which can be imported by the "install" comm
and.
    r, run             Run a command in a distribution.
    di, get-dir        Get the installation directory of a distribution.
    gv, get-version    Get the filesystem version of a distribution.
    ge, get-env        Get the default environment variables of a distribution.
    se, set-env        Set the default environment variables of a distribution.
    ae, add-env        Add to the default environment variables of a distribution.
    re, remove-env     Remove from the default environment variables of a distribution.
    gu, get-uid        Get the UID of the default user of a distribution.
    su, set-uid        Set the UID of the default user of a distribution.
    gk, get-kernelcmd  Get the default kernel command line of a distribution.
    sk, set-kernelcmd  Set the default kernel command line of a distribution.
    gf, get-flags      Get some flags of a distribution. See https://docs.microsoft.com/en-us/previous-versions/windows/
desktop/api/wslapi/ne-wslapi-wsl_distribution_flags for details.
    sf, set-flags      Set some flags of a distribution. See https://docs.microsoft.com/en-us/previous-versions/windows/
desktop/api/wslapi/ne-wslapi-wsl_distribution_flags for details.
    s, shortcut        Create a shortcut to launch a distribution.
    ec, export-config  Export configuration of a distribution to an XML file.
    ic, import-config  Import configuration of a distribution from an XML file.
    sm, summary        Get general information of a distribution.
    version            Get version information about this LxRunOffline.exe.

 

可以看到LxRunOffline.exe包含了很多wsl.exe的功能。

開始遷移:

首先用LxRunOffline.exe list或者wsl.exe -l命令查看你可以使用子系統名稱。

然后使用 lxrunoffline move 進行遷移 , -n 指定你要遷移的系統名 ,-d 指定你新系統的遷移路徑。

.\LxRunOffline.exe move -n Ubuntu -d d:\install\tools\Ubuntu

遷移過程會出現WARNING 不用管, 等待一段時間結束就算遷移完成了。

 

使用LxRunOffline.exe get-dir 查詢系統目錄,可見已經更改成功。

PS C:\Users\luoz>  d:\LxRunOffline.exe get-dir -n Ubuntu
d:\install\tools\Ubuntu

 

三、不同發行版鏡像交換/遷移

由於我最開始在應用商店下載了Ubuntu18.04 LTS,后來在子系統內升級到了Ubuntu20.04 LTS。但是名字還是顯示Ubuntu18.04,並且后續也不能在應用商店內跟隨升級。

但應用商店內的Ubuntu確一直保持在最新LTS版本,可以在應用商店內保持更新。

迫於強迫症的我,想要換到Ubuntu包,但又想保持Ubuntu18.04內的最新鏡像和軟件。於是試出了以下方法。

1、首先將Ubuntu和Ubuntu1804都轉換為wsl2。

PS D:\> wsl --set-version Ubuntu 2
PS D:\> wsl --set-version Ubuntu1804 2

 

2、進到各自的目錄交換ext4.vhdx文件。

 

 3、大功告成。若出現unregister錯誤只需用LxRunOffline重新注冊一下即可。

PS C:\Users\luoz>  d:\LxRunOffline.exe register -n Ubuntu-18.04 -d D:\install\tools\Ubuntu1804

 

至此,鏡像已經交換啦!

 四、WSL離線安裝

參照微軟官方的文檔,里面說得很詳細了。

https://docs.microsoft.com/en-us/windows/wsl/install-manual

文檔里包含了各個Linux發行版的下載鏈接。

下載后的文件是appx格式,可以直接雙擊安裝!

安裝后的路徑在系統默認目錄,可以參照上面的方法將rootfs遷移到其他目錄。

除此之外,也可以通過LxRunOffline來實現自定義安裝:

1、首先解壓.appx文件

2、用LxRunOffline安裝:

LxRunOffline -i -n <systemname> -d <rootfsdir> -f <systempack>
#d:\Downloads\LxRunOffline.exe i -n Debian -d d:\install\Debian -f d:\Debian\install.tar.gz
systemname是我們對子系統的命名,rootfsdir是我們安裝后子系統的rootfs目錄所在,systempack是我們解壓出來的install.tar.gz文件,等待運行成功后,我們自定義根目錄的子系統就創建好了。
通過命令終端輸入wsl啟動子系統,這時你會發現子系統啥都沒有,進入直接就是root用戶。另外開始菜單不會有單獨的啟動的圖標,具體喜歡哪種方式就看自己個人的喜好了!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM