Skip to content

Instantly share code, notes, and snippets.

@kissgyorgy
Created July 24, 2023 13:21
Show Gist options
  • Save kissgyorgy/e916d9f4e678343682cb15257367a1f7 to your computer and use it in GitHub Desktop.
Save kissgyorgy/e916d9f4e678343682cb15257367a1f7 to your computer and use it in GitHub Desktop.
WSL: Automatically mount disk on WSL startup
[Unit]
Description = Mount disk
Before = local-fs-pre.target
[Service]
User = root
Type = oneshot
RemainAfterExit = yes
ExecStart = /usr/local/sbin/mount-disk.sh
[Install]
WantedBy=default.target
#!/bin/bash
set -eo pipefail
if [[ "$EUID" -ne 0 ]]; then
echo "Script should run as root"
exit 1
fi
DISK='/dev/disk/by-id/<disk-id>'
# Needs a Windows Task for mounting the disk to WSL
# See: https://github.com/microsoft/WSL/issues/6073#issuecomment-1266405095
/mnt/c/Windows/system32/schtasks.exe /run /tn mount-wsl-disk
# schtasks returns immediately, the disk might not be mounted
# before we want to run the mount command
while [[ ! -b "$DISK" ]]; do
echo -n .
sleep 1
done
mount "$DISK-part1" /backup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment