Re: devd based AUTOMOUNTER

From: vermaden <vermaden_at_interia.pl>
Date: Fri, 17 Feb 2012 22:59:05 +0100
... even newer version, seems to have all 'problems' fixed now ;)

#! /bin/sh

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
LOG="/var/log/automount.log"
STATE="/var/run/automount.state"
DATEFMT="%Y-%m-%d %H:%M:%S"

__create_mount_point() { # /* 1=DEV */
  MNT="/mnt/$( basename ${1} )"
  mkdir -p ${MNT}
}

__state_lock() {
  while [ -f ${STATE}.lock ]; do sleep 0.5; done
  :> ${STATE}.lock
}

__state_unlock() {
  rm ${STATE}.lock
}

__state_add() { # /* 1=DEV 2=PROVIDER 3=MNT */
  __state_lock
  grep -E "${3}" ${STATE} 1> /dev/null 2> /dev/null && {
    __log "${1}:duplicated '${STATE}'"
    return 1
  }
  echo "${1} ${2} ${3}" >> ${STATE}
  __state_unlock
}

__state_remove() { # /* 1=MNT 2=STATE 3=LINE */
  BSMNT=$( echo ${1} | sed 's/\//\\\//g' )
  sed -i '' "/${BSMNT}\$/d" ${2}
}

__log() { # /* _at_=MESSAGE */
  echo $( date +"${DATEFMT}" ) ${_at_} >> ${LOG}
}

case ${2} in
  (attach)
    for I in /dev/${1}*
    do
      case $( file -L -s ${I} ) in
        (*NTFS*)
          __create_mount_point ${I}
          ntfs-3g ${I} ${MNT} # /* sysutils/fusefs-ntfs */
          __log "${I}:mount (ntfs)"
          ;;
        (*FAT*)
          __create_mount_point ${I}
          fsck_msdosfs -y ${I}
          mount_msdosfs -o large -l -L pl_PL.ISO8859-2 -D cp852 ${I} ${MNT}
          __log "${I}:mount (fat)"
          ;;
        (*ext2*)
          __create_mount_point ${I}
          fsck.ext2 -y ${I}
          mount -t ext2fs ${I} ${MNT}
          __log "${I}:mount (ext2)"
          ;;
        (*ext3*)
          __create_mount_point ${I}
          fsck.ext3 -y ${I}
          mount -t ext2fs ${I} ${MNT}
          __log "${I}:mount (ext3)"
          ;;
        (*ext4*)
          __create_mount_point ${I}
          fsck.ext4 -y ${I}
          ext4fuse ${I} ${MNT} # /* sysutils/fusefs-ext4fuse */
          __log "${I}:mount (ext4)"
          ;;
        (*Unix\ Fast\ File*)
          __create_mount_point ${I}
          fsck_ufs -y ${I}
          mount ${I} ${MNT}
          __log "${I}:mount (ufs)"
          ;;
        (*)
          case $( dd < ${O} count=1 | strings | head -1 ) in
            (EXFAT)
              __create_mount_point ${I}
              mount.exfat ${I} ${MNT} # /* sysutils/fusefs-exfat */
              __log "${I}:mount (ufs)"
              ;;
            (*) continue ;;
          esac
          ;;
      esac
      __state_add ${I} $( mount | grep -m 1 " ${MNT} " | awk '{printf $1}' ) \
        ${MNT} || continue
    done
    ;;

  (detach)
    MOUNT=$( mount )
    __state_lock
    grep ${1} ${STATE} \
      | while read DEV PROVIDER MNT
        do
          TARGET=$( echo "${MOUNT}" | grep -E "^${PROVIDER} " | awk '{print $3}' )
          [ -z ${TARGET} ] && {
            __state_remove ${MNT} ${STATE} ${LINE}
            continue
          }
          umount -f ${TARGET} &
          unset TARGET
          __state_remove ${MNT} ${STATE} ${LINE}
          __log "${DEV}:umount"
        done
    __state_unlock
    __log "/dev/${1}:detach"
    ;;

esac
Received on Fri Feb 17 2012 - 20:59:07 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:40:24 UTC