Skip to content

Instantly share code, notes, and snippets.

@kaystrobach
Forked from fgrehm/01-setup.sh
Created July 31, 2013 15:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kaystrobach/faa8c22e7ac648f99250 to your computer and use it in GitHub Desktop.
Save kaystrobach/faa8c22e7ac648f99250 to your computer and use it in GitHub Desktop.
as i'm root, ~should be used instead of /home/vagrant :D
# Run it with '\curl -s https://raw.github.com/gist/0c7d283241ca13832047/01-setup.sh | bash'
if ! $(vagrant box list | grep 'quantal64' -q); then
vagrant box add quantal64 http://dl.dropbox.com/u/13510779/lxc-quantal-amd64-2013-07-12.box
fi
wget -q https://raw.github.com/gist/0c7d283241ca13832047/02-lxc-template.sh -O ~/.vagrant.d/boxes/quantal64/lxc/lxc-template
sed 's/$tarball/"\/home\/vagrant\/.vagrant.d\/boxes\/quantal64\/lxc\/rootfs.tar.gz"/' ~/.vagrant.d/boxes/quantal64/lxc/lxc-template -i
wget -q https://raw.github.com/gist/0c7d283241ca13832047/03-config -O ~/.vagrant.d/boxes/quantal64/lxc/lxc.conf
sed 's/\^state:/is/' ~/.vagrant.d/gems/gems/vagrant-lxc-0.4.0/lib/vagrant-lxc/driver/cli.rb -i.bkp
sed 's/run :shutdown/run :stop/' ~/.vagrant.d/gems/gems/vagrant-lxc-0.4.0/lib/vagrant-lxc/driver/cli.rb -i.bkp
sed 's/\(LEASES_PATHS = %w(\)/\1\n \/var\/lib\/misc\/dnsmasq.lxcbr0.leases/' ~/.vagrant.d/gems/gems/vagrant-lxc-0.4.0/lib/vagrant-lxc/driver/fetch_ip_from_dnsmasq.rb -i.bkp
#!/bin/bash
set -e
if [ -r /etc/default/lxc ]; then
. /etc/default/lxc
fi
extract_rootfs()
{
tarball=$1
rootfs=$2
echo "Extracting $tarball ..."
mkdir -p $(dirname $rootfs)
(cd `dirname $rootfs` && tar xfz $tarball)
return 0
}
copy_configuration()
{
path=$1
rootfs=$2
name=$3
grep -q "^lxc.rootfs" $path/config 2>/dev/null || echo -e "\nlxc.rootfs = $rootfs" >> $path/config
# if there is exactly one veth network entry, make sure it has an
# associated hwaddr.
nics=`grep -e '^lxc\.network\.type[ \t]*=[ \t]*veth' $path/config | wc -l`
if [ $nics -eq 1 ]; then
grep -q "^lxc.network.hwaddr" $path/config || sed -i -e "/^lxc\.network\.type[ \t]*=[ \t]*veth/a lxc.network.hwaddr = 00:16:3e:$(openssl rand -hex 3| sed 's/\(..\)/\1:/g; s/.$//')" $path/config
fi
if [ $? -ne 0 ]; then
echo "Failed to add configuration"
return 1
fi
return 0
}
arch=$(uname -m)
# Code taken from debootstrap
if [ -x /usr/bin/dpkg ] && /usr/bin/dpkg --print-architecture >/dev/null 2>&1; then
arch=`/usr/bin/dpkg --print-architecture`
elif type udpkg >/dev/null 2>&1 && udpkg --print-architecture >/dev/null 2>&1; then
arch=`/usr/bin/udpkg --print-architecture`
else
arch=$(uname -m)
if [ "$arch" = "i686" ]; then
arch="i386"
elif [ "$arch" = "x86_64" ]; then
arch="amd64"
elif [ "$arch" = "armv7l" ]; then
arch="armel"
fi
fi
options=$(getopt -o a:b:hp:r:xn:FS:d:C -l arch:,help,path:,release:,trim,name:,flush-cache,auth-key:,debug:,tarball: -- "$@")
if [ $? -ne 0 ]; then
usage $(basename $0)
exit 1
fi
eval set -- "$options"
debug=0
trim_container=0
hostarch=$arch
while true
do
case "$1" in
-p|--path) path=$2; shift 2;;
-n|--name) name=$2; shift 2;;
-a|--arch) arch=$2; shift 2;;
-d|--debug) debug=1; shift 1;;
--) shift 1; break ;;
*) break ;;
esac
done
if [ $debug -eq 1 ]; then
set -x
fi
if [ "$arch" == "i686" ]; then
arch=i386
fi
if [ $hostarch = "i386" -a $arch = "amd64" ]; then
echo "can't create amd64 container on i386"
exit 1
fi
if [ -z "$path" ]; then
echo "'path' parameter is required"
exit 1
fi
if [ "$(id -u)" != "0" ]; then
echo "This script should be run as 'root'"
exit 1
fi
# detect rootfs
config="$path/config"
if grep -q '^lxc.rootfs' $config 2>/dev/null ; then
rootfs=`grep 'lxc.rootfs =' $config | awk -F= '{ print $2 }'`
else
rootfs=$path/rootfs
fi
extract_rootfs $tarball $rootfs
if [ $? -ne 0 ]; then
echo "failed to extract rootfs tarball"
exit 1
fi
copy_configuration $path $rootfs $name $arch
if [ $? -ne 0 ]; then
echo "failed write configuration file"
exit 1
fi
lxc.network.type=veth
lxc.network.link=lxcbr0
lxc.network.flags=up
lxc.utsname = test
lxc.tty = 4
lxc.pts = 1024
lxc.cgroup.devices.deny = a
# /dev/null and zero
lxc.cgroup.devices.allow = c 1:3 rwm
lxc.cgroup.devices.allow = c 1:5 rwm
# consoles
lxc.cgroup.devices.allow = c 5:1 rwm
lxc.cgroup.devices.allow = c 5:0 rwm
lxc.cgroup.devices.allow = c 4:0 rwm
lxc.cgroup.devices.allow = c 4:1 rwm
# /dev/{,u}random
lxc.cgroup.devices.allow = c 1:9 rwm
lxc.cgroup.devices.allow = c 1:8 rwm
lxc.cgroup.devices.allow = c 136:* rwm
lxc.cgroup.devices.allow = c 5:2 rwm
# rtc
lxc.cgroup.devices.allow = c 254:0 rwm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment