linux-hotplug.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* persistent CD aliases
@ 2006-03-29 11:50 Marco d'Itri
  2006-03-31  5:46 ` Alexander E. Patrakov
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Marco d'Itri @ 2006-03-29 11:50 UTC (permalink / raw)
  To: linux-hotplug


[-- Attachment #1.1: Type: text/plain, Size: 149 bytes --]

I am attaching the rules and the script which I am using in the Debian 
package to generate on demand aliases for CD readers.

-- 
ciao,
Marco

[-- Attachment #1.2: cd-aliases-generator.rules --]
[-- Type: text/plain, Size: 694 bytes --]

# These rules generate rules for the /dev/{cdrom,dvd,...} symlinks and
# write them in /etc/udev/rules.d/z20_persistent-cd.rules.
#
# If you want to configure them manually then just delete the symlink
# pointing to this file.
#
# The default symlink for this file is z75_cd-aliases-generator.rules.

ACTION!="add",				GOTO="cd_aliases_generator_end"
SUBSYSTEM!="block",			GOTO="cd_aliases_generator_end"
ENV{GENERATED}=="?*",			GOTO="cd_aliases_generator_end"

# The variables for IDE devices are imported in permissions.rules.
KERNEL=="sr[0-9]*", IMPORT{program}="cdrom_id --export $tempnode"

ENV{ID_CDROM}=="?*", PROGRAM="write_cd_aliases", SYMLINK+="%c"

LABEL="cd_aliases_generator_end"


[-- Attachment #1.3: write_cd_aliases --]
[-- Type: text/plain, Size: 3033 bytes --]

#!/bin/sh -e

RULES_FILE="/etc/udev/rules.d/z25_persistent-cd.rules"

##############################################################################
lock_rules_file() {
  RULES_LOCK="/dev/.udev/.lock-${RULES_FILE##*/}"

  retry=30
  while ! mkdir $RULES_LOCK 2> /dev/null; do
    if [ $retry -eq 0 ]; then
       echo "Cannot lock $RULES_FILE!" >&2
       exit 2
    fi
    sleep 1
    retry=$(($retry - 1))
  done
}

unlock_rules_file() {
  rmdir $RULES_LOCK || true
}

# returns true if a directory is writeable
writeable() {
  if ln -s check $1/.is-writeable 2> /dev/null; then
    rm -f $1/.is-writeable
    return 0
  else
    return 1
  fi
}

choose_rules_file() {
  [ -e "$RULES_FILE" ] || PRINT_HEADER=1
  if writeable ${RULES_FILE%/*}; then
    RO_RULES_FILE='/dev/null'
  else
    RO_RULES_FILE=$RULES_FILE
    RULES_FILE="/dev/.udev/tmp-rules--${RULES_FILE##*/}"
  fi
}

##############################################################################
find_next_available() {
  local links="$(find_all_symlinks $1)"

  local basename=${links%%[0-9]*}
  local max=-1
  for name in $links; do
    local num=${name#$basename}
    [ "$num" ] || num=0
    [ $num -gt $max ] && max=$num
  done

  max=$(($max + 1))
  # "name0" actually is just "name"
  [ $max -eq 0 ] && return
  echo "$max"
}

find_all_symlinks() {
  local linkre="$1"
  local match="$2"

  [ -e $RULES_FILE ] || return

  local search='.*[[:space:],]SYMLINK+="\('"$linkre"'\)"[[:space:]]*\(,.*\|\\\|\)$'

  sed -n -e "${match}s/${search}/\1/p" $RO_RULES_FILE $RULES_FILE
}

write_rule() {
  local match="$1"
  local link="$2"
  local comment="$3"

  {
  if [ "$PRINT_HEADER" ]; then
    PRINT_HEADER=
    echo "# This file was automatically generated by the $0"
    echo "# program, probably run by the cd-aliases-generator.rules rules file."
    echo "#"
    echo "# You can modify it, as long as you keep each rule on a single line"
    echo "# and set the \$GENERATED variable."
    echo ""
  fi

  [ "$comment" ] && echo "# $comment"
  echo "ACTION==\"add\", $match, SYMLINK+=\"$link\", ENV{GENERATED}=\"1\""
  } >> $RULES_FILE
  SYMLINKS="$SYMLINKS $link"
}

##############################################################################
if [ -z "$DEVPATH" ]; then
  echo "Missing \$DEVPATH." >&2
  exit 1
fi
if [ -z "$ID_CDROM" ]; then
  echo "$DEVPATH is not a CD reader." >&2
  exit 1
fi

# Prevent parallel processes from modifying the file at the same time.
lock_rules_file

# Check if the rules file is writeable.
choose_rules_file

link_num=$(find_next_available 'cdrom[0-9]*')

#match="ENV{ID_PATH}==\"$ID_PATH\""

#kernel=${DEVPATH##*/}
#match="KERNEL==\"$kernel\""

id=${PHYSDEVPATH##*/}
match="BUS==\"$PHYSDEVBUS\", ID==\"$id\""

comment="$ID_MODEL ($ID_PATH)"

			  write_rule "$match" "cdrom$link_num" "$comment"
[ "$ID_CDROM_CD_RW" ]  && write_rule "$match" "cdrw$link_num"
[ "$ID_CDROM_DVD" ]    && write_rule "$match" "dvd$link_num"
[ "$ID_CDROM_DVD_RW" ] && write_rule "$match" "dvdrw$link_num"

unlock_rules_file

echo $SYMLINKS

exit 0


[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 191 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: persistent CD aliases
  2006-03-29 11:50 persistent CD aliases Marco d'Itri
@ 2006-03-31  5:46 ` Alexander E. Patrakov
  2006-03-31  7:26 ` Marco d'Itri
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Alexander E. Patrakov @ 2006-03-31  5:46 UTC (permalink / raw)
  To: linux-hotplug

Marco d'Itri wrote:
> I am attaching the rules and the script which I am using in the Debian 
> package to generate on demand aliases for CD readers.

Suppose the following scenario:

1) Someone has /dev/hdc as his only CD-ROM
2) He boots the computer, the script creates the /dev/cdrom -> hdc symlink
3) He powers the computer off, moves the CD-ROM to /dev/hdd, adds a disk as 
/dev/hdc, and boots again
4) The script creates /dev/cdrom1 -> hdd cymlink. The old /dev/cdrom -> hdc 
symlink stays, but points to some non-CD-ROM device.

Should one treat this as a bug?


-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid\x110944&bid$1720&dat\x121642
_______________________________________________
Linux-hotplug-devel mailing list  http://linux-hotplug.sourceforge.net
Linux-hotplug-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: persistent CD aliases
  2006-03-29 11:50 persistent CD aliases Marco d'Itri
  2006-03-31  5:46 ` Alexander E. Patrakov
