Re: [systemd-devel] Unable to get service to wait for a mount before starting

2024-11-10 Thread Andrei Borzenkov

10.11.2024 17:53, Adam Nielsen wrote:

network-online.target by itself does absolutely nothing. You are
expected to have some service that is ordered
Before=network-online.target and actually waits until network is online.
Standard network management applications that asynchronously configure
interfaces (NetworkManager, systemd-networkd) have matching services.
Some implementations (like SUSE wicked) are synchronous and
network-online.target is effectively noop. Nothing stops you from
creating your own service that performs custom checks.


I'm using systemd-networkd with a static configuration, i.e. my
*.network file contains:
.
   [Network]
   ...
   DNS=192.168.0.1  # local machine running BIND
   DNS=192.168.0.2  # second local machine running BIND

I assumed systemd and networkd would play nice together without any
special configuration but it seems this is not the case.



"Playing nice" is too subjective.

If you need network at some point during boot you need to ensure it. 
Read "man systemd-networkd-wait-online".


The primary reason for automount is to avoid this hard requirement.


I can see in the logs:

   systemd-networkd[505]: eth0: Link UP
   systemd-networkd[505]: eth0: Gained IPv6LL

Followed four seconds later by NFS mounts failing because they can't
resolve the hostname, and ntpd failing because it can't resolve the NTP
server hostname either.

systemd tries to restart ntpd nine seconds after that, and that time it
works.

Looking at the logs for systemd-resolved, it appears to be started at
the same second as the NFS mounts and ntpd, so I wonder if that's the
problem?  The network-online.target doesn't actually wait for
systemd-resolved to have properly set up DNS?



You seem to fundamentally misunderstand systemd architecture. *Target* 
does not and can not wait for anything except services configured in its 
After directives.


Re: [systemd-devel] Unable to get service to wait for a mount before starting

2024-11-10 Thread Adam Nielsen
> network-online.target by itself does absolutely nothing. You are 
> expected to have some service that is ordered 
> Before=network-online.target and actually waits until network is online. 
> Standard network management applications that asynchronously configure 
> interfaces (NetworkManager, systemd-networkd) have matching services. 
> Some implementations (like SUSE wicked) are synchronous and 
> network-online.target is effectively noop. Nothing stops you from 
> creating your own service that performs custom checks.

I'm using systemd-networkd with a static configuration, i.e. my
*.network file contains:

  [Network]
  ...
  DNS=192.168.0.1  # local machine running BIND
  DNS=192.168.0.2  # second local machine running BIND

I assumed systemd and networkd would play nice together without any
special configuration but it seems this is not the case.

I can see in the logs:

  systemd-networkd[505]: eth0: Link UP
  systemd-networkd[505]: eth0: Gained IPv6LL

Followed four seconds later by NFS mounts failing because they can't
resolve the hostname, and ntpd failing because it can't resolve the NTP
server hostname either.

systemd tries to restart ntpd nine seconds after that, and that time it
works.

Looking at the logs for systemd-resolved, it appears to be started at
the same second as the NFS mounts and ntpd, so I wonder if that's the
problem?  The network-online.target doesn't actually wait for
systemd-resolved to have properly set up DNS?

Cheers,
Adam.


Re: [systemd-devel] Unable to get service to wait for a mount before starting

2024-11-10 Thread Andrei Borzenkov

10.11.2024 15:20, Adam Nielsen wrote:


I'm not sure what you mean by not having a proper network-online.target
implementation.  I'm using the one distributed with systemd.  There
aren't any other versions or overrides for it in use.  The unit in
question specifies "After=network-online.target".



network-online.target by itself does absolutely nothing. You are 
expected to have some service that is ordered 
Before=network-online.target and actually waits until network is online. 
Standard network management applications that asynchronously configure 
interfaces (NetworkManager, systemd-networkd) have matching services. 
Some implementations (like SUSE wicked) are synchronous and 
network-online.target is effectively noop. Nothing stops you from 
creating your own service that performs custom checks.


Re: [systemd-devel] Unable to get service to wait for a mount before starting

2024-11-10 Thread Adam Nielsen
I'm replying to all three messages in one to avoid spamming the list:

On Tue, 29 Oct 2024 21:39:12 +0300
Andrei Borzenkov  wrote:

> > server:/path /mnt/server/path nfs4 
> > x-gvfs-hide,noauto,x-systemd.automount,timeo=15,soft,proto=tcp 0 0
> > /mnt/server/path/subfolder /mnt/share none 
> > bind,_netdev,x-systemd.automount,x-systemd.requires=/mnt/server/path 0 0
> >   
> 
> x-systemd.automount makes no sense. You service has implicit Requires 
> mnt-share.mount that will trigger mounting of both filesystems on boot 
> anyway and won't have any idle timeout.

