Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Make sure your storage devices retain their /dev entries
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
optilude
Apprentice
Apprentice


Joined: 29 May 2002
Posts: 248
Location: England

PostPosted: Fri Dec 26, 2003 3:05 am    Post subject: Make sure your storage devices retain their /dev entries Reply with quote

Notice - UDEV is a much better (more flexible) way of doing this. Look at GWN for February 17th for some pointers.

It started in this thread, which lead me to discover this post, which gave me enough information to write the script below...

My problem is that hot-pluggable devices (such as my Creative Labs MuVo NX) get assigned devices in /dev (such as /dev/sda1, /dev/sda2 etc.) on a first-come-first-serve basis. Hence, if I happen to plug in my MuVo before my SD card reader, it gets /dev/sda, and the reader gets /dev/sdb, and vice-versa. This messes up my mount points in /etc/fstab. Using hotplug, though, I use the following script to automatically create and remove a symlink in /dev to the MuVo based on its name. You (probably) need a 2.6 kernel, sysfs mounted on /sys, usb-storage as a module and hotplug to make this work.

First, the script. You may have to create the "block" directory, and make sure the filename ends with ".hotplug":
/etc/hotplug.d/block/make-symlinks.hotplug
Code:

#!/bin/bash

# Hotplug script to automatically make symlinks to hotplugged devices.
# Requires a configuration file in /etc/hotplug.d/block called symlinks.rc
# This file should have the following format:

# /dev/muvo NOMAD MuVo NX
# /dev/other Some other device

# The first word on each line should be the desired symlink position, and the
# second argument should be the model, as found in /sys/block/sd?/device/model
# when the device is plugged in. Hence, if you plug in the device with model
# name NOMAD MuVo NX, a symlink to its device node (e.g. /dev/sda) will be
# created in /dev/muvo. You can then set up an fstab entry to use /dev/muvo
# as the device for the MuVo USB device.

CONFIG="/etc/hotplug.d/block/symlinks.rc"

# Read the config file and figure out the symlink
# Arguments: the full model string
get_symlink () {
   # Get the model, but make it whitespace agnostic, since sometimes the name
   # is padded with whitespace
   local target_model=$(echo $1)
   local link=""
   
   link=$(cat "${CONFIG}" | grep -i "${target_model}" | awk '{ print $1 }')
   
   if test -n "${link}" ; then
      echo "${link}"
      return 0
   else
      return 1
   fi
}

# Wait for a file to appear (stolen from Wout Mertens, wmertens@gentoo.org)
wait_for_file () {
   local count=0
   while test $count -lt 10 && ! test -r "$1" ; do
      sleep 1
      count=$((count + 1))
   done
   test $count -eq 10 && return 1
   return 0
}

# Only handle block devices
test "${1}" = "block" || exit 1 # (Not called for block hotplug)

# Ensure we have a partition device
partition=$(echo "${DEVPATH}" | awk -F\/ '{ print $4 }')
test -n "${partition}" || exit 2 # (Invalid $DEVPATH; should be /block/sd?/sd??)

# Figure which device to mount
device=$(echo "${DEVPATH}" | awk -F\/ '{ print $3 }')
test -n "${device}" || exit 2

# Get the model we're looking for
model_file="/sys/block/${device}/device/model"

wait_for_file "${model_file}"
test -r "${model_file}" || exit 3 # (Could not read model)

model=$(< ${model_file})
test -n "${model}" || exit 3

link=$(get_symlink "${model}")
test -n "${link}" || exit 4 # (Could not find link for model)

case "${ACTION}" in
   add)
      ln -sf "/dev/${partition}" "${link}"
      ;;
   remove)
      test -L "${link}" && rm -f "${link}"
      ;;
esac


Then, you need to set it up. You'll need to know the model name of your device, which you can find in /sys/block/sd?/device/model, where sd? is the name of the block device created for the storage device. Then decide what name you want your symlink to have, and create a configuration file following this example:

/etc/hotplug.d/block/symlinks.rc
Code:

/dev/muvo NOMAD MuVo NX
/dev/cardreader SD Card Reader


The file format is pretty strict, so please make sure that the device is the first thing on the line, followed by some whitespace, followed by the contents of /sys/block/sd?/device/model. Do not put anything else in the file.

Note that some devices (like my MuVo) pads the name string with spaces. To make that less annoying, the script will convert *any* whitespace in the model string it reads from /sys/block/sd?/device/model to a *single* whitespace. Hence, if the name of the SD card reader had been "SD Card Reader", you should only write "SD Card Reader" (with single spaces between each word) in the file. Just put a single space between each word in the name, and you'll be fine.

The end result should be that whenever you plug a device in, a symlink to a valid partition is created in /dev; so in the above example, /dev/muvo will point to /dev/sda1 or /dev/sdb1 or /dev/sdc1, regardless of when the MuVo was plugged in.

Hope you find it useful!

Martin
_________________
--
"Life is both a major and a minor key" -- Travis


Last edited by optilude on Tue Feb 17, 2004 11:01 pm; edited 1 time in total
Back to top
View user's profile Send private message
pschram
n00b
n00b


Joined: 04 Jan 2003
Posts: 44

PostPosted: Thu Jan 08, 2004 5:53 pm    Post subject: Reply with quote

Very cool stuff. This is really simple and handy. One note though: You could add to your post that it is neccesary to make the script executable. At first it didn't work, so I looked at the permissions and to let the script execute it has to be executable (duh!!!). I couldn't find this in your post, maybe it could safe some people time.

Anyway, I really like it. Well done.
Back to top
View user's profile Send private message
Elm0
Apprentice
Apprentice


Joined: 24 Nov 2002
Posts: 281
Location: UK

PostPosted: Sun Feb 15, 2004 6:28 pm    Post subject: Reply with quote

This is really nice, great work, however the script isn't quite flexible enough yet. On my iPod (2 partitions /dev/sdx1 /dev/sdx2) and USB Floppy drive (appear to have 4 partitions so /dev/sdx[1-4] entries but I actually want to mount the /dev/sdx drive) I can't get the symlink to point to the correct partition.

I'm not a great bash scripter, so I would be really grateful if you could modify the script for us gentooers so that in symlinks.rc syntax such as the following could be used:

Code:

/dev/ipod iPod 2
/dev/usbfloppy USB UF000x 0


Just to clarify the ipod entry would symlink /dev/ipod to /dev/sdx2 whilst the /dev/usbfloppy would symlink to /dev/sdx (signified by the 0).
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum