github.com/rothwerx/packer@v0.9.0/post-processor/vagrant/libvirt.go (about)

     1  package vagrant
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/mitchellh/packer/packer"
     6  	"path/filepath"
     7  	"strings"
     8  )
     9  
    10  type LibVirtProvider struct{}
    11  
    12  func (p *LibVirtProvider) KeepInputArtifact() bool {
    13  	return false
    14  }
    15  func (p *LibVirtProvider) Process(ui packer.Ui, artifact packer.Artifact, dir string) (vagrantfile string, metadata map[string]interface{}, err error) {
    16  	diskName := artifact.State("diskName").(string)
    17  
    18  	// Copy the disk image into the temporary directory (as box.img)
    19  	for _, path := range artifact.Files() {
    20  		if strings.HasSuffix(path, "/"+diskName) {
    21  			ui.Message(fmt.Sprintf("Copying from artifact: %s", path))
    22  			dstPath := filepath.Join(dir, "box.img")
    23  			if err = CopyContents(dstPath, path); err != nil {
    24  				return
    25  			}
    26  		}
    27  	}
    28  
    29  	format := artifact.State("diskType").(string)
    30  	origSize := artifact.State("diskSize").(uint64)
    31  	size := origSize / 1024 // In MB, want GB
    32  	if origSize%1024 > 0 {
    33  		// Make sure we don't make the size smaller
    34  		size++
    35  	}
    36  	domainType := artifact.State("domainType").(string)
    37  
    38  	// Convert domain type to libvirt driver
    39  	var driver string
    40  	switch domainType {
    41  	case "kvm", "qemu":
    42  		driver = domainType
    43  	default:
    44  		return "", nil, fmt.Errorf("Unknown libvirt domain type: %s", domainType)
    45  	}
    46  
    47  	// Create the metadata
    48  	metadata = map[string]interface{}{
    49  		"provider":     "libvirt",
    50  		"format":       format,
    51  		"virtual_size": size,
    52  	}
    53  
    54  	vagrantfile = fmt.Sprintf(libvirtVagrantfile, driver)
    55  	return
    56  }
    57  
    58  var libvirtVagrantfile = `
    59  Vagrant.configure("2") do |config|
    60    config.vm.provider :libvirt do |libvirt|
    61      libvirt.driver = "%s"
    62    end
    63  end
    64  `