Tag Archive ย้าย

Byphunsanit

WSL: move to another drive

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

  1. สร้างไฟล์ WSL2_move.ps1 โดยมีเนื้อหา (Download)
    &lt;#<br>PowerShell move Windows Subsystem for Linux 2 (WSL2) version 1.1<br>#by pitt phunsanit<br>https://pitt.plusmagi.com<br>[email protected]<br><br>replace $DistributionName with WSL Linux DistributionNames<br>example Alpine, docker-desktop-data, docker-desktop, Ubuntu<br><br>replace $folderPath with new path<br>#><br><br>$DistributionName = 'Ubuntu'<br>$folderPath = 'C:\UsersWSL\'<br><br># common<br><br>Clear-Host<br><br>$commandString = ''<br><br>#run and show message and exit batch<br>function CommandOrExitBatch {<br>	param(<br>		[Parameter(Mandatory=$true)]<br>		[string] $commandString<br>	  )<br><br>	Write-Host $commandString -f Blue<br><br>	try{<br>		# Use strict mode within iex for better error handling<br>		#Invoke-Expression -Command { $commandString } -UseStrict  -ErrorAction Stop<br>		#Invoke-Expression -Command $commandString<br>		Invoke-Expression -Command $commandString<br>	} catch {<br>		Write-Error "Script failed with error: $_.Exception.Message"<br><br>		Write-Warning "Exit code: $($_.Exception.InnerException.ExitCode)"# Assuming script sets exit code<br><br>		Write-Output "This won't be executed"<br><br>		exit 1<br>	}<br><br>}<br><br># process<br><br>#list installed linux DistributionNames<br>Write-Host 'list installed linux DistributionNames' -f Blue<br><br>$commandString = 'wsl --list --verbose'<br><br>CommandOrExitBatch -commandString $commandString<br><br>#confirm<br>Write-Host 'WSL Linux DistributionNames is"' + $DistributionName + '"' -f Blue<br><br>#archive type<br>$archiveType = Read-Host "Select archive type (tar is default, q to quit): tar, vhd, q"<br>switch ($archiveType) {<br>	'tar' { Write-Host "Selected tar archive format." }<br>	'vhd'  { Write-Host "Specifies the export distribution should be a .vhdx file (this is only supported using WSL 2)" }<br>	'q' { Write-Host "Exiting..."; exit }  # Exit the batch script when q is chosen<br>	default { Write-Host "Invalid selection. Defaulting to tar archive format." ; $archiveType = "tar" }<br>}<br><br>Write-Host "You have chosen: $archiveType"<br><br>if($archiveType -eq 'q')<br>{<br>	GOTO :eof  # Exit the subroutine<br>}<br><br>if($archiveType -eq 'vhd')<br>{<br>	$exportPath = [string]::Concat($folderPath, $DistributionName, '.vhdx')<br>}<br>else<br>{<br>	$exportPath = [string]::Concat($folderPath, $DistributionName, '.tar')<br>}<br>$importPath = [string]::Concat($folderPath, $DistributionName)<br><br>#make folder<br>#Check if Folder exists<br>If(!(Test-Path -Path $folderPath))<br>{<br>	#PowerShell create directory<br>	Write-Host 'create directory "' + $importPath + '".' -f Blue<br><br>	$commandString = 'New-Item -ItemType Directory -Path ' + $importPath<br><br>	CommandOrExitBatch -commandString $commandString<br><br>	Write-Host 'New folder "'$importPath'" created successfully.' -f Green<br>}<br>Else<br>{<br>	Write-Host 'Folder "'$importPath'" already exists.' -f Yellow<br>}<br><br>#stop distribution name<br>Write-Host 'stop DistributionName' -f Blue<br><br>$commandString = 'wsl --terminate ' + $DistributionName<br><br>CommandOrExitBatch -commandString $commandString<br><br>#export DistributionName<br>Write-Host 'export DistributionName' -f Blue<br><br>if($archiveType -eq 'vhd')<br>{<br>	$commandString = 'wsl --export --vhd ' + $DistributionName + ' ' + $exportPath<br>}<br>else<br>{<br>	$commandString = 'wsl --export ' + $DistributionName + ' ' + $exportPath<br>}<br><br>CommandOrExitBatch -commandString $commandString<br><br>#Unregister DistributionName<br>Write-Host 'Unregister DistributionName' -f Blue<br><br>$commandString = 'wsl --unregister ' + $DistributionName<br><br>CommandOrExitBatch -commandString $commandString<br><br>#import DistributionName<br>Write-Host 'import DistributionName' -f Blue<br><br>if($archiveType -eq 'vhd')<br>{<br>	$commandString = 'wsl --import --vhd ' + $DistributionName + ' ' + $importPath + ' ' + $exportPath<br>}<br>else {<br>	$commandString = 'wsl --import ' + $DistributionName + ' ' + $importPath + ' ' + $exportPath<br>}<br><br>CommandOrExitBatch -commandString $commandString<br><br>#update WSL<br>Write-Host 'update WSL' -f Blue<br><br>$commandString = 'wsl --update'<br><br>CommandOrExitBatch -commandString $commandString<br><br>#list installed linux DistributionNames<br>Write-Host 'list installed linux DistributionNames' -f Blue<br><br>$commandString = 'wsl --list --verbose'<br><br>CommandOrExitBatch -commandString $commandString<br><br>#summary<br>Write-Host 'Move WSL"' + $DistributionName + '" to "' + $importPath + '" successfully?' -f Green<br><br>#show file<br>Write-Host 'show file' -f Blue<br><br>$commandString = 'Get-ChildItem -Path ' + $folderPath<br><br>CommandOrExitBatch -commandString $commandString<br><br>#end of file
  2. เรียกใช้โดยเปิด PowerShell โดยสิทธิ์ administrator
  3. cd ไป folder ที่ save ไฟล์ WSL2_move.ps1 เช่น
    cd C:\UsersGit\phunsanit\snippets\WSL
    แล้ว enter
  4. run โดยใช้
    .\WSL2_move.ps1
    แล้ว enter
  5. เปิด terminal ใหม่ จะเห็นว่ามี Ubuntu เพิ่มขึ้นมา

ดูเพิ่มเติม