2022-02-04 18:59:42 +00:00
|
|
|
package main
|
|
|
|
|
2022-03-08 10:45:56 +00:00
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2024-01-20 16:26:28 +00:00
|
|
|
"syscall"
|
2022-03-08 10:45:56 +00:00
|
|
|
|
2024-01-20 15:41:54 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2022-03-08 10:45:56 +00:00
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
|
2022-04-04 08:29:28 +00:00
|
|
|
config "novit.tech/direktil/pkg/bootstrapconfig"
|
2022-03-08 10:45:56 +00:00
|
|
|
)
|
|
|
|
|
2022-02-04 18:59:42 +00:00
|
|
|
func bootV2() {
|
2024-01-20 15:41:54 +00:00
|
|
|
log.Info().Msg("-- boot v2 --")
|
2022-03-08 10:45:56 +00:00
|
|
|
|
2023-12-04 12:59:37 +00:00
|
|
|
kernelVersion := unameRelease()
|
2024-01-20 15:41:54 +00:00
|
|
|
log.Info().Str("version", kernelVersion).Msg("Linux")
|
2023-12-04 12:59:37 +00:00
|
|
|
|
2022-03-08 10:45:56 +00:00
|
|
|
cfg := &config.Config{}
|
|
|
|
|
|
|
|
{
|
|
|
|
f, err := os.Open("/config.yaml")
|
|
|
|
if err != nil {
|
|
|
|
fatal("failed to open /config.yaml: ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = yaml.NewDecoder(f).Decode(cfg)
|
|
|
|
f.Close()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fatal("failed to parse /config.yaml: ", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-20 15:41:54 +00:00
|
|
|
log.Info().Msg("config loaded")
|
|
|
|
|
|
|
|
if cfg.AntiPhishingCode != "" {
|
|
|
|
log.Info().Str("anti-phishing-code", cfg.AntiPhishingCode).Send()
|
|
|
|
}
|
2022-03-08 10:45:56 +00:00
|
|
|
|
|
|
|
auths = cfg.Auths
|
|
|
|
|
|
|
|
// mount kernel modules
|
2023-12-04 12:59:37 +00:00
|
|
|
if cfg.Modules == "" {
|
2024-01-20 15:41:54 +00:00
|
|
|
log.Warn().Msg("NOT mounting modules (\"modules:\" not specified)")
|
2023-12-04 12:59:37 +00:00
|
|
|
} else {
|
2024-01-20 15:41:54 +00:00
|
|
|
log.Info().Str("from", cfg.Modules).Msg("mounting modules")
|
2023-12-04 12:59:37 +00:00
|
|
|
mountSquahfs(cfg.Modules, "/modules")
|
|
|
|
|
|
|
|
modulesSourcePath := "/modules/lib/modules/" + kernelVersion
|
|
|
|
if _, err := os.Stat(modulesSourcePath); err != nil {
|
|
|
|
fatal("invalid modules dir: ", err)
|
2022-03-08 10:45:56 +00:00
|
|
|
}
|
|
|
|
|
2023-12-04 12:59:37 +00:00
|
|
|
os.MkdirAll("/lib/modules", 0755)
|
|
|
|
if err := os.Symlink(modulesSourcePath, "/lib/modules/"+kernelVersion); err != nil {
|
2022-03-08 10:45:56 +00:00
|
|
|
fatal("failed to symlink modules: ", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// load basic modules
|
2024-01-20 15:41:54 +00:00
|
|
|
for _, module := range []string{"unix"} {
|
|
|
|
log.Info().Str("module", module).Msg("loading module")
|
|
|
|
run("modprobe", module)
|
|
|
|
}
|
2022-03-08 10:45:56 +00:00
|
|
|
|
|
|
|
// devices init
|
2024-01-20 15:41:54 +00:00
|
|
|
log.Info().Msg("starting udevd")
|
2022-03-08 10:45:56 +00:00
|
|
|
err := exec.Command("udevd").Start()
|
|
|
|
if err != nil {
|
|
|
|
fatal("failed to start udevd: ", err)
|
|
|
|
}
|
|
|
|
|
2024-01-20 15:41:54 +00:00
|
|
|
log.Info().Msg("udevadm triggers")
|
2022-03-08 10:45:56 +00:00
|
|
|
run("udevadm", "trigger", "-c", "add", "-t", "devices")
|
|
|
|
run("udevadm", "trigger", "-c", "add", "-t", "subsystems")
|
|
|
|
|
2024-01-20 15:41:54 +00:00
|
|
|
log.Info().Msg("udevadm settle")
|
2022-03-08 10:45:56 +00:00
|
|
|
run("udevadm", "settle")
|
|
|
|
|
|
|
|
// networks
|
|
|
|
setupNetworks(cfg)
|
|
|
|
|
|
|
|
// Wireguard VPN
|
|
|
|
// TODO startVPN()
|
|
|
|
|
|
|
|
// SSH service
|
|
|
|
startSSH(cfg)
|
|
|
|
|
2023-12-17 12:48:18 +00:00
|
|
|
// dmcrypt blockdevs
|
|
|
|
setupCrypt(cfg.PreLVMCrypt, map[string]string{})
|
|
|
|
|
2022-03-08 10:45:56 +00:00
|
|
|
// LVM
|
|
|
|
setupLVM(cfg)
|
|
|
|
|
|
|
|
// bootstrap the system
|
|
|
|
bootstrap(cfg)
|
|
|
|
|
|
|
|
// finalize
|
|
|
|
finalizeBoot()
|
2022-02-04 18:59:42 +00:00
|
|
|
}
|
2024-01-20 16:26:28 +00:00
|
|
|
|
|
|
|
func finalizeBoot() {
|
|
|
|
// switch root
|
|
|
|
log.Info().Msg("switching root")
|
|
|
|
err := syscall.Exec("/sbin/switch_root", []string{"switch_root",
|
|
|
|
"-c", "/dev/console", "/system", "/sbin/init"}, os.Environ())
|
|
|
|
fatal("switch_root failed: ", err)
|
|
|
|
}
|