-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathqemu.go
212 lines (171 loc) · 5.08 KB
/
qemu.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
package qemu
import (
"net"
"time"
"github.com/digitalocean/go-qemu/hypervisor"
"github.com/digitalocean/go-qemu/qemu"
"github.com/digitalocean/go-qemu/qmp"
)
var (
defaultTimeout = 3 * time.Second
)
//=====================================================================================================
// DriverCType defines the possible driver connection type to be used
// for the Domain.
type DriverCType int
// contains two available DriverCType for connecting with the qemu hypervisor.
const (
RPCCDriver DriverCType = iota
SocketCDriver
)
//=====================================================================================================
//=====================================================================================================
var (
// DefaultDomain defines a default domain configuration for use when
// a configuration is not giving.
DefaultDomain = DomainConfig{
Network: "unix",
Address: "/var/run/libvirt/libvirt-sock",
Timeout: defaultTimeout,
DCType: RPCCDriver,
}
)
// DomainConfig defines the configuration used to build drive a Domain
// internal settings.
type DomainConfig struct {
Network string
Address string
Timeout time.Duration
DCType DriverCType
Addresses []hypervisor.SocketAddress
}
//=====================================================================================================
// Domain defines a single qemu vm which is used to interface and
// operate with the vm for interaction.
type Domain struct {
config DomainConfig
driver hypervisor.Driver
hypervisor *hypervisor.Hypervisor
}
// NewDomain returns a new Domain instance which allows interaction with the
// installed qemu hypervisor.
func NewDomain(config *DomainConfig) *Domain {
var dm Domain
if config == nil {
dm.config = DefaultDomain
} else {
dm.config = *config
}
switch dm.config.DCType {
case RPCCDriver:
dm.driver = hypervisor.NewRPCDriver(func() (net.Conn, error) {
return net.DialTimeout(dm.config.Network, dm.config.Address, dm.config.Timeout)
})
break
case SocketCDriver:
addresses := append([]hypervisor.SocketAddress{
{
Network: dm.config.Network,
Address: dm.config.Address,
Timeout: dm.config.Timeout,
},
}, dm.config.Addresses...)
dm.driver = hypervisor.NewSocketDriver(addresses)
break
}
dm.hypervisor = hypervisor.New(dm.driver)
return &dm
}
// ListVMS returns the list of all VMS available in the hypervisor.
func (dm *Domain) ListVMS() ([]string, error) {
return dm.hypervisor.DomainNames()
}
// NewContainer returns a new DomainContainer for a giving hypervisor vm.
func (dm *Domain) NewContainer(domainName string) (*DomainContainer, error) {
domain, err := dm.hypervisor.Domain(domainName)
if err != nil {
return nil, err
}
return &DomainContainer{
name: domainName,
domain: domain,
}, nil
}
// DomainContainer defines a structure which interacts with a given VM
// domain object.
type DomainContainer struct {
name string
domain *qemu.Domain
}
// Domain returns the underline qemu.Domain object for this container.
func (dmc *DomainContainer) Domain() *qemu.Domain {
return dmc.domain
}
// Name returns the giving name associated with the domain vm.
func (dmc *DomainContainer) Name() string {
return dmc.name
}
// Devices returns the giving list of available devices for the domain vm.
func (dmc *DomainContainer) Devices() ([]qemu.BlockDevice, []qemu.PCIDevice, error) {
pcis, err := dmc.domain.PCIDevices()
if err != nil {
return nil, nil, err
}
blocks, err := dmc.domain.BlockDevices()
if err != nil {
return nil, nil, err
}
return blocks, pcis, nil
}
// Status returns the domain vm current status.
func (dmc *DomainContainer) Status() (qemu.Status, error) {
return dmc.domain.Status()
}
// Network returns network details related to the vm.
func (dmc *DomainContainer) Network() ([]byte, error) {
// if status, err := dmc.domain.Status
res, err := dmc.domain.Run(qmp.Command{
Execute: "query-vnc",
})
return res, err
}
// Wakeup activtes the domain if wakeup and starts the domains vm for
// interaction.
func (dmc *DomainContainer) Wakeup() error {
// if status, err := dmc.domain.Status
_, err := dmc.domain.Run(qmp.Command{
Execute: "system_wakeup",
})
return err
}
// Resume activtes the domain if paused and resumes the domains vm for
// interaction.
func (dmc *DomainContainer) Resume() error {
// if status, err := dmc.domain.Status
_, err := dmc.domain.Run(qmp.Command{
Execute: "cont",
})
return err
}
// Start activtes the domain if not running and starts the domains vm for
// interaction.
func (dmc *DomainContainer) Start() error {
// if status, err := dmc.domain.Status
_, err := dmc.domain.Run(qmp.Command{
Execute: "system_wakeup",
})
return err
}
// Reset resets the domain and its state.
func (dmc *DomainContainer) Reset() error {
return dmc.domain.SystemReset()
}
// Stop Poweroff the giving domain.
func (dmc *DomainContainer) Stop() error {
return dmc.domain.SystemPowerdown()
}
// NetworkAddress returns the associated network address for the vm.
func (dmc *DomainContainer) NetworkAddress() string {
var addr string
return addr
}