github.com/mirantis/virtlet@v1.5.2-0.20191204181327-1659b8a48e9b/pkg/libvirttools/root_volumesource.go (about) 1 /* 2 Copyright 2017 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 22 libvirtxml "github.com/libvirt/libvirt-go-xml" 23 24 "github.com/Mirantis/virtlet/pkg/metadata/types" 25 "github.com/Mirantis/virtlet/pkg/virt" 26 ) 27 28 // rootVolume denotes the root disk of the VM 29 type rootVolume struct { 30 volumeBase 31 } 32 33 var _ VMVolume = &rootVolume{} 34 35 // GetRootVolume returns volume source for root volume clone. 36 func GetRootVolume(config *types.VMConfig, owner volumeOwner) ([]VMVolume, error) { 37 var vol VMVolume 38 rootDev := config.RootVolumeDevice() 39 if rootDev != nil { 40 vol = &persistentRootVolume{ 41 volumeBase: volumeBase{config, owner}, 42 dev: *rootDev, 43 } 44 } else { 45 vol = &rootVolume{ 46 volumeBase{config, owner}, 47 } 48 } 49 return []VMVolume{vol}, nil 50 } 51 52 func (v *rootVolume) volumeName() string { 53 return "virtlet_root_" + v.config.DomainUUID 54 } 55 56 func (v *rootVolume) createVolume() (virt.StorageVolume, error) { 57 imagePath, _, virtualSize, err := v.owner.ImageManager().GetImagePathDigestAndVirtualSize(v.config.Image) 58 if err != nil { 59 return nil, err 60 } 61 62 if v.config.ParsedAnnotations != nil && v.config.ParsedAnnotations.RootVolumeSize > 0 && 63 uint64(v.config.ParsedAnnotations.RootVolumeSize) > virtualSize { 64 virtualSize = uint64(v.config.ParsedAnnotations.RootVolumeSize) 65 } 66 67 storagePool, err := v.owner.StoragePool() 68 if err != nil { 69 return nil, err 70 } 71 return storagePool.CreateStorageVol(&libvirtxml.StorageVolume{ 72 Type: "file", 73 Name: v.volumeName(), 74 Allocation: &libvirtxml.StorageVolumeSize{ 75 Unit: "b", 76 Value: 0, 77 }, 78 Capacity: &libvirtxml.StorageVolumeSize{ 79 Unit: "b", 80 Value: virtualSize, 81 }, 82 Target: &libvirtxml.StorageVolumeTarget{ 83 Format: &libvirtxml.StorageVolumeTargetFormat{Type: "qcow2"}, 84 }, 85 BackingStore: &libvirtxml.StorageVolumeBackingStore{ 86 Path: imagePath, 87 Format: &libvirtxml.StorageVolumeTargetFormat{Type: "qcow2"}, 88 }, 89 }) 90 } 91 92 func (v *rootVolume) IsDisk() bool { return true } 93 94 func (v *rootVolume) UUID() string { return "" } 95 96 func (v *rootVolume) Setup() (*libvirtxml.DomainDisk, *libvirtxml.DomainFilesystem, error) { 97 vol, err := v.createVolume() 98 if err != nil { 99 return nil, nil, err 100 } 101 102 volPath, err := vol.Path() 103 if err != nil { 104 return nil, nil, fmt.Errorf("error getting root volume path: %v", err) 105 } 106 107 if len(v.config.ParsedAnnotations.InjectedFiles) > 0 { 108 if err := v.owner.StorageConnection().PutFiles(volPath, v.config.ParsedAnnotations.InjectedFiles); err != nil { 109 return nil, nil, fmt.Errorf("error adding files to rootfs: %v", err) 110 } 111 } 112 113 return &libvirtxml.DomainDisk{ 114 Device: "disk", 115 Driver: &libvirtxml.DomainDiskDriver{Name: "qemu", Type: "qcow2"}, 116 Source: &libvirtxml.DomainDiskSource{File: &libvirtxml.DomainDiskSourceFile{File: volPath}}, 117 }, nil, nil 118 } 119 120 func (v *rootVolume) Teardown() error { 121 storagePool, err := v.owner.StoragePool() 122 if err != nil { 123 return err 124 } 125 return storagePool.RemoveVolumeByName(v.volumeName()) 126 }