github.com/mirantis/virtlet@v1.5.2-0.20191204181327-1659b8a48e9b/pkg/libvirttools/config_volumesource.go (about)

     1  /*
     2  Copyright 2016-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  	"os"
    21  
    22  	"github.com/golang/glog"
    23  	libvirtxml "github.com/libvirt/libvirt-go-xml"
    24  
    25  	"github.com/Mirantis/virtlet/pkg/metadata/types"
    26  )
    27  
    28  var configIsoDir = "/var/lib/virtlet/config"
    29  
    30  // configVolume denotes an ISO image using config format
    31  // that contains cloud-init meta-data and user-data
    32  type configVolume struct {
    33  	volumeBase
    34  }
    35  
    36  var _ VMVolume = &configVolume{}
    37  
    38  // GetConfigVolume returns a config volume source which will produce an ISO
    39  // image with CloudInit compatible configuration data.
    40  func GetConfigVolume(config *types.VMConfig, owner volumeOwner) ([]VMVolume, error) {
    41  	return []VMVolume{
    42  		&configVolume{
    43  			volumeBase{config, owner},
    44  		},
    45  	}, nil
    46  }
    47  
    48  func (v *configVolume) IsDisk() bool { return true }
    49  
    50  func (v *configVolume) UUID() string { return "" }
    51  
    52  func (v *configVolume) cloudInitGenerator() *CloudInitGenerator {
    53  	return NewCloudInitGenerator(v.config, configIsoDir)
    54  }
    55  
    56  func (v *configVolume) Setup() (*libvirtxml.DomainDisk, *libvirtxml.DomainFilesystem, error) {
    57  	return v.cloudInitGenerator().DiskDef(), nil, nil
    58  }
    59  
    60  func (v *configVolume) WriteImage(volumeMap diskPathMap) error {
    61  	return v.cloudInitGenerator().GenerateImage(volumeMap)
    62  }
    63  
    64  func (v *configVolume) Teardown() error {
    65  	isoPath := v.cloudInitGenerator().IsoPath()
    66  	if err := os.Remove(isoPath); err != nil && !os.IsNotExist(err) {
    67  		glog.Warningf("Cannot remove temporary config file %q: %v", isoPath, err)
    68  	}
    69  	return nil
    70  }
    71  
    72  // SetConfigIsoDir sets a directory for config iso dir.
    73  // It can be useful in tests
    74  func SetConfigIsoDir(dir string) {
    75  	configIsoDir = dir
    76  }
    77  
    78  // TODO: this file needs a test