You are not logged in.

#1 2012-09-22 20:01:53

Thme
Member
From: Raleigh NC
Registered: 2012-01-22
Posts: 105

[Solved]Automating a script for a usb device on systemd?

Solution:See 3rd post by 65kid.
   Ok I'm trying to learn how to automate the execution of a script I wrote to run whenever a specific usb device is plugged in. In this case a usb flash drive with a specific partition identified though UUID following the path /dev/disk/by-uuid/specified-partion's-uuid.
   Basically the script uses rsync to update a snapshot of my system which I made bootable on this device. following the guide given in the wiki on full system backup using rsync I edited the --exclude options to ignore the contents of my /boot/ /etc/fstab and /etc/mkinitcpio.conf since the fstab entries, hooks array and initramfs and kernel images generated with them needed to be changed so as to make this snapshot bootable and adapted for use on the usb stick.
the ultimate goal is to have a bootable snapshot the can be updated with whatever changes have been made with my system(i.e. pacman -Syu updates,config changes etc...) while leaving out the important things needed to have the usb still bootable afterwards. I've already succeeded at getting the script to work but I have to mount it manually then execute the script myself and I know that it works afterwards as I can still boot from the stick afterwards.
   This is not a question about rsync (which is why the script isn't posted here), to repeat myself, It's about how to execute ANY custom script whenever a specific device is plugged in. More specifically when a storage device with a certain uuid is detected after the device has been plugged in. I believe it may be different for running pure systemd and so that was the reasoning for having it mentioned in the subjects title(if I'm wrong then correct my understanding). If anything a few pointers on where to look would be appreciated. I've been googling and reading the wiki a bit with nothing specific on how to approach this. If an example of the script is requested I'll post it but it's not relevent to my question.
   For a general veiw of my setup regarding external storage volumes I do not automount anything as I do not use all the things I plug in in the same manner. Some of the stuff I do requires them to be unmounted or  mounted to different mount points(specified by me). The script handles the mounting part by itself just fine. The idea I have is to run it automatically when the flash drive is present. Any suggestions?

Last edited by Thme (2012-12-29 00:06:56)


"Hidden are the ways for those who pass by, for light is perished and darkness comes into being." Nephthys:
Ancient Egyptian Coffin Texts

Offline

#2 2012-09-22 20:37:25

WonderWoofy
Member
From: Los Gatos, CA
Registered: 2012-05-19
Posts: 8,414

Re: [Solved]Automating a script for a usb device on systemd?

udev rules


Edit: read this http://www.reactivated.net/writing_udev_rules.html

Last edited by WonderWoofy (2012-09-22 20:40:07)

Offline

#3 2012-09-22 20:38:02

65kid
Member
From: Germany
Registered: 2011-01-26
Posts: 663

Re: [Solved]Automating a script for a usb device on systemd?

if you mount the device from the script you could do something like this:

[Unit]
After=dev-....device

[Service]
ExecStart=/my/script.sh

[Install]
WantedBy=dev-....device
systemctl enable script.service

the service should then be started when the device is plugged in.

to get the unit name of the partition, check the output of:

systemctl --all --full -t device

Offline

#4 2012-09-22 20:47:12

65kid
Member
From: Germany
Registered: 2011-01-26
Posts: 663

Re: [Solved]Automating a script for a usb device on systemd?

WonderWoofy wrote:

that reminds me, you can use the SYSTEMD_WANTS tag in udev to start the service when the device is plugged in. this is documented in "man systemd.device" (and according to the man page this is actually the recommended method).

it is however not supported to start long running processes via RUN+=.

Offline

#5 2012-09-22 20:55:34

WonderWoofy
Member
From: Los Gatos, CA
Registered: 2012-05-19
Posts: 8,414

Re: [Solved]Automating a script for a usb device on systemd?

That is a good point... it would be an awfully long process.  I didn't really think that one over very well now did I?

Offline

#6 2012-09-22 23:28:50

Thme
Member
From: Raleigh NC
Registered: 2012-01-22
Posts: 105

Re: [Solved]Automating a script for a usb device on systemd?

Thanks for the feedback. I wasn't sure if the method described for systemd was what I was looking for but now I'm starting to make more sense of how systemd works. A service file makes so much sense too as it was essentially what I wanted to learn for other purposes as well. when I have it up and running ill post it and mark this as solved. and the tag in udev makes a lot of sense as well. I'm considering adding to the wiki on the rsync method for updating a bootable snapshot on thumbdrives. This would make the setup self working afterwards for those with large enough flash drives.


"Hidden are the ways for those who pass by, for light is perished and darkness comes into being." Nephthys:
Ancient Egyptian Coffin Texts

Offline

#7 2012-09-23 00:55:06

cfr
Member
From: Cymru
Registered: 2011-11-27
Posts: 7,148

Re: [Solved]Automating a script for a usb device on systemd?

I seem to remember a developer (falcondy?) saying that udev rules shouldn't even be used for mounting so I imagine running a full back up as well would amount to outright exploitation  wink .


CLI Paste | How To Ask Questions

Arch Linux | x86_64 | GPT | EFI boot | refind | stub loader | systemd | LVM2 on LUKS
Lenovo x270 | Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz | Intel Wireless 8265/8275 | US keyboard w/ Euro | 512G NVMe INTEL SSDPEKKF512G7L

Offline

#8 2012-09-23 06:22:03

Awebb
Member
Registered: 2010-05-06
Posts: 6,598

Re: [Solved]Automating a script for a usb device on systemd?

You "seem to remember" has FUD potential, could you care to find the post again?

Offline

#9 2012-09-23 12:15:54

65kid
Member
From: Germany
Registered: 2011-01-26
Posts: 663

Re: [Solved]Automating a script for a usb device on systemd?

Awebb wrote:

You "seem to remember" has FUD potential, could you care to find the post again?

he is probably referring to this: https://bbs.archlinux.org/viewtopic.php … 2#p1113682
imho this is way different, the SYSTEMD_WANTS tag is completely ok to use. yes, he may be mounting the drive from the script, but imho he is not "exploiting" anything here. I personally don't see a much more elegant way to do this.

these automount udev rules were however crappy and hackish. They were created by people who are like "but udisks is bloat!!1!" and whoever wrote them didn't really seem to know what he was doing because, for example, unmounting a device after it was unplugged from the system is completely non-sense.

Offline

#10 2012-09-23 14:12:40

cfr
Member
From: Cymru
Registered: 2011-11-27
Posts: 7,148

Re: [Solved]Automating a script for a usb device on systemd?

Yes. That was it - thank you.

And I had in mind the inclusion of the backup itself in the udev rule - not the use of the SYSTEMD_WANTS tag. (I don't know anything about the latter but others clearly do.)


CLI Paste | How To Ask Questions

Arch Linux | x86_64 | GPT | EFI boot | refind | stub loader | systemd | LVM2 on LUKS
Lenovo x270 | Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz | Intel Wireless 8265/8275 | US keyboard w/ Euro | 512G NVMe INTEL SSDPEKKF512G7L

Offline

#11 2012-09-23 15:26:59

lilsirecho
Veteran
Registered: 2003-10-24
Posts: 5,000

Re: [Solved]Automating a script for a usb device on systemd?

Perhaps ldm package will be of interest to you...............

It mounts by uuid any usb device inserted.


Prediction...This year will be a very odd year!
Hard work does not kill people but why risk it: Charlie Mccarthy
A man is not complete until he is married..then..he is finished.
When ALL is lost, what can be found? Even bytes get lonely for a little bit!     X-ray confirms Iam spineless!

Offline

Board footer

Powered by FluxBB