Just moozing

Before you can check your notes, you must make them…

Pushing libvirt vagrant boxes to hashicorp

with one comment

So you have a vagrant machine running and you want to share it with other people. This entry is about how to make a box and share it on hashicorp.com.

Vagrant has build-in support for downloading boxes from Hashicorp. And Hashicorp has a nice interface, so we’ll go with that.

I am using Libvirt, and the box creation part is specific to that.


Creating box

In this example, the domain is base-ob-dev and the qcow file is called base-ob-dev.qcow2.


Locate base file

The “hdd” is located somewhere in a Libvirt storage pool.

$ qcowfile=$(sudo virsh dumpxml base-ob-dev | grep 'source file' | cut -d "'" -f 2)
$ echo $qcowfile


Convert it

We use the script from the Libvirt plugin, that I modified. My version is on Github as part of the vagrantify project.


$ cd libvirt-openbsd
$ sudo ../libvirt-box/create_box.sh $qcowfile base-ob.box

Normal users do not have access to image files, so extra privileges are needed.

Currently the only example is libvirt-openbsd. The are two data files in the directory

  • Vagranfile.base: base data for the default Vagrantfile to be used whenever a box is used.
  • metadata.json.base: base data for vagrant box metadata. Hashicorp will add more by themselves.

Now you have an base-ob.box file that can be added as a vagrant box.


Local usage only

You can install it in your ordinary vagrant system

$ vagrant box add base-ob.box --name base-ob

The box should now be installed and you can use it like so

$ cd <somewhere else>
$ vagrant init base-ob
$ vagrant up

This will not have any versioning built-in. You must add it yourself. See the docs for that.


Uploading to Hashicorp

You may do it all using their website, but I am always on headless servers, limited bandwidth and so on, so I prefer to do it from the command line.

First you need an API token. Create it here and save it somewhere safe.

To upload a new box, I followed the guide here. It required the boxes and the versions to be created first, before uploading. This I did using the webinterface from a laptop (not the headless virtualization host). It is done here.

Note: A quirk, which might be Libvirt related, is that you need to create a Libvirt provider for the version. And without choosing a file to upload, click update provider. Then you can get the download path needed.

To facilitate things, I used environment variables


Fill in with your values.

Get the path

curl "https://atlas.hashicorp.com/api/v1/box/$ATLASUSERNAME/$BOX_NAME/version/$VERSION/provider/$PROVIDER_NAME/upload?access_token=$ACCESSTOKEN"

If you get a “resource not found”, double check your values, and/or check to note above.

Store the path for later

ATLASPATH=$(curl "https://atlas.hashicorp.com/api/v1/box/$ATLASUSERNAME/$BOX_NAME/version/$VERSION/provider/$PROVIDER_NAME/upload?access_token=$ACCESSTOKEN" |cut -d "," -f1 | cut -d'"' -f4)

Upload the file. This is the part that takes a long time on a slow Internet connection.

curl -X PUT --upload-file $BOX_FILENAME $ATLASPATH

To verify that it worked, go to the list of boxes, and check the box for a new version. If applicable, change release the new version.


On the page it will also show how to use the box. It will say something like

vagrant init moozer/base-ob; vagrant up --provider libvirt

The first time moozer/base-ob is used, the box will be downloaded automatically. Later use, will not trigger redownload.

A cool feature of using Libvirt (with qcow images), is that the new virtual machine uses a snapshot of the initial storage file, so it initially uses almost no extra disk space.


Updating boxes locally

List all boxes and check for new versions

vagrant box outdated --global


And update it if applicable

vagrant box update --box moozer/base-ob

If you errors like

/home/moz/.vagrant.d/gems/gems/fog-libvirt-0.0.2/lib/fog/libvirt/requests/compute/list_volumes.rb:32:in `info’: Call to virStorageVolGetInfo failed: Storage volume not found: no storage vol with matching path ‘/var/lib/libvirt/images/base-ob57_vagrant_box_image_0.img’ (Libvirt::RetrieveError)

Try issuing a storage refresh. This will solve any differences between what Libvirt thinks is on disk and what is actually on disk

sudo virsh pool-refresh default

You might use a different pool than default.





Written by moozing

July 29, 2015 at 12:00

Posted in Tech

Tagged with , ,

One Response

Subscribe to comments with RSS.

  1. […] made a separate blog entry about […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: