Mounting Windows shares containing spaces Topic is solved

Having issues with your DietPi installation, or, found a bug? Post it here.
SNG
Posts: 9
Joined: Thu Apr 16, 2020 1:11 pm

Mounting Windows shares containing spaces

Post by SNG »

I'm looking for some help mounting a windows network samba share which contains spaces in the path. I can mount it if I edit /etc/fstab directly, using a successful mount of a path without spaces as a template and using \040 to replace the spaces in my desired path. However, Add network drive from DietPi-Drive_Manager will not accept the spaces in the network path dialogue and fails with the following output:-
[FAILED] Samba mount failed with the following error output:

mount error(95): Operation not supported
Refer to the mount.cifs(8) manual page (e.g. man mount.cifs)
mount error(95): Operation not supported
Refer to the mount.cifs(8) manual page (e.g. man mount.cifs)
mount error(2): No such file or directory
Refer to the mount.cifs(8) manual page (e.g. man mount.cifs)
mount error(2): No such file or directory
Refer to the mount.cifs(8) manual page (e.g. man mount.cifs)
mount error(20): Not a directory
Refer to the mount.cifs(8) manual page (e.g. man mount.cifs)

I've tried using single and double quotes in various ways to enclose the path and I've tried replacing the spaces with the escape sequence \040 in the share name dialogue, all fail the same way.

Is there a way to make it work?
Regards
SNG
User avatar
Joulinar
Posts: 2059
Joined: Sat Nov 16, 2019 12:49 am

Re: Mounting Windows shares containing spaces

Post by Joulinar »

well I guess you found the workaround already be editing /etc/fstab

For the rest, I guess @MichaIng would need to have a look.
Pls let us know if a solution is working. This could help others if they hit by similar situation. Your DietPi Team
User avatar
WarHawk
Posts: 608
Joined: Thu Jul 20, 2017 8:55 am

Re: Mounting Windows shares containing spaces

Post by WarHawk »

I believe you have to add a / after each word

mount My/ Computer/ and the like

Ah correction....I googled and found that you put a \040 where the space would be

Like My\040Computer
User avatar
Joulinar
Posts: 2059
Joined: Sat Nov 16, 2019 12:49 am

Re: Mounting Windows shares containing spaces

Post by Joulinar »

Hi,

I guess the \040 syntax is for fstab, not for shell command line. A working syntax could be like this. Was working quite well on my test.

Code: Select all

mount -t cifs '//192.168.0.10/test 123 test' /mnt/samba -o user=some,pass=user
I placed a small 'echo' inside drive manager script. This is the output. So \040 is already placed BUT using double backslash :?

Code: Select all

mount -t cifs -o username=some,password=user,iocharset=utf8,uid=dietpi,gid=dietpi,file_mode=0770,dir_mode=0770,vers=3.1.1 //192.168.0.10/test\\040123\\040test /mnt/samba
Anyway it would be better to use quotes.
Pls let us know if a solution is working. This could help others if they hit by similar situation. Your DietPi Team
SNG
Posts: 9
Joined: Thu Apr 16, 2020 1:11 pm

Re: Mounting Windows shares containing spaces

Post by SNG »

Thanks for your efforts, Joulinar.

