write_cd_aliases 2.6 KB

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