forked from quadrifoglio/go-qemu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage.go
534 lines (454 loc) · 14.1 KB
/
image.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
package qemu
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"strconv"
"time"
)
const (
ImageFormatRAW = "raw"
ImageFormatCLOOP = "cloop"
ImageFormatCOW = "cow"
ImageFormatQCOW = "qcow"
ImageFormatQCOW2 = "qcow2"
ImageFormatVDMK = "vdmk"
ImageFormatVDI = "vdi"
ImageFormatVHDX = "vhdx"
ImageFormatVPC = "vpc"
CompatLevelQCOW3 = "1.1"
CompatLevelQCOW2 = "0.10"
ImagePreallocMetadata = "metadata"
ImagePreallocFalloc = "falloc"
ImagePreallocFull = "full"
CipherAlgorithmAES256 = "aes-256"
CipherHashAlgorithmSHA256 = "sha256"
CipherFormatLUKS = "luks"
CipherFormatAES = "aes"
CipherModeXTS = "xts"
IVGenAlgPlain64 = "plain64"
IVGenHashAlgorithmSHA256 = "sha256"
)
// Image represents a QEMU disk image
type Image struct {
Path string // Image location (filepath)
Format string // Image format
Size uint64 // Image size in bytes
Secret string // Image secret, this enables encryption
BackingFile string // Image backing file (filepath)
Encrypted bool // Image encryption value (readonly)
LazyRefcounts bool // Image lazy refcount value
CompatLevel string // Image compatibility level
RefcountBits int64 // Image refcount bits
ClusterSizeKB int64 // Image cluster size (bytes)
ExtendedL2 bool // Image L2 table extension value
Preallocation string // Image preallocation type
CipherAlgorithm string // Image encryption cipher algorithm
CipherMode string // Image encryption cipher mode
CipherFormat string // Image encryption cipher format
CipherHashAlg string // Image encryption cipher hash algorithm
EncryptIterTime int64 // Image encryption PBKDF iteration time (ms)
EncryptIvGenAlg string // Image encryption IV generation algorithm
EncryptIvGenHashAlg string // Image encryption IV generation hash algorithm
snapshots []Snapshot // Image snapshot array
}
// Snapshot represents a QEMU image snapshot
// Snapshots are snapshots of the complete virtual machine including CPU state
// RAM, device state and the content of all the writable disks
type Snapshot struct {
ID int // Snapshot numerical ID
Name string // Snapshot Name
Date time.Time // Snapshot creation Date
VMClock time.Time
}
// NewImage constructs a new Image data structure based
// on the specified parameters
func NewImage(path, format string, size uint64) Image {
var img Image
img.Path = path
img.Format = format
img.Size = size
img.ClusterSizeKB = 64
img.RefcountBits = 16
return img
}
// NewEncryptedImage constructs a new Image data structure based
// on the specified parameters
func NewEncryptedImage(path, format, secret string, size uint64) (Image, error) {
var img Image
img.Path = path
img.Format = format
img.Size = size
img.Secret = secret
img.Encrypted = true
img.ClusterSizeKB = 64
img.RefcountBits = 16
if format != ImageFormatQCOW2 {
return img, fmt.Errorf("encrypted volumes must be of the type 'ImageFormatQCOW2'")
}
return img, nil
}
// OpenImage retrieves the information of the specified image
// file into an Image data structure
func OpenImage(path string) (Image, error) {
var img Image
if _, err := os.Stat(path); os.IsNotExist(err) {
return img, err
}
img.Path = path
img, err := img.retreiveInfos()
if err != nil {
return img, err
}
if img.Encrypted {
return img, fmt.Errorf("image is encrypted but secret was not provided")
}
return img, nil
}
// OpenEncryptedImage retrieves the information of the specified image
// file into an Image data structure
func OpenEncryptedImage(path, secret string) (Image, error) {
var img Image
if _, err := os.Stat(path); os.IsNotExist(err) {
return img, err
}
img.Path = path
img.Encrypted = true
img.Secret = secret
img, err := img.retreiveInfos()
if err != nil {
return img, err
}
if secret == "" {
return img, fmt.Errorf("cannot open encrypted image without secret")
}
if !img.Encrypted {
return img, fmt.Errorf("image is not encrypted")
}
return img, nil
}
func (i *Image) retreiveInfos() (Image, error) {
type snapshotInfo struct {
ID string `json:"id"`
Name string `json:"name"`
DateSec int64 `json:"date-sec"`
DateNsec int64 `json:"date-nsec"`
ClockSec int64 `json:"vm-clock-sec"`
ClockNsec int64 `json:"vm-clock-nsec"`
}
type imgInfo struct {
Snapshots []snapshotInfo `json:"snapshots"`
Format string `json:"format"`
Size uint64 `json:"virtual-size"`
Encrypted bool `json:"encrypted,omitempty"`
}
var info imgInfo
cmd := exec.Command("qemu-img", "info", "--output=json", i.Path)
out, err := cmd.CombinedOutput()
if err != nil {
return *i, fmt.Errorf("'qemu-img info' output: %s", oneLine(out))
}
err = json.Unmarshal(out, &info)
if err != nil {
return *i, fmt.Errorf("'qemu-img info' invalid json output")
}
i.Format = info.Format
i.Size = info.Size
if i.Secret != "" {
i.Encrypted = true
} else {
i.Encrypted = info.Encrypted
}
i.snapshots = make([]Snapshot, 0)
for _, snap := range info.Snapshots {
var s Snapshot
id, err := strconv.Atoi(snap.ID)
if err != nil {
continue
}
s.ID = id
s.Name = snap.Name
s.Date = time.Unix(snap.DateSec, snap.DateNsec)
s.VMClock = time.Unix(snap.ClockSec, snap.ClockNsec)
i.snapshots = append(i.snapshots, s)
}
return *i, nil
}
// Snapshots returns the snapshots contained
// within the image
func (i Image) Snapshots() ([]Snapshot, error) {
_, err := i.retreiveInfos()
if err != nil {
return nil, err
}
if len(i.snapshots) == 0 {
return make([]Snapshot, 0), nil
}
return i.snapshots, nil
}
// CreateSnapshot creates a snapshot of the image
// with the specified name
func (i *Image) CreateSnapshot(name string) (Snapshot, error) {
var snap Snapshot
// Handles normal volumes
if i.Encrypted == false {
cmd := exec.Command("qemu-img", "snapshot", "-c", name, i.Path)
out, err := cmd.CombinedOutput()
if err != nil {
return snap, fmt.Errorf("'qemu-img snapshot' output: %s", oneLine(out))
}
snaps, err := i.Snapshots()
if err != nil {
return snap, err
}
var exists bool
for _, s := range snaps {
if s.Name == name {
snap = s
exists = true
break
}
}
if exists {
return snap, nil
} else {
return snap, fmt.Errorf("couldn't find newly created snapshot")
}
}
// Handles encrypted volumes
cmd := exec.Command("qemu-img", "snapshot", "--object", "secret,id=sec0,data="+i.Secret, "--image-opts", "-c", name, "encrypt.format=luks,encrypt.key-secret=sec0,file.filename="+i.Path)
out, err := cmd.CombinedOutput()
if err != nil {
return snap, fmt.Errorf("'qemu-img snapshot' output: %s", oneLine(out))
}
snaps, err := i.Snapshots()
if err != nil {
return snap, err
}
var exists bool
for _, s := range snaps {
if s.Name == name {
snap = s
exists = true
break
}
}
if exists {
return snap, nil
} else {
return snap, fmt.Errorf("couldn't find newly created snapshot")
}
}
// OptimizeSpeed Optimizes the way QEMU handles caching of data while writing to a volume.
// OptimizeSpeed DOES NOT WORK ON EXISTING VOLUMES
func (i Image) OptimizeSpeed() Image {
i.LazyRefcounts = true
i.CompatLevel = CompatLevelQCOW3
i.RefcountBits = 64
i.ClusterSizeKB = 1024
i.ExtendedL2 = true
i.Preallocation = "full"
switch {
case i.Encrypted:
i.CipherAlgorithm = CipherAlgorithmAES256
i.CipherHashAlg = CipherHashAlgorithmSHA256
i.CipherFormat = CipherFormatLUKS
i.CipherMode = CipherModeXTS
i.EncryptIvGenAlg = IVGenAlgPlain64
i.EncryptIterTime = 1000
i.EncryptIvGenHashAlg = IVGenHashAlgorithmSHA256
}
return i
}
// OptimizeSize Optimizes the way QEMU handles the allocation of data to a volume.
// OptimizeSize DOES NOT WORK ON EXISTING VOLUMES
func (i Image) OptimizeSize() Image {
i.RefcountBits = 16
i.ClusterSizeKB = 64
i.ExtendedL2 = true
i.Preallocation = "metadata"
switch {
case i.Encrypted:
i.CipherAlgorithm = CipherAlgorithmAES256
i.CipherHashAlg = CipherHashAlgorithmSHA256
i.CipherFormat = CipherFormatLUKS
i.CipherMode = CipherModeXTS
i.EncryptIvGenAlg = IVGenAlgPlain64
i.EncryptIterTime = 2000
i.EncryptIvGenHashAlg = IVGenHashAlgorithmSHA256
}
return i
}
// RestoreSnapshot restores the the image to the
// specified snapshot name
func (i Image) RestoreSnapshot(name string) error {
// Handles normal volumes
if i.Encrypted == false {
cmd := exec.Command("qemu-img", "snapshot", "-a", name, i.Path)
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("'qemu-img snapshot' output: %s", oneLine(out))
}
return nil
}
// Handles encrypted volumes
cmd := exec.Command("qemu-img", "snapshot", "--object", "secret,id=sec0,data="+i.Secret, "--image-opts", "-a", name, "encrypt.format=luks,encrypt.key-secret=sec0,file.filename="+i.Path)
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("'qemu-img snapshot' output: %s", oneLine(out))
}
return nil
}
// DeleteSnapshot deletes the the corresponding
// snapshot from the image
func (i Image) DeleteSnapshot(name string) error {
if i.Encrypted == false {
cmd := exec.Command("qemu-img", "snapshot", "-d", name, i.Path)
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("'qemu-img snapshot' output: %s", oneLine(out))
}
return nil
}
cmd := exec.Command("qemu-img", "snapshot", "--object", "secret,id=sec0,data="+i.Secret, "--image-opts", "-d", name, "encrypt.format=luks,encrypt.key-secret=sec0,file.filename="+i.Path)
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("'qemu-img snapshot' output: %s", oneLine(out))
}
return nil
}
// SetBackingFile sets a backing file for the image
// If it is specified, the image will only record the
// differences from the backing file
func (i *Image) SetBackingFile(backingFile string) error {
if _, err := os.Stat(backingFile); os.IsNotExist(err) {
return err
}
i.BackingFile = backingFile
return nil
}
// Create actually creates the image based on the Image structure
// using the 'qemu-img create' command. If a secret is set, the volume is provisioned
// with encryption enabled.
func (i Image) Create() error {
if i.Encrypted == false {
args := []string{"create", "-f", i.Format}
if len(i.BackingFile) > 0 {
args = append(args, "-o")
args = append(args, fmt.Sprintf("backing_file=%s", i.BackingFile))
}
if len(i.CompatLevel) > 0 {
args = append(args, "-o")
args = append(args, fmt.Sprintf("compat=%s", i.CompatLevel))
}
if i.ClusterSizeKB != 64 {
args = append(args, "-o")
args = append(args, fmt.Sprintf("cluster_size=%dK", i.ClusterSizeKB))
}
if i.ExtendedL2 {
args = append(args, "-o")
args = append(args, "extended_l2=on")
}
if i.LazyRefcounts {
args = append(args, "-o")
args = append(args, "lazy_refcounts=on")
}
if len(i.Preallocation) > 0 {
args = append(args, "-o")
args = append(args, fmt.Sprintf("preallocation=%s", i.Preallocation))
} else {
args = append(args, "-o")
args = append(args, fmt.Sprintf("preallocation=metadata"))
}
if i.RefcountBits != 16 {
args = append(args, "-o")
args = append(args, fmt.Sprintf("refcount_bits=%d", i.RefcountBits))
}
args = append(args, i.Path)
args = append(args, strconv.FormatUint(i.Size, 10))
cmd := exec.Command("qemu-img", args...)
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("'qemu-img create' output: %s", oneLine(out))
}
return nil
}
if i.Format != ImageFormatQCOW2 {
return fmt.Errorf("encrypted volumes must be qcow2 format")
}
args := []string{"create", "--object", "secret,id=sec0,data=" + i.Secret, "-f", i.Format, "-o", "encrypt.key-secret=sec0"}
if len(i.BackingFile) > 0 {
args = append(args, "-o")
args = append(args, fmt.Sprintf("backing_file=%s", i.BackingFile))
}
if len(i.CompatLevel) > 0 {
args = append(args, "-o")
args = append(args, fmt.Sprintf("compat=%s", i.CompatLevel))
}
if i.ClusterSizeKB != 64 {
args = append(args, "-o")
args = append(args, fmt.Sprintf("cluster_size=%dK", i.ClusterSizeKB))
}
if i.ExtendedL2 {
args = append(args, "-o")
args = append(args, "extended_l2=on")
}
if i.LazyRefcounts {
args = append(args, "-o")
args = append(args, "lazy_refcounts=on")
}
if len(i.Preallocation) > 0 {
args = append(args, "-o")
args = append(args, fmt.Sprintf("preallocation=%s", i.Preallocation))
} else {
args = append(args, "-o")
args = append(args, fmt.Sprintf("preallocation=metadata"))
}
if i.RefcountBits != 16 {
args = append(args, "-o")
args = append(args, fmt.Sprintf("refcount_bits=%d", i.RefcountBits))
}
if i.EncryptIterTime != 0 {
args = append(args, "-o", fmt.Sprintf("encrypt.iter-time=%d", i.EncryptIterTime))
}
if len(i.EncryptIvGenHashAlg) > 0 {
args = append(args, "-o", fmt.Sprintf("encrypt.ivgen-hash-alg=%s", i.EncryptIvGenHashAlg))
}
if len(i.EncryptIvGenAlg) > 0 {
args = append(args, "-o", fmt.Sprintf("encrypt.ivgen-alg=%s", i.EncryptIvGenAlg))
}
if len(i.CipherMode) > 0 {
args = append(args, "-o", fmt.Sprintf("encrypt.cipher-mode=%s", i.CipherMode))
}
if len(i.CipherAlgorithm) > 0 {
args = append(args, "-o", fmt.Sprintf("encrypt.cipher-alg=%s", i.CipherAlgorithm))
}
if len(i.CipherHashAlg) > 0 {
args = append(args, "-o", fmt.Sprintf("encrypt.hash-alg=%s", i.CipherHashAlg))
}
if len(i.CipherFormat) > 0 {
args = append(args, "-o", fmt.Sprintf("encrypt.format=%s", i.CipherFormat))
} else {
args = append(args, "-o", "encrypt.format=luks")
}
args = append(args, i.Path)
args = append(args, strconv.FormatUint(i.Size, 10))
cmd := exec.Command("qemu-img", args...)
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("'qemu-img create' output: %s", oneLine(out))
}
return nil
}
// Rebase changes the backing file of the image
// to the specified file path
func (i *Image) Rebase(backingFile string) error {
i.BackingFile = backingFile
cmd := exec.Command("qemu-img", "rebase", "-b", backingFile, i.Path)
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("'qemu-img rebase' output: %s", oneLine(out))
}
return nil
}