forked from insomniacslk/u-root
-
Notifications
You must be signed in to change notification settings - Fork 1
/
boot.go
154 lines (137 loc) · 4.24 KB
/
boot.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
// Copyright 2017-2018 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"errors"
"flag"
"fmt"
"log"
"strconv"
"strings"
"github.com/u-root/u-root/pkg/boot/diskboot"
"github.com/u-root/u-root/pkg/boot/kexec"
"github.com/u-root/u-root/pkg/cmdline"
"github.com/u-root/u-root/pkg/mount"
)
var (
v = flag.Bool("v", false, "Print debug messages")
verbose = func(string, ...interface{}) {}
dryrun = flag.Bool("dryrun", false, "Only print out kexec commands")
devGlob = flag.String("dev", "/sys/class/block/*", "Device glob")
sDeviceIndex = flag.String("d", "", "Device index")
sConfigIndex = flag.String("c", "", "Config index")
sEntryIndex = flag.String("n", "", "Entry index")
removeCmdlineItem = flag.String("remove", "console", "comma separated list of kernel params value to remove from parsed kernel configuration (default to console)")
reuseCmdlineItem = flag.String("reuse", "console", "comma separated list of kernel params value to reuse from current kernel (default to console)")
appendCmdline = flag.String("append", "", "Additional kernel params")
devices []*diskboot.Device
)
func getDevice() (*diskboot.Device, error) {
devices = diskboot.FindDevices(*devGlob)
if len(devices) == 0 {
return nil, errors.New("No devices found")
}
verbose("Got devices: %#v", devices)
var err error
deviceIndex := 0
if len(devices) > 1 {
if *sDeviceIndex == "" {
for i, device := range devices {
log.Printf("Device #%v: %s", i, device)
}
return nil, errors.New("multiple devices found - must specify a device index")
}
if deviceIndex, err = strconv.Atoi(*sDeviceIndex); err != nil ||
deviceIndex < 0 || deviceIndex >= len(devices) {
return nil, fmt.Errorf("invalid device index %q", *sDeviceIndex)
}
}
return devices[deviceIndex], nil
}
func getConfig(device *diskboot.Device) (*diskboot.Config, error) {
configs := device.Configs
if len(configs) == 0 {
return nil, errors.New("No config found")
}
verbose("Got configs: %#v", configs)
var err error
configIndex := 0
if len(configs) > 1 {
if *sConfigIndex == "" {
for i, config := range configs {
log.Printf("Config #%v: path: %v", i, config.ConfigPath)
}
return nil, errors.New("Multiple configs found - must specify a config index")
}
if configIndex, err = strconv.Atoi(*sConfigIndex); err != nil ||
configIndex < 0 || configIndex >= len(configs) {
return nil, fmt.Errorf("invalid config index %q", *sConfigIndex)
}
}
return configs[configIndex], nil
}
func getEntry(config *diskboot.Config) (*diskboot.Entry, error) {
verbose("Got entries: %#v", config.Entries)
var err error
entryIndex := 0
if *sEntryIndex != "" {
if entryIndex, err = strconv.Atoi(*sEntryIndex); err != nil ||
entryIndex < 0 || entryIndex >= len(config.Entries) {
return nil, fmt.Errorf("invalid entry index %q", *sEntryIndex)
}
} else if config.DefaultEntry >= 0 {
entryIndex = config.DefaultEntry
} else {
for i, entry := range config.Entries {
log.Printf("Entry #%v: %#v", i, entry)
}
return nil, errors.New("No entry specified")
}
return &config.Entries[entryIndex], nil
}
func bootEntry(config *diskboot.Config, entry *diskboot.Entry) error {
verbose("Booting entry: %v", entry)
filter := cmdline.NewUpdateFilter(*appendCmdline, strings.Split(*removeCmdlineItem, ","), strings.Split(*reuseCmdlineItem, ","))
err := entry.KexecLoad(config.MountPath, filter, *dryrun)
if err != nil {
return fmt.Errorf("error doing kexec load: %v", err)
}
if *dryrun {
return nil
}
err = kexec.Reboot()
if err != nil {
return fmt.Errorf("error doing kexec reboot: %v", err)
}
return nil
}
func cleanDevices() {
for _, device := range devices {
if err := device.Unmount(mount.MNT_FORCE); err != nil {
log.Printf("Error unmounting device %s: %v", device, err)
}
}
}
func main() {
flag.Parse()
if *v {
verbose = log.Printf
}
defer cleanDevices()
device, err := getDevice()
if err != nil {
log.Fatal(err)
}
config, err := getConfig(device)
if err != nil {
log.Fatal(err)
}
entry, err := getEntry(config)
if err != nil {
log.Fatal(err)
}
if err := bootEntry(config, entry); err != nil {
log.Fatal(err)
}
}