From 3a348ee320dc1ee9c36062dc653db2895a8fc239 Mon Sep 17 00:00:00 2001 From: viju Date: Thu, 23 Jul 2026 20:34:04 +0530 Subject: [PATCH 1/2] fix: use annotation for MirageOS net device name Ref: #315 Signed-off-by: viju --- pkg/unikontainers/config.go | 15 ++++++++++ pkg/unikontainers/config_test.go | 4 +++ pkg/unikontainers/types/types.go | 1 + pkg/unikontainers/unikernels/mirage.go | 33 ++++++++++++++++----- pkg/unikontainers/unikernels/mirage_test.go | 22 ++++++++++++++ pkg/unikontainers/unikontainers.go | 11 +++---- 6 files changed, 73 insertions(+), 13 deletions(-) diff --git a/pkg/unikontainers/config.go b/pkg/unikontainers/config.go index 4649ea681..14a4cb1a7 100644 --- a/pkg/unikontainers/config.go +++ b/pkg/unikontainers/config.go @@ -45,6 +45,7 @@ const ( annotBlock = "com.urunc.unikernel.block" annotBlockMntPoint = "com.urunc.unikernel.blkMntPoint" annotMountRootfs = "com.urunc.unikernel.mountRootfs" + annotNetDev = "com.urunc.unikernel.solo5NetDev" ) // A UnikernelConfig struct holds the info provided by bima image on how to execute our unikernel @@ -58,6 +59,7 @@ type UnikernelConfig struct { Block string `json:"com.urunc.unikernel.block,omitempty"` BlkMntPoint string `json:"com.urunc.unikernel.blkMntPoint,omitempty"` MountRootfs string `json:"com.urunc.unikernel.mountRootfs"` + NetDev string `json:"com.urunc.unikernel.solo5NetDev,omitempty"` } // validate checks if the mandatory configuration fields are present. @@ -119,6 +121,7 @@ func getConfigFromSpec(spec *specs.Spec) *UnikernelConfig { block := spec.Annotations[annotBlock] blkMntPoint := spec.Annotations[annotBlockMntPoint] MountRootfs := spec.Annotations[annotMountRootfs] + netDev := spec.Annotations[annotNetDev] uniklog.WithFields(logrus.Fields{ "unikernelType": unikernelType, "unikernelVersion": unikernelVersion, @@ -129,6 +132,7 @@ func getConfigFromSpec(spec *specs.Spec) *UnikernelConfig { "block": block, "blkMntPoint": blkMntPoint, "mountRootfs": MountRootfs, + "netDev": netDev, }).WithField("source", "spec").Debug("urunc annotations") return &UnikernelConfig{ @@ -141,6 +145,7 @@ func getConfigFromSpec(spec *specs.Spec) *UnikernelConfig { Block: block, BlkMntPoint: blkMntPoint, MountRootfs: MountRootfs, + NetDev: netDev, } } @@ -180,6 +185,7 @@ func getConfigFromJSON(jsonFilePath string) (*UnikernelConfig, error) { "block": tryDecode(conf.Block), "blkMntPoint": tryDecode(conf.BlkMntPoint), "mountRootfs": tryDecode(conf.MountRootfs), + "netDev": tryDecode(conf.NetDev), }).WithField("source", uruncJSONFilename).Debug("urunc annotations") return &conf, nil @@ -250,6 +256,12 @@ func (c *UnikernelConfig) decode() error { } c.MountRootfs = string(decoded) + decoded, err = base64.StdEncoding.DecodeString(c.NetDev) + if err != nil { + return fmt.Errorf("failed to decode netDev: %v", err) + } + c.NetDev = string(decoded) + return nil } @@ -283,6 +295,9 @@ func (c *UnikernelConfig) Map() map[string]string { if c.MountRootfs != "" { myMap[annotMountRootfs] = c.MountRootfs } + if c.NetDev != "" { + myMap[annotNetDev] = c.NetDev + } return myMap } diff --git a/pkg/unikontainers/config_test.go b/pkg/unikontainers/config_test.go index 5579122f3..fc8bf6070 100644 --- a/pkg/unikontainers/config_test.go +++ b/pkg/unikontainers/config_test.go @@ -38,6 +38,7 @@ func TestGetConfigFromSpec(t *testing.T) { annotBlock: "block1", annotBlockMntPoint: "point1", annotMountRootfs: "true", + annotNetDev: "management", }, } @@ -50,6 +51,7 @@ func TestGetConfigFromSpec(t *testing.T) { Block: "block1", BlkMntPoint: "point1", MountRootfs: "true", + NetDev: "management", } config := getConfigFromSpec(spec) @@ -239,6 +241,7 @@ func TestMap(t *testing.T) { Block: "block_value", BlkMntPoint: "point_value", MountRootfs: "false", + NetDev: "netdev_value", } expectedMap := map[string]string{ annotCmdLine: "cmd_value", @@ -249,6 +252,7 @@ func TestMap(t *testing.T) { annotBlock: "block_value", annotBlockMntPoint: "point_value", annotMountRootfs: "false", + annotNetDev: "netdev_value", } resultMap := config.Map() assert.Equal(t, expectedMap, resultMap) diff --git a/pkg/unikontainers/types/types.go b/pkg/unikontainers/types/types.go index c6388e2cc..04f5204b4 100644 --- a/pkg/unikontainers/types/types.go +++ b/pkg/unikontainers/types/types.go @@ -86,6 +86,7 @@ type UnikernelParams struct { Monitor string // The monitor where guest will execute Version string // The version of the unikernel InitrdPath string // The path to the initrd of the unikernel + NetDevName string // The name of the guest network device declared at build time Net NetDevParams Block []BlockDevParams Rootfs RootfsParams // Information about rootfs diff --git a/pkg/unikontainers/unikernels/mirage.go b/pkg/unikontainers/unikernels/mirage.go index 17ad02389..32292c813 100644 --- a/pkg/unikontainers/unikernels/mirage.go +++ b/pkg/unikontainers/unikernels/mirage.go @@ -24,10 +24,11 @@ import ( const MirageUnikernel string = "mirage" type Mirage struct { - Command string - Monitor string - Net MirageNet - Block []MirageBlock + Command string + Monitor string + Net MirageNet + Block []MirageBlock + netDevName string } type MirageNet struct { @@ -57,8 +58,8 @@ func (m *Mirage) SupportsFS(_ string) bool { func (m *Mirage) MonitorNetCli(ifName string, mac string) string { switch m.Monitor { case "hvt", "spt": - netOption := "--net:service=" + ifName - netOption += " --net-mac:service=" + mac + netOption := "--net:" + m.netDevName + "=" + ifName + netOption += " --net-mac:" + m.netDevName + "=" + mac return netOption default: return "" @@ -103,8 +104,18 @@ func (m *Mirage) Init(data types.UnikernelParams) error { if err != nil { return err } - m.Net.Address = fmt.Sprintf("--ipv4=%s/%d", data.Net.IP, mask) - m.Net.Gateway = "--ipv4-gateway=" + data.Net.Gateway + // Mirage groups a network device's command line options under a prefix, + // and that prefix is the device name. The default device "service" uses + // the plain --ipv4 and --ipv4-gateway flags, but any other device needs + // its name as the prefix, like --management-ipv4 for a device called + // "management". So we only add the prefix when the device is not "service". + if data.NetDevName != "" && data.NetDevName != "service" { + m.Net.Address = fmt.Sprintf("--%s-ipv4=%s/%d", data.NetDevName, data.Net.IP, mask) + m.Net.Gateway = "--" + data.NetDevName + "-ipv4-gateway=" + data.Net.Gateway + } else { + m.Net.Address = fmt.Sprintf("--ipv4=%s/%d", data.Net.IP, mask) + m.Net.Gateway = "--ipv4-gateway=" + data.Net.Gateway + } } m.Block = make([]MirageBlock, 0, len(data.Block)) for _, blk := range data.Block { @@ -118,6 +129,12 @@ func (m *Mirage) Init(data types.UnikernelParams) error { m.Command = strings.Join(data.CmdLine, " ") m.Monitor = data.Monitor + if data.NetDevName != "" { + m.netDevName = data.NetDevName + } else { + m.netDevName = "service" + } + return nil } diff --git a/pkg/unikontainers/unikernels/mirage_test.go b/pkg/unikontainers/unikernels/mirage_test.go index 6d749bb41..e762a7cf8 100644 --- a/pkg/unikontainers/unikernels/mirage_test.go +++ b/pkg/unikontainers/unikernels/mirage_test.go @@ -73,3 +73,25 @@ func TestMirageInitSubnetMask(t *testing.T) { }) } } + +func TestMirageNetDevName(t *testing.T) { + t.Run("uses net device name from annotation", func(t *testing.T) { + t.Parallel() + m := &Mirage{} + err := m.Init(types.UnikernelParams{Monitor: "hvt", NetDevName: "management"}) + assert.NoError(t, err) + cli := m.MonitorNetCli("tap0", "aa:bb:cc:dd:ee:ff") + assert.Contains(t, cli, "--net:management=tap0") + assert.Contains(t, cli, "--net-mac:management=aa:bb:cc:dd:ee:ff") + }) + + t.Run("falls back to service when annotation is absent", func(t *testing.T) { + t.Parallel() + m := &Mirage{} + err := m.Init(types.UnikernelParams{Monitor: "hvt"}) + assert.NoError(t, err) + cli := m.MonitorNetCli("tap0", "aa:bb:cc:dd:ee:ff") + assert.Contains(t, cli, "--net:service=tap0") + assert.Contains(t, cli, "--net-mac:service=aa:bb:cc:dd:ee:ff") + }) +} diff --git a/pkg/unikontainers/unikontainers.go b/pkg/unikontainers/unikontainers.go index 84172aca4..d3de43dfb 100644 --- a/pkg/unikontainers/unikontainers.go +++ b/pkg/unikontainers/unikontainers.go @@ -439,11 +439,12 @@ func (u *Unikontainer) Exec(metrics m.Writer) error { // UnikernelParams // populate unikernel params unikernelParams := types.UnikernelParams{ - CmdLine: u.Spec.Process.Args, - EnvVars: u.Spec.Process.Env, - Monitor: vmmType, - Version: unikernelVersion, - ProcConf: procAttrs, + CmdLine: u.Spec.Process.Args, + EnvVars: u.Spec.Process.Env, + Monitor: vmmType, + Version: unikernelVersion, + ProcConf: procAttrs, + NetDevName: u.State.Annotations[annotNetDev], } if len(unikernelParams.CmdLine) == 0 { unikernelParams.CmdLine = strings.Fields(u.State.Annotations[annotCmdLine]) From 1fd8c85b05d998215106fdee62cc034e903e4a2c Mon Sep 17 00:00:00 2001 From: viju Date: Fri, 24 Jul 2026 10:42:53 +0530 Subject: [PATCH 2/2] chore: add to contributors list Signed-off-by: viju --- .github/contributors.yaml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/contributors.yaml b/.github/contributors.yaml index 411f9f42a..80df2d170 100644 --- a/.github/contributors.yaml +++ b/.github/contributors.yaml @@ -107,3 +107,6 @@ users: Chennamma-Hotkar: name: Chennamma Hotkar email: channuhotkar@gmail.com + pocopepe: + name: Viju Sanjai + email: avijusanjai@gmail.com