LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop
User Name
Password
Linux - Desktop This forum is for the discussion of all Linux Software used in a desktop context.

Notices


Reply
  Search this Thread
Old 02-14-2007, 06:43 PM   #1
Bane73
LQ Newbie
 
Registered: Jul 2004
Distribution: Mandrake 10.0 Official
Posts: 5

Rep: Reputation: 0
Running a script when auto-mounter detects USB thumb-drive insertion...


Knowing linux, I know there has got to be a way to do this... however, not knowing linux well enough, I'm not sure how to go about it Hopefully someone here can point me down the right path...

I have a script I run that sync's the contents of my USB thumb-drive to a directory on my harddrive and it works great... however, it's annoying to run... I'd like to find out what script the auto-mounter runs when I insert the USB stick so that I can see if I can just call my script from the end of the auto-mounter's script and thus sync my stuff automatically whenever I insert the stick... any ideas???

It may not matter, but for reference, I'm running openSUSE 10.2


~Brandon
 
Old 02-14-2007, 07:35 PM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Which desktop are you using. If it is KDE, a popup appears asking what you want to do. I think that could add an entry to the dialog and configure it to run your sync command.

There is a HAL helper script that I saw once, but I'm not certain under what circumstances it is called. Using "locate" might help pin it down. Also look at the process listing. I think there is a KDE daemon involved as well, which allows the user to graphically control the course of action to take.
 
Old 07-13-2007, 10:01 PM   #3
cordel
LQ Newbie
 
Registered: Mar 2005
Location: /USA/Washington/Seattle
Distribution: CentOS, xen, FC, Debian, BSD
Posts: 1

