PlusMagi's Blog By Pitt Phunsanit

WSL: move to another drive

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

  1. สร้างไฟล์ WSL2_move.ps1 โดยมีเนื้อหา (Download)
    <#
    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 = &apos;Ubuntu&apos;
    $folderPath = &apos;C:\Users\Shared\WSLs\&apos;
    
    # common
    
    Clear-Host
    
    $commandString = &apos;&apos;
    
    #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 &apos;Script failed with error: &apos; + $ ($_.Exception.Message) # Assuming script sets exit code
    		Write-Warning &apos;Exit code: &apos; + $ ($_.Exception.InnerException.ExitCode) Write-Output "This won&apos;t be executed"
    
    		exit 1
    	}
    
    }
    
    # process
    
    #list installed linux DistributionNames
    Write-Host &apos;list installed linux DistributionNames&apos; -f Blue
    
    $commandString = &apos;wsl --list --verbose&apos;
    
    CommandOrExitBatch -commandString $commandString
    
    #confirm
    Write-Host (&apos;WSL Linux DistributionNames is "&apos; + $ ($DistributionName) + &apos;"&apos;) -f Blue
    
    #archive type
    $archiveType = Read-Host "Select archive type (tar is default, q to quit) : tar, vhd, q"
    switch ($archiveType) {
    	&apos;tar&apos; { Write-Host "Selected tar archive format." }
    	&apos;vhd&apos; { Write-Host "Specifies the export distribution should be a .vhdx file (this is only supported using WSL 2) " }
    	&apos;q&apos; { 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 (&apos;You have chosen: &apos; + $archiveType) if ($archiveType -eq &apos;q&apos;) {
    	GOTO :eof # Exit the subroutine
    }
    
    if ($archiveType -eq &apos;vhd&apos;) {
    	$exportPath = [string]::Concat ($folderPath, $DistributionName, &apos;.vhdx&apos;) }
    else
    {
    	$exportPath = [string]::Concat ($folderPath, $DistributionName, &apos;.tar&apos;) }
    $importPath = [string]::Concat ($folderPath, $DistributionName) #make folder
    #Check if Folder exists
    If (! (Test-Path -Path $exportPath)) {
    	#PowerShell create directory
    	Write-Host (&apos;create directory "&apos; + $ ($importPath) + &apos;"&apos;) -f Blue
    
    	$commandString = &apos;New-Item -ItemType Directory -Path &apos; + $importPath
    
    	CommandOrExitBatch -commandString $commandString
    
    	Write-Host (&apos;New folder "&apos; + $ ($importPath) + &apos;" created successfully.&apos;) -b White -f DarkGreen
    }
    Else
    {
    	Write-Host (&apos;Folder "&apos; + $ ($importPath) + &apos;" already exists.&apos;) -f Yellow
    }
    
    #stop distribution name
    Write-Host &apos;stop DistributionName&apos; -f Blue
    
    $commandString = &apos;wsl --terminate &apos; + $DistributionName
    
    CommandOrExitBatch -commandString $commandString
    
    #export DistributionName
    Write-Host &apos;export DistributionName&apos; -f Blue
    
    if ($archiveType -eq &apos;vhd&apos;) {
    	$commandString = &apos;wsl --export --vhd &apos; + $DistributionName + &apos; &apos; + $exportPath
    }
    else
    {
    	$commandString = &apos;wsl --export &apos; + $DistributionName + &apos; &apos; + $exportPath
    }
    
    CommandOrExitBatch -commandString $commandString
    
    #Unregister DistributionName
    Write-Host &apos;Unregister DistributionName&apos; -f Blue
    
    $commandString = &apos;wsl --unregister &apos; + $DistributionName
    
    CommandOrExitBatch -commandString $commandString
    
    #import DistributionName
    Write-Host &apos;import DistributionName&apos; -f Blue
    
    if ($archiveType -eq &apos;vhd&apos;) {
    	$commandString = &apos;wsl --import --vhd &apos; + $DistributionName + &apos; &apos; + $importPath + &apos; &apos; + $exportPath
    }
    else {
    	$commandString = &apos;wsl --import &apos; + $DistributionName + &apos; &apos; + $importPath + &apos; &apos; + $exportPath
    }
    
    CommandOrExitBatch -commandString $commandString
    
    #update WSL
    Write-Host &apos;update WSL&apos; -f Blue
    
    $commandString = &apos;wsl --update&apos;
    
    CommandOrExitBatch -commandString $commandString
    
    #list installed linux DistributionNames
    Write-Host &apos;list installed linux DistributionNames&apos; -f Blue
    
    $commandString = &apos;wsl --list --verbose&apos;
    
    CommandOrExitBatch -commandString $commandString
    
    #summary
    Write-Host (&apos;Move WSL "&apos; + $ ($DistributionName) + &apos;" to "&apos; + $ ($importPath) + &apos;" successfully?&apos;) -b White -f DarkGreen
    
    #show file
    Write-Host &apos;show file&apos; -f Blue
    
    $commandString = &apos;Get-ChildItem -Path &apos; + $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 เพิ่มขึ้นมา

ดูเพิ่มเติม

Exit mobile version