diff --git a/cli/config/configuration-qemu.toml.in b/cli/config/configuration-qemu.toml.in index 1176999a8d..08ebba1250 100644 --- a/cli/config/configuration-qemu.toml.in +++ b/cli/config/configuration-qemu.toml.in @@ -365,6 +365,10 @@ valid_file_mem_backends = @DEFVALIDFILEMEMBACKENDS@ # Default /var/run/kata-containers/cache.sock #vm_cache_endpoint = "/var/run/kata-containers/cache.sock" +# -pflash can add image file to VM. The arguments of it should be in format +# of ["/path/to/flash0.img", "/path/to/flash1.img"] +#pflashes = [] + [proxy.@PROJECT_TYPE@] path = "@PROXYPATH@" diff --git a/pkg/katatestutils/utils.go b/pkg/katatestutils/utils.go index 9560cf48c8..06c252f1d5 100644 --- a/pkg/katatestutils/utils.go +++ b/pkg/katatestutils/utils.go @@ -29,6 +29,7 @@ type RuntimeConfigOptions struct { AgentTraceType string SharedFS string VirtioFSDaemon string + PFlash []string PCIeRootPort uint32 DisableBlock bool EnableIOThreads bool diff --git a/pkg/katautils/config.go b/pkg/katautils/config.go index 744efb3795..28f973d7eb 100644 --- a/pkg/katautils/config.go +++ b/pkg/katautils/config.go @@ -106,6 +106,7 @@ type hypervisor struct { VirtioFSDaemonList []string `toml:"valid_virtio_fs_daemon_paths"` VirtioFSCache string `toml:"virtio_fs_cache"` VirtioFSExtraArgs []string `toml:"virtio_fs_extra_args"` + PFlashList []string `toml:"pflashes"` VirtioFSCacheSize uint32 `toml:"virtio_fs_cache_size"` BlockDeviceCacheSet bool `toml:"block_device_cache_set"` BlockDeviceCacheDirect bool `toml:"block_device_cache_direct"` @@ -250,6 +251,23 @@ func (h hypervisor) firmware() (string, error) { return ResolvePath(p) } +func (h hypervisor) PFlash() ([]string, error) { + pflashes := h.PFlashList + + if len(pflashes) == 0 { + return []string(nil), nil + } + + for _, pflash := range pflashes { + _, err := ResolvePath(pflash) + if err != nil { + return []string{}, fmt.Errorf("pflash path doesn't exist: %s", pflash) + } + } + + return pflashes, nil +} + func (h hypervisor) machineAccelerators() string { var machineAccelerators string for _, accelerator := range strings.Split(h.MachineAccelerators, ",") { @@ -626,6 +644,11 @@ func newQemuHypervisorConfig(h hypervisor) (vc.HypervisorConfig, error) { return vc.HypervisorConfig{}, err } + pflashes, err := h.PFlash() + if err != nil { + return vc.HypervisorConfig{}, err + } + machineAccelerators := h.machineAccelerators() cpuFeatures := h.cpuFeatures() kernelParams := h.kernelParams() @@ -690,6 +713,7 @@ func newQemuHypervisorConfig(h hypervisor) (vc.HypervisorConfig, error) { VirtioFSCacheSize: h.VirtioFSCacheSize, VirtioFSCache: h.defaultVirtioFSCache(), VirtioFSExtraArgs: h.VirtioFSExtraArgs, + PFlash: pflashes, MemPrealloc: h.MemPrealloc, HugePages: h.HugePages, IOMMU: h.IOMMU, diff --git a/virtcontainers/hypervisor.go b/virtcontainers/hypervisor.go index aea67ecca3..4e9c130b86 100644 --- a/virtcontainers/hypervisor.go +++ b/virtcontainers/hypervisor.go @@ -333,6 +333,9 @@ type HypervisorConfig struct { // File based memory backend root directory FileBackedMemRootDir string + // PFlash image paths + PFlash []string + // FileBackedMemRootList is the list of valid root directories values for annotations FileBackedMemRootList []string diff --git a/virtcontainers/qemu.go b/virtcontainers/qemu.go index 86c380b2ab..21a40f2d98 100644 --- a/virtcontainers/qemu.go +++ b/virtcontainers/qemu.go @@ -261,6 +261,7 @@ func (q *qemu) setup(id string, hypervisorConfig *HypervisorConfig) error { } q.arch.setBridges(q.state.Bridges) + q.arch.setPFlash(q.config.PFlash) if create { q.Logger().Debug("Creating bridges") @@ -570,6 +571,11 @@ func (q *qemu) createSandbox(ctx context.Context, id string, networkNS NetworkNa return err } + pflash, err := q.arch.getPFlash() + if err != nil { + return err + } + qemuPath, err := q.qemuPath() if err != nil { return err @@ -593,6 +599,7 @@ func (q *qemu) createSandbox(ctx context.Context, id string, networkNS NetworkNa VGA: "none", GlobalParam: "kvm-pit.lost_tick_policy=discard", Bios: firmwarePath, + PFlash: pflash, PidFile: filepath.Join(q.store.RunVMStoragePath(), q.id, "pid"), } diff --git a/virtcontainers/qemu_arch_base.go b/virtcontainers/qemu_arch_base.go index 3157ef56e3..f5027591a4 100644 --- a/virtcontainers/qemu_arch_base.go +++ b/virtcontainers/qemu_arch_base.go @@ -119,6 +119,12 @@ type qemuArch interface { // addBridge adds a new Bridge to the list of Bridges addBridge(types.Bridge) + // getPFlash() get pflash from configuration + getPFlash() ([]string, error) + + // setPFlash() grants access to pflash + setPFlash([]string) + // handleImagePath handles the Hypervisor Config image path handleImagePath(config HypervisorConfig) @@ -149,6 +155,7 @@ type qemuArchBase struct { kernelParamsDebug []Param kernelParams []Param Bridges []types.Bridge + PFlash []string } const ( @@ -800,3 +807,11 @@ func (q *qemuArchBase) appendIOMMU(devices []govmmQemu.Device) ([]govmmQemu.Devi return devices, fmt.Errorf("Machine Type %s does not support vIOMMU", q.machineType) } } + +func (q *qemuArchBase) getPFlash() ([]string, error) { + return q.PFlash, nil +} + +func (q *qemuArchBase) setPFlash(p []string) { + q.PFlash = p +} diff --git a/virtcontainers/qemu_arm64.go b/virtcontainers/qemu_arm64.go index e9d9d3198b..aa6634de3b 100644 --- a/virtcontainers/qemu_arm64.go +++ b/virtcontainers/qemu_arm64.go @@ -122,3 +122,16 @@ func (q *qemuArm64) append9PVolume(devices []govmmQemu.Device, volume types.Volu devices = append(devices, d) return devices, nil } + +func (q *qemuArm64) getPFlash() ([]string, error) { + length := len(q.PFlash) + if length == 0 { + return nil, nil + } else if length == 1 { + return nil, fmt.Errorf("two pflash images needed for arm64") + } else if length == 2 { + return q.PFlash, nil + } else { + return nil, fmt.Errorf("too many pflash images for arm64") + } +}