#!/bin/sh do_start() { echo "Starting USB mount" # getting each device pluged in the computer and detected by the system for i in /sys/block/sd* do # getting the device path DEV="/dev/`basename $i`" # if the device exists if [ $DEV != 0 ] then # trying to get partitions of each devices for j in `seq 1 9` do # setting partition path DEVn="$DEV$j" # if the partition exists if [ -e $DEVn ] then # searching if the device is mounted using grep on the command mount INMOUNT="`mount|grep -o $DEVn`" case $INMOUNT in $DEVn) # this means that the device is mounted so do nothing :) a="" ;; *) # this means that the device is not mounted # getting UUID, LABEL and TYPE's file system of listed device uuid=`blkid $DEVn|awk -F"UUID" '{print $2;}'|awk -F"TYPE" '{print $1;}'|sed s/=//|sed s/\"//g` label=`blkid $DEVn|awk -F"LABEL" '{print $2;}'|awk -F"UUID" '{print $1;}'|sed s/=//|sed s/\"//g` type=`blkid $DEVn|awk -F"TYPE" '{print $2;}'|sed s/=//|sed s/\"//g` # these 3 instructions are made for deleting spaces at the end of each string # so test with case will work UUID=`echo -n $uuid` LABEL=`echo -n $label|sed s/\'/_/g|sed s/" "/_/g` TYPE=`echo -n $type` # now we can do something with the unmounted partition case $TYPE in swap) # if the type of the partition is swap, then do nothing :) a="" ;; ext4|ext3|vfat|ntfs) # here we can really do something echo " Detecting $DEVn UUID=$UUID, LABEL=$LABEL, FSTYPE=$TYPE" # if partition has a label or not if [ $LABEL = "" ] then DIRNAME="/mnt/$UUID" else DIRNAME="/mnt/$LABEL" fi # verifying if the $DIRNAME exists in order to mount the partition in this directory if test -d $DIRNAME then a="" else mkdir $DIRNAME fi # adding the device into the fstab file echo " Adding $DEVn in /etc/fstab" echo "#$DEVn =>" >> /etc/fstab echo "UUID=$UUID $DIRNAME auto defaults,uid=1000,gid=1000 0 0" >> /etc/fstab echo "$DIRNAME" >> /var/log/mounted.log # adding the new directory in exports file to use with nfs echo " Adding $DEVn in /etc/exports" echo "$DIRNAME 10.0.2.0/23(rw,sync,no_root_squash,no_subtree_check)" >> /etc/exports ;; *) # in all other case, do nothing a="" ;; esac ;; esac fi done fi done # mounting all file system echo " Mounting all devices" mount -a # reloading NFS server to share the directory on the network echo " Reloading NFS server" service nfs-kernel-server reload echo "Done" } do_stop() { echo "Stopping USB mount" # unmounting all USB device for i in `cat /var/log/mounted.log` do echo "Unmount $i" umount $i done # removing log file rm /var/log/mounted.log # setting default files cat /etc/fstab.bak > /etc/fstab cat /etc/exports.bak > /etc/exports # reloading NFS server service nfs-kernel-server reload } case $1 in start) do_start ;; stop) do_stop ;; restart) do_stop sleep 10 do_start ;; *) echo "Usage : service mount-usb {start|stop|restart}" ;; esac exit 0