-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathspice.go
81 lines (67 loc) · 2.65 KB
/
spice.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package qcli
import (
"fmt"
"strings"
)
const RemoteDisplayPortBase = 5900
const SpiceSerialNamespace = "com.redhat.spice.0"
const SpiceCharDevDriver = "spicevmc"
const SpiceCharDevName = "vdagent"
// SpiceDevice represents a qemu spice protocol device.
type SpiceDevice struct {
ID string `yaml:"id"`
Port string `yaml:"port"`
HostAddress string `yaml:"host-address"`
TLSPort string `yaml:"tls-port"`
DisableTicketing bool `yaml:"disable-ticketing"`
// FIXME: implement the rest of -spice
}
// Valid returns true if there is a valid structure defined for SpiceDevice
func (dev SpiceDevice) Valid() error {
if dev.Port == "" && dev.TLSPort == "" {
return fmt.Errorf("SpiceDevice 'Port' or 'TLSPort' value is required")
}
if dev.Port != "" && dev.TLSPort != "" {
return fmt.Errorf("SpiceDevice has 'Port' and 'TLSPort' set, only one allowed")
}
return nil
}
// QemuParams returns the qemu parameters built out of this spice device.
func (dev SpiceDevice) QemuParams(config *Config) []string {
var qemuParams []string
var deviceParams []string
var virtportParams []string
var chardevParams []string
if dev.Port != "" {
deviceParams = append(deviceParams, fmt.Sprintf("port=%s", dev.Port))
}
if dev.TLSPort != "" {
deviceParams = append(deviceParams, fmt.Sprintf("tls-port=%s", dev.TLSPort))
}
addr := "127.0.0.1"
if dev.HostAddress != "" {
addr = dev.HostAddress
}
deviceParams = append(deviceParams, fmt.Sprintf("addr=%s", addr))
if dev.DisableTicketing {
deviceParams = append(deviceParams, fmt.Sprintf("disable-ticketing=on"))
}
// add the virtserialport to enable copy-paste if guest is configured
// -device virtserialport,chardev=spicechannel0,name=com.redhat.spice.0
chardevID := "spicechannel0"
virtportParams = append(virtportParams, "virtserialport")
virtportParams = append(virtportParams, fmt.Sprintf("chardev=%s", chardevID))
virtportParams = append(virtportParams, fmt.Sprintf("name=%s", SpiceSerialNamespace))
// -chardev spicevmc,id=spicechannel0,name=vdagent
chardevParams = append(chardevParams, SpiceCharDevDriver)
chardevParams = append(chardevParams, fmt.Sprintf("id=%s", chardevID))
chardevParams = append(chardevParams, fmt.Sprintf("name=%s", SpiceCharDevName))
qemuParams = append(qemuParams, "-spice")
qemuParams = append(qemuParams, strings.Join(deviceParams, ","))
qemuParams = append(qemuParams, "-device", "virtio-serial-pci")
qemuParams = append(qemuParams, "-device")
qemuParams = append(qemuParams, strings.Join(virtportParams, ","))
qemuParams = append(qemuParams, "-chardev")
qemuParams = append(qemuParams, strings.Join(chardevParams, ","))
return qemuParams
}