forked from openstack/devstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_lxc.sh
More file actions
executable file
·135 lines (112 loc) · 3.5 KB
/
build_lxc.sh
File metadata and controls
executable file
·135 lines (112 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env bash
# Configurable params
BRIDGE=${BRIDGE:-br0}
CONTAINER=${CONTAINER:-STACK}
CONTAINER_IP=${CONTAINER_IP:-192.168.1.50}
CONTAINER_CIDR=${CONTAINER_CIDR:-$CONTAINER_IP/24}
CONTAINER_NETMASK=${CONTAINER_NETMASK:-255.255.255.0}
CONTAINER_GATEWAY=${CONTAINER_GATEWAY:-192.168.1.1}
NAMESERVER=${NAMESERVER:-192.168.1.1}
COPYENV=${COPYENV:-1}
# Param string to pass to stack.sh. Like "EC2_DMZ_HOST=192.168.1.1 MYSQL_USER=nova"
STACKSH_PARAMS=${STACKSH_PARAMS:-}
# Install cgroup-bin if we don't have it yet
if ! which cgdelete | grep -q cgdelete; then
apt-get install cgroup-bin
fi
# Create lxc configuration
LXC_CONF=/tmp/$CONTAINER.conf
cat > $LXC_CONF <<EOF
lxc.network.type = veth
lxc.network.link = $BRIDGE
lxc.network.flags = up
lxc.network.ipv4 = $CONTAINER_CIDR
# allow tap/tun devices
lxc.cgroup.devices.allow = c 10:200 rwm
EOF
# Shutdown any existing container
lxc-stop -n $CONTAINER
# This kills zombie containers
if [ -d /cgroup/$CONTAINER ]; then
cgdelete -r cpu,net_cls:$CONTAINER
fi
# Warm the base image on first install
CACHEDIR=/var/cache/lxc/natty/rootfs-amd64
if [ ! -d $CACHEDIR ]; then
# trigger the initial debootstrap
lxc-create -n $CONTAINER -t natty -f $LXC_CONF
chroot $CACHEDIR apt-get update
chroot $CACHEDIR apt-get install -y `cat apts/* | cut -d\# -f1 | egrep -v "(rabbitmq|libvirt-bin|mysql-server)"`
chroot $CACHEDIR pip install `cat pips/*`
fi
# Destroy the old container
lxc-destroy -n $CONTAINER
# Create the container
lxc-create -n $CONTAINER -t natty -f $LXC_CONF
# Specify where our container rootfs lives
ROOTFS=/var/lib/lxc/$CONTAINER/rootfs/
# Create a stack user that is a member of the libvirtd group so that stack
# is able to interact with libvirt.
chroot $ROOTFS groupadd libvirtd
chroot $ROOTFS useradd stack -s /bin/bash -d /opt -G libvirtd
# a simple password - pass
echo stack:pass | chroot $ROOTFS chpasswd
# and has sudo ability (in the future this should be limited to only what
# stack requires)
echo "stack ALL=(ALL) NOPASSWD: ALL" >> $ROOTFS/etc/sudoers
function cp_it {
if [ -e $1 ] || [ -d $1 ]; then
cp -pr $1 $2
fi
}
# Copy over your ssh keys and env if desired
if [ "$COPYENV" = "1" ]; then
cp_it ~/.ssh $ROOTFS/opt/.ssh
cp_it ~/.ssh/id_rsa.pub $ROOTFS/opt/.ssh/authorized_keys
cp_it ~/.gitconfig $ROOTFS/opt/.gitconfig
cp_it ~/.vimrc $ROOTFS/opt/.vimrc
cp_it ~/.bashrc $ROOTFS/opt/.bashrc
fi
# Give stack ownership over /opt so it may do the work needed
chroot $ROOTFS chown -R stack /opt
# Configure instance network
INTERFACES=$ROOTFS/etc/network/interfaces
cat > $INTERFACES <<EOF
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet static
address $CONTAINER_IP
netmask $CONTAINER_NETMASK
gateway $CONTAINER_GATEWAY
EOF
# Configure the runner
RUN_SH=$ROOTFS/root/run.sh
cat > $RUN_SH <<EOF
#!/bin/bash
# Make sure dns is set up
echo "nameserver $NAMESERVER" | resolvconf -a eth0
sleep 1
# Install and run stack.sh
apt-get update
apt-get -y --force-yes install git-core vim-nox sudo
if [ ! -d "~/nfs-stack" ]
su -c "git clone git://github.com/cloudbuilders/nfs-stack.git ~/nfs-stack" stack
fi
su -c "cd ~/nfs-stack && $STACKSH_PARAMS ./stack.sh" stack
EOF
# Make the run.sh executable
chmod 700 $RUN_SH
# Make runner launch on boot
RC_LOCAL=$ROOTFS/etc/rc.local
cat > $RC_LOCAL <<EOF
#!/bin/sh -e
/root/run.sh
EOF
# Configure cgroup directory
if ! mount | grep -q cgroup; then
mkdir -p /cgroup
mount none -t cgroup /cgroup
fi
# Start our container
lxc-start -d -n $CONTAINER