forked from u-root/u-root
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
94 lines (82 loc) · 2.87 KB
/
main.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
// Copyright 2017-2019 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 (
"flag"
"log"
"os"
"os/exec"
"os/signal"
"time"
"github.com/u-root/u-root/pkg/booter"
)
var (
allowInteractive = flag.Bool("i", true, "Allow user to interrupt boot process and run commands")
doQuiet = flag.Bool("q", false, "Disable verbose output")
interval = flag.Int("I", 1, "Interval in seconds before looping to the next boot command")
noDefaultBoot = flag.Bool("nodefault", false, "Do not attempt default boot entries if regular ones fail")
)
var defaultBootsequence = [][]string{
{"fbnetboot", "-userclass", "linuxboot"},
{"localboot", "-grub"},
}
func main() {
flag.Parse()
log.Print(`
____ _ _ _
/ ___| _ _ ___| |_ ___ _ __ ___ | |__ ___ ___ | |_
\___ \| | | / __| __/ _ \ '_ ` + "`" + ` _ \| '_ \ / _ \ / _ \| __|
___) | |_| \__ \ || __/ | | | | | |_) | (_) | (_) | |_
|____/ \__, |___/\__\___|_| |_| |_|_.__/ \___/ \___/ \__|
|___/
`)
sleepInterval := time.Duration(*interval) * time.Second
if *allowInteractive {
log.Printf("**************************************************************************")
log.Print("Starting boot sequence, press CTRL-C within 5 seconds to drop into a shell")
log.Printf("**************************************************************************")
time.Sleep(5 * time.Second)
} else {
signal.Ignore()
}
// Get and show boot entries
bootEntries := booter.GetBootEntries()
log.Printf("BOOT ENTRIES:")
for _, entry := range bootEntries {
log.Printf(" %v) %+v", entry.Name, string(entry.Config))
}
for _, entry := range bootEntries {
log.Printf("Trying boot entry %s: %s", entry.Name, string(entry.Config))
if err := entry.Booter.Boot(); err != nil {
log.Printf("Warning: failed to boot with configuration: %+v", entry)
}
if !*doQuiet {
log.Printf("Sleeping %v before attempting next boot command", sleepInterval)
}
time.Sleep(sleepInterval)
}
// if boot entries failed, use the default boot sequence
log.Printf("Boot entries failed")
if !*noDefaultBoot {
log.Print("Falling back to the default boot sequence")
for {
for _, bootcmd := range defaultBootsequence {
if !*doQuiet {
bootcmd = append(bootcmd, "-d")
}
log.Printf("Running boot command: %v", bootcmd)
cmd := exec.Command(bootcmd[0], bootcmd[1:]...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
log.Printf("Error executing %v: %v", cmd, err)
}
}
if !*doQuiet {
log.Printf("Sleeping %v before attempting next boot command", sleepInterval)
}
time.Sleep(sleepInterval)
}
}
}