-
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathgrub.go
More file actions
194 lines (167 loc) · 5.3 KB
/
grub.go
File metadata and controls
194 lines (167 loc) · 5.3 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
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
}
// createABSpecificGrub creates a directory that contains the root specific root information
func createABSpecificGrub(kernelVersion string, rootUuid string, rootLabel string, generatedGrubConfigPath string, bootMountpoint string, filesDir string) error {
PrintVerboseInfo("createABSpecificGrub", "creating root specific grub info")
bootPrefix := filepath.Join("/abroot", rootLabel)
configDir := filepath.Join(bootMountpoint, bootPrefix)
err := MoveFile(
filepath.Join(filesDir, "vmlinuz-"+kernelVersion),
filepath.Join(configDir, "vmlinuz-"+kernelVersion),
)
if err != nil {
PrintVerboseErr("createABSpecificGrub", 1, err)
return err
}
err = MoveFile(
filepath.Join(filesDir, "initrd.img-"+kernelVersion),
filepath.Join(configDir, "initrd.img-"+kernelVersion),
)
if err != nil {
PrintVerboseErr("createABSpecificGrub", 2, err)
return err
}
err = MoveFile(
filepath.Join(filesDir, "config-"+kernelVersion),
filepath.Join(configDir, "config-"+kernelVersion),
)
if err != nil {
PrintVerboseErr("createABSpecificGrub", 3, err)
return err
}
err = MoveFile(
filepath.Join(filesDir, "System.map-"+kernelVersion),
filepath.Join(configDir, "System.map-"+kernelVersion),
)
if err != nil {
PrintVerboseErr("createABSpecificGrub", 4, err)
return err
}
kargs, err := KargsRead()
if err != nil {
PrintVerboseErr("createABSpecificGrub", 5, err)
return err
}
var systemRoot string
if settings.Cnf.ThinProvisioning {
diskM := NewDiskManager()
sysRootPart, err := diskM.GetPartitionByLabel(rootLabel)
if err != nil {
PrintVerboseErr("createABSpecificGrub", 6, err)
return err
}
systemRoot = "/dev/mapper/" + sysRootPart.Device
} else {
systemRoot = "UUID=" + rootUuid
}
confPath := filepath.Join(configDir, "abroot.cfg")
template := `
linux %s/vmlinuz-%s root=%s %s
initrd %s/initrd.img-%s
`
_ = os.RemoveAll(confPath)
err = os.MkdirAll(configDir, 0755)
if err != nil {
PrintVerboseErr("createABSpecificGrub", 7, err)
return err
}
abrootBootConfig := fmt.Sprintf(template, bootPrefix, kernelVersion, systemRoot, kargs, bootPrefix, kernelVersion)
generatedGrubConfigContents, err := os.ReadFile(generatedGrubConfigPath)
if err != nil {
PrintVerboseErr("createABSpecificGrub", 8, "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("createABSpecificGrub", 9, err)
return err
}
grubConfigWithBootEntry := strings.Replace(generatedGrubConfig, "REPLACED_BY_ABROOT", abrootBootConfig, 1)
err = os.WriteFile(confPath, []byte(grubConfigWithBootEntry), 0644)
if err != nil {
PrintVerboseErr("createABSpecificGrub", 10, "could not read grub config", err)
return err
}
PrintVerboseInfo("createABSpecificGrub", "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
}
}