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