-
Notifications
You must be signed in to change notification settings - Fork 3
/
disk.go
254 lines (238 loc) · 8.92 KB
/
disk.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// Copyright (C) 2017 Battelle Memorial Institute
// All rights reserved.
//
// This software may be modified and distributed under the terms
// of the BSD-2 license. See the LICENSE file for details.
package ovirtapi
import (
"encoding/json"
"fmt"
)
type VolumeGroup struct {
id string `json:"id,omitempty"`
logical_units []LogicalUnit `json:"logical_units,omitempty"`
name string `json:"name,omitempty"`
}
type LogicalUnit struct {
Address string `json:"address,omitempty"`
DiscardMaxSize int `json:"discard_max_size,omitempty"`
// The maximum number of bytes that can be discarded by the logical unit's underlying storage in a single operation.
DiscardZeroesData string `json:"discard_zeroes_data,omitempty"`
// True, if previously discarded blocks in the logical unit's underlying storage are read back as zeros.
DiskID string `json:"disk_id,omitempty"`
ID string `json:"id,omitempty"`
LUNMapping int `json:"lun_mapping,omitempty,string"`
Password string `json:"password,omitempty"`
Paths int `json:"paths,omitempty,string"`
Port int `json:"port,omitempty,string"`
Portal string `json:"portal,omitempty"`
ProductID string `json:"product_id,omitempty"`
Serial string `json:"serial,omitempty"`
Size int `json:"size,omitempty"`
Status string `json:"status,omitempty"`
StorageDomainID string `json:"storage_domain_id,omitempty"`
Target string `json:"target,omitempty"`
Username string `json:"username,omitempty"`
VendorID string `json:"vendor_id,omitempty"`
VolumeGroupID string `json:"volume_group_id,omitempty"`
}
type HostStorage struct {
Address string `json:"address,omitempty"`
// Free text containing comments about this object.
Comment string `json:"comment,omitempty"`
// A human-readable description in plain text.
Description string `json:"description,omitempty"`
// A unique identifier.
ID string `json:"id,omitempty"`
LogicalUnits []LogicalUnit `json:"logical_units,omitempty"`
MountOptions string `json:"mount_options,omitempty"`
// A human-readable name in plain text.
Name string `json:"name,omitempty"`
// The number of times to retry a request before attempting further recovery actions.
NfsRetrans int `json:"nfs_retrans,omitempty,string"`
// The time in tenths of a second to wait for a response before retrying NFS requests.
NfsTimeo int `json:"nfs_timeo,omitempty,string"`
NfsVersion string `json:"nfs_version,omitempty"`
OverrideLUNS string `json:"override_luns,omitempty"`
Password string `json:"password,omitempty"`
Path string `json:"path,omitempty"`
Port int `json:"port,omitempty,string"`
Portal string `json:"portal,omitempty"`
Target string `json:"target,omitempty"`
Type string `json:"type,omitempty"`
Username string `json:"username,omitempty"`
VfsType string `json:"vfs_type,omitempty"`
VolumeGroup *VolumeGroup `json:"volume_group,omitempty"`
}
type StorageDomains struct {
//TODO make StorageDomain
StorageDomain []Link `json:"storage_domain,omitempty"`
}
type Disk struct {
OvirtObject
// Indicates if the disk is visible to the virtual machine.
Active string `json:"active,omitempty"`
// The actual size of the disk, in bytes.
ActualSize int `json:"actual_size,omitempty"`
Alias string `json:"alias,omitempty"`
// Indicates if the disk is marked as bootable.
Bootable string `json:"bootable,omitempty"`
// Free text containing comments about this object.
Comment string `json:"comment,omitempty"`
// The underlying storage format.
Format string `json:"format,omitempty"`
ImageID string `json:"image_id,omitempty"`
// The initial size of a sparse image disk created on block storage, in bytes.
InitialSize int `json:"initial_size,omitempty"`
// The type of interface driver used to connect the disk device to the virtual machine.
Interface string `json:"interface,omitempty"`
LogicalName string `json:"logical_name,omitempty"`
LunStorage *HostStorage `json:"lun_storage,omitempty"`
// Indicates if disk errors should cause virtual machine to be paused or if disk errors should be propagated to the the guest operating system instead.
PropagateErrors string `json:"propagate_errors,omitempty"`
// The virtual size of the disk, in bytes.
ProvisionedSize int `json:"provisioned_size,omitempty"`
// The underlying QCOW version of a QCOW volume.
QcowVersion string `json:"qcow_version,omitempty"`
// Indicates if the disk is in read-only mode.
ReadOnly string `json:"read_only,omitempty"`
SGIO string `json:"sgio,omitempty"`
// Indicates if the disk can be attached to multiple virtual machines.
Shareable string `json:"shareable,omitempty"`
// Indicates if the physical storage for the disk should not be preallocated.
Sparse string `json:"sparse,omitempty"`
// The status of the disk device.
Status string `json:"status,omitempty"`
StorageType string `json:"storage_type,omitempty"`
UsesSCSIReservation string `json:"uses_scsi_reservation,omitempty"`
// Indicates if the disk's blocks will be read back as zeros after it is deleted:
//
// - On block storage, the disk will be zeroed and only then deleted.
WipeAfterDelete string `json:"wipe_after_delete,omitempty"`
// TODO Make DiskProfile
DiskProfile *Link `json:"disk_profile,omitempty"`
// Optionally references to an instance type the device is used by.
// TODO Make InstanceType
InstanceType *Link `json:"instance_type,omitempty"`
// TODO Make OpenStackVolumeType
OpenstackVolumeType *Link `json:"openstack_volume_type,omitempty"`
// TODO Make Permission
Permissions []Link `json:"permissions,omitempty"`
// TODO Make Quota
Quota *Link `json:"quota,omitempty"`
// TODO Make Snapshot
Snapshot *Link `json:"snapshot,omitempty"`
// Statistics exposed by the disk.
// TODO Make Statistic
Statistics []Link `json:"statistics,omitempty"`
// The storage domains associated with this disk.
// TODO Make StorageDomain
StorageDomains *StorageDomains `json:"storage_domains,omitempty"`
// Optionally references to a template the device is used by.
Template *Template `json:"template,omitempty"`
// References to the virtual machines that are using this device.
VMs []VM `json:"vms,omitempty"`
}
func (con *Connection) GetDisk(id string) (*Disk, error) {
body, err := con.GetLinkBody("disks", id)
if err != nil {
return nil, err
}
disk := con.NewDisk()
err = json.Unmarshal(body, disk)
if err != nil {
return nil, err
}
return disk, err
}
// Update Synchronize the local Disk with a copy from the server
func (disk *Disk) Update() error {
if disk.Href == "" {
return fmt.Errorf("Disk has not been saved to the server")
}
newDisk, err := disk.Con.GetDisk(disk.ID)
if err != nil {
return err
}
*disk = *newDisk
return nil
}
func (con *Connection) GetAllDisks() ([]*Disk, error) {
body, err := con.GetLinkBody("disks", "")
if err != nil {
return nil, err
}
disks := []*Disk{}
err = json.Unmarshal(body, &struct {
Disk *[]*Disk
}{&disks})
if err != nil {
return nil, err
}
for _, disk := range disks {
disk.Con = con
}
return disks, err
}
func (con *Connection) NewDisk() *Disk {
return &Disk{OvirtObject: OvirtObject{Con: con}}
}
func (disk *Disk) Save() error {
body, err := json.MarshalIndent(disk, "", " ")
if err != nil {
return err
}
// If there is a link, it is an already saved disk, we need to update it
if disk.OvirtObject.Href != "" {
body, err = disk.Con.Request("PUT", disk.Con.ResolveLink(disk.Href), body)
if err != nil {
return err
}
} else {
link, err := disk.Con.GetLink("disks")
if err != nil {
return err
}
body, err = disk.Con.Request("POST", link, body)
if err != nil {
return err
}
}
tempDisk := Disk{OvirtObject: OvirtObject{Con: disk.Con}}
err = json.Unmarshal(body, &tempDisk)
if err != nil {
return err
}
*disk = tempDisk
return nil
}
// // Copy This operation copies a disk to the specified storage domain.
// func (vm *VM) Copy(async string, disk *Disk, filter string, storageDomain *StorageDomain) error {
// return vm.DoAction("copy", Action{
// Async: aync,
// Disk: disk,
// Filter: filter,
// StorageDomain: StorageDomain,
// })
// }
// func (vm *VM) Export(async, filter string, storageDomain *StorageDomain) error {
// return vm.DoAction("export", Action{
// Async: aync,
// Disk: disk,
// Filter: filter,
// StorageDomain: StorageDomain,
// })
// }
// // Move a disk to another storage domain.
// func (vm *VM) Move(async, filter string, storageDomain *StorageDomain) error {
// return vm.DoAction("move", Action{
// Async: aync,
// Disk: disk,
// Filter: filter,
// StorageDomain: StorageDomain,
// })
// }
// Sparsify the disk.
func (vm *VM) Sparsify() error {
return vm.DoAction("move", Action{})
}