0

I'm trying to trigger a systemd service from udev when a USB storage device is inserted, and pass device info (the name) to my service which will feed it to a script.

I have created a udev rule(/etc/udev/rules.d/99-foo.rules):

ACTION=="add", KERNEL=="sd?", SUBSYSTEM=="block", ENV{ID_BUS}=="usb", TAG+="systemd", PROGRAM="/bin/systemd-escape -p %k", ENV{SYSTEMD_WANTS}+="bar@%c.service"

My systemd service (/etc/systemd/system/bar.service) is relatively basic:

[Unit]
Description=Script to do stuff with inserted device.

[Service]
Type=oneshot
ExecStart=/root/bin/usbhook %I

[Install]
WantedBy=multi-user.target

When I check /var/log/syslog on USB drive insertion, I see the drive identified by the machine, but my systemd service does not launch:

Jan  1 18:58:18 metapi kernel: [ 4542.190520] usb 1-1: New USB device found, idVendor=0781, idProduct=55a5, bcdDevice= 1.00
Jan  1 18:58:18 metapi kernel: [ 4542.190547] usb 1-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
Jan  1 18:58:18 metapi kernel: [ 4542.190560] usb 1-1: Product: Cruzer Snap
Jan  1 18:58:18 metapi kernel: [ 4542.190571] usb 1-1: Manufacturer: SanDisk
Jan  1 18:58:18 metapi kernel: [ 4542.190584] usb 1-1: SerialNumber: 4C530000180521234567
Jan  1 18:58:18 metapi kernel: [ 4542.211636] usb-storage 1-1:1.0: USB Mass Storage device detected
Jan  1 18:58:18 metapi kernel: [ 4542.225388] scsi host0: usb-storage 1-1:1.0
Jan  1 18:58:19 metapi mtp-probe: checking bus 1, device 22: "/sys/devices/platform/soc/20980000.usb/usb1/1-1"
Jan  1 18:58:19 metapi mtp-probe: bus: 1, device: 22 was not an MTP device
Jan  1 18:58:19 metapi mtp-probe: checking bus 1, device 22: "/sys/devices/platform/soc/20980000.usb/usb1/1-1"
Jan  1 18:58:19 metapi mtp-probe: bus: 1, device: 22 was not an MTP device
Jan  1 18:58:19 metapi kernel: [ 4543.230653] scsi 0:0:0:0: Direct-Access     SanDisk  Cruzer Snap      1.00 PQ: 0 ANSI: 6
Jan  1 18:58:19 metapi kernel: [ 4543.232528] sd 0:0:0:0: [sda] 250085376 512-byte logical blocks: (128 GB/119 GiB)
Jan  1 18:58:19 metapi kernel: [ 4543.234365] sd 0:0:0:0: [sda] Write Protect is off
Jan  1 18:58:19 metapi kernel: [ 4543.234392] sd 0:0:0:0: [sda] Mode Sense: 43 00 00 00
Jan  1 18:58:19 metapi kernel: [ 4543.235109] sd 0:0:0:0: [sda] Write cache: disabled, read cache: enabled, doesn't support DPO or FUA
Jan  1 18:58:19 metapi kernel: [ 4543.236616] sd 0:0:0:0: Attached scsi generic sg0 type 0
Jan  1 18:58:19 metapi kernel: [ 4543.266857]  sda: sda1
Jan  1 18:58:20 metapi kernel: [ 4543.274096] sd 0:0:0:0: [sda] Attached SCSI removable disk

Note that I believe my udev rules and systemd script should flow properly. If I replace my udev rules with ACTION=="add", KERNEL=="sd?", SUBSYSTEM=="block", ENV{ID_BUS}=="usb", TAG+="systemd", ENV{SYSTEMD_WANTS}+="bar.service", which doesn't pass an argument, the systemd script launches:

...
Jan  1 19:03:27 metapi kernel: [ 4850.274286] sd 0:0:0:0: [sda] 250085376 512-byte logical blocks: (128 GB/119 GiB)
Jan  1 19:03:27 metapi kernel: [ 4850.275523] sd 0:0:0:0: Attached scsi generic sg0 type 0
Jan  1 19:03:27 metapi kernel: [ 4850.276057] sd 0:0:0:0: [sda] Write Protect is off
Jan  1 19:03:27 metapi kernel: [ 4850.276081] sd 0:0:0:0: [sda] Mode Sense: 43 00 00 00
Jan  1 19:03:27 metapi kernel: [ 4850.276840] sd 0:0:0:0: [sda] Write cache: disabled, read cache: enabled, doesn't support DPO or FUA
Jan  1 19:03:27 metapi kernel: [ 4850.309435]  sda: sda1
Jan  1 19:03:27 metapi kernel: [ 4850.316819] sd 0:0:0:0: [sda] Attached SCSI removable disk
Jan  1 19:03:27 metapi systemd[1]: Starting Script to do stuff with inserted device....
0

That's not an argument. Services do not have arguments. That's an instance name for a template unit

… that you do not have. You are attempting to start an instance bar@sda.service, but you do not have a bar@.service template service unit to be instantiated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.