Skip to contentSkip to conversation

dhcp: add systemd service file

PATCH New Eric Le Bihan 3 participants 8 messages Sep 15, 2014 – Oct 29, 2014

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 dhcp: add systemd service file

Add a systemd service file to start dhcpd.

The network interfaces to listen on can be set via the variable INTERFACES in /etc/default/dhcpd.conf. Example:

INTERFACES="eth0 eth1"

Also install a tmpfiles.d file to create /var/lib/dhcp/dhcpd.leases (needed at dhcpd start-up).

---
 package/dhcp/dhcp.mk       | 15 +++++++++++++++
 package/dhcp/dhcpd.service | 13 +++++++++++++
 2 files changed, 28 insertions(+)
 create mode 100644 package/dhcp/dhcpd.service
package/dhcp/dhcp.mk +15 -0
index 9251185..1b914a8 100644
@@ -74,6 +74,21 @@ define DHCP_INSTALL_INIT_SYSV
 		$(TARGET_DIR)/etc/init.d/S80dhcp-relay
 endef
 
+define DHCP_INSTALL_INIT_SYSTEMD
+	$(INSTALL) -D -m 644 package/dhcp/dhcpd.service \
+		$(TARGET_DIR)/lib/systemd/system/dhcpd.service
+
+	mkdir -p $(TARGET_DIR)/etc/systemd/system/multi-user.target.wants
+
+	ln -sf ../../../../lib/systemd/system/dhcpd.service \
+		$(TARGET_DIR)/etc/systemd/system/multi-user.target.wants/dhcpd.service
+
+	echo "d /var/lib/dhcp 0755 - - - -" > \
+		$(TARGET_DIR)/usr/lib/tmpfiles.d/dhcpd.conf
+	echo "f /var/lib/dhcp/dhcpd.leases - - - - -" >> \
+		$(TARGET_DIR)/usr/lib/tmpfiles.d/dhcpd.conf
+endef
+
 define DHCP_INSTALL_TARGET_CMDS
 	$(DHCP_INSTALL_RELAY)
 	$(DHCP_INSTALL_SERVER)
package/dhcp/dhcpd.service new file +13 -0
new file mode 100644
index 0000000..7b265cb
@@ -0,0 +1,13 @@
+[Unit]
+Description=DHCP server
+After=network.target
+
+[Service]
+Type=forking
+PIDFile=/run/dhcpd.pid
+ExecStart=/usr/sbin/dhcpd -q -pf /run/dhcpd.pid $INTERFACES
+KillSignal=SIGINT
+EnvironmentFile=/etc/default/dhcpd.conf
+
+[Install]
+WantedBy=multi-user.target
Signature · 1 line
1.9.3
#1 lore raw

PATCH 1/1 dhcp: add systemd service file

Eric, All,

On 2014-09-15 23:04 +0200, Eric Le Bihan spake thusly:

Quoted 28 lines

Add a systemd service file to start dhcpd.

The network interfaces to listen on can be set via the variable INTERFACES in /etc/default/dhcpd.conf. Example:

INTERFACES="eth0 eth1"

Also install a tmpfiles.d file to create /var/lib/dhcp/dhcpd.leases (needed at dhcpd start-up).

---
 package/dhcp/dhcp.mk       | 15 +++++++++++++++
 package/dhcp/dhcpd.service | 13 +++++++++++++
 2 files changed, 28 insertions(+)
 create mode 100644 package/dhcp/dhcpd.service
package/dhcp/dhcp.mk +3 -0
index 9251185..1b914a8 100644
@@ -74,6 +74,21 @@ define DHCP_INSTALL_INIT_SYSV
 		$(TARGET_DIR)/etc/init.d/S80dhcp-relay
 endef
 
+define DHCP_INSTALL_INIT_SYSTEMD
+	$(INSTALL) -D -m 644 package/dhcp/dhcpd.service \
+		$(TARGET_DIR)/lib/systemd/system/dhcpd.service

package/dhcp/dhcp.mk define DHCP_INSTALL_INIT_SYSV · line 89

