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