2

To implement a easy backup system in a PLC-application, we access a USB-Stick attached to an external Ubuntu-system. Therefore, the first attached USB-drive should be shared over Samba as soon as it gets attached.

The first attached USB always gets the path /dev/sdb. This dynamically mounted removable device should now always be shared with a fix name like "USB_DRIVE".

My problem is, that the drive is mounted with it's device description like "KENSINGTON". If the folder /media/KENSINGTON is shared, and the device gets removed and another Drive with the name "SCANDISK" is attached, it will not be shared.

So here are my questions:

  1. Is there a possibility to share a removable device with its path? So that always path /dev/sdb gets shared as soon as it is connected?
  2. I tried to bind a folder /mnt/USB_DRIVE to a specific mounted device like /media/KENSIGNTON. But the name of the folder in the media folder is not fixes. Can the name of the dynamically created folder /media/KENSINGTON be changed to the name of the path like /media/sdb'
  3. Is there another way to share the first attached usb-drive?

I'm aware of the security topics. The samba-shared folder is protected by a username and password.

A user should have the freedom to attach the stick he want to create a backup. So I cannot use the device name but need to determine the first one connected which has the path /dev/sdb/

/etc/fstab is definitely not working because the devices are dynamically plugged.

The best solution would be, if ubuntu would not mount the devices to the folder /media/ (Sample /media/Kensington) but with the path /media/ (Sample /media/sdb) Can it be configured somehow?

The usbmount tool seems to be a solution pointing into this direction. But as soon as it is installed, it generates the folders and they all remain empty, even if an usb-device is attached. What can be done wrong? Is it possible to configure the usbmount to create the folders usb[0-7] only when the specific device is attached and not permanently?

Thomas Ward
  • 74,764
hafisch
  • 135
  • How many computers are there? Are there different operating systems or only Ubuntu? Is it feasible to modify the settings in all the computers involved? Or should the USB drive be modified to make all [maybe different] operating systems identify it correctly? – sudodus Jul 04 '22 at 13:21
  • 3
    This does not sound like a good idea. What if someone inserts the wrong stick which holds sensitiv data, this data will then be shared. – mook765 Jul 04 '22 at 13:30
  • Would the method using /etc/fstab described here work in your case? You would need some modifications for example some mount options for your particular case. Or maybe using mount via a script more or less automatically similar to what is described there? -- But I agree with mook, that there are problems with this idea: 1. risk with sensitive data; 2. A USB stick is not reliable enough for backup, I would suggest a USB HDD or USB SSD. – sudodus Jul 04 '22 at 13:34
  • It seems that I understood your question the wrong way ... I thought you wanted to only share one unique USB disk ... turns out it's the other way around :-) ... It's possible but I totally agree with @mook765 ... It's better be avoided. – Raffa Jul 04 '22 at 17:17
  • something like usbmount should always mount the first connected USB with the same name ... AutoFS maps can do the same as well. – Raffa Jul 04 '22 at 17:26
  • Why not detach the two? So have a fixed shared location not being the usb where you put the backup AND have it make a copy to an USB when it is inserted? Heck, why is there a need for the USB? >:-D – Rinzwind Jul 04 '22 at 17:37
  • 1
    The script in this answer by @sudodus can be easily modified to automatically mount /dev/sdb1 to a fixed permanent name under /media or another mount point in the users home directory using the udisks utility. – Raffa Jul 04 '22 at 18:42
  • Indeed the script might be a possible solution and my knowhow is sufficient to adjust it for my needs to mount it correctly. Thanks for that. But can someone tell me, how I can detect if the previously mounted device has been disconnected in order to unmount it and to remove the folder? – hafisch Jul 04 '22 at 19:47
  • @hafisch, I can try to help you with that tomorrow (it is getting late here). – sudodus Jul 04 '22 at 19:54

1 Answers1

2

I found a solution. I check the file devices with the script mentioned above. if it exists, I'll create a folder and mount it. If it's already mounted, nothing will be done. As soon as no device is connected, and the /dev/sdb is not found anymore, it will be unmounted and deleted.

#!/bin/bash

#general definitions MNT_PATH="/media/USB_DRIVE" DEV_ID="sdd1" FILE_SYSTEM="vfat"

check if user is root

if [ "$(whoami)" != "root" ] then echo "run with sudo or as root" exit fi

#get list with all attached devices for i in $(lsblk -lo name,fstype,hotplug,type|grep '1 part$'|tr -s ' ' ' '|sed 's/ 1 part$//'|grep ' ..$'|tr ' ' '') do devId=${i%} devPath=/dev/$devId fileSystem=${i#*_}

check if device path matches

if [ "$devId" == $DEV_ID ] then # check if file system matches if [ "$fileSystem" == $FILE_SYSTEM ] then

device found - mount

check if device not mounted already

    if ! [[ $(findmnt -M "$MNT_PATH") ]]
     then
 # create folder and mount
     mkdir -p /media/USB_DRIVE
     mount -o rw,user,exec,umask=0000 "$devPath" /media/USB_DRIVE
     printf "mounted\n"
   exit
  else
    # already mounted
    printf "already mounted\n"
    exit

fi fi fi

done

check if mounted device exists

if [[ $(findmnt -M "$MNT_PATH") ]] then

unmount device

sudo umount /media/USB_DRIVE
sudo rm -r /media/USB_DRIVE/
printf "unmounted\n"

else

device already unmounted

printf "already unmounted\n" fi

The script works so far. Unclear is, how such a script must be called. Will it be called with an endless while loop or can it be called somehow in a specific cycle?

hafisch
  • 135
  • 1
    Both ways: In a for loop you can have a sleep command that lets the system rest some seconds or minutes or hours, whatever is suitable. You can also use cron, and because you need elevated permissions, you should add a line for it using sudo crontab -e. You can find good tutorials about cron and crontab via the internet. (The minimum interval using cron is one minute, the maximum interval one year). – sudodus Jul 05 '22 at 08:30
  • 1
    Impressive … you should consider coming back here more often to help answer other people’s questions … you have the potential … well done … As for how to run the script … in addition to what @sudodus has mentioned is to set an udev rule … or even a systemd service. – Raffa Jul 05 '22 at 10:02