Following on I had a look at the drive manager script and line 1757 is:
"samba_clientshare=${G_WHIP_RETURNED_VALUE//[[:space:]]/\\\\040}"
which does seem to replace spaces with "\\040", so I changed it to:
samba_clientshare=${G_WHIP_RETURNED_VALUE//[[:space:]]/\\040}

Still had the same problem though, which is strange 'cos it works fine if I edit fstab by hand! Looking further the script tries to mount the share using mount cifs versions from 3.1.1 down to 1.0 and on success updates fstab with that command.

My set-up is latest Buster dietpi running on a pi3 and the share is on an ancient version of a Windows 2008 server (WHS 2011). I did some tests and from the command line, versions above 2.1 do not work even with a non space path, so I cannot test further with versions 3.0 and 3.1.1.

Testing versions 1.0, 2.0 and 2.1, I tried to mount the the problem share from the command line on my system and found that I could not get "/040" to work as a substitute for a space using any of them. The command line I tested was:
mount -t cifs -o username="MyUser",password="thisPswd",iocharset=utf8,uid=dietpi,gid=dietpi,file_mode=0770,dir_mode=0770,vers=2.1 //192.168.99.201/BTSync\040Share /mnt/sambaBTsync

However both enclosing the share path in double quotes or using a simple backslash space "\ ", to escape the space, worked just fine:
mount -t cifs -o username="MyUser",password="thisPswd",iocharset=utf8,uid=dietpi,gid=dietpi,file_mode=0770,dir_mode=0770,vers=2.1 "//192.168.99.201/BTSync Share" /mnt/sambaBTsync
or
mount -t cifs -o username="MyUser",password="thisPswd",iocharset=utf8,uid=dietpi,gid=dietpi,file_mode=0770,dir_mode=0770,vers=2.1 //192.168.99.201/BTSync\ Share /mnt/sambaBTsync.

My conclusion is that although the code in drive manager may work for versions 3.0 and abover (I cannot test it), for version 2.1 and below the script is broken because mount cifs does not support the \040 substitution for a space in the same way it is used in fstab.

Does my testing and conclusion seem correct, or have I misunderstood how it all works? Any comments welcome.

Regards
SNG
User avatar
Joulinar
Posts: 2059
Joined: Sat Nov 16, 2019 12:49 am

Re: Mounting Windows shares containing spaces

Post by Joulinar »

yes your observation is correct. mount cifs does not support the \040. I'm not really good on shell scripting but I tried die play around with drive manager script and was able to get the correct syntax which is working on manual execution. However drive manager still did not like it.

first one if failing with "mount.cifs: bad UNC ('//192.168.0.10/test 123 test')"

Code: Select all

mount -t cifs -o username=MyUser,password=MyPass,iocharset=utf8,uid=dietpi,gid=dietpi,file_mode=0770,dir_mode=0770,vers=3.1.1 '//192.168.0.10/test 123 test' /mnt/samba
while the second one if failing with "mount error(2): No such file or directory"

Code: Select all

mount -t cifs -o username=MyUser,password=MyPass,iocharset=utf8,uid=dietpi,gid=dietpi,file_mode=0770,dir_mode=0770,vers=3.1.1 //192.168.0.10/test\ 123\ test /mnt/samba
as I said, both commands are working if I put them directly into console
Pls let us know if a solution is working. This could help others if they hit by similar situation. Your DietPi Team
SNG
Posts: 9
Joined: Thu Apr 16, 2020 1:11 pm

Re: Mounting Windows shares containing spaces

Post by SNG »

Got it working!
The two issues in the existing code are:
1. Spaces are replaced with \\040 rather than \040 required by fstab. (line 1758)
2. The mount command does not accept \040 (line 1793) as a space substitute.

To fix these I changed line 1758 which gets the share directory from the user and replaces any and all spaces with \\040, from:
samba_clientshare=${G_WHIP_RETURNED_VALUE//[[:space:]]/\\\\040}
to:

Code: Select all

samba_clientshare=${G_WHIP_RETURNED_VALUE//[[:space:]]/\\040}
so that spaces are replaced with \040.

Then line 1793, which uses samba_clientshare, is modified so that \\040 is replaced in-line with an escaped space (\ )

from:
if mount -t cifs -o username="$samba_clientuser",password="$samba_clientpassword",iocharset=utf8,uid=dietpi,gid=dietpi,file_mode=0770,dir_mode=0770,vers=$i //"$samba_clientname"/"${samba_clientshare}" "$samba_fp_mount_target" &>> $fp_tmp; then
to

Code: Select all

if mount -t cifs -o username="$samba_clientuser",password="$samba_clientpassword",iocharset=utf8,uid=dietpi,gid=dietpi,file_mode=0770,dir_mode=0770,vers=$i //"$samba_clientname"/"${samba_clientshare//\\040/\ }" "$samba_fp_mount_target" &>> $fp_tmp; then
This line, 1793, is in a for loop which steps down through versions of cifs from 3.1.1 until it finds the highest supported version which works, before updating fstab. My change does a string in string replacement on samba_clientshare, like this
${samba_clientshare//\\040/\ }
making it compatible with the mount command.

I tried putting the the escaped path into a variable and then using that variable in the mount command but I couldn't find how to get that to work!! Anyway doing it in-line seems much neater.

Regards
SNG
User avatar
Joulinar
Posts: 2059
Joined: Sat Nov 16, 2019 12:49 am

Re: Mounting Windows shares containing spaces

Post by Joulinar »

yup that seems to be working, at least for the mount part. What I noticed, some strange visualisation within drive manager with the mount containing spaces. For testing purposes i did 2 mounts. First one is a normal one, the second a mount with spaces.

Code: Select all

root@DietPi3:~# df -h|grep samba
//192.168.0.10/Share          2.7T  566G  2.1T  22% /mnt/samba
//192.168.0.10/test 123 test  2.7T  566G  2.1T  22% /mnt/samba1
root@DietPi3:~#
You can see on the picture attached that information for 2nd mount are "incorrect" :)

It seems drive manager did not detect the drive correctly even if it is correctly added to /etc/fstab

Code: Select all

[ INFO ] DietPi-Drive_Manager |  - Detected mounted drive: //192.168.0.10/test > 2.1T
as well removing the mount is not possible

Code: Select all

[.     ] DietPi-Drive_Manager | umount 2.1T
[FAILED] DietPi-Drive_Manager | umount 2.1T
Attachments
picture.PNG
Pls let us know if a solution is working. This could help others if they hit by similar situation. Your DietPi Team
SNG
Posts: 9
Joined: Thu Apr 16, 2020 1:11 pm

Re: Mounting Windows shares containing spaces

Post by SNG »

Oh dear! It seems that dietpi's mount detection code doesn't handle spaces correctly either :?
I had scanned dietpi-drive_manager code to see where the variable samba_clientshare (a local variable) was being used to make sure my changes were not going to break anything else and all seems fine, so the detection code cannot rely on the that variable.

I've identified the detection code and it is way beyond me so, sadly, it looks as though I shall have to look for a solution for my application somewhere other than DietPi 'cos I don't suppose this issue will get fixed any time soon:cry:

All the same, I'll have a look to see how I go about raising a bug report.

Regards
SNG
User avatar
Joulinar
Posts: 2059
Joined: Sat Nov 16, 2019 12:49 am

Re: Mounting Windows shares containing spaces

Post by Joulinar »

I guess there isn't a real issue for your system. You can always use /etc/fstab directly to setup/mount your drive during boot. And this is something you don't do on regular basis, efforts are limited I would say. But yes it might be something to be checked by @MichaIng . I was looking into code as well and I found the section where drives are detected and it seems using df command. Detection start around line 170. Looks like it's going to separate the information by space and put values into an array. But it's way above my shell scripting skill :cry:

Detection section

Code: Select all

		# Detect mounted drives
		G_DIETPI-NOTIFY 2 'Detecting drives, please wait...'
		# - Only detect mounts with valid source path (word 1 contains "/")
		mawk '$1~/\//{print}' <<< $(df -Pha) > .df_out_tmp
Separate section

Code: Select all

			aDRIVE_ISMOUNTED[$index]=1
			aDRIVE_MOUNT_SOURCE[$index]=$(mawk '{print $1}' <<< $line)
			aDRIVE_SIZE_TOTAL[$index]=$(mawk '{print $2}' <<< $line)
			aDRIVE_SIZE_USED[$index]=$(mawk '{print $3}' <<< $line)
			aDRIVE_SIZE_FREE[$index]=$(mawk '{print $4}' <<< $line)
			aDRIVE_SIZE_PERCENTUSED[$index]=$(mawk '{print $5}' <<< $line)
			aDRIVE_MOUNT_TARGET[$index]=$(mawk '{print $6}' <<< $line)
Pls let us know if a solution is working. This could help others if they hit by similar situation. Your DietPi Team
Post Reply