write_cd_rules revision 8b5651bb009bf892b3a5537c5f36b8891f238aa4
2N/A#!/bin/sh -e
2N/A
2N/A# This script is run if an optical drive lacks a rule for persistent naming.
2N/A#
2N/A# It adds symlinks for optical drives based on the device class determined
2N/A# by cdrom_id and used ID_PATH to identify the device.
2N/A
2N/A# (C) 2006 Marco d'Itri <md@Linux.IT>
2N/A#
2N/A# This program is free software: you can redistribute it and/or modify
2N/A# it under the terms of the GNU General Public License as published by
2N/A# the Free Software Foundation, either version 2 of the License, or
2N/A# (at your option) any later version.
2N/A
2N/A# This program is distributed in the hope that it will be useful,
2N/A# but WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2N/A# GNU General Public License for more details.
2N/A
2N/A# You should have received a copy of the GNU General Public License
2N/A# along with this program. If not, see <http://www.gnu.org/licenses/>.
2N/A
2N/A# debug, if UDEV_LOG=<debug>
2N/Aif [ -n "$UDEV_LOG" ]; then
2N/A if [ "$UDEV_LOG" -ge 7 ]; then
2N/A set -x
2N/A fi
2N/Afi
2N/A
2N/ARULES_FILE="/etc/udev/rules.d/70-persistent-cd.rules"
2N/A
2N/A. /lib/udev/rule_generator.functions
2N/A
2N/Afind_next_available() {
2N/A raw_find_next_available "$(find_all_rules 'SYMLINK\+=' "$1")"
2N/A}
2N/A
2N/Awrite_rule() {
2N/A local match="$1"
2N/A local link="$2"
2N/A local comment="$3"
2N/A
2N/A {
2N/A if [ "$PRINT_HEADER" ]; then
2N/A PRINT_HEADER=
2N/A echo "# This file was automatically generated by the $0"
2N/A echo "# program, run by the cd-aliases-generator.rules rules file."
2N/A echo "#"
2N/A echo "# You can modify it, as long as you keep each rule on a single"
2N/A echo "# line, and set the \$GENERATED variable."
2N/A echo ""
2N/A fi
2N/A
2N/A [ "$comment" ] && echo "# $comment"
2N/A echo "$match, SYMLINK+=\"$link\", ENV{GENERATED}=\"1\""
2N/A } >> $RULES_FILE
2N/A SYMLINKS="$SYMLINKS $link"
2N/A}
2N/A
2N/Aif [ -z "$DEVPATH" ]; then
2N/A echo "Missing \$DEVPATH." >&2
2N/A exit 1
2N/Afi
2N/Aif [ -z "$ID_CDROM" ]; then
2N/A echo "$DEVPATH is not a CD reader." >&2
2N/A exit 1
2N/Afi
2N/A
2N/Aif [ "$1" ]; then
2N/A METHOD="$1"
2N/Aelse
2N/A METHOD='by-path'
2N/Afi
2N/A
2N/Acase "$METHOD" in
2N/A by-path)
2N/A if [ -z "$ID_PATH" ]; then
2N/A echo "$DEVPATH not supported by path_id. by-id may work." >&2
2N/A exit 1
2N/A fi
2N/A RULE="ENV{ID_PATH}==\"$ID_PATH\""
2N/A ;;
2N/A
2N/A by-id)
2N/A if [ "$ID_SERIAL" ]; then
2N/A RULE="ENV{ID_SERIAL}==\"$ID_SERIAL\""
2N/A elif [ "$ID_MODEL" -a "$ID_REVISION" ]; then
2N/A RULE="ENV{ID_MODEL}==\"$ID_MODEL\", ENV{ID_REVISION}==\"$ID_REVISION\""
2N/A else
2N/A echo "$DEVPATH not supported by ata_id. by-path may work." >&2
2N/A exit 1
2N/A fi
2N/A ;;
2N/A
2N/A *)
2N/A echo "Invalid argument (must be either by-path or by-id)." >&2
2N/A exit 1
2N/A ;;
2N/Aesac
2N/A
2N/A# Prevent concurrent processes from modifying the file at the same time.
2N/Alock_rules_file
2N/A
2N/A# Check if the rules file is writeable.
2N/Achoose_rules_file
2N/A
2N/Alink_num=$(find_next_available 'cdrom[0-9]*')
2N/A
2N/Amatch="SUBSYSTEM==\"block\", ENV{ID_CDROM}==\"?*\", $RULE"
2N/A
2N/Acomment="$ID_MODEL ($ID_PATH)"
2N/A
2N/A write_rule "$match" "cdrom$link_num" "$comment"
2N/A[ "$ID_CDROM_CD_R" -o "$ID_CDROM_CD_RW" ] && \
2N/A write_rule "$match" "cdrw$link_num"
2N/A[ "$ID_CDROM_DVD" ] && \
2N/A write_rule "$match" "dvd$link_num"
2N/A[ "$ID_CDROM_DVD_R" -o "$ID_CDROM_DVD_RW" -o "$ID_CDROM_DVD_RAM" ] && \
2N/A write_rule "$match" "dvdrw$link_num"
2N/Aecho >> $RULES_FILE
2N/A
2N/Aunlock_rules_file
2N/A
2N/Aecho $SYMLINKS
2N/A
2N/Aexit 0
2N/A
2N/A