4

I have a set of hard drives which are rotated for offsite backup. There are several steps that need to be performed when one of the disks is reinserted which I've rolled into a simple bash script. I'd like to execute the script automatically when one of the disks is hot-swapped in.

Ideally I'd have a list of disk-id's (/dev/disk/by-id/...) for the disks which should trigger the script; when one of them is inserted the script would be ran and the ID of the specific disk would be passed in as a parameter. As an alternative it would be good enough to have a copy of the script per-disk and have each script ran for its respective disk.

I'm running Ubuntu Server 14.04 LTS, and have been skimming similar questions related to USB drives and am looking at udev and hotplug as the two systems that I'll likely need to use, but was hoping someone may be able to point me to a specific succinct solution for my needs.

masegaloeh
  • 17,978
  • 9
  • 56
  • 104
STW
  • 960
  • 1
  • 7
  • 24
  • Write an UDEV rule for that. – Petter H Dec 30 '14 at 20:00
  • 1
    And when writing that udev rule, have a look at the system udev rules how the entries in disk/by-id are created. At least on my Slackware system they are created using variables like `$env{ID_SCSI_COMPAT}` and `$env{ID_SERIAL}` – Henrik Carlqvist Dec 31 '14 at 11:05

1 Answers1

1

My usual approach to udev rule writing is to find something similar to what I need in /lib/udev and complement it with a google search or a quick look at the man pages.

For your case, most of the mojo can be found in /lib/udev/rules.d/60-persistent-storage.rules. I came up with this rule:

ACTION=="add", SUBSYSTEM=="block", ENV{ID_SERIAL}=="XXXXXXXX", PROGRAM="/opt/bin/myscript $env{ID_SERIAL}"

You would need one rule for each drive, and your script receives the drive serial as a parameter.

I'm only using the drive serial here, that should be enough but you can rebuild the whole ID as seen in /dev/disk/by-id with this string:

$env{ID_BUS}-$env{ID_SERIAL}

To check the drive's serial number you can use:

udevadm info -n /dev/disk/by-id/XXXXXXXX | grep -i SERIAL
STW
  • 960
  • 1
  • 7
  • 24
GnP
  • 955
  • 8
  • 15
  • Thanks, I think this'll be exactly what I need--I'll accept the answer after I get the chance to test it during the next rotation – STW Dec 31 '14 at 15:25
  • 1
    I strongly suggest that you test this (or any advice from random people on the internet) before you deploy it on production. [It's quite easy to test udev rules](https://wiki.archlinux.org/index.php/udev#Testing_rules_before_loading) – GnP Dec 31 '14 at 15:44
  • Thanks gnp, that link looks useful. I'd planned on having the rule run a basic 'hello world' bash script to verify that it would execute with IDs as expected – STW Dec 31 '14 at 15:48
  • 1
    This is working well; I had a problem running my rule too early--`ENV{ID_SERIAL}` wasn't matching. Renaming the file `99-local.rules` (running it last) solved that. – STW Mar 11 '15 at 16:30