-
Notifications
You must be signed in to change notification settings - Fork 3
/
export.go
90 lines (72 loc) · 2.33 KB
/
export.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
// Copyright (c) 2019 The virnsnap authors. See file "AUTHORS".
// Licensed under the MIT License. You have obtained a copy of the License at
// the "LICENSE" file in this repository.
// Package virt implements high-level functions for handling virtual machines
// (VMS) that use the more low-level libvirt functions internally.
package virt
import (
"fmt"
"os"
"path"
"github.com/joroec/virsnap/pkg/fs"
"github.com/kennygrant/sanitize"
"github.com/joroec/virsnap/pkg/instrument/log"
libvirtxml "github.com/libvirt/libvirt-go-xml"
)
// Export is a function that exports a given VM.
func (vm *VM) Export(outputDirectory string, perm os.FileMode, logger log.Logger) error {
// get the XML descriptor
xml, err := vm.Instance.GetXMLDesc(0)
if err != nil {
err = fmt.Errorf("unable to get XML descriptor of VM: %s", err)
return err
}
descriptor := libvirtxml.Domain{}
err = descriptor.Unmarshal(xml)
if err != nil {
err = fmt.Errorf("unable to unmarshal XML descriptor of VM: %s", err)
return err
}
// create the output directory for the VM if not already existing
sanVMName := sanitize.BaseName(vm.Descriptor.Name)
vmOutputDir := path.Join(outputDirectory, sanVMName)
err = os.MkdirAll(vmOutputDir, perm)
if err != nil {
return err
}
// loop over HDDs and store them using differential file sync
for _, disk := range descriptor.Devices.Disks {
// only observe disks, not cdroms
if disk.Device != "disk" {
continue
}
filepath := disk.Source.File.File
if filepath == "" {
logger.Errorf("could not get filepath of disk '%s'", disk.Target)
continue
}
filename := path.Base(filepath)
// transform descriptor
disk.Source.File.File = "./" + filename
// sync file
err = fs.Sync(filepath, path.Join(vmOutputDir, filename), logger)
if err != nil {
logger.Errorf("could sync the disk '%s': %v", filepath, err)
}
}
// store new descriptor alongside the disk files
xmldoc, err := descriptor.Marshal()
if err != nil {
err = fmt.Errorf("could marshal the new descriptor '%v': %v", descriptor, err)
return err
}
// create descriptor file if not existent, overwrite of existent
file, err := os.Create(path.Join(vmOutputDir, "descriptor.xml"))
if err != nil {
err = fmt.Errorf("could not open new descriptor file: %v", err)
return err
}
defer file.Close()
file.WriteString(xmldoc)
return nil
}