Quoted 10 lines
package/dhcp/dhcp.mk +10 -0
+
+	mkdir -p $(TARGET_DIR)/etc/systemd/system/multi-user.target.wants
+
+	ln -sf ../../../../lib/systemd/system/dhcpd.service \
+		$(TARGET_DIR)/etc/systemd/system/multi-user.target.wants/dhcpd.service
+
+	echo "d /var/lib/dhcp 0755 - - - -" > \
+		$(TARGET_DIR)/usr/lib/tmpfiles.d/dhcpd.conf
+	echo "f /var/lib/dhcp/dhcpd.leases - - - - -" >> \
+		$(TARGET_DIR)/usr/lib/tmpfiles.d/dhcpd.conf

I'm not sure to understand those two. Are they needed on the target?

They suspiciously look like our 'makedev' syntax. Does systemd recognise this too?

Regards,
Yann E. MORIN.

Quoted 31 lines
package/dhcp/dhcp.mk +2 -0
+endef
+
 define DHCP_INSTALL_TARGET_CMDS
 	$(DHCP_INSTALL_RELAY)
 	$(DHCP_INSTALL_SERVER)
package/dhcp/dhcpd.service new file +13 -0
new file mode 100644
index 0000000..7b265cb
@@ -0,0 +1,13 @@
+[Unit]
+Description=DHCP server
+After=network.target
+
+[Service]
+Type=forking
+PIDFile=/run/dhcpd.pid
+ExecStart=/usr/sbin/dhcpd -q -pf /run/dhcpd.pid $INTERFACES
+KillSignal=SIGINT
+EnvironmentFile=/etc/default/dhcpd.conf
+
+[Install]
+WantedBy=multi-user.target
Signature · 6 lines
1.9.3

_______________________________________________
buildroot mailing list
buildroot at busybox.net
http://lists.busybox.net/mailman/listinfo/buildroot
Signature · 6 lines
.-----------------.--------------------.------------------.--------------------.
|  Yann E. MORIN  | Real-Time Embedded | /"\ ASCII RIBBON | Erics' conspiracy: |
| +33 662 376 056 | Software  Designer | \ / CAMPAIGN     |  ___               |
| +33 223 225 172 `------------.-------:  X  AGAINST      |  \e/  There is no  |
| http://ymorin.is-a-geek.org/ | _/*\_ | / \ HTML MAIL    |   v   conspiracy.  |
'------------------------------^-------^------------------^--------------------'
#2 lore raw

PATCH 1/1 dhcp: add systemd service file

Hi!

Quoted 44 lines · Yann E. MORIN

Eric, All,

On 2014-09-15 23:04 +0200, Eric Le Bihan spake thusly:

Quoted 28 lines

Add a systemd service file to start dhcpd.

The network interfaces to listen on can be set via the variable INTERFACES in /etc/default/dhcpd.conf. Example:

INTERFACES="eth0 eth1"

Also install a tmpfiles.d file to create /var/lib/dhcp/dhcpd.leases (needed at dhcpd start-up).

---
 package/dhcp/dhcp.mk       | 15 +++++++++++++++
 package/dhcp/dhcpd.service | 13 +++++++++++++
 2 files changed, 28 insertions(+)
 create mode 100644 package/dhcp/dhcpd.service
package/dhcp/dhcp.mk +3 -0
index 9251185..1b914a8 100644
@@ -74,6 +74,21 @@ define DHCP_INSTALL_INIT_SYSV
 		$(TARGET_DIR)/etc/init.d/S80dhcp-relay
 endef
 
+define DHCP_INSTALL_INIT_SYSTEMD
+	$(INSTALL) -D -m 644 package/dhcp/dhcpd.service \
+		$(TARGET_DIR)/lib/systemd/system/dhcpd.service

package/dhcp/dhcp.mk define DHCP_INSTALL_INIT_SYSV · line 89

Quoted 10 lines
package/dhcp/dhcp.mk +10 -0
+
+	mkdir -p $(TARGET_DIR)/etc/systemd/system/multi-user.target.wants
+
+	ln -sf ../../../../lib/systemd/system/dhcpd.service \
+		$(TARGET_DIR)/etc/systemd/system/multi-user.target.wants/dhcpd.service
+
+	echo "d /var/lib/dhcp 0755 - - - -" > \
+		$(TARGET_DIR)/usr/lib/tmpfiles.d/dhcpd.conf
+	echo "f /var/lib/dhcp/dhcpd.leases - - - - -" >> \
+		$(TARGET_DIR)/usr/lib/tmpfiles.d/dhcpd.conf

I'm not sure to understand those two. Are they needed on the target?

Quoted 2 lines

They suspiciously look like our 'makedev' syntax. Does systemd recognise this too?

Among the zillion of tools provided by systemd, there is 'systemd-tmpfiles' [1]. This program creates, deletes, and cleans up volatile and temporary files and directories based on configuration files stored in /usr/lib/tmpfiles.d and /etc/tmpfiles.d.

It is true that these files follow a syntax similar to makedev [2]. But unlike 'makedev' which sets the proper access rights for / entries at build time, 'systemd-tmpfiles' creates the files at runtime if they are missing.

In the current default skeleton used by Buildroot, parts of /var are tmpfs-based and thus get cleaned at each reboot. 'systemd-tmpfiles' can recreate them.

For some reasons, when using systemd, dhcpd complains at start-up about /var/lib/dhcp/dhcpd.leases not being accessible. So the Makefile installs a file named /usr/lib/tmpfiles.d/dhcp.conf, which contains "instructions" for creating the missing file with the proper access rights.

I know that a package named 'foo' can add system users via FOO_USERS, thanks to 'mkusers'. Can the same be done with 'makedev'? This would allow a package to create files with the proper owner and access rights without having to provide additional files.

Side note: since version 215, systemd provides systemd-sysusers, which creates system users and groups in /etc/passwd and /etc/group based on the configuration files in /usr/lib/sysusers.d/. It is said to be useful for systems where /etc can be "reset" to a default version. I still haven't found a useful use case for it in an embedded system, though.

[1] http://www.freedesktop.org/software/systemd/man/systemd-tmpfiles.html
[2] http://www.freedesktop.org/software/systemd/man/tmpfiles.d.html

Best regards,
ELB

#3 lore raw

PATCH 1/1 dhcp: add systemd service file

Eric, All,

On 2014-09-22 12:52 +0200, Eric Le Bihan spake thusly:

Quoted 3 lines · Yann E. MORIN
Quoted 2 lines

On 2014-09-15 23:04 +0200, Eric Le Bihan spake thusly:

Quoted 1 line

Add a systemd service file to start dhcpd.

[--SNIP--]

Quoted 19 lines
Quoted 9 lines

package/dhcp/dhcp.mk define DHCP_INSTALL_INIT_SYSV · line 89

Quoted 4 lines
package/dhcp/dhcp.mk +4 -0
+	echo "d /var/lib/dhcp 0755 - - - -" > \
+		$(TARGET_DIR)/usr/lib/tmpfiles.d/dhcpd.conf
+	echo "f /var/lib/dhcp/dhcpd.leases - - - - -" >> \
+		$(TARGET_DIR)/usr/lib/tmpfiles.d/dhcpd.conf

I'm not sure to understand those two. Are they needed on the target?

They suspiciously look like our 'makedev' syntax. Does systemd recognise this too?

Among the zillion of tools provided by systemd, there is 'systemd-tmpfiles' [1]. This program creates, deletes, and cleans up volatile and temporary files and directories based on configuration files stored in /usr/lib/tmpfiles.d and /etc/tmpfiles.d.

It is true that these files follow a syntax similar to makedev [2]. But unlike 'makedev' which sets the proper access rights for / entries at build time, 'systemd-tmpfiles' creates the files at runtime if they are missing.

Quoted 8 lines

In the current default skeleton used by Buildroot, parts of /var are tmpfs-based and thus get cleaned at each reboot. 'systemd-tmpfiles' can recreate them.

For some reasons, when using systemd, dhcpd complains at start-up about /var/lib/dhcp/dhcpd.leases not being accessible. So the Makefile installs a file named /usr/lib/tmpfiles.d/dhcp.conf, which contains "instructions" for creating the missing file with the proper access rights.

Thanks for the thorough explanations!

Quoted 2 lines

I know that a package named 'foo' can add system users via FOO_USERS, thanks to 'mkusers'. Can the same be done with 'makedev'?

Yes, it is possible, see:
http://nightly.buildroot.org/#_infrastructure_for_packages_with_specific_build_systems
and:
http://nightly.buildroot.org/#makedev-syntax

You'd need something like:

    define LIBFOO_PERMISSIONS
        /var/lib/dhcp d 0755 0 0 - - - - -
        /var/lib/dhcp/dhcpd.leases f 0600 0 0 - - - - -
    endef
Quoted 3 lines

This would allow a package
to create files with the proper owner and access rights without having to provide additional files.

Problem is /var/lib/dhcp/dhcpd.leases should be on a RW filesystem, and the only one Buildroot gurantees to be RW is /tmp (which is a tmpfs).

But /var/lib does not point to a subdir in /tmp so whether we use systemd's feature to create missing files, or the makedev above, we'd still need to make /var/lib/dhcp a symling to somewhere in /tmp.

Regards,
Yann E. MORIN.

Quoted 11 lines

Side note: since version 215, systemd provides systemd-sysusers, which creates system users and groups in /etc/passwd and /etc/group based on the configuration files in /usr/lib/sysusers.d/. It is said to be useful for systems where /etc can be "reset" to a default version. I still haven't found a useful use case for it in an embedded system, though.

[1] http://www.freedesktop.org/software/systemd/man/systemd-tmpfiles.html
[2] http://www.freedesktop.org/software/systemd/man/tmpfiles.d.html

Best regards,
ELB

Signature · 6 lines
.-----------------.--------------------.------------------.--------------------.
|  Yann E. MORIN  | Real-Time Embedded | /"\ ASCII RIBBON | Erics' conspiracy: |
| +33 662 376 056 | Software  Designer | \ / CAMPAIGN     |  ___               |
| +33 223 225 172 `------------.-------:  X  AGAINST      |  \e/  There is no  |
| http://ymorin.is-a-geek.org/ | _/*\_ | / \ HTML MAIL    |   v   conspiracy.  |
'------------------------------^-------^------------------^--------------------'
#4 lore raw

