virtcontainers: create generic function

Create generic function to be reused to the reimplemented methods by
various architectures

Fixes: #1153

Signed-off-by: Alice Frosi <afrosi@de.ibm.com>
Reviewed-by: Jan Schintag <jan.schintag@de.ibm.com>
This commit is contained in:
Alice Frosi 2019-07-03 15:07:23 +02:00 committed by Jan Schintag
parent e99739f9bd
commit 7eec67044f

View File

@ -315,14 +315,14 @@ func (q *qemuArchBase) appendConsole(devices []govmmQemu.Device, path string) []
return devices return devices
} }
func (q *qemuArchBase) appendImage(devices []govmmQemu.Device, path string) ([]govmmQemu.Device, error) { func genericImage(path string) (config.BlockDrive, error) {
if _, err := os.Stat(path); os.IsNotExist(err) { if _, err := os.Stat(path); os.IsNotExist(err) {
return nil, err return config.BlockDrive{}, err
} }
randBytes, err := utils.GenerateRandomBytes(8) randBytes, err := utils.GenerateRandomBytes(8)
if err != nil { if err != nil {
return nil, err return config.BlockDrive{}, err
} }
id := utils.MakeNameID("image", hex.EncodeToString(randBytes), maxDevIDSize) id := utils.MakeNameID("image", hex.EncodeToString(randBytes), maxDevIDSize)
@ -333,13 +333,21 @@ func (q *qemuArchBase) appendImage(devices []govmmQemu.Device, path string) ([]g
ID: id, ID: id,
} }
return drive, nil
}
func (q *qemuArchBase) appendImage(devices []govmmQemu.Device, path string) ([]govmmQemu.Device, error) {
drive, err := genericImage(path)
if err != nil {
return nil, err
}
return q.appendBlockDevice(devices, drive), nil return q.appendBlockDevice(devices, drive), nil
} }
func (q *qemuArchBase) appendSCSIController(devices []govmmQemu.Device, enableIOThreads bool) ([]govmmQemu.Device, *govmmQemu.IOThread) { func genericSCSIController(enableIOThreads, nestedRun bool) (govmmQemu.SCSIController, *govmmQemu.IOThread) {
scsiController := govmmQemu.SCSIController{ scsiController := govmmQemu.SCSIController{
ID: scsiControllerID, ID: scsiControllerID,
DisableModern: q.nestedRun, DisableModern: nestedRun,
} }
var t *govmmQemu.IOThread var t *govmmQemu.IOThread
@ -354,8 +362,12 @@ func (q *qemuArchBase) appendSCSIController(devices []govmmQemu.Device, enableIO
scsiController.IOThread = t.ID scsiController.IOThread = t.ID
} }
devices = append(devices, scsiController) return scsiController, t
}
func (q *qemuArchBase) appendSCSIController(devices []govmmQemu.Device, enableIOThreads bool) ([]govmmQemu.Device, *govmmQemu.IOThread) {
d, t := genericSCSIController(enableIOThreads, q.nestedRun)
devices = append(devices, d)
return devices, t return devices, t
} }
@ -388,28 +400,30 @@ func (q *qemuArchBase) appendBridges(devices []govmmQemu.Device) []govmmQemu.Dev
return devices return devices
} }
func (q *qemuArchBase) append9PVolume(devices []govmmQemu.Device, volume types.Volume) []govmmQemu.Device { func generic9PVolume(volume types.Volume, nestedRun bool) govmmQemu.FSDevice {
if volume.MountTag == "" || volume.HostPath == "" {
return devices
}
devID := fmt.Sprintf("extra-9p-%s", volume.MountTag) devID := fmt.Sprintf("extra-9p-%s", volume.MountTag)
if len(devID) > maxDevIDSize { if len(devID) > maxDevIDSize {
devID = devID[:maxDevIDSize] devID = devID[:maxDevIDSize]
} }
devices = append(devices, return govmmQemu.FSDevice{
govmmQemu.FSDevice{ Driver: govmmQemu.Virtio9P,
Driver: govmmQemu.Virtio9P, FSDriver: govmmQemu.Local,
FSDriver: govmmQemu.Local, ID: devID,
ID: devID, Path: volume.HostPath,
Path: volume.HostPath, MountTag: volume.MountTag,
MountTag: volume.MountTag, SecurityModel: govmmQemu.None,
SecurityModel: govmmQemu.None, DisableModern: nestedRun,
DisableModern: q.nestedRun, }
}, }
)
func (q *qemuArchBase) append9PVolume(devices []govmmQemu.Device, volume types.Volume) []govmmQemu.Device {
if volume.MountTag == "" || volume.HostPath == "" {
return devices
}
d := generic9PVolume(volume, q.nestedRun)
devices = append(devices, d)
return devices return devices
} }
@ -464,70 +478,83 @@ func networkModelToQemuType(model NetInterworkingModel) govmmQemu.NetDeviceType
} }
} }
func (q *qemuArchBase) appendNetwork(devices []govmmQemu.Device, endpoint Endpoint) []govmmQemu.Device { func genericNetwork(endpoint Endpoint, vhost, nestedRun bool, index int) (govmmQemu.NetDevice, error) {
var d govmmQemu.NetDevice
switch ep := endpoint.(type) { switch ep := endpoint.(type) {
case *VethEndpoint, *BridgedMacvlanEndpoint, *IPVlanEndpoint: case *VethEndpoint, *BridgedMacvlanEndpoint, *IPVlanEndpoint:
netPair := ep.NetworkPair() netPair := ep.NetworkPair()
devices = append(devices, d = govmmQemu.NetDevice{
govmmQemu.NetDevice{ Type: networkModelToQemuType(netPair.NetInterworkingModel),
Type: networkModelToQemuType(netPair.NetInterworkingModel), Driver: govmmQemu.VirtioNet,
Driver: govmmQemu.VirtioNet, ID: fmt.Sprintf("network-%d", index),
ID: fmt.Sprintf("network-%d", q.networkIndex), IFName: netPair.TAPIface.Name,
IFName: netPair.TAPIface.Name, MACAddress: netPair.TAPIface.HardAddr,
MACAddress: netPair.TAPIface.HardAddr, DownScript: "no",
DownScript: "no", Script: "no",
Script: "no", VHost: vhost,
VHost: q.vhost, DisableModern: nestedRun,
DisableModern: q.nestedRun, FDs: netPair.VMFds,
FDs: netPair.VMFds, VhostFDs: netPair.VhostFds,
VhostFDs: netPair.VhostFds, }
},
)
q.networkIndex++
case *MacvtapEndpoint: case *MacvtapEndpoint:
devices = append(devices, d = govmmQemu.NetDevice{
govmmQemu.NetDevice{ Type: govmmQemu.MACVTAP,
Type: govmmQemu.MACVTAP, Driver: govmmQemu.VirtioNet,
Driver: govmmQemu.VirtioNet, ID: fmt.Sprintf("network-%d", index),
ID: fmt.Sprintf("network-%d", q.networkIndex), IFName: ep.Name(),
IFName: ep.Name(), MACAddress: ep.HardwareAddr(),
MACAddress: ep.HardwareAddr(), DownScript: "no",
DownScript: "no", Script: "no",
Script: "no", VHost: vhost,
VHost: q.vhost, DisableModern: nestedRun,
DisableModern: q.nestedRun, FDs: ep.VMFds,
FDs: ep.VMFds, VhostFDs: ep.VhostFds,
VhostFDs: ep.VhostFds, }
}, default:
) return govmmQemu.NetDevice{}, fmt.Errorf("Unknown type for endpoint")
q.networkIndex++
} }
return d, nil
}
func (q *qemuArchBase) appendNetwork(devices []govmmQemu.Device, endpoint Endpoint) []govmmQemu.Device {
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
}
q.networkIndex++
devices = append(devices, d)
return devices return devices
} }
func (q *qemuArchBase) appendBlockDevice(devices []govmmQemu.Device, drive config.BlockDrive) []govmmQemu.Device { func genericBlockDevice(drive config.BlockDrive, nestedRun bool) (govmmQemu.BlockDevice, error) {
if drive.File == "" || drive.ID == "" || drive.Format == "" { if drive.File == "" || drive.ID == "" || drive.Format == "" {
return devices return govmmQemu.BlockDevice{}, fmt.Errorf("Empty File, ID or Format for drive %v", drive)
} }
if len(drive.ID) > maxDevIDSize { if len(drive.ID) > maxDevIDSize {
drive.ID = drive.ID[:maxDevIDSize] drive.ID = drive.ID[:maxDevIDSize]
} }
devices = append(devices, return govmmQemu.BlockDevice{
govmmQemu.BlockDevice{ Driver: govmmQemu.VirtioBlock,
Driver: govmmQemu.VirtioBlock, ID: drive.ID,
ID: drive.ID, File: drive.File,
File: drive.File, AIO: govmmQemu.Threads,
AIO: govmmQemu.Threads, Format: govmmQemu.BlockDeviceFormat(drive.Format),
Format: govmmQemu.BlockDeviceFormat(drive.Format), Interface: "none",
Interface: "none", DisableModern: nestedRun,
DisableModern: q.nestedRun, }, nil
}, }
)
func (q *qemuArchBase) appendBlockDevice(devices []govmmQemu.Device, drive config.BlockDrive) []govmmQemu.Device {
d, err := genericBlockDevice(drive, q.nestedRun)
if err != nil {
virtLog.WithField("subsystem", "qemuArch").WithError(err).Error("Failed to append block device")
return devices
}
devices = append(devices, d)
return devices return devices
} }