diff --git a/qemu.go b/qemu.go index 3a035439b3..850269369d 100644 --- a/qemu.go +++ b/qemu.go @@ -45,8 +45,8 @@ type Machine struct { // Device describes a device to be created by qemu. type Device struct { - // Type is the qemu device type - Type string + // Driver is the qemu device driver + Driver string // ID is the user defined device ID. ID string @@ -57,12 +57,18 @@ type Device struct { // FSDev is the device filesystem identifier. FSDev string + // NetDev is the networking device identifier. + NetDev string + // MountTag is the device filesystem mount point tag. // It is only relevant when combined with FSDev. MountTag string // CharDev is the device character device identifier. CharDev string + + // MACAddress is the networking device interface MAC address. + MACAddress string } // Object is a qemu object representation. @@ -321,10 +327,10 @@ func appendQMPSocket(params []string, config Config) []string { func appendDevices(params []string, config Config) []string { for _, d := range config.Devices { - if d.Type != "" { + if d.Driver != "" { var deviceParams []string - deviceParams = append(deviceParams, fmt.Sprintf("%s", d.Type)) + deviceParams = append(deviceParams, fmt.Sprintf("%s", d.Driver)) if d.ID != "" { deviceParams = append(deviceParams, fmt.Sprintf(",id=%s", d.ID)) @@ -346,6 +352,14 @@ func appendDevices(params []string, config Config) []string { } } + if d.NetDev != "" { + deviceParams = append(deviceParams, fmt.Sprintf(",netdev=%s", d.NetDev)) + + if d.MACAddress != "" { + deviceParams = append(deviceParams, fmt.Sprintf(",mac=%s", d.MACAddress)) + } + } + params = append(params, "-device") params = append(params, strings.Join(deviceParams, "")) } diff --git a/qemu_test.go b/qemu_test.go index 6d634bb955..432096acaf 100644 --- a/qemu_test.go +++ b/qemu_test.go @@ -117,7 +117,7 @@ var deviceNVDIMMString = "-device nvdimm,id=nv0,memdev=mem0" func TestAppendDeviceNVDIMM(t *testing.T) { device := Device{ - Type: "nvdimm", + Driver: "nvdimm", ID: "nv0", MemDev: "mem0", } @@ -129,7 +129,7 @@ var deviceFSString = "-device virtio-9p-pci,fsdev=workload9p,mount_tag=rootfs" func TestAppendDeviceFS(t *testing.T) { device := Device{ - Type: "virtio-9p-pci", + Driver: "virtio-9p-pci", FSDev: "workload9p", MountTag: "rootfs", } @@ -137,6 +137,18 @@ func TestAppendDeviceFS(t *testing.T) { testAppend(device, deviceFSString, t) } +var deviceNetworkString = "-device virtio-net-pci,netdev=eth0,mac=01:02:de:ad:be:ef" + +func TestAppendDeviceNetwork(t *testing.T) { + device := Device{ + Driver: "virtio-net-pci", + NetDev: "eth0", + MACAddress: "01:02:de:ad:be:ef", + } + + testAppend(device, deviceNetworkString, t) +} + func TestAppendEmptyDevice(t *testing.T) { device := Device{}