From 37aeabbeff90d9d0a30e704f0f74e9bcac1ad298 Mon Sep 17 00:00:00 2001 From: Fabio Rehm Date: Wed, 18 Sep 2013 22:52:47 -0300 Subject: [PATCH] Add new bash script to build base ubuntu box --- boxes/build-ubuntu-box.sh | 86 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100755 boxes/build-ubuntu-box.sh diff --git a/boxes/build-ubuntu-box.sh b/boxes/build-ubuntu-box.sh new file mode 100755 index 0000000..1e3a90f --- /dev/null +++ b/boxes/build-ubuntu-box.sh @@ -0,0 +1,86 @@ +#!/bin/bash + +# Script used to build Ubuntu base vagrant-lxc containers +# USAGE: +# $ sudo ./build-ubuntu-box.sh UBUNTU_RELEASE BOX_ARCH + +# TODO: * Add support for flushing cache and specifying a custom base Ubuntu lxc +# template instead of system's built in +# * Embed vagrant public key + +################################################################################## +# 1 - Create the base container + +RELEASE=${1:-"raring"} +ARCH=${2:-"amd64"} + +lxc-create -n ${RELEASE}-base -t ubuntu -- --release ${RELEASE} --arch ${ARCH} + + +################################################################################## +# 2 - Prepare vagrant user + +ROOTFS=/var/lib/lxc/${RELEASE}-base/rootfs +chroot ${ROOTFS} usermod -l vagrant -d /home/vagrant ubuntu + +echo -n 'vagrant:vagrant' | chroot ${ROOTFS} chpasswd + + +################################################################################## +# 3 - Setup SSH access and passwordless sudo + +# Configure SSH access +mkdir -p ${ROOTFS}/home/vagrant/.ssh +wget https://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub -O ${ROOTFS}/home/vagrant/.ssh/authorized_keys +chroot ${ROOTFS} chown -R vagrant: /home/vagrant/.ssh + +# Enable passwordless sudo for users under the "sudo" group +cp ${ROOTFS}/etc/sudoers{,.orig} +sed -i -e \ + 's/%sudo\s\+ALL=(ALL\(:ALL\)\?)\s\+ALL/%sudo ALL=NOPASSWD:ALL/g' \ + ${ROOTFS}/etc/sudoers + + +################################################################################## +# 4 - Add some goodies + +PACKAGES=(vim curl wget manpages bash-completion) +chroot ${ROOTFS} apt-get install ${PACKAGES[*]} -y --force-yes + + +################################################################################## +# 5 - Configuration management tools + + +# TODO + + +################################################################################## +# 6 - Free up some disk space + +rm -rf ${ROOTFS}/tmp/* +chroot ${ROOTFS} apt-get clean + + +################################################################################## +# 7 - Build box package + +# Set up a working dir +mkdir -p /tmp/vagrant-lxc-${RELEASE} + +# Compress container's rootfs +cd /var/lib/lxc/${RELEASE}-base +tar --numeric-owner -czf /tmp/vagrant-lxc-${RELEASE}/rootfs.tar.gz ./rootfs/* + +# Prepare package contents +cd /tmp/vagrant-lxc-${RELEASE} +wget https://raw.github.com/fgrehm/vagrant-lxc/master/boxes/common/lxc-template +wget https://raw.github.com/fgrehm/vagrant-lxc/master/boxes/common/lxc.conf +wget https://raw.github.com/fgrehm/vagrant-lxc/master/boxes/common/metadata.json +chmod +x lxc-template + +# Vagrant box! +PKG=vagrant-lxc-${RELEASE}-${ARCH}.box +tar -czf $PKG ./* + +echo "The base box was built successfully to ${PKG}"