Mostrando entradas con la etiqueta usb. Mostrar todas las entradas
Mostrando entradas con la etiqueta usb. Mostrar todas las entradas

05 septiembre 2008

Automontaje (beta)

Scottiepipas tuvo la inquietud de como hacer automontaje de usb extraibles en slackware, y dedujo que con reglas udev podría hacerse, y funcionaba a la perfección, creaba la carpeta y montaba el dispositivo, pero no creaba el link en el escritorio, pero bueno, como ya hace todo lo que tiene que hacer ahi va toda la info:

Los 3 primeros scripts vienen directamente de Zenwalk, el ultimo con 2 modificaciones, el ultimo es de cosecha propia.

Modificamos /etc/udev/udev.conf

# udev.conf
# The main config file for udev
#
# This file can be used to override some of udev's default values
# for where it looks for files, and where it places device nodes.


# udev_root - where in the filesystem to place the device nodes
udev_root="/dev/"

# udev_db - The name and location of the udev database.
udev_db="/dev/.udev.tdb"

# udev_rules - The name and location of the udev rules dir
udev_rules="/etc/udev/rules.d"

# default_mode - set the default mode for all nodes that have no
# explicit match in the permissions file
default_mode="666"

# default_owner - set the default owner for all nodes that have no
# explicit match in the permissions file
default_owner="root"

# default_group - set the default group for all nodes that have no
# explicit match in the permissions file
default_group="root"

# udev_log - set to "yes" if you want logging, else "no"
udev_log="no"

Creamos /etc/udev/rules.d/20-hotmount.rules

# USB storage devices
KERNEL=="sd*|sr*|ub*", ACTION=="add", SUBSYSTEMS=="usb", ENV{DEVFLAG}="usbdisk", NAME="%k", GROUP="cdrom", MODE="0660", RUN+="/lib/udev/usbmount.sh add /dev/%k"
KERNEL=="sd*|sr*|ub*", ACTION=="remove", ENV{DEVFLAG}=="usbdisk", RUN+="/lib/udev/usbmount.sh remove /dev/%k"


Creamos /lib/udev/usbmount.sh
#!/bin/sh
# This script mounts USB and other mass storage devices when they are plugged in
# and unmounts them when they are removed.
# It also creates mountpoints as needed, and removes them afterward
# Copyright (C) 2006 Jean-Philippe Guillemin and Mark Colclough
#
# This file is free software; the copyright holder gives unlimited
# permission to copy and/or distribute it, with or without
# modifications, as long as this notice is preserved.
#
# This file is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY, to the extent permitted by law; without
# even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE.
#

# Usage : [hotmount.sh|usbmount.sh|firemount.sh|pcmciamount.sh] (add|remove) DEVICE [MOUNTPOINT]
# if the variable $NICEMOUNTPOINT is defined it will be used as the first tried mountpoint

# version 1.0 11/06/2006

VERBOSE="no"

# Log a string via the syslog facility.
log()
{
[ "$VERBOSE" = "yes" ] && logger -p user.$1 -t "usbmount[$$]" -- "$2"
}


# Test if the first parameter is in the list given by the second
# parameter.
in_list()
{
for v in $2; do
test "$1" != "$v" || return 0
done
return 1
}

# Test if /lib/udev/vol_id is executable.
test -x /lib/udev/vol_id || { log err "cannnot execute /lib/udev/vol_id"; exit 1; }

# Default mountpoints depend on how we were called
case "${0##*/}" in
firemount.sh)
MOUNTPOINTS="/mnt/fire /mnt/fire1 /mnt/fire2 /mnt/fire3 /mnt/fire4 /mnt/fire5 /mnt/fire6 /mnt/fire7 /mnt/fire8 /mnt/fire9 /mnt/fire10 /mnt/fire11"
;;
pcmciamount.sh)
MOUNTPOINTS="/mnt/pcm /mnt/pcm1 /mnt/pcm2 /mnt/pcm3 /mnt/pcm4 /mnt/pcm5 /mnt/pcm6 /mnt/pcm7 /mnt/pcm8 /mnt/pcm9 /mnt/pcm10 /mnt/pcm11"
;;
mmcmount.sh)
MOUNTPOINTS="/mnt/mmc /mnt/mmc1 /mnt/mmc2 /mnt/mmc3 /mnt/mmc4 /mnt/mmc5 /mnt/mmc6 /mnt/mmc7 /mnt/mmc8 /mnt/mmc9 /mnt/mmc10 /mnt/mmc11"
;;
*)
MOUNTPOINTS="/mnt/usb /mnt/usb1 /mnt/usb2 /mnt/usb3 /mnt/usb4 /mnt/usb5 /mnt/usb6 /mnt/usb7 /mnt/usb8 /mnt/usb9 /mnt/usb10 /mnt/usb11"
;;
esac
MOUNTOPTIONS=" noauto,user,rw,owner,exec"