This is just for consistency.  I have around 10 shares from the same
NFS server so it's easier for maintenance to keep the options all the
same for them all.  If I remove x-systemd.automount then some of the
shares don't mount when I first access them.

> Instead of guessing you should have read the logs. I cannot reproduce it 
> (without using NFS) and educated guess is - you do not have proper 
> network-online.target implementation, so your NFS is attempted to be 
> mounted before network setup is finished and this attempt fails.

You're right, I should have read the logs.  The problem seems to be
that systemd is trying to mount the NFS shares before it has configured
DNS, so it fails because the hostname doesn't resolve.  So I will have
to figure out how to get systemd to properly wait until the network is
configured before trying to mount NFS shares.

I'm not sure what you mean by not having a proper network-online.target
implementation.  I'm using the one distributed with systemd.  There
aren't any other versions or overrides for it in use.  The unit in
question specifies "After=network-online.target".

On Tue, 29 Oct 2024 09:09:31 +0200
Mantas Mikulėnas  wrote:

> Units are only restarted when they are already running and their daemon
> process fails; they're never "retried" when conditions or dependencies
> change when the unit never reached 'starting' at all.
> 
> If systemd itself is responsible for the mount (i.e. there is a fstab entry
> or a .mount unit), then your service can have a dependency on it, and can
> be ordered to wait until the mount succeeds (either using explicit
> Requires= & After= on the .mount, or the combined RequiresMountsFor= that
> you already have).

You are right and this does appear to work.  The fact that the shares
were all available when I logged in was what made me think it was
running the code before the shares were mounted, but now I see systemd
was unable to mount the shares because the network hadn't been set up
yet.  But by the time I logged in systemd had finished setting up the
network and the shares auto-mounted, hiding the problem.

> But if the mount just randomly shows up by some external means, that won't
> happen – the .mount unit didn't exist at boot time, the dependency wasn't
> met and that's the end of story. In this case, you have to do the
> *opposite* – if you want systemd to react to the event of the mount showing
> up, then your service has to be a dependency of the mount.
> 
> Even externally established mounts have virtual .mount units in systemd, so
> if you want your service to be started by the *event* of /mnt/share being
> mounted, [Install] WantedBy=mnt-share.mount would achieve that.

I presume in this case if I stopped the service, then
unmounted/remounted the share, the service would start again
automatically?  That's not what I particularly want.  I don't want the
service to start in response to the share appearing, I'd like the service
to start at boot time and if the share has to be mounted in order for
that to happen, then for it to do so.

On Tue, 29 Oct 2024 08:06:21 +
Henti Smith  wrote:

> I had a similar requirement and the way I implemented it was to create
> a systemd.mount for the NFS mount and then ensure that I have the mount in
> the "After" and add a " Requires" in the service that will depend on the
> mount. An example below:
> 
> After=network.target nfs.mount
> Requires=nfs.mount
> 
> I hope this helps.

Thanks for the suggestion!  It appears this is what the
RequiresMountFor= essentially does, so I should be able to get the same
result with that, once I figure out what's going on with the network.

Cheers,
Adam.


Re: [systemd-devel] Unable to get service to wait for a mount before starting

2024-10-29 Thread Andrei Borzenkov

29.10.2024 10:03, Adam Nielsen wrote:

