====== Automounting usb drives is udev ====== I've a GoFlexNet that runs Slackware Arm that I like to use as NAS (nice to be able to do software raid with the 2 sata drives). Along with having an array I also wanted to be able to share any usb disk really quickly without having to interact in any way with the appliance: this is what I came up with: The idea is to use the volume labels to have the usb drive mounted and exported via NFS. Any partition that has no label or that is linux swap will be ignored. Well the umounting upon drive unplug is pointless but it also cleans up the exported filesystems avoiding writes in the parent directory. I wrote a couple of really simple udev rules to call the management program: root@nas:~# cat /etc/udev/rules.d/99-persistent-personal.rules ACTION=="add",KERNEL=="sd[a-z][1-9]", PROGRAM="automount" ACTION=="remove",KERNEL=="sd[a-z][1-9]", PROGRAM="automount" root@nas:~# And this is the management script root@nas:~# cat /lib/udev/automount #!/bin/bash PATH=/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin #echo "$(date) " >> /tmp/env.txt #/usr/bin/env >> /tmp/env.txt #id >> /tmp/env.txt DEV=$(basename $DEVNAME) LABEL=$(ls -l /dev/disk/by-label/ |grep $DEV |awk '{print $(NF-2)}') [ "$LABEL" = "" ] && LABEL=$ID_FS_LABEL [ "$LABEL" = "" ] && exit [ "$ID_PART_ENTRY_TYPE" = "0x82" ] && exit echo "$DEV $LABEL $ACTION" >> /tmp/env.txt case $ACTION in add) mkdir -p /mnt/exports/$LABEL logger "automount: add $LABEL" exportfs -u *:/mnt/exports/$LABEL mount LABEL=$LABEL /mnt/exports/$LABEL [ $(grep -c "^/mnt/exports/$LABEL" /etc/exports) -eq 0 ] && \ echo "/mnt/exports/$LABEL *(rw,no_root_squash,no_subtree_check,nohide)" >> /etc/exports exportfs -r -a ;; remove) logger "automount: remove $LABEL" exportfs -u *:/mnt/exports/$LABEL mount -o remount,ro /mnt/exports/$LABEL umount /mnt/exports/$LABEL rmdir /mnt/exports/$LABEL ;; esac root@nas:~# This is what my /etc/exports looks like initially: root@nas:~# cat /etc/exports # See exports(5) for a description. # This file contains a list of all directories exported to other computers. # It is used by rpc.nfsd and rpc.mountd. /mnt/exports *(rw,no_root_squash,no_subtree_check,crossmnt) root@nas:~# it gets populated with new lines as the usb drives come and go. The only thing you may want to do before unplugging any usb drive is remounting the filesystem ReadOnly on the nas manually. There aren't any buttons on the GoFlexNet that can be used to tigger this unfortunately, ====== Sources ====== {{tag>howtos author_ louigi600}}