How to enable a Windows application in WINE to access a Samba share on a NAS (continued)

In a 2016 post ‘How to enable a Windows application in WINE to access a Samba share on a NAS‘ I explained how to mount in Linux a networked SMB shared folder so that a Windows application running via WINE could access the folder as Drive Y: in order to open and save files in it. In that blog post I also listed a couple of Bash scripts to facilitate the mounting and unmounting of the SMB share for the WINEPREFIX used for the Windows application (~/.wine-pdfxve6 in the example I gave for PDF-XChange Editor, Version 6). However, as I have several Windows applications running via WINE on my machines, and I have used a different WINEPREFIX for each of them, I wanted to be able to mount the SMB share for whichever of those applications I happen to be using at the time. Therefore I modified the original Bash scripts as shown below. The Desktop Configuration files (.desktop files) to launch the scripts are essentially the same as in my earlier blog post; I have just removed the references to the specific Windows application. The four modified files are listed below. Obviously change the username, SMB share name and SMB server name to suit your own situation.

1. Bash script ~/mount_bsfnas1_brianfolder_share.sh

#!/bin/bash
mount_share () {
    echo
    echo "Enter your Linux account password below..."
    echo
    sudo ln -s /media/bsfnas1/brianfolder ~/$PREFIX/dosdevices/y:
    sudo mount.cifs //bsfnas1/brianfolder/ -o user=brianfolder,pass=enricocaruso,uid=$(id -u),gid=$(id -g) ~/$PREFIX/dosdevices/y:
}
echo
echo "This will mount the Samba share folder brianfolder on the bsfnas1 machine."
echo
echo
echo "== Select which WINEPREFIX you wish to use =="
echo
ls ~/.wine-* | grep .wine | awk -F'/' '{print NR " " substr($4, 1, length($4)-1)}'
NUMPREFIXES=$(ls ~/.wine-* | grep .wine | wc -l)
echo
read -p "Enter number (q to abort) and press ENTER: " CHOICE
if [ "$CHOICE" != "q" ] && [ "$CHOICE" -gt 0 ] && [ "$CHOICE" -le $NUMPREFIXES ]; then
    PREFIX=$(ls ~/.wine-* | grep .wine | awk -F'/' '{print NR " " substr($4, 1, length($4)-1)}' | grep "$CHOICE " | awk -F' ' '{print $2}')
    echo
    if [ ! -e ~/$PREFIX/dosdevices/y: ]; then
        mount_share
    else
        echo -n "~/$PREFIX/dosdevices/y: already exists. Is it OK to proceed anyway (y/n)? "
        read ANSWER
        if [ $ANSWER = "y" ]; then
            rm ~/$PREFIX/dosdevices/y:
            mount_share
        fi
    fi
    echo
fi
if grep -q "/media/bsfnas1/brianfolder" /proc/mounts; then
    echo "Samba share //bsfnas1/brianfolder is mounted for WINEPREFIX ~/$PREFIX ."
else
    echo "Samba share //bsfnas1/brianfolder is not mounted."
fi
echo
echo "You may now close this window."
read ANSWER
exit

2. Bash script ~/umount_bsfnas1_brianfolder_share.sh

#!/bin/bash
echo
echo "This will unmount the Samba share folder brianfolder on the bsfnas1 machine."
echo
echo "Enter your Linux account password below..."
echo
sudo umount ~/.wine-*/dosdevices/y: 2>/dev/null
echo
if grep -q "/media/bsfnas1/brianfolder" /proc/mounts; then
  echo "Samba share //bsfnas1/brianfolder is mounted."
else
  echo "Samba share //bsfnas1/brianfolder is not mounted."
fi
echo
echo "You may now close this window."
exit

3. Desktop Configuration file ~/Desktop/mount_bsfnas1_brianfolder_share.desktop

[Desktop Entry]
Comment[en_GB]=Mount bsfnas1 brianfolder share for current WINEPREFIX
Comment=Mount bsfnas1 brianfolder share for current WINEPREFIX
Exec=sh /home/fitzcarraldo/mount_bsfnas1_brianfolder_share.sh
GenericName[en_GB]=Mount bsfnas1 brianfolder share for current WINEPREFIX
GenericName=Mount bsfnas1 brianfolder share for current WINEPREFIX
Icon=media-mount
MimeType=
Name[en_GB]=mount_bsfnas1_brianfolder_share
Name=mount_bsfnas1_brianfolder_share
Path=
StartupNotify=true
Terminal=true
TerminalOptions=\s--noclose
Type=Application
X-DBUS-ServiceName=
X-DBUS-StartupType=none
X-KDE-SubstituteUID=false
X-KDE-Username=fitzcarraldo

4. Desktop Configuration file ~/Desktop/umount_bsfnas1_brianfolder_share.desktop

[Desktop Entry]
Comment[en_GB]=Unmount bsfnas1 brianfolder share for current WINEPREFIX
Comment=Unmount bsfnas1 brianfolder share for current WINEPREFIX
Exec=sh /home/fitzcarraldo/umount_bsfnas1_brianfolder_share.sh
GenericName[en_GB]=Unmount bsfnas1 brianfolder share for current WINEPREFIX
GenericName=Unmount bsfnas1 brianfolder share for current WINEPREFIX
Icon=media-eject
MimeType=
Name[en_GB]=umount_bsfnas1_brianfolder_share
Name=umount_bsfnas1_brianfolder_share
Path=
StartupNotify=true
Terminal=true
TerminalOptions=\s--noclose
Type=Application
X-DBUS-ServiceName=
X-DBUS-StartupType=none
X-KDE-SubstituteUID=false
X-KDE-Username=fitzcarraldo

Now when I double-click on the icon to mount the SMB share for a Windows application running via WINE, a terminal window pops up displaying the WINEPREFIXs currently installed on my machine:


This will mount the Samba share folder brianfolder on the bsfnas1 machine.


== Select which WINEPREFIX you wish to use ==

1 .wine-3dimviewer
2 .wine-myphoneexplorer
3 .wine-nbtscan
4 .wine-pdfxve6
5 .wine-PortableApps
6 .wine-radiant
7 .wine-symmetry
8 .wine-visio
9 .wine-xnviewmp

Enter number (q to abort) and press ENTER: 

Let’s say I want to use the Windows application XnViewMP. I would enter ‘9’ and press ‘Enter’. The rest of the interaction should be obvious:


This will mount the Samba share folder brianfolder on the bsfnas1 machine.


== Select which WINEPREFIX you wish to use ==

1 .wine-3dimviewer
2 .wine-myphoneexplorer
3 .wine-nbtscan
4 .wine-pdfxve6
5 .wine-PortableApps
6 .wine-radiant
7 .wine-symmetry
8 .wine-visio
9 .wine-xnviewmp

Enter number (q to abort) and press ENTER: 9

~/.wine-xnviewmp/dosdevices/y: already exists. Is it OK to proceed anyway (y/n)? y

Enter your Linux account password below...

[sudo] password for fitzcarraldo: 

Samba share //bsfnas1/brianfolder is mounted for WINEPREFIX ~/.wine-xnviewmp .

You may now close this window.

Henceforth the Windows application XnViewMP will be able to access the Y: drive which is actually the SMB share //bsfnas1/brianfolder.

Once I have finished using the application, I just double-click on the the icon to unmount the SMB share, and a terminal window pops up displaying the following:


This will unmount the Samba share folder brianfolder on the bsfnas1 machine.

Enter your Linux account password below...

[sudo] password for fitzcarraldo: 

Samba share //bsfnas1/brianfolder is not mounted.

You may now close this window.

Once I have entered my Linux password for the local machine, the script will unmount the SMB share and the terminal window will close automatically if you have configured the Desktop Configuration file by right-clicking on the icon and unticking ‘Do not close when command exits’ in KDE, ‘Keep terminal window open after command execution’ in LXDE, or similar in other desktop environments.

Note: If you use Microsoft Office via WINE, you also might be interested in a comment on my earlier blog post about a Microsoft Office problem in saving files to a remote SMB share.

How to enable a Windows application in WINE to access a Samba share on a NAS

I recently installed the Windows application PDF-XChange Editor under WINE in Gentoo Linux on one of my laptops. The application works fine but it could not detect the SMB/CIFS (Samba) share folder on my NAS. When I clicked on ‘File‘ > ‘Open...‘ in the application, the left pane of the ‘Open Files‘ dialogue window displayed the following options:

+ Favourites
– Desktop
  + My Computer
  + My Documents
    Trash
  + /

If I clicked on ‘My Computer‘, the right pane of the dialogue window then displayed the following options:

Control Panel
(C:)
(D:)
(E:)
(F:)
(G:)
(Z:)

None of the entries in either pane enabled me to get to the Samba shares on my NAS. Anyway, it turned out to be relatively easy to configure the installation on my laptop to enable the Windows application to access the Samba shared folder on the NAS, and the basic procedure was as follows:

  1. Create a mountpoint.
  2. In the directory $WINEPREFIX/dosdevices/ create a symbolic link to the mountpoint.
  3. Mount the network share on the mountpoint.

Let’s look in detail at the procedure…

