-
Notifications
You must be signed in to change notification settings - Fork 129
/
root_volumesource.go
126 lines (105 loc) · 3.39 KB
/
root_volumesource.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
/*
Copyright 2017 Mirantis
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package libvirttools
import (
"fmt"
libvirtxml "github.com/libvirt/libvirt-go-xml"
"github.com/Mirantis/virtlet/pkg/metadata/types"
"github.com/Mirantis/virtlet/pkg/virt"
)
// rootVolume denotes the root disk of the VM
type rootVolume struct {
volumeBase
}
var _ VMVolume = &rootVolume{}
// GetRootVolume returns volume source for root volume clone.
func GetRootVolume(config *types.VMConfig, owner volumeOwner) ([]VMVolume, error) {
var vol VMVolume
rootDev := config.RootVolumeDevice()
if rootDev != nil {
vol = &persistentRootVolume{
volumeBase: volumeBase{config, owner},
dev: *rootDev,
}
} else {
vol = &rootVolume{
volumeBase{config, owner},
}
}
return []VMVolume{vol}, nil
}
func (v *rootVolume) volumeName() string {
return "virtlet_root_" + v.config.DomainUUID
}
func (v *rootVolume) createVolume() (virt.StorageVolume, error) {
imagePath, _, virtualSize, err := v.owner.ImageManager().GetImagePathDigestAndVirtualSize(v.config.Image)
if err != nil {
return nil, err
}
if v.config.ParsedAnnotations != nil && v.config.ParsedAnnotations.RootVolumeSize > 0 &&
uint64(v.config.ParsedAnnotations.RootVolumeSize) > virtualSize {
virtualSize = uint64(v.config.ParsedAnnotations.RootVolumeSize)
}
storagePool, err := v.owner.StoragePool()
if err != nil {
return nil, err
}
return storagePool.CreateStorageVol(&libvirtxml.StorageVolume{
Type: "file",
Name: v.volumeName(),
Allocation: &libvirtxml.StorageVolumeSize{
Unit: "b",
Value: 0,
},
Capacity: &libvirtxml.StorageVolumeSize{
Unit: "b",
Value: virtualSize,
},
Target: &libvirtxml.StorageVolumeTarget{
Format: &libvirtxml.StorageVolumeTargetFormat{Type: "qcow2"},
},
BackingStore: &libvirtxml.StorageVolumeBackingStore{
Path: imagePath,
Format: &libvirtxml.StorageVolumeTargetFormat{Type: "qcow2"},
},
})
}
func (v *rootVolume) IsDisk() bool { return true }
func (v *rootVolume) UUID() string { return "" }
func (v *rootVolume) Setup() (*libvirtxml.DomainDisk, *libvirtxml.DomainFilesystem, error) {
vol, err := v.createVolume()
if err != nil {
return nil, nil, err
}
volPath, err := vol.Path()
if err != nil {
return nil, nil, fmt.Errorf("error getting root volume path: %v", err)
}
if len(v.config.ParsedAnnotations.InjectedFiles) > 0 {
if err := v.owner.StorageConnection().PutFiles(volPath, v.config.ParsedAnnotations.InjectedFiles); err != nil {
return nil, nil, fmt.Errorf("error adding files to rootfs: %v", err)
}
}
return &libvirtxml.DomainDisk{
Device: "disk",
Driver: &libvirtxml.DomainDiskDriver{Name: "qemu", Type: "qcow2"},
Source: &libvirtxml.DomainDiskSource{File: &libvirtxml.DomainDiskSourceFile{File: volPath}},
}, nil, nil
}
func (v *rootVolume) Teardown() error {
storagePool, err := v.owner.StoragePool()
if err != nil {
return err
}
return storagePool.RemoveVolumeByName(v.volumeName())
}