@ 2006-03-31  7:26 ` Marco d'Itri
  2006-04-16  7:57 ` Alexander E. Patrakov
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Marco d'Itri @ 2006-03-31  7:26 UTC (permalink / raw)
  To: linux-hotplug

On Mar 31, "Alexander E. Patrakov" <patrakov@ums.usu.ru> wrote:

> 3) He powers the computer off, moves the CD-ROM to /dev/hdd, adds a disk as 
> /dev/hdc, and boots again
> 4) The script creates /dev/cdrom1 -> hdd cymlink. The old /dev/cdrom -> hdc 
> symlink stays, but points to some non-CD-ROM device.
> 
> Should one treat this as a bug?
No.

-- 
ciao,
Marco


-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid\x110944&bid$1720&dat\x121642
_______________________________________________
Linux-hotplug-devel mailing list  http://linux-hotplug.sourceforge.net
Linux-hotplug-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: persistent CD aliases
  2006-03-29 11:50 persistent CD aliases Marco d'Itri
  2006-03-31  5:46 ` Alexander E. Patrakov
  2006-03-31  7:26 ` Marco d'Itri
@ 2006-04-16  7:57 ` Alexander E. Patrakov
  2006-04-17 17:25 ` Andrey Borzenkov
  2006-04-18  1:58 ` Alexander E. Patrakov
  4 siblings, 0 replies; 6+ messages in thread
From: Alexander E. Patrakov @ 2006-04-16  7:57 UTC (permalink / raw)
  To: linux-hotplug

[-- Attachment #1: Type: text/plain, Size: 1227 bytes --]

Marco d'Itri wrote:
> I am attaching the rules and the script which I am using in the Debian 
> package to generate on demand aliases for CD readers.

This is a bit buggy :(

> choose_rules_file() {
>   [ -e "$RULES_FILE" ] || PRINT_HEADER=1
>   if writeable ${RULES_FILE%/*}; then
>     RO_RULES_FILE='/dev/null'
>   else
>     RO_RULES_FILE=$RULES_FILE
>     RULES_FILE="/dev/.udev/tmp-rules--${RULES_FILE##*/}"
>   fi
> }

Sorry, I don't understand how /dev/.udev/tmp-rules--* is moved to 
/etc/udev/rules.d. In my version, I removed this hack and instead added the rule 
to fail the entire uevent if / is not writeable. Then the udev_retry script that 
runs after mounting filesystems picks the uevents up from /dev/.udev/failed 
(yes, I know that this script is not available in Debian).

> find_next_available() {
>   local links="$(find_all_symlinks $1)"

This contains embedded newlines.

>   local basename=${links%%[0-9]*}

This doesn't work as expected because of newlines. To see the effect, connect 4 
IDE CD-ROMs to one box (special thanks to Archaic from the Linux From Scratch 
team for testing this).

Anyway, the fixed script (adapted to non-Debian systems) and rules are attached.

-- 
Alexander E. Patrakov

[-- Attachment #2: 81-cdrom.rules --]
[-- Type: text/plain, Size: 96 bytes --]

ACTION=="add", ENV{ID_TYPE}=="cd", IMPORT{program}="cdrom_id --export $tempnode", GROUP="cdrom"

[-- Attachment #3: 83-cdrom.rules --]
[-- Type: text/plain, Size: 521 bytes --]

ACTION!="add",                          GOTO="cd_aliases_generator_end"
SUBSYSTEM!="block",                     GOTO="cd_aliases_generator_end"
ENV{GENERATED}=="?*",                   GOTO="cd_aliases_generator_end"

# Fail the uevent if the autogenerated rules cannot be saved
ENV{ID_CDROM}=="?*", PROGRAM="/bin/grep -c ' / [^[:space:]]* rw' /proc/mounts", RESULT!="2", RUN+="/bin/false", GOTO="cd_aliases_generator_end"

ENV{ID_CDROM}=="?*", PROGRAM="write_cd_aliases", SYMLINK+="%c"

LABEL="cd_aliases_generator_end"


[-- Attachment #4: write_cd_aliases --]
[-- Type: text/plain, Size: 2646 bytes --]

#!/bin/sh -e

RULES_FILE="/etc/udev/rules.d/82-persistent-cd.rules"

##############################################################################
lock_rules_file() {
  RULES_LOCK="/dev/.udev/.lock-${RULES_FILE##*/}"

  retry=30
  while ! mkdir $RULES_LOCK 2> /dev/null; do
    if [ $retry -eq 0 ]; then
       echo "Cannot lock $RULES_FILE!" >&2
       exit 2
    fi
    sleep 1
    retry=$(($retry - 1))
  done
}

unlock_rules_file() {
  rmdir $RULES_LOCK || true
}

##############################################################################
find_next_available() {
  # use echo to convert newlines to spaces
  local links=`echo $(find_all_symlinks $1)`
  local basename=${links%%[ 0-9]*}
  local max=-1
  for name in $links; do
    local num=${name#$basename}
    [ "$num" ] || num=0
    [ $num -gt $max ] && max=$num
  done

  max=$(($max + 1))
  # "name0" actually is just "name"
  [ $max -eq 0 ] && return
  echo "$max"
}

find_all_symlinks() {
  local linkre="$1"
  local match="$2"

  [ -e $RULES_FILE ] || return

  local search='.*[[:space:],]SYMLINK+="\('"$linkre"'\)"[[:space:]]*\(,.*\|\\\|\)$'

  sed -n -e "${match}s/${search}/\1/p" $RULES_FILE
}

write_rule() {
  local match="$1"
  local link="$2"
  local comment="$3"

  [ -e "$RULES_FILE" ] || PRINT_HEADER=1
  {
  if [ "$PRINT_HEADER" ]; then
    PRINT_HEADER=
    echo "# This file was automatically generated by the $0"
    echo "# program, probably run by the 83-cdrom.rules rules file."
    echo "#"
    echo "# You can modify it, as long as you keep each rule on a single line"
    echo "# and set the \$GENERATED variable."
    echo ""
  fi

  [ "$comment" ] && echo "# $comment"
  echo "ACTION==\"add\", $match, ENV{ID_CDROM}==\"1\", SYMLINK+=\"$link\", ENV{GENERATED}=\"1\""
  } >> $RULES_FILE
  SYMLINKS="$SYMLINKS $link"
}

##############################################################################
if [ -z "$DEVPATH" ]; then
  echo "Missing \$DEVPATH." >&2
  exit 1
fi
if [ -z "$ID_CDROM" ]; then
  echo "$DEVPATH is not a CD reader." >&2
  exit 1
fi

# Prevent parallel processes from modifying the file at the same time.
lock_rules_file

link_num=$(find_next_available 'cdrom[0-9]*')

#match="ENV{ID_PATH}==\"$ID_PATH\""

#kernel=${DEVPATH##*/}
#match="KERNEL==\"$kernel\""

id=${PHYSDEVPATH##*/}
match="BUS==\"$PHYSDEVBUS\", ID==\"$id\""

comment="$ID_MODEL ($ID_PATH)"

			  write_rule "$match" "cdrom$link_num" "$comment"
[ "$ID_CDROM_CD_RW" ]  && write_rule "$match" "cdrw$link_num"
[ "$ID_CDROM_DVD" ]    && write_rule "$match" "dvd$link_num"
[ "$ID_CDROM_DVD_RW" ] && write_rule "$match" "dvdrw$link_num"

unlock_rules_file

echo $SYMLINKS

exit 0


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: persistent CD aliases
  2006-03-29 11:50 persistent CD aliases Marco d'Itri
                   ` (2 preceding siblings ...)
  2006-04-16  7:57 ` Alexander E. Patrakov
@ 2006-04-17 17:25 ` Andrey Borzenkov
  2006-04-18  1:58 ` Alexander E. Patrakov
  4 siblings, 0 replies; 6+ messages in thread
From: Andrey Borzenkov @ 2006-04-17 17:25 UTC (permalink / raw)
  To: linux-hotplug

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Sunday 16 April 2006 11:57, Alexander E. Patrakov wrote:
>
> Sorry, I don't understand how /dev/.udev/tmp-rules--* is moved to
> /etc/udev/rules.d. In my version, I removed this hack and instead added the
> rule to fail the entire uevent if / is not writeable. Then the udev_retry
> script that runs after mounting filesystems picks the uevents up from
> /dev/.udev/failed (yes, I know that this script is not available in
> Debian).
>

Could you post this script or link to it? Thank you

- -andrey
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (GNU/Linux)

iD8DBQFEQ8+MR6LMutpd94wRAuZVAJ4ijzBlanP5YEl2njAR0k9exMnPAgCfaWPr
XgVwBkgtFCu1Uf4Yw2af6wE=HqSW
-----END PGP SIGNATURE-----


-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid\x110944&bid$1720&dat\x121642
_______________________________________________
Linux-hotplug-devel mailing list  http://linux-hotplug.sourceforge.net
Linux-hotplug-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: persistent CD aliases
  2006-03-29 11:50 persistent CD aliases Marco d'Itri
                   ` (3 preceding siblings ...)
  2006-04-17 17:25 ` Andrey Borzenkov
@ 2006-04-18  1:58 ` Alexander E. Patrakov
  4 siblings, 0 replies; 6+ messages in thread
