Skip to content

Instantly share code, notes, and snippets.

@fgrehm
Created July 19, 2013 01:25
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save fgrehm/6034400 to your computer and use it in GitHub Desktop.
Save fgrehm/6034400 to your computer and use it in GitHub Desktop.
#!/bin/bash
##################################################################################
# 1 - Create the base container
SUITE=squeeze
RELEASE=$SUITE
sudo lxc-create -n ${RELEASE}-base -t debian
rootfs="/var/lib/lxc/${RELEASE}-base/rootfs"
# This fixes some networking issues
# See https://github.com/fgrehm/vagrant-lxc/issues/91 for more info
sudo sed -i -e "s/\(127.0.0.1\s\+localhost\)/\1\n127.0.1.1\t${RELEASE}-base\n/g" ${rootfs}/etc/hosts
# This ensures that `/tmp` does not get cleared on halt
# See https://github.com/fgrehm/vagrant-lxc/issues/68 for more info
sudo chroot $rootfs /usr/sbin/update-rc.d -f checkroot-bootclean.sh remove
sudo chroot $rootfs /usr/sbin/update-rc.d -f mountall-bootclean.sh remove
sudo chroot $rootfs /usr/sbin/update-rc.d -f mountnfs-bootclean.sh remove
##################################################################################
# 2 - Prepare vagrant user
ROOTFS=/var/lib/lxc/${RELEASE}-base/rootfs
sudo chroot ${ROOTFS} useradd --create-home -s /bin/bash vagrant
echo -n 'vagrant:vagrant' | sudo chroot ${ROOTFS} chpasswd
##################################################################################
# 3 - Setup SSH access and passwordless sudo
sudo chroot ${ROOTFS} apt-get install sudo -y --force-yes
sudo chroot ${ROOTFS} adduser vagrant sudo
# Configure SSH access
sudo mkdir -p ${ROOTFS}/home/vagrant/.ssh
sudo wget https://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub -O ${ROOTFS}/home/vagrant/.ssh/authorized_keys
sudo chroot ${ROOTFS} chown -R vagrant: /home/vagrant/.ssh
# Enable passwordless sudo for users under the "sudo" group
sudo cp ${ROOTFS}/etc/sudoers{,.orig}
sudo sed -i -e \
's/%sudo\s\+ALL=(ALL\(:ALL\)\?)\s\+ALL/%sudo ALL=NOPASSWD:ALL/g' \
${ROOTFS}/etc/sudoers
##################################################################################
# 4 - Add some goodies
sudo chroot ${ROOTFS} apt-get install -y --force-yes ca-certificates
sudo chroot ${ROOTFS} apt-get update
# ADD YOUR STUFF HERE
##################################################################################
# 5 - Free up some disk space
sudo rm -rf ${ROOTFS}/tmp/*
sudo chroot ${ROOTFS} apt-get clean
##################################################################################
# 6 - Build box package
# Set up a working dir
mkdir -p /tmp/vagrant-lxc-${RELEASE}
# Compress container's rootfs
cd /var/lib/lxc/${RELEASE}-base
sudo tar --numeric-owner -czf /tmp/vagrant-lxc-${RELEASE}/rootfs.tar.gz ./rootfs/*
# Prepare package contents
cd /tmp/vagrant-lxc-${RELEASE}
sudo chown $USER:`id -gn` rootfs.tar.gz
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!
tar -czf vagrant-lxc-${RELEASE}.box ./*
#!/bin/bash
##################################################################################
# 1 - Create the base container
RELEASE=precise
ARCH=amd64
sudo lxc-create -n ${RELEASE}-base -t ubuntu -- --release ${RELEASE} --arch ${ARCH}
##################################################################################
# 2 - Prepare vagrant user
ROOTFS=/var/lib/lxc/${RELEASE}-base/rootfs
sudo chroot ${ROOTFS} usermod -l vagrant -d /home/vagrant ubuntu
echo -n 'vagrant:vagrant' | sudo chroot ${ROOTFS} chpasswd
##################################################################################
# 3 - Setup SSH access and passwordless sudo
# Configure SSH access
sudo mkdir -p ${ROOTFS}/home/vagrant/.ssh
sudo wget https://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub -O ${ROOTFS}/home/vagrant/.ssh/authorized_keys
sudo chroot ${ROOTFS} chown -R vagrant: /home/vagrant/.ssh
# Enable passwordless sudo for users under the "sudo" group
sudo cp ${ROOTFS}/etc/sudoers{,.orig}
sudo 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)
sudo chroot ${ROOTFS} apt-get install ${PACKAGES[*]} -y --force-yes
# ADD YOUR STUFF HERE
##################################################################################
# 5 - Free up some disk space
sudo rm -rf ${ROOTFS}/tmp/*
sudo chroot ${ROOTFS} apt-get clean
##################################################################################
# 6 - Build box package
# Set up a working dir
mkdir -p /tmp/vagrant-lxc-${RELEASE}
# Compress container's rootfs
cd /var/lib/lxc/${RELEASE}-base
sudo tar --numeric-owner -czf /tmp/vagrant-lxc-${RELEASE}/rootfs.tar.gz ./rootfs/*
# Prepare package contents
cd /tmp/vagrant-lxc-${RELEASE}
sudo chown $USER:`id -gn` rootfs.tar.gz
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!
tar -czf vagrant-lxc-${RELEASE}-${ARCH}.box ./*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment