[ltp] handling UltraBay with hard disk

Simon Williams linux-thinkpad@linux-thinkpad.org
Sat, 09 May 2009 09:48:31 +0100


This is a multi-part message in MIME format.
--------------020701070608040309070904
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

Peter F. Patel-Schneider wrote:
>> Lay it on me.  What I have is a modified version of the hotswap stuff
>> from ThinkWiki and *it* is a crazy combination of a shell script and
>> perl.   >> I would also like to update the ThinkWiki page
>> http://www.thinkwiki.org/wiki/How_to_hotswap_UltraBay_devices
>> but I don't know what can safely be removed.  Perhaps I'll just clean
>> out anything that looks too old to me and see if anyone screams.

> As thinkwiki.org has been down for a while, I thought that I would
> socialize my 90% solution directly.

It would be great if between us we could write up a proper solution. I 
have exams for the next month or so, but after that I might get back 
into this a bit.

I know nothing about HAL/udev or whatever other dynamic device managers, 
but it should be possible to put it into one of those surely. KDE and 
gnome have options for ejecting USB devices now, so it must be possible 
to tell them how to eject an ultrabay device and put it into the same 
system like it is in Windows.

Here's my current script anyway. ub is the main script I run to 
eject/insert. I use kdialog for errors because I run it from alt+f2 in 
KDE. ubject is a sudo'd script to power off the device. I also added an 
option to flash the LED so I knew it had succeeded. I think the idea was 
I would use that to provide output instead of kdialog and hook it into 
the ACPI events, but I didn't get round to it.

Apologies for it being truly horrible code. This was written a long time 
ago, before I did any real coding. You should see my 'fixsound' and 
'wifi' scripts! I really should learn to do these things properly lol.

Hope this is of some use. Please do whatever you like with it. It works 
for me at least. You'll need the debian-hotswap package though.

Simon

--------------020701070608040309070904
Content-Type: text/plain;
 name="ubeject"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="ubeject"

#!/bin/bash
case $1 in
	bayled)
	echo 4 blink > /proc/acpi/ibm/led
	sleep 3
	echo 4 on > /proc/acpi/ibm/led
	;;
	*)
#	echo eject > /proc/acpi/ibm/bay
	echo 1 > /sys/devices/platform/bay.0/eject
	;;
esac

--------------020701070608040309070904
Content-Type: text/plain;
 name="ub"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="ub"

#!/bin/bash

hotswap=/usr/bin/hotswap
CDROMDEVICE=/dev/hdc
RESCAN=1
INSERT=0

sync

eject_commands()
{ :
INSERT=0
	sync
	PROBESTATE=`$hotswap probe-ide`
	if [ "$PROBESTATE" == "absent" ]; then
		if [ "`mount | grep /dev/fd0`" != "" ]; then
			# floppy mounted
			sync
			umount /dev/fd0
			if [ "`mount | grep /dev/fd0`" != "" ]; then
				kdialog --title Hotswap --sorry "Unable to unmount /dev/fd0."
			else
				sudo /usr/local/bin/ubeject
				if [ "$?" == "0" ]; then
					INSERT=1
				else
#					kdialog --title Hotswap --msgbox "Remove the floppy drive and click OK." ||
					kdialog --title Hotswap --sorry "Unable to power down device." && 
					echo "Unable to power down device."
				fi
			fi
		else
#			kdialog --title Hotswap --sorry "No device detected."
			echo "No device present or floppy not mounted."
			sudo /usr/local/bin/ubeject
			if [ "$?" == "0" ]; then
				INSERT=1
			else
#				kdialog --title Hotswap --msgbox "Remove the floppy drive and click OK." ||
				kdialog --title Hotswap --sorry "Unable to power down device." &&
				echo "Unable to power down device."
			fi
		fi
	else
		MOUNTSTATE=`$hotswap mounted-ide`
		if [ "$MOUNTSTATE" == "yes" ] || [ "`mount | grep $CDROMDEVICE`" != "" ]; then
			sync
			for dev in `mount | grep $CDROMDEVICE | cut -d " " -f 1` ; do
			umount $dev
			done
		fi
		MOUNTSTATE=`$hotswap mounted-ide`
		if [ "$MOUNTSTATE" != "no" ] || [ "`mount | grep $CDROMDEVICE`" != "" ]; then
			kdialog --title Hotswap --sorry "Device still mounted: `mount | grep $CDROMDEVICE | cut -d " " -f 1`"
			echo "Device still mounted: `mount | grep $CDROMDEVICE | cut -d " " -f 1 `"
		else
			$hotswap unregister-ide
			RETURN=$?
			if [ "$RETURN" != "0" ]; then
				kdialog --title Hotswap --error "Something has gone wrong with unregistering."
				echo "Something has gone wrong with unregistering."
			else
				sudo /usr/local/bin/ubeject
                                if [ "$?" == "0" ]; then
                                        INSERT=1
                                else
#                                       kdialog --title Hotswap --msgbox "Remove the floppy drive and click OK." ||
                                        kdialog --title Hotswap --sorry "Unable to power down device." &&
                                        echo "Unable to power down device."
                                fi
	
			fi
		fi
	fi
if [ "$INSERT" = "1" ]; then
	true
else
	false
fi
}

insert_commands()
{ :
RESCAN=1
while [ "$RESCAN" == "1" ]; do
	sync
	$hotswap rescan-ide
	RETURN=$?
	sleep 1
	if [ "$RETURN" != "0" ]; then
		echo "Something has gone wrong with registering."
		kdialog --title Hotswap --warningyesno "Something has gone wrong with registering. Rescan?"
		RETURN=$?
		if [ "$RETURN" == "0" ]; then
			RESCAN=1
		else
			RESCAN=0
		fi
	else if [ "`$hotswap probe-ide`" == "absent" ]; then
		echo "No IDE device detected."
		kdialog --title Hotswap --warningyesno "No IDE device detected. Rescan?"
		RETURN=$?
		if [ "$RETURN" == "0" ]; then
			RESCAN=1
		else
			RESCAN=0
		fi
	else
		RESCAN=0
		sudo /usr/local/bin/ubeject bayled
	fi
	fi
done
}

scan_commands()
{ :
	sync
	PROBESTATE=`$hotswap probe-ide` &&
	kdialog --title Hotswap --msgbox "$PROBESTATE"
	echo "$PROBESTATE"
}

case $1 in
	eject)
		eject_commands
	;;
	insert)
		insert_commands
	;;
	swap)
		eject_commands && insert_commands
	;;
	scan)
		scan_commands
	;;
	*)
		kdialog --title Hotswap --sorry "Usage: hotswap {eject|insert|swap|scan}."
	;;
esac

--------------020701070608040309070904--