saucelabs/packer-builder-lxc

Name: packer-builder-lxc

Owner: Sauce Labs

Description: Packer builder for LXC containers

Forked from: JScott/packer-builder-lxc

Created: 2017-06-27 17:56:16.0

Updated: 2017-07-20 14:21:04.0

Pushed: 2017-07-25 14:24:08.0

Homepage:

Size: 89

Language: Go

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

packer-builder-lxc

Lxc builder for Packer, with working provisioning.

OS Support

As building platform only Linux is supported, at this point Ubuntu and Debian is tested. Please consult your lxc templates so you can pass the right parameters. All OSes should be buildable if they are supported by your lxc templates.

Debian

Wheezy is supported with lxc 1.0.0. An older version is shipped, with a broken lxc-attach command, so it's not supported. A backport deb can be created easily following this guide. Debian does not ship network support by default for lxc, you have to configure it manually. See the vagrant-lxc wiki for a detailed howto.

If you want to do anything during provisioning that needs network access, you'll need to edit /etc/lxc/default.conf to bring the network up on new containers:

network.type=veth
network.link=lxcbr0
network.flags=up

If your containers do not get an ip address from dhcp you need to turn off checksum offloading on the bridge:

n/ethtool -K lxcbr0 tx off

If you're done with all of these you are ready to build containers on wheezy!

Ubuntu

Everything above saucy should be supported (saucy is tested). The default configuration is good to go!

If your containers do not get an ip address from dhcp you need to turn off checksum offloading on the bridge:

n/ethtool -K lxcbr0 tx off

Building from source

Install golang-go: https://golang.org/doc/install#install

Building will require Go 1.7 or higher.

Install dependencies:

et github.com/mitchellh/gox
et github.com/mitchellh/go-fs
et github.com/mitchellh/multistep
et github.com/hashicorp/packer

Remove a few vendors from Packer's new structure that will break packer-builder-lxc:

rf $GOPATH/src/github.com/hashicorp/packer/vendor/github.com/mitchellh/multistep
rf $GOPATH/src/github.com/hashicorp/packer/vendor/github.com/mitchellh/mapstructure

After removing the vendor binaries included with packer, you can install this plugin:

et github.com/saucelabs/packer-builder-lxc

Go to the source directory, usually it is in $GOPATH/src/github.com/saucelabs/packer-builder-lxc

GOPATH/src/github.com/saucelabs/packer-builder-lxc

Build binary file with gox for desired platform:

-os=linux -arch=amd64 -output=pkg/{{.OS}}_{{.Arch}}/packer-builder-lxc

Copy output binary file to desired location, for example to /vagrant/packer:

kg/linux_amd64/packer-builder-lxc /vagrant/packer

Installation

Add the executable to your .packerconfig Core Config, if you use custom path like /vagrant/packer/packer-builder-lxc:


uilders": {
"lxc": "/vagrant/packer/packer-builder-lxc"


From now you should be able to use lxc in packer builders.

Example packer templates

Building wheezy on saucy:

uilders": [
{
  "type": "lxc",
  "config_file": "lxc/config",
  "lxc_template": {
    "name": "debian",
    "environment_vars": [
      "MIRROR=http://http.debian.net/debian/",
      "SUITE=wheezy"
    ]
  },
  "target_runlevel": 3
}


The config_file is an lxc config file which will be bundled with the machine. You can create your own or just grab the debian or ubuntu from vagrant-lxc-base-boxes.

Building wheezy on wheezy:

uilders": [
{
  "type": "lxc",
  "config_file": "lxc/config",
  "lxc_template": {
    "name": "debian",
    "parameters": ["--arch", "amd64", "--release", "wheezy"],
    "environment_vars": [
      "MIRROR=http://http.debian.net/debian/"
    ],
  },
  "target_runlevel": 3
}

rovisioners": [
{
  "type": "shell",
  "only": ["lxc"],
  "environment_vars": [
    "DISTRIBUTION=debian",
    "RELEASE=wheezy"
  ],
  "scripts": [
    "scripts/lxc/base.sh",
    "scripts/lxc/vagrant-lxc-fixes.sh"
  ]
}

ost-processors": [
{
  "type": "compress",
  "output": "output-vagrant/wheezy64-lxc.box"
}


Note the differences in template parameters/envvars!

Starting a build from an existing rootfs:

uilders": [
{
  "type": "lxc",
  "target_runlevel": 2,
  "container_name": "base",
  "config_file": "lxc.config",
  "init_timeout": "120s",
  "rootfs": {
    "archive": "/path/to/rootfs.tar.gz",
    "config": "/path/to/lxc.config"
  }
}


Export options:

Change the export tarball name and cherry pick folders you want to export


uilders": [
{
  "type": "lxc",
  "target_runlevel": 2,
  "container_name": "base",
  "config_file": "lxc.config",
  "init_timeout": "120s",
  "lxc_template": {
    "name": "ubuntu",
    "parameters": [
      "-d",
      "ubuntu",
      "-r",
      "trusty",
      "-a",
      "amd64"
    ]
  },
  "export_config" : {
    "filename": "androidsdk64.tar.gz",
    "folders": [
      { "src": "/tmp/android-sdk-linux", "dest": "/mytar/android-sdk-linux" },
      { "src": "/tmp/.android", "dest": "/mytar/tmp/.android" }
    ]
  }
}


Sidedisk :

Put host's tarball path in sidedisks, and it will be unarchived on running container


uilders": [
{
  "type": "lxc",
  "target_runlevel": 2,
  "container_name": "base",
  "config_file": "lxc.config",
  "init_timeout": "120s",
  "lxc_template": {
    "name": "ubuntu",
    "parameters": [
      "-d",
      "ubuntu",
      "-r",
      "trusty",
      "-a",
      "amd64"
    ]
  },
  "sidedisks": [
    {
      "archive": "/home/host_user/sdk.tar.gz",
      "dest": "/mnt/android/sdk"
    }
  ]
}


Vagrant publishing

The output artifact can be compressed with the compress publisher to create a working vagrant box (see example).


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.