i had a previous post where title does not reflect what i want to do anymore.
autofs is broken in my distro.
cannot figure a comprehensive way to use systemd mount/automount for the task.
i am not a computer person, so i prompted bing chat ai to create the following script. i use systemd.service to start the script. i have not exhaustively tested it, but seem to work with minor issue.
issue:
i do not understand every line. i want to minimize bandwidth use, hence on demand, but is it still pinging every other minute (last line: while true, do sleep 1...)?
the script is very long and cumbersome.
fuser solely relies on INACTIVE_TIME, as such share goes offline even if the share is opened in nautilus file manager - bing chat suggested replacing fuser with lsof +D for the unmount_share function would solve this issue, but i have not tested/revised the script yet.
> #!/bin/bash
>
> #Define the mount point and the remote server details
> MOUNT_POINT="/mnt/Data"
> REMOTE_SERVER="server"
> REMOTE_PATH="/mnt/share"
> INACTIVE_TIME=120
>
> #Define the trigger directory to monitor for access events
> TRIGGER_FILE="/mnt/Data"
>
> #Function to check if the remote server is online
> function is_server_online {
> ping -c 1 $REMOTE_SERVER > /dev/null 2>&1
> return $?
> }
>
> #Function to clean up stale mount point
> function cleanup_stale_mount {
> # Check if the mount point is already mounted
> if mountpoint -q $MOUNT_POINT; then
> # Unmount the stale mount point
> sudo umount -f $MOUNT_POINT
>
> # Check if the unmount was successful
> if [ $? -eq 0 ]; then
> echo "Successfully unmounted stale mount point $MOUNT_POINT"
> else
> echo "Failed to unmount stale mount point $MOUNT_POINT"
> fi
> fi
> }
>
> #Function to unmount the share when not in use
> function unmount_share {
> # Check if the mount point is already mounted
> if mountpoint -q $MOUNT_POINT; then
> # Check if any processes are accessing the mount point
> if fuser -m $MOUNT_POINT > /dev/null 2>&1; then
> echo "$MOUNT_POINT is in use, not unmounting."
> sleep $INACTIVE_TIME && unmount_share &
> else
> # Unmount the share
> sudo umount $MOUNT_POINT
>
> # Check if the unmount was successful
> if [ $? -eq 0 ]; then
> echo "Successfully unmounted $MOUNT_POINT"
> else
> echo "Failed to unmount $MOUNT_POINT"
> fi
> fi
> fi
> }
>
> #Function to connect to the share on demand.
> function connect_share {
> # Check if the remote server is online and mount point is not already mounted.
> if is_server_online && ! mountpoint -q $MOUNT_POINT; then
>
> # Create the mount point if it doesn't exist.
> if [ ! -d $MOUNT_POINT ]; then
> mkdir -p $MOUNT_POINT
> fi
>
> # Mount the remote path to the mount point.
> sudo mount -t nfs $REMOTE_SERVER:$REMOTE_PATH $MOUNT_POINT
>
> # Check if the mount was successful.
> if [ $? -eq 0 ]; then
> echo "Successfully mounted $REMOTE_SERVER:$REMOTE_PATH to $MOUNT_POINT"
> sleep $INACTIVE_TIME && unmount_share &
> else
> echo "Failed to mount $REMOTE_SERVER:$REMOTE_PATH to $MOUNT_POINT"
> fi
>
> elif ! is_server_online; then
> cleanup_stale_mount
> fi
>
> }
>
> #Function to monitor trigger directory for access events and mount share when accessed.
> function monitor_trigger {
> # Wait for an access event on the trigger directory.
> inotifywait -e access $TRIGGER_FILE
>
> # Mount the share when the trigger directory is accessed.
> connect_share
>
> # Keep monitoring the trigger directory for access events.
> monitor_trigger &
> }
>
> #Start monitoring the trigger directory for access events.
> monitor_trigger &
>
> #Function to handle shutdown and reboot signals.
> function shutdown {
> cleanup_stale_mount
> exit 0
> }
>
> trap shutdown SIGINT SIGTERM SIGKILL
>
> #Keep script running until shutdown or reboot signal is received.
> while true; do sleep 1; done;
In earlier post, you mentioned that the end goal of this is to Not waste network bandwidth while not accessing the network filesystem.
If the share is not used, then no network activity is done, at least not noticeable one.
Why would you want to kill it then ? Do you suspect it might be used unintentionally ?
As for issue when doing shutdown, reboots and hanging.. what does the console say or journal ?
Does the operating system eventually shutdowns/reboot or this is forever ?
It would also be good to know exact operating system and version on your laptop as well as NFS server in question (type, OS etc.)
You should using soft mounts with desired timeo, retrans options (see man or guides online to calculate).
Other then those, i would give netdev option a shot.
Consider playing with those options to see reduction in reboot/shutdown times while using systemd automount feature for mount.
I see this as reinventing the wheel and using brute force to do regular operation on operating system.
Of course, if it doesn't work regular ways, one can always resort to fuser, lsof and likes but lets try to see where it hangs and why first by using systemd automount with options specified above.
In earlier post, you mentioned that the end goal of this is to Not waste network bandwidth while not accessing the network filesystem. If the share is not used, then no network activity is done, at least not noticeable one. Why would you want to kill it then ? Do you suspect it might be used unintentionally ?
an earlier version, instead of on demand the script would regularly ping the server to unmount the share if server is down. for 12 hours, it would use approx 8MB of bandwidth.
As for issue when doing shutdown, reboots and hanging.. what does the console say or journal ? Does the operating system eventually shutdowns/reboot or this is forever ?
i dont know enough about logs or debugging. it hangs forever, needed hard reboot/shutdown.
It would also be good to know exact operating system and version on your laptop as well as NFS server in question (type, OS etc.)
client - clearlinux, server -opensuse
You should using soft mounts with desired timeo, retrans options (see man or guides online to calculate). Other then those, i would give netdev option a shot. Consider playing with those options to see reduction in reboot/shutdown times while using systemd automount feature for mount.
I see this as reinventing the wheel and using brute force to do regular operation on operating system.
no sh yes indeed, sherlock. that is why i prefaced by saying the script is cumbersome and the simplest solution, autofs, is broken/unmaintained in clearlinux. as i have learnt from the past while, it is not trivial or regular matter trying to replace autofs. i have not found one success story on google.
i will investigate some more nfs mount options to be used with systemd mount/automount.
Perhaps you should do a nfsmount wrapper that runs your task.
Example: ./nfsmount /path/to/backupscript
cat nfsmount
#!/bin/bash
# mount, run the given command, umount
mountpoint="/mnt/Data"
nfsserver="server"
nfsresource="${nfsserver}:/mnt/share"
PATH=/bin:/usr/bin:/sbin:/usr/sbin
if ! ping -c 1 "$nfsserver" > /dev/null 2>&1
then
echo "$nfsserver not pingable - exit"
exit 1
fi
if ! mount -t nfs "$nfsresource" "$mountpoint"
then
echo "Failed mount $nfsresource $mountpoint - exit"
exit 1
fi
echo "Running $@ ..."
"$@"
echo "Exitcode is $?"
if ! umount "$mountpath"
then
echo "Friendly umount failed, trying force+lazy"
trials=2
while ! umount -f -l "$mountpoint"
do
if ((--trials == 0))
then
echo "Giving up - exit"
exit 2
else
echo "Still failed - retrying"
sleep 10
done
fi
echo "Done."
but it just nags me that i cannot get on-demand nfs access to work without autofs. at least for now i know i have a systemd fall back as well as the cumbersome script from bing chat.
I've read this topic and your previous topic regarding auto-mounting, etc and I apologize in advance if what I'm about to say is blindingly obvious.
Auto mount has a configurable timeout, typically 300 secs or 5 minutes, to umount an idle NFS handle. However, if a NFS server goes offline whilst work is being done, i.e., files being accessed, then the system will wait to see if that NFS handle comes back online otherwise work gets lost and it is a recipe for filesystem corruption. This timeout should be much longer before a fatal error is reported. It would do the same if a local disk suddenly went offline for no good reason.
So the two timeouts are distinctly different and when you say the NFS server occasionally goes offline, as if that's a regular event, I would ask why is that happening? Surely that's the real problem to solve.
Secondly, if you rely on a automount timeout to umount idle NFS handles then you should check that the client application is designed properly in that when it triggers a remote mount, it does its work, and then closes all files.. Only then will the automount timeout timer start and execute a umount after so many seconds. So ensure that your client application is designed properly and completely makes that NFS handle 'idle', i.e., get in, do what you need to do, and get out.
Be very aware that an automount timeout and a disk unaccessible (offline) timeout are completely different things.
thankyou for your concern. this is just a home LAN setup. the server is just a desktop computer with lots of storage on a different floor may or may not have someone using in at any time. it gets turned off generally at the end of the day, but sometimes, someone forget and turn it off after they finish using it in the middle of the day. it is a makeshift setup obviously but is adequate for our current needs.
i just want a way to easily backup emails and photos from my laptop, without having to check on whether the desktop is on.
i hear you about the timeouts. if i were transferring large files, then yes, i would make sure the desktop stays on. similarly i do not like to work on large file remotely, i would rather copy it over to laptop, work on it and save it back to the desktop.
Just wanted to throw this in to see if it would help. You mentioned you could not figure a comprehensive way to use systemd automount and I thought this might help you.
This is for sshfs, but you can substitute it for NFS pretty simply - perhaps Google could help here.