virtcontainer: add error return code

Add error return code to append functions.

Fixes: #2035

Signed-off-by: Alice Frosi <afrosi@de.ibm.com>
This commit is contained in:
Alice Frosi 2019-09-05 13:40:08 +02:00
parent 94c47dcecd
commit e3f92fe59b
5 changed files with 84 additions and 89 deletions

View File

@ -407,7 +407,10 @@ func (q *qemu) buildDevices(initrdPath string) ([]govmmQemu.Device, *govmmQemu.I
// bridge gets the first available PCI address i.e bridgePCIStartAddr
devices = q.arch.appendBridges(devices)
devices = q.arch.appendConsole(devices, console)
devices, err = q.arch.appendConsole(devices, console)
if err != nil {
return nil, nil, err
}
if initrdPath == "" {
devices, err = q.appendImage(devices)
@ -418,7 +421,7 @@ func (q *qemu) buildDevices(initrdPath string) ([]govmmQemu.Device, *govmmQemu.I
var ioThread *govmmQemu.IOThread
if q.config.BlockDeviceDriver == config.VirtioSCSI {
devices, ioThread = q.arch.appendSCSIController(devices, q.config.EnableIOThreads)
return q.arch.appendSCSIController(devices, q.config.EnableIOThreads)
}
return devices, ioThread, nil
@ -590,7 +593,10 @@ func (q *qemu) createSandbox(ctx context.Context, id string, networkNS NetworkNa
ID: rngID,
Filename: q.config.EntropySource,
}
qemuConfig.Devices = q.arch.appendRNGDevice(qemuConfig.Devices, rngDev)
qemuConfig.Devices, err = q.arch.appendRNGDevice(qemuConfig.Devices, rngDev)
if err != nil {
return err
}
q.qemuConfig = qemuConfig
@ -1578,7 +1584,7 @@ func (q *qemu) addDevice(devInfo interface{}, devType deviceType) error {
q.qemuConfig.Devices, err = q.arch.appendVhostUserDevice(q.qemuConfig.Devices, vhostDev)
} else {
q.Logger().WithField("volume-type", "virtio-9p").Info("adding volume")
q.qemuConfig.Devices = q.arch.append9PVolume(q.qemuConfig.Devices, v)
q.qemuConfig.Devices, err = q.arch.append9PVolume(q.qemuConfig.Devices, v)
}
case types.Socket:
q.qemuConfig.Devices = q.arch.appendSocket(q.qemuConfig.Devices, v)
@ -1586,9 +1592,9 @@ func (q *qemu) addDevice(devInfo interface{}, devType deviceType) error {
q.fds = append(q.fds, v.vhostFd)
q.qemuConfig.Devices = q.arch.appendVSockPCI(q.qemuConfig.Devices, v)
case Endpoint:
q.qemuConfig.Devices = q.arch.appendNetwork(q.qemuConfig.Devices, v)
q.qemuConfig.Devices, err = q.arch.appendNetwork(q.qemuConfig.Devices, v)
case config.BlockDrive:
q.qemuConfig.Devices = q.arch.appendBlockDevice(q.qemuConfig.Devices, v)
q.qemuConfig.Devices, err = q.arch.appendBlockDevice(q.qemuConfig.Devices, v)
case config.VhostUserDeviceAttrs:
q.qemuConfig.Devices, err = q.arch.appendVhostUserDevice(q.qemuConfig.Devices, v)
case config.VFIODev:

View File

@ -62,19 +62,19 @@ type qemuArch interface {
memoryTopology(memoryMb, hostMemoryMb uint64, slots uint8) govmmQemu.Memory
// appendConsole appends a console to devices
appendConsole(devices []govmmQemu.Device, path string) []govmmQemu.Device
appendConsole(devices []govmmQemu.Device, path string) ([]govmmQemu.Device, error)
// appendImage appends an image to devices
appendImage(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)
appendSCSIController(devices []govmmQemu.Device, enableIOThreads bool) ([]govmmQemu.Device, *govmmQemu.IOThread, error)
// appendBridges appends bridges to devices
appendBridges(devices []govmmQemu.Device) []govmmQemu.Device
// append9PVolume appends a 9P volume to devices
append9PVolume(devices []govmmQemu.Device, volume types.Volume) []govmmQemu.Device
append9PVolume(devices []govmmQemu.Device, volume types.Volume) ([]govmmQemu.Device, error)
// appendSocket appends a socket to devices
appendSocket(devices []govmmQemu.Device, socket types.Socket) []govmmQemu.Device
@ -83,10 +83,10 @@ type qemuArch interface {
appendVSockPCI(devices []govmmQemu.Device, vsock kataVSOCK) []govmmQemu.Device
// appendNetwork appends a endpoint device to devices
appendNetwork(devices []govmmQemu.Device, endpoint Endpoint) []govmmQemu.Device
appendNetwork(devices []govmmQemu.Device, endpoint Endpoint) ([]govmmQemu.Device, error)
// appendBlockDevice appends a block drive to devices
appendBlockDevice(devices []govmmQemu.Device, drive config.BlockDrive) []govmmQemu.Device
appendBlockDevice(devices []govmmQemu.Device, drive config.BlockDrive) ([]govmmQemu.Device, error)
// appendVhostUserDevice appends a vhost user device to devices
appendVhostUserDevice(devices []govmmQemu.Device, drive config.VhostUserDeviceAttrs) ([]govmmQemu.Device, error)
@ -95,7 +95,7 @@ type qemuArch interface {
appendVFIODevice(devices []govmmQemu.Device, vfioDevice config.VFIODev) []govmmQemu.Device
// appendRNGDevice appends a RNG device to devices
appendRNGDevice(devices []govmmQemu.Device, rngDevice config.RNGDev) []govmmQemu.Device
appendRNGDevice(devices []govmmQemu.Device, rngDevice config.RNGDev) ([]govmmQemu.Device, error)
// addDeviceToBridge adds devices to the bus
addDeviceToBridge(ID string, t types.Type) (string, types.Bridge, error)
@ -293,7 +293,7 @@ func (q *qemuArchBase) memoryTopology(memoryMb, hostMemoryMb uint64, slots uint8
return memory
}
func (q *qemuArchBase) appendConsole(devices []govmmQemu.Device, path string) []govmmQemu.Device {
func (q *qemuArchBase) appendConsole(devices []govmmQemu.Device, path string) ([]govmmQemu.Device, error) {
serial := govmmQemu.SerialDevice{
Driver: govmmQemu.VirtioSerial,
ID: "serial0",
@ -312,7 +312,7 @@ func (q *qemuArchBase) appendConsole(devices []govmmQemu.Device, path string) []
devices = append(devices, console)
return devices
return devices, nil
}
func genericImage(path string) (config.BlockDrive, error) {
@ -341,7 +341,11 @@ func (q *qemuArchBase) appendImage(devices []govmmQemu.Device, path string) ([]g
if err != nil {
return nil, err
}
return q.appendBlockDevice(devices, drive), nil
devices, err = q.appendBlockDevice(devices, drive)
if err != nil {
return nil, err
}
return devices, nil
}
func genericSCSIController(enableIOThreads, nestedRun bool) (govmmQemu.SCSIController, *govmmQemu.IOThread) {
@ -365,10 +369,10 @@ func genericSCSIController(enableIOThreads, nestedRun bool) (govmmQemu.SCSIContr
return scsiController, t
}
func (q *qemuArchBase) appendSCSIController(devices []govmmQemu.Device, enableIOThreads bool) ([]govmmQemu.Device, *govmmQemu.IOThread) {
func (q *qemuArchBase) appendSCSIController(devices []govmmQemu.Device, enableIOThreads bool) ([]govmmQemu.Device, *govmmQemu.IOThread, error) {
d, t := genericSCSIController(enableIOThreads, q.nestedRun)
devices = append(devices, d)
return devices, t
return devices, t, nil
}
// appendBridges appends to devices the given bridges
@ -417,14 +421,14 @@ func generic9PVolume(volume types.Volume, nestedRun bool) govmmQemu.FSDevice {
}
}
func (q *qemuArchBase) append9PVolume(devices []govmmQemu.Device, volume types.Volume) []govmmQemu.Device {
func (q *qemuArchBase) append9PVolume(devices []govmmQemu.Device, volume types.Volume) ([]govmmQemu.Device, error) {
if volume.MountTag == "" || volume.HostPath == "" {
return devices
return devices, nil
}
d := generic9PVolume(volume, q.nestedRun)
devices = append(devices, d)
return devices
return devices, nil
}
func (q *qemuArchBase) appendSocket(devices []govmmQemu.Device, socket types.Socket) []govmmQemu.Device {
@ -517,15 +521,14 @@ func genericNetwork(endpoint Endpoint, vhost, nestedRun bool, index int) (govmmQ
return d, nil
}
func (q *qemuArchBase) appendNetwork(devices []govmmQemu.Device, endpoint Endpoint) []govmmQemu.Device {
func (q *qemuArchBase) appendNetwork(devices []govmmQemu.Device, endpoint Endpoint) ([]govmmQemu.Device, error) {
d, err := genericNetwork(endpoint, q.vhost, q.nestedRun, q.networkIndex)
if err != nil {
virtLog.WithField("subsystem", "qemuArch").WithError(err).Error("Failed to append network")
return devices
return devices, fmt.Errorf("Failed to append network %v", err)
}
q.networkIndex++
devices = append(devices, d)
return devices
return devices, nil
}
func genericBlockDevice(drive config.BlockDrive, nestedRun bool) (govmmQemu.BlockDevice, error) {
@ -548,14 +551,13 @@ func genericBlockDevice(drive config.BlockDrive, nestedRun bool) (govmmQemu.Bloc
}, nil
}
func (q *qemuArchBase) appendBlockDevice(devices []govmmQemu.Device, drive config.BlockDrive) []govmmQemu.Device {
func (q *qemuArchBase) appendBlockDevice(devices []govmmQemu.Device, drive config.BlockDrive) ([]govmmQemu.Device, error) {
d, err := genericBlockDevice(drive, q.nestedRun)
if err != nil {
virtLog.WithField("subsystem", "qemuArch").WithError(err).Error("Failed to append block device")
return devices
return devices, fmt.Errorf("Failed to append block device %v", err)
}
devices = append(devices, d)
return devices
return devices, nil
}
func (q *qemuArchBase) appendVhostUserDevice(devices []govmmQemu.Device, attr config.VhostUserDeviceAttrs) ([]govmmQemu.Device, error) {
@ -599,7 +601,7 @@ func (q *qemuArchBase) appendVFIODevice(devices []govmmQemu.Device, vfioDev conf
return devices
}
func (q *qemuArchBase) appendRNGDevice(devices []govmmQemu.Device, rngDev config.RNGDev) []govmmQemu.Device {
func (q *qemuArchBase) appendRNGDevice(devices []govmmQemu.Device, rngDev config.RNGDev) ([]govmmQemu.Device, error) {
devices = append(devices,
govmmQemu.RngDevice{
ID: rngDev.ID,
@ -607,7 +609,7 @@ func (q *qemuArchBase) appendRNGDevice(devices []govmmQemu.Device, rngDev config
},
)
return devices
return devices, nil
}
func (q *qemuArchBase) handleImagePath(config HypervisorConfig) {

View File

@ -238,11 +238,11 @@ func testQemuArchBaseAppend(t *testing.T, structure interface{}, expected []govm
switch s := structure.(type) {
case types.Volume:
devices = qemuArchBase.append9PVolume(devices, s)
devices, err = qemuArchBase.append9PVolume(devices, s)
case types.Socket:
devices = qemuArchBase.appendSocket(devices, s)
case config.BlockDrive:
devices = qemuArchBase.appendBlockDevice(devices, s)
devices, err = qemuArchBase.appendBlockDevice(devices, s)
case config.VFIODev:
devices = qemuArchBase.appendVFIODevice(devices, s)
case config.VhostUserDeviceAttrs:
@ -255,6 +255,7 @@ func testQemuArchBaseAppend(t *testing.T, structure interface{}, expected []govm
func TestQemuArchBaseAppendConsoles(t *testing.T) {
var devices []govmmQemu.Device
var err error
assert := assert.New(t)
qemuArchBase := newQemuArchBase()
@ -274,7 +275,8 @@ func TestQemuArchBaseAppendConsoles(t *testing.T) {
},
}
devices = qemuArchBase.appendConsole(devices, path)
devices, err = qemuArchBase.appendConsole(devices, path)
assert.NoError(err)
assert.Equal(expectedOut, devices)
}
@ -484,16 +486,19 @@ func TestQemuArchBaseAppendSCSIController(t *testing.T) {
},
}
devices, ioThread := qemuArchBase.appendSCSIController(devices, false)
devices, ioThread, err := qemuArchBase.appendSCSIController(devices, false)
assert.Equal(expectedOut, devices)
assert.Nil(ioThread)
assert.NoError(err)
_, ioThread = qemuArchBase.appendSCSIController(devices, true)
_, ioThread, err = qemuArchBase.appendSCSIController(devices, true)
assert.NotNil(ioThread)
assert.NoError(err)
}
func TestQemuArchBaseAppendNetwork(t *testing.T) {
var devices []govmmQemu.Device
var err error
assert := assert.New(t)
qemuArchBase := newQemuArchBase()
@ -551,7 +556,9 @@ func TestQemuArchBaseAppendNetwork(t *testing.T) {
},
}
devices = qemuArchBase.appendNetwork(devices, macvlanEp)
devices = qemuArchBase.appendNetwork(devices, macvtapEp)
devices, err = qemuArchBase.appendNetwork(devices, macvlanEp)
assert.NoError(err)
devices, err = qemuArchBase.appendNetwork(devices, macvtapEp)
assert.NoError(err)
assert.Equal(expectedOut, devices)
}

View File

@ -148,7 +148,11 @@ func (q *qemuPPC64le) appendImage(devices []govmmQemu.Device, path string) ([]go
ID: id,
}
return q.appendBlockDevice(devices, drive), nil
devices, err = q.appendBlockDevice(devices, drive)
if err != nil {
return nil, err
}
return devices, nil
}
// appendBridges appends to devices the given bridges

View File

@ -92,19 +92,17 @@ func (q *qemuS390x) bridges(number uint32) {
// appendConsole appends a console to devices.
// The function has been overwriten to correctly set the driver to the CCW device
func (q *qemuS390x) appendConsole(devices []govmmQemu.Device, path string) []govmmQemu.Device {
func (q *qemuS390x) appendConsole(devices []govmmQemu.Device, path string) ([]govmmQemu.Device, error) {
id := "serial0"
addr, b, err := q.addDeviceToBridge(id, types.CCW)
if err != nil {
virtLog.WithField("subsystem", "qemus390x").WithError(err).Error("Failed to append console")
return devices
return devices, fmt.Errorf("Failed to append console %v", err)
}
var devno string
devno, err = b.AddressFormatCCW(addr)
if err != nil {
virtLog.WithField("subsystem", "qemus390x").WithError(err).Error("Failed to append console")
return devices
return devices, fmt.Errorf("Failed to append console %v", err)
}
serial := govmmQemu.SerialDevice{
@ -126,37 +124,24 @@ func (q *qemuS390x) appendConsole(devices []govmmQemu.Device, path string) []gov
devices = append(devices, console)
return devices
return devices, nil
}
func (q *qemuS390x) appendImage(devices []govmmQemu.Device, path string) ([]govmmQemu.Device, error) {
drive, err := genericImage(path)
if err != nil {
virtLog.WithField("subsystem", "qemus390x").WithError(err).Error("Failed to append image")
return nil, err
}
return q.appendBlockDevice(devices, drive), nil
}
func (q *qemuS390x) appendBlockDevice(devices []govmmQemu.Device, drive config.BlockDrive) []govmmQemu.Device {
func (q *qemuS390x) appendBlockDevice(devices []govmmQemu.Device, drive config.BlockDrive) ([]govmmQemu.Device, error) {
d, err := genericBlockDevice(drive, false)
if err != nil {
virtLog.WithField("subsystem", "qemus390x").WithError(err).Error("Failed to append blk-dev")
return devices
return devices, fmt.Errorf("Failed to append blk-dev %v", err)
}
addr, b, err := q.addDeviceToBridge(drive.ID, types.CCW)
if err != nil {
virtLog.WithField("subsystem", "qemus390x").WithError(err).Error("Failed to append blk-dev")
return devices
return devices, fmt.Errorf("Failed to append blk-dev %v", err)
}
d.DevNo, err = b.AddressFormatCCW(addr)
if err != nil {
virtLog.WithField("subsystem", "qemus390x").WithError(err).Error("Failed to append blk-dev")
return devices
return devices, fmt.Errorf("Failed to append blk-dev %v", err)
}
devices = append(devices, d)
return devices
return devices, nil
}
// appendVhostUserDevice throws an error if vhost devices are tried to be used.
@ -171,39 +156,34 @@ func (q *qemuS390x) supportGuestMemoryHotplug() bool {
return false
}
func (q *qemuS390x) appendNetwork(devices []govmmQemu.Device, endpoint Endpoint) []govmmQemu.Device {
func (q *qemuS390x) appendNetwork(devices []govmmQemu.Device, endpoint Endpoint) ([]govmmQemu.Device, error) {
d, err := genericNetwork(endpoint, false, false, q.networkIndex)
if err != nil {
virtLog.WithField("subsystem", "qemus390x").WithError(err).Error("Failed to append network")
return devices
return devices, fmt.Errorf("Failed to append network %v", err)
}
q.networkIndex++
addr, b, err := q.addDeviceToBridge(d.ID, types.CCW)
if err != nil {
virtLog.WithField("subsystem", "qemus390x").WithError(err).Error("Failed to append network")
return devices
return devices, fmt.Errorf("Failed to append network %v", err)
}
d.DevNo, err = b.AddressFormatCCW(addr)
if err != nil {
virtLog.WithField("subsystem", "qemus390x").WithError(err).Error("Failed to append network")
return devices
return devices, fmt.Errorf("Failed to append network %v", err)
}
devices = append(devices, d)
return devices
return devices, nil
}
func (q *qemuS390x) appendRNGDevice(devices []govmmQemu.Device, rngDev config.RNGDev) []govmmQemu.Device {
func (q *qemuS390x) appendRNGDevice(devices []govmmQemu.Device, rngDev config.RNGDev) ([]govmmQemu.Device, error) {
addr, b, err := q.addDeviceToBridge(rngDev.ID, types.CCW)
if err != nil {
virtLog.WithField("subsystem", "qemus390x").WithError(err).Error("Failed to append RNG-Device")
return devices
return devices, fmt.Errorf("Failed to append RNG-Device %v", err)
}
var devno string
devno, err = b.AddressFormatCCW(addr)
if err != nil {
virtLog.WithField("subsystem", "qemus390x").WithError(err).Error("Failed to append RNG-Device")
return devices
return devices, fmt.Errorf("Failed to append RNG-Device %v", err)
}
devices = append(devices,
@ -214,26 +194,24 @@ func (q *qemuS390x) appendRNGDevice(devices []govmmQemu.Device, rngDev config.RN
},
)
return devices
return devices, nil
}
func (q *qemuS390x) append9PVolume(devices []govmmQemu.Device, volume types.Volume) []govmmQemu.Device {
func (q *qemuS390x) append9PVolume(devices []govmmQemu.Device, volume types.Volume) ([]govmmQemu.Device, error) {
if volume.MountTag == "" || volume.HostPath == "" {
return devices
return devices, nil
}
d := generic9PVolume(volume, false)
addr, b, err := q.addDeviceToBridge(d.ID, types.CCW)
if err != nil {
virtLog.WithField("subsystem", "qemus390x").WithError(err).Error("Failed to append 9p-Volume")
return devices
return devices, fmt.Errorf("Failed to append 9p-Volume %v", err)
}
d.DevNo, err = b.AddressFormatCCW(addr)
if err != nil {
virtLog.WithField("subsystem", "qemus390x").WithError(err).Error("Failed to append 9p-Volume")
return devices
return devices, fmt.Errorf("Failed to append 9p-Volume %v", err)
}
devices = append(devices, d)
return devices
return devices, nil
}
// appendBridges appends to devices the given bridges
@ -241,19 +219,17 @@ func (q *qemuS390x) appendBridges(devices []govmmQemu.Device) []govmmQemu.Device
return genericAppendBridges(devices, q.Bridges, q.machineType)
}
func (q *qemuS390x) appendSCSIController(devices []govmmQemu.Device, enableIOThreads bool) ([]govmmQemu.Device, *govmmQemu.IOThread) {
func (q *qemuS390x) appendSCSIController(devices []govmmQemu.Device, enableIOThreads bool) ([]govmmQemu.Device, *govmmQemu.IOThread, error) {
d, t := genericSCSIController(enableIOThreads, q.nestedRun)
addr, b, err := q.addDeviceToBridge(d.ID, types.CCW)
if err != nil {
virtLog.WithField("subsystem", "qemus390x").WithError(err).Error("Failed to append scsi-controller")
return devices, nil
return devices, nil, fmt.Errorf("Failed to append scsi-controller %v", err)
}
d.DevNo, err = b.AddressFormatCCW(addr)
if err != nil {
virtLog.WithField("subsystem", "qemus390x").WithError(err).Error("Failed to append scsi-controller")
return devices, nil
return devices, nil, fmt.Errorf("Failed to append scsi-controller %v", err)
}
devices = append(devices, d)
return devices, t
return devices, t, nil
}