I tried using RequiresMountsFor=/mnt/share however again, if it runs too
early in the boot process the mount is not there and the unit fails to
start, and is not retried (logs just say "Job myservice.service/start
failed with result 'dependency'.")


RequiresMountsFor only works if systemd knows about this mount point
which in practice usually means this mount point must be present in
/etc/fstab.


Thanks for the suggestion!  I do have it in /etc/fstab, although there
are two:

server:/path /mnt/server/path nfs4 
x-gvfs-hide,noauto,x-systemd.automount,timeo=15,soft,proto=tcp 0 0
/mnt/server/path/subfolder /mnt/share none 
bind,_netdev,x-systemd.automount,x-systemd.requires=/mnt/server/path 0 0



x-systemd.automount makes no sense. You service has implicit Requires 
mnt-share.mount that will trigger mounting of both filesystems on boot 
anyway and won't have any idle timeout.



This mounts an NFS path into a folder, then bind mounts a folder within
it to /mnt/share.  I then use RequiresMountsFor=/mnt/share, since it's
the second mount that has to happen before the systemd unit can start.

The mounts work (they always mount in the correct sequence) it's just
that RequiresMountsFor= doesn't seem to like this set up.



Instead of guessing you should have read the logs. I cannot reproduce it 
(without using NFS) and educated guess is - you do not have proper 
network-online.target implementation, so your NFS is attempted to be 
mounted before network setup is finished and this attempt fails.


But without logs we can speculate until the cows come home.


Re: [systemd-devel] Unable to get service to wait for a mount before starting

2024-10-29 Thread Henti Smith
On Tue, 29 Oct 2024 at 02:50, Adam Nielsen  wrote:

> Hi all,
>
> I'm having some problems getting a systemd unit to start during system
> boot.  Can anyone advise what I'm doing wrong?
>
> The service starts fine if I run `systemctl start myservice`, the issue
> is that it fails to start during boot.
>
> The service runs off a network share, so if it starts too early in the
> boot process the share is not mounted and it fails for that reason and
> systemd does not try to restart it later, despite the unit file saying
> to restart forever.
>
> I tried using ConditionPathExists=/mnt/share/something however because
> the path does not exist when the service is run during boot, the unit
> is never started and it is not retried later when the path does exist.
>
> I tried using RequiresMountsFor=/mnt/share however again, if it runs too
> early in the boot process the mount is not there and the unit fails to
> start, and is not retried (logs just say "Job myservice.service/start
> failed with result 'dependency'.")
>
> Currently I have to wait until the machine has booted, SSH into it, and
> then use systemctl to start the service.  I'm sure there must be a way
> to get this service to start automatically during boot, however I
> haven't yet worked it out.
>
> Any suggestions how I can indicate that the service should start after
> a specific path is mounted?
>

I had a similar requirement and the way I implemented it was to create
a systemd.mount for the NFS mount and then ensure that I have the mount in
the "After" and add a " Requires" in the service that will depend on the
mount. An example below:

After=network.target nfs.mount
Requires=nfs.mount

I hope this helps.

Kind regards
Henti


Re: [systemd-devel] Unable to get service to wait for a mount before starting

2024-10-29 Thread Mantas Mikulėnas
On Tue, Oct 29, 2024 at 4:50 AM Adam Nielsen  wrote:

> Hi all,
>
> I'm having some problems getting a systemd unit to start during system
> boot.  Can anyone advise what I'm doing wrong?
>
> The service starts fine if I run `systemctl start myservice`, the issue
> is that it fails to start during boot.
>
> The service runs off a network share, so if it starts too early in the
> boot process the share is not mounted and it fails for that reason and
> systemd does not try to restart it later, despite the unit file saying
> to restart forever.
>

Units are only restarted when they are already running and their daemon
process fails; they're never "retried" when conditions or dependencies
change when the unit never reached 'starting' at all.

If systemd itself is responsible for the mount (i.e. there is a fstab entry
or a .mount unit), then your service can have a dependency on it, and can
be ordered to wait until the mount succeeds (either using explicit
Requires= & After= on the .mount, or the combined RequiresMountsFor= that
you already have).

(The RequiresMountsFor would internally expand to [Unit]
Requires=mnt-share.mount After=mnt-share.mount, or something along those
lines.)

But if the mount just randomly shows up by some external means, that won't
happen – the .mount unit didn't exist at boot time, the dependency wasn't
met and that's the end of story. In this case, you have to do the
*opposite* – if you want systemd to react to the event of the mount showing
up, then your service has to be a dependency of the mount.

Even externally established mounts have virtual .mount units in systemd, so
if you want your service to be started by the *event* of /mnt/share being
mounted, [Install] WantedBy=mnt-share.mount would achieve that.

-- 
Mantas Mikulėnas


Re: [systemd-devel] Unable to get service to wait for a mount before starting

2024-10-29 Thread Adam Nielsen
> > I tried using RequiresMountsFor=/mnt/share however again, if it runs too
> > early in the boot process the mount is not there and the unit fails to
> > start, and is not retried (logs just say "Job myservice.service/start
> > failed with result 'dependency'.")
> 
> RequiresMountsFor only works if systemd knows about this mount point
> which in practice usually means this mount point must be present in
> /etc/fstab.

Thanks for the suggestion!  I do have it in /etc/fstab, although there
are two:

server:/path /mnt/server/path nfs4 
x-gvfs-hide,noauto,x-systemd.automount,timeo=15,soft,proto=tcp 0 0
/mnt/server/path/subfolder /mnt/share none 
bind,_netdev,x-systemd.automount,x-systemd.requires=/mnt/server/path 0 0

This mounts an NFS path into a folder, then bind mounts a folder within
it to /mnt/share.  I then use RequiresMountsFor=/mnt/share, since it's
the second mount that has to happen before the systemd unit can start.

The mounts work (they always mount in the correct sequence) it's just
that RequiresMountsFor= doesn't seem to like this set up.

Cheers,
Adam.


Re: [systemd-devel] Unable to get service to wait for a mount before starting

2024-10-28 Thread Andrei Borzenkov
On Tue, Oct 29, 2024 at 5:50 AM Adam Nielsen  wrote:
>
> I tried using RequiresMountsFor=/mnt/share however again, if it runs too
> early in the boot process the mount is not there and the unit fails to
> start, and is not retried (logs just say "Job myservice.service/start
> failed with result 'dependency'.")
>

RequiresMountsFor only works if systemd knows about this mount point
which in practice usually means this mount point must be present in
/etc/fstab.