mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-08-13 13:46:46 +00:00
qemu: refactor appendImage
Do not implement in each arch code. We should have a generic implementation instead. -. amd64 and arm64 uses nvdimm -. ppc64le and s390x uses virtio-blk Signed-off-by: Peng Tao <bergwolf@hyper.sh>
This commit is contained in:
parent
1296f6f1ad
commit
dd5b446997
@ -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
|
||||
|
@ -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
|
||||
|
@ -305,6 +305,7 @@ func TestQemuArchBaseAppendImage(t *testing.T) {
|
||||
AIO: govmmQemu.Threads,
|
||||
Format: "raw",
|
||||
Interface: "none",
|
||||
ShareRW: true,
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -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 {
|
||||
|
@ -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)
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user