LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Hot swapping cd-roms w/ hdparm -R/-U (https://www.linuxquestions.org/questions/linux-general-1/hot-swapping-cd-roms-w-hdparm-r-u-370344/)

infinity42 10-06-2005 11:22 AM

Hot swapping cd-roms w/ hdparm -R/-U
 
I was feeling bored and vaguely insane today, so I thought I would try hotswapping my cdrom drives as a challenge really.

I have two ide channels:
ide0: hda (main harddrive)
ide1: hdc && hdd (cdroms)

First I did:
Code:

#hdparm -Y /dev/hdc /dev/hdd
To put them to sleep. Took ages, but seemed to work fine.
Then I ran:
Code:

# hdparm -U 1 /dev/hdc
Took a long time, and the output went something like this:
Code:

/dev/hda:
 attempting to unregister hwif#1

That was all, no error messages. Well ide1 was still in /proc/ide, but i pulled the power on the drives, then the ide cables. Plugged the ide cable into two other cdroms (I have 5 in my box.. don't ask), then the power. They spun up and did the whirry thing. The ide light on the case came on, and has been on solid since. Well ide1 was still in proc, so i had a look at /proc/ide/ide1/hdc/model and it still thought that the other drives where there. So I ran
Code:

# hdparm -R 0x170 0x376 15 /dev/hdc
At that point it seg faulted! Also ide1 disappeared from proc at this point, and /dev/hdc && /dev/hdd disappeared (I use udev). So I tried
Code:

# hdparm -R 0x170 0x376 15 /dev/hda
as the device doesn't matter really, it's just used for ioctl i believe. This spits out:
Code:

/dev/hda:
 attempting to scan hwif (0x170, 0x376, 15)
 HDIO_SCAN_HWIF failed: Input/output error

and this comes out in dmesg:
Code:

ide1: I/O resource 0x376-0x376 not free.
ide1: ports already in use, skipping probe

I tried running
Code:

# hdparm -U 1 /dev/hda
but it just seems to hang there.

Any ideas? (I'm happy to try most things, however insane) I will reboot soonish and try it again, see if i can get more logging out of the kernel to see what is going on.

(Yes I know this is unsupported, blah blah.. dangerous, blah blah.. )

infinity42 10-06-2005 01:03 PM

I've just worked it out! To unregister run
Code:

#hdparm -U 1 /dev/hda
i.e. a with a device not on the IDE bus you are trying to disconnect! This removes ide1 perfectly. Now swap the drives around and run
Code:

#hdparm -R 0x170 0x376 15 /dev/hda
(I think these numbers are always the same for ide1. You can find them in /var/log/dmesg; they look something like:
Code:

Probing IDE interface ide0...
ide0 at 0x1f0-0x1f7,0x3f6 on irq 14
Probing IDE interface ide1...
ide1 at 0x170-0x177,0x376 on irq 15

) Check in dmesg and you should see something like this:
Code:

ide1 at 0x170-0x177,0x376 on irq 15
hdc: ATAPI 32X CD-ROM drive, 128kB Cache
hdd: ATAPI 12X CD-ROM drive, 86kB Cache

although probably with more descriptive names (these cdroms are a couple of quite old ones)

I would not reccomend trying this with harddrives unless you are sure that the harddrive is not spinning and the heads are parked before you pull the power; I think hdparm -Y should do that, but I'm really not sure. Don't even think about trying to hot-swap your root drive ;).

Hope that helps someone, or at least someone finds it interesting.

infinity42 10-06-2005 05:00 PM

I have just written a (very simplistic) script to make this process easier. I'll share it here just in case anyone ever wants it:
Code:

#!/bin/zsh
DEVICE=/dev/hda
BUS=1
IO_ADR=0x170
if [ -z $1 ];then
        echo "Usage: $0 [off|on|rescan]"
        exit
fi
if [ "$1" = "on" ];then;do_r='y';fi
if [ "$1" = "off" ];then;do_u='y';fi
if [ "$1" = "rescan" ];then;do_u='y';do_r='y';fi
[ ! -z $do_u ] && hdparm -U $BUS $DEVICE
[ ! -z $do_r ] && hdparm -R $IO_ADR 0 0 $DEVICE

It is written for zsh, but it would be trivial to port it to bash. See above post for info about how it works.

I should just mention that this is considered dangerous to hardware etc. I would personally recommend you only use it on cdrom drives, and use common sense when messing around inside a powered on computer. I hold no responsibility blah blah if it wipes your harddrive/causes a nuclear disaster (Those bugs in the kernel where fixed last year :P)

Enjoy!

[Note: a mod may want to move this to the success stories forum if you think it's appropriate]


All times are GMT -5. The time now is 08:20 PM.