This repository was archived by the owner on Feb 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 128
/
Copy pathutils.go
156 lines (141 loc) · 4.22 KB
/
utils.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
155
156
package main
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"syscall"
specs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/urfave/cli"
)
const (
defaultKernelInstallDir string = "/var/lib/hyper"
)
func firstExistingFile(candidates []string) string {
for _, file := range candidates {
if _, err := os.Stat(file); err == nil {
return file
}
}
return ""
}
func getDefaultBundlePath() string {
cwd, err := os.Getwd()
if err != nil {
return ""
}
return cwd
}
// find it from spec.Process.Env, runv's env(todo) and context.GlobalString
func chooseKernelFromConfigs(context *cli.Context, spec *specs.Spec) string {
for k, env := range spec.Process.Env {
slices := strings.Split(env, "=")
if len(slices) == 2 && slices[0] == "hypervisor.kernel" {
// remove kernel env because this is only allow to be used by runv
spec.Process.Env = append(spec.Process.Env[:k], spec.Process.Env[k+1:]...)
return slices[1]
}
}
return context.GlobalString("kernel")
}
func chooseInitrdFromConfigs(context *cli.Context, spec *specs.Spec) string {
for k, env := range spec.Process.Env {
slices := strings.Split(env, "=")
if len(slices) == 2 && slices[0] == "hypervisor.initrd" {
// remove initrd env because this is only allow to be used by runv
spec.Process.Env = append(spec.Process.Env[:k], spec.Process.Env[k+1:]...)
return slices[1]
}
}
return context.GlobalString("initrd")
}
func chooseBiosFromConfigs(context *cli.Context, spec *specs.Spec) string {
for k, env := range spec.Process.Env {
slices := strings.Split(env, "=")
if len(slices) == 2 && slices[0] == "hypervisor.bios" {
// remove bios env because this is only allow to be used by runv
spec.Process.Env = append(spec.Process.Env[:k], spec.Process.Env[k+1:]...)
return slices[1]
}
}
return context.GlobalString("bios")
}
func chooseCbfsFromConfigs(context *cli.Context, spec *specs.Spec) string {
for k, env := range spec.Process.Env {
slices := strings.Split(env, "=")
if len(slices) == 2 && slices[0] == "hypervisor.cbfs" {
// remove cbfs env because this is only allow to be used by runv
spec.Process.Env = append(spec.Process.Env[:k], spec.Process.Env[k+1:]...)
return slices[1]
}
}
return context.GlobalString("cbfs")
}
// getKernelFiles chooses kernel/initrd/bios/cbfs files based on user specified ones
func getKernelFiles(context *cli.Context, spec *specs.Spec) (string, string, string, string, error) {
kernel := chooseKernelFromConfigs(context, spec)
initrd := chooseInitrdFromConfigs(context, spec)
bios := chooseBiosFromConfigs(context, spec)
cbfs := chooseCbfsFromConfigs(context, spec)
bundle := context.String("bundle")
if kernel != "" && initrd != "" {
return kernel, initrd, bios, cbfs, nil
}
// choose from filesystem
for k, v := range map[*string][]string{
&kernel: {
filepath.Join(bundle, spec.Root.Path, "boot/vmlinuz"),
filepath.Join(bundle, "boot/vmlinuz"),
filepath.Join(bundle, "vmlinuz"),
filepath.Join(defaultKernelInstallDir, "kernel"),
},
&initrd: {
filepath.Join(bundle, spec.Root.Path, "boot/initrd.img"),
filepath.Join(bundle, "boot/initrd.img"),
filepath.Join(bundle, "initrd.img"),
filepath.Join(defaultKernelInstallDir, "hyper-initrd.img"),
},
&bios: {
filepath.Join(bundle, spec.Root.Path, "boot/bios.bin"),
filepath.Join(bundle, "boot/bios.bin"),
filepath.Join(bundle, "bios.bin"),
filepath.Join(defaultKernelInstallDir, "bios.bin"),
},
&cbfs: {
filepath.Join(bundle, spec.Root.Path, "boot/cbfs.rom"),
filepath.Join(bundle, "boot/cbfs.rom"),
filepath.Join(bundle, "cbfs.rom"),
filepath.Join(defaultKernelInstallDir, "cbfs.rom"),
},
} {
if *k == "" {
*k = firstExistingFile(v)
}
if *k != "" {
var err error
*k, err = filepath.Abs(*k)
if err != nil {
return "", "", "", "", fmt.Errorf("cannot get abs path for kernel files: %v", err)
}
}
}
return kernel, initrd, bios, cbfs, nil
}
func osProcessWait(process *os.Process) (int, error) {
state, err := process.Wait()
if err != nil {
return -1, err
}
if state.Success() {
return 0, nil
}
ret := -1
if status, ok := state.Sys().(syscall.WaitStatus); ok {
ret = status.ExitStatus()
if ret != 0 {
err = errors.New("")
}
}
return ret, err
}