Andrew Rynhard ca35b85300 refactor: improve installation reliability
This change aims to make installations more unified and reliable. It
introduces the concept of a mountpoint manager that is capable of
mounting, unmounting, and moving a set of mountpoints in the correct
order.

Signed-off-by: Andrew Rynhard <andrew@andrewrynhard.com>
2019-08-01 11:44:40 -07:00

82 lines
1.9 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 (
"log"
"time"
"github.com/pkg/errors"
"github.com/talos-systems/talos/internal/pkg/constants"
"github.com/talos-systems/talos/internal/pkg/kmsg"
"github.com/talos-systems/talos/internal/pkg/mount/manager"
"github.com/talos-systems/talos/internal/pkg/mount/manager/squashfs"
"github.com/talos-systems/talos/internal/pkg/mount/manager/virtual"
"github.com/talos-systems/talos/internal/pkg/mount/switchroot"
"golang.org/x/sys/unix"
)
// nolint: gocyclo
func run() (err error) {
// Mount the virtual devices.
mountpoints, err := virtual.MountPoints()
if err != nil {
return err
}
virtual := manager.NewManager(mountpoints)
if err = virtual.MountAll(); err != nil {
return err
}
// Setup logging to /dev/kmsg.
_, err = kmsg.Setup("[talos] [initramfs]")
if err != nil {
return err
}
// Mount the rootfs.
log.Println("mounting the rootfs")
mountpoints, err = squashfs.MountPoints(constants.NewRoot)
if err != nil {
return err
}
squashfs := manager.NewManager(mountpoints)
if err = squashfs.MountAll(); err != nil {
return err
}
// Switch into the new rootfs.
log.Println("entering the rootfs")
if err = switchroot.Switch(constants.NewRoot, virtual); err != nil {
return err
}
return nil
}
func recovery() {
if r := recover(); r != nil {
log.Printf("recovered from: %+v\n", r)
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(errors.Wrap(err, "early boot failed"))
}
// We should never reach this point if things are working as intended.
panic(errors.New("unknown error"))
}