How to unmount a drive in samba and mount in home

Note that mount and umount need root permissions (sudo). You would need to disable the password prompt for sudo mount and sudo umount globally , to run this as user in a script.

So easier would be using /etc/fstab or udisks2.

The drive in question is in /etc/fstab already.
This drive would cause the the system to fail booting so had to include ‘nofail’ in the options to allow the computer to boot.

I don’t know what udisk2 is.

udisks2 is the background program, which is used generally in your file-manager. So when you click on the partition name there, it gets mounted instantly as user.

The mount point is predefined to /run/media/user/partitionname/. As an option, you switch it to /media/partitionname.

$ cat /etc/udev/rules.d/99-udisks2.rules
# UDISKS_FILESYSTEM_SHARED
# ==1: mount filesystem to a shared directory (/media/VolumeName)
# ==0: mount filesystem to a private directory (/run/media/$USER/VolumeName)
# See udisks(8)
ENV{ID_FS_USAGE}=="filesystem|other|crypto", ENV{UDISKS_FILESYSTEM_SHARED}="1"

and also define mount options exactly for a specific UUID/LABEL:

# File: /etc/udisks2/mount_options.conf
# Some comment
[/dev/disk/by-uuid/<UUID-here>]
umask=0000,uid=$UID,gid=$GID,windows_names,big_writes,hide_dot_files,hide_hid_files

In /etc/udisks2/mount_options.conf.example you get all default mount options.

Why would you mount it to /home/user/mountpoint anyway? I don’t understand why people like to do it that way. :man_shrugging:

You can mount it also by:

udisks2ctl --block-device /dev/sdXY

:notebook: It mounts only after logging in as a user, so it doesn’t mount at startup globally.

:spiral_notepad: That can be used in “Settings and Sessions startup” without doubt.

Then there is something wrong in your fstab. Note that frank is not a valid uid or gid. A uid or gid is a number. Type id in a terminal. Most likely it gets ignored, since you set umask=0000 which effectively set everything to 777 → Everyone can do anything.

It appears that you know a lot about these thing but unfortunately I don’t follow most of the detail.
My etc/fstab for that drive is:

UUID=01D91DAE1DEFF380	/home/frank/Data	ntfs-3g	defaults,nofail,nls=utf8,umask=0000,dmask=0000,fmask=0000,uid=1000,gid=1000,windows_names	0	0

I place these ntfs drives in my user directory because I also have pCloud account and it’s virtual drive is mounted there. I asked if can be moved it and they said it cannot be moved due to permissions considerations. When I first installed Manjaro, my drives were mounted in the /mnt folder but I did have permission problems. So when I found out why pCloud is mounted in the home directory I decided to mount them there also. This is the drive I read from and write to a lot. As I stated mostly it mounts correctly but the few times it doesn’t, it mounts in the smaba directory. I thought that with a script I could check where it is mounted and if in the samba directory, then unmount it and mount it where it should be.

I don’t think I’ll ever find out why sometimes the drive is not mounted by fsab. So, I think a work around solution is a script in Settings and Sessions.
Does the Settings and Sessions run things as root or user?

Can you share the output of:

systemctl list-units --type=mount --all --no-pager

I would expect that this gets mounted by a custom systemd unit.

Obviously, it always starts as user, never as root.

Looks pretty ok.

1 Like

Result:

[frank@Dell1 ~]$ systemctl list-units --type=mount --all --no-pager
  UNIT                       LOAD   ACTIVE   SUB     DESCRIPTION                
  -.mount                    loaded active   mounted Root Mount
  boot.mount                 loaded active   mounted /boot
  dev-hugepages.mount        loaded active   mounted Huge Pages File System
  dev-mqueue.mount           loaded active   mounted POSIX Message Queue File S…
  home-frank-Data.mount      loaded active   mounted /home/frank/Data
  home-frank-pCloudDrive.mo… loaded active   mounted /home/frank/pCloudDrive
  home-frank-Win.mount       loaded active   mounted /home/frank/Win
  home.mount                 loaded active   mounted /home
  proc-sys-fs-binfmt_misc.m… loaded active   mounted Arbitrary Executable File …
  run-credentials-systemd\x… loaded active   mounted /run/credentials/systemd-s…
  run-credentials-systemd\x… loaded active   mounted /run/credentials/systemd-s…
  run-credentials-systemd\x… loaded active   mounted /run/credentials/systemd-t…
  run-credentials-systemd\x… loaded active   mounted /run/credentials/systemd-t…
  run-user-1000-doc.mount    loaded active   mounted /run/user/1000/doc
  run-user-1000-gvfs.mount   loaded active   mounted /run/user/1000/gvfs
  run-user-1000.mount        loaded active   mounted /run/user/1000
  srv-samba-ntfs.mount       loaded inactive dead    Data drive
  sys-fs-fuse-connections.m… loaded active   mounted FUSE Control File System
  sys-kernel-config.mount    loaded active   mounted Kernel Configuration File …
  sys-kernel-debug.mount     loaded active   mounted Kernel Debug File System
  sys-kernel-tracing.mount   loaded active   mounted Kernel Trace File System
  tmp-.mount_pcloudNYfyNg.m… loaded active   mounted /tmp/.mount_pcloudNYfyNg
  tmp-ram.mount              loaded active   mounted /tmp/ram
  tmp.mount                  loaded active   mounted Temporary Directory /tmp
  var-lib-machines.mount     loaded inactive dead    Virtual Machine and Contai…

LOAD   = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB    = The low-level unit activation state, values depend on unit type.
25 loaded units listed.
To show all installed unit files use 'systemctl list-unit-files'.
[frank@Dell1 ~]$

Moderator edit: In the future, please use proper formatting: [HowTo] Post command output and file content as formatted text

As I thought. Check if you created a file in: /etc/systemd/system/ called srv-samba-ntfs.mount or/and srv-samba-ntfs.automount. Remove it, and it will not be mounted there at boot time.

2 Likes

Yes, there is a file called:-

I had a look at the contents as follows:
[Unit]
Description=Data drive

[Mount]
What=/dev/disk/by-uuid/01D91DAE1DEFF380
Where=/srv/samba/ntfs
Type=ntfs
Options=defaults,rw,noatime

[Install]
WantedBy=multi-user.target

If I delete this file and fstab does not mount the drive, I will still have manually mount in my home folder. Why not change ‘Where=’ location to my home folder and it will mount it there.
Wouldn’t my problem be solved?

If you like to do so, then do so. But I don’t know if your samba will work then. :man_shrugging:

That’s what I am afraid of.
If samba fails to work, and I restore the file, will samba also restore?

I decided to take your advice and delete the file rather than edit it.
I’ll try it out.
I will still need a way to be able to run a script with root permission at boot time to mount the drive if and when it is required.
Where do I place this script?

The dead easy method is

Thank you trying but I don’t follow any of the ideas in the link. I cannot answer most of the things in the prototype 1 file that need answering. And I don’t follow the rest either.

This might be simple for someone on a linux system for 20 years but I’ve been here for a few weeks with doubts about continuing numerous time with all the problems I’ve had.

Surely it can’t be so difficult to place a script somewhere to have it run with root permissions during boot. There are probably dozens if not hundreds of scripts already running.

What is the purpose of the /srv mount? For samba access in your network? But if you unmount it, it will not be available anymore in samba and only mounted in your home.

What is your actual intention? Where should what be mounted and when?

I explain again.
I have an ntfs drive mounted by fstab at boot at my home directory.
This drive is shared on a windows workgroup network and works fine in both directions.
Sometimes something happens to this drive and ftab does not mount it. I accidentally found out that on these occasions that drive is mounted in /srv/samba/ntfs, I assume samba did the mounting. I did not create this mount.

At those times the original share does not work. So, what I wanted to do was to unmount the drive and mount it in my home directory. If I do this manually it works fine including the share.

I have now deleted the file in /etc/systemd/system/srv-samba-ntfs.mount so maybe the drive will not be mounted in the /srv/samba/ntfs directory.

I now have to wait for fstab to fail to see what happens. But what this happens, I want to run a script that will mount the drive in my home directory.

Where do I place the script file to have it executed with root permissions as the mount requires that.

You were on the right track with the systemd unit - the only thing you should do is to disable the unit and use an automount unit to mount the share when the mount point is accessed.

For the GVFS mount script - The only part you have to amend in the script is this section.

# MODIFY THESE VARIABLES
# your samba server's hostname or IP address
HOST="my-server"

# the share name on the server
SHARENAME="my-share"

# credentials
USERNAME=
WORKGROUP=
PASSWD=

# /END MODIFY

Give the script a meaningful name - like the name of the share e.g. my-share.sh and make the script executable.

change the HOST variable to the name or IP of computer serving the share
change the SHARENAME variable to the name of the share you want to mount

ONLY if your share requires username and password you need to insert the relevant information in the USERNAME, WORKGROUP, and PASSWD variables.

Then run the script when you need the share

my-share.sh

if you need to unmount it

my-share -u

All the systemd stuff is sugar on the cake

This is not as simple as you say.
By disabling the unit do you mean unmounting the drive or removing from fstab?
What is a GVFS?
Do I copy that script, edit it, and save to a file like my-share.sh?
Do I place the file at /etc/systemd/system?
How does it know where to mount the drive?

I found a website outlining a method of creating scripts that may be run at boot time with root permissions at:

I followed the method to create a script , placing it in /etc/systemd/system/.
I have now tested this by re-booting the system and my script did run.
So, this part of the work around is working. I need to wait for fstab to fail mounting the ntfs drive to see if this script will mount the drive as I am hoping.
The root problem of course is that sometimes fstab fails to mount this drive but manually mounting is no problem. Yes, something must be wrong with the drive, but there should be a way to configure fstab to still mount as it can be manually.
So, I need to wait for fsab to fail to mount the drive on boot with my script mounting it anyway.

Ok, so here is what you probably miss.

The systemd services that are described in this thread are run by systemd witch has credentials to run as root, kinda, it will be able to mount is what I’m going at. A script is run by the user starting it, so if you want to start it as root you have to start it with sudo, and you don’t do that “easily” in a graphical interface, for good reason.

So there is no need to make a script for you to run in the first place, what you want to do is make a SERVICE (run by systemd) that handles it for you and above is described how to.
Otherwise this describes exactly what I think you want. Just change what is needed and remove what is not (if it’s an ntfs change from cifs to that and remove login credentials f.ex)

You can add:

After=network-online.target
Requisite=network-online.target

after [Unit] in your path-to-mount.mount file if it’s a network mount so you know that networking is up and running before it tries to mount.
You don’t even need anything in fstab with this setup.

But if you REALLY want to use a script and fstab I think you can add user in the options for the drive in your fstab. The option user gives permission to users to mount so your script MIGHT work, not to sure about umount and user privileges.