How to hotswap Ultrabay devices

From ThinkWiki
Revision as of 12:11, 23 June 2009 by Tonko (Talk | contribs) (Password protected HDD)
Jump to: navigation, search

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) where fully IDE (PATA) for both the internal HDD and Ultrabay. For this 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 through a separate SATA-PATA bridge chip the actual disk is still legacy IDE. 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 ide-disk.

ThinkPads like the R60, T60 and Z60 series are also hybrids, but they use a native SATA HDD internal. Just like with the previous models though 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 X300 are all fully SATA for both the internal HDD and the Ultrabay device, so both are handled by the same SATA driver.

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
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 -
X200, X200 Tablet, X300, 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 -
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.

When using the ata_piix driver

The following applies when using the ata_piix driver, which is really the driver you should be using if your Ultrabay is legacy IDE (PATA).

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


shopt -s nullglob
logger ultrabay_eject storage device $DEVPATH
export DISPLAY=:0.0 # required for notify-send

# 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) {
                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("notify-send -u normal -t 5000 \"Unmounted $dir\"");
        exit 1 if $bad;

# Get the UltraBay's /dev/foo block device node
ultrabay_dev_node() {
        UDEV_PATH="`readlink -e "$ULTRABAY_SYSDIR/block/"*`" || return 1
        UDEV_NAME="`udevadm info --query=name --path=$UDEV_PATH`" || return 1
        echo /dev/$UDEV_NAME

if [ -d $ULTRABAY_SYSDIR ]; then
        # 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;
        # 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:
        echo 1 > $ULTRABAY_SYSDIR/delete
        logger ultrabay_eject no ultrabay device directory
        echo 2 > /proc/acpi/ibm/beep  # triple error tone
        exit 1

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

# Turn off power to the UltraBay
dock=$( /bin/grep ata_bay /sys/devices/platform/dock.?/type )
dock=${dock%%/type:ata_bay} # needed for 2.6.27 and later
if [ -n "$dock" -a -d "$dock" ]; then
        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"

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

Only needed for kernels prior to 2.6.26

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

echo 12 > /proc/acpi/ibm/beep
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

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
# 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.

Model-specific notes

See the table above for a list of ata_piix supported laptops and the bus ID to use.

HAL support

Help needed
If the script below does not work, or you have a A or W-series ThinkPad, please provide your lshal output on the Talk:How_to_hotswap_Ultrabay_devices page
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">
    <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 T20, T21, T22, X20 and X21 -->
        <match key="/org/freedesktop/Hal/devices/computer:system.hardware.version" contains_outof="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>

        <!-- 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>

        <!-- 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>

        <!-- 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 key="/org/freedesktop/Hal/devices/computer:system.hardware.vendor" string="LENOVO">

        <!-- will match 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>

        <!-- will 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>

        <!-- will 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>

        <!-- will 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>


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.

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 ibm-acpi eject function.

The ibm-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, and 2.6.15-arch) and T42 (kernel 2.6.13), but fails on a ThinkPad T43 (kernel

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 ibm-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">

    <!-- 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>


For more details see HAL section for ata driver.

Password protected HDD

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:

hdparm --security-unlock PASSWD /dev/sdb

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"

As such the only available option is to reboot and let the BIOS unlock the HDD.