diff --git a/qemu/qmp.go b/qemu/qmp.go index fccc0f56ae..40e4781a69 100644 --- a/qemu/qmp.go +++ b/qemu/qmp.go @@ -986,3 +986,17 @@ func (q *QMP) ExecuteCharDevUnixSocketAdd(ctx context.Context, id, path string, } return q.executeCommand(ctx, "chardev-add", args, nil) } + +// ExecuteVirtSerialPortAdd adds a virtserialport. +// id is an identifier for the virtserialport, name is a name for the virtserialport and +// it will be visible in the VM, chardev is the character device id previously added. +func (q *QMP) ExecuteVirtSerialPortAdd(ctx context.Context, id, name, chardev string) error { + args := map[string]interface{}{ + "driver": VirtioSerialPort, + "id": id, + "name": name, + "chardev": chardev, + } + + return q.executeCommand(ctx, "device_add", args, nil) +} diff --git a/qemu/qmp_test.go b/qemu/qmp_test.go index 2f60db1abd..c6c4ed4f00 100644 --- a/qemu/qmp_test.go +++ b/qemu/qmp_test.go @@ -1057,3 +1057,20 @@ func TestExecuteCharDevUnixSocketAdd(t *testing.T) { q.Shutdown() <-disconnectedCh } + +// Checks virtio serial port hotplug +func TestExecuteVirtSerialPortAdd(t *testing.T) { + connectedCh := make(chan *QMPVersion) + disconnectedCh := make(chan struct{}) + buf := newQMPTestCommandBuffer(t) + buf.AddCommand("device_add", nil, "return", nil) + cfg := QMPConfig{Logger: qmpTestLogger{}} + q := startQMPLoop(buf, cfg, connectedCh, disconnectedCh) + checkVersion(t, connectedCh) + err := q.ExecuteVirtSerialPortAdd(context.Background(), "foo", "foo.channel", "foo") + if err != nil { + t.Fatalf("Unexpected error %v", err) + } + q.Shutdown() + <-disconnectedCh +}