remove boot-v1 support; disable user-services by default

This commit is contained in:
Mikaël Cluseau 2024-01-20 17:44:45 +01:00
parent f8a8344049
commit d0b8c75a9e
13 changed files with 13 additions and 570 deletions

View File

@ -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"
}

View File

@ -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"
}

12
layer/etc/init.d/dkl-svc Executable file
View File

@ -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()
{
}

View File

@ -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

View File

@ -1 +0,0 @@
../../init.d/dkl-default

View File

@ -1 +0,0 @@
../../init.d/dkl-user-services

View File

@ -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)
}
}

View File

@ -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 <<EOF\nroot:"+passwordHash+"\nEOF")
}
// - groups
for _, group := range cfg.Groups {
opts := make([]string, 0)
opts = append(opts, "-r")
if group.Gid != 0 {
opts = append(opts, "-g", strconv.Itoa(group.Gid))
}
opts = append(opts, group.Name)
sys.MustRun("groupadd", opts...)
}
// - user
for _, user := range cfg.Users {
opts := make([]string, 0)
opts = append(opts, "-r")
if user.Gid != 0 {
opts = append(opts, "-g", strconv.Itoa(user.Gid))
}
if user.Uid != 0 {
opts = append(opts, "-u", strconv.Itoa(user.Uid))
}
opts = append(opts, user.Name)
sys.MustRun("useradd", opts...)
}
// - files
if err := apply.Files(cfg, ""); err != nil {
log.Fatal("FATAL: ", err)
}
}

View File

@ -1,190 +0,0 @@
package initboot
import (
"bytes"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"
"time"
"novit.tech/direktil/pkg/config"
"novit.tech/direktil/inits/pkg/sys"
"novit.tech/direktil/inits/pkg/vars"
)
func setupLVM() {
if !dmInProc() {
sys.MustRun("modprobe", "dm-mod")
}
// start lvmetad
if lvmetad, err := exec.LookPath("lvmetad"); err == nil {
sys.Mkdir("/run/lvm", 0700)
sys.Mkdir("/run/lock/lvm", 0700)
sys.Run(lvmetad)
sys.WaitFile("/run/lvm/lvmetad.socket", time.After(30*time.Second))
} else {
log.Print("skipping lvmetad setup: ", err)
}
// scan devices
sys.Run("lvm", "pvscan")
sys.Run("lvm", "vgscan", "--mknodes")
sys.Run("lvm", "vgchange", "--sysinit", "-a", "ly")
cfg := sys.Config()
// setup storage
log.Print("checking storage")
if err := exec.Command("vgdisplay", "storage").Run(); err != nil {
log.Print("- creating VG storage")
setupVG(vars.BootArgValue("storage", cfg.Storage.UdevMatch))
}
for _, name := range cfg.Storage.RemoveVolumes {
dev := "/dev/storage/" + name
if _, err := os.Stat(dev); os.IsNotExist(err) {
continue
} else if err != nil {
log.Fatal("failed to stat ", dev, ": ", err)
}
log.Print("- removing LV ", name)
cmd := exec.Command("lvremove", "-f", "storage/"+name)
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
log.Fatal("failed to remove LV ", name)
}
}
// setup volumes
for _, volume := range cfg.Storage.Volumes {
if err := exec.Command("lvdisplay", "storage/"+volume.Name).Run(); err != nil {
log.Print("- creating LV ", volume.Name)
setupLV(volume)
}
dev := "/dev/storage/" + volume.Name
sys.WaitFile(dev, time.After(30*time.Second))
log.Printf("checking filesystem on %s", dev)
sys.MustRun("fsck", "-p", dev)
sys.Mount(dev, volume.Mount.Path, volume.FS,
syscall.MS_NOATIME|syscall.MS_RELATIME,
volume.Mount.Options)
}
}
func dmInProc() bool {
for _, f := range []string{"devices", "misc"} {
c, err := ioutil.ReadFile("/proc/" + f)
if err != nil {
log.Fatalf("failed to read %s: %v", f, err)
}
if !bytes.Contains(c, []byte("device-mapper")) {
return false
}
}
return true
}
func setupVG(udevMatch string) {
const pDevName = "DEVNAME="
dev := ""
try := 0
retry:
paths, err := filepath.Glob("/sys/class/block/*")
if err != nil {
log.Fatal("failed to list block devices: ", err)
}
for _, path := range paths {
// ignore loop devices
if strings.HasPrefix("loop", filepath.Base(path)) {
continue
}
// fetch udev informations
out, err := exec.Command("udevadm", "info", "-q", "property", path).CombinedOutput()
if err != nil {
log.Printf("WARNING: udev query of %q failed: %v\n%s", path, err, string(out))
continue
}
propertyLines := strings.Split(strings.TrimSpace(string(out)), "\n")
devPath := ""
matches := false
for _, line := range propertyLines {
if strings.HasPrefix(line, pDevName) {
devPath = line[len(pDevName):]
}
if matched, err := filepath.Match(udevMatch, line); err != nil {
log.Fatalf("FATAL: invalid match: %q: %v", udevMatch, err)
} else if matched {
matches = true
}
if devPath != "" && matches {
break
}
}
if devPath != "" && matches {
dev = devPath
break
}
}
if dev == "" {
time.Sleep(1 * time.Second)
try++
if try > 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)...)
}

View File

@ -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
}

View File

@ -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)
}
}

View File

@ -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) {
}

View File

@ -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