-
Notifications
You must be signed in to change notification settings - Fork 128
/
Copy pathfilesystemlist.go
145 lines (126 loc) · 3.84 KB
/
filesystemlist.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
package libvirttools
import (
"fmt"
"os"
"path"
"strings"
"syscall"
"github.com/golang/glog"
libvirtxml "github.com/libvirt/libvirt-go-xml"
"github.com/Mirantis/virtlet/pkg/metadata/types"
)
const (
mountTagfor9pfs = "virtletShared"
mountPointPathfor9pfs = "/virtletShared"
)
type filesystemItem struct {
mount types.VMMount
volumeMountPoint string
}
type filesystemList struct {
config *types.VMConfig
items []*filesystemItem
podVolumesPath string
}
func generateMountTag(hostPath string) string {
return fmt.Sprintf("volume-%s", path.Base(hostPath))
}
func newFilesystemList(config *types.VMConfig, vConfig VirtualizationConfig) (*filesystemList, error) {
volumePoolPath := supportedStoragePools[vConfig.VolumePoolName]
if _, err := os.Stat(volumePoolPath); err != nil {
return nil, err
}
podVolumesPath := path.Join(volumePoolPath, config.DomainUUID)
err := os.MkdirAll(podVolumesPath, 0777)
if err != nil {
glog.Errorf("Failed to create podVolumesPath: %v", err)
return nil, err
}
err = ChownForEmulator(podVolumesPath, true)
if err != nil {
glog.Errorf("Failed to Change owner of podVolumesPath to emulator: %v", err)
return nil, err
}
var items []*filesystemItem
for _, m := range config.Mounts {
if isRegularFile(m.HostPath) ||
strings.Contains(m.HostPath, flexvolumeSubdir) ||
strings.Contains(m.HostPath, "kubernetes.io~secret") ||
strings.Contains(m.HostPath, "kubernetes.io~configmap") {
continue
}
volumeMountPoint := path.Join(podVolumesPath, path.Base(m.HostPath))
items = append(items, &filesystemItem{m, volumeMountPoint})
}
return &filesystemList{config, items, podVolumesPath}, nil
}
func (fsList *filesystemList) setup() ([]libvirtxml.DomainFilesystem, error) {
var filesystems []libvirtxml.DomainFilesystem
for n, item := range fsList.items {
err := item.setup()
if err != nil {
// try to tear down volumes that were already set up
for _, item := range fsList.items[:n] {
if err := item.teardown(); err != nil {
glog.Warningf("Failed to tear down a fs volume on error: %v", err)
}
}
if err := os.RemoveAll(fsList.podVolumesPath); err != nil {
glog.Warningf("failed to remove '%s': %v", fsList.podVolumesPath)
}
return nil, err
}
}
fsDef := &libvirtxml.DomainFilesystem{
AccessMode: "squash",
Source: &libvirtxml.DomainFilesystemSource{Mount: &libvirtxml.DomainFilesystemSourceMount{Dir: fsList.podVolumesPath}},
Target: &libvirtxml.DomainFilesystemTarget{Dir: mountTagfor9pfs},
}
filesystems = append(filesystems, *fsDef)
return filesystems, nil
}
func (fsList *filesystemList) teardown() error {
var errs []string
for _, item := range fsList.items {
if err := item.teardown(); err != nil {
errs = append(errs, err.Error())
}
}
if err := os.RemoveAll(fsList.podVolumesPath); err != nil {
errs = append(errs, err.Error())
}
if errs != nil {
return fmt.Errorf("failed to tear down some of the fs volumes:\n%s", strings.Join(errs, "\n"))
}
return nil
}
func (fsItem *filesystemItem) setup() error {
err := os.MkdirAll(fsItem.volumeMountPoint, 0777)
if err == nil {
err = ChownForEmulator(fsItem.volumeMountPoint, true)
}
if err == nil {
err = syscall.Mount(fsItem.mount.HostPath, fsItem.volumeMountPoint, "bind", syscall.MS_BIND|syscall.MS_REC, "")
}
if err == nil {
err = ChownForEmulator(fsItem.volumeMountPoint, true)
}
if err != nil {
glog.Errorf("Failed to create vm pod path: %v", err)
return err
}
return nil
}
func (fsItem *filesystemItem) teardown() error {
var err error
if _, err = os.Stat(fsItem.volumeMountPoint); err == nil {
err = syscall.Unmount(fsItem.volumeMountPoint, 0)
}
if err == nil {
err = os.RemoveAll(fsItem.volumeMountPoint)
}
if err != nil {
return fmt.Errorf("failed to tear down fs volume mountpoint '%s': %v", fsItem.volumeMountPoint, err)
}
return nil
}