Skip to content

Instantly share code, notes, and snippets.

@rxw1
Last active May 6, 2023 23:12
Show Gist options
  • Save rxw1/2ddb64ab95dbb1c005f4ce688bef303f to your computer and use it in GitHub Desktop.
Save rxw1/2ddb64ab95dbb1c005f4ce688bef303f to your computer and use it in GitHub Desktop.
Mount Windows Drives to WSL
# Author: https://github.com/rxw1 <rxw1@pm.me>
# Last edit: Sun May 7 01:10:50 AM CEST 2023
# https://gist.github.com/rxw1/2ddb64ab95dbb1c005f4ce688bef303f
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
$admin = $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
if (!$admin) {
Write-Output("Script must be run as Administrator")
exit 1
}
class Disk {
[int]$DeviceId
[string]$SerialNumber
[string]$FriendlyName
[string]$ObjectId
[string]$Name
[bool]$Mount
[string]$UUID
}
$disks = @(
[Disk]@{
DeviceId = 1;
Mount = $true;
Name = "Something";
SerialNumber = "WD-XO0JAR9OOPH3";
FriendlyName = "WDC WD40EFRX-68WT0N0";
ObjectId = "60b01762-c524-4820-83ca-499c1a0d9472";
UUID = "6c6fb321-abfc-42b2-ad63-b3b48aec5239"
}
[Disk]@{
DeviceId = 4;
Mount = $true;
Name = "Gentoo";
SerialNumber = "S1GHEUNAO5BAI7XU";
FriendlyName = "Samsung SSD 840 EVO 250GB";
ObjectId = "82f4095c-5008-4e4e-9d86-71b144073982";
UUID = "abe4a558-a9b0-4f33-9e61-c3124c8d1666";
}
)
# How the ObjectId is obtained:
#
# Get-PhysicalDisk
# | Select-Object DeviceId,SerialNumber,FriendlyName,ObjectId
# | Sort-Object { [int]$_.DeviceId }
# | Format-List
#
# We take the last UUID contained in the ObjectId after PD, as it seems to be
# the only real(?) unique identifier that works for all kinds of disks.
#
# DeviceId might change sometimes, depending on the disks physically connected
# to the computer. SerialNumber is not unique for some disks and just reports
# 00000000000000000000.
function Get-Linux-Device {
$d = $null # Device
$n = 0 # Retry counter
# Try to get the device 10 times, as it might not be available immediately
Do {
$d = wsl sudo blkid -U $args[0] 2>$null
if ($null -eq $d ) {
wsl sudo blkid --match-tag none 2>$null
}
$n++
} Until (($null -ne $d) -or ($n -eq 10))
return $d
if ($?) {
Write-Host $d
}
else {
Write-Host "blkid failed for $($args[0])"
}
if ($d -eq "") {
$d = wsl blkid -U $args[0] 2>$null
if ($d -eq "") {
$d = wsl blkid -U $args[0] 2>$null
}
}
return $d
}
# Main loop: Iterate over all disks and mount them to WSL
ForEach ($d in @($disks)) {
ForEach ($p in @(Get-PhysicalDisk)) {
if ($d.Mount) {
# Match our noted UUID to the ObjectId returned by Get-PhysicalDisk
if ($p.Objectid -match $d.ObjectId) {
# Pretty output
Write-Host "Mounting " -NoNewLine
Write-Host "$($d.Name) " -ForegroundColor Blue -NoNewline
Write-Host "$($d.SerialNumber) $($d.ObjectId) " -ForegroundColor Magenta
# Mount the drive to WSL
wsl --mount \\.\PHYSICALDRIVE$($p.DeviceId) --bare
$linuxDevice = Get-Linux-Device $d.UUID
Write-Host "$($linuxDevice)" -ForegroundColor Green
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment