Skip to content

Instantly share code, notes, and snippets.

@hiono
Last active January 2, 2016 04:49
Show Gist options
  • Save hiono/8253076 to your computer and use it in GitHub Desktop.
Save hiono/8253076 to your computer and use it in GitHub Desktop.
Shellscript to create the ubuntu-box of vagrant-lxc
#!/bin/bash
##################################################################################
# 1 - Prepare environments
RELEASE=trusty
ARCH=64 # OR 32
ADD_PACKAGES="vim,curl,wget,manpages,bash-completion,keychain,tmux,python-software-properties"
case ${ARCH} in
64) BOXNAME=${RELEASE}${ARCH}
ARCH=amd64 ;;
32) BOXNAME=${RELEASE}
ARCH=i386 ;;
*) echo wrong architecture.
exit 1 ;;
esac
ROOTFS=/var/lib/lxc/${BOXNAME}/rootfs
AUTHKEY=$(mktemp)
wget -c -q -O ${AUTHKEY} https://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub || exit 1
trap 'echo exit...;rm -f ${AUTHKEY}' EXIT
##################################################################################
# 2 - Create the base container
sudo lxc-create -n ${BOXNAME} -t ubuntu -- \
--release ${RELEASE} --arch ${ARCH} \
--user 'vagrant' --password 'vagrant' \
--auth-key ${AUTHKEY} \
--packages ${ADD_PACKAGES}
##################################################################################
# 3 - Setup passwordless sudo
# Enable passwordless sudo for the vagrant user
if [ -f ${ROOTFS}/etc/sudoers.d/vagrant ]; then
echo 'Skipping sudoers file creation.'
else
echo 'Sudoers file was not found'
echo "vagrant ALL=(ALL) NOPASSWD:ALL" | sudo tee ${ROOTFS}/etc/sudoers.d/vagrant
sudo chmod 0440 ${ROOTFS}/etc/sudoers.d/vagrant
echo 'Sudoers file created.'
fi
##################################################################################
# 4 - Add some goodies
# initialize environment
array (){ eval ${1:?}=\(\"\${@\:2}\"\); }
array INSTALL_PACKAGES # null
array REMOVE_PACKAGES # null
array PPA # null
# install git
# array PPA+ ppa:git-core/ppa
array INSTALL_PACKAGES+ git-core
# install emacs24
# array PPA+ ppa:cassou/emacs
array INSTALL_PACKAGES+ emacs24-nox
# install aspell
array INSTALL_PACKAGES+ aspell aspell-en
# oparate package installation
[ ${#REMOVE_PACKAGES[@]} > 0 ] \
&& sudo chroot ${ROOTFS} apt-get purge --force-yes -y ${REMOVE_PACKAGES[@]}
for (( I = 0; I < ${#PPA[@]}; ++I ))
do
sudo chroot ${ROOTFS} add-apt-repository ${PPA[$I]} -y
done
sudo chroot ${ROOTFS} apt-get update
[ ${#INSTALL_PACKAGES[@]} > 0 ] \
&& sudo chroot ${ROOTFS} apt-get install --force-yes -y --no-install-recommends ${INSTALL_PACKAGES[@]}
sudo chroot ${ROOTFS} apt-get autoremove
##################################################################################
# 5 - Free up some disk space
sudo chroot ${ROOTFS} rm -rf ${ROOTFS}/tmp/*
sudo chroot ${ROOTFS} apt-get clean
##################################################################################
# 6 - Build box package
# Set up a working dir
WDIR="./vagrant-lxc-${BOXNAME}-$(date +'%y-%m-%d')"
mkdir -p ${WDIR}
# "Compress container's rootfs"
sudo tar --numeric-owner -czf ${WDIR}/rootfs.tar.gz \
-C /var/lib/lxc/${BOXNAME} ./rootfs || exit 1
# Prepare package contents
pushd ${WDIR}
sudo chown $USER:`id -gn` rootfs.tar.gz
wget -c -q -O lxc-config https://raw.githubusercontent.com/fgrehm/vagrant-lxc-base-boxes/master/conf/ubuntu
wget -c -q -O metadata.json https://raw.githubusercontent.com/fgrehm/vagrant-lxc-base-boxes/master/conf/metadata.json
NOW=$(date -u)
sed -i "s/<TODAY>/${NOW}/" metadata.json
# Vagrant box!
tar -czf vagrant-lxc-${BOXNAME}.box ./lxc-config ./metadata.json ./rootfs.tar.gz && \
rm ./lxc-config ./metadata.json ./rootfs.tar.gz || exit 1
sudo lxc-destroy -n ${BOXNAME}
popd
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment