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
  • 78,878
hafisch
  • 135

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