1
0

write_cd_aliases 2.8 KB

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