1
0

write_cd_rules 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #!/bin/sh -e
  2. # This script is run if an optical drive lacks a rule for persistent naming.
  3. #
  4. # It adds symlinks for optical drives based on the device class determined
  5. # by cdrom_id and used ID_PATH to identify the device.
  6. # (C) 2006 Marco d'Itri <md@Linux.IT>
  7. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation, either version 2 of the License, or
  11. # (at your option) any later version.
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. # debug, if UDEV_LOG=<debug>
  19. if [ -n "$UDEV_LOG" ]; then
  20. if [ "$UDEV_LOG" -ge 7 ]; then
  21. set -x
  22. fi
  23. fi
  24. RULES_FILE="/etc/udev/rules.d/70-persistent-cd.rules"
  25. . /lib/udev/rule_generator.functions
  26. find_next_available() {
  27. raw_find_next_available "$(find_all_rules 'SYMLINK\+=' "$1")"
  28. }
  29. write_rule() {
  30. local match="$1"
  31. local link="$2"
  32. local comment="$3"
  33. {
  34. if [ "$PRINT_HEADER" ]; then
  35. PRINT_HEADER=
  36. echo "# This file was automatically generated by the $0"
  37. echo "# program, run by the cd-aliases-generator.rules rules file."
  38. echo "#"
  39. echo "# You can modify it, as long as you keep each rule on a single"
  40. echo "# line, and set the \$GENERATED variable."
  41. echo ""
  42. fi
  43. [ "$comment" ] && echo "# $comment"
  44. echo "$match, SYMLINK+=\"$link\", ENV{GENERATED}=\"1\""
  45. } >> $RULES_FILE
  46. SYMLINKS="$SYMLINKS $link"
  47. }
  48. if [ -z "$DEVPATH" ]; then
  49. echo "Missing \$DEVPATH." >&2
  50. exit 1
  51. fi
  52. if [ -z "$ID_CDROM" ]; then
  53. echo "$DEVPATH is not a CD reader." >&2
  54. exit 1
  55. fi
  56. if [ "$1" ]; then
  57. METHOD="$1"
  58. else
  59. METHOD='by-path'
  60. fi
  61. case "$METHOD" in
  62. by-path)
  63. if [ -z "$ID_PATH" ]; then
  64. echo "$DEVPATH not supported by path_id. by-id may work." >&2
  65. exit 1
  66. fi
  67. RULE="ENV{ID_PATH}==\"$ID_PATH\""
  68. ;;
  69. by-id)
  70. if [ "$ID_SERIAL" ]; then
  71. RULE="ENV{ID_SERIAL}==\"$ID_SERIAL\""
  72. elif [ "$ID_MODEL" -a "$ID_REVISION" ]; then
  73. RULE="ENV{ID_MODEL}==\"$ID_MODEL\", ENV{ID_REVISION}==\"$ID_REVISION\""
  74. else
  75. echo "$DEVPATH not supported by ata_id. by-path may work." >&2
  76. exit 1
  77. fi
  78. ;;
  79. *)
  80. echo "Invalid argument (must be either by-path or by-id)." >&2
  81. exit 1
  82. ;;
  83. esac
  84. # Prevent concurrent processes from modifying the file at the same time.
  85. lock_rules_file
  86. # Check if the rules file is writeable.
  87. choose_rules_file
  88. link_num=$(find_next_available 'cdrom[0-9]*')
  89. match="SUBSYSTEM==\"block\", ENV{ID_CDROM}==\"?*\", $RULE"
  90. comment="$ID_MODEL ($ID_PATH)"
  91. write_rule "$match" "cdrom$link_num" "$comment"
  92. [ "$ID_CDROM_CD_R" -o "$ID_CDROM_CD_RW" ] && \
  93. write_rule "$match" "cdrw$link_num"
  94. [ "$ID_CDROM_DVD" ] && \
  95. write_rule "$match" "dvd$link_num"
  96. [ "$ID_CDROM_DVD_R" -o "$ID_CDROM_DVD_RW" -o "$ID_CDROM_DVD_RAM" ] && \
  97. write_rule "$match" "dvdrw$link_num"
  98. echo >> $RULES_FILE
  99. unlock_rules_file
  100. echo $SYMLINKS
  101. exit 0