From 25db6133984901f34aae7bfc279de8aae2b60f38 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Wed, 8 Mar 2023 18:50:22 +0100 Subject: [PATCH] Refactor boot mode handling With the recent `if:` addition to Finit we can simplify a lot of the logic for disabling and enabling features in Infix. Standardizing on conditions that Finit provides. We create boot/netconf, boot/etc, and boot/profinet conditions early, after all filesystems have been mounted (and cleaned), so they are all set before Finit loads its configuration files in runlevel S. The default is still to boot into a managed NETCONF mode, since the end goal remains to integrate PROFINET as yet another service. The other mode, native /etc, should then be the only exception. Signed-off-by: Joachim Wiberg --- board/common/rootfs/etc/finit.d/10-infix.conf | 2 +- board/common/rootfs/lib/infix/clean-etc | 2 +- board/common/rootfs/lib/infix/mnt | 36 ++----------------- board/common/rootfs/lib/infix/swup | 12 ++----- board/common/rootfs/lib/infix/use-etc | 21 +++++++++-- .../libexec/finit/hook/mount/all/boot-mode.sh | 26 ++++++++++++++ package/mech/clixon.conf | 8 ++--- .../skeleton-init-finit.mk | 1 + 8 files changed, 55 insertions(+), 53 deletions(-) create mode 100755 board/common/rootfs/libexec/finit/hook/mount/all/boot-mode.sh diff --git a/board/common/rootfs/etc/finit.d/10-infix.conf b/board/common/rootfs/etc/finit.d/10-infix.conf index be44296a..286c955a 100644 --- a/board/common/rootfs/etc/finit.d/10-infix.conf +++ b/board/common/rootfs/etc/finit.d/10-infix.conf @@ -1,2 +1,2 @@ run [S] /lib/infix/nameif -- Probing network interfaces -run [S] /lib/infix/swup -- +run if: [S] /lib/infix/swup -- diff --git a/board/common/rootfs/lib/infix/clean-etc b/board/common/rootfs/lib/infix/clean-etc index a0d4485b..ce28ba77 100755 --- a/board/common/rootfs/lib/infix/clean-etc +++ b/board/common/rootfs/lib/infix/clean-etc @@ -4,7 +4,7 @@ /lib/infix/use-etc && exit 0 # This is a managed node, ignore all default configuration... -rm -f /etc/rc.local +rm -f /etc/rc.local rm -rf /etc/finit.d/enabled/*.conf rm -rf /etc/network/interfaces* diff --git a/board/common/rootfs/lib/infix/mnt b/board/common/rootfs/lib/infix/mnt index ac73fbda..4f9d019f 100755 --- a/board/common/rootfs/lib/infix/mnt +++ b/board/common/rootfs/lib/infix/mnt @@ -27,7 +27,7 @@ mount_rw() mountpoint -q "/$1" && return 0 # TODO: Also look for UBI partitions - mount LABEL="$1" && return 0 + mount LABEL="$1" 2>/dev/null && return 0 return 1 } @@ -49,29 +49,6 @@ mount_overlay() -o lowerdir="$dst",upperdir="$u",workdir="$w" } -use_etc() -{ - if [ -f /mnt/cfg/infix/.use_etc ]; then - return 0 - fi - - # Finit's condition system is not yet bootstrapped when /etc/fstab - # is parsed, so we have to manually check for the boot/etc - # condition. - awk ' - $0 == "finit.cond" { - cond=1; - next; - } - cond == 1 { - use_etc = index($0, "etc") != 0; - } - END { - exit(use_etc ? 0 : 1); - } - ' /proc/1/cmdline -} - # Fall back to console logging if sysklogd is too old if ! logger -? |grep -q "Log to kernel"; then opt="-c" @@ -111,16 +88,8 @@ else sync fi - # Report to the setup tool the current boot mode - if use_etc; then + if /lib/infix/use-etc; then etcsrc=/mnt/cfg - if [ -f /mnt/cfg/infix/.use_etc ]; then - cat /mnt/cfg/infix/.use_etc > /tmp/.boot_mode - else - echo "native /etc" > /tmp/.boot_mode - fi - else - echo "NETCONF" > /tmp/.boot_mode fi # Ensure that all users in wheel can create the .reset file @@ -128,7 +97,6 @@ else chgrp wheel /mnt/cfg/infix fi - mount_overlay cfg $cfgsrc /cfg mount_overlay etc $etcsrc /etc mount_overlay home $cfgsrc /home diff --git a/board/common/rootfs/lib/infix/swup b/board/common/rootfs/lib/infix/swup index 095d3954..0b699c83 100755 --- a/board/common/rootfs/lib/infix/swup +++ b/board/common/rootfs/lib/infix/swup @@ -1,15 +1,7 @@ #!/bin/sh +# Only runs in native /etc and PROFINET nodes. -# Only create a default config on unmanaged nodes. -if ! /lib/infix/use-etc; then - initctl cond set netconf - exit 0 -fi - -# PROFINET is a variant of native /etc, make sure to -# enable it and disable the default LLDP daemon. -if grep -qi profinet /mnt/cfg/infix/.use_etc; then - initctl enable profeth +if initctl cond get boot/profinet; then initctl enable snmpd initctl disable lldpd fi diff --git a/board/common/rootfs/lib/infix/use-etc b/board/common/rootfs/lib/infix/use-etc index fa8c941e..7364d03b 100755 --- a/board/common/rootfs/lib/infix/use-etc +++ b/board/common/rootfs/lib/infix/use-etc @@ -1,6 +1,21 @@ #!/bin/sh -[ -f /mnt/cfg/infix/.use_etc ] && exit 0 -initctl cond get boot/etc && exit 0 +if [ -f /mnt/cfg/infix/.use_etc ]; then + exit 0 +fi -exit 1 +# Finit's condition system may not yet be bootstrapped when this script +# is called, e.g., when /etc/fstab is parsed, so we have to manually +# check for the boot/etc condition. +awk ' + $0 == "finit.cond" { + cond=1; + next; + } + cond == 1 { + use_etc = index($0, "etc") != 0; + } + END { + exit(use_etc ? 0 : 1); + } +' /proc/1/cmdline diff --git a/board/common/rootfs/libexec/finit/hook/mount/all/boot-mode.sh b/board/common/rootfs/libexec/finit/hook/mount/all/boot-mode.sh new file mode 100755 index 00000000..da0e90e2 --- /dev/null +++ b/board/common/rootfs/libexec/finit/hook/mount/all/boot-mode.sh @@ -0,0 +1,26 @@ +#!/bin/sh +# Override boot mode based on user input from setup tool + +set_cond() +{ + mkdir -p /run/finit/cond/boot + ln -s /run/finit/cond/reconf "/run/finit/cond/boot/$1" +} + +# Report to the setup and show tools the current boot mode +if /lib/infix/use-etc; then + if [ -f /mnt/cfg/infix/.use_etc ]; then + cat /mnt/cfg/infix/.use_etc > /tmp/.boot_mode + if grep -qi profinet /mnt/cfg/infix/.use_etc; then + set_cond profinet + else + set_cond etc + fi + else + echo "native /etc" > /tmp/.boot_mode + set_cond etc + fi +else + echo "NETCONF" > /tmp/.boot_mode + set_cond netconf +fi diff --git a/package/mech/clixon.conf b/package/mech/clixon.conf index cac86a4e..8cbe6a32 100644 --- a/package/mech/clixon.conf +++ b/package/mech/clixon.conf @@ -1,5 +1,5 @@ -run if: [S] /lib/infix/clean-etc -- -run if: [S] /lib/infix/prep-db -- -run if: [S] :boot clixon_backend -F -1 -s startup -- Loading startup configuration +run if: [S] /lib/infix/clean-etc -- +run if: [S] /lib/infix/prep-db -- +run if: [S] :boot clixon_backend -F -1 -s startup -- Loading startup configuration -service if: [12345789] clixon_backend -F -s none -- Configuration daemon +service if: [12345789] clixon_backend -F -s none -- Configuration daemon diff --git a/package/skeleton-init-finit/skeleton-init-finit.mk b/package/skeleton-init-finit/skeleton-init-finit.mk index c04b5d9a..b661ae6c 100644 --- a/package/skeleton-init-finit/skeleton-init-finit.mk +++ b/package/skeleton-init-finit/skeleton-init-finit.mk @@ -151,6 +151,7 @@ endif ifeq ($(BR2_PACKAGE_PROFETH),y) define SKELETON_INIT_FINIT_SET_PROFETH cp $(SKELETON_INIT_FINIT_AVAILABLE)/profeth.conf $(FINIT_D)/available/ + ln -sf ../available/profeth.conf $(FINIT_D)/enabled/ endef SKELETON_INIT_FINIT_TARGET_FINALIZE_HOOKS += SKELETON_INIT_FINIT_SET_PROFETH endif