Aug 19, 2008

SCSI and SATA hotplug in Linux

Well, this is my first post of my first blog, I didn't really knew where to start from, so let's go on with some classic but useful stuff.
A lot of times I had to add a hard drive to a running linux server (or my personal box) without shutting it down; here is how i dit it on a lot of different versions of linux (starting with early 2.4 until today's testing 2.6.24). Check first if your hardware supports hotplug (SCSI proprietary drivers do generally support that and generic SATA linux driver AHCI supports also hotplug). For info about SATA and linux drivers, check out http://linuxmafia.com/faq/Hardware/sata.html.

First, plug your SCSI or SATA hard drive in the machine. You shouldn't see anything in your "dmesg".
Then, have a look at your /proc/scsi/scsi (use cat command, no text editor). It should look like :

Attached devices:
Host: scsi0 Channel: 00 Id: 00 Lun: 00
Vendor: SEAGATE Model: ST336607LC Rev: DS09
Type: Direct-Access ANSI SCSI revision: 03
Host: scsi0 Channel: 00 Id: 01 Lun: 00
Vendor: SEAGATE Model: ST3146807LC Rev: DS09
Type: Direct-Access ANSI SCSI revision: 03
Host: scsi0 Channel: 00 Id: 02 Lun: 00
Vendor: MAXTOR Model: ATLAS15K2_146SCA Rev: JNZ6
Type: Direct-Access ANSI SCSI revision: 03
Host: scsi0 Channel: 00 Id: 03 Lun: 00
Vendor: FUJITSU Model: MAX3147NC Rev: 5D03
Type: Direct-Access ANSI SCSI revision: 03
Host: scsi0 Channel: 00 Id: 04 Lun: 00
Vendor: MAXTOR Model: ATLAS15K2_146SCA Rev: JNZ6
Type: Direct-Access ANSI SCSI revision: 03
Host: scsi0 Channel: 00 Id: 06 Lun: 00
Vendor: SDR Model: GEM318P Rev: 1
Type: Processor ANSI SCSI revision: 02


This is obviously a list of all your up and running hdds (in this case a DELL PE1900 with SCSI backplane controller and 5 disks inside), including here the SCSI controller in 0060 position. As you may guess, what interests us is knowing the next free id for inserting our brand new disk.
Here the next and last free id is 5 on scsi0, channel0, lun0 (depending on your configuration, you may have several scsi controllers or several channels so be careful). In case you add several SCSI disks on a controller without rebooting the machine, the SCSI ids should match the physical position on the controller (else on reboot the kernel will remap the devices) or you should mount devices by UUID.
For SATA disks, don't use next free id, but next scsi. This is how /proc/scsi/scsi looks on my NForce4 with 4 SATA disks :

Attached devices:
Host: scsi0 Channel: 00 Id: 00 Lun: 00
Vendor: ATA Model: ST380211AS Rev: 3.AA
Type: Direct-Access ANSI SCSI revision: 05
Host: scsi1 Channel: 00 Id: 00 Lun: 00
Vendor: ATA Model: ST3808110AS Rev: 3.AA
Type: Direct-Access ANSI SCSI revision: 05
Host: scsi2 Channel: 00 Id: 00 Lun: 00
Vendor: ATA Model: WDC WD1600JS-00M Rev: 02.0
Type: Direct-Access ANSI SCSI revision: 05
Host: scsi3 Channel: 00 Id: 00 Lun: 00
Vendor: ATA Model: Hitachi HDT72503 Rev: V54O
Type: Direct-Access ANSI SCSI revision: 05

So now that we know what is the next free position for our device, we'll insert it into the system. For doing that, use following command, customized to your needs (this one is for the PE1900, the NForce4 runs only 4 SATAs) :
echo "scsi add-single-device 0 0 5 0" > /proc/scsi/scsiThis should hang out your system for a few seconds, and then your kernel should let you know that a drive has been added.
Further, you'll have to create new partitions. I use command line fdisk command (but QTParted, Gparted or parted work as well) :
fdisk /dev/sde, create new primary, etc.Then create the filesystems of your choice on the disk and mount them into your system.

P.S : The same command is used for removing hdds from the system :
echo "scsi remove-single-device 0 0 5 0" > /proc/scsi/scsiDo I have to mention that partitions on this hard have to be unmounted before doing that ?

Have fun !

1 comment:

Unknown said...

Well the tutorial make it so easy that i think everyone can easily do it. The post style is awesome, keep it up.
recovering deleted emails