Difference between revisions of "How to hotswap Ultrabay devices"

From ThinkWiki
Jump to: navigation, search
m (The unmount-part have to run if docked == 1)
(Remove hdparm -Y from eject script (see discussion for rationale))
 
(18 intermediate revisions by 9 users not shown)
Line 95: Line 95:
 
# To find the right value, insert the UltraBay optical drive and run:
 
# To find the right value, insert the UltraBay optical drive and run:
 
# udevadm info --query=path --name=/dev/sr0 | perl -pe 's!/block/...$!!'
 
# udevadm info --query=path --name=/dev/sr0 | perl -pe 's!/block/...$!!'
if [ "$DEVPATH" = "" ]
+
 
then
+
# Try to guess DEVPATH. It works with optical drive or Hard disk (or SSD)
  DEVPATH="/devices/pci0000:00/0000:00:1f.2/host1/target1:0:0/1:0:0:0"
+
if [ "$DEVPATH" = "" ]; then
 +
        # try to guess for CD
 +
        DEVPATH=$(udevadm info --query=path --name=/dev/sr0 | perl -pe 's!/block/...$!!')
 +
        if [ "$DEVPATH" = "" ]; then
 +
                echo "No optical device attached?"
 +
                echo "Try for harddisk"
 +
                # try to guess for sdb
 +
                DEVPATH=$(udevadm info --query=path --name=/dev/sdb | perl -pe 's!/block/...$!!')
 +
        fi
 
fi
 
fi
  
Line 141: Line 149:
 
}
 
}
  
# Get the UltraBay's /dev/foo block device node
+
if [ -d $ULTRABAY_SYSDIR ]; then
ultrabay_dev_node() {
+
logger ultrabay_eject dock occupied, shutting down storage device $DEVPATH
        UDEV_PATH="`readlink -e "$ULTRABAY_SYSDIR/block/"*`" || return 1
+
sync
        UDEV_NAME="`udevadm info --query=name --path=$UDEV_PATH`" || return 1
+
# Unmount filesystems backed by this device
        echo /dev/$UDEV_NAME
+
## This seems to be very inelegant and prone to failure
}
+
unmount_rdev `cat $ULTRABAY_SYSDIR/block/*/dev    \
 
+
  $ULTRABAY_SYSDIR/block/*/*/dev` \
if [ $( cat $dock/docked ) == 0 ]; then
+
|| {
 +
logger ultrabay_eject umounting failed
 +
echo 2 > /proc/acpi/ibm/beep  # triple error tone
 +
notify-send -u critical -t 100000 "ThinkPad Ultrabay eject failed" "Please do not pull the device, doing so could cause file corruption and possibly hang the system. Unmounting of the filesystem on the ThinkPad Ultrabay device failed. Please put the eject leaver back in place, and try to unmount the filesystem manually. If this succeeds you can try the eject again"
 +
exit 1;
 +
}
 +
sync
 +
# Let HAL+KDE notice the unmount
 +
sleep 0.5
 +
# Unregister this SCSI device:
 +
sync
 +
echo 1 > $ULTRABAY_SYSDIR/delete
 +
elif [ $( cat $dock/docked ) == 0 ]; then
 
         logger ultrabay_eject dock reports empty
 
         logger ultrabay_eject dock reports empty
else  
+
else
if [ -d $ULTRABAY_SYSDIR ]; then
+
logger ultrabay_eject bay occupied but incorrect device path $DEVPATH
logger ultrabay_eject dock occupied, shutting down storage device $DEVPATH
+
notify-send -u critical -t 100000 "ThinkPad Ultrabay eject failed" "Bay occupied but incorrect device path"
sync
+
echo 2 > /proc/acpi/ibm/beep  # triple error tone
# Unmount filesystems backed by this device
+
exit 1
## This seems to be very inelegant and prone to failure
 
unmount_rdev `cat $ULTRABAY_SYSDIR/block/*/dev    \
 
  $ULTRABAY_SYSDIR/block/*/*/dev`  \
 
|| {
 
logger ultrabay_eject umounting failed
 
echo 2 > /proc/acpi/ibm/beep  # triple error tone
 
notify-send -u critical -t 100000 "ThinkPad Ultrabay eject failed" "Please do not pull the device, doing so could cause file corruption and possibly hang the system. Unmounting of the filesystem on the ThinkPad Ultrabay device failed. Please put the eject leaver back in place, and try to unmount the filesystem manually. If this succeeds you can try the eject again"
 
exit 1;
 
}
 
sync
 
# Nicely power off the device
 
DEVNODE=`ultrabay_dev_node` && hdparm -Y $DEVNODE
 
# Let HAL+KDE notice the unmount and let the disk spin down
 
sleep 0.5
 
# Unregister this SCSI device:
 
sync
 
echo 1 > $ULTRABAY_SYSDIR/delete
 
else
 
logger ultrabay_eject bay occupied but incorrect device path $DEVPATH
 
notify-send -u critical -t 100000 "ThinkPad Ultrabay eject failed" "Bay occupied but incorrect device path"
 
echo 2 > /proc/acpi/ibm/beep  # triple error tone
 
exit 1
 
fi
 
 
fi
 
fi
  
Line 192: Line 189:
 
notify-send -u normal -t 10000 "Safe to remove device" "The ThinkPad Ultrabay device can now safely be removed"
 
notify-send -u normal -t 10000 "Safe to remove device" "The ThinkPad Ultrabay device can now safely be removed"
 
</pre>
 
</pre>
 +
