Tag Archive folder

Byphunsanit

WSL: move to another drive

โดยปกติ wsl จะถูกติดตั้งใน %USERPROFILE%\AppData\Local\Packages ถ้าจะย้ายไปไดรฟ์อื่นหรือย้ายไปที่อื่น เพราะ drive c เต็มหรืออยากจะ restore windows ทำได้โดย

  1. สร้างไฟล์ WSL2_move.ps1 โดยมีเนื้อหา ( Download )
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    <#
    PowerShell move Windows Subsystem for Linux 2 (WSL2) version 1.2
    #by pitt phunsanit
    https://pitt.plusmagi.com
    phunsanit@gmail.com
     
    replace $DistributionName with WSL Linux DistributionNames
    example Alpine, docker-desktop-data, docker-desktop, Ubuntu
     
    replace $folderPath with new path
    #>
     
    $DistributionName = 'Ubuntu'
    $folderPath = 'C:\Users\Shared\WSLs\'
     
    # common
     
    Clear-Host
     
    $commandString = ''
     
    #run and show message and exit batch
    function CommandOrExitBatch {
        param(
            [Parameter(Mandatory=$true)]
            [string] $commandString
          )
     
        Write-Host $commandString -f Yellow
     
        try{
            # Use strict mode within iex for better error handling
            #Invoke-Expression -Command { $commandString } -UseStrict  -ErrorAction Stop
            #Invoke-Expression -Command $commandString
            Invoke-Expression -Command $commandString
        } catch {
            Write-Error 'Script failed with error: ' + $($_.Exception.Message)
            # Assuming script sets exit code
            Write-Warning 'Exit code: ' + $($_.Exception.InnerException.ExitCode)
     
            Write-Output "This won't be executed"
     
            exit 1
        }
     
    }
     
    # process
     
    #list installed linux DistributionNames
    Write-Host 'list installed linux DistributionNames' -f Blue
     
    $commandString = 'wsl --list --verbose'
     
    CommandOrExitBatch -commandString $commandString
     
    #confirm
    Write-Host('WSL Linux DistributionNames is "' + $($DistributionName) + '"') -f Blue
     
    #archive type
    $archiveType = Read-Host "Select archive type (tar is default, q to quit): tar, vhd, q"
    switch ($archiveType) {
        'tar' { Write-Host "Selected tar archive format." }
        'vhd'  { Write-Host "Specifies the export distribution should be a .vhdx file (this is only supported using WSL 2)" }
        'q' { Write-Host "Exiting..."; exit # Exit the batch script when q is chosen
        default { Write-Host "Invalid selection. Defaulting to tar archive format." ; $archiveType = "tar" }
    }
     
    Write-Host('You have chosen: ' + $archiveType)
     
    if($archiveType -eq 'q')
    {
        GOTO :eof  # Exit the subroutine
    }
     
    if($archiveType -eq 'vhd')
    {
        $exportPath = [string]::Concat($folderPath, $DistributionName, '.vhdx')
    }
    else
    {
        $exportPath = [string]::Concat($folderPath, $DistributionName, '.tar')
    }
    $importPath = [string]::Concat($folderPath, $DistributionName)
     
    #make folder
    #Check if Folder exists
    If(!(Test-Path -Path $exportPath))
    {
        #PowerShell create directory
        Write-Host('create directory "' + $($importPath) + '"') -f Blue
     
        $commandString = 'New-Item -ItemType Directory -Path ' + $importPath
     
        CommandOrExitBatch -commandString $commandString
     
        Write-Host('New folder "' + $($importPath) + '" created successfully.') -b White -f DarkGreen
    }
    Else
    {
        Write-Host('Folder "' + $($importPath) + '" already exists.') -f Yellow
    }
     
    #stop distribution name
    Write-Host 'stop DistributionName' -f Blue
     
    $commandString = 'wsl --terminate ' + $DistributionName
     
    CommandOrExitBatch -commandString $commandString
     
    #export DistributionName
    Write-Host 'export DistributionName' -f Blue
     
    if($archiveType -eq 'vhd')
    {
        $commandString = 'wsl --export --vhd ' + $DistributionName + ' ' + $exportPath
    }
    else
    {
        $commandString = 'wsl --export ' + $DistributionName + ' ' + $exportPath
    }
     
    CommandOrExitBatch -commandString $commandString
     
    #Unregister DistributionName
    Write-Host 'Unregister DistributionName' -f Blue
     
    $commandString = 'wsl --unregister ' + $DistributionName
     
    CommandOrExitBatch -commandString $commandString
     
    #import DistributionName
    Write-Host 'import DistributionName' -f Blue
     
    if($archiveType -eq 'vhd')
    {
        $commandString = 'wsl --import --vhd ' + $DistributionName + ' ' + $importPath + ' ' + $exportPath
    }
    else {
        $commandString = 'wsl --import ' + $DistributionName + ' ' + $importPath + ' ' + $exportPath
    }
     
    CommandOrExitBatch -commandString $commandString
     
    #update WSL
    Write-Host 'update WSL' -f Blue
     
    $commandString = 'wsl --update'
     
    CommandOrExitBatch -commandString $commandString
     
    #list installed linux DistributionNames
    Write-Host 'list installed linux DistributionNames' -f Blue
     
    $commandString = 'wsl --list --verbose'
     
    CommandOrExitBatch -commandString $commandString
     
    #summary
    Write-Host('Move WSL "' + $($DistributionName) + '" to "' + $($importPath) + '" successfully?') -b White -f DarkGreen
     
    #show file
    Write-Host 'show file' -f Blue
     
    $commandString = 'Get-ChildItem -Path ' + $folderPath
     
    CommandOrExitBatch -commandString $commandString
     
    #end of file
  2. เรียกใช้โดยเปิด PowerShell โดยสิทธิ์ administrator
  3. cd ไป folder ที่ save ไฟล์ WSL2_move.ps1 เช่น
    cd C:\Users\Shared\Gits\phunsanit\snippets\WSL
    แล้ว enter
  4. run โดยใช้
    .\WSL2_move.ps1
    แล้ว enter
  5. เปิด terminal ใหม่ จะเห็นว่ามี Ubuntu เพิ่มขึ้นมา

ดูเพิ่มเติม