Andrew Rynhard 49307d554d refactor: improve machined
This is a rewrite of machined. It addresses some of the limitations and
complexity in the implementation. This introduces the idea of a
controller. A controller is responsible for managing the runtime, the
sequencer, and a new state type introduced in this PR.

A few highlights are:

- no more event bus
- functional approach to tasks (no more types defined for each task)
  - the task function definition now offers a lot more context, like
    access to raw API requests, the current sequence, a logger, the new
    state interface, and the runtime interface.
- no more panics to handle reboots
- additional initialize and reboot sequences
- graceful gRPC server shutdown on critical errors
- config is now stored at install time to avoid having to download it at
  install time and at boot time
- upgrades now use the local config instead of downloading it
- the upgrade API's preserve option takes precedence over the config's
  install force option

Additionally, this pulls various packes in under machined to make the
code easier to navigate.

Signed-off-by: Andrew Rynhard <andrew@andrewrynhard.com>
2020-04-28 08:20:55 -07:00

105 lines
2.4 KiB
Go

// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package main
import (
"errors"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"time"
"golang.org/x/sys/unix"
"github.com/talos-systems/go-procfs/procfs"
"github.com/talos-systems/talos/internal/pkg/kmsg"
"github.com/talos-systems/talos/internal/pkg/mount"
"github.com/talos-systems/talos/internal/pkg/mount/switchroot"
"github.com/talos-systems/talos/pkg/constants"
"github.com/talos-systems/talos/pkg/version"
)
// nolint: gocyclo
func run() (err error) {
// Mount the pseudo devices.
pseudo, err := mount.PseudoMountPoints()
if err != nil {
return err
}
if err = mount.Mount(pseudo); err != nil {
return err
}
// Setup logging to /dev/kmsg.
err = kmsg.Setup("[talos] [initramfs]", false)
if err != nil {
return err
}
log.Printf("booting Talos %s", version.Tag)
// Mount the rootfs.
log.Println("mounting the rootfs")
squashfs, err := mount.SquashfsMountPoints(constants.NewRoot)
if err != nil {
return err
}
if err = mount.Mount(squashfs); err != nil {
return err
}
// Switch into the new rootfs.
log.Println("entering the rootfs")
if err = switchroot.Switch(constants.NewRoot, pseudo); err != nil {
return err
}
return nil
}
func recovery() {
// If panic is set in the kernel flags, we'll hang instead of rebooting.
// But we still allow users to hit CTRL+ALT+DEL to try and restart when they're ready.
// Listening for these signals also keep us from deadlocking the goroutine.
if r := recover(); r != nil {
log.Printf("recovered from: %+v\n", r)
p := procfs.ProcCmdline().Get(constants.KernelParamPanic).First()
if p != nil && *p == "0" {
log.Printf("panic=0 kernel flag found. sleeping forever")
exitSignal := make(chan os.Signal, 1)
signal.Notify(exitSignal, syscall.SIGINT, syscall.SIGTERM)
<-exitSignal
}
for i := 10; i >= 0; i-- {
log.Printf("rebooting in %d seconds\n", i)
time.Sleep(1 * time.Second)
}
}
// nolint: errcheck
unix.Reboot(unix.LINUX_REBOOT_CMD_RESTART)
}
func main() {
defer recovery()
if err := run(); err != nil {
panic(fmt.Errorf("early boot failed: %w", err))
}
// We should never reach this point if things are working as intended.
panic(errors.New("unknown error"))
}