github.com/mirantis/virtlet@v1.5.2-0.20191204181327-1659b8a48e9b/pkg/libvirttools/block_volumesource.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  	"path/filepath"
    21  
    22  	libvirtxml "github.com/libvirt/libvirt-go-xml"
    23  
    24  	"github.com/Mirantis/virtlet/pkg/metadata/types"
    25  )
    26  
    27  // blockBolume denotes a block device that's made accessible inside the VM
    28  type blockVolume struct {
    29  	volumeBase
    30  	dev types.VMVolumeDevice
    31  }
    32  
    33  var _ VMVolume = &blockVolume{}
    34  
    35  func (v *blockVolume) IsDisk() bool { return true }
    36  
    37  func (v *blockVolume) UUID() string {
    38  	return v.dev.UUID()
    39  }
    40  
    41  func (v *blockVolume) Setup() (*libvirtxml.DomainDisk, *libvirtxml.DomainFilesystem, error) {
    42  	// we need to follow the symlinks as only devices under /dev
    43  	// will be chown'ed properly by QEMU
    44  	hostPath, err := filepath.EvalSymlinks(v.dev.HostPath)
    45  	if err != nil {
    46  		return nil, nil, err
    47  	}
    48  	return &libvirtxml.DomainDisk{
    49  		Device: "disk",
    50  		Source: &libvirtxml.DomainDiskSource{Block: &libvirtxml.DomainDiskSourceBlock{Dev: hostPath}},
    51  		Driver: &libvirtxml.DomainDiskDriver{Name: "qemu", Type: "raw"},
    52  	}, nil, nil
    53  }
    54  
    55  func (v *blockVolume) Teardown() error {
    56  	return nil
    57  }
    58  
    59  // GetBlockVolumes returns VMVolume objects for block devices that are
    60  // passed to the pod.
    61  func GetBlockVolumes(config *types.VMConfig, owner volumeOwner) ([]VMVolume, error) {
    62  	var vols []VMVolume
    63  	for _, dev := range config.VolumeDevices {
    64  		if dev.IsRoot() {
    65  			continue
    66  		}
    67  		vols = append(vols, &blockVolume{
    68  			volumeBase: volumeBase{config, owner},
    69  			dev:        dev,
    70  		})
    71  	}
    72  	return vols, nil
    73  }