chiark / gitweb /
write_cd_rules: identity-based persistence
[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 RULES_FILE="/etc/udev/rules.d/70-persistent-cd.rules"
15
16 . /lib/udev/rule_generator.functions
17
18 find_next_available() {
19         raw_find_next_available "$(find_all_rules 'SYMLINK+=' $1)"
20 }
21
22 write_rule() {
23         local match="$1"
24         local link="$2"
25         local comment="$3"
26
27         {
28         if [ "$PRINT_HEADER" ]; then
29                 PRINT_HEADER=
30                 echo "# This file was automatically generated by the $0"
31                 echo "# program, probably run by the cd-aliases-generator.rules rules file."
32                 echo "#"
33                 echo "# You can modify it, as long as you keep each rule on a single line"
34                 echo "# and set the \$GENERATED variable."
35                 echo ""
36         fi
37
38         [ "$comment" ] && echo "# $comment"
39         echo "$match, SYMLINK+=\"$link\", ENV{GENERATED}=\"1\""
40         } >> $RULES_FILE
41         SYMLINKS="$SYMLINKS $link"
42 }
43
44 if [ -z "$DEVPATH" ]; then
45         echo "Missing \$DEVPATH." >&2
46         exit 1
47 fi
48 if [ -z "$ID_CDROM" ]; then
49         echo "$DEVPATH is not a CD reader." >&2
50         exit 1
51 fi
52
53 if [ "$1" ]; then
54         METHOD="$1"
55 else
56         METHOD='by-path'
57 fi
58
59 case "$METHOD" in
60         by-path)
61         if [ -z "$ID_PATH" ]; then
62                 echo "$DEVPATH not supported by path_id. by-id may work." >&2
63                 exit 1
64         fi
65         RULE="ENV{ID_PATH}==\"$ID_PATH\""
66         ;;
67
68         by-id)
69         if [ "$ID_SERIAL" ]; then
70                 RULE="ENV{ID_SERIAL}==\"$ID_SERIAL\""
71         elif [ "$ID_MODEL" -a "$ID_REVISION" ]; then
72                 RULE="ENV{ID_MODEL}==\"$ID_MODEL\", ENV{ID_REVISION}==\"$ID_REVISION\""
73         else
74                 echo "$DEVPATH not supported by ata_id. by-path may work." >&2
75                 exit 1
76         fi
77         ;;
78
79         *)
80         echo "Invalid argument (must be either by-path or by-id)." >&2
81         exit 1
82         ;;
83 esac
84
85 # Prevent concurrent processes from modifying the file at the same time.
86 lock_rules_file
87
88 # Check if the rules file is writeable.
89 choose_rules_file
90
91 link_num=$(find_next_available 'cdrom[0-9]*')
92
93 match="ENV{ID_CDROM}==\"?*\", $RULE"
94
95 comment="$ID_MODEL ($ID_PATH)"
96
97         write_rule "$match" "cdrom$link_num" "$comment"
98 [ "$ID_CDROM_CD_R" -o "$ID_CDROM_CD_RW" ] && \
99         write_rule "$match" "cdrw$link_num"
100 [ "$ID_CDROM_DVD" ] && \
101         write_rule "$match" "dvd$link_num"
102 [ "$ID_CDROM_DVD_R" -o "$ID_CDROM_DVD_RW" -o "$ID_CDROM_DVD_RAM" ] && \
103         write_rule "$match" "dvdrw$link_num"
104
105 unlock_rules_file
106
107 echo $SYMLINKS
108
109 exit 0
110