|
| 1 | +// Copyright (c) 2023-2026, Nubificus LTD |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package unikernels |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "runtime" |
| 20 | + "strings" |
| 21 | + |
| 22 | + "github.com/urunc-dev/urunc/pkg/unikontainers/types" |
| 23 | +) |
| 24 | + |
| 25 | +const HermitUnikernel string = "hermit" |
| 26 | + |
| 27 | +type Hermit struct { |
| 28 | + Command string |
| 29 | + Monitor string |
| 30 | + Net HermitNet |
| 31 | +} |
| 32 | + |
| 33 | +type HermitNet struct { |
| 34 | + Address string |
| 35 | + Mask int |
| 36 | + Gateway string |
| 37 | +} |
| 38 | + |
| 39 | +func (h *Hermit) CommandString() (string, error) { |
| 40 | + var args []string |
| 41 | + |
| 42 | + if h.Net.Address != "" { |
| 43 | + args = append(args, fmt.Sprintf("ip=%s/%d", h.Net.Address, h.Net.Mask)) |
| 44 | + } |
| 45 | + if h.Net.Gateway != "" { |
| 46 | + args = append(args, fmt.Sprintf("gateway=%s", h.Net.Gateway)) |
| 47 | + } |
| 48 | + |
| 49 | + // Add separator ONLY if we have net args AND a command |
| 50 | + appArgs := strings.TrimSpace(h.Command) |
| 51 | + if len(args) > 0 && appArgs != "" { |
| 52 | + args = append(args, "--") |
| 53 | + } |
| 54 | + |
| 55 | + if appArgs != "" { |
| 56 | + args = append(args, appArgs) |
| 57 | + } |
| 58 | + |
| 59 | + return strings.Join(args, " "), nil |
| 60 | +} |
| 61 | + |
| 62 | +func (h *Hermit) SupportsBlock() bool { |
| 63 | + return false |
| 64 | +} |
| 65 | + |
| 66 | +func (h *Hermit) SupportsFS(fsType string) bool { |
| 67 | + return fsType == "initrd" |
| 68 | +} |
| 69 | + |
| 70 | +func (h *Hermit) MonitorNetCli(ifName string, mac string) string { |
| 71 | + switch h.Monitor { |
| 72 | + case "qemu": |
| 73 | + netdev := fmt.Sprintf(" -netdev tap,id=net0,ifname=%s,script=no,downscript=no", ifName) |
| 74 | + |
| 75 | + var deviceArgs string |
| 76 | + |
| 77 | + // QEMU on x86_64 typically uses virtio-net-pci. |
| 78 | + // On arm64 virtio-net-device is the safer default. |
| 79 | + if runtime.GOARCH == "arm64" { |
| 80 | + deviceArgs = " -device " + "virtio-net-device" + ",netdev=net0" |
| 81 | + } else { |
| 82 | + deviceArgs = " -device " + "virtio-net-pci" + ",netdev=net0,disable-legacy=on" |
| 83 | + } |
| 84 | + |
| 85 | + if mac != "" { |
| 86 | + deviceArgs += ",mac=" + mac |
| 87 | + } |
| 88 | + |
| 89 | + return netdev + deviceArgs |
| 90 | + default: |
| 91 | + return "" |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +func (h *Hermit) MonitorBlockCli() []types.MonitorBlockArgs { |
| 96 | + return nil |
| 97 | +} |
| 98 | + |
| 99 | +func (h *Hermit) MonitorCli() types.MonitorCliArgs { |
| 100 | + return types.MonitorCliArgs{ |
| 101 | + OtherArgs: " -no-reboot", |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +func (h *Hermit) Init(data types.UnikernelParams) error { |
| 106 | + |
| 107 | + if data.Net.Mask != "" { |
| 108 | + mask, err := subnetMaskToCIDR(data.Net.Mask) |
| 109 | + if err != nil { |
| 110 | + return err |
| 111 | + } |
| 112 | + h.Net.Address = data.Net.IP |
| 113 | + h.Net.Gateway = data.Net.Gateway |
| 114 | + h.Net.Mask = mask |
| 115 | + } |
| 116 | + |
| 117 | + h.Command = strings.Join(data.CmdLine, " ") |
| 118 | + h.Monitor = data.Monitor |
| 119 | + |
| 120 | + return nil |
| 121 | +} |
| 122 | + |
| 123 | +func newHermit() *Hermit { |
| 124 | + return new(Hermit) |
| 125 | +} |
0 commit comments