a possible fix for the notify not working thing is here [[Talk:How_to_hotswap_Ultrabay_devices#get_notify-send_working]]
 +
 
Then make sure ownership and permissions are set correct
 
Then make sure ownership and permissions are set correct
 
  chown root:root /usr/local/sbin/ultrabay_eject
 
  chown root:root /usr/local/sbin/ultrabay_eject
Line 244: Line 243:
 
This was tested on a T61p running Gentoo with tuxonice 2.6.28-r8 without any issues.
 
This was tested on a T61p running Gentoo with tuxonice 2.6.28-r8 without any issues.
  
 +
===udisks support===
 +
Just like the HAL support mentioned below this is only about marking the device as hotpluggable.
  
 +
Recently distributions do not use HAL anymore. On these systems Gnome, KDE, etc. use udisks instead to discover installed disks. So today most people will have to tell udisks that the ultrabay device is a removeable device. The HAL configuration described below does not work anymore.
 +
 +
Udisks reads device properties from udev. In order to force a device to be considered "removeable" we have to set the udev property "UDISKS_SYSTEM_INTERNAL" to "0". (man 7 udisks)
 +
In order for this to work you need at least version 1.0.3 of udisks.
 +
 +
Here is an example of a udev rule that adds the property. You can add it to the file that contains the other utrabay rules (i.e. {{path|/etc/udev/rules.d/50-thinkpad-ultrabay.rules}}). You may have to adjust the DEVPATH.
 +
<pre>
 +
SUBSYSTEMS=="block", DEVPATH=="/devices/pci0000:00/0000:00:1f.1/host4/*", ENV{UDISKS_SYSTEM_INTERNAL}="0"
 +
</pre>
 +
 +
Now tell udev to reread the rules:
 +
<pre>
 +
udevadm control --reload-rules
 +
</pre>
 +
 +
Now your window manager should give you a notification when you plug in a disk into the ultrabay.
  
 
===HAL support===
 
===HAL support===
Line 354: Line 371:
 
           </match>
 
           </match>
 
         </match>
 
         </match>
 +
 +
        <!-- Lenovo ThinkPad T410, T510 -->
 +
        <match key="/org/freedesktop/Hal/devices/computer:system.hardware.version" contains_outof="T410;T510">
 +
          <match key="storage.bus" string_outof="pci;scsi">
 +
            <match key="info.parent" string="/org/freedesktop/Hal/devices/pci_8086_3b2f_scsi_host_0_scsi_device_lun0">
 +
              <merge key="storage.hotpluggable" type="bool">true</merge>
 +
            </match>
 +
          </match>
 +
        </match>
 +
  
 
       </match>
 
       </match>
Line 368: Line 395:
  
 
To check the effect, look at your lshal output, the section for your optical disk (/dev/sr0 typically) or your 2nd hdd (/dev/sdb typically) should have storage.hotpluggable = true after restarting your hal deamon or rebooting.
 
To check the effect, look at your lshal output, the section for your optical disk (/dev/sr0 typically) or your 2nd hdd (/dev/sdb typically) should have storage.hotpluggable = true after restarting your hal deamon or rebooting.
 +
 +
===Disable ultrabay at boot time===
 +
To safe some energy you can create an init.d start script to automatically "eject" the ultrabay at boot time<br />
 +
vim /etc/init.d/stop-ultrabay
 +
 +
#! /bin/sh
 +
### BEGIN INIT INFO
 +
# Provides:          stop-ultrabay
 +
# Required-Start:    $remote_fs $syslog
 +
# Required-Stop:    $remote_fs $syslog
 +
# Default-Start:    2 3 4 5
 +
# Default-Stop:      0 1 6
 +
# Short-Description: stops the ultrabay
 +
### END INIT INFO
 +
 +
# Script to shutdown ultrabay at boot time. To reenable, simply push the ultrabay eject lever
 +
# and put it again.
 +
# By Frank Doelitzscher, 2010-08-04
 +
#
 +
 +
# This things will always run
 +
/usr/local/sbin/ultrabay_eject
 +
 +
# Carry out specific functions when asked to by the system
 +
case "$1" in
 +
*)
 +
    echo "This script runs automatically at boot time."
 +
    echo "It does not need any switches like start|stop"
 +
    exit 1
 +
    ;;
 +
esac
 +
exit 0
 +
Then ensure it has the right permissions<br />
 +
chmod 755 /etc/init.d/stop-ultrabay
 +
 +
And make it known to the init subsystem<br />
 +
update-rc.d -f stop-ultrabay start 90 2 3 4 5 .
 +
 +
It just gets a start script since it does not make sense to execute it at a shutdown or restart.<br />
 +
 +
This was tested on a T60 running Debian Squeeze with standard 2.6.32-5-686 #1 SMP without any issues.
  
 
==When using the <tt>ide-disk</tt> driver==
 
==When using the <tt>ide-disk</tt> driver==
Line 407: Line 475:
  
 
==Password protected HDD==
 
==Password protected HDD==
 +
{{WARN|Do not attempt to set a new HDD password from Linux, if you succeed you may find you can no longer unlock the drive or remove the password in any way}}
 +
There are multiple issues with hotplugging a password protected HDD. In short, it does not work from within the operating system. You will need to reboot, or use the suspend/wakeup procedure below, and let the BIOS do the unlock.
 +
===hdparm===
 +
While in theory you should be a able to use hdparm to unlock a drive, or set or remove a password. In practice none of it works the way it should.
 +
 +
First of all, by default the BIOS during POST will freeze the security settings of any drives that are present, preventing you from making changes until the drive is power cycled. You can see this by running the following command
 +
# hdparm -I /dev/sdb|grep frozen
 +
not frozen
 +
If the above command simply returns 'frozen' any hdparm security commands will fail with an IO error. If this is an Ultrabay HDD you can simply remove the drive and hotplug it again to reset the drive.
 +
 +
If your drive has a PW set by the BIOS, any attempt to unlock it will result in an IO error.
 +
 +
But a fatal flaw is to use hdparm to actually set a password, if you do you will find the following issues
 +
* setting a Master password will appear to succeed, but in fact does nothing
 +
* setting a User password does succeed, but you will no longer be able to unlock or remove the password using either hdparm or the BIOS. hdparm will just give IO errors, and the BIOS will report the password being wrong. It is speculated that the BIOS does some kind of hashing to the password, which is why setting the PW from the BIOS and setting the PW with hdparm is incompatible.
 +
 +
# hdparm --verbose --user-master u --security-disable test /dev/sdb
 +
security_password="test"
 +
 +
/dev/sdb:
 +
  Issuing SECURITY_DISABLE command, password="test", user=user
 +
oflags.lob_all=0x82, flags={ feat command }
 +
oflags.hob_all=0x82, flags={ feat }
 +
using LBA48 taskfile
 +
outgoing cdb:  85 0b 26 00 00 00 00 00 00 00 00 00 00 40 f2 00
 +
data:  00 00 74 65 73 74 00 00 00 00 00 00 00 00 00 00
 +
SG_IO: ATA_16 status=0x2, host_status=0x0, driver_status=0x8
 +
SG_IO: sb[]:  72 0b 00 00 00 00 00 0e 09 0c 01 04 00 00 00 00 00 00 00 00 e0 51 00 00 00 00 00 00 00 00 00 00
 +
SG_IO: desc[]:  09 0c 01 04 00 00 00 00 00 00 00 00
 +
      ATA_16 stat=51 err=04 nsect=00 lbal=00 lbam=00 lbah=00 dev=e0
 +
I/O error, ata_op=0xf2 ata_status=0x51 ata_error=0x04
 +
SECURITY_UNLOCK: Input/output error
 +
 +
*[https://bugzilla.redhat.com/show_bug.cgi?id=603018 bug report against hdparm]
 +
 +
===Letting the BIOS unlock the disk by suspending the computer===
 +
 +
A password protected disk can be unlocked by ensuring the laptop has been suspended '''before''' inserting the disk into the Ultrabay.  The device being removed should still be ejected before suspending the laptop.
 +
 +
Upon waking up (resuming) the computer the BIOS should prompt for the password, and provided you enter it correctly, will unlock the disk.  Once Linux regains control it may be necessary to re-scan the SCSI bus to find the disk and make it available, using the command "sudo rescan-scsi-bus.sh".
 +
 +
This is confirmed to work on a T61 running Ubuntu 8.04, (kernel 2.6.24) with the scsitools package installed.
 +
 +
===libata EH going crazy===
 +
hotplugging a password protected HDD causes the libata error handler to go crazy
 +
 +
ACPI: \_SB_.PCI0.IDE0.SCND.MSTR - docking
 +
ata2: exception Emask 0x10 SAct 0x0 SErr 0x0 action 0xe frozen
 +
ata2: ACPI event
 +
ata2: soft resetting link
 +
ata2.00: ATA-6: HTS726060M9AT00, MH4OA6DA, max UDMA/100
 +
ata2.00: 117210240 sectors, multi 0: LBA
 +
ata2.00: limited to UDMA/33 due to 40-wire cable
 +
ata2.00: configured for UDMA/33
 +
ata2: EH complete
 +
scsi 1:0:0:0: Direct-Access    ATA      HTS726060M9AT00  MH4O PQ: 0 ANSI: 5
 +
sd 1:0:0:0: Attached scsi generic sg1 type 0
 +
sd 1:0:0:0: [sdb] 117210240 512-byte logical blocks: (60.0 GB/55.8 GiB)
 +
sd 1:0:0:0: [sdb] Write Protect is off
 +
sd 1:0:0:0: [sdb] Write cache: enabled, read cache: enabled, doesn't support DPO or FUA
 +
sdb:
 +
 +
After which there are hundreds of errors such as these...
 +
ata2.00: exception Emask 0x0 SAct 0x0 SErr 0x0 action 0x0
 +
ata2.00: BMDMA stat 0x5
 +
ata2.00: failed command: READ DMA
 +
ata2.00: cmd c8/00:08:00:00:00/00:00:00:00:00/e0 tag 0 dma 4096 in
 +
        res 51/04:08:00:00:00/00:00:00:00:00/e0 Emask 0x1 (device error)
 +
ata2.00: status: { DRDY ERR }
 +
ata2.00: error: { ABRT }
 +
ata2.00: configured for UDMA/33
 +
ata2: EH complete
  
If you are hot-swapping a hard disk on a Ultrabay HDD tray, and the HDD has a password set, you will have to enter the password before it can be used. In theory you should be able to do so with hdparm as follows for a Master password:
+
Basically what is happening is that libata is ignoring the ATA IDENTIFY data, that reports that the drive is locked, and just tries to scan for every partition type possible.
hdparm --security-unlock PASSWD /dev/sdb
+
*[https://bugzilla.kernel.org/show_bug.cgi?id=16181 Bug report against libata]
Or if it is a user password
 
hdparm --security-unlock PASSWD --user-master u /dev/sdb
 
  
However on my system with Fedora 11 the above returns "SECURITY_UNLOCK: Input/output error"
+
===udisks===
 +
lastly, there should be some userland support to detect the password protected HDD and prompt you for a password or retrieve the password from gnome-keyring. But that will require hdparm to be able to unlock the HDD first, and to figure out if the BIOS is doing some kind of hashing and how.
  
As such the only available option is to reboot and let the BIOS unlock the HDD.
+
*[https://bugs.freedesktop.org/show_bug.cgi?id=23110 bug report against udisks]
  
 
[[Category:Scripts]]
 
[[Category:Scripts]]
 
[[Category:HOWTOs]]
 
[[Category:HOWTOs]]

Latest revision as of 01:58, 11 January 2024

The following discusses hotswap (AKA "hotplug") of devices in the Ultrabay.

This page has a lot of old information. Some cleanup might be useful.


Which driver?

This may be confusing, so first a quick history lesson.

Older ThinkPads (up to the R51, T42p and X40) were fully IDE (PATA) for both the internal HDD and Ultrabay. For these there are two drivers, the modern libata based ata_piix and the old legacy piix/ide-disk combo. Most modern distributions will default to the ata_piix driver, which is really what you want anyway.

ThinkPads like the R52, T43, T43p, X41 and X41 Tablet are strange hybrids. They have a Serial-ATA (SATA) chipset for the internal disk controller, but the actual disk is still legacy IDE, using a separate SATA-PATA bridge chip. The Ultrabay, though, is still connected to the legacy IDE host controller. This results in a setup where the internal disk is handled by a SATA driver and the Ultrabay is handled by either ata_piix or piix/ide-disk.

ThinkPads like the R60, T60 and Z60 series are also hybrids, but they use a native SATA HDD internal. Ultrabay still uses legacy IDE (PATA) though. So just like with the previous models you end up with two separate drivers for the internal HDD and the Ultrabay.

The latest generation ThinkPads like the R400, R500, T400, T500, W500, W700, X200, and X301 are all fully SATA for both the internal HDD and the Ultrabay device, so both are handled by the same SATA driver.

TODO
Figure out in which category the X60 series ThinkPads truly belong. Some of these machines might have been shipped with a 1.8" HDD which is likely to be PATA, in fact the HMM even states so. But tabook and ltwbook only lists SATA models, so the 1.8" model might never have shipped. But even if it did there will be a SATA-PATA bridge chip, so from the OS it will look like SATA.
Overview of disk interface types
Thinkpad internal HDD Ultrabay Modern Driver Old Driver
A20m, A20p, A21e, A21m, A21p, A22e, A22m, A22p, A30, A30p, A31, A31p Legacy 2.5" IDE (PATA) Legacy IDE (PATA) ata_piix piix + ide-disk
R30, R31, R32, R40, R50, R50e, R50p, R51 Legacy 2.5" IDE (PATA) Legacy IDE (PATA) ata_piix piix + ide-disk
R52 Legacy 2.5" PATA HDD with conversion to SATA Legacy IDE (PATA) ahci + ata_piix -
R60, R61 2.5" SATA Legacy IDE (PATA) ahci + ata_piix -
R400, R500 2.5" SATA SATA ahci -
T20, T21, T22, T23, T30, T40, T40p, T41, T41p, T42, T42p Legacy 2.5" IDE (PATA) Legacy IDE (PATA) ata_piix piix + ide-disk
T43, T43p Legacy 2.5" PATA HDD with conversion to SATA Legacy IDE (PATA) ahci + ata_piix -
T60, T60p, T61, T61p 2.5" SATA Legacy IDE (PATA) ahci + ata_piix -
T400, T500 2.5" SATA SATA ahci -
X20, X21, X22, X23, X24, X30, X31, X32 Legacy 2.5" IDE (PATA) Legacy IDE (PATA) ata_piix piix + ide-disk
X40 Legacy 1.8" IDE (PATA) Legacy IDE (PATA) ata_piix piix + ide-disk
X41, X41 Tablet Legacy 1.8" PATA HDD with conversion to SATA Legacy IDE (PATA) ahci + ata_piix -
X60, X60s, X60 Tablet, X61, X61s, X61 Tablet 2.5" HDD is SATA, 1.8" HDD is PATA with SATA conversion Legacy IDE (PATA) ahci + ata_piix -
X300 2.5" SATA Legacy IDE (PATA) ahci + ata_piix -
X200, X200 Tablet, X301 2.5" SATA SATA ahci -
W500, W700 2.5" SATA SATA ahci -
Z60m, Z60t, Z61e, Z61m, Z61p, Z61t 2.5" SATA Legacy IDE (PATA) ahci + ata_piix -
NOTE!
Debian users: at least up the the kernel released with Lenny, Debian applies a patch in the Debian kernel sources which prevents the use of ata_piix with PATA devices (like the IDE bus for the Ultrabay). You will have to either use an upstream kernel or (re)patch the Debian kernel sources. You may want to have a look at the following Debian bug reports as well: #444182, #463833.

Modern systems (using the ahci or ata_piix drivers)

The following applies when using the ahci or ata_piix driver (which supercedes the old ide_disk driver).

You should at least be using a distribution with kernel 2.6.23. If you have a distribution with an older kernel, please update your distribution, as in addition to a newer kernel, you will need some of the newer userland tools.

Using libata-acpi and udev

Starting from kernel 2.6.26-rc5, it's possible to use libata-acpi in combination with udev to hotswap. Please check if CONFIG_ATA_ACPI is enabled in your kernel configuration.

For 2.6.25, you need two patches which can be easily backported ([1],[2]).

Create a file /etc/udev/rules.d/50-thinkpad-ultrabay.rules with the following contents:

ENV{BAY_EVENT}=="3", ACTION=="change", SUBSYSTEM=="scsi", RUN+="/usr/local/sbin/ultrabay_eject"

Then ensure it has the right permissions

chown root:root /etc/udev/rules.d/50-thinkpad-ultrabay.rules
chmod 644 /etc/udev/rules.d/50-thinkpad-ultrabay.rules

For Red Hat/Fedora and other systems with selinux, you also need to set the security context:

chcon system_u:object_r:etc_runtime_t:s0 /etc/udev/rules.d/50-thinkpad-ultrabay.rules

Now create the below ultrabay-eject script

Script for Ultrabay eject

The following script does the following:

  • Automatically unmounts the relevant filesystems and power off the Ultrabay when the Ultrabay eject lever is released. Screams if some filesystem can't be unmounted.

Create a executable file /usr/local/sbin/ultrabay_eject with the following content. Note if you want to be able to run it directly (not by the UDEV eject script), then you need to manually edit the DEVPATH variable near the top of the script.

#!/bin/bash

# Change the following DEVPATH= to match your system, if you want to run this directly instead of having it called by the udev eject script
# To find the right value, insert the UltraBay optical drive and run:
# udevadm info --query=path --name=/dev/sr0 | perl -pe 's!/block/...$!!'

# Try to guess DEVPATH. It works with optical drive or Hard disk (or SSD)
if [ "$DEVPATH" = "" ]; then
        # try to guess for CD
        DEVPATH=$(udevadm info --query=path --name=/dev/sr0 | perl -pe 's!/block/...$!!')
        if [ "$DEVPATH" = "" ]; then
                echo "No optical device attached?"
                echo "Try for harddisk"
                # try to guess for sdb
                DEVPATH=$(udevadm info --query=path --name=/dev/sdb | perl -pe 's!/block/...$!!')
        fi
fi

shopt -s nullglob
export DISPLAY=:0.0 # required for notify-send
ULTRABAY_SYSDIR=/sys$DEVPATH

# Find generic dock interface for UltraBay
dock=$( /bin/grep -l ata_bay /sys/devices/platform/dock.?/type )
dock=${dock%%/type}
if [ -n "$dock" -a -d "$dock" ]; then
        logger ultrabay_eject starting eject of $dock
else
        logger ultrabay_eject cannot locate bay dock device
        notify-send -u critical -t 100000 "ThinkPad Ultrabay eject failed" "Cannot locate bay dock device"
fi

# Umount the filesystem(s) backed by the given major:minor device(s)
unmount_rdev() { perl - "$@" <<'EOPERL'  # let's do it in Perl
        for $major_minor (@ARGV) {
                $major_minor =~ m/^(\d+):(\d+)$/ or die;
                push(@tgt_rdevs, ($1<<8)|$2);
        }
        # Sort by reverse length of mount point, to unmount sub-directories first
        open MOUNTS,"</proc/mounts" or die "$!";
        @mounts=sort { length($b->[1]) <=> length($a->[1]) } map { [ split ] } <MOUNTS>;
        close MOUNTS;
        foreach $m (@mounts) {
                ($dev,$dir)=@$m;
                next unless -b $dev;  $rdev=(stat($dev))[6];
                next unless grep($_==$rdev, @tgt_rdevs);
                system("umount","-v","$dir")==0  or  $bad=1;
                if ($bad == 1) {
                        system("logger","ultrabay_eject","ERROR unmounting",$dev,$dir);
                        system("notify-send -u critical -t 100000 \"Error unmounting $dir\" \"Unmounting of $dir on $dev failed!\"");
                } else {
                        system("logger","ultrabay_eject","unmounted",$dev,$dir);
                        system("notify-send -u normal -t 5000 \"Unmounted $dir\"");
                };
        }
        exit 1 if $bad;
EOPERL
}

if [ -d $ULTRABAY_SYSDIR ]; then
	logger ultrabay_eject dock occupied, shutting down storage device $DEVPATH
	sync
	# Unmount filesystems backed by this device
	## This seems to be very inelegant and prone to failure
	unmount_rdev `cat $ULTRABAY_SYSDIR/block/*/dev     \
			  $ULTRABAY_SYSDIR/block/*/*/dev`  \
	|| {
		logger ultrabay_eject umounting failed
		echo 2 > /proc/acpi/ibm/beep  # triple error tone
		notify-send -u critical -t 100000 "ThinkPad Ultrabay eject failed" "Please do not pull the device, doing so could cause file corruption and possibly hang the system. Unmounting of the filesystem on the ThinkPad Ultrabay device failed. Please put the eject leaver back in place, and try to unmount the filesystem manually. If this succeeds you can try the eject again"
		exit 1;
	}
	sync
	# Let HAL+KDE notice the unmount
	sleep 0.5
	# Unregister this SCSI device:
	sync
	echo 1 > $ULTRABAY_SYSDIR/delete
elif [ $( cat $dock/docked ) == 0 ]; then
        logger ultrabay_eject dock reports empty
else
	logger ultrabay_eject bay occupied but incorrect device path $DEVPATH
	notify-send -u critical -t 100000 "ThinkPad Ultrabay eject failed" "Bay occupied but incorrect device path"
	echo 2 > /proc/acpi/ibm/beep  # triple error tone
	exit 1
fi

# We need sleep here so someone can disconnect the bay and the drive
sleep 1

# Turn off power to the UltraBay
logger ultrabay_eject undocking $dock
echo 1 > $dock/undock

# Tell the user we're OK
logger ultrabay_eject done
echo 12 > /proc/acpi/ibm/beep
notify-send -u normal -t 10000 "Safe to remove device" "The ThinkPad Ultrabay device can now safely be removed"

a possible fix for the notify not working thing is here Talk:How_to_hotswap_Ultrabay_devices#get_notify-send_working

Then make sure ownership and permissions are set correct

chown root:root /usr/local/sbin/ultrabay_eject
chmod 555 /usr/local/sbin/ultrabay_eject

If the ata_piix driver is built in the kernel, it will catch bay events and you will see the following message in your system log:

thinkpad_acpi: another device driver is already handling bay events
thinkpad_acpi: disabling subdriver bay

You should load thinkpad_acpi before ata_piix if you want it to handle bay events.

Script for Ultrabay insert

ATTENTION!
Only needed for kernels prior to 2.6.26

Create /usr/local/sbin/ultrabay_insert with the following content:

#!/bin/bash
echo 12 > /proc/acpi/ibm/beep
sync
echo 0 0 0 > /sys/class/scsi_host/host1/scan

and set the right permissions

chown root:root /usr/local/sbin/ultrabay_insert
chmod 555 /usr/local/sbin/ultrabay_insert


Using the generic bay driver and udev

ATTENTION!
If you have kernel 2.6.26 or newer, please use the above #Using libata-acpi and udev method instead

Starting from kernel 2.6.23-rc3, it's possible to use the generic bay driver in combination with udev to hotswap. Please check if CONFIG_ACPI_BAY is enabled (module or built-in) in your kernel configuration.

Create /etc/udev/rules.d/ibm-ultrabay.rules:

ENV{BAY_EVENT}=="3", KERNEL=="bay.0", ACTION=="change", SUBSYSTEM=="platform", RUN+="/usr/local/sbin/ultrabay_eject"
ENV{BAY_EVENT}=="1", KERNEL=="bay.0", ACTION=="change", SUBSYSTEM=="platform", RUN+="/usr/local/sbin/ultrabay_insert"

Starting with kernel 2.6.28, the bay driver was removed from the mainline kernel. The dock driver replaced it completely. Make sure CONFIG_ACPI_DOCK is enabled in the kernel, and use the following udev rules:

ENV{EVENT}=="undock", KERNEL=="dock.2", ACTION=="change", SUBSYSTEM=="platform", RUN+="/usr/local/sbin/ultrabay_eject"
ENV{EVENT}=="dock", KERNEL=="dock.2", ACTION=="change", SUBSYSTEM=="platform", RUN+="/usr/local/sbin/ultrabay_insert"

However, the ultra_eject script needs to be changed, otherwise udev goes into an infinite event loop. It will send undock events every few seconds forever. That means as soon as the ultrabay is plugged in again, it will be immediately ejected. For kernels 2.6.28 and higher, the bay cannot be powered off using this method, so change the end of the script to read:

# Turn off power to the UltraBay:
if [ -d /sys/devices/platform/bay.0 ]; then
	echo 1 > /sys/devices/platform/bay.0/eject
elif [ -e /proc/acpi/ibm/bay ]; then
	echo eject > /proc/acpi/ibm/bay
fi
# Tell the user we're OK
echo 12 > /proc/acpi/ibm/beep

Alternatively, instead of modifying the script, use the libata-acpi method below. This was tested on a T61p running Gentoo with tuxonice 2.6.28-r8 without any issues.

udisks support

Just like the HAL support mentioned below this is only about marking the device as hotpluggable.

Recently distributions do not use HAL anymore. On these systems Gnome, KDE, etc. use udisks instead to discover installed disks. So today most people will have to tell udisks that the ultrabay device is a removeable device. The HAL configuration described below does not work anymore.

Udisks reads device properties from udev. In order to force a device to be considered "removeable" we have to set the udev property "UDISKS_SYSTEM_INTERNAL" to "0". (man 7 udisks) In order for this to work you need at least version 1.0.3 of udisks.

Here is an example of a udev rule that adds the property. You can add it to the file that contains the other utrabay rules (i.e. /etc/udev/rules.d/50-thinkpad-ultrabay.rules). You may have to adjust the DEVPATH.

SUBSYSTEMS=="block", DEVPATH=="/devices/pci0000:00/0000:00:1f.1/host4/*", ENV{UDISKS_SYSTEM_INTERNAL}="0"

Now tell udev to reread the rules:

udevadm control --reload-rules

Now your window manager should give you a notification when you plug in a disk into the ultrabay.

HAL support

Help needed
If the script below does not work, please provide your lshal output on the Talk:How_to_hotswap_Ultrabay_devices page
NOTE!
If your HAL version is older then 0.5.11, you need to replace "info.parent" with "storage.physical_device".

HAL support is only needed for proper notification to your desktop applications, such as Gnome and KDE. You will still require the udev script to handle the proper removal.

To tell HAL that devices connected to the UltraBay port are hotpluggable, you need to create a file /etc/hal/fdi/information/10-thinkpad-ultrabay.fdi with the following content:

<?xml version="1.0" encoding="ISO-8859-1"?> <!-- -*- SGML -*- -->

<!--
HAL config file to set ThinkPad Ultrabay eject capability flag.
Note that ThinkPad model matching is done on a partial match, so T40 will also match T40p, which is what we want.

If your ThinkPad model is not listed, please check with lspci what PCI ID your disk controller is
-->

<deviceinfo version="0.2">
  <device>
    <match key="/org/freedesktop/Hal/devices/computer:system.hardware.version" prefix="ThinkPad">
      <match key="/org/freedesktop/Hal/devices/computer:system.hardware.vendor" string="IBM">

        <!-- IBM ThinkPad A20m, A20p, A21e, A21m, A21p, A22e, A22m, A22p, T20, T21, T22, X20 and X21 -->
        <match key="/org/freedesktop/Hal/devices/computer:system.hardware.version" contains_outof="A20;A21;A22;T20;T21;T22;X20;X21">
          <match key="storage.bus" string_outof="pci;scsi">
            <match key="info.parent" string="/org/freedesktop/Hal/devices/pci_8086_7111_scsi_host_0_scsi_device_lun0">
              <merge key="storage.hotpluggable" type="bool">true</merge>
            </match>
          </match>
        </match>

        <!-- IBM ThinkPad T23, T30, X22, X23, X24 and X30 -->
        <match key="/org/freedesktop/Hal/devices/computer:system.hardware.version" contains_outof="T23;T30;X22;X23;X24;X30">
          <match key="storage.bus" string_outof="pci;scsi">
            <match key="info.parent" string="/org/freedesktop/Hal/devices/pci_8086_248a_scsi_host_0_scsi_device_lun0">
              <merge key="storage.hotpluggable" type="bool">true</merge>
            </match>
          </match>
        </match>

        <!-- IBM ThinkPad A30, A30p, A31 and A31p - 2 Ultrabay devices-->
        <match key="/org/freedesktop/Hal/devices/computer:system.hardware.version" contains_outof="A30;A31">
          <match key="storage.bus" string_outof="pci;scsi">
            <match key="info.parent" string="/org/freedesktop/Hal/devices/pci_8086_248a_scsi_host_scsi_device_lun0_0">
              <merge key="storage.hotpluggable" type="bool">true</merge>
            </match>
            <match key="info.parent" string="/org/freedesktop/Hal/devices/pci_8086_248a_scsi_host_0_scsi_device_lun0">
              <merge key="storage.hotpluggable" type="bool">true</merge>
            </match>
          </match>
        </match>

        <!-- IBM ThinkPad R50, R51, T40, T40p, T41, T41p, X31, X32 and X40 -->
        <match key="/org/freedesktop/Hal/devices/computer:system.hardware.version" contains_outof="R50;R51;T40;T41;T42;X31;X32;X40">
          <match key="storage.bus" string_outof="pci;scsi">
            <match key="info.parent" string="/org/freedesktop/Hal/devices/pci_8086_24ca_scsi_host_0_scsi_device_lun0">
              <merge key="storage.hotpluggable" type="bool">true</merge>
            </match>
          </match>
        </match>

        <!-- IBM ThinkPad R52, T43, T43p, X41, Z60m and Z60p -->
        <match key="/org/freedesktop/Hal/devices/computer:system.hardware.version" contains_outof="R52;T43;X41;Z60">
          <match key="storage.bus" string_outof="pci;scsi">
            <match key="info.parent" string="/org/freedesktop/Hal/devices/pci_8086_2653_scsi_host_scsi_device_lun0">
              <merge key="storage.hotpluggable" type="bool">true</merge>
            </match>
          </match>
        </match>

      </match>
      <match key="/org/freedesktop/Hal/devices/computer:system.hardware.vendor" string="LENOVO">

        <!-- Lenovo ThinkPad T60, T60p, Z61m, Z61t and Z61p -->
        <match key="/org/freedesktop/Hal/devices/computer:system.hardware.version" contains_outof="T60;Z61">
          <match key="storage.bus" string_outof="pci;scsi">
            <match key="info.parent" string="/org/freedesktop/Hal/devices/pci_8086_27df_scsi_host_scsi_device_lun0">
              <merge key="storage.hotpluggable" type="bool">true</merge>
            </match>
          </match>
        </match>

        <!-- Lenovo ThinkPad R60 and X60 -->
        <match key="/org/freedesktop/Hal/devices/computer:system.hardware.version" contains_outof="R60;X60">
          <match key="storage.bus" string_outof="pci;scsi">
            <match key="info.parent" string="/org/freedesktop/Hal/devices/pci_8086_27c4_scsi_host_scsi_device_lun0">
              <merge key="storage.hotpluggable" type="bool">true</merge>
            </match>
          </match>
        </match>

        <!-- Lenovo ThinkPad R61, T61, T61p and X300 -->
        <match key="/org/freedesktop/Hal/devices/computer:system.hardware.version" contains_outof="R61;T61;X300">
          <match key="storage.bus" string_outof="pci;scsi">
            <match key="info.parent" string="/org/freedesktop/Hal/devices/pci_8086_2850_scsi_host_scsi_device_lun0">
              <merge key="storage.hotpluggable" type="bool">true</merge>
            </match>
          </match>
        </match>

        <!-- Lenovo ThinkPad R400, R500, T400, T500, W500, W700, X200 and X301 -->
        <match key="/org/freedesktop/Hal/devices/computer:system.hardware.version" contains_outof="R400;R500;T400;T500;W500;W700;X200;X301">
          <match key="storage.bus" string_outof="pci;scsi">
            <match key="info.parent" string="/org/freedesktop/Hal/devices/pci_8086_2929_scsi_host_0_scsi_device_lun0">
              <merge key="storage.hotpluggable" type="bool">true</merge>
            </match>
          </match>
        </match>

        <!-- Lenovo ThinkPad T410, T510 -->
        <match key="/org/freedesktop/Hal/devices/computer:system.hardware.version" contains_outof="T410;T510">
          <match key="storage.bus" string_outof="pci;scsi">
            <match key="info.parent" string="/org/freedesktop/Hal/devices/pci_8086_3b2f_scsi_host_0_scsi_device_lun0">
              <merge key="storage.hotpluggable" type="bool">true</merge>
            </match>
          </match>
        </match>


      </match>
    </match>
  </device>
</deviceinfo>

Then ensure it has the right permissions

chown root:root /etc/hal/fdi/information/10-thinkpad-ultrabay.fdi
chmod 644 /etc/hal/fdi/information/10-thinkpad-ultrabay.fdi


To check the effect, look at your lshal output, the section for your optical disk (/dev/sr0 typically) or your 2nd hdd (/dev/sdb typically) should have storage.hotpluggable = true after restarting your hal deamon or rebooting.

Disable ultrabay at boot time

To safe some energy you can create an init.d start script to automatically "eject" the ultrabay at boot time
vim /etc/init.d/stop-ultrabay

#! /bin/sh 
### BEGIN INIT INFO
# Provides:          stop-ultrabay
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: stops the ultrabay
### END INIT INFO

# Script to shutdown ultrabay at boot time. To reenable, simply push the ultrabay eject lever
# and put it again.
# By Frank Doelitzscher, 2010-08-04
#

# This things will always run
/usr/local/sbin/ultrabay_eject

# Carry out specific functions when asked to by the system
case "$1" in
*)
   echo "This script runs automatically at boot time."
   echo "It does not need any switches like start|stop"
   exit 1
   ;;
esac 
exit 0

Then ensure it has the right permissions

chmod 755 /etc/init.d/stop-ultrabay

And make it known to the init subsystem

update-rc.d -f stop-ultrabay start 90 2 3 4 5 .

It just gets a start script since it does not make sense to execute it at a shutdown or restart.

This was tested on a T60 running Debian Squeeze with standard 2.6.32-5-686 #1 SMP without any issues.

When using the ide-disk driver

The following applies if you use the ide-disk driver for the UltraBay device.
Note that essentially, you shouldn't be using this driver, and use ata_piix instead unless you're running an older kernel where ata_piix won't work for you

Hotswapping is supposed to be supported as well, using either hdparm/Debian hotswap or lt_hotswap to (un)register IDE devices. The latter is the recommended method with kernels from 2.6, since it will leave DMA working. However, for recent models (R52, T43, X41, Z60 and later) no method is known to work while maintaining DMA support; see Problems with SATA and Linux.

Only IDE devices (HDD's, optical drives, zip drives) require special treatment - batteries, floppies and other devices can just be pulled from the bay, provided they are not mounted or in use at the time. However, you should still power them down first using the thinkpad-acpi eject function.

The thinkpad-acpi kernel module has an eject function (# echo eject > /proc/acpi/ibm/bay). This only manages the ACPI calls to power down the device and the bay. It does not actually unregister the device from the IDE driver. # cat /proc/acpi/ibm/bay shows "unoccupied" unless an IDE device is present, but the eject function still works and should still be used.

To unregister the device, you can either use the Debian hotswap package, or lt_hotswap. Note that lt_hotswap doesn't seem to even build on recent (at 2.6.22 or newer, although it probably broke before 2.6.22) and on newer kernels, you should not build/load the old IDE PIIX (CONFIG_BLK_DEV_PIIX) and use the natively hotswapping ata_piix (CONFIG_ATA_PIIX) as explained on the top of this page.

Debian hotswap also allows the drive to be swapped as a normal user by default, which is useful. You should use hotswap to unregister the device and then # echo eject > /proc/acpi/ibm/bay. However, if you use this method on a 2.6 kernel, you will lose DMA support for the reinserted drive. This is due to kernel issues. This method was reported to work on a ThinkPad T23 (kernels 2.6.8.1, 2.6.14.2 and 2.6.15-arch) and T42 (kernel 2.6.13), but fails on a ThinkPad T43 (kernel 2.6.14.3).

lt_hotswap is now the recommended method to un- and reregister the IDE device. It installs as a kernel module and has support for automatically unregistering (if loaded with the auto_eject=1 option) the device when the eject event is generated by thinkpad-acpi. It will leave DMA support intact. It has supported to work on a ThinkPad T22 and T40 and should work with many other models (but not recent models which require the ata_piix driver for disk DMA support). With the lt_hotswap kernel module loaded, run: # modprobe -r ide_cd && modprobe -r cdrom && echo "MSTR eject" > /proc/acpi/lths

HAL support

HAL support also can also be configured To do so, create the file /etc/hal/fdi/information/10-ultrabay.fdi as follows:

<?xml version="1.0" encoding="UTF-8"?> <!-- -*- SGML -*- -->

<deviceinfo version="0.2">
  <device>

    <!-- UltraBay Devices -->
    <match key="storage.bus" string="ide">
      <match key="storage.physical_device" string="/org/freedesktop/Hal/devices/pci_8086_24ca_ide_1_0">
        <merge key="storage.hotpluggable" type="bool">true</merge>
      </match>
    </match>

  </device>
</deviceinfo>

For more details see HAL section for ata driver.

Password protected HDD

ATTENTION!
Do not attempt to set a new HDD password from Linux, if you succeed you may find you can no longer unlock the drive or remove the password in any way

There are multiple issues with hotplugging a password protected HDD. In short, it does not work from within the operating system. You will need to reboot, or use the suspend/wakeup procedure below, and let the BIOS do the unlock.

hdparm

While in theory you should be a able to use hdparm to unlock a drive, or set or remove a password. In practice none of it works the way it should.

First of all, by default the BIOS during POST will freeze the security settings of any drives that are present, preventing you from making changes until the drive is power cycled. You can see this by running the following command

# hdparm -I /dev/sdb|grep frozen
	not	frozen

If the above command simply returns 'frozen' any hdparm security commands will fail with an IO error. If this is an Ultrabay HDD you can simply remove the drive and hotplug it again to reset the drive.

If your drive has a PW set by the BIOS, any attempt to unlock it will result in an IO error.

But a fatal flaw is to use hdparm to actually set a password, if you do you will find the following issues

  • setting a Master password will appear to succeed, but in fact does nothing
  • setting a User password does succeed, but you will no longer be able to unlock or remove the password using either hdparm or the BIOS. hdparm will just give IO errors, and the BIOS will report the password being wrong. It is speculated that the BIOS does some kind of hashing to the password, which is why setting the PW from the BIOS and setting the PW with hdparm is incompatible.
# hdparm --verbose --user-master u --security-disable test /dev/sdb
security_password="test"

/dev/sdb:
 Issuing SECURITY_DISABLE command, password="test", user=user
oflags.lob_all=0x82, flags={ feat command }
oflags.hob_all=0x82, flags={ feat }
using LBA48 taskfile
outgoing cdb:  85 0b 26 00 00 00 00 00 00 00 00 00 00 40 f2 00
data:  00 00 74 65 73 74 00 00 00 00 00 00 00 00 00 00
SG_IO: ATA_16 status=0x2, host_status=0x0, driver_status=0x8
SG_IO: sb[]:  72 0b 00 00 00 00 00 0e 09 0c 01 04 00 00 00 00 00 00 00 00 e0 51 00 00 00 00 00 00 00 00 00 00
SG_IO: desc[]:  09 0c 01 04 00 00 00 00 00 00 00 00
      ATA_16 stat=51 err=04 nsect=00 lbal=00 lbam=00 lbah=00 dev=e0
I/O error, ata_op=0xf2 ata_status=0x51 ata_error=0x04
SECURITY_UNLOCK: Input/output error

Letting the BIOS unlock the disk by suspending the computer

A password protected disk can be unlocked by ensuring the laptop has been suspended before inserting the disk into the Ultrabay. The device being removed should still be ejected before suspending the laptop.

Upon waking up (resuming) the computer the BIOS should prompt for the password, and provided you enter it correctly, will unlock the disk. Once Linux regains control it may be necessary to re-scan the SCSI bus to find the disk and make it available, using the command "sudo rescan-scsi-bus.sh".

This is confirmed to work on a T61 running Ubuntu 8.04, (kernel 2.6.24) with the scsitools package installed.

libata EH going crazy

hotplugging a password protected HDD causes the libata error handler to go crazy

ACPI: \_SB_.PCI0.IDE0.SCND.MSTR - docking
ata2: exception Emask 0x10 SAct 0x0 SErr 0x0 action 0xe frozen
ata2: ACPI event
ata2: soft resetting link
ata2.00: ATA-6: HTS726060M9AT00, MH4OA6DA, max UDMA/100
ata2.00: 117210240 sectors, multi 0: LBA 
ata2.00: limited to UDMA/33 due to 40-wire cable
ata2.00: configured for UDMA/33
ata2: EH complete
scsi 1:0:0:0: Direct-Access     ATA      HTS726060M9AT00  MH4O PQ: 0 ANSI: 5
sd 1:0:0:0: Attached scsi generic sg1 type 0
sd 1:0:0:0: [sdb] 117210240 512-byte logical blocks: (60.0 GB/55.8 GiB)
sd 1:0:0:0: [sdb] Write Protect is off
sd 1:0:0:0: [sdb] Write cache: enabled, read cache: enabled, doesn't support DPO or FUA
sdb:

After which there are hundreds of errors such as these...

ata2.00: exception Emask 0x0 SAct 0x0 SErr 0x0 action 0x0
ata2.00: BMDMA stat 0x5
ata2.00: failed command: READ DMA
ata2.00: cmd c8/00:08:00:00:00/00:00:00:00:00/e0 tag 0 dma 4096 in
        res 51/04:08:00:00:00/00:00:00:00:00/e0 Emask 0x1 (device error)
ata2.00: status: { DRDY ERR }
ata2.00: error: { ABRT }
ata2.00: configured for UDMA/33
ata2: EH complete

Basically what is happening is that libata is ignoring the ATA IDENTIFY data, that reports that the drive is locked, and just tries to scan for every partition type possible.

udisks

lastly, there should be some userland support to detect the password protected HDD and prompt you for a password or retrieve the password from gnome-keyring. But that will require hdparm to be able to unlock the HDD first, and to figure out if the BIOS is doing some kind of hashing and how.