forked from Code-Hex/vz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootloader.go
89 lines (76 loc) · 2.04 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
package vz
/*
#cgo darwin CFLAGS: -x objective-c -fno-objc-arc
#cgo darwin LDFLAGS: -lobjc -framework Foundation -framework Virtualization
# include "virtualization.h"
*/
import "C"
import (
"fmt"
"runtime"
)
// BootLoader is the interface of boot loader definitions.
// see: LinuxBootLoader
type BootLoader interface {
NSObject
bootLoader()
}
type baseBootLoader struct{}
func (*baseBootLoader) bootLoader() {}
var _ BootLoader = (*LinuxBootLoader)(nil)
// LinuxBootLoader Boot loader configuration for a Linux kernel.
type LinuxBootLoader struct {
vmlinuzPath string
initrdPath string
cmdLine string
pointer
*baseBootLoader
}
func (b *LinuxBootLoader) String() string {
return fmt.Sprintf(
"vmlinuz: %q, initrd: %q, command-line: %q",
b.vmlinuzPath,
b.initrdPath,
b.cmdLine,
)
}
type LinuxBootLoaderOption func(b *LinuxBootLoader)
// WithCommandLine sets the command-line parameters.
// see: https://www.kernel.org/doc/html/latest/admin-guide/kernel-parameters.html
func WithCommandLine(cmdLine string) LinuxBootLoaderOption {
return func(b *LinuxBootLoader) {
b.cmdLine = cmdLine
cs := charWithGoString(cmdLine)
defer cs.Free()
C.setCommandLineVZLinuxBootLoader(b.Ptr(), cs.CString())
}
}
// WithInitrd sets the optional initial RAM disk.
func WithInitrd(initrdPath string) LinuxBootLoaderOption {
return func(b *LinuxBootLoader) {
b.initrdPath = initrdPath
cs := charWithGoString(initrdPath)
defer cs.Free()
C.setInitialRamdiskURLVZLinuxBootLoader(b.Ptr(), cs.CString())
}
}
// NewLinuxBootLoader creates a LinuxBootLoader with the Linux kernel passed as Path.
func NewLinuxBootLoader(vmlinuz string, opts ...LinuxBootLoaderOption) *LinuxBootLoader {
vmlinuzPath := charWithGoString(vmlinuz)
defer vmlinuzPath.Free()
bootLoader := &LinuxBootLoader{
vmlinuzPath: vmlinuz,
pointer: pointer{
ptr: C.newVZLinuxBootLoader(
vmlinuzPath.CString(),
),
},
}
runtime.SetFinalizer(bootLoader, func(self *LinuxBootLoader) {
self.Release()
})
for _, opt := range opts {
opt(bootLoader)
}
return bootLoader
}