You are not logged in.

#1 2021-08-03 20:22:18

miroslec
Member
From: Lisbon
Registered: 2013-03-05
Posts: 10

Automount USB and run systemd services until the drive is unmounted

I've been searching for an elegant way of automating a very tedious process.

I've managed to start a systemd service when I plug in a USB external drive by editing the service and adding some udev rules (more below) but I've been struggling with other parts of the automation.

This is the workflow I want to achieve:

  1. Plug in the USB Disk (single partition)

  2. sdX1 gets automatically mounted to /mnt/data with some mounting options

  3. Some systemd services are started (which interact with the files in the mountpoint.

  4. I manually issue a command to unmount the drive.

  5. Services get automatically terminated.

I've been experimenting with Transmission, so I have the following service:

[Unit]
Description=Transmission BitTorrent Daemon
After=network.target
After=mnt-data.mount

[Service]
User=transmission
Type=notify
ExecStart=/usr/bin/transmission-daemon -f --log-error
ExecReload=/bin/kill -s HUP $MAINPID
NoNewPrivileges=true

[Install]
WantedBy=multi-user.target
WantedBy=mnt-data.mount

And this is my udev rules file:

SUBSYSTEMS=="usb", ATTRS{idProduct}=="1153", ACTION=="add", ENV{SYSTEMD_WANTS}=="transmission.service"

I managed to get the service started when the drive gets mounted, but it's the automount part, and killing the services after the unmount that's eluding me.

I appreciate your help.

Offline

Board footer

Powered by FluxBB