blob: 97e8565cd7bedae310395d5876b331a45b2c09c1 [file] [log] [blame]
Tim Windelschmidt6d33a432025-02-04 14:34:25 +01001// Copyright The Monogon Project Authors.
2// SPDX-License-Identifier: Apache-2.0
3
Tim Windelschmidt7a1b27d2024-02-22 23:54:58 +01004package main
5
6import (
Tim Windelschmidt7a1b27d2024-02-22 23:54:58 +01007 _ "embed"
8 "fmt"
Tim Windelschmidt7a1b27d2024-02-22 23:54:58 +01009 "os"
10 "path/filepath"
11
12 "source.monogon.dev/go/logging"
Jan Schäre19d2792025-06-23 12:37:58 +000013 "source.monogon.dev/metropolis/installer/install"
Tim Windelschmidt7a1b27d2024-02-22 23:54:58 +010014 "source.monogon.dev/osbase/blockdev"
Tim Windelschmidt7a1b27d2024-02-22 23:54:58 +010015 "source.monogon.dev/osbase/efivarfs"
Jan Schär5fdca562025-04-14 11:33:29 +000016 "source.monogon.dev/osbase/oci"
Jan Schäre19d2792025-06-23 12:37:58 +000017 "source.monogon.dev/osbase/oci/osimage"
Jan Schärc1b6df42025-03-20 08:52:18 +000018 "source.monogon.dev/osbase/structfs"
Tim Windelschmidt7a1b27d2024-02-22 23:54:58 +010019)
20
Jan Schär2b9a0a02025-07-09 07:54:12 +000021//go:embed metropolis/node/abloader/abloader.efi
Tim Windelschmidt7a1b27d2024-02-22 23:54:58 +010022var abloader []byte
23
Tim Windelschmidt7a1b27d2024-02-22 23:54:58 +010024// EnvInstallTarget environment variable which tells the takeover binary where
25// to install to
26const EnvInstallTarget = "TAKEOVER_INSTALL_TARGET"
27
28func installMetropolis(l logging.Leveled) error {
29 // Validate we are running via EFI.
30 if _, err := os.Stat("/sys/firmware/efi"); os.IsNotExist(err) {
Tim Windelschmidt1f51cf42024-10-01 17:04:28 +020031 // nolint:ST1005
Tim Windelschmidt7a1b27d2024-02-22 23:54:58 +010032 return fmt.Errorf("Monogon OS can only be installed on EFI-booted machines, this one is not")
33 }
34
35 metropolisSpecRaw, err := os.ReadFile("/params.pb")
36 if err != nil {
37 return err
38 }
39
Jan Schär5fdca562025-04-14 11:33:29 +000040 image, err := oci.ReadLayout("/osimage")
Tim Windelschmidt7a1b27d2024-02-22 23:54:58 +010041 if err != nil {
Jan Schär5fdca562025-04-14 11:33:29 +000042 return fmt.Errorf("failed to read OS image: %w", err)
Tim Windelschmidt7a1b27d2024-02-22 23:54:58 +010043 }
44
Jan Schär5fdca562025-04-14 11:33:29 +000045 installParams, err := setupOSImageParams(image, metropolisSpecRaw, os.Getenv(EnvInstallTarget))
Tim Windelschmidt7a1b27d2024-02-22 23:54:58 +010046 if err != nil {
47 return err
48 }
49
Jan Schäre19d2792025-06-23 12:37:58 +000050 be, err := install.Write(installParams)
Tim Windelschmidt7a1b27d2024-02-22 23:54:58 +010051 if err != nil {
52 return fmt.Errorf("failed to apply installation: %w", err)
53 }
54 bootEntryIdx, err := efivarfs.AddBootEntry(be)
55 if err != nil {
56 return fmt.Errorf("error creating EFI boot entry: %w", err)
57 }
58 if err := efivarfs.SetBootOrder(efivarfs.BootOrder{uint16(bootEntryIdx)}); err != nil {
59 return fmt.Errorf("error setting EFI boot order: %w", err)
60 }
61 l.Info("Metropolis installation completed")
62 return nil
63}
64
Jan Schäre19d2792025-06-23 12:37:58 +000065func setupOSImageParams(image *oci.Image, metropolisSpecRaw []byte, installTarget string) (*install.Params, error) {
Tim Windelschmidt7a1b27d2024-02-22 23:54:58 +010066 rootDev, err := blockdev.Open(filepath.Join("/dev", installTarget))
67 if err != nil {
68 return nil, fmt.Errorf("failed to open root device: %w", err)
69 }
70
Jan Schäre19d2792025-06-23 12:37:58 +000071 osImage, err := osimage.Read(image)
Tim Windelschmidt7a1b27d2024-02-22 23:54:58 +010072 if err != nil {
Jan Schär5fdca562025-04-14 11:33:29 +000073 return nil, fmt.Errorf("failed to read OS image: %w", err)
Tim Windelschmidt7a1b27d2024-02-22 23:54:58 +010074 }
75
Jan Schäre19d2792025-06-23 12:37:58 +000076 return &install.Params{
77 PartitionSize: install.PartitionSizeInfo{
Tim Windelschmidt7a1b27d2024-02-22 23:54:58 +010078 ESP: 384,
79 System: 4096,
80 Data: 128,
81 },
Jan Schärdaf9e952025-06-23 13:28:16 +000082 OSImage: osImage,
Jan Schärc1b6df42025-03-20 08:52:18 +000083 ABLoader: structfs.Bytes(abloader),
84 NodeParameters: structfs.Bytes(metropolisSpecRaw),
Tim Windelschmidt7a1b27d2024-02-22 23:54:58 +010085 Output: rootDev,
86 }, nil
87}