Rep: Reputation: 0
You can do this by useing udev rules:
Plug your thumbdrive in, and
cat /proc/scsi/usb-storage/*

You should find an entry for it similar to this:
Host scsi7: usb-storage
Vendor: Memorex
Product: TRAVELDRIVE 005B
Serial Number: 07751F9402C9
Protocol: Transparent SCSI
Transport: Bulk
Quirks:

Now, with either the serial number or the vendor/model combo, you can write the rule. The rule creates a symlink for the device in the /dev tree, for example, /dev/TRAVELDRIVE

create a text file in /etc/udev/rules.d called 95.usbbackup.rules. You can use a number other than 95, but keep in mind that udev processes rules in alphanumeric order, and it's better to have local rules processed last.

If you have a serial number, type a rule like this (all on one line) into the file, and save it:

BUS="usb", SYSFS{serial}="07751F9402C9", SYMLINK="TRAVELDRIVE", RUN+="/usr/local/bin/YOUR_SCRIPT.sh [Options if your script has them] "

You can string as many SYSFS{} entries together as you need to identify the drive uniquely.

BUS="scsi", SYSFS{vendor}==" Memorex", SYSFS{product}=="TRAVELDRIVE 005B ", SYMLINK="TRAVELDRIVE", RUN+="/usr/local/bin/backup-usbthumb.sh"

Your rule now fires every time you plug in your thumbdrive.

Last edited by cordel; 07-13-2007 at 11:18 PM.
 
Old 07-17-2007, 04:30 PM   #4
happyslacker
LQ Newbie
 
Registered: Jul 2007
Posts: 15

Rep: Reputation: 0
See the source of vsupdfstab-0.1-noarch-0.tgz:

/etc/vsupdfstab.udev
Code:
#!/bin/bash

# /etc/vsupdfstab.udev
# версия от 08.05.2006 г. 20:35

# Този скрипт добавя във файла /etc/fstab запомнящите устройства
# (памети и фотоапарати на USB), малко след момента в който се включат
# към системата и ги премахва от /etc/fstab, когато бъдат премахнати.
#
# Устройството не се монтира с опция "sync", защото това намалява драстично
# живота на паметта. Също така се ползва опция noatime, която допълнително
# увеличава живота на паметта.
#
# Преди да се извади устройството трябва ръчно да се напише команда sync или 
# да се демонтира. При изваждане на устройството този скрипт  го демонтира,
# но тогава вече ще е късно, ако потребителя не го е демонтирал ръчно или
# не е написал команда sync.
#
# Допускаме, че $DEVNAME винаги е истинския път към устройството (а не път към
# негова символна връзка). Ако това не е така, скрипта няма да работи (обаче не вярвам
# udevd да дава път към връзка).
#
# Този скрипт нарочно не монтира дяловете, защото това не е съвместимо с КДЕ:
# http://vslivecd.openfmi.net/viewtopic.php?t=58

if [ -z "$DEVNAME" -a -z "$SUBSYSTEM" -o -z "$DEVPATH" -o -z "$ACTION" ]; then
  echo
  echo "This script must be called by udevd because it needs the following environment variables: DEVPATH, ACTION, and DEVNAME or SUBSYSTEM"
  echo
  exit
fi

dev_log=/var/log/test_vsupdfstab.log
echo "***************************">> $dev_log
date >> $dev_log
echo "Някакво събитие се е случило:" >> $dev_log
echo "DEVNAME=$DEVNAME" >> $dev_log
echo "SUBSYSTEM=$SUBSYSTEM" >> $dev_log
echo "DEVPATH=$DEVPATH" >> $dev_log
echo "ACTION=$ACTION" >> $dev_log
echo "ID_BUS=$ID_BUS" >> $dev_log
echo "ID_FS_LABEL=$ID_FS_LABEL" >> $dev_log
echo "ID_FS_LABEL_SAFE=$ID_FS_LABEL_SAFE" >> $dev_log
echo "ID_FS_TYPE=$ID_FS_TYPE" >> $dev_log
echo "ID_FS_USAGE=$ID_FS_USAGE" >> $dev_log
echo "ID_FS_UUID=$ID_FS_UUID" >> $dev_log
echo "ID_FS_VERSION=$ID_FS_VERSION" >> $dev_log
echo "ID_MODEL=$ID_MODEL" >> $dev_log
echo "ID_PATH=$ID_PATH" >> $dev_log
echo "ID_REVISION=$ID_REVISION" >> $dev_log
echo "ID_SERIAL=$ID_SERIAL" >> $dev_log
echo "ID_TYPE=$ID_TYPE" >> $dev_log
echo "ID_VENDOR=$ID_VENDOR" >> $dev_log

error_msg_to_log(){
  echo "***************************">> $dev_log
  date >> $dev_log
  echo "$1" >> $dev_log
}

if [ ! "$SUBSYSTEM" = "block" ]; then
 error_msg_to_log "Хм... SUBSYSTEM не е block - изпълнението на $0 спира (команда exit 0)"
 exit 0
fi

if [ ! "$ID_BUS" = "usb" ]; then
 error_msg_to_log "Хм... ID_BUS не е usb - изпълнението на $0 спира (команда exit 0)"
 exit 0
fi

# $1 = устройството
clear_if_device_is() {
read sDEV sMNTP sOPT s1 s2
REAL_sDEV=`readlink -f $sDEV`
if [ "$1" = "$REAL_sDEV" ]; then
 umount $REAL_DEV
 umount $sMNTP
 umount -l $REAL_DEV
 umount -l $sMNTP
 rmdir $sMNTP
 return 1
fi
}

remove_from_fstab3() {
fstab_path="/etc/fstab"
fstab_temp="/etc/fstab.tmp.remove_from_fstab"
echo -n "" > $fstab_temp
REAL_DEV=`readlink -f $1`
cat $fstab_path | while read LN;
do
 echo $LN | cut -d "#" -f 1 | clear_if_device_is "$REAL_DEV"
 if [ $? = 0 ]; then
  echo "$LN" >> $fstab_temp
 fi
done
mv "$fstab_temp" "$fstab_path"
}

# Добавяне на информация в /etc/fstab, че устройство $1 че ще се
# монитира в точка $2
add_device_to_fstab()
{
  if [ -e "$2" -o ! -b "$1" ]; then return 1; fi # ако директорията съществува е опасно да се продължи

  if [ "$ID_FS_TYPE" = "" ]; then
   error_msg_to_log "Udevd не каза каква е файловата система на $1"
   return 1
   else
   fstype="$ID_FS_TYPE"
  fi

  options="auto,users,noatime,async"
   # auto: за да могат да се монтират с команда mount -a 
   # users: за да могат да се (де)монтират от всички потребители
   # noatime,async: удължаване живота на Flash паметта

  case "$fstype" in
   ntfs) options="${options},rw,nls=utf8,fmask=111,dmask=000" ; fstype=ntfs-3g ;;
                                                              # ^^^^^^^^^^^^^^ - ако има проблеми - да се изтрие
   msdos) options="${options},fmask=111,dmask=000" ;;
   vfat) options="${options},utf8,codepage=855,fmask=111,dmask=000" ;; 
  esac
  mkdir -p $2
  echo "$1 $2 $fstype $options 0 0" >> /etc/fstab

}

device_is_in_line() {
read sDEV sMNTP sOPT s1 s2
REAL_sDEV=`readlink -f $sDEV`
if [ "$1" = "$REAL_sDEV" ]; then
 return 1
fi
}

device_is_not_added2() {
cat /etc/fstab | while read LN; do
echo $LN | device_is_in_line $1
if [ ! $? = 0 ]; then return 1 ; fi
done
}

# ще се пробва да монтира даже и /dev/???, а не само дяловете му (напр. /dev/sdb1)
# това го правим, защото някой може да е решил да си форматира паметта без да направи
# таблица на дяловете (от която няма много смисъл точно в този случай)
#if echo "$DEVNAME" |  egrep -o "^/dev/[a-z]+[0-9]+" 2>/dev/null 1>/dev/null ; then

if [ "$ACTION" = "add" ] ; then
   if device_is_not_added2 $DEVNAME; then
	MNTP_dir=`basename "$DEVNAME"`
	MNTP="/media/usb_$MNTP_dir"
	error_msg_to_log "Изпълнява се: add_device_to_fstab \"$DEVNAME\" \"$MNTP\""
	add_device_to_fstab "$DEVNAME" "$MNTP"
   else 
       error_msg_to_log "Нарочно няма да се добави $DEVNAME в /etc/fstab, защото вече го има там"
   fi
elif [ "$ACTION" = "remove" ] ; then
	error_msg_to_log "Изпълнява се: remove_from_fstab3 \"$DEVNAME\""
	remove_from_fstab3 "$DEVNAME"
fi

#fi

/etc/udev/rules.d/usbflash.rules
Code:
# /etc/udev/rules.d/usbflash.rules
#
# Автоматично монтиране и демонтиране на USB паметите
# и автоматично вписване/изтриване от /etc/fstab.
#

SUBSYSTEM=="block", ACTION=="add", RUN+="/etc/vsupdfstab.udev"
SUBSYSTEM=="block", ACTION=="remove", RUN+="/etc/vsupdfstab.udev"
Quote:
IMPORTANT NOTES

You must remember to unmount device (or type command "sync") before attempting to remove removable media otherwise your data may be lost - Linux does not necessarily write data to the media immediately and as such a copy command may continue to happen in the background after it has returned on the command line. When you perform an unmount (or type command "sync"), any data that is still pending is written to the device.
Warning! Do not mount flash memory with option "sync"! This will decrase the life of memory!

Last edited by happyslacker; 07-17-2007 at 04:35 PM.
 
Old 10-24-2008, 04:08 AM   #5
koodough
LQ Newbie
 
Registered: Oct 2008
Posts: 1

Rep: Reputation: 0
Smile Working code example of udev auto executing

Here a working code example of udev auto executing my usb thumb drive

file:///etc/udev/rules.d/10.usbtest.rules

SUBSYSTEMS=="usb", ATTRS{serial}=="077B03B40D57", SYMLINK+="patriotUSB", RUN+="/usr/bin/beep"

Code that will beep when any USB is plugged in

SUBSYSTEMS=="usb", RUN+="/usr/bin/beep"

NOTE: you probably might not have the action beep so just download it from your package manager



I learned out do write this out correctly from this site
reactivated.net/writing_udev_rules.html
 
  


Reply

Tags
auto, backup, mount, script, sync, synchronize, udev rules, usb, usb automount, usb boot


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Using a usb thumb drive or flash drive as a swap partition. stevenjoseph Linux - Hardware 8 01-16-2012 01:09 PM
auto mounter vs /etc/fstab bondoq Linux - General 7 09-08-2006 04:41 PM
SuSe Linux 9.2 Freezes After Insertion of USB Flash Drive deepgrewal SUSE / openSUSE 13 04-13-2005 02:55 AM
USB drive using different partitions (usb key thumb drive) Arodef Linux - Hardware 0 08-04-2004 07:36 PM
auto mounter? lemay_jeff Linux - Newbie 3 06-15-2004 03:37 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop

All times are GMT -5. The time now is 08:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration