github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/post-processor/vagrant/libvirt.go (about) 1 package vagrant 2 3 import ( 4 "fmt" 5 "github.com/hashicorp/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 "none", "tcg": 42 driver = "qemu" 43 case "kvm": 44 driver = domainType 45 default: 46 return "", nil, fmt.Errorf("Unknown libvirt domain type: %s", domainType) 47 } 48 49 // Create the metadata 50 metadata = map[string]interface{}{ 51 "provider": "libvirt", 52 "format": format, 53 "virtual_size": size, 54 } 55 56 vagrantfile = fmt.Sprintf(libvirtVagrantfile, driver) 57 return 58 } 59 60 var libvirtVagrantfile = ` 61 Vagrant.configure("2") do |config| 62 config.vm.provider :libvirt do |libvirt| 63 libvirt.driver = "%s" 64 end 65 end 66 `