Por que minha regra do udev é executada três vezes?

2

Eu fiz uma pequena regra do udev que lança um script que faz um arquivo do meu diretório ~/Documents . Isso faz o trabalho, mas quando eu leio o arquivo de log, parece que o script de arquivamento é executado 3 vezes quando eu conecto minha chave.

Aqui está minha regra:

ACTION=="add", SUBSYSTEMS=="usb", DRIVERS=="usb", ATTRS{idVendor}=="0951", ATTRS{idProduct}=="1642", RUN+="/bin/sh /root/auto-archive" 

Aqui está meu script:

#! /bin/sh

sleep 5

# test if awesome is running
if ps aux | grep -v launch | grep -v grep | grep awesome
then
    echo awesome is running >> /home/purplepsycho/log
else
    # echo awesome not running another guy must be logged
    exit
fi

if mount | grep /media/usb
then
    echo /media/usb already mounted >> /home/purplepsycho/log
    exit
fi

# mount key
echo mounting... >> /home/purplepsycho/log
/usr/bin/sudo -u purplepsycho mount /media/usb

# test if mount have been succesful
if [ $? -ne 0 ]
then
    echo mount failed >> /home/purplepsycho/log
    exit
fi
# archive dir
ARC_DIR="/media/usb/archive"

# make directory
mkdir -p $ARC_DIR

# archive name
NAME=$(date +"archive-%Y-%m-%d.tgz")

# test if an archive already exists for today
if [ -f $ARC_DIR/$NAME ] 
then
    echo archive file already exists for today >> /home/purplepsycho/log
    exit
fi

# initialize log file
echo $NAME > $ARC_DIR/files.txt

# make the archive
tar -zcvf $ARC_DIR/$NAME /home/purplepsycho/Documents/* >> $ARC_DIR/files.txt

Alguma ideia? Obrigado.

--- Editar após o comentário do Sparhawk ---

Eu corro udevadm na minha chave:

udevadm info -a -p $(udevadm info -q path -n /dev/sbd)

Que deu:

looking at device '/devices/pci0000:00/0000:00:1d.7/usb1/1-1/1-1:1.0/host4/target4:0:0/4:0:0:0/block/sdb':
  KERNEL=="sdb"
  SUBSYSTEM=="block"
  DRIVER==""

looking at parent device '/devices/pci0000:00/0000:00:1d.7/usb1/1-1/1-1:1.0/host4/target4:0:0/4:0:0:0':
  KERNELS=="4:0:0:0"
  SUBSYSTEMS=="scsi"
  DRIVERS=="sd"

looking at parent device '/devices/pci0000:00/0000:00:1d.7/usb1/1-1/1-1:1.0/host4/target4:0:0':
  KERNELS=="target4:0:0"
  SUBSYSTEMS=="scsi"
  DRIVERS==""

looking at parent device '/devices/pci0000:00/0000:00:1d.7/usb1/1-1/1-1:1.0/host4':
  KERNELS=="host4"
  SUBSYSTEMS=="scsi"
  DRIVERS==""

looking at parent device '/devices/pci0000:00/0000:00:1d.7/usb1/1-1/1-1:1.0':
  KERNELS=="1-1:1.0"
  SUBSYSTEMS=="usb"
  DRIVERS=="usb-storage"

looking at parent device '/devices/pci0000:00/0000:00:1d.7/usb1/1-1':
  KERNELS=="1-1"
  SUBSYSTEMS=="usb"
  DRIVERS=="usb"
  ATTRS{idVendor}=="0951"
  ATTRS{idProduct}=="1642"

looking at parent device '/devices/pci0000:00/0000:00:1d.7/usb1':
  KERNELS=="usb1"
  SUBSYSTEMS=="usb"
  DRIVERS=="usb"
  ATTRS{idVendor}=="1d6b"
  ATTRS{idProduct}=="0002"

looking at parent device '/devices/pci0000:00/0000:00:1d.7':
  KERNELS=="0000:00:1d.7"
  SUBSYSTEMS=="pci"
  DRIVERS=="ehci-pci"
  ATTRS{irq}=="23"

looking at parent device '/devices/pci0000:00':
  KERNELS=="pci0000:00"
  SUBSYSTEMS==""
  DRIVERS==""

Eu tentei escrever regras apenas com base em:

  KERNELS=="1-1"
  SUBSYSTEMS=="usb"
  DRIVERS=="usb"
  ATTRS{idVendor}=="0951"
  ATTRS{idProduct}=="1642"

e

  KERNELS=="usb1"
  SUBSYSTEMS=="usb"
  DRIVERS=="usb"
  ATTRS{idVendor}=="1d6b"
  ATTRS{idProduct}=="0002"

O resultado é o mesmo: o script é chamado várias vezes ...

    
por purplepsycho 15.12.2015 / 21:50

1 resposta

2

O problema vem da sua regra: você só tenta combinar um pai do seu dispositivo, e não o próprio dispositivo ... consulte link para detalhes:

  • você deve corresponder ao dispositivo ( KERNEL= , SUBSYSTEM= , ATTR= ...),
  • e um de seus pais ( KERNELS= , SUBSYSTEMS= , ATTRS= ...) (observe o 'S' no final nos campos).

A regra corrigida é:

ACTION=="add", KERNEL=="sdb1", SUBSYSTEM=="block", ATTRS{vendor}=="Kingston", ATTRS{model}=="DT 101 G2", RUN+="/bin/sh /root/auto-archive"
    
por 03.01.2016 / 17:10

Tags