diff --git a/pkg/unikontainers/config.go b/pkg/unikontainers/config.go index 9184db97..43be8d38 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" + annotBlkDev = "com.urunc.unikernel.solo5BlkDev" ) // 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"` + BlkDev string `json:"com.urunc.unikernel.solo5BlkDev,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] + blkDev := spec.Annotations[annotBlkDev] 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), + "blkDev": tryDecode(blkDev), }).WithField("source", "spec").Debug("urunc annotations") return &UnikernelConfig{ @@ -141,6 +145,7 @@ func getConfigFromSpec(spec *specs.Spec) *UnikernelConfig { Block: block, BlkMntPoint: blkMntPoint, MountRootfs: MountRootfs, + BlkDev: blkDev, } } @@ -180,6 +185,7 @@ func getConfigFromJSON(jsonFilePath string) (*UnikernelConfig, error) { "block": tryDecode(conf.Block), "blkMntPoint": tryDecode(conf.BlkMntPoint), "mountRootfs": tryDecode(conf.MountRootfs), + "blkDev": tryDecode(conf.BlkDev), }).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.BlkDev) + if err != nil { + return fmt.Errorf("failed to decode blkDev: %v", err) + } + c.BlkDev = string(decoded) + return nil } @@ -283,6 +295,9 @@ func (c *UnikernelConfig) Map() map[string]string { if c.MountRootfs != "" { myMap[annotMountRootfs] = c.MountRootfs } + if c.BlkDev != "" { + myMap[annotBlkDev] = c.BlkDev + } return myMap } diff --git a/pkg/unikontainers/config_test.go b/pkg/unikontainers/config_test.go index 5579122f..97410f9a 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", + annotBlkDev: "mydisk", }, } @@ -50,6 +51,7 @@ func TestGetConfigFromSpec(t *testing.T) { Block: "block1", BlkMntPoint: "point1", MountRootfs: "true", + BlkDev: "mydisk", } config := getConfigFromSpec(spec) @@ -239,6 +241,7 @@ func TestMap(t *testing.T) { Block: "block_value", BlkMntPoint: "point_value", MountRootfs: "false", + BlkDev: "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", + annotBlkDev: "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 c6388e2c..fc54e93c 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 + BlkDevName string // The name of the guest block 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 17ad0238..738bf3b9 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 + blkDevName 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.blkDevName, 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.BlkDevName != "" { + m.blkDevName = data.BlkDevName + } else { + m.blkDevName = "storage" + } + return nil } diff --git a/pkg/unikontainers/unikernels/mirage_test.go b/pkg/unikontainers/unikernels/mirage_test.go index 6d749bb4..7a76c92d 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 TestMirageBlkDevName(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", + BlkDevName: "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 fc297f93..6dc4b9f0 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, + BlkDevName: u.State.Annotations[annotBlkDev], } if len(unikernelParams.CmdLine) == 0 { unikernelParams.CmdLine = strings.Fields(u.State.Annotations[annotCmdLine])