PATCH 1/1 dhcp: add systemd service file

Quoted 56 lines · Yann E. MORIN

Eric, All,

On 2014-09-22 12:52 +0200, Eric Le Bihan spake thusly:

Quoted 3 lines · Yann E. MORIN
Quoted 2 lines

On 2014-09-15 23:04 +0200, Eric Le Bihan spake thusly:

Quoted 1 line

Add a systemd service file to start dhcpd.

[--SNIP--]

Quoted 19 lines
Quoted 9 lines

package/dhcp/dhcp.mk define DHCP_INSTALL_INIT_SYSV · line 89

Quoted 4 lines
package/dhcp/dhcp.mk +4 -0
+	echo "d /var/lib/dhcp 0755 - - - -" > \
+		$(TARGET_DIR)/usr/lib/tmpfiles.d/dhcpd.conf
+	echo "f /var/lib/dhcp/dhcpd.leases - - - - -" >> \
+		$(TARGET_DIR)/usr/lib/tmpfiles.d/dhcpd.conf

I'm not sure to understand those two. Are they needed on the target?

They suspiciously look like our 'makedev' syntax. Does systemd recognise this too?

Among the zillion of tools provided by systemd, there is 'systemd-tmpfiles' [1]. This program creates, deletes, and cleans up volatile and temporary files and directories based on configuration files stored in /usr/lib/tmpfiles.d and /etc/tmpfiles.d.

It is true that these files follow a syntax similar to makedev [2]. But unlike 'makedev' which sets the proper access rights for / entries at build time, 'systemd-tmpfiles' creates the files at runtime if they are missing.

Quoted 8 lines

In the current default skeleton used by Buildroot, parts of /var are tmpfs-based and thus get cleaned at each reboot. 'systemd-tmpfiles' can recreate them.

For some reasons, when using systemd, dhcpd complains at start-up about /var/lib/dhcp/dhcpd.leases not being accessible. So the Makefile installs a file named /usr/lib/tmpfiles.d/dhcp.conf, which contains "instructions" for creating the missing file with the proper access rights.

Thanks for the thorough explanations!

Quoted 2 lines

I know that a package named 'foo' can add system users via FOO_USERS, thanks to 'mkusers'. Can the same be done with 'makedev'?

Yes, it is possible, see:
http://nightly.buildroot.org/#_infrastructure_for_packages_with_specific_build_systems
and:
http://nightly.buildroot.org/#makedev-syntax