# The device name MUST be given as 2nd argument
DEVNAME="$2"

# Maybe a user-defined mountpoint in fstab ?
FSTABMOUNTPOINT="$(grep "$DEVNAME[[:space:]]" /etc/fstab | awk '{print $2}' | tail -n1)"
[ "$FSTABMOUNTPOINT" ] && MOUNTPOINTS="$FSTABMOUNTPOINT $MOUNTPOINTS"
FSTABMOUNTOPTIONS="$(grep "$DEVNAME[[:space:]]" /etc/fstab | awk '{print $4}' | tail -n1)"

# The mount point COULD be given as 3rd argument
[ "$3" ] && MOUNTPOINTS="$3 $MOUNTPOINTS"

# if the variable $NICEMOUNTPOINT is defined it will be used as the first tried mountpoint
[ "$NICEMOUNTPOINT" ] && MOUNTPOINTS="$NICEMOUNTPOINT $MOUNTPOINTS"

umask 000

if [ "$1" = add ] ; then

log info "devname == $DEVNAME !!"
# Try to read from the device. Some devices need a few seconds
# initialization time before they can be accessed. Give up after
# 20 seconds (a Markish infinite... ;).
log info "testing whether $DEVNAME is readable"
read_success=no
for t in 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19; do
dd if="$DEVNAME" of=/dev/null bs=512 count=1
if [ "$?" = "0" ] ; then
read_success=yes
break
fi
log info "attempt $t to read from $DEVNAME failed"
sleep 1
done
if [ "$read_success" != "yes" ]; then
log err "cannot read from $DEVNAME"
exit 1
fi

# Test if the device contains a filesystem.
/lib/udev/vol_id "$DEVNAME" | egrep -q '^ID_FS_USAGE=(filesystem|disklabel)$'
if [ "$?" = "0" ] ; then
log info "$DEVNAME contains a filesystem or disklabel"

fstype="$(/lib/udev/vol_id -t $DEVNAME )"
log info "$DEVNAME contains filesystem type $fstype"

# We search for available mountpoint in the list
for mountpoint in $MOUNTPOINTS; do

