Skip to main content
spell fix
Source Link
Bex
  • 2.9k
  • 3
  • 26
  • 34

I recently had almost the same problmeproblem, so I wrote a little script myself and the according udev rules. Your can find a longer explanation on my website solvedforhome.com

I recently had almost the same problme, so I wrote a little script myself and the according udev rules. Your can find a longer explanation on my website solvedforhome.com

I recently had almost the same problem, so I wrote a little script myself and the according udev rules. Your can find a longer explanation on my website solvedforhome.com

Source Link

I recently had almost the same problme, so I wrote a little script myself and the according udev rules. Your can find a longer explanation on my website solvedforhome.com

With this script any usb stick/drive or hdd/sdd will automatically be mounted under /media/<USB_DEVICE_LABEL>/ and unmounted when the device is plugged off (logging included).

The short version here:

First you create the udev rules with

sudo nano /etc/udev/rules.d/00-mount_manager

and write this into the file:

ACTION=="add",SUBSYSTEM=="block",KERNEL=="sd*[!0-9]",RUN+="/root/mount_manager/mount_manager add"
ACTION=="remove",SUBSYSTEM=="block",KERNEL=="sd*[!0-9]",RUN+="/root/mount_manager/mount_manager remove"

Then create the script with

sudo mkdir /root/mount_manager
sudo nano /root/mount_manager/mount_manager

and copy this into the file:

#!/bin/bash

APP_DIR=/root/mount_manager
LOG_DIR=$APP_DIR
LOG_FILE="${LOG_DIR}/mount_manager.log"
MOUNT_DIR=/media/

log_private=true
log_syslog=true

user=root
group=root

arr_media_mounted=()
arr_media_unmounted=()
arr_plugged_in_path=()
arr_plugged_in_label=()
arr_plugged_in_type=()

# function for mounting different file systems
function mounter {
    # $1 device path /dev/sd*
    # $2 name or label of device
    # $3 format or type

    if [ "vfat" == "$3" ]; then
        mount -t vfat -o utf8,uid=${user},gid=${group} $2 $MOUNT_DIR$1 >> $LOG_FILE 2>&1
    fi
        if [ "ntfs" == "$3" ]; then
        mount -t ntfs -o rw $2 $MOUNT_DIR$1 >> $LOG_FILE 2>&1 #not tested
    fi
        if [ "hfsplus" == "$3" ]; then
        mount -t hfsplus -o utf8,uid=${user},gid=${group} $2 $MOUNT_DIR$1 >> $LOG_FILE 2>&1 #not tested
    fi
        if [ "exfat" == "$3" ]; then
        mount -t exfat -o utf8,uid=${user},gid=${group} $2 $MOUNT_DIR$1 >> $LOG_FILE 2>&1 #not tested
    fi
        if [ "ext4" == "$3" ]; then
        mount -t ext4 -o defaults $2 $MOUNT_DIR$1 >> $LOG_FILE 2>&1
    fi
}

# functions for loggin output
function log {
    if [ "$log_private" = true ] ; then
        echo "$(date +"%Y.%m.%d %H:%M:%S") $1" >> $LOG_FILE
    fi
}
function log_info {
    log "INFO: $1"
        if [ "$log_syslog" = true ] ; then
        logger $1 -p info -t ${0#./}
    fi
}
function log_warn {
    log "WARNING: $1"
        if [ "$log_syslog" = true ] ; then
                logger $1 -p warning -t ${0#./}
    fi
}
function log_err {
        log "ERROR: $1"
        if [ "$log_syslog" = true ] ; then
                logger $1 -p error -t ${0#./}
    fi
}

# backup log
if [ "$(wc -l < $LOG_FILE)" -gt 200 ]; then
    mv $LOG_FILE ${LOG_FILE}.1
fi


######################################################
#
#       start main routine

log_info "========== mount_manager called by udev $1 =========="


# get directories in mounting directory $MOUNT_DIR
for dir in $(ls -d "${MOUNT_DIR}"*); do
    if mount | grep $dir > /dev/null; then
        arr_media_mounted+=(${dir#$MOUNT_DIR})
    else
        arr_media_unmounted+=(${dir#$MOUNT_DIR})
    fi
done


# get plugged in devices
regex='(/dev/sd[a-z]1): LABEL=\"([A-Za-z0-9_\-]+)\".*TYPE=\"([a-zA-Z0-9]+)\"'
while IFS= read -r line; do
    if [[ $line =~ $regex ]];then
        name="${BASH_REMATCH[1]}"
        label="${BASH_REMATCH[2]}"
        type="${BASH_REMATCH[3]}"
        arr_plugged_in_path+=($name)
        arr_plugged_in_label+=($label)
        arr_plugged_in_type+=($type)
    fi
done < <(blkid)


# check directories in /media yet not plugged in devices
for mounted_item in ${arr_media_mounted[*]}
do
    if ! [[ " ${arr_plugged_in_label[*]} " == *" ${mounted_item} "* ]]; then
        log_info "Mounted yet unplugged directory '${mounted_item}' found."
        umount $MOUNT_DIR$mounted_item >> $LOG_FILE 2>&1
        if ! [ "$(ls -A $MOUNT_DIR$mounted_item)" ]; then
            log_info " - Directory now unmounted and empty and will be deleted."
            rm -rf $MOUNT_DIR$mounted_item >> $LOG_FILE 2>&1
        else
            log_err " - Directory not empty. This should not happen after unmount. Please check '${$MOUNT_DIR$mounted_item}'"
        fi
    fi
done
for unmounted_item in ${arr_media_unmounted[*]}
do
        if ! [[ " ${arr_plugged_in_label[*]} " == *" ${unmounted_item} "* ]]; then
                log_info "Unmounted and unplugged directory '${unmounted_item}' found."
        if ! [ "$(ls -A $MOUNT_DIR$unmounted_item)" ]; then
            log_info " - Directory is empty and will be deleted."
            rm -rf $MOUNT_DIR$unmounted_item >> $LOG_FILE 2>&1
        else
            log_warn " - Directory not empty. No further action."
        fi
        fi
done

# check plugged in devices
i=0
for plugged_in_item in ${arr_plugged_in_label[*]}
do
    log_info "Plugged in device '${plugged_in_item}' will be checked."
    if [[ " ${arr_media_mounted[*]} " == *" ${plugged_in_item} "* ]]; then
        log_info " - Mounted device '${plugged_in_item}' found. Great."
    else
        if [[ " ${arr_media_unmounted[*]} " == *" ${plugged_in_item} "* ]]; then
            log_info " - Unmounted yet plugged in device '${plugged_in_item}' found. It will be mounted."
            else
            log_info " - Newly plugged in device '${plugged_in_item}' found. Directory will be created and device will be mounted."
            mkdir $MOUNT_DIR$plugged_in_item >> $LOG_FILE 2>&1
        fi
        mounter $plugged_in_item ${arr_plugged_in_path[i]} ${arr_plugged_in_type[i]}
    fi
    ((i++))
done

Now make the script executable

sudo chmod o+x /root/mount_manager/mount_manager

and reload udev rules with

sudo udevadm control --reload-rules

Done! Enjoy it and maybe leave a comment on solvedforhome.com.