Advertisement
JessiBaughman

Mount Windows Drives in WSL

Apr 4th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.52 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. # winmount - A script to mount/unmount Windows drives on WSL other than C:
  4. #
  5. # Usage: winmount <Windows Drive letter> (e.g. winmount d)
  6. #   -Note: input is not case sensitive
  7. #
  8. # G+ Post: https://plus.google.com/+JessiBaughman/posts/W8ZhpJ36Hap
  9. # Created by: Jessi A. Baughman
  10. # Last Modified: 2018-04-04
  11.  
  12. thisScript=$(basename $0)
  13.  
  14. if [[ $# -eq 1 ]];then
  15.     # Convert input to respective upper/lower case values
  16.     x=$1
  17.     windrive=${x^^}
  18.     wsldrive=${x,,}
  19.    
  20.     if [[ $wsldrive == 'c' ]]; then # Prevent problems with /mnt/c
  21.         echo;echo "ERROR: Cannot modify C: mount"
  22.         echo;exit
  23.     fi
  24.  
  25.     # Check if drive is already mounted in WSL
  26.     e=`mount | grep -c /mnt/$wsldrive`
  27.     if [[ $e -gt 0 ]]; then # Is mounted
  28.         echo;echo "Drive $windrive: is already mounted."
  29.         echo -n "Unmount $windrive: drive [y/N]? "
  30.         unmount='N'
  31.         read unmount
  32.         if [[ ${unmount,,} == 'y' || ${unmount^^} == 'Y' ]]; then
  33.             sudo umount /mnt/$wsldrive
  34.             echo;echo "Drive $windrive: unmounted."
  35.         else
  36.             echo;echo "Drive $windrive: left mounted."
  37.         fi
  38.     else # Drive is not mounted
  39.         if [[ ! -d /mnt/$wsldrive ]]; then # Directory needs created
  40.             sudo mkdir /mnt/$wsldrive
  41.         fi
  42.        
  43.         # Mount drive
  44.         echo;e=0
  45.         sudo mount -t drvfs $windrive: /mnt/$wsldrive || e=1
  46.        
  47.         if [[ $e -gt 0 ]];then
  48.             echo "Failed to mount $windrive:";echo; exit
  49.         else       
  50.             echo "Drive $windrive: mounted to /mnt/$wsldrive"
  51.         fi
  52.     fi
  53. else
  54.     echo;echo "Usage: $thisScript <Windows Drive Letter to Mount>"
  55.          echo " e.g.: $thisScript d"
  56. fi
  57. echo
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement