Skip to content

Instantly share code, notes, and snippets.

@kesselborn
Created August 26, 2011 09:58
Show Gist options
  • Save kesselborn/1173110 to your computer and use it in GitHub Desktop.
Save kesselborn/1173110 to your computer and use it in GitHub Desktop.
creates a simple lxc container and starts bash in it
#!/bin/bash
# creates a simple lxc container and starts bash in it
# ... steps from tutorial on:
# http://www.phenona.com/blog/using-lxc-linux-containers-in-amazon-ec2/
if [ "$(whoami)" != "root" ]
then
echo "script must be executed as root!"
exit 1
fi
echo "********* installing lxc"
apt-get update && apt-get install lxc debootstrap bridge-utils dnsmasq
echo "********* checking lxc installation"
if ! lxc-checkconfig
then
echo "error executing lxc-checkconfig"
return 2
fi
echo "********* mounting cgroup"
mkdir /cgroup
mount -t cgroup none /cgroup
echo "none /cgroup cgroup defaults 0 0" >> /etc/fstab
echo "********* creating network bridge for container"
brctl addbr br0
brctl setfd br0 0
ifconfig br0 192.168.3.1 up
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sysctl -w net.ipv4.ip_forward=1
mkdir /tmp/backups
test -f /tmp/backups/dnsmasq.conf || cp /etc/dnsmasq.conf /tmp/backups
cat <<EOF> /etc/dnsmasq.conf
domain-needed
bogus-priv
interface = br0
listen-address = 127.0.0.1
listen-address = 192.168.3.1
expand-hosts
domain = containers
dhcp-range = 192.168.3.50,192.168.3.200,1h
EOF
cat <<EOF> /etc/dhcp3/dhclient-enter-hooks.d/lxc
prepend domain-name-servers 127.0.0.1;
prepend domain-search "containers.";
EOF
apt-get install dhcp3-client
dhclient -e IF_METRIC=100 -pf /var/run/dhclient.eth0.pid -lf /var/lib/dhcp3/dhclient.eth0.leases eth0
/etc/init.d/unbound stop
service dnsmasq restart
echo "********* creating ubuntu container"
wget -O lxc-ubuntu http://xrl.us/ec2ubuntulxc
chmod +x lxc-ubuntu
./lxc-ubuntu -p /mnt/vm0 -n vm0
lxc-start -n vm0 /bin/bash
@yaronf
Copy link

yaronf commented Jan 7, 2014

Sadly, the URL on line 60 is broken.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment