I'm running Ubuntu 22.04 through WSL 2, and I have Google Drive through Windows 11 which creates a virtual disk on G:
. I have the drive mounted in WSL at /mnt/g
. Sometimes, I think because of the network disconnecting when the computer sleeps, /mnt/g
ends up in a weird state where it seems to exist and yet not exist at the same time. This is the output of ls -al /mnt
:
ls: cannot access '/mnt/g': No such devicetotal 16Kdrwxr-xr-x 8 root root 4.0K Nov 21 21:13 ./drwxr-xr-x 27 root root 4.0K Dec 2 08:52 ../drwxr-xr-x 1 me me 4.0K Dec 1 08:40 c/drwxr-xr-x 2 root root 4.0K Nov 21 21:13 d/drwxr-xr-x 2 root root 4.0K Jun 2 2023 f/d????????? ? ? ? ? ? g/drwxrwxrwt 2 root root 60 Dec 2 08:52 wsl/drwxrwxrwt 7 root root 300 Dec 5 06:23 wslg/
I realize I'm supposed to re-mount the drive if this happens. I have a script called ezmount
that simplifies the process. Here are the contents:
#!/usr/bin/env bashDRIVE_LETTER="$1"if [[ ! "$DRIVE_LETTER" ]]; then echo Please enter a drive letter. exit 1fimount_path="/mnt/${DRIVE_LETTER,,}"if [[ ! -e "$mount_path" ]]; then sudo mkdir "$mount_path" if [[ "$?" ]]; then echo Something went wrong in trying to create the directory "$mount_path". exit 1 fifisudo mount -t drvfs "${DRIVE_LETTER^^}:" "$mount_path" -o "uid=$(id -u $USER),gid=$(id -g $USER),metadata"
When the script gets to the line sudo mkdir "$mount_path"
, the response is mkdir: cannot create directory ‘/mnt/g’: File exists
, and the script terminates without mounting. So even though checking for the existence of /mnt/g
through [[ -e /mnt/g ]]
returns false, Ubuntu still claims the path exists. In general, I want the script to create the mount path if it doesn't exist in case a new drive is added, but in this scenario I want it to realize the path already exists and re-mount it. I don't want to have to type the long mount
command myself, that's why I wrote the script. Is there some command I should use other than -e
that would work in this scenario?