devd based AUTOMOUNTER

From: vermaden <vermaden_at_interia.pl>
Date: Fri, 17 Feb 2012 18:49:17 +0100
Hi,

I have finally made some effort on writing flexible yet very simple automounter for FreeBSD desktop.

Feel free to submit me BUG reports ;)

It currently supports these file formats:
-- NTFS(rw) requires [port]sysutils/fusefs-ntfs[/port]
-- FAT/FAT32
-- exFAT requires [port]sysutils/fusefs-exfat[/port]
-- EXT2
-- EXT3
-- EXT4 requires [port]sysutils/fusefs-ext4fuse[/port]
-- UFS (DOH!)

It keeps state of the mounted devices at /var/run/automount.state and logs all activities to /var/log/automount.log file.

The place for the script is at /usr/local/sbin/automount.sh executable.

The only additional configuration it requires are those lines at the end of /etc/devd.conf file along with restarting /etc/rc.d/devd daemon.

notify 200 {
  match "system" "DEVFS";
  match "type" "CREATE";
  match "cdev" "(da|mmcsd)[0-9]+";
  action "/usr/local/sbin/automount.sh $cdev attach";
};

notify 200 {
  match "system" "DEVFS";
  match "type" "DESTROY";
  match "cdev" "(da|mmcsd)[0-9]+";
  action "/usr/local/sbin/automount.sh $cdev detach";
};

The /usr/local/sbin/automount.sh executable is here:

#! /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
  echo "${1} ${2} ${3}" >> ${STATE}
  __state_unlock
}

__state_remove() { # /* 1=MNT 2=STATE 3=LINE */
  LINE=$( grep -n -E "${1}$" ${2} | cut -d : -f 1 )
  sed -i '' ${3}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 -o longnames -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 " ${MNT} " | awk '{printf $1}' ) ${MNT}
    done
    ;;

  (detach)
    MOUNTED=$( mount )
    __state_lock
    while read DEV PROVIDER MNT
    do
      TARGET=$( echo "${MOUNTED}" | 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}
    __state_unlock
    __log "/dev/${1}:detach"
    ;;

esac


PS. Below are links for 'mirror' threads.
http://forums.freebsd.org/showthread.php?t=29895
http://daemonforums.org/showthread.php?t=6838

Regards,
vermaden
























---
Received on Fri Feb 17 2012 - 17:09:29 UTC

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