/
netbooter.go
119 lines (112 loc) · 3.79 KB
/
netbooter.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
// Copyright 2017-2023 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 systembooter
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"strconv"
"github.com/u-root/u-root/pkg/ulog"
)
// NetBooter implements the Booter interface for booting over DHCPv6.
// See NewNetBooterDHCPv6 for details on the fields.
type NetBooter struct {
Type string `json:"type"`
Method string `json:"method"`
MAC string `json:"mac"`
OverrideURL *string `json:"override_url,omitempty"`
Retries *int `json:"retries,omitempty"`
DebugOnFailure bool `json:"debug_on_failure,omitempty"`
}
// NewNetBooter parses a boot entry config and returns a Booter instance, or an
// error if any
func NewNetBooter(config []byte, l ulog.Logger) (Booter, error) {
// The configuration format for a NetBooterDHCPv6 entry is a JSON with the
// following structure:
// {
// "type": "netboot",
// "method": "<method>",
// "mac": "<mac_addr>",
// "override_url": "<url>",
// "retries": <num_retries>,
// "debug_on_failure": <true|false>
// }
//
// `type` is always set to "netboot".
// `method` is one of "dhcpv6", "slaac" or "dhcpv4".
// `mac` is the MAC address of the interface to use to boot.
// `override_url` is an optional URL used to override the boot file URL used
// to fetch the network boot program. This field becomes mandatory if
// `method` is set to "slaac".
// `retries` is the number of times a DHCP request should be retried if
// failed. If unspecified, it will use the underlying `netboot` program's
// default.
// `debug_on_failure` is an optional boolean that will signal a request for
// a debugging attempt if netboot fails.
//
// An example configuration is:
// {
// "type": "netboot",
// "method": "dhcpv6",
// "mac": "aa:bb:cc:dd:ee:ff",
// "override_url": "http://[fe80::face:booc]:8080/path/to/boot/file"
// }
//
// Note that the optional `override_url` in the example above will override
// whatever URL is returned in the OPT_BOOTFILE_URL for DHCPv6, or TFTP server
// name + bootfile URL in case of DHCPv4.
//
// Additional options may be added in the future.
l.Printf("Trying NetBooter...")
l.Printf("Config: %s", string(config))
nb := NetBooter{}
if err := json.Unmarshal(config, &nb); err != nil {
return nil, err
}
l.Printf("NetBooter: %+v", nb)
if nb.Type != "netboot" {
return nil, fmt.Errorf("%w:%q", errWrongType, nb.Type)
}
return &nb, nil
}
// Boot will run the boot procedure. In the case of NetBooter, it will call the
// `fbnetboot` command
func (nb *NetBooter) Boot(debugEnabled bool) error {
var bootcmd []string
var l ulog.Logger = ulog.Null
if debugEnabled {
bootcmd = []string{"fbnetboot", "-v", "-userclass", "linuxboot"}
l = ulog.Log
} else {
bootcmd = []string{"fbnetboot", "-userclass", "linuxboot"}
}
if nb.OverrideURL != nil {
bootcmd = append(bootcmd, "-netboot-url", *nb.OverrideURL)
}
if nb.Retries != nil {
bootcmd = append(bootcmd, "-retries", strconv.Itoa(*nb.Retries))
}
if nb.Method == "dhcpv6" {
bootcmd = append(bootcmd, []string{"-6=true", "-4=false"}...)
} else if nb.Method == "dhcpv4" {
bootcmd = append(bootcmd, []string{"-6=false", "-4=true"}...)
} else {
return fmt.Errorf("netboot: unknown method %s", nb.Method)
}
if nb.DebugOnFailure {
bootcmd = append(bootcmd, "-fix")
}
l.Printf("Executing command: %v", bootcmd)
cmd := exec.Command(bootcmd[0], bootcmd[1:]...)
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("error executing %v: %v", cmd, err)
}
return nil
}
// TypeName returns the name of the booter type
func (nb *NetBooter) TypeName() string {
return nb.Type
}