Created
July 24, 2023 13:21
-
-
Save kissgyorgy/e916d9f4e678343682cb15257367a1f7 to your computer and use it in GitHub Desktop.
WSL: Automatically mount disk on WSL startup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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