diff --git a/virtcontainers/qemu_amd64.go b/virtcontainers/qemu_amd64.go index 9e998246c2..5e870c5f59 100644 --- a/virtcontainers/qemu_amd64.go +++ b/virtcontainers/qemu_amd64.go @@ -6,7 +6,6 @@ package virtcontainers import ( - "os" "strings" "time" @@ -159,29 +158,7 @@ func (q *qemuAmd64) memoryTopology(memoryMb, hostMemoryMb uint64, slots uint8) g } func (q *qemuAmd64) appendImage(devices []govmmQemu.Device, path string) ([]govmmQemu.Device, error) { - imageFile, err := os.Open(path) - if err != nil { - return nil, err - } - defer func() { _ = imageFile.Close() }() - - imageStat, err := imageFile.Stat() - if err != nil { - return nil, err - } - - object := govmmQemu.Object{ - Driver: govmmQemu.NVDIMM, - Type: govmmQemu.MemoryBackendFile, - DeviceID: "nv0", - ID: "mem0", - MemPath: path, - Size: (uint64)(imageStat.Size()), - } - - devices = append(devices, object) - - return devices, nil + return q.appendNvdimmImage(devices, path) } // appendBridges appends to devices the given bridges diff --git a/virtcontainers/qemu_arch_base.go b/virtcontainers/qemu_arch_base.go index 41e7018c6e..d32b23d40a 100644 --- a/virtcontainers/qemu_arch_base.go +++ b/virtcontainers/qemu_arch_base.go @@ -67,6 +67,12 @@ type qemuArch interface { // appendImage appends an image to devices appendImage(devices []govmmQemu.Device, path string) ([]govmmQemu.Device, error) + // appendBlockImage appends an image as block device + appendBlockImage(devices []govmmQemu.Device, path string) ([]govmmQemu.Device, error) + + // appendNvdimmImage appends an image as nvdimm device + appendNvdimmImage(devices []govmmQemu.Device, path string) ([]govmmQemu.Device, error) + // appendSCSIController appens a SCSI controller to devices appendSCSIController(devices []govmmQemu.Device, enableIOThreads bool) ([]govmmQemu.Device, *govmmQemu.IOThread, error) @@ -328,15 +334,46 @@ func genericImage(path string) (config.BlockDrive, error) { id := utils.MakeNameID("image", hex.EncodeToString(randBytes), maxDevIDSize) drive := config.BlockDrive{ - File: path, - Format: "raw", - ID: id, + File: path, + Format: "raw", + ID: id, + ShareRW: true, } return drive, nil } +func (q *qemuArchBase) appendNvdimmImage(devices []govmmQemu.Device, path string) ([]govmmQemu.Device, error) { + imageFile, err := os.Open(path) + if err != nil { + return nil, err + } + defer imageFile.Close() + + imageStat, err := imageFile.Stat() + if err != nil { + return nil, err + } + + object := govmmQemu.Object{ + Driver: govmmQemu.NVDIMM, + Type: govmmQemu.MemoryBackendFile, + DeviceID: "nv0", + ID: "mem0", + MemPath: path, + Size: (uint64)(imageStat.Size()), + } + + devices = append(devices, object) + + return devices, nil +} + func (q *qemuArchBase) appendImage(devices []govmmQemu.Device, path string) ([]govmmQemu.Device, error) { + return q.appendBlockImage(devices, path) +} + +func (q *qemuArchBase) appendBlockImage(devices []govmmQemu.Device, path string) ([]govmmQemu.Device, error) { drive, err := genericImage(path) if err != nil { return nil, err diff --git a/virtcontainers/qemu_arch_base_test.go b/virtcontainers/qemu_arch_base_test.go index 68c6c4b381..008e903c7d 100644 --- a/virtcontainers/qemu_arch_base_test.go +++ b/virtcontainers/qemu_arch_base_test.go @@ -305,6 +305,7 @@ func TestQemuArchBaseAppendImage(t *testing.T) { AIO: govmmQemu.Threads, Format: "raw", Interface: "none", + ShareRW: true, }, } diff --git a/virtcontainers/qemu_arm64.go b/virtcontainers/qemu_arm64.go index 74935e7dcf..4a65e2a7a7 100644 --- a/virtcontainers/qemu_arm64.go +++ b/virtcontainers/qemu_arm64.go @@ -8,7 +8,6 @@ package virtcontainers import ( "context" "io/ioutil" - "os" "runtime" "strings" "time" @@ -179,29 +178,7 @@ func (q *qemuArm64) appendBridges(devices []govmmQemu.Device) []govmmQemu.Device } func (q *qemuArm64) appendImage(devices []govmmQemu.Device, path string) ([]govmmQemu.Device, error) { - imageFile, err := os.Open(path) - if err != nil { - return nil, err - } - defer imageFile.Close() - - imageStat, err := imageFile.Stat() - if err != nil { - return nil, err - } - - object := govmmQemu.Object{ - Driver: govmmQemu.NVDIMM, - Type: govmmQemu.MemoryBackendFile, - DeviceID: "nv0", - ID: "mem0", - MemPath: path, - Size: (uint64)(imageStat.Size()), - } - - devices = append(devices, object) - - return devices, nil + return q.appendNvdimmImage(devices, path) } func (q *qemuArm64) setIgnoreSharedMemoryMigrationCaps(_ context.Context, _ *govmmQemu.QMP) error { diff --git a/virtcontainers/qemu_ppc64le.go b/virtcontainers/qemu_ppc64le.go index 323356215e..21e3b6c2f2 100644 --- a/virtcontainers/qemu_ppc64le.go +++ b/virtcontainers/qemu_ppc64le.go @@ -6,14 +6,10 @@ package virtcontainers import ( - "encoding/hex" - "os" "time" govmmQemu "github.com/intel/govmm/qemu" - deviceConfig "github.com/kata-containers/runtime/virtcontainers/device/config" "github.com/kata-containers/runtime/virtcontainers/types" - "github.com/kata-containers/runtime/virtcontainers/utils" "github.com/sirupsen/logrus" ) @@ -130,31 +126,6 @@ func (q *qemuPPC64le) memoryTopology(memoryMb, hostMemoryMb uint64, slots uint8) return genericMemoryTopology(memoryMb, hostMemoryMb, slots, q.memoryOffset) } -func (q *qemuPPC64le) appendImage(devices []govmmQemu.Device, path string) ([]govmmQemu.Device, error) { - if _, err := os.Stat(path); os.IsNotExist(err) { - return nil, err - } - - randBytes, err := utils.GenerateRandomBytes(8) - if err != nil { - return nil, err - } - - id := utils.MakeNameID("image", hex.EncodeToString(randBytes), maxDevIDSize) - - drive := deviceConfig.BlockDrive{ - File: path, - Format: "raw", - ID: id, - } - - devices, err = q.appendBlockDevice(devices, drive) - if err != nil { - return nil, err - } - return devices, nil -} - // appendBridges appends to devices the given bridges func (q *qemuPPC64le) appendBridges(devices []govmmQemu.Device) []govmmQemu.Device { return genericAppendBridges(devices, q.Bridges, q.machineType) diff --git a/virtcontainers/qemu_s390x.go b/virtcontainers/qemu_s390x.go index 5c5b16bc75..d2575ccdcc 100644 --- a/virtcontainers/qemu_s390x.go +++ b/virtcontainers/qemu_s390x.go @@ -7,10 +7,11 @@ package virtcontainers import ( "fmt" + "time" + govmmQemu "github.com/intel/govmm/qemu" "github.com/kata-containers/runtime/virtcontainers/device/config" "github.com/kata-containers/runtime/virtcontainers/types" - "time" ) type qemuS390x struct { @@ -126,19 +127,6 @@ func (q *qemuS390x) appendConsole(devices []govmmQemu.Device, path string) ([]go return devices, nil } -func (q *qemuS390x) appendImage(devices []govmmQemu.Device, path string) ([]govmmQemu.Device, error) { - drive, err := genericImage(path) - if err != nil { - return nil, err - } - drive.ShareRW = true - devices, err = q.appendBlockDevice(devices, drive) - if err != nil { - return nil, err - } - return devices, nil -} - func (q *qemuS390x) appendBlockDevice(devices []govmmQemu.Device, drive config.BlockDrive) ([]govmmQemu.Device, error) { d, err := genericBlockDevice(drive, false) if err != nil {