chiark / gitweb /
[PATCH] update udev scsi_id to scsi_id 0.5
[elogind.git] / extras / scsi_id / gen_scsi_id_udev_rules.sh
1 #! /bin/sh
2
3 # This script generates and sends to stdout a set of udev.rules for use
4 # with all scsi block devices on your system. It creates a udev key NAME
5 # with prefix defaulting to "disk-", and appends the current kernel name
6 # and the udev kernel number (the partition number, empty for the entire
7 # disk).
8 #
9 # Managing these is probably better done via a gui interface.
10 #
11 # You can edit and append the output to your /etc/udev/udev.rules file.
12 # You probably want to to change names to be non-kernel defaults, so as to
13 # avoid confusion if a configuration change modifies /sys/block/sd*
14 # naming.
15 #
16 # /etc/scsi_id.config must be properly configured. If you are using this
17 # script, you probably want a single line enabling scsi_id for all
18 # devices as follows:
19 #
20 # options=-g
21 #
22 # The above assumes you will not attach block devices that do not
23 # properly support the page codes used by scsi_id, this is especially true
24 # of many USB mass storage devices (mainly flash card readers).
25 #
26
27 prefix=disk-
28 scsi_id=/sbin/scsi_id
29
30 sysfs_dir=$(mount | awk '$5 == "sysfs" {print $3}')
31 if [ "$sysfs_dir" = "" ]
32 then
33         sysfs_dir="/sys"
34         echo "Using sysfs mount point \"$sysfs_dir\"" >&2
35 fi
36
37 c=$(ls /${sysfs_dir}/block/sd* 2>/dev/null | wc -l)
38 if [ $c = 0 ]
39 then
40         echo $0 no block devices present >&2
41         exit 1
42 fi
43
44 echo "#"
45 echo "# Start of autogenerated scsi_id rules. Edit the NAME portions of these"
46 echo "# rules to your liking."
47 echo "#"
48 first_line=yes
49
50 #
51 cd ${sysfs_dir}/block
52 for name in sd*
53 do
54         id=$($scsi_id -s /block/$name)
55         if [ $? != 0 ]
56         then
57                 echo $0 failed for device $name exiting >&2
58                 exit 1
59         fi
60         if [ $first_line = "yes" ]
61         then
62                 first_line=no
63                 echo "BUS=\"scsi\", PROGRAM=\"${scsi_id}\", RESULT=\"${id}\", NAME=\"${prefix}${name}%n\""
64                 echo
65                 echo "# Further RESULT keys use the result of the last PROGRAM rule."
66                 echo "# Be careful not to add any rules containing PROGRAM key between here"
67                 echo "# and the end of this section"
68                 echo
69         else
70                 # No PROGRAM, so just use the last result of PROGRAM. The
71                 # following is the same as the above without the PROGRAM
72                 # key.
73                 echo "BUS=\"scsi\", RESULT=\"${id}\", NAME=\"${prefix}${name}%n\""
74         fi
75
76 done
77
78 echo "#"
79 echo "# End of autogenerated scsi_id rules"
80 echo "#"