From: Alexander E. Patrakov @ 2006-04-18  1:58 UTC (permalink / raw)
  To: linux-hotplug

Andrey Borzenkov wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> On Sunday 16 April 2006 11:57, Alexander E. Patrakov wrote:
>> Sorry, I don't understand how /dev/.udev/tmp-rules--* is moved to
>> /etc/udev/rules.d. In my version, I removed this hack and instead added the
>> rule to fail the entire uevent if / is not writeable. Then the udev_retry
>> script that runs after mounting filesystems picks the uevents up from
>> /dev/.udev/failed (yes, I know that this script is not available in
>> Debian).
>>
> 
> Could you post this script or link to it? Thank you

http://wiki.linuxfromscratch.org/lfs/ticket/1769

-- 
Alexander E. Patrakov


-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid\x110944&bid$1720&dat\x121642
_______________________________________________
Linux-hotplug-devel mailing list  http://linux-hotplug.sourceforge.net
Linux-hotplug-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2006-04-18  1:58 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-03-29 11:50 persistent CD aliases Marco d'Itri
2006-03-31  5:46 ` Alexander E. Patrakov
2006-03-31  7:26 ` Marco d'Itri
2006-04-16  7:57 ` Alexander E. Patrakov
2006-04-17 17:25 ` Andrey Borzenkov
2006-04-18  1:58 ` Alexander E. Patrakov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).