diff --git a/pkg/unikontainers/config.go b/pkg/unikontainers/config.go index 9184db979..e0abba930 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" + annotSolo5BlkDev = "com.urunc.unikernel.blkDev" ) // 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"` + Solo5BlkDev string `json:"com.urunc.unikernel.blkDev,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] + solo5BlkDev := spec.Annotations[annotSolo5BlkDev] uniklog.WithFields(logrus.Fields{ "unikernelType": tryDecode(unikernelType), "unikernelVersion": tryDecode(unikernelVersion), @@ -129,6 +132,7 @@ func getConfigFromSpec(spec *specs.Spec) *UnikernelConfig { "block": tryDecode(block), "blkMntPoint": tryDecode(blkMntPoint), "mountRootfs": tryDecode(MountRootfs), + "solo5BlkDev": tryDecode(solo5BlkDev), }).WithField("source", "spec").Debug("urunc annotations") return &UnikernelConfig{ @@ -141,6 +145,7 @@ func getConfigFromSpec(spec *specs.Spec) *UnikernelConfig { Block: block, BlkMntPoint: blkMntPoint, MountRootfs: MountRootfs, + Solo5BlkDev: solo5BlkDev, } } @@ -180,6 +185,7 @@ func getConfigFromJSON(jsonFilePath string) (*UnikernelConfig, error) { "block": tryDecode(conf.Block), "blkMntPoint": tryDecode(conf.BlkMntPoint), "mountRootfs": tryDecode(conf.MountRootfs), + "solo5BlkDev": tryDecode(conf.Solo5BlkDev), }).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.Solo5BlkDev) + if err != nil { + return fmt.Errorf("failed to decode blkDev: %v", err) + } + c.Solo5BlkDev = string(decoded) + return nil } @@ -283,6 +295,9 @@ func (c *UnikernelConfig) Map() map[string]string { if c.MountRootfs != "" { myMap[annotMountRootfs] = c.MountRootfs } + if c.Solo5BlkDev != "" { + myMap[annotSolo5BlkDev] = c.Solo5BlkDev + } return myMap } diff --git a/pkg/unikontainers/config_test.go b/pkg/unikontainers/config_test.go index 5579122f3..ebfdba9cd 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", + annotSolo5BlkDev: "mydisk", }, } @@ -50,6 +51,7 @@ func TestGetConfigFromSpec(t *testing.T) { Block: "block1", BlkMntPoint: "point1", MountRootfs: "true", + Solo5BlkDev: "mydisk", } config := getConfigFromSpec(spec) @@ -239,6 +241,7 @@ func TestMap(t *testing.T) { Block: "block_value", BlkMntPoint: "point_value", MountRootfs: "false", + Solo5BlkDev: "blkdev_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", + annotSolo5BlkDev: "blkdev_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..8d6642d3d 100644 --- a/pkg/unikontainers/types/types.go +++ b/pkg/unikontainers/types/types.go @@ -86,10 +86,12 @@ 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 - Net NetDevParams - Block []BlockDevParams - Rootfs RootfsParams // Information about rootfs - ProcConf ProcessConfig // Information for the process execution inside the guest + // Solo5BlkDevName is the Solo5 block device name declared at build time + Solo5BlkDevName string + Net NetDevParams + Block []BlockDevParams + Rootfs RootfsParams // Information about rootfs + ProcConf ProcessConfig // Information for the process execution inside the guest } // ExecArgs holds the data required by Execve to start the VMM diff --git a/pkg/unikontainers/unikernels/mirage.go b/pkg/unikontainers/unikernels/mirage.go index 17ad02389..567f279e2 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 + solo5BlkDevName string } type MirageNet struct { @@ -83,7 +84,7 @@ func (m *Mirage) MonitorBlockCli() []types.MonitorBlockArgs { // how MirageOS handles/configures them. return []types.MonitorBlockArgs{ { - ID: "storage", + ID: m.solo5BlkDevName, Path: m.Block[0].HostPath, }, } @@ -118,6 +119,12 @@ func (m *Mirage) Init(data types.UnikernelParams) error { m.Command = strings.Join(data.CmdLine, " ") m.Monitor = data.Monitor + if data.Solo5BlkDevName != "" { + m.solo5BlkDevName = data.Solo5BlkDevName + } else { + m.solo5BlkDevName = "storage" + } + return nil } diff --git a/pkg/unikontainers/unikernels/mirage_test.go b/pkg/unikontainers/unikernels/mirage_test.go index 6d749bb41..9eb311f72 100644 --- a/pkg/unikontainers/unikernels/mirage_test.go +++ b/pkg/unikontainers/unikernels/mirage_test.go @@ -73,3 +73,33 @@ func TestMirageInitSubnetMask(t *testing.T) { }) } } + +func TestMirageSolo5BlkDevName(t *testing.T) { + t.Run("uses block device name from annotation", func(t *testing.T) { + t.Parallel() + m := &Mirage{} + err := m.Init(types.UnikernelParams{ + Monitor: "hvt", + Solo5BlkDevName: "mydisk", + Block: []types.BlockDevParams{{Source: "/path/to/img"}}, + }) + assert.NoError(t, err) + args := m.MonitorBlockCli() + assert.Len(t, args, 1) + assert.Equal(t, "mydisk", args[0].ID) + assert.Equal(t, "/path/to/img", args[0].Path) + }) + + t.Run("falls back to storage when annotation is absent", func(t *testing.T) { + t.Parallel() + m := &Mirage{} + err := m.Init(types.UnikernelParams{ + Monitor: "hvt", + Block: []types.BlockDevParams{{Source: "/path/to/img"}}, + }) + assert.NoError(t, err) + args := m.MonitorBlockCli() + assert.Len(t, args, 1) + assert.Equal(t, "storage", args[0].ID) + }) +} diff --git a/pkg/unikontainers/unikontainers.go b/pkg/unikontainers/unikontainers.go index fc297f937..c1a9e6a8d 100644 --- a/pkg/unikontainers/unikontainers.go +++ b/pkg/unikontainers/unikontainers.go @@ -391,11 +391,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, + Solo5BlkDevName: u.State.Annotations[annotSolo5BlkDev], } if len(unikernelParams.CmdLine) == 0 { unikernelParams.CmdLine = strings.Fields(u.State.Annotations[annotCmdLine])