chiark / gitweb /
Merge branch 'master' of git+ssh://master.kernel.org/pub/scm/linux/hotplug/udev
[elogind.git] / extras / rule_generator / write_cd_rules
1 #!/bin/sh -e
2
3 # This script is run if an optical drive lacks a rule for persistent naming.
4 #
5 # It adds symlinks for optical drives based on the device class determined
6 # by cdrom_id and used ID_PATH to identify the device.
7 #
8 # (C) 2006 Marco d'Itri <md@Linux.IT>
9 #
10 # This program is free software; you can redistribute it and/or modify it
11 # under the terms of the GNU General Public License as published by the
12 # Free Software Foundation version 2 of the License.
13
14 # debug, if UDEV_LOG=<debug>
15 if [ -n "$UDEV_LOG" ]; then
16         if [ "$UDEV_LOG" -ge 7 ]; then
17                 set -x
18         fi
19 fi
20
21 RULES_FILE="/etc/udev/rules.d/70-persistent-cd.rules"
22
23 . /lib/udev/rule_generator.functions
24
25 find_next_available() {
26         raw_find_next_available "$(find_all_rules 'SYMLINK\+=' "$1")"
27 }
28
29 write_rule() {
30         local match="$1"
31         local link="$2"
32         local comment="$3"
33
34         {
35         if [ "$PRINT_HEADER" ]; then
36                 PRINT_HEADER=
37                 echo "# This file was automatically generated by the $0"
38                 echo "# program, run by the cd-aliases-generator.rules rules file."
39                 echo "#"
40                 echo "# You can modify it, as long as you keep each rule on a single"
41                 echo "# line, and set the \$GENERATED variable."
42                 echo ""
43         fi
44
45         [ "$comment" ] && echo "# $comment"
46         echo "$match, SYMLINK+=\"$link\", ENV{GENERATED}=\"1\""
47         } >> $RULES_FILE
48         SYMLINKS="$SYMLINKS $link"
49 }
50
51 if [ -z "$DEVPATH" ]; then
52         echo "Missing \$DEVPATH." >&2
53         exit 1
54 fi
55 if [ -z "$ID_CDROM" ]; then
56         echo "$DEVPATH is not a CD reader." >&2
57         exit 1
58 fi
59
60 if [ "$1" ]; then
61         METHOD="$1"
62 else
63         METHOD='by-path'
64 fi
65
66 case "$METHOD" in
67         by-path)
68         if [ -z "$ID_PATH" ]; then
69                 echo "$DEVPATH not supported by path_id. by-id may work." >&2
70                 exit 1
71         fi
72         RULE="ENV{ID_PATH}==\"$ID_PATH\""
73         ;;
74
75         by-id)
76         if [ "$ID_SERIAL" ]; then
77                 RULE="ENV{ID_SERIAL}==\"$ID_SERIAL\""
78         elif [ "$ID_MODEL" -a "$ID_REVISION" ]; then
79                 RULE="ENV{ID_MODEL}==\"$ID_MODEL\", ENV{ID_REVISION}==\"$ID_REVISION\""
80         else
81                 echo "$DEVPATH not supported by ata_id. by-path may work." >&2
82                 exit 1
83         fi
84         ;;
85
86         *)
87         echo "Invalid argument (must be either by-path or by-id)." >&2
88         exit 1
89         ;;
90 esac
91
92 # Prevent concurrent processes from modifying the file at the same time.
93 lock_rules_file
94
95 # Check if the rules file is writeable.
96 choose_rules_file
97
98 link_num=$(find_next_available 'cdrom[0-9]*')
99
100 match="SUBSYSTEM==\"block\", ENV{ID_CDROM}==\"?*\", $RULE"
101
102 comment="$ID_MODEL ($ID_PATH)"
103
104         write_rule "$match" "cdrom$link_num" "$comment"
105 [ "$ID_CDROM_CD_R" -o "$ID_CDROM_CD_RW" ] && \
106         write_rule "$match" "cdrw$link_num"
107 [ "$ID_CDROM_DVD" ] && \
108         write_rule "$match" "dvd$link_num"
109 [ "$ID_CDROM_DVD_R" -o "$ID_CDROM_DVD_RW" -o "$ID_CDROM_DVD_RAM" ] && \
110         write_rule "$match" "dvdrw$link_num"
111 echo >> $RULES_FILE
112
113 unlock_rules_file
114
115 echo $SYMLINKS
116
117 exit 0
118