forked from u-root/u-root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
esxiboot.go
120 lines (111 loc) · 3.24 KB
/
esxiboot.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
// Copyright 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.
// esxiboot executes ESXi kernel over the running kernel.
//
// Synopsis:
// esxiboot [-d --device] [-c --config] [-r --cdrom]
//
// Description:
// Loads and executes ESXi kernel.
//
// Options:
// --config=FILE or -c=FILE: set the ESXi config
// --device=FILE or -d=FILE: set an ESXi disk to boot from
// --cdrom=FILE or -r=FILE: set an ESXI CDROM to boot from
// --append: append kernel cmdline arguments
//
// --device is required to kexec installed ESXi instance.
// You don't need it if you kexec ESXi installer.
//
// The config file has the following syntax:
//
// kernel=PATH
// kernelopt=OPTS
// modules=MOD1 [ARGS] --- MOD2 [ARGS] --- ...
//
// Lines starting with '#' are ignored.
package main
import (
"log"
"os"
"strings"
flag "github.com/spf13/pflag"
"github.com/u-root/u-root/pkg/boot"
"github.com/u-root/u-root/pkg/boot/esxi"
"github.com/u-root/u-root/pkg/mount"
)
var (
cfg = flag.StringP("config", "c", "", "ESXi config file")
cdrom = flag.StringP("cdrom", "r", "", "ESXi CDROM boot device")
diskDev = flag.StringP("device", "d", "", "ESXi disk boot device")
appendCmdline = flag.StringArray("append", nil, "Arguments to append to kernel cmdline")
dryRun = flag.Bool("dry-run", false, "dry run (just mount + load the kernel, don't kexec)")
)
func main() {
flag.Parse()
if *diskDev == "" && *cfg == "" && *cdrom == "" {
log.Printf("Either --config, --device, or --cdrom must be specified")
flag.PrintDefaults()
os.Exit(1)
}
if len(*diskDev) > 0 {
imgs, mps, err := esxi.LoadDisk(*diskDev)
if err != nil {
log.Fatalf("Failed to load ESXi configuration: %v", err)
}
loaded := false
for _, img := range imgs {
if len(*appendCmdline) > 0 {
img.Cmdline = img.Cmdline + " " + strings.Join(*appendCmdline, " ")
}
if err := img.Load(false); err != nil {
log.Printf("Failed to load ESXi image (%v) into memory: %v", img, err)
} else {
log.Printf("Loaded image: %v", img)
// We loaded one, that's it.
loaded = true
break
}
}
for _, mp := range mps {
if err := mp.Unmount(mount.MNT_DETACH); err != nil {
log.Printf("Failed to unmount %s: %v", mp, err)
}
}
if !loaded {
log.Fatalf("Failed to load all ESXi images found.")
}
} else {
var err error
var img *boot.MultibootImage
var mp *mount.MountPoint
if len(*cfg) > 0 {
img, err = esxi.LoadConfig(*cfg)
} else if len(*cdrom) > 0 {
img, mp, err = esxi.LoadCDROM(*cdrom)
}
if err != nil {
log.Fatalf("Failed to load ESXi configuration: %v", err)
}
if len(*appendCmdline) > 0 {
img.Cmdline = img.Cmdline + " " + strings.Join(*appendCmdline, " ")
}
if err := img.Load(false); err != nil {
log.Fatalf("Failed to load ESXi image (%v) into memory: %v", img, err)
}
log.Printf("Loaded image: %v", img)
if mp != nil {
if err := mp.Unmount(mount.MNT_DETACH); err != nil {
log.Printf("Failed to unmount %s: %v", mp, err)
}
}
}
if *dryRun {
log.Printf("Dry run: not booting kernel.")
os.Exit(0)
}
if err := boot.Execute(); err != nil {
log.Fatalf("Failed to boot image: %v", err)
}
}