From d0b8c75a9eb3baf1d7450520250be30224899ab3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=C3=ABl=20Cluseau?= Date: Sat, 20 Jan 2024 17:44:45 +0100 Subject: [PATCH] remove boot-v1 support; disable user-services by default --- layer/etc/init.d/dkl-boot | 16 -- layer/etc/init.d/dkl-default | 16 -- layer/etc/init.d/dkl-svc | 12 ++ layer/etc/rc.conf | 26 +-- layer/etc/runlevels/default/dkl-default | 1 - layer/etc/runlevels/default/dkl-user-services | 1 - pkg/cmd/init/boot/boot.go | 37 ---- pkg/cmd/init/boot/files.go | 61 ------ pkg/cmd/init/boot/lvm.go | 190 ------------------ pkg/cmd/init/boot/network.go | 156 -------------- pkg/cmd/init/boot/state.go | 48 ----- pkg/cmd/init/default/default.go | 15 -- pkg/cmd/init/init.go | 4 - 13 files changed, 13 insertions(+), 570 deletions(-) delete mode 100755 layer/etc/init.d/dkl-boot delete mode 100755 layer/etc/init.d/dkl-default create mode 100755 layer/etc/init.d/dkl-svc delete mode 120000 layer/etc/runlevels/default/dkl-default delete mode 120000 layer/etc/runlevels/default/dkl-user-services delete mode 100644 pkg/cmd/init/boot/boot.go delete mode 100644 pkg/cmd/init/boot/files.go delete mode 100644 pkg/cmd/init/boot/lvm.go delete mode 100644 pkg/cmd/init/boot/network.go delete mode 100644 pkg/cmd/init/boot/state.go diff --git a/layer/etc/init.d/dkl-boot b/layer/etc/init.d/dkl-boot deleted file mode 100755 index d5f672d..0000000 --- a/layer/etc/init.d/dkl-boot +++ /dev/null @@ -1,16 +0,0 @@ -#!/sbin/openrc-run - -description="Direktil boot operations." - -depend() -{ - provide net - before sysctl -} - -start() -{ - ebegin "Running Direktil boot operations" - /sbin/dkl init boot - eend $? "Direktil boot operations failed" -} diff --git a/layer/etc/init.d/dkl-default b/layer/etc/init.d/dkl-default deleted file mode 100755 index 7d7238a..0000000 --- a/layer/etc/init.d/dkl-default +++ /dev/null @@ -1,16 +0,0 @@ -#!/sbin/openrc-run - -description="Direktil default operations." - -depend() -{ - # need anything? - : -} - -start() -{ - ebegin "Running Direktil default operations" - /sbin/dkl init default - eend $? "Direktil default operations failed" -} diff --git a/layer/etc/init.d/dkl-svc b/layer/etc/init.d/dkl-svc new file mode 100755 index 0000000..a0dff7f --- /dev/null +++ b/layer/etc/init.d/dkl-svc @@ -0,0 +1,12 @@ +#!/sbin/openrc-run + +description="dkl: $SVCNAME" + +supervisor=supervise-daemon +command="/sbin/dkl logger -- /etc/direktil/services/$SVCNAME" + +pidfile=/run/dkl-$SVCNAME.pid + +depend() +{ +} diff --git a/layer/etc/rc.conf b/layer/etc/rc.conf index 9f86e60..2dd068d 100644 --- a/layer/etc/rc.conf +++ b/layer/etc/rc.conf @@ -1,30 +1,6 @@ # Global OpenRC configuration settings rc_shell=/sbin/sulogin - -# rc_logger launches a logging daemon to log the entire rc process to -# /var/log/rc.log -# NOTE: Linux systems require the devfs service to be started before -# logging can take place and as such cannot log the sysinit runlevel. -#rc_logger="NO" - -# Through rc_log_path you can specify a custom log file. -# The default value is: /var/log/rc.log -#rc_log_path="/var/log/rc.log" - -# By default we assume that all daemons will start correctly. -# However, some do not - a classic example is that they fork and return 0 AND -# then child barfs on a configuration error. Or the daemon has a bug and the -# child crashes. You can set the number of milliseconds start-stop-daemon -# waits to check that the daemon is still running after starting here. -# The default is 0 - no checking. -#rc_start_wait=100 - -# Set unicode to YES to turn on unicode support for keyboards and screens. unicode="YES" - -############################################################################## -# SERVICE CONFIGURATION VARIABLES - rc_tty_number=12 ############################################################################## @@ -35,7 +11,7 @@ rc_tty_number=12 # cgroups version 1 on /sys/fs/cgroup. # "legacy" mounts cgroups version 1 on /sys/fs/cgroup # "unified" mounts cgroups version 2 on /sys/fs/cgroup -rc_cgroup_mode="legacy" +rc_cgroup_mode="hybrid" # This switch controls whether or not cgroups version 1 controllers are # individually mounted under diff --git a/layer/etc/runlevels/default/dkl-default b/layer/etc/runlevels/default/dkl-default deleted file mode 120000 index cff26c8..0000000 --- a/layer/etc/runlevels/default/dkl-default +++ /dev/null @@ -1 +0,0 @@ -../../init.d/dkl-default \ No newline at end of file diff --git a/layer/etc/runlevels/default/dkl-user-services b/layer/etc/runlevels/default/dkl-user-services deleted file mode 120000 index 4b145f9..0000000 --- a/layer/etc/runlevels/default/dkl-user-services +++ /dev/null @@ -1 +0,0 @@ -../../init.d/dkl-user-services \ No newline at end of file diff --git a/pkg/cmd/init/boot/boot.go b/pkg/cmd/init/boot/boot.go deleted file mode 100644 index 47d7df6..0000000 --- a/pkg/cmd/init/boot/boot.go +++ /dev/null @@ -1,37 +0,0 @@ -package initboot - -import ( - "log" - - "github.com/spf13/cobra" - - "novit.tech/direktil/inits/pkg/sys" -) - -var ( - doNetwork bool -) - -func Command() (c *cobra.Command) { - c = &cobra.Command{ - Use: "boot", - Short: "boot stage", - Run: run, - } - - return -} - -func run(c *cobra.Command, args []string) { - step("files", setupFiles) - step("modules", setupModules) - step("network", setupNetworking) - step("lvm", setupLVM) -} - -func setupModules() { - for _, mod := range sys.Config().Modules { - log.Print("loading module ", mod) - sys.Run("modprobe", mod) - } -} diff --git a/pkg/cmd/init/boot/files.go b/pkg/cmd/init/boot/files.go deleted file mode 100644 index 304457f..0000000 --- a/pkg/cmd/init/boot/files.go +++ /dev/null @@ -1,61 +0,0 @@ -package initboot - -import ( - "log" - "strconv" - "syscall" - - "novit.tech/direktil/pkg/config/apply" - - "novit.tech/direktil/inits/pkg/sys" -) - -func setupFiles() { - cfg := sys.Config() - - // make root rshared (default in systemd, required by Kubernetes 1.10+) - // equivalent to "mount --make-rshared /" - // see kernel's Documentation/sharedsubtree.txt (search rshared) - if err := syscall.Mount("", "/", "", syscall.MS_SHARED|syscall.MS_REC, ""); err != nil { - log.Fatalf("FATAL: mount --make-rshared / failed: %v", err) - } - - // - setup root user - if passwordHash := cfg.RootUser.PasswordHash; passwordHash == "" { - sys.MustRun("/usr/bin/passwd", "-d", "root") - } else { - sys.MustRun("/bin/sh", "-c", "chpasswd --encrypted < 30 { - log.Fatal("FATAL: storage device not found after 30s: ", udevMatch) - } - goto retry - } - - log.Print("found storage device at ", dev) - - sys.MustRun("pvcreate", dev) - sys.MustRun("vgcreate", "storage", dev) -} - -func setupLV(volume config.VolumeDef) { - if volume.Extents != "" { - sys.MustRun("lvcreate", "-l", volume.Extents, "-n", volume.Name, "storage") - } else { - sys.MustRun("lvcreate", "-L", volume.Size, "-n", volume.Name, "storage") - } - - // wait the device link - devPath := "/dev/storage/" + volume.Name - sys.WaitFile(devPath, time.After(30*time.Second)) - - args := make([]string, 0) - - switch volume.FS { - case "btrfs": - args = append(args, "-f") - case "ext4": - args = append(args, "-F") - } - - sys.MustRun("mkfs."+volume.FS, append(args, devPath)...) -} diff --git a/pkg/cmd/init/boot/network.go b/pkg/cmd/init/boot/network.go deleted file mode 100644 index de509fb..0000000 --- a/pkg/cmd/init/boot/network.go +++ /dev/null @@ -1,156 +0,0 @@ -package initboot - -import ( - "bytes" - "fmt" - "log" - "net" - "os" - "os/exec" - "path/filepath" - "time" - - "github.com/go-ping/ping" - "novit.tech/direktil/pkg/config" - - "novit.tech/direktil/inits/pkg/sys" - "novit.tech/direktil/inits/pkg/vars" -) - -var networkStarted = map[string]bool{} - -func setupNetworking() { - cfg := sys.Config() - for idx, network := range cfg.Networks { - step(fmt.Sprintf("network:%d", idx), func() { setupNetwork(idx, network) }) - } -} - -func setupNetwork(idx int, network config.NetworkDef) { - tries := 0 -retry: - ifaces, err := net.Interfaces() - if err != nil { - log.Fatalf("FATAL: failed to get network interfaces: %v", err) - } - - match := false - for _, iface := range ifaces { - if networkStarted[iface.Name] { - continue - } - - if network.Match.Name != "" { - if ok, err := filepath.Match(network.Match.Name, iface.Name); err != nil { - log.Fatalf("FATAL: network[%d] name match error: %v", idx, err) - } else if !ok { - continue - } - } - - if network.Match.Ping != nil { - log.Printf("network[%d] ping check on %s", idx, iface.Name) - - if ok, err := networkPingCheck(iface.Name, network); err != nil { - log.Printf("ERROR: network[%d] ping check failed: %v", idx, err) - - } else if !ok { - continue - } - } - - log.Printf("network[%d] matches interface %s", idx, iface.Name) - match = true - - startNetwork(iface.Name, idx, network) - - if !network.Match.All { - return - } - } - - if !match { - log.Printf("WARNING: network[%d] did not match any interface", idx) - - tries++ - if network.Optional && tries > 3 { - return - } - - time.Sleep(1 * time.Second) - log.Printf("WARNING: network[%d] retrying (try: %d)", idx, tries) - goto retry - } -} - -func startNetwork(ifaceName string, idx int, network config.NetworkDef) { - cfg := sys.Config() - - log.Printf("starting network[%d]", idx) - - script := vars.Substitute([]byte(network.Script), cfg) - - c := exec.Command("/bin/sh") - c.Stdin = bytes.NewBuffer(script) - c.Stdout = os.Stdout - c.Stderr = os.Stderr - - // TODO doc - c.Env = append(append(make([]string, 0), os.Environ()...), "IFNAME="+ifaceName) - - if err := c.Run(); err != nil { - links, _ := exec.Command("ip", "link", "ls").CombinedOutput() - log.Fatalf("FATAL: network setup failed (link list below): %v\n%s", err, string(links)) - } - - networkStarted[ifaceName] = true -} - -func networkPingCheck(ifName string, network config.NetworkDef) (b bool, err error) { - check := network.Match.Ping - - source := string(vars.Substitute([]byte(check.Source), sys.Config())) - - if err = sys.Run("ip", "addr", "add", source, "dev", ifName); err != nil { - return - } - if err = sys.Run("ip", "link", "set", ifName, "up"); err != nil { - return - } - - defer func() { - sys.MustRun("ip", "link", "set", ifName, "down") - sys.MustRun("ip", "addr", "del", source, "dev", ifName) - }() - - count := 3 - if check.Count != 0 { - count = check.Count - } - - for n := 0; n < count; n++ { - // TODO probably better to use golang.org/x/net/icmp directly - pinger, e := ping.NewPinger(network.Match.Ping.Target) - if e != nil { - err = e - return - } - - pinger.Count = 1 - - pinger.Timeout = 1 * time.Second - if check.Timeout > 0 { - pinger.Timeout = time.Duration(check.Timeout) * time.Second - } - - pinger.SetPrivileged(true) - pinger.Run() - - if pinger.Statistics().PacketsRecv > 0 { - b = true - return - } - } - - return -} diff --git a/pkg/cmd/init/boot/state.go b/pkg/cmd/init/boot/state.go deleted file mode 100644 index 39f146b..0000000 --- a/pkg/cmd/init/boot/state.go +++ /dev/null @@ -1,48 +0,0 @@ -package initboot - -import ( - "encoding/json" - "io/ioutil" - "log" - "os" -) - -var stateFile = "/run/dkl-boot.state" - -func readState() (state map[string]bool) { - state = map[string]bool{} - - ba, err := ioutil.ReadFile(stateFile) - if err != nil { - if os.IsNotExist(err) { - return - } - log.Fatal("failed to read state: ", err) - } - - err = json.Unmarshal(ba, &state) - if err != nil { - log.Fatal("failed to parse state: ", err) - } - - return -} - -func writeState(state map[string]bool) { - ba, err := json.Marshal(state) - if err != nil { - log.Fatal("failed to serialize state: ", err) - } - - ioutil.WriteFile(stateFile, ba, 0600) -} - -func step(step string, operation func()) { - state := readState() - if !state[step] { - operation() - - state[step] = true - writeState(state) - } -} diff --git a/pkg/cmd/init/default/default.go b/pkg/cmd/init/default/default.go index 93a627c..6d6007f 100644 --- a/pkg/cmd/init/default/default.go +++ b/pkg/cmd/init/default/default.go @@ -1,16 +1 @@ package initdefault - -import "github.com/spf13/cobra" - -func Command() (c *cobra.Command) { - c = &cobra.Command{ - Use: "default", - Short: "default stage", - Run: run, - } - - return -} - -func run(c *cobra.Command, args []string) { -} diff --git a/pkg/cmd/init/init.go b/pkg/cmd/init/init.go index f75706a..315960c 100644 --- a/pkg/cmd/init/init.go +++ b/pkg/cmd/init/init.go @@ -6,8 +6,6 @@ import ( "github.com/spf13/cobra" - initboot "novit.tech/direktil/inits/pkg/cmd/init/boot" - initdefault "novit.tech/direktil/inits/pkg/cmd/init/default" initservice "novit.tech/direktil/inits/pkg/cmd/init/service" ) @@ -26,8 +24,6 @@ func Command() (c *cobra.Command) { }, } - c.AddCommand(initboot.Command()) - c.AddCommand(initdefault.Command()) c.AddCommand(initservice.Command()) return