-
Notifications
You must be signed in to change notification settings - Fork 64
/
runner_kernel.go
85 lines (72 loc) · 2.21 KB
/
runner_kernel.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
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2022, Unikraft GmbH and The KraftKit Authors.
// Licensed under the BSD-3-Clause License (the "License").
// You may not use this file except in compliance with the License.
package run
import (
"context"
"debug/elf"
"fmt"
"path/filepath"
machineapi "kraftkit.sh/api/machine/v1alpha1"
"kraftkit.sh/unikraft"
)
// runnerKernel is a simple runner used for instantiating a prebuilt Unikraft
// unikernel which is used in the most verbose usecase. E.g.:
//
// $ kraft run path/to/kernel_qemu-x86_64
type runnerKernel struct {
kernelPath string
args []string
}
// String implements Runner.
func (runner *runnerKernel) String() string {
return "kernel"
}
// Runnable implements Runner.
func (runner *runnerKernel) Runnable(ctx context.Context, opts *Run, args ...string) (bool, error) {
if len(args) == 0 {
return false, fmt.Errorf("no arguments supplied")
}
var err error
runner.kernelPath, err = filepath.Abs(args[0])
if err != nil {
return false, err
}
runner.args = args[1:]
return unikraft.IsFileUnikraftUnikernel(runner.kernelPath)
}
// Prepare implements Runner.
func (runner *runnerKernel) Prepare(ctx context.Context, opts *Run, machine *machineapi.Machine, args ...string) error {
filename := filepath.Base(runner.kernelPath)
machine.Spec.Platform = opts.platform.String()
machine.Spec.Kernel = "kernel://" + filename
machine.Status.KernelPath = runner.kernelPath
machine.Spec.ApplicationArgs = runner.args
// We need to know the architecture pre-emptively, see if we can
// "intelligently" guess this by inspecting the ELF binary if the -m|--arch
// has not been provided.
if opts.Architecture == "" {
fe, err := elf.Open(runner.kernelPath)
if err != nil {
return err
}
defer fe.Close()
switch fe.Machine {
case elf.EM_X86_64, elf.EM_386:
machine.Spec.Architecture = "x86_64"
case elf.EM_ARM:
machine.Spec.Architecture = "arm"
case elf.EM_AARCH64:
machine.Spec.Architecture = "arm64"
default:
return fmt.Errorf("unsupported kernel architecture: %v", fe.Machine.String())
}
} else {
machine.Spec.Architecture = opts.Architecture
}
if len(opts.InitRd) > 0 {
machine.Status.InitrdPath = opts.InitRd
}
return nil
}