-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
grub.go
162 lines (136 loc) · 4.55 KB
/
grub.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package core
/* License: GPLv3
Authors:
Mirko Brombin <mirko@fabricators.ltd>
Vanilla OS Contributors <https://github.com/vanilla-os/>
Copyright: 2024
Description:
ABRoot is utility which provides full immutability and
atomicity to a Linux system, by transacting between
two root filesystems. Updates are performed using OCI
images, to ensure that the system is always in a
consistent state.
*/
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/vanilla-os/abroot/settings"
)
// Grub represents a grub instance, it exposes methods to generate a new grub
// config compatible with ABRoot, and to check if the system is booted into
// the present root or the future root
type Grub struct {
PresentRoot string
FutureRoot string
}
// generateABGrubConf generates a new grub config with the given details
func generateABGrubConf(kernelVersion string, rootPath string, rootUuid string, rootLabel string, generatedGrubConfigPath string) error {
PrintVerboseInfo("generateABGrubConf", "generating grub config for ABRoot")
kargs, err := KargsRead()
if err != nil {
PrintVerboseErr("generateABGrubConf", 0, err)
return err
}
var grubPath, bootPrefix, systemRoot string
if settings.Cnf.ThinProvisioning {
grubPath = filepath.Join(rootPath, "boot", "init", rootLabel)
bootPrefix = "/" + rootLabel
diskM := NewDiskManager()
sysRootPart, err := diskM.GetPartitionByLabel(rootLabel)
if err != nil {
PrintVerboseErr("generateABGrubConf", 3, err)
return err
}
systemRoot = "/dev/mapper/" + sysRootPart.Device
} else {
grubPath = filepath.Join(rootPath, "boot", "grub")
bootPrefix = "/.system/boot"
systemRoot = "UUID=" + rootUuid
}
confPath := filepath.Join(grubPath, "abroot.cfg")
template := ` search --no-floppy --fs-uuid --set=root %s
linux %s/vmlinuz-%s root=%s %s
initrd %s/initrd.img-%s
`
err = os.MkdirAll(grubPath, 0755)
if err != nil {
PrintVerboseErr("generateABGrubConf", 2, err)
return err
}
abrootBootConfig := fmt.Sprintf(template, rootUuid, bootPrefix, kernelVersion, systemRoot, kargs, bootPrefix, kernelVersion)
generatedGrubConfigContents, err := os.ReadFile(filepath.Join(rootPath, generatedGrubConfigPath))
if err != nil {
PrintVerboseErr("generateABGrubConf", 3, "could not read grub config", err)
return err
}
generatedGrubConfig := string(generatedGrubConfigContents)
replacementString := "REPLACED_BY_ABROOT"
if !strings.Contains(generatedGrubConfig, replacementString) {
err := errors.New("could not find replacement string \"" + replacementString + "\", check /etc/grub.d configuration")
PrintVerboseErr("generateABGrubConf", 3.1, err)
return err
}
grubConfigWithBootEntry := strings.Replace(generatedGrubConfig, "REPLACED_BY_ABROOT", abrootBootConfig, 1)
err = os.WriteFile(confPath, []byte(grubConfigWithBootEntry), 0644)
if err != nil {
PrintVerboseErr("generateABGrubConf", 4, "could not read grub config", err)
return err
}
PrintVerboseInfo("generateABGrubConf", "done")
return nil
}
// NewGrub creates a new Grub instance
func NewGrub(bootPart Partition) (*Grub, error) {
PrintVerboseInfo("NewGrub", "running...")
grubPath := filepath.Join(bootPart.MountPoint, "grub")
confPath := filepath.Join(grubPath, "grub.cfg")
cfg, err := os.ReadFile(confPath)
if err != nil {
PrintVerboseErr("NewGrub", 0, err)
return nil, err
}
var presentRoot, futureRoot string
for _, entry := range strings.Split(string(cfg), "\n") {
if strings.Contains(entry, "abroot-a") {
if strings.Contains(entry, "Current State") {
presentRoot = "a"
} else if strings.Contains(entry, "Previous State") {
futureRoot = "a"
}
} else if strings.Contains(entry, "abroot-b") {
if strings.Contains(entry, "Current State") {
presentRoot = "b"
} else if strings.Contains(entry, "Previous State") {
futureRoot = "b"
}
}
}
if presentRoot == "" || futureRoot == "" {
err := errors.New("could not find root partitions")
PrintVerboseErr("NewGrub", 1, err)
return nil, err
}
PrintVerboseInfo("NewGrub", "done")
return &Grub{
PresentRoot: presentRoot,
FutureRoot: futureRoot,
}, nil
}
func (g *Grub) IsBootedIntoPresentRoot() (bool, error) {
PrintVerboseInfo("Grub.IsBootedIntoPresentRoot", "running...")
a := NewABRootManager()
future, err := a.GetFuture()
if err != nil {
return false, err
}
if g.FutureRoot == "a" {
PrintVerboseInfo("Grub.IsBootedIntoPresentRoot", "done")
return future.Label == settings.Cnf.PartLabelA, nil
} else {
PrintVerboseInfo("Grub.IsBootedIntoPresentRoot", "done")
return future.Label == settings.Cnf.PartLabelB, nil
}
}