开发者

How to be informed when a HDD is detected in Linux

开发者 https://www.devze.com 2023-04-03 10:43 出处:网络
We have an embedded Linux build that starts with a HDD in sleep mode (hardware). The software then starts and enables power to the drive. Linux detects the drive fine:

We have an embedded Linux build that starts with a HDD in sleep mode (hardware). The software then starts and enables power to the drive. Linux detects the drive fine:

ata1: exception Emask 0x10 SAct 0x0 SErr 0x4050000 action 0x42 frozen
ata1: soft resetting port
ata1: SATA link up 1.5 Gbps (SStatus 113 SControl F0000)
ata1.00: ATA-8, max UDMA/133, 976773168 sectors: LBA48 NCQ (depth 0/32)
ata1.00: ata1: dev 0 multi count 0
ata1.00: configured for UDMA/133
ata1: EH complete
  Vendor: ATA       Model: Hitachi HCS5C105  Rev: JC2O
  Type:   Direct-Access                      ANSI SCSI revision: 05
SCSI device sda: 976773168 512-byte hdwr sectors (500108 MB)
sda: Write开发者_运维知识库 Protect is off
SCSI device sda: drive cache: write back
SCSI device sda: 976773168 512-byte hdwr sectors (500108 MB)
sda: Write Protect is off
SCSI device sda: drive cache: write back
 sda: sda1 sda2
sd 0:0:0:0: Attached scsi disk sda
sd 0:0:0:0: Attached scsi generic sg0 type 0

Unfortunately the software attempts to access the drive before the above SCSI detect happens. We could add a sleep but this is not a reliable mechanism as we have noticed that the time taken to detect is non-deterministic.

Is there any way to get the kernel/hotplug/etc. system to report when the HDD is detected?

Thanks.


I think the answer is somewhere in the udev device manager (wikipedia page). It is fully usable in an embedded environement.

It allows you to write rules that are able to launch scripts/programs when some devices are detected.

Something like this could do the stuff :

KERNEL=="sda", RUN+="/usr/bin/my_program"

If you have an old kernel, you may have the hotplug system activated.

The last chance wouls be the inotify tool that let you monitor anything in your file system (even /dev directory :)).


You have a set of options to do this, from the simplest to the most complicated:

  • Manual polling. Simply write a shell script that polls until the hard disk becomes available before actually using it. Granted, this is not the nicest solution, but it works and is simple.
  • mdev. If your embedded Linux system is Busybox based (which I hope it is!), then Busybox integrates the mdev program. Its usage is very simple and is documented at http://git.buildroot.net/busybox/tree/docs/mdev.txt. mdev will very easily allow you to run a shell script when a device gets detected. If you already have Busybox in your embedded Linux system, this is definitely the solution I would recommend.
  • udev, as proposed by others. This is the solution used in full blown desktop/server systems. It has more dependencies than mdev and is a bit harder to setup. If you only have your hard disk detection problem to solve, I'd say that using udev is a bit overkill, but if you intend to use it for other purposes, why not.
  • udev + udisks + dbus, this is actually what is used on full blown systems. udisks is a daemon that uses udev to be notified of new storage devices appearing in the system, it gets some additional informations about them and then send a message over D-Bus. It allows provides D-Bus services to manipulate the storage devices.

I hope this gives you an overview of the possible solutions.


You can listen to the uevent netlink socket - you will get the same events as udevd

see http://lwn.net/Articles/242046/

0

精彩评论

暂无评论...
验证码 换一张
取 消