#!/bin/bash DEBUG=no # usbdrived # # Author: Daniel Cifuentes # Version: v67, July 2008 # This major version: # - Handles multiple slices on multiple disk(s) # - Creates a link for each partition found in a single directory # - Recognises Gnome and creates desktop icons # - Handles Windows and GNOME at the same time # - Presents the links via the mount name for Windows users # - Has improved stop capability # - Creates README files for end users (configurable) # - has an installer to run auto from X11 startup # - Does not allow the creation of files on the mount point/linkdir # - Installer works in Solaris and Linux - tested on Centos 5.1 and SLES 10.1 # This minor version: # fixes the index to display install instructions # change the link to a visible one # moved the sleep to the front to allow login to complete # better "exiting" after user exits session # cleaned up all output redirections and implemented better debugging # set nautilus to open the real directory for the link so that windows will close when a drive is pulled # Where the output of the command is going to - useful when debugging if [ $DEBUG = yes ] then set -x rm -f /tmp/usbdrived.$USER.* DEFAULTOUTPUT=/tmp/usbdrived.$USER.$$ set > $DEFAULTOUTPUT echo >> $DEFAULTOUTPUT else DEFAULTOUTPUT=/dev/null fi print-install-instructions () { #GENERAL INSTRUCTIONS BELOW, AND SAME IS PRINTED WHEN CALLING usbdrived install #Note that correct interpretation is achieved through "usbdrived install" call cat < ]" #==================================================================== # # USAGE NOTES: # There is no builtin mechanism (yet?) to eject the USB drive(s) and it is therefore # always advisable to ensure that there are no write operations taking place when # unplugging a USB drive. There are no guarantees given when using this tool that data will # not be corrupted by device pull out. # #===================================================================== # ********** PARAMETER SETTINGS ********************* #How often to check for devices SLEEP=8 #Should the system create a usage README file? [yes/no] CREATEUSAGEFILE=yes USAGEFILENAME="Attaching USB Drives to your Sun Ray README.txt" #For icons on the desktop - GNOME if [ `uname` = "SunOS" ] then GNOMEICON=blueprint-gnome-dev-harddisk.png else GNOMEICON=gnome-dev-harddisk-usb.png fi #The name for the script USBDRIVEDNAME=usbdrived #If there is a file manager, specify it here #The script will make sure you are under gnome by looking for $HOME/Desktop [ -x /opt/gnome/bin/nautilus ] && FILEMGRCMD="/opt/gnome/bin/nautilus --no-desktop " #SLES 10SP1 [ -x /usr/bin/nautilus ] && FILEMGRCMD="/usr/bin/nautilus --no-desktop " #Solaris/RED HAT 5.1/CENTOS 5.1 #Where the script is to be copied to and run from SCRIPTINSTALLDIR=/opt/SunRayAddons/bin #These next ones should not need changing #What name will the links go by? USBDRIVENAME="USBDRV" #Where are the links to be created? USBLINKDIR=$HOME/USBDRVS #Where Sun Ray keeps its mounts and disks SRSSMOUNTS=/tmp/SUNWut/mnt/$LOGNAME #Where is utdiskadm installed? #This default can be overridden in case your SRSS is somewhat old UTDISKADM=/opt/SUNWut/bin/utdiskadm # ********** END PARAMETER SETTINGS ********************* create-gnome-icon () { #$1 is the path to the link DISKPATH=$1 DISKNAME=`basename $DISKPATH` ICONNAME=$HOME/Desktop/${USBDRIVENAME}-$DISKNAME.desktop echo "[Desktop Entry]" > $ICONNAME echo "Version=1.0" >> $ICONNAME echo "Encoding=UTF-8" >> $ICONNAME echo "Type=Link" >> $ICONNAME echo "URL=file://$DISKPATH" >> $ICONNAME echo "Icon=$GNOMEICON" >> $ICONNAME echo "Name=USB Drive [$DISKNAME]" >> $ICONNAME } create-usage-file () { cat < "$USAGEFILE" README - HOW TO USE USB DRIVES CONNECTED TO YOUR SUN RAY ======================================================== In general: - If the files you are manipulating are large, consider copying them onto your desktop first and then copying them back when you are done. - Make sure you give the system enough time to open your directories and files, as there's a lot going on over the network. - Remember to save your files before unplugging the drive, and ensure that both your system feedback and the activity light on the drive are quisced before unplugging. - Remember to close your applications before unplugging your drive. You may otherwise find that some windows will remain open even if the drive is no longer attached. The system will ensure that your experience is nevertheless adequate, but for your own good.... Under Microsoft Windows: - A network drive ("USBDRVS") is created in "My Computer" to create pointers to all your USB drives/partitions. - In this network drive (USBDRVS) you will find a folder for each of your mounted USB drives/partitions. - As you plug and unplug your USB drive(s) be sure to "refresh" this drive so that you can see what's really there at any point in time. Under Gnome/JDS: - When you insert a USB drive, you will automatically see an icon on your desktop for every drive/partition. - A file manager (typicall nautilus) will open automatically for each drive/partition as they are made available - These icons and windows will disappear automatically when unplugging the drive(s) daniel.cifuentes@sun.com/2008 CLOSEFILE } cleanup-and-exit () { # EXITCODES ARE: # 0 stop was called or usbdrived was ctrl-c'd # 1 usbdrived is already running # 2 Session is not on a Sun Ray! # 3 Exiting because the X session exited or this proc was orphaned # 4 received a trap to exit # 99 Unknown Error EXITCODE=$1 #usbdrived is already running, better not clean up! [ "$EXITCODE" = 1 ] && exit 1 # Return mount points to their healthy state... chmod 700 $USBLINKDIR chmod 700 $SRSSMOUNTS rm -r $USBLINKDIR rm $HOME/Desktop/${USBDRIVENAME}*.desktop #Delete usage file if its there rm "$USAGEFILE" #usbdrived was ctrl-c'd [ -z "$EXITCODE" ] && EXITCODE=0 exit $EXITCODE } run-usbdrived-installer () { #Check utdiskadm version UTDKAV=`cat /opt/SUNWut/bin/utdiskadm | grep '# ident "@(#)utdiskadm.ksh' | awk '{print $4}'` if [ $UTDKAV \< 1.23 ] then echo echo "Your SRSS has an old version of utdiskadm which does not " echo "work under CAM mode. To address this issue:" echo "1) Make a copy of /opt/SUNWut/bin/utdiskadm, e.g. utdiskadm.fix" echo "2) Edit utdiskadm.fix and change the line" echo " UTUSERNAME=\"\`logname\`\"" echo " to" echo " UTUSERNAME=\$USER" echo "3) Change the PARAMETER in this script that reads" echo " UTDISKADM=/opt/SUNWut/bin/utdiskadm" echo " to point to your utdiskadm.fix" echo "Now, ctrl-C this script or continue with the installation." fi echo echo "This option installs usbdrived to launch automatically as follows:" echo "0) GENERIC INSTALL INSTRUCTIONS" echo "1) System-wide X11 Startup (preferred)" echo "2) /etc/profile" echo "3) .profile login for this user" echo echo -n "Please enter your install choice: " read INSTALLOPTION if [ "$INSTALLOPTION" = 0 ] then print-install-instructions else id | grep "uid=0" > /dev/null 2>&1 if [ $? -ne 0 ] then USERISROOT=no else USERISROOT=yes fi case "$INSTALLOPTION" in 1) if [ `uname -s` = "SunOS" ] then #There was much debate on whether this should reside in /etc/dt/config/Xsession.d #however, /etc/dt/... scripts are executed BEFORE /usr/dt/.... #and at that point, UTDEVROOT is not set, which causes the sript to fail when #invoking utdiskadm. DESTINATIONFILE=/usr/dt/config/Xsession.d/1350.usbdrived else DESTINATIONFILE=/etc/X11/xinit/xinitrc.d/1350.usbdrived.sh fi #Delete the file if there is an old install... if [ $USERISROOT = no ] then echo "You must run this command as root." exit else rm $DESTINATIONFILE >/dev/null 2>&1 fi;; 2) DESTINATIONFILE=/etc/profile if [ $USERISROOT = no ] then echo "You must run this command as root." exit fi;; 3) DESTINATIONFILE=$HOME/.profile SCRIPTINSTALLDIR=$HOME/bin;; *) echo "You need to choose options 1-4. Exiting..." exit;; esac echo -n "Directory to install usbdrived file to: [$SCRIPTINSTALLDIR] " read USERINSTALLDIR if [ ! -z "$USERINSTALLDIR" ] then SCRIPTINSTALLDIR=$USERINSTALLDIR fi mkdir -p $SCRIPTINSTALLDIR cp `dirname $0`/`basename $0` $SCRIPTINSTALLDIR/$USBDRIVEDNAME if [ $? -ne 0 ] then echo "Could not copy the script to destination directory." echo "Please try again." exit else chmod +x $SCRIPTINSTALLDIR/$USBDRIVEDNAME fi #This will not clobber any existing files... ">>" echo "/opt/SUNWut/bin/utaction -c \"exec $SCRIPTINSTALLDIR/usbdrived start\" -d \"exec $SCRIPTINSTALLDIR/usbdrived stop\" -i &" >> $DESTINATIONFILE if [ $? -ne 0 ] then echo "Could not create launcher command in $SCRIPTINSTALLDIR." echo "Please try again." exit fi chmod +x $DESTINATIONFILE echo "The usbdrived utility has been installed. You will need to restart your session to start it." fi } start () { # Not on a Sun Ray, time to go! if [ -z "$SUN_SUNRAY_TOKEN" ] then cleanup-and-exit 2 fi if [ ! -d $USBLINKDIR ] then mkdir $USBLINKDIR if [ -d $HOME/Desktop ] then # For Gnome/JDS users, the file goes on the desktop USAGEFILE="$HOME/Desktop/$USAGEFILENAME" else # For Windows users, the file goes on the USBDRV net disk in "My Computer" USAGEFILE="$USBLINKDIR/$USAGEFILENAME" fi [ $CREATEUSAGEFILE = yes ] && create-usage-file else PID=`cat $USBLINKDIR/.PID` if [ -z "$PID" ] then #I'm not sure what just happened! cleanup-and-exit 99 else ps -p $PID | grep -v TTY if [ $? -eq 0 ] then # usbdrived is running already! cleanup-and-exit 1 fi fi fi # OK, usbdrived is not running, set the stage echo $$ > $USBLINKDIR/.PID CURRMOUNTS="" # Change mode of mount point to avoid users creating files there in JDS chmod 500 $SRSSMOUNTS while [ 1 ] do #the sleep time is at the begining to allow complete login before #setting anything up, as the desktop may not be up and running just yet sleep $SLEEP DEVICE=notempty NEWMOUNTS="" for UTDISKADMLINE in `$UTDISKADM -l | grep /tmp/SUNWut/mnt | awk '{print $3}'` do NEWMOUNTS="$NEWMOUNTS $UTDISKADMLINE" done if [ "$NEWMOUNTS" != "$CURRMOUNTS" ] then #Mounts have changed, action! CURRMOUNTS=$NEWMOUNTS for DISK in $NEWMOUNTS do #We have found a device to check on ls -l ${USBLINKDIR}/* 2>/dev/null | grep `basename $DISK` if [ $? -ne 0 ] then #The device is not linked NEWLINK=${USBLINKDIR}/`basename $DISK` # Change the linkdir permissions to create a new link # and restore after, to avoid user files from being created there chmod 700 $USBLINKDIR ln -s $DISK $NEWLINK chmod 500 $USBLINKDIR if [ -d "$HOME/Desktop" ] then #We are in GNOME, create icon on desktop create-gnome-icon $NEWLINK #Start file manager if there is one, and it's desired [ ! -z "$FILEMGRCMD" ] && ($FILEMGRCMD "$DISK" &) fi fi done #Now, we check the list in reverse, we look at the devices linked #and if the link exists but the device is not there, we pull it... DIRENTRY=notempty LINKNUM=1 for LINKENTRY in `file -h "${USBLINKDIR}"/* | grep "symbolic link" | cut -f1 -d:` do echo $NEWMOUNTS | grep `basename $LINKENTRY` if [ $? -ne 0 ] then #Device for link is not mounted! chmod 700 $USBLINKDIR rm "$LINKENTRY" chmod 500 $USBLINKDIR if [ -d "$HOME/Desktop" ] then #We are in JDS, delete icons on desktop grep -l `basename $LINKENTRY` $HOME/Desktop/${USBDRIVENAME}*.desktop 2>/dev/null | xargs rm fi fi done fi #Last administrative issue - make sure I'm not orphaned... ps -p $PPID [ $? -eq 1 ] && cleanup-and-exit 3 done } case "$1" in start) trap cleanup-and-exit 2 3 start >> $DEFAULTOUTPUT 2>&1;; stop) PID=`cat $USBLINKDIR/.PID` if [ ! -z "$PID" ] then kill -9 $PID fi cleanup-and-exit 0;; install) run-usbdrived-installer;; *) echo "USAGE: usbdrived ";; esac