查看: 454|回复: 2

[实用软件分享] move-wsl-master——把 wsl 挪到其他路径

[复制链接]

0

技术

7

魅力

1

原创

网站编辑

我最可铐

Rank: 8Rank: 8

积分
6869
人气
217
分享
594

最佳新人活跃会员

发表于 2023-9-30 20:35:07 | 显示全部楼层 |阅读模式
本帖最后由 explore 于 2023-9-30 20:37 编辑

注:来自 Git*ub 同名项目,本文仅为转载
游客,如果您要查看本帖隐藏内容请回复




脚本如下:
[PowerShell] 纯文本查看 复制代码
Set-StrictMode -Version latest;

function Cleanup()
{
    # Remove temporary file
    Write-Host "Cleaning up ...";
    Remove-Item -ErrorAction Ignore $tempFile;
}

# function to get distros
function Get-Distros()
{
    # wsl seems to output Unicode always. When parsing results in PowerShell it will try to convert
    # to Unicode strings (again) assuming it's in the Console.OutputEncoding code page (437 in my case).
    # This causes incorrect results. We are forcing Console.OutputEncoding to be Unicode here
    # to avoid the unnecessary conversion.
    $consoleEncoding = [Console]::OutputEncoding;
    [Console]::OutputEncoding = [System.Text.Encoding]::Unicode;
    $result = wsl -l -v | ConvertFrom-String -PropertyNames SELECTED, NAME, STATE, VERSION | Select-Object -Skip 1;
    [Console]::OutputEncoding = $consoleEncoding;

    return $result;
}

# get and make sure there are distros
Write-Host 'Getting distros...';
$distros = @(Get-Distros);
$distroList = @($distros | ForEach-Object { $_.NAME });
if ($distroList.Length -le 0)
{
    Write-Error 'No distro found';
    Exit 1;
}

# prompt and get the distro to move
Write-Host "Select distro to move:";
$id = 0;
$distroList | ForEach-Object { Write-Host "$($id+1): $($distroList[$id])" -ForegroundColor Yellow; $id++; }
$selected = [int](Read-Host);
if (($selected -gt $distroList.Length) -or ($selected -le 0))
{
    Write-Error "Invalid selection. Select a distro from 1 to $($distroList.Length)";
    Exit 1;
}
$distro = $distroList[$selected - 1];

# get target directory
Write-Host 'Enter WSL target directory:';
$targetFolder = Read-Host;

# check if the target folder is the root of a drive
if ($targetFolder.Length -le 3 -and $targetFolder.EndsWith(':\'))
{
    Write-Error 'Target folder cannot be root of a drive';
    Exit 1;
}

$targetFolder = $targetFolder.trimend('\');

# confirm
$confirm = Read-Host "Move $($distro) to `"$($targetFolder)`"? (Y|n)";
if ($confirm -ne 'Y')
{
    Write-Error 'User canceled';
    Exit 1;
}

# Create target dir if non existent
if (-not(Test-Path $targetFolder))
{
    New-Item -Path $targetFolder -ItemType 'directory' | Out-Null;
    if (-not($?))
    {
        Write-Error "Failed to create target folder `"$($targetFolder)`"";
        Exit 1;
    }
} elseif (Test-Path (-join($targetFolder, "\ext4.vhdx")))
{
    Write-Error "Target folder already contains an ext4.vhdx file that will get overwritten. Aborting.";
    Exit 1;
}

# Export WSL image to tar file
$tempFile = Join-Path $targetFolder "$($distro).tar";
Write-Host "Exporting VHDX to `"$($tempFile)`" ...";
& cmd /c wsl --export $distro "`"$tempFile`"";
if (-not($? -and (Test-Path $tempFile -PathType Leaf)))
{
    Write-Error "ERROR: Export failed";
    Cleanup;
    Exit 2;
}

# Unregister WSL so we can register it again at new location
Write-Host "Unregistering WSL ..."
& cmd /c wsl --unregister $distro | Out-Null

# Importing WSL at new location
Write-Host "Importing $distro from $targetFolder..."
& cmd /c wsl --import $distro $targetFolder "`"$tempFile`"" --version $distros[$selected -1].VERSION;

# Validating
$newDistros = @(Get-Distros);
$newDistroList = @($newDistros | ForEach-Object { $_.NAME });
if ($newDistroList -notcontains $distro)
{
    Write-Error "Import failed. Distro not found. Export file at $tempFile";
    Exit 3;
}

if (-not(Test-Path "$($targetFolder)\ext4.vhdx") -And -not(Test-Path "$($targetFolder)\rootfs"))
{
    Write-Error "ERROR: Import failed. Target file/folder not found. Export file at $tempFile";
    Exit 4;
}

Cleanup;

Write-Host "Done!" -ForegroundColor Green;





以下为 README.md 的内容,使用 https://mm2bc.ustc-zzzz.net/ 转为 BBcode.


Move WSL

PowerShell script to move WSL 1 and WSL 2 distros VHDX file to a different location.



Usage

Warning

This script uses official wsl commands and was used by a lot of people. Nevertheless some people had weird issues that resulted in broken WSL disks.
Make sure you have a backup of your data, so you can restore in case of an error.

Interactive way of moving wsl for Windows PowerShell.

1) ./move-wsl.ps1 2) Select your distro 3) Enter your target (i.e. D:\wsl target\ubuntu)

Moving Docker WSL

Before moving Docker WSL make sure to stop the Docker service. Otherwise Docker will crash and you may need to reset it to factory defaults.

FAQ

Default user was switched to root when moving a distro

Set your default user inside your distro by adding the following configuration to your /etc/wsl.conf.

  1. [user]
  2. default=YOUR_USERNAME
复制代码

If the file doesn't exist create it manually. Then exit your distro, terminate it (wsl -t YOUR_DISTRO) and start it again. For further options see Microsoft Docs.

Some distributions also allow settings the default user via command line with YOUR_DISTRO config --default-user YOUR_USER (e.g. ubuntu config --default-user johndoe). Make sure to shutdown your distro before (wsl -t YOUR_DISTRO).

Standard distro switched when moving it

Since we need to unregister to import it with the same name, the standard distro can be switched. Just set your standard distro again:

  1. wsl -s YOUR_DISTRO
复制代码

WSL version was switched when moving distro

On import the distro will be registered with the current default WSL version. You can set your default WSL version with wsl --set-default-version <Version>. When the WSL version was accidentally changed while moving, you can set the version with wsl --set-version <Distro> <Version>.



本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

0

技术

6

魅力

1

原创

版主

禁止发言

Rank: 7Rank: 7Rank: 7

积分
6160
人气
175
分享
56

最佳新人活跃会员灌水之王

发表于 2023-10-1 09:20:22 | 显示全部楼层
回来发帖了?
希望输出更优质的内容
一只压力超大的屑
我的博客:点此进入

0

技术

9

魅力

1

原创

版主

禁止访问

Rank: 7Rank: 7Rank: 7

积分
7734
人气
176
分享
52

最佳新人活跃会员

发表于 2023-10-3 11:17:50 | 显示全部楼层
不懂就问,G*****b为什么要用星号
这个网站本身不违法啊
小菜鸡一枚~
Gitee主页:https://gitee.com/juruoqwq
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表