#!/bin/sh # Mount and view pen drive # needs sudo permission for mount and umount # # (c) Eko M. Budi, 2004 # (c) Vector Linux, 2004 # # License : GNU GPL MNT_PENDRIVE=${MNT_PENDRIVE:-/mnt/pendrive} MNT_USB_STORAGE=${MNT_USB:-/mnt/usb-storage} # Check if already mounted (should be by usb-mount) LINE=`mount | grep -e "^.* on $MNT_USB_STORAGE"` if [ "$LINE" ]; then echo "$MNT_USB_STORAGE is already mounted, exploring ..." explorer $MNT_USB_STORAGE exit 0 fi /sbin/modprobe usb-storage &> /dev/null sleep 1 # Determine user through ownership of console. Barring the absence of # perl or misconfigured X, this command cannot fail. Look familiar? It's used # by explorer, too. USB_USER=$(ls -l /dev/console | perl -e \ 'local $_ = <>; split / +/; print $_[2], "\n";'); USB_GROUP=users DEF_OPT="sync,dirsync,user" case `uname -r` in 2.2*|2.4*) MASK_OPT="umask=7" ;; *) MASK_OPT="umask=117,dmask=007" ;; esac mount_dev() { FSYS=`sudo disktype /dev/$1 | grep -m 1 "file system" | cut -f1 -d ' '` case $FSYS in Ext2) fsOpt="noatime" fsType="ext2" ;; Ext3) fsOpt="noatime" fsType="ext3" ;; ReiserFS) fsOpt="noatime" fsType="reiserfs" ;; NTFS) fsOpt="uid=$USB_USER,gid=$USB_GROUP,$MASK_OPT,quiet,noatime" fsType="ntfs" ;; FAT16|FAT32) fsOpt="uid=$USB_USER,gid=$USB_GROUP,$MASK_OPT,shortname=mixed,quiet,noatime" fsType="vfat" ;; "") return 1 ;; *) fsOpt="uid=$USB_USER,gid=$USB_GROUP,$MASK_OPT,quiet,noatime" fsType="auto" ;; esac echo "Pendrive found on $DEV" mkdir -p $MNT_PENDRIVE echo echo "mount /dev/$DEV $MNT_PENDRIVE -t $fsType -o $fsOpt,$DEF_OPT" if sudo /bin/mount /dev/$DEV $MNT_PENDRIVE -t $fsType -o $fsOpt,$DEF_OPT; then echo "Pendrive mounted on $MNT_PENDRIVE" explorer $MNT_PENDRIVE sudo /bin/umount $MNT_PENDRIVE echo "Pendrive unmounted" exit 0 else message "ERROR" "Could not mount pendrive $DEV" exit 1 fi echo CCC sleep 1 exit 0 } # Kernel 2.4. based # # Tested on Kernel 2.4.27 # Dependes on dmesg output like this: # # Attached scsi removable disk sda at scsi0, channel 0, id 0, lun 0 # SCSI device sda: 240640 512-byte hdwr sectors (123 MB) # sda: Write Protect is off # sda: sda1 <<== detected device (sda1) # WARNING: USB Mass Storage data integrity not assured # USB Mass Storage device found at 2 <<== identified pattern pd_kernel24() { echo "Kernel 2.4 USB pendrive:" LINE=`dmesg | grep -B 5 -e "^USB Mass Storage device found" | grep -e '^ sd.:'` if [ "$LINE" ]; then DEV=`echo $LINE | cut -f2 -d:` DEV=`echo $DEV` mount_dev $DEV else message "ERROR" "Could not find pendrive" exit 1 fi } # Tested on Kernel 2.6.12 # Dependes on dmesg output like this: # # sda : sda1 <<== detected device (sda1) # As of Standard 5.0 RC2, we are using usb-storage driver pd_kernel26() { echo "Kernel 2.6 USB pendrive:" DEV=`dmesg | grep -A 12 "^usb-storage: device found" | grep '^ sd.:' | \ sed -e 's/^ sd.: //g'` if [ "$DEV" ]; then mount_dev $DEV; else message "ERROR" "Could not find pendrive" exit 1 fi } if [[ "`uname -r`" =~ "2.4.[0-9]*" ]] ; then pd_kernel24 elif [[ "`uname -r`" =~ "2.6.[0-9]*" ]]; then pd_kernel26 else message "ERROR" "Unknown kernel version" exit 1 fi