user2993813
user2993813

Reputation: 11

Systemd, how to mount a device at boot, but disable automount after boot

I do not seem to find a simple solution to the following problem:

I have a device listed in fstab, this should get mounted at boot. But if I manually unmount/remove the device after boot and if I present the device later on, systemd sees the device and automatically mounts it.

So how to prevent the latter (like pre-systemd behaviour). I can not use noauto in /etc/fstab since that will disable mounting at boot, which I still want to have.

There are some ways to workaround systemd for this problem. But I would like to see it fixed with using systemd.

After some digging it seems that the fstab systemd generator is creating device units and mount units. The generator seems to add implicit values to this generated device unit, one of them is a "Wants" to the mount unit. Causing a dependency between the mount and the device. How can I influence or override the systemd generators so that it does not create this "Wants" dependency between the device and the mount?

show dev-mapper-test.device |grep -i wants Wants=mnt-test.mount

But now the tricky part, even if you could override that "wants" then also starting at boot will be disabled...

Thanks

Upvotes: 0

Views: 1952

Answers (1)

kodu.cloud
kodu.cloud

Reputation: 1600

You can write systemd unit with Type=oneshot.

Type=oneshot: this is useful for scripts that do a single job and then exit.

Example:

[Unit]
Description=one_mount
After=network.target

[Service]
Type=oneshot
ExecStart=/usr/bin/mount /dev/partition /path/to/point
ExecStop=/usr/bin/umount /path/to/point

[Install]
WantedBy=multi-user.target

Upvotes: 1

Related Questions