My Clevo W230SS laptop running Gentoo Linux Stable Branch amd64 currently has KDE Plasma 5.6.5 and WINE 1.9.18 installed. I had used a WINE prefix of ~/.wine-pdfxve6 to install the Windows application in the fitzcarraldo user account. Let us say that the hostname of my Linux NAS is ‘bsfnas1‘, the name of the Samba shared folder on the NAS is ‘brianfolder‘, the Samba username for that shared folder on the NAS is ‘brian‘ and the Samba share password on the NAS is ‘enricocaruso‘.

First I checked which drive letters were already being used by WINE:

$ ls -la ~/.wine-pdfxve6/dosdevices/
total 8
drwxr-xr-x 2 fitzcarraldo fitzcarraldo 4096 Sep 16 23:18 .
drwxr-xr-x 4 fitzcarraldo fitzcarraldo 4096 Sep 17 04:03 ..
lrwxrwxrwx 1 fitzcarraldo fitzcarraldo   10 Sep 16 23:18 c: -> ../drive_c
lrwxrwxrwx 1 fitzcarraldo fitzcarraldo    9 Sep 16 23:18 d:: -> /dev/sdb1
lrwxrwxrwx 1 fitzcarraldo fitzcarraldo    8 Sep 16 23:18 e:: -> /dev/sdc
lrwxrwxrwx 1 fitzcarraldo fitzcarraldo    9 Sep 16 23:18 f:: -> /dev/sdc1
lrwxrwxrwx 1 fitzcarraldo fitzcarraldo    8 Sep 16 23:18 g:: -> /dev/sdb
lrwxrwxrwx 1 fitzcarraldo fitzcarraldo    1 Sep 16 23:18 z: -> /

As no Windows Y: drive was listed, I decided to use that drive letter for my network Samba share as shown in the steps below.

I created a mountpoint for the share:

$ sudo mkdir -p /media/bsfnas1/brianfolder

Then I created the symlink:

$ ln -s /media/bsfnas1/brianfolder ~/.wine-pdfxve6/dosdevices/y:
$ ls -la ~/.wine-pdfxve6/dosdevices/
total 8
drwxr-xr-x 2 fitzcarraldo fitzcarraldo 4096 Sep 17 15:38 .
drwxr-xr-x 4 fitzcarraldo fitzcarraldo 4096 Sep 17 15:39 ..
lrwxrwxrwx 1 fitzcarraldo fitzcarraldo   10 Sep 16 23:18 c: -> ../drive_c
lrwxrwxrwx 1 fitzcarraldo fitzcarraldo    9 Sep 16 23:18 d:: -> /dev/sdb1
lrwxrwxrwx 1 fitzcarraldo fitzcarraldo    8 Sep 16 23:18 e:: -> /dev/sdc
lrwxrwxrwx 1 fitzcarraldo fitzcarraldo    9 Sep 16 23:18 f:: -> /dev/sdc1
lrwxrwxrwx 1 fitzcarraldo fitzcarraldo    8 Sep 16 23:18 g:: -> /dev/sdb
lrwxrwxrwx 1 fitzcarraldo fitzcarraldo   11 Sep 17 15:37 y: -> /media/bsfnas1/brianfolder
lrwxrwxrwx 1 fitzcarraldo fitzcarraldo    1 Sep 16 23:18 z: -> /

Finally, I mounted the network share onto the symlink:

$ sudo mount.cifs //bsfnas1/brianfolder/ -o user=brian,pass=enricocarusou,id=$(id -u),gid=$(id -g) ~/.wine-pdfxve6/dosdevices/y:

As before, I see the following when I click on ‘File‘ > ‘Open...‘ in the Windows application running under WINE:

+ Favourites
– Desktop
  + My Computer
  + My Documents
    Trash
  + /

If I click on ‘My Computer‘ in the ‘Open File‘ dialogue window, the following items are displayed in the right pane of the window:

Control Panel
(C:)
(D:)
(E:)
(F:)
(G:)
(Y:)
(Z:)

and I can select the ‘(Y:)‘ and browse the contents of the shared folder brianfolder on the NAS drive.

Although I found it was unnecessary to do it for PDF-XChange Editor, apparently some Windows applications require the use of Windows UNC syntax, so I also did the following:

$ mkdir -p ~/.wine-pdfxve6/dosdevices/unc/bsfnas1
$ ln -s /media/bsfnas1/brianfolder ~/.wine-pdfxve6/dosdevices/unc/bsfnas1/brianfolder

If I wanted to unmount the Samba share explicitly, rather than leaving it to be unmounted automatically when I shutdown the laptop, I would enter the following command:

$ sudo umount ~/.wine-pdfxve6/dosdevices/y\:/

Then the Windows application on my laptop would no longer be able to browse the unmounted network share:

$ ls ~/.wine-pdfxve6/dosdevices/
c:  d::  e::  f::  g::  unc  y:  z:
$ ls -la /media/bsfnas1/brianfolder
total 8
drwxr-xr-x 2 root root 4096 Sep 17 15:35 .
drwxr-xr-x 4 root root 4096 Sep 17 15:35 ..
$

You may be wondering why I did not add an entry in the file /etc/fstab on my laptop, to mount the Samba share automatically when I boot the laptop. The reason I didn’t is because I often use the laptop away from home and the NAS on my home network is then inaccessible in any case. However, to save myself the hassle of having to enter the mount command manually when I am at home and want to use the Windows application to open a file that is in the NAS shared folder, I created two Desktop Configuration files named mount_bsfnas1_brianfolder_share.desktop and umount_bsfnas1_brianfolder_share.desktop with nice icons in my ~/Desktop directory on the laptop:

[Desktop Entry]
Comment[en_GB]=Mount bsfnas1 brianfolder share for PDF-XChange Editor
Comment=Mount bsfnas1 brianfolder share for PDF-XChange Editor
Exec=sh /home/fitzcarraldo/mount_bsfnas1_brianfolder_share.sh
GenericName[en_GB]=Mount bsfnas1 brianfolder share for PDF-XChange Editor
GenericName=Mount bsfnas1 brianfolder share for PDF-XChange Editor
Icon=media-mount
MimeType=
Name[en_GB]=mount_bsfnas1_brianfolder_share
Name=mount_bsfnas1_brianfolder_share
Path=
StartupNotify=true
Terminal=true
TerminalOptions=\s--noclose
Type=Application
X-DBUS-ServiceName=
X-DBUS-StartupType=none
X-KDE-SubstituteUID=false
X-KDE-Username=fitzcarraldo

[Desktop Entry]
Comment[en_GB]=Unmount bsfnas1 brianfolder share for PDF-XChange Editor
Comment=Unmount bsfnas1 brianfolder share for PDF-XChange Editor
Exec=sh /home/fitzcarraldo/umount_bsfnas1_brianfolder_share.sh
GenericName[en_GB]=Unmount bsfnas1 brianfolder share for PDF-XChange Editor
GenericName=Unmount bsfnas1 brianfolder share for PDF-XChange Editor
Icon=media-eject
MimeType=
Name[en_GB]=umount_bsfnas1_brianfolder_share
Name=umount_bsfnas1_brianfolder_share
Path=
StartupNotify=true
Terminal=true
TerminalOptions=\s--noclose
Type=Application
X-DBUS-ServiceName=
X-DBUS-StartupType=none
X-KDE-SubstituteUID=false
X-KDE-Username=fitzcarraldo

and the Bash scripts mount_bsfnas1_brianfolder_share.sh and umount_bsfnas1_brianfolder_share.sh launched by the above two Desktop Configuration files are, respectively:

#!/bin/bash
echo "This will mount the Samba share folder brianfolder on the bsfnas1 machine."
echo
echo "Enter your Linux account password below..."
echo
sudo mount.cifs //bsfnas1/brianfolder/ -o user=brian,pass=enricocaruso,uid=$(id -u),gid=$(id -g) ~/.wine-pdfxve6/dosdevices/y:
echo
if grep -q "/media/bsfnas1/brianfolder" /proc/mounts; then
  echo "Samba share //bsfnas1/brianfolder is mounted."
else
  echo "Samba share //bsfnas1/brianfolder is not mounted."
fi
echo
echo "You may now close this window."

#!/bin/bash
echo "This will unmount the Samba share folder brianfolder on the bsfnas1 machine."
echo
echo "Enter your Linux account password below..."
echo
sudo umount ~/.wine-pdfxve6/dosdevices/y:
echo
if grep -q "/media/bsfnas1/brianfolder" /proc/mounts; then
  echo "Samba share //bsfnas1/brianfolder is mounted."
else
  echo "Samba share //bsfnas1/brianfolder is not mounted."
fi
echo
echo "You may now close this window."

Don’t forget to make the two shell scripts executable:

$ chmod +x /home/fitzcarraldo/mount_bsfnas1_brianfolder_share.sh
$ chmod +x /home/fitzcarraldo/umount_bsfnas1_brianfolder_share.sh

If I was doing this on a desktop PC instead of a laptop, instead of creating the above-mentioned .desktop files and Bash scripts I would have added the following line in the file /etc/fstab to mount the NAS Samba shared folder automatically at boot:

//bsfnas1/brianfolder  /media/bsfnas1/brianfolder  cifs  rw,iocharset=utf8,user=brian,pass=enricocaruso  0   0

It works (I’ve tried it).

Further reading
WineHQ Forums – Mapped network drive in wine.
Estendendo suporte a UNC no Wine.