2

I want to mount a drive with a specific label automatically everytime it gets pugged-in. I do not want to use any other tool than systemd as other units depend on the mount and must be started automatically.

What I have got so far:

/lib/systemd/system/mount-camera.mount

[Unit]
Description=the camera SDCard
StopWhenUnneeded=true
Before=copy-camera.service

[Mount]
What=LABEL=CAMERA
Where=/mount/camera
Type=vfat

[Install]
WantedBy=multi-user.target 

/lib/systemd/system/mount-camera.automount

[Unit]
Description=camera SDCard

[Automount]
Where=/mount/camera

[Install]
WantedBy=multi-user.target

This does successfully mount the drive if I cd into the mountpoint. However I want the drive to be mounted just because I insert it because there is a service that is started by the mount unit.

Is it possible to achieve this using systemd? If yes: How?

If it is impossible: is there a way I could do it with very few extra steps?

Note

I know that I could add a timer that cds into the mountpoint every x seconds - but this is note really elegant, is it?

4
  • udev does the mounting and systemd monitors udev automatically I think. Mar 23, 2018 at 12:23
  • @WinEunuuchs2Unix So there is no way to do it without writing an udev rule? Mar 23, 2018 at 12:40
  • 1
    Is there a particular reason you can't just add the device to the fstab? I have a removable drive bay that I pop drives in and out all the time and they automount based on what is in fstab.
    – rtaft
    Mar 23, 2018 at 13:09
  • @rtaft I thought this would be in conflict with my systemd mount unit. See my answer below. Mar 23, 2018 at 13:33

2 Answers 2

2

From reading man systemd.mount and man systemd.automount, it appears what you want is not possible systemd alone. Adding udev rules is the recommended solution.

1

As @MarkStosberg pointed out: it appears that this is not possible with systemd alone.

As @rtaft pointed out: using fstab is worth a try.

The solution is: Add an entry to the fstab and you are good to go. There are no conflicts, systemd resolves the files automatically.

Add to /etc/fstab:

LABEL=CAMERA                    /mount/camera auto user,rw,nofail 0 0

The service gets started as expected. I have kept both the .automount and the .mount unit files.

3
  • Does it mean you have the two systemd units mentioned in your question plus this fstab entry? Or do you now omit one or both of the systemd units?
    – PerlDuck
    Mar 23, 2018 at 14:43
  • Btw, I have a line LABEL=chunkA /mnt/chunkA ext4 noauto,x-systemd.automount,defaults 0 2 in my fstab (note the x-systemd.automount setting) and no systemd units at all to achieve automounting. But that's not a removable disk and I just did it for fun and out of curiousity.
    – PerlDuck
    Mar 23, 2018 at 14:48
  • 1
    @PerlDuck I made an edit regarding your first question. Also I need the systemd units because I automatically start another systemd service after mounting the drive. Mar 25, 2018 at 19:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .