bashrc/.bashrc.d/mountwinshare.sh

42 lines
1.3 KiB
Bash
Raw Normal View History

# Mount winshare within wsl on win vm
#
# Necessary to mount any device that doesn't get mounted automatically by wsl
#
# Ref: https://docs.microsoft.com/en-us/archive/blogs/wsl/file-system-improvements-to-the-windows-subsystem-for-linux
mountwinshare() {
local targetdir="/mnt/z"
local sharedir="Z:"
if [ "$1" == "-v" ]; then
local verbose=1
fi
# Only do this in a specific env
if [ "$(hostname)" == "DT-KD-VM" ]; then
# Create the mountpoint if it doesn't exist
if [ ! -d "$targetdir" ]; then
if sudo mkdir "$targetdir"; then
[ -n "$verbose" ] && echo "mountwinshare: Created mount directory: $targetdir"
else
[ -n "$verbose" ] && echo "mountwinshare: Failed to create mount directory: $targetdir"
fi
fi
# Check if $targetdir is already in use as a mountpoint
if mountpoint -q "$targetdir"; then
[ -n "$verbose" ] && echo "mountwinshare: $targetdir already in use. Skipping."
else
# Do the thing! (see reference for details)
if sudo mount -t drvfs "$sharedir" "$targetdir"; then
[ -n "$verbose" ] && echo "mountwinshare: Mounted $sharedir to $targetdir"
else
[ -n "$verbose" ] && echo "mountwinshare: Failed to mount $sharedir to $targetdir"
fi
fi
else
[ -n "$verbose" ] && echo "mountwinshare: wrong env. skipping"
fi
}
mountwinshare