1 | #!/bin/sh -e
|
---|
2 |
|
---|
3 | # Updated for LFS by B. Dubbs, 2012-09-23
|
---|
4 | # Fix RULES_LOCK location
|
---|
5 |
|
---|
6 |
|
---|
7 | RULES_FILE="/etc/udev/rules.d/82-persistent-cd.rules"
|
---|
8 |
|
---|
9 | ##############################################################################
|
---|
10 | lock_rules_file() {
|
---|
11 | RULES_LOCK="/run/udev/lock-${RULES_FILE##*/}"
|
---|
12 |
|
---|
13 | if [ ! -d /run/udev/ ]; then
|
---|
14 | echo "write_cd_aliases: /run/udev does not exist!" >&2
|
---|
15 | exit 2
|
---|
16 | fi
|
---|
17 |
|
---|
18 | retry=10
|
---|
19 | while ! mkdir $RULES_LOCK 2> /dev/null; do
|
---|
20 | if [ $retry -eq 0 ]; then
|
---|
21 | echo "Cannot lock $RULES_FILE!" >&2
|
---|
22 | exit 2
|
---|
23 | fi
|
---|
24 | sleep 1
|
---|
25 | retry=$(($retry - 1))
|
---|
26 | done
|
---|
27 | }
|
---|
28 |
|
---|
29 | unlock_rules_file() {
|
---|
30 | rmdir $RULES_LOCK || true
|
---|
31 | }
|
---|
32 |
|
---|
33 | ##############################################################################
|
---|
34 | find_next_available() {
|
---|
35 | # use echo to convert newlines to spaces
|
---|
36 | local links=`echo $(find_all_symlinks $1)`
|
---|
37 | local basename=${links%%[ 0-9]*}
|
---|
38 | local max=-1
|
---|
39 | for name in $links; do
|
---|
40 | local num=${name#$basename}
|
---|
41 | [ "$num" ] || num=0
|
---|
42 | [ $num -gt $max ] && max=$num
|
---|
43 | done
|
---|
44 |
|
---|
45 | max=$(($max + 1))
|
---|
46 | # "name0" actually is just "name"
|
---|
47 | [ $max -eq 0 ] && return
|
---|
48 | echo "$max"
|
---|
49 | }
|
---|
50 |
|
---|
51 | find_all_symlinks() {
|
---|
52 | local linkre="$1"
|
---|
53 | local match="$2"
|
---|
54 |
|
---|
55 | [ -e $RULES_FILE ] || return
|
---|
56 |
|
---|
57 | local search='.*[[:space:],]SYMLINK+="\('"$linkre"'\)"[[:space:]]*\(,.*\|\\\|\)$'
|
---|
58 |
|
---|
59 | sed -n -e "${match}s/${search}/\1/p" $RULES_FILE
|
---|
60 | }
|
---|
61 |
|
---|
62 | write_rule() {
|
---|
63 | local match="$1"
|
---|
64 | local link="$2"
|
---|
65 | local comment="$3"
|
---|
66 |
|
---|
67 | [ -e "$RULES_FILE" ] || PRINT_HEADER=1
|
---|
68 | {
|
---|
69 | if [ "$PRINT_HEADER" ]; then
|
---|
70 | PRINT_HEADER=
|
---|
71 | echo "# This file was automatically generated by the $0"
|
---|
72 | echo "# program, probably run by the 83-cdrom.rules rules file."
|
---|
73 | echo "#"
|
---|
74 | echo "# You can modify it, as long as you keep each rule on a single line"
|
---|
75 | echo "# and set the \$GENERATED variable."
|
---|
76 | echo ""
|
---|
77 | fi
|
---|
78 |
|
---|
79 | [ "$comment" ] && echo "# $comment"
|
---|
80 | echo "ACTION==\"add\", SUBSYSTEM==\"block\", $match, ENV{ID_CDROM}==\"1\", SYMLINK+=\"$link\", ENV{GENERATED}=\"1\""
|
---|
81 | } >> $RULES_FILE
|
---|
82 | SYMLINKS="$SYMLINKS $link"
|
---|
83 | }
|
---|
84 |
|
---|
85 | ##############################################################################
|
---|
86 | if [ -z "$DEVPATH" ]; then
|
---|
87 | echo "Missing \$DEVPATH." >&2
|
---|
88 | exit 1
|
---|
89 | fi
|
---|
90 | if [ -z "$ID_CDROM" ]; then
|
---|
91 | echo "$DEVPATH is not a CD reader." >&2
|
---|
92 | exit 1
|
---|
93 | fi
|
---|
94 |
|
---|
95 | # Prevent parallel processes from modifying the file at the same time.
|
---|
96 | lock_rules_file
|
---|
97 |
|
---|
98 | link_num=$(find_next_available 'cdrom[0-9]*')
|
---|
99 |
|
---|
100 | #match="ENV{ID_PATH}==\"$ID_PATH\""
|
---|
101 |
|
---|
102 | #kernel=${DEVPATH##*/}
|
---|
103 | #match="KERNEL==\"$kernel\""
|
---|
104 |
|
---|
105 | id=${PHYSDEVPATH##*/}
|
---|
106 | match="BUS==\"$PHYSDEVBUS\", ID==\"$id\""
|
---|
107 |
|
---|
108 | comment="$ID_MODEL ($ID_PATH)"
|
---|
109 |
|
---|
110 | write_rule "$match" "cdrom$link_num" "$comment"
|
---|
111 | [ "$ID_CDROM_CD_RW" ] && write_rule "$match" "cdrw$link_num"
|
---|
112 | [ "$ID_CDROM_DVD" ] && write_rule "$match" "dvd$link_num"
|
---|
113 | [ "$ID_CDROM_DVD_RW" ] && write_rule "$match" "dvdrw$link_num"
|
---|
114 |
|
---|
115 | unlock_rules_file
|
---|
116 |
|
---|
117 | echo $SYMLINKS
|
---|
118 |
|
---|
119 | exit 0
|
---|
120 |
|
---|