github.com/mirantis/virtlet@v1.5.2-0.20191204181327-1659b8a48e9b/pkg/libvirttools/filesystemvolume.go (about) 1 /* 2 Copyright 2018 Mirantis 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package libvirttools 18 19 import ( 20 "fmt" 21 "os" 22 "path" 23 24 libvirtxml "github.com/libvirt/libvirt-go-xml" 25 26 "github.com/Mirantis/virtlet/pkg/metadata/types" 27 ) 28 29 // filesystemVolume denotes the filesystem mount of the VM 30 type filesystemVolume struct { 31 volumeBase 32 mount types.VMMount 33 volumeMountPoint string 34 chownRecursively bool 35 } 36 37 var _ VMVolume = &filesystemVolume{} 38 39 func (v *filesystemVolume) IsDisk() bool { return false } 40 41 func (v *filesystemVolume) UUID() string { return "" } 42 43 func (v *filesystemVolume) Setup() (*libvirtxml.DomainDisk, *libvirtxml.DomainFilesystem, error) { 44 fsys := v.owner.FileSystem() 45 err := os.MkdirAll(v.volumeMountPoint, 0777) 46 if err == nil { 47 err = fsys.ChownForEmulator(v.volumeMountPoint, false) 48 } 49 if err == nil { 50 err = fsys.Mount(v.mount.HostPath, v.volumeMountPoint, "bind", true) 51 } 52 if err == nil { 53 err = fsys.ChownForEmulator(v.volumeMountPoint, v.chownRecursively) 54 } 55 if err != nil { 56 return nil, nil, fmt.Errorf("failed to create vm pod path %q: %v", v.volumeMountPoint, err) 57 } 58 59 fsDef := &libvirtxml.DomainFilesystem{ 60 AccessMode: "squash", 61 Source: &libvirtxml.DomainFilesystemSource{Mount: &libvirtxml.DomainFilesystemSourceMount{Dir: v.volumeMountPoint}}, 62 Target: &libvirtxml.DomainFilesystemTarget{Dir: path.Base(v.mount.ContainerPath)}, 63 } 64 65 return nil, fsDef, nil 66 } 67 68 func (v *filesystemVolume) Teardown() error { 69 var err error 70 if _, err = os.Stat(v.volumeMountPoint); err == nil { 71 err = v.owner.FileSystem().Unmount(v.volumeMountPoint, true) 72 } 73 if err == nil { 74 err = os.Remove(v.volumeMountPoint) 75 } 76 if err != nil { 77 return fmt.Errorf("failed to tear down fs volume mountpoint '%s': %v", v.volumeMountPoint, err) 78 } 79 return nil 80 }