From bea102ce50a5b0a273b29eb83fd3828ca6873d87 Mon Sep 17 00:00:00 2001 From: David Thurstenson Date: Sat, 6 Jun 2020 17:23:33 -0700 Subject: [PATCH] Added mountwinshare.sh to facilitate automatic mounting of a mapped network share to the wsl env --- .bashrc.d/mountwinshare.sh | 41 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 .bashrc.d/mountwinshare.sh diff --git a/.bashrc.d/mountwinshare.sh b/.bashrc.d/mountwinshare.sh new file mode 100644 index 0000000..7d13a15 --- /dev/null +++ b/.bashrc.d/mountwinshare.sh @@ -0,0 +1,41 @@ +# 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