hashicorp / packer

Packer is a tool for creating identical machine images for multiple platforms from a single source configuration.
http://www.packer.io
Other
15.11k stars 3.33k forks source link

Feat. - Document the build of QEMU- / libvirt-box on macOS #6954

Closed hbokh closed 6 years ago

hbokh commented 6 years ago

If it is even possible to successfully build and export (e.g. to Vagrant Cloud) a libvirt-type Vagrant-box on macOS, please add to the documentation HOW.

As of yet I can partly build a Debian 9-box based on tylert/packer-build and these added settings in file template/debian/9_stretch/base.json:

The command ./script/qemu.sh template/debian/9_stretch/base.json will error at the end, complaining about Unknown libvirt domain type: hvf, which seems logic, because it is not kvm and KVM is not available on macOS AFAIK:

==> qemu: Gracefully halting virtual machine...
==> qemu: Running post-processor: vagrant
==> qemu (vagrant): Creating Vagrant box for 'libvirt' provider
    qemu (vagrant): Copying from artifact: build/2018-11-02-09-30-05/base-stretch
==> qemu: Running post-processor: compress
==> qemu (compress): Using pgzip compression with 8 cores for build/2018-11-02-09-30-05/base-stretch.raw.gz
==> qemu (compress): Archiving build/2018-11-02-09-30-05/base-stretch with pgzip
==> qemu (compress): Archive build/2018-11-02-09-30-05/base-stretch.raw.gz completed
==> qemu: Running post-processor: shell-local
==> qemu (shell-local): Running local shell script: /var/folders/kl/bqr5zh8x5pd_99lzq769krnr0000gn/T/packer-shell027133938
Build 'qemu' errored: 1 error(s) occurred:

* Post-processor failed: Unknown libvirt domain type: hvf

==> Some builds didn't complete successfully and had errors:
--> qemu: 1 error(s) occurred:

* Post-processor failed: Unknown libvirt domain type: hvf

==> Builds finished but no artifacts were created.

A raw disk and a compressed disk can be found in the build-directory, but not resulting in what is to be expected.

Expected output / result (build on a Linux host with QEMU-kvm, libvirt and such):

==> qemu: Gracefully halting virtual machine...
==> qemu: Running post-processor: vagrant
==> qemu (vagrant): Creating Vagrant box for 'libvirt' provider
    qemu (vagrant): Copying from artifact: build/2018-11-03-11-45-31/base-stretch
    qemu (vagrant): Using custom Vagrantfile: source/debian/9_stretch/base.vagrant
    qemu (vagrant): Compressing: Vagrantfile
    qemu (vagrant): Compressing: box.img
    qemu (vagrant): Compressing: metadata.json
==> qemu: Running post-processor: compress
==> qemu (compress): Using pgzip compression with 2 cores for build/2018-11-03-11-45-31/base-stretch.raw.gz
==> qemu (compress): Archiving build/2018-11-03-11-45-31/base-stretch with pgzip
==> qemu (compress): Archive build/2018-11-03-11-45-31/base-stretch.raw.gz completed
==> qemu: Running post-processor: shell-local
==> qemu (shell-local): Running local shell script: /tmp/packer-shell067816088
==> Builds finished. The artifacts of successful builds are:

The output is free of errors and results in the creation of an (exportable) base-stretch-0.0.0-qemu.box.

Once again: is this result even possible on macOS and if yes, how? TIA!

Versions:

rickard-von-essen commented 6 years ago

From this it looks like the only drivers none, tcg, and kvm is currently supported. But if you know what driver hvf should use it's probably trivial to patch.

    switch domainType {
    case "none", "tcg":
        driver = "qemu"
    case "kvm":
        driver = domainType
    default:
        return "", nil, fmt.Errorf("Unknown libvirt domain type: %s", domainType)
    }

See libvirt.go#L41-L48

hbokh commented 6 years ago

Thanks, Rickard, you beat me to that! I was thinking of "just" adding hvf as an option to line https://github.com/hashicorp/packer/blob/master/post-processor/vagrant/libvirt.go#L42 but not sure about these drivers and the possible effect(s).

But without a patch like this, it is not possible -as of yet- to create a libvirt-box on macOS?

hbokh commented 6 years ago

This patch seems to work:

diff --git a/post-processor/vagrant/libvirt.go b/post-processor/vagrant/libvirt.go
index c20e8473b..60e274a57 100644
--- a/post-processor/vagrant/libvirt.go
+++ b/post-processor/vagrant/libvirt.go
@@ -39,7 +39,7 @@ func (p *LibVirtProvider) Process(ui packer.Ui, artifact packer.Artifact, dir st
        // Convert domain type to libvirt driver
        var driver string
        switch domainType {
-       case "none", "tcg":
+       case "none", "tcg", "hvf":
                driver = "qemu"
        case "kvm":
                driver = domainType

With the above change I recompiled Packer and ran it on macOS just as described in the first issue-post. Here is the trailing part of the buildlog as a gist.

The results in the build-dir:

-rw-r--r-- 1 hbokh 7864320000 Nov  4 08:51 base-stretch
-rw-r--r-- 1 hbokh  543173696 Nov  4 08:51 base-stretch-0.0.0-qemu.box
-rw-r--r-- 1 hbokh  543172961 Nov  4 08:52 base-stretch.raw.gz

And finally the contents of the box-file base-stretch-0.0.0-qemu.box:

-rw-r--r-- 1 hbokh        629 Nov  4 08:51 Vagrantfile
-rw-r--r-- 1 hbokh 7864320000 Nov  4 08:51 box.img
-rw-r--r-- 1 hbokh         55 Nov  4 08:51 metadata.json

So far this seems to solve my issue. I have to go through the contributing-doc to see how to commit this as a PR.

rickard-von-essen commented 6 years ago

Closed in #6955

ghost commented 4 years ago

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.