Cannot make Udev rules without init system.

I want to make stuff happen without systemd being involved. My desired rule is something like this:

ACTION=="add", KERNEL=="tty*", RUN+="/bin/sh -c '/app/script.sh'"

but I can only obtain this functionality like this, with systemd being present:

ACTION=="add", KERNEL=="tty*", \
   TAG+="systemd", ENV{SYSTEMD_WANTS}="script@%k.service"

With the following systemd service:

[Unit]
Description=Run script when tty* device plugged in
BindsTo=dev-%i.device
After=dev-%i.device

[Service]
Type=oneshot
User=root
RemainAfterExit=yes
ExecStart=/app/script.sh

This is inconsistent with https://github.com/tmigone/balena-udev-examples/tree/master/usb-copy which seems to work without systemd. Any advice? Thanks!

Apologies, I believe I’ve solved the problem as it’s now working as expected. The problem was I was using UDEV=1 instead of UDEV=on.

The documentation suggests using UDEV=1 here, which is why I did so. https://www.balena.io/docs/reference/base-images/base-images/

Hmm. So the documentation suggests that 1, true and on are all evaluated as on. I will double check this later.

However, I am now having a separate issue.

The script I want to run is a curl script to a local container. Let’s say the container’s name is container1.

With systemd I can run:

curl -siL -X POST -H "Content-Type: application/json" -H "X-Api-Key: ${APIKEY}" http://container1/api/connection -d '{"command":"connect"}'

Without systemd I cannot run this, as it returns nothing. The base image documentation states:

Note: The new balenalib base images make sure udevd runs in its own network namespace,”

I believe this has something to do with it? This is really annoying, is there any way around this?

I see in the balena base image unshare is used to cause my problem here.

I didn’t realise udev functionality would be as simple as running udevd --daemon & at the start of my entry. I would say I’m happy with this solution :slight_smile:

Looks like you managed to get there in the end? Thanks for posting the results so anybody stuck in a similar situation might be helped!

A small note, 1, on and true will indeed all evaluate to truthy when setting env vars in the base image.