Network dependent services

From Gentoo Wiki
Jump to:navigation Jump to:search

Services like netmount or fetchmail which depend on an available network connection could be started/stopped with the following setup:[1]

Precondition

Precondition is that network management is done with dhcpcd.

Implementation

Get the patch for dhcpcd:[2]

root #mkdir -p /etc/portage/patches/net-misc/dhcpcd-6.9.0
root #wget https://522206.bugs.gentoo.org/attachment.cgi?id=402858 -P /etc/portage/patches/net-misc/dhcpcd-6.9.0

Get the patch for openrc:[3]

root #mkdir -p /etc/portage/patches/sys-apps/openrc-0.13.11
root #wget https://bugs.gentoo.org/attachment.cgi?id=402856 -P /etc/portage/patches/sys-apps/openrc-0.13.11

Re-emerge both dhcpcd and openrc:

root #emerge -1avt =net-misc/dhcpcd-6.6.7 =sys-apps/openrc-0.13.11

Add one line start_inactive=true to the init script:[4]

root #patch /etc/init.d/dhcpcd < attachment.cgi?id=384410

Restart dhcpcd:

root #/etc/init.d/dhcpcd restart

Later versions than the presently stable of net-misc/dhcpcd and sys-apps/openrc have not been tested with these patches. It does not work for sys-apps/openrc-0.16.4.

Result

Services having "need net" in their init.d scripts like fetchmail would then start after dhcpcd is started.

FILE /etc/init.d/fetchmail
#!/sbin/runscript

piddir=${pid_dir:-/var/run/fetchmail}
pid_file=${piddir}/${RC_SVCNAME}.pid
rcfile=/etc/${RC_SVCNAME}rc

depend() {
        need net
        use mta
}
root #eselect rc start fetchmail
Starting init script
dhcpcd          * Starting DHCP Client Daemon ...
fetchmail       * WARNING: fetchmail is scheduled to start when dhcpcd has started            [ ok ]

They will be stopped when dhcpcd turns inactive and will be restarted when dhcpcd is back.

user $rc-config show default
  dhcpcd                    [inactive]
  fetchmail                 [stopped]

This should be sufficient for most end user computers. For more complex requirements in dependency behaviour see OpenRC#Dependency_behaviour.


References