-
Notifications
You must be signed in to change notification settings - Fork 1
/
bootloader.go
331 lines (261 loc) · 9.14 KB
/
bootloader.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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package main
import (
"bytes"
"errors"
"fmt"
"os"
"strings"
"github.com/AspieSoft/go-regex-re2/v2"
"github.com/AspieSoft/goutil/bash"
)
func chrootBootloaderSetup(installDisk string, diskParts diskPartList, cpu cpuType, tarName string) error {
//todo: use alternative to systemd-boot for non-efi systems
// if [ -d /sys/firmware/efi ] (efi supported) else (efi not supported)
// also detect if device is removable (USB/SD)
// https://wiki.gentoo.org/wiki/Handbook:AMD64/Installation/Bootloader
// also look at other boot loaders
//todo: consider using 'lilo' for non-efi boot (and possibly for efi)
//todo: consider using 'efibootmgr' for uefi
// https://wiki.gentoo.org/wiki/Handbook:AMD64/Installation/Bootloader
/* if strings.Contains(tarName, "openrc") {
err = chrootSetupBootOpenRC(tarName)
if err != nil {
return err
}
}else if strings.Contains(tarName, "systemd") {
err = chrootSetupBootSystemd(tarName)
if err != nil {
return err
}
} */
if installUSB {
return chrootSetupBoot_usb(installDisk, diskParts, cpu, tarName)
}
if _, e := os.Stat("/sys/firmware/efi"); e == nil {
return chrootSetupBoot_efi(installDisk, diskParts, cpu, tarName)
}
return chrootSetupBoot_noefi(installDisk, diskParts, cpu, tarName)
}
func chrootSetupBoot_efi(installDisk string, diskParts diskPartList, cpu cpuType, tarName string) error {
//todo: install grub or an allternative that works with efi
// may also try efibootmgr to not have a secondary bootloader (if possible)
// https://wiki.gentoo.org/wiki/Handbook:AMD64/Installation/Bootloader
//!note: remember to test with secure boot
return nil
}
func chrootSetupBoot_noefi(installDisk string, diskParts diskPartList, cpu cpuType, tarName string) error {
fmt.Println("(chroot) installing sys-boot/lilo...")
errList := install(`sys-boot/lilo`)
if len(errList) != 0 {
chrootProgressAdd(2500)
return errors.New("(chroot) error: failed to install sys-boot/lilo")
}
chrootProgressAdd(1000)
fmt.Println("(chroot) configuring lilo...")
files, err := os.ReadDir("/boot")
if err != nil {
chrootProgressAdd(1500)
return errors.New("(chroot) error: failed to read directory '/boot'")
}
var vmLinuz string
var initRamFS string
for _, file := range files {
name := file.Name()
if strings.HasPrefix(name, "vmlinuz") {
vmLinuz = name
}else if strings.HasPrefix(name, "initramfs") {
initRamFS = name
}
}
if vmLinuz == "" {
chrootProgressAdd(1500)
return errors.New("error: failed to find file '/boot/vmlinuz*'")
}
if initRamFS == "" {
chrootProgressAdd(1500)
return errors.New("error: failed to find file '/boot/initramfs*'")
}
vmLinuz = strings.Trim(vmLinuz, "\r\n ")
initRamFS = strings.Trim(initRamFS, "\r\n ")
chrootProgressAdd(100)
rootUUID, err := bash.RunRaw(`lsblk /dev/`+diskParts.root+` -lino UUID | head -n1`, "", nil)
if err != nil || len(rootUUID) == 0 {
chrootProgressAdd(1400)
return errors.New("error: failed to find uuid of root partition")
}
liloConf := regex.JoinBytes(
`boot=`, installDisk, '\n',
// `prompt`, '\n',
// `timeout=50`, '\n',
`default=`, distroName, '\n',
`compact`, '\n',
'\n',
`image=`, `/boot/`, vmLinuz, '\n',
` label=`, distroName, '\n',
` read-only`, '\n',
` append="root=uuid="`, bytes.Trim(rootUUID, "\r\n "), '\n',
` initrd=/boot/`, initRamFS, '\n',
)
chrootProgressAdd(100)
if diskParts.rootB != "" {
rootUUID, err := bash.RunRaw(`lsblk /dev/`+diskParts.rootB+` -lino UUID | head -n1`, "", nil)
if err != nil || len(rootUUID) == 0 {
chrootProgressAdd(1300)
return errors.New("error: failed to find uuid of root partition")
}
liloConf = append(liloConf, regex.JoinBytes(
'\n',
`image=`, `/boot/`, vmLinuz, '\n',
` label=`, distroName, `.back`, '\n',
` read-only`, '\n',
` append="root=uuid="`, bytes.Trim(rootUUID, "\r\n "), '\n',
` initrd=/boot/`, initRamFS, '\n',
)...)
}
chrootProgressAdd(100)
liloConf = append(liloConf, regex.JoinBytes(
'\n',
`image=`, `/boot/`, vmLinuz, '\n',
` label=`, distroName, `.rescue`, '\n',
` read-only`, '\n',
` append="root=uuid="`, bytes.Trim(rootUUID, "\r\n "), '\n',
` initrd=/boot/`, initRamFS, '\n',
` append="init=/bin/bb"`, '\n',
)...)
chrootProgressAdd(100)
os.WriteFile("/etc/lilo.conf", liloConf, 0644)
chrootProgressAdd(100)
_, err = bash.Run([]string{`/sbin/lilo`}, "", nil, true, false)
if err != nil {
chrootProgressAdd(1000)
return errors.New("(chroot) error: failed to install lilo config")
}
chrootProgressAdd(1000)
return nil
}
func chrootSetupBoot_usb(installDisk string, diskParts diskPartList, cpu cpuType, tarName string) error {
//todo: install syslinux for live usb bootloader
// https://wiki.gentoo.org/wiki/Syslinux
// https://wiki.gentoo.org/wiki/LiveUSB
return nil
}
//!old
func chrootSetupBootOpenRC(tarName string) error {
fmt.Println("(chroot) installing systemd-boot for openrc...")
errList := install(`sys-apps/systemd-utils`)
if len(errList) != 0 {
chrootProgressAdd(3000)
return errors.New("(chroot) error: failed to install sys-apps/systemd-utils")
}
chrootProgressAdd(1000)
os.MkdirAll("/etc/portage/package.use", 0644)
err := os.WriteFile("/etc/portage/package.use/systemd-utils", []byte("sys-apps/systemd-utils boot"), 0644)
if err != nil {
chrootProgressAdd(2000)
return errors.New("(chroot) error: failed to write file '/etc/portage/package.use/systemd-utils'")
}
_, err = bash.Run([]string{`emerge`, `--oneshot`, `--quiet`, `sys-apps/systemd-utils`}, "/", nil, true, false)
if len(errList) != 0 {
chrootProgressAdd(2000)
return errors.New("(chroot) error: failed to reinstall sys-apps/systemd-utils")
}
chrootProgressAdd(1000)
_, err = bash.Run([]string{`bootctl`, `install`}, "/", nil, true, false)
if len(errList) != 0 {
chrootProgressAdd(1000)
return errors.New("(chroot) error: failed to run 'bootctl install'")
}
chrootProgressAdd(1000)
return nil
}
func chrootSetupBootSystemd(tarName string) error {
fmt.Println("(chroot) installing systemd-boot for systemd...")
errList := install(`sys-apps/systemd`)
if len(errList) != 0 {
chrootProgressAdd(3000)
return errors.New("(chroot) error: failed to install sys-apps/systemd")
}
chrootProgressAdd(1000)
os.MkdirAll("/etc/portage/package.use", 0644)
err := os.WriteFile("/etc/portage/package.use/systemd", []byte("sys-apps/systemd boot"), 0644)
if err != nil {
chrootProgressAdd(2000)
return errors.New("(chroot) error: failed to write file '/etc/portage/package.use/systemd'")
}
_, err = bash.Run([]string{`emerge`, `--oneshot`, `--quiet`, `sys-apps/systemd`}, "/", nil, true, false)
if len(errList) != 0 {
chrootProgressAdd(2000)
return errors.New("(chroot) error: failed to reinstall sys-apps/systemd")
}
chrootProgressAdd(1000)
_, err = bash.Run([]string{`bootctl`, `install`}, "/", nil, true, false)
if len(errList) != 0 {
chrootProgressAdd(1000)
return errors.New("(chroot) error: failed to run 'bootctl install'")
}
chrootProgressAdd(1000)
return nil
}
func chrootSetupBoot(diskParts diskPartList, tarName string) error {
fmt.Println("(chroot) configuring systemd-boot...")
files, err := os.ReadDir("/boot")
if err != nil {
chrootProgressAdd(1500)
return errors.New("(chroot) error: failed to read directory '/boot'")
}
var vmLinuz string
var initRamFS string
for _, file := range files {
name := file.Name()
if strings.HasPrefix(name, "vmlinuz") {
vmLinuz = name
}else if strings.HasPrefix(name, "initramfs") {
initRamFS = name
}
}
if vmLinuz == "" {
chrootProgressAdd(1500)
return errors.New("error: failed to find file '/boot/vmlinuz*'")
}
if initRamFS == "" {
chrootProgressAdd(1500)
return errors.New("error: failed to find file '/boot/initramfs*'")
}
vmLinuz = strings.Trim(vmLinuz, "\r\n ")
initRamFS = strings.Trim(initRamFS, "\r\n ")
chrootProgressAdd(100)
rootUUID, err := bash.RunRaw(`lsblk /dev/`+diskParts.root+` -lino UUID | head -n1`, "", nil)
if err != nil || len(rootUUID) == 0 {
chrootProgressAdd(1400)
return errors.New("error: failed to find uuid of root partition")
}
os.WriteFile("/boot/loader/entries/"+strings.ToLower(distroName)+".conf", regex.JoinBytes(
`title `, distroName, '\n',
`linux /`, vmLinuz, '\n',
`initrd /`, initRamFS, '\n',
`options root="UUID=`, bytes.Trim(rootUUID, "\r\n "), `" quiet`, '\n',
), 0644)
chrootProgressAdd(200)
if diskParts.rootB != "" {
rootUUID, err := bash.RunRaw(`lsblk /dev/`+diskParts.rootB+` -lino UUID | head -n1`, "", nil)
if err != nil || len(rootUUID) == 0 {
chrootProgressAdd(1200)
return errors.New("error: failed to find uuid of root partition")
}
os.WriteFile("/boot/loader/entries/"+strings.ToLower(distroName)+"-back.conf", regex.JoinBytes(
`title `, distroName, ` (Back)`, '\n',
`linux /`, vmLinuz, '\n',
`initrd /`, initRamFS, '\n',
`options root="UUID=`, bytes.Trim(rootUUID, "\r\n "), `" quiet`, '\n',
), 0644)
}
chrootProgressAdd(200)
//todo: find out how to set default boot entry (systemd-boot)
errList := install(`sys-kernel/installkernel-systemd-boot`)
if len(errList) != 0 {
chrootProgressAdd(1000)
return errors.New("(chroot) error: failed to install sys-kernel/installkernel-systemd-boot")
}
chrootProgressAdd(1000)
return nil
}