Auto connecting removable media

There are a number of interfaces that wont auto connect such as removable media, serial port, snapd, etc does this apply to snaps running as daemon’s. I would also like them to be able to run scripts when a user plugs in removable media, as is currently possible through udev rules.

Is it possible to configure this? ie:

  1. Autodetection of removable media
  2. Running scripts if removable media meets certain requirements.
  3. Autoconnection of specific interfaces

Another question is if they have been connected manually, does this persist between reboots?

1 Like

To get interfaces, that are not default connected when declared, to auto-connect you need to petition the store section of the forum with your use-case and rationale for the admins to vote on whether to allow auto-connection.

See: Process for aliases, auto-connections and tracks

But for development purposes, using devmode I should be able to connect to them? Are there any config in the gadget snap to get them to run some script on connection like traditional udev rules?

if you use --devmode you dont need to connect anything (well, it quietens the logs if you do (you get less “APPROVED” messages)), devmode installed snaps run completely unconfined.

okay, must be another issue, why I can’t see the devices on /media. Do you have any guidance on how the removable media can trigger a daemon service to start. in classic ubuntu the following works:

ACTION=="add", KERNEL=="sd[ab]1", TAG+="systemd", ENV{SYSTEMD_WANTS}="powersaving.service"1

Here is a link to someone just needing removable media to be automounted:

This is full tutorial

I misread this as a request for ‘removable-media’ to be auto-connected, but for others, @peter.milani’s question is about making ntfs-formatted USB drives automount in /media. AFAICT, there are missing packages in core for this to work.

perhaps …

but it would also need the udisks2 snap installed to actually do the mounting, nothing in core would auto-mount anything without udisks

Yes, @jdstrand I had expected the removable media plug to auto mount when removable media was inserted.

I am also trying to get scripts to run after said media was inserted (ie for copying files etc.). On a classic system that is done via a udev rule for systemd to call a service that copies files etc. That service also mounted the usb too.

Are the socket modes under the daemon definition the snapcraft.yaml a mechanism to trigger the daemon when the socket is available?

So effectively systemd and udev work pretty much the same between the classic and ubuntu core. If you want to start a script when removable media is plugged in:

Setup script to run as a daemon… Snap the script to run as a daemon using the command reference, likely you’ll want it to be one-shot or forking. Upon install this will create a service definition in
/etc/systemd/system/snap.my_snap_name.usb-mount.service assuming your daemon command is called usb-mount.

Insert new rule /etc/udev/rules.d to specify that systemd wants your service.

$cat /etc/udev/rules.d/99-usb-mount.rules
ACTION=="add", KERNEL=="sd[ab]", TAG+="systemd", ENV{SYSTEMD_WANTS}+="snap.my_snap_name.usb-mount.service"

Now when you plug in removable media and /dev/sda or /dev/sdb turn up your service will be called.