You'd need something like:

    define LIBFOO_PERMISSIONS
        /var/lib/dhcp d 0755 0 0 - - - - -
        /var/lib/dhcp/dhcpd.leases f 0600 0 0 - - - - -
    endef
Quoted 3 lines

This would allow a package
to create files with the proper owner and access rights without having to provide additional files.

Quoted 7 lines

Problem is /var/lib/dhcp/dhcpd.leases should be on a RW filesystem, and the only one Buildroot gurantees to be RW is /tmp (which is a tmpfs).

But /var/lib does not point to a subdir in /tmp so whether we use systemd's feature to create missing files, or the makedev above, we'd still need to make /var/lib/dhcp a symling to somewhere in /tmp.

I re-read dhcp.mk: we can see that, when installing the program in the target directory, /var/lib/dhcp is created as a symlink to /tmp, which is good. Besides the Busybox/SysV init script S80dhcp-server creates /var/lib/dhcp/dhcpd.leases if it does not exist. Great!

So the problem is already solved when using Busybox/SysV and for systemd, it is solved via the proposed patch :-)

Best regards
ELB

#5 lore raw

PATCH 1/1 dhcp: add systemd service file

In reply to Eric Le Bihan

Dear Eric Le Bihan,

Quoted 10 lines · Eric Le Bihan

Add a systemd service file to start dhcpd.

The network interfaces to listen on can be set via the variable INTERFACES in /etc/default/dhcpd.conf. Example:

INTERFACES="eth0 eth1"

Also install a tmpfiles.d file to create /var/lib/dhcp/dhcpd.leases (needed at dhcpd start-up).

Quoted 6 lines
---
 package/dhcp/dhcp.mk       | 15 +++++++++++++++
 package/dhcp/dhcpd.service | 13 +++++++++++++
 2 files changed, 28 insertions(+)
 create mode 100644 package/dhcp/dhcpd.service

What is the status of this patch? Yann made some comments, which apparently you agreed on. Is a new version of the patch needed?

Thanks,

Thomas

Signature · 3 lines
Thomas Petazzoni, CTO, Free Electrons
Embedded Linux, Kernel and Android engineering
http://free-electrons.com
#6 lore raw

PATCH 1/1 dhcp: add systemd service file

Hi!

Quoted 20 lines · Thomas Petazzoni

Dear Eric Le Bihan,

Quoted 10 lines · Eric Le Bihan

Add a systemd service file to start dhcpd.

The network interfaces to listen on can be set via the variable INTERFACES in /etc/default/dhcpd.conf. Example:

INTERFACES="eth0 eth1"

Also install a tmpfiles.d file to create /var/lib/dhcp/dhcpd.leases (needed at dhcpd start-up).

Quoted 6 lines
---
 package/dhcp/dhcp.mk       | 15 +++++++++++++++
 package/dhcp/dhcpd.service | 13 +++++++++++++
 2 files changed, 28 insertions(+)
 create mode 100644 package/dhcp/dhcpd.service
Quoted 2 lines

What is the status of this patch? Yann made some comments, which apparently you agreed on. Is a new version of the patch needed?

As explained in my reply to Yann, there is no need for a new version of the patch.

Best regards
ELB

#7 lore raw

PATCH 1/1 dhcp: add systemd service file

In reply to Eric Le Bihan

Dear Eric Le Bihan,

Quoted 10 lines · Eric Le Bihan

Add a systemd service file to start dhcpd.

The network interfaces to listen on can be set via the variable INTERFACES in /etc/default/dhcpd.conf. Example:

INTERFACES="eth0 eth1"

Also install a tmpfiles.d file to create /var/lib/dhcp/dhcpd.leases (needed at dhcpd start-up).

Quoted 6 lines
---
 package/dhcp/dhcp.mk       | 15 +++++++++++++++
 package/dhcp/dhcpd.service | 13 +++++++++++++
 2 files changed, 28 insertions(+)
 create mode 100644 package/dhcp/dhcpd.service

Applied, thanks.

Thomas

Signature · 3 lines
Thomas Petazzoni, CTO, Free Electrons
Embedded Linux, Kernel and Android engineering
http://free-electrons.com