initrd/apply-config.go

141 lines
3.2 KiB
Go
Raw Normal View History

2022-02-04 18:59:42 +00:00
package main
import (
"os"
"path/filepath"
2023-12-04 12:59:37 +00:00
"strconv"
2022-02-04 18:59:42 +00:00
"strings"
"syscall"
"time"
2024-01-20 15:41:54 +00:00
"github.com/rs/zerolog/log"
2022-02-04 18:59:42 +00:00
yaml "gopkg.in/yaml.v2"
2024-01-20 15:41:54 +00:00
"novit.tech/direktil/pkg/config/apply"
2022-02-04 18:59:42 +00:00
)
var loopOffset = 0
func applyConfig(cfgPath string, bootMounted bool) (cfg *configV1) {
2023-11-27 13:08:44 +00:00
cfgBytes, err := os.ReadFile(cfgPath)
2022-02-04 18:59:42 +00:00
if err != nil {
fatalf("failed to read %s: %v", cfgPath, err)
}
cfg = &configV1{}
2022-02-04 18:59:42 +00:00
if err := yaml.Unmarshal(cfgBytes, cfg); err != nil {
fatal("failed to load config: ", err)
}
// mount layers
if len(cfg.Layers) == 0 {
fatal("no layers configured!")
}
layersInMemory := paramBool("layers-in-mem", false)
2024-01-20 15:41:54 +00:00
log.Info().Strs("layers", cfg.Layers).Bool("in-memory", layersInMemory).Msg("mounting layers")
2022-02-04 18:59:42 +00:00
const layersInMemDir = "/layers-in-mem"
if layersInMemory {
mkdir(layersInMemDir, 0700)
mount("layers-mem", layersInMemDir, "tmpfs", 0, "")
}
lowers := make([]string, len(cfg.Layers))
for i, layer := range cfg.Layers {
2024-01-20 15:41:54 +00:00
log := log.With().Str("layer", layer).Logger()
2022-02-04 18:59:42 +00:00
path := layerPath(layer)
info, err := os.Stat(path)
if err != nil {
fatal(err)
}
2024-01-20 15:41:54 +00:00
log.Info().Int64("size", info.Size()).Msg("layer found")
2022-02-04 18:59:42 +00:00
if layersInMemory {
2024-01-20 15:41:54 +00:00
log.Info().Msg("copying to memory")
2022-02-04 18:59:42 +00:00
targetPath := filepath.Join(layersInMemDir, layer)
cp(path, targetPath)
path = targetPath
}
dir := "/layers/" + layer
lowers[i] = dir
2023-12-04 12:59:37 +00:00
mountSquahfs(path, dir)
2022-02-04 18:59:42 +00:00
}
// prepare system root
mount("mem", "/changes", "tmpfs", 0, "")
mkdir("/changes/workdir", 0755)
mkdir("/changes/upperdir", 0755)
mount("overlay", "/system", "overlay", rootMountFlags,
"lowerdir="+strings.Join(lowers, ":")+",upperdir=/changes/upperdir,workdir=/changes/workdir")
2023-12-04 12:59:37 +00:00
// 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("", "/system", "", syscall.MS_SHARED|syscall.MS_REC, ""); err != nil {
fatalf("FATAL: mount --make-rshared / failed: %v", err)
}
2022-02-04 18:59:42 +00:00
if bootMounted {
if layersInMemory {
if err := syscall.Unmount("/boot", 0); err != nil {
2024-01-20 15:41:54 +00:00
log.Warn().Err(err).Msg("failed to unmount /boot")
2022-02-04 18:59:42 +00:00
time.Sleep(2 * time.Second)
}
} else {
mount("/boot", "/system/boot", "", syscall.MS_BIND, "")
}
}
// - write files
2024-01-20 15:41:54 +00:00
apply.Files(cfg.Files, "/system")
2022-02-04 18:59:42 +00:00
2023-12-04 12:59:37 +00:00
// - groups
for _, group := range cfg.Groups {
2024-01-20 15:41:54 +00:00
logEvt := log.Info().Str("group", group.Name)
2023-12-04 12:59:37 +00:00
opts := make([]string, 0)
opts = append(opts /* chroot */, "/system", "groupadd", "-r")
if group.Gid != 0 {
opts = append(opts, "-g", strconv.Itoa(group.Gid))
2024-01-20 15:41:54 +00:00
logEvt.Int("gid", group.Gid)
2023-12-04 12:59:37 +00:00
}
opts = append(opts, group.Name)
2024-01-20 15:41:54 +00:00
logEvt.Msg("creating group")
2023-12-04 12:59:37 +00:00
run("chroot", opts...)
}
// - user
for _, user := range cfg.Users {
2024-01-20 15:41:54 +00:00
logEvt := log.Info().Str("user", user.Name)
2023-12-04 12:59:37 +00:00
opts := make([]string, 0)
opts = append(opts /* chroot */, "/system", "useradd", "-r")
if user.Gid != 0 {
opts = append(opts, "-g", strconv.Itoa(user.Gid))
2024-01-20 15:41:54 +00:00
logEvt.Int("gid", user.Gid)
2023-12-04 12:59:37 +00:00
}
if user.Uid != 0 {
opts = append(opts, "-u", strconv.Itoa(user.Uid))
2024-01-20 15:41:54 +00:00
logEvt.Int("uid", user.Uid)
2023-12-04 12:59:37 +00:00
}
opts = append(opts, user.Name)
2024-01-20 15:41:54 +00:00
logEvt.Msg("creating user")
2023-12-04 12:59:37 +00:00
run("chroot", opts...)
}
return
}