Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
qemu: refactor appendImage
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
bergwolf committed Dec 25, 2019
1 parent 1296f6f commit dd5b446
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 94 deletions.
25 changes: 1 addition & 24 deletions virtcontainers/qemu_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package virtcontainers

import (
"os"
"strings"
"time"

Expand Down Expand Up @@ -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
Expand Down
43 changes: 40 additions & 3 deletions virtcontainers/qemu_arch_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions virtcontainers/qemu_arch_base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ func TestQemuArchBaseAppendImage(t *testing.T) {
AIO: govmmQemu.Threads,
Format: "raw",
Interface: "none",
ShareRW: true,
},
}

Expand Down
25 changes: 1 addition & 24 deletions virtcontainers/qemu_arm64.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package virtcontainers
import (
"context"
"io/ioutil"
"os"
"runtime"
"strings"
"time"
Expand Down Expand Up @@ -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 {
Expand Down
29 changes: 0 additions & 29 deletions virtcontainers/qemu_ppc64le.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
Expand Down
16 changes: 2 additions & 14 deletions virtcontainers/qemu_s390x.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit dd5b446

Please sign in to comment.