# Get a lock for the mountpoint under consideration
for t in 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19; do
(set -C; echo "$$" > /var/lock/${mountpoint##*/}) && break
log info "waiting for $mountpoint lockfile to be released"
sleep 1
done

if [ -d "$mountpoint" ]; then
log info "mountpoint $mountpoint exists"
if [ ! "$( mount | awk '{print $3}' | grep "$mountpoint" )" ]; then
log info "mountpoint $mountpoint is available for $DEVNAME"
break
else
log info "mountpoint $mountpoint is busy"
rm -f /var/lock/${mountpoint##*/}
continue
fi
else
log info "creating mountpoint $mountpoint for $DEVNAME"
mkdir -p -m0 $mountpoint
umount -l "$mountpoint"
log info "mountpoint $mountpoint is available for $DEVNAME"
break
fi
rm -f /var/lock/${mountpoint##*/}
done

if [ "$mountpoint" ] ; then

# Determine mount options.
options=""
if [ "$FSTABMOUNTOPTIONS" ]; then
options="$FSTABMOUNTOPTIONS"
else
[ "$MOUNTOPTIONS" ] && options="$MOUNTOPTIONS"
[ ! "$fstype" = "vfat" ] && options="${options},sync,dirsync"
[ "$fstype" = "vfat" ] && options="${options},uid=0,gid=100,umask=0"
fi

# Mount the filesystem.
for t in 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19; do
[ "$( mount | awk '{print $1}' | grep "$DEVNAME" )" ] && break
log info "executing command: mount -t $fstype -o $options $DEVNAME $mountpoint"
eval "mount -t $fstype -o $options $DEVNAME $mountpoint"
/lib/udev/montaje.sh $DEVNAME $mountpoint
done
rm -f /var/lock/${mountpoint##*/}
else
# No suitable mount point found.
log warning "no mountpoint found for $DEVNAME"
exit 1
fi
else
log info "$DEVNAME does not contain a filesystem or disklabel"
fi

elif [ "$1" = remove ] ; then
/lib/udev/montaje.sh $DEVNAME $DEVNAME $1
# A block or partition device has been removed.
# Test if it is mounted.
while read device mountpoint fstype remainder; do
if [ "$DEVNAME" = "$device" ] ; then
# If the mountpoint and filesystem type are maintained by
# this script, unmount the filesystem.
if in_list "$mountpoint" "$MOUNTPOINTS" ; then
log info "unmounting $mountpoint"
umount -l "$mountpoint"
rmdir $mountpoint && break
log info "cannot remove $mountpoint, maybe not empty"
fi
break
fi
done < /proc/mounts fi exit 0


Por último creamos /lib/udev/montaje.sh

#!/bin/bash
#*******************************************
# Llamado desde usbmount.sh
# /lib/udev/montaje.sh $DEVNAME $mountpoint [remove]
#
# v 0.3

nombre=`echo $2 | cut -c 6,7,8,9`
usuario=`w | grep k | cut -f 1 -d ' '`

if [ $usuario == "root" ] ; then
export HOME=/root
else
export HOME=/home/$usuario
fi


if [ "$3" != remove ]
then
echo " [Desktop Entry]
Type=FSDevice
Dev=$1
MountPoint=$2
Encoding=UTF-8
Icon=usbpendrive_mount
UnmountIcon=usbpendrive_unmount
Comment=$ID_SERIAL $ID_FS_TYPE
Name=$nombre ($ID_FS_LABEL)" > $HOME/Desktop/$nombre.desktop
echo "$1 $2 vfat users" >> /etc/fstab

############
#Popup en KDE (test)

export DISPLAY=":0"
export XAUTHORITY=$HOME/.Xauthority
/usr/bin/xhost + local:$usuario
/usr/bin/kdialog --title "Montado: $ID_MODEL [$ID_VENDOR]" --passivepopup " Etiqueta: $ID_FS_LABEL
Sistema de ficheros: $ID_FS_TYPE
Punto de montaje: $2
Dispositivo: $1" 7 --geometry 10x10-0-0
/usr/bin/xhost - local:$usuario
###########

else
borra=`cat /etc/fstab | grep $2 | cut -f 2 -d ' '`
cat /etc/fstab | grep $2 -v > /etc/fstab
rmdir $borra
dis=`echo $borra | cut -d / -f 3 | cut -d ' ' -f 1`
rm $HOME/Desktop/$dis.desktop
fi



y con esto lo que conseguimos es que al pinchar nuestra memoria usb aparezca en nuestro escritorio un link a ella, con opcion de desmontarla con un par de clicks, al quitar nuestro usb el link desaparece.

No olvidar dar permiso de ejecucion a los scripts

chmod +x script


Empaquetado aqui v0.4. Recomiendo hacer una copia de seguridad de /etc/udev/udev.conf, ya que es el unico que se sobreescribe, y después darle permiso de ejecución a los ficheros.

01 febrero 2007

Control acceso con pendrive

Hace tiempo nos planteamos añadirle un poco mas de seguridad al acceso en sistema Linux, y se nos ocurrió utilizar un pendrive.
La ejecución al final se truncó y no llegamos a nada, y esta mañana me lo replanteé y ahi están los scripts.

/etc/rc.d/usb.sh

#!/bin/bash
usb_on=`cat /proc/bus/usb/devices | grep "SerialNumber=5B6C1D8E497A"`
## ********************************** numero de serie de mi pendrive #
## Sustituir por el que convenga ##
if [ ! -z "$usb_on" ] ; then
exit 1;
else
exit 0;
fi


y añadimos al final de /etc/profile

/etc/rc.d/usb.sh

if [ $? == 1 ] ; then
echo "Bienvenido"
else
echo "Inserte Pendrive para acceder al sistema"
exit -1
fi

todo esto dando por supuesto que cada pendrive tiene un único numero de serie, ya ire perfeccionando el script y lo subire, que esto salio de 6 minutos de lucidez xD.

un saludo