From: Wout Mertens <wmertens@cisco.com>
To: linux-hotplug@vger.kernel.org
Subject: Re: Working on a usb-storage hotplug script
Date: Wed, 10 Dec 2003 19:29:58 +0000 [thread overview]
Message-ID: <marc-linux-hotplug-107108467730680@msgid-missing> (raw)
In-Reply-To: <marc-linux-hotplug-107006883203516@msgid-missing>
Yesterday at 17:09, Olaf Hering ponderously produced:
> On Tue, Dec 09, Wout Mertens wrote:
>
> > Comments?
>
> forget 2.4, work on 2.6. 2.6 will give you a block event if there is
> something of interrest.
Well, you're right. I just created a hotplug script that will automount
all block devices that get attached. I use this behaviour to use a USB
key as a token for connecting to a VNC session from a thin client.
It's way cleaner than the 2.4 version...
Here's the script, comments?
=====================#!/bin/sh
#
# Automount hotplugged block devices, by Wout Mertens.
#
# Linux v2.6 version
# This script is released under version 2 of the GPL.
# To install this, either make it /sbin/hotplug, or, if you have
# linux-hotplug installed, make it /etc/hotplug.d/block/automount.hotplug .
# It needs to be executable. I tested it with busybox's ash.
#
# The devices will be mounted for the console user if applicable.
#
# To work, this needs:
# - v2.6 kernel support:
# - hotplugging, /proc mounted, /sys mounted
# - filesystems that will be mounted, like vfat
# - sh, echo, tr, stat or ls, getent or grep passwd,
# mount, umount, mkdir, rmdir. logger is used if there.
#
# Possible improvements:
# - Create a desktop entry for the device.
# - Call a notifier script when mounting/unmounting. This could create the
# desktop entry by itself.
# - Mount as supermount if available. This will improve behaviour for
# synchronous writes and hanging mounts, although umount -lf already
# does a good job.
# - Detect filesystem on device, so filesystems that don't need a mounting
# user can skip user detection.
# Debugging
# set -xv
# exec > /tmp/hotplug.$$ 2>&1
PATH="/bin:/sbin:/usr/bin:/usr/sbin"
export PATH
# We only handle block device messages
[ "$1" != "block" ] && exit 0
# Proclaim stuff to the world
mesg () {
logger -t $0 "$*"
}
# Is path already mounted?
is_path_mounted () {
while read dev path rest
do
if [ "$path" = "$1" ]
then
return 0
fi
done < /proc/mounts
return 1
}
# Get mounted path for a device
get_mounted_path () {
while read dev path rest
do
if [ "$dev" = "$1" ]
then
echo "$path"
return 0
fi
done < /proc/mounts
return 1
}
# Wait for a file to appear
wait_for () {
local count=0
while [ $count -lt 10 ] && [ ! -e "$1" ]
do
sleep 1
done
[ $count -eq 10 ] && return 1
return 0
}
# Remove strange characters from a filename
clean_filename () {
echo $1 | tr " /?*\"<>" "_______"
}
# Figure out the device to mount
set `echo $DEVPATH | tr / " "`
[ $# -ne 3 ] && exit 0
DEVICE=/dev/$3
if [ "$ACTION" = "remove" ]
then
# Unmount it
dir=`get_mounted_path $DEVICE`
[ -d "$dir" ] || exit 1
umount -lf "$dir"
rmdir "$dir"
exit 1
fi
if [ "$ACTION" = "add" ]
then
# Mount it
# Make sure we have support for vfat
modprobe -q vfat
wait_for /sys/$1/$2/device/vendor || exit 1
# Find the name
PRODUCT=`cat /sys/$1/$2/device/model`
if [ -z "$PRODUCT" ]
then
PRODUCT=generic
fi
# Find out where we mount it
MOUNTPATH=/mnt/usb/`clean_filename "$PRODUCT"`
if is_path_mounted "$MOUNTPATH"
then
count=1
while is_path_mounted "${MOUNTPATH}_${count}"
do
count=$(( $count + 1 ))
done
MOUNTPATH="${MOUNTPATH}_${count}"
fi
# Make sure it's a directory
if [ -e "$MOUNTPATH" ]
then
if [ ! -d "$MOUNTPATH" ]
then
mesg "$MOUNTPATH exists but is not a directory"
exit 1
fi
else
mkdir -p "$MOUNTPATH"
if [ $? -ne 0 ]
then
mesg "Could not create mountpoint $MOUNTPATH"
exit 1
fi
fi
# Find out who we are going to mount it as
CONSOLEUSER=`stat -c%U /dev/console 2>/dev/null`
if [ -z "$CONSOLEUSER" ]
then
set `ls -l /dev/console`
CONSOLEUSER=$3
fi
[ -n "$CONSOLEUSER" ] || CONSOLEUSER=root
PASSWD=`getent passwd $CONSOLEUSER 2>/dev/null`
if [ -z "$PASSWD" ]
then
PASSWD=`grep "$CONSOLEUSER" /etc/passwd||grep root /etc/passwd`
fi
if [ -z "$PASSWD" ]
then
mesg "Could not get password entry for $CONSOLEUSER"
exit 1
fi
set `echo $PASSWD | tr : " "`
if [ $# -lt 4 ]
then
mesg "Bad password entry for $CONSOLEUSER"
exit 1
fi
# These options should prevent abuse and make it writeable for the
# console user.
MOUNTOPTS="-s -osync,nosuid,umask\a7,uid=$3,gid=$4"
mesg Mounting $DEVICE on $MOUNTPATH, options $MOUNTOPTS
mount $MOUNTOPTS $DEVICE $MOUNTPATH
fi
=====================
-------------------------------------------------------
This SF.net email is sponsored by: IBM Linux Tutorials.
Become an expert in LINUX or just sharpen your skills. Sign up for IBM's
Free Linux Tutorials. Learn everything from the bash shell to sys admin.
Click now! http://ads.osdn.com/?ad_id\x1278&alloc_id371&op=click
_______________________________________________
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
next prev parent reply other threads:[~2003-12-10 19:29 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-11-29 1:19 Working on a usb-storage hotplug script Martin
2003-11-29 12:33 ` reflex
2003-11-29 14:06 ` Oliver Neukum
2003-11-29 18:05 ` Marco d'Itri
2003-11-30 11:19 ` reflex
2003-11-30 19:38 ` Greg KH
2003-12-09 9:05 ` Wout Mertens
2003-12-09 16:09 ` Olaf Hering
2003-12-09 16:24 ` Greg KH
2003-12-10 19:29 ` Wout Mertens [this message]
2003-12-10 19:41 ` Wout Mertens
2003-12-10 19:49 ` Olaf Hering
2003-12-11 15:37 ` Wout Mertens
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=marc-linux-hotplug-107108467730680@msgid-missing \
--to=wmertens@cisco.com \
--cc=linux-hotplug@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).