-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmachine.go
214 lines (175 loc) · 4.47 KB
/
machine.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package qemu
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"strconv"
"syscall"
"time"
)
// Machine represents a QEMU virtual machine
type Machine struct {
Cores int // Number of CPU cores
Memory uint64 // RAM quantity in megabytes
cd string
display string
vnc string
monitor string
drives []Drive
ifaces []NetDev
custom [][]string
}
// Drive represents a machine hard drive
type Drive struct {
Path string // Image file path
Format string // Image format
}
// NewMachine creates a new virtual machine
// with the specified number of cpu cores and memory
func NewMachine(cores int, memory uint64) Machine {
var machine Machine
machine.Cores = cores
machine.Memory = memory
machine.drives = make([]Drive, 0)
return machine
}
// AddCDRom attaches a disk image
// as a CD-ROM on the machine
func (m *Machine) AddCDRom(dev string) {
m.cd = dev
}
// AddDrive attaches a new hard drive to
// the virtual machine
func (m *Machine) AddDrive(d Drive) {
m.drives = append(m.drives, d)
}
// AddDriveImage attaches the specified Image to
// the virtual machine
func (m *Machine) AddDriveImage(img Image) {
m.drives = append(m.drives, Drive{img.Path, img.Format})
}
// AddNetworkDevice attaches the specified netdev tp
// the virtual machine
func (m *Machine) AddNetworkDevice(netdev NetDev) {
m.ifaces = append(m.ifaces, netdev)
}
// SetDisplay sets the display mode
// for the virtual machine
func (m *Machine) SetDisplay(mode string) {
m.display = mode
}
// AddVNC attaches a VNC server to
// the virtual machine, bound to the specified address and port
// If wsPort is not 0, VNC will work over WebSocket on that port
func (m *Machine) AddVNC(addr string, port, wsPort int, passwd bool) {
m.vnc = fmt.Sprintf("%s:%d", addr, port)
if wsPort > 0 {
m.vnc = fmt.Sprintf("%s,websocket=%d", m.vnc, wsPort)
if passwd {
m.vnc = fmt.Sprintf("%s,password", m.vnc)
}
}
}
// AddMonitor redirects the QEMU monitor
// to the specified unix socket file
func (m *Machine) AddMonitorUnix(dev string) {
m.monitor = dev
}
// AddOption adds a custom command line option
// to the QEMU start command
func (m *Machine) AddOption(opt, val string) {
m.custom = append(m.custom, []string{opt, val})
}
// Start stars the machine
// The 'kvm' bool specifies if KVM should be used
// It returns the QEMU process and an error (if any)
func (m *Machine) Start(arch string, kvm bool, stderrCb func(s string)) (*os.Process, error) {
qemu := fmt.Sprintf("qemu-system-%s", arch)
args := []string{"-smp", strconv.Itoa(m.Cores), "-m", strconv.FormatUint(m.Memory, 10)}
if kvm {
args = append(args, "-enable-kvm")
}
if len(m.cd) > 0 {
args = append(args, "-cdrom")
args = append(args, m.cd)
}
for _, drive := range m.drives {
args = append(args, "-drive")
args = append(args, fmt.Sprintf("file=%s,format=%s", drive.Path, drive.Format))
}
if len(m.ifaces) == 0 {
args = append(args, "-net")
args = append(args, "none")
}
for _, iface := range m.ifaces {
s := fmt.Sprintf("%s,id=%s", iface.Type, iface.ID)
if len(iface.IfName) > 0 {
s = fmt.Sprintf("%s,ifname=%s", s, iface.IfName)
}
args = append(args, "-netdev")
args = append(args, s)
s = fmt.Sprintf("virtio-net,netdev=%s", iface.ID)
if len(iface.MAC) > 0 {
s = fmt.Sprintf("%s,mac=%s", s, iface.MAC)
}
args = append(args, "-device")
args = append(args, s)
}
if len(m.vnc) > 0 {
args = append(args, "-vnc")
args = append(args, m.vnc)
} else if len(m.display) == 0 {
args = append(args, "-display")
args = append(args, "none")
}
if len(m.display) > 0 {
args = append(args, "-display")
args = append(args, m.display)
}
if len(m.monitor) > 0 {
args = append(args, "-qmp")
args = append(args, fmt.Sprintf("unix:%s,server,nowait", m.monitor))
}
for _, c := range m.custom {
args = append(args, c[0])
args = append(args, c[1])
}
cmd := exec.Command(qemu, args...)
cmd.SysProcAttr = new(syscall.SysProcAttr)
cmd.SysProcAttr.Setsid = true
stderr, err := cmd.StderrPipe()
if err == nil {
go func() {
s, err := ioutil.ReadAll(stderr)
if err != nil {
return
}
stderrCb(string(s))
}()
}
err = cmd.Start()
if err != nil {
return nil, err
}
proc := cmd.Process
errc := make(chan error)
go func() {
err := cmd.Wait()
if err != nil {
errc <- fmt.Errorf("'qemu-system-%s': %s", arch, err)
return
}
}()
time.Sleep(50 * time.Millisecond)
var vmerr error
select {
case vmerr = <-errc:
if vmerr != nil {
return nil, vmerr
}
default:
break
}
return proc, nil
}