2014-01-05 20:02:30 -05:00
|
|
|
package vagrant
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2018-01-22 20:21:10 -05:00
|
|
|
|
|
|
|
"github.com/hashicorp/packer/packer"
|
2014-01-05 20:02:30 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type LibVirtProvider struct{}
|
|
|
|
|
|
|
|
func (p *LibVirtProvider) KeepInputArtifact() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
func (p *LibVirtProvider) Process(ui packer.Ui, artifact packer.Artifact, dir string) (vagrantfile string, metadata map[string]interface{}, err error) {
|
|
|
|
diskName := artifact.State("diskName").(string)
|
|
|
|
|
|
|
|
// Copy the disk image into the temporary directory (as box.img)
|
|
|
|
for _, path := range artifact.Files() {
|
|
|
|
if strings.HasSuffix(path, "/"+diskName) {
|
|
|
|
ui.Message(fmt.Sprintf("Copying from artifact: %s", path))
|
|
|
|
dstPath := filepath.Join(dir, "box.img")
|
|
|
|
if err = CopyContents(dstPath, path); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
format := artifact.State("diskType").(string)
|
|
|
|
origSize := artifact.State("diskSize").(uint64)
|
|
|
|
size := origSize / 1024 // In MB, want GB
|
2014-10-28 11:43:19 -04:00
|
|
|
if origSize%1024 > 0 {
|
2014-01-05 20:02:30 -05:00
|
|
|
// Make sure we don't make the size smaller
|
|
|
|
size++
|
|
|
|
}
|
|
|
|
domainType := artifact.State("domainType").(string)
|
|
|
|
|
|
|
|
// Convert domain type to libvirt driver
|
|
|
|
var driver string
|
|
|
|
switch domainType {
|
2016-11-03 07:55:55 -04:00
|
|
|
case "none", "tcg":
|
|
|
|
driver = "qemu"
|
|
|
|
case "kvm":
|
2014-01-05 20:02:30 -05:00
|
|
|
driver = domainType
|
|
|
|
default:
|
|
|
|
return "", nil, fmt.Errorf("Unknown libvirt domain type: %s", domainType)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the metadata
|
|
|
|
metadata = map[string]interface{}{
|
|
|
|
"provider": "libvirt",
|
|
|
|
"format": format,
|
|
|
|
"virtual_size": size,
|
|
|
|
}
|
|
|
|
|
|
|
|
vagrantfile = fmt.Sprintf(libvirtVagrantfile, driver)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var libvirtVagrantfile = `
|
|
|
|
Vagrant.configure("2") do |config|
|
|
|
|
config.vm.provider :libvirt do |libvirt|
|
|
|
|
libvirt.driver = "%s"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
`
|