summaryrefslogtreecommitdiff
path: root/extras/scsi_id/gen_scsi_id_udev_rules.sh
blob: 874e94e8bba939331c776d6eee57a88f7b279522 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#! /bin/sh

# This script generates and sends to stdout a set of udev.rules for use
# with all scsi block devices on your system. It creates a udev key NAME
# with prefix defaulting to "disk-", and appends the current kernel name
# and the udev kernel number (the partition number, empty for the entire
# disk).
#
# Managing these is probably better done via a gui interface.
#
# You can edit and append the output to your /etc/udev/udev.rules file.
# You probably want to to change names to be non-kernel defaults, so as to
# avoid confusion if a configuration change modifies /sys/block/sd*
# naming.
#
# /etc/scsi_id.config must be properly configured. If you are using this
# script, you probably want a single line enabling scsi_id for all
# devices as follows:
#
# options=-g
#
# The above assumes you will not attach block devices that do not
# properly support the page codes used by scsi_id, this is especially true
# of many USB mass storage devices (mainly flash card readers).
#

prefix=disk-
scsi_id=/sbin/scsi_id

sysfs_dir=$(mount | awk '$5 == "sysfs" {print $3}')
if [ "$sysfs_dir" = "" ]
then
	sysfs_dir="/sys"
	echo "Using sysfs mount point \"$sysfs_dir\"" >&2
fi

c=$(ls /${sysfs_dir}/block/sd* 2>/dev/null | wc -l)
if [ $c = 0 ]
then
	echo $0 no block devices present >&2
	exit 1
fi

echo "#"
echo "# Start of autogenerated scsi_id rules. Edit the NAME portions of these"
echo "# rules to your liking."
echo "#"
first_line=yes

#
cd ${sysfs_dir}/block
for name in sd*
do
	id=$($scsi_id -s /block/$name)
	if [ $? != 0 ]
	then
		echo $0 failed for device $name exiting >&2
		exit 1
	fi
	if [ $first_line = "yes" ]
	then
		first_line=no
		echo "BUS=\"scsi\", PROGRAM=\"${scsi_id}\", RESULT=\"${id}\", NAME=\"${prefix}${name}%n\""
		echo
		echo "# Further RESULT keys use the result of the last PROGRAM rule."
		echo "# Be careful not to add any rules containing PROGRAM key between here"
		echo "# and the end of this section"
		echo
	else
		# No PROGRAM, so just use the last result of PROGRAM. The
		# following is the same as the above without the PROGRAM
		# key.
		echo "BUS=\"scsi\", RESULT=\"${id}\", NAME=\"${prefix}${name}%n\""
	fi

done

echo "#"
echo "# End of autogenerated scsi_id rules"
echo "#"