Commit 89ea5a933df2c56a554bd2ce13aae716f7f3b1d3
1 parent
13a29822
Exists in
master
and in
67 other branches
Add basic Vagrant infrastructure
Showing
7 changed files
with
331 additions
and
0 deletions
Show diff stats
.gitignore
@@ -0,0 +1,45 @@ | @@ -0,0 +1,45 @@ | ||
1 | +BOX = centos6 | ||
2 | +TEMPLATE_ARGS = -t centos -- --release 6.5 | ||
3 | +LXC_PATH := $(shell sudo lxc-config lxc.lxcpath) | ||
4 | + | ||
5 | +$(BOX).box: metadata.json lxc.conf lxc-template rootfs.tar.gz | ||
6 | + $(RM) $@ | ||
7 | + tar czf $@ $^ | ||
8 | + | ||
9 | +rootfs.tar.gz: rootfs | ||
10 | + $(RM) $@ | ||
11 | + sudo tar czfp $@ ./rootfs | ||
12 | + | ||
13 | +foo: .$(BOX)-base | ||
14 | + cp -dR --preserve=mode,ownership /etc/vim foo | ||
15 | + | ||
16 | +rootfs: .$(BOX)-base | ||
17 | + sudo cp -dR --preserve=mode,ownership $(LXC_PATH)/$(BOX)-base/rootfs rootfs | ||
18 | + sudo ./vagrant-setup rootfs | ||
19 | + | ||
20 | +lxc.conf: .$(BOX)-base | ||
21 | + sed -e '/^lxc.rootfs\s*=/d' $(LXC_PATH)/$(BOX)-base/config > $@ || ($(RM) $@; false) | ||
22 | + | ||
23 | +.$(BOX)-base: | ||
24 | + sudo lxc-create -n $(BOX)-base $(TEMPLATE_ARGS) | ||
25 | + touch $@ | ||
26 | + | ||
27 | +.PHONY: install uninstall clean | ||
28 | + | ||
29 | +metadata.json: metadata.json.in | ||
30 | + ./metadata.json.in > $@ | ||
31 | + | ||
32 | +uninstall: | ||
33 | + vagrant box remove $(BOX) | ||
34 | + | ||
35 | +install: $(BOX).box | ||
36 | + vagrant box add $(BOX) $^ | ||
37 | + | ||
38 | +clean: | ||
39 | + sudo $(RM) -r rootfs | ||
40 | + sudo $(RM) rootfs.tar.gz | ||
41 | + $(RM) .$(BOX)-base | ||
42 | + $(RM) $(BOX).box | ||
43 | + $(RM) lxc.conf | ||
44 | + $(RM) metadata.json | ||
45 | + sudo lxc-destroy -n $(BOX)-base |
@@ -0,0 +1,218 @@ | @@ -0,0 +1,218 @@ | ||
1 | +#!/bin/bash | ||
2 | + | ||
3 | +# This is a modified version of /usr/share/lxc/templates/lxc-ubuntu | ||
4 | +# that comes with Ubuntu 13.04 changed to suit vagrant-lxc needs | ||
5 | + | ||
6 | +# | ||
7 | +# template script for generating ubuntu container for LXC | ||
8 | +# | ||
9 | +# This script consolidates and extends the existing lxc ubuntu scripts | ||
10 | +# | ||
11 | + | ||
12 | +# Copyright © 2011 Serge Hallyn <serge.hallyn@canonical.com> | ||
13 | +# Copyright © 2010 Wilhelm Meier | ||
14 | +# Author: Wilhelm Meier <wilhelm.meier@fh-kl.de> | ||
15 | +# | ||
16 | +# This program is free software; you can redistribute it and/or modify | ||
17 | +# it under the terms of the GNU General Public License version 2, as | ||
18 | +# published by the Free Software Foundation. | ||
19 | + | ||
20 | +# This program is distributed in the hope that it will be useful, | ||
21 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
22 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
23 | +# GNU General Public License for more details. | ||
24 | + | ||
25 | +# You should have received a copy of the GNU General Public License along | ||
26 | +# with this program; if not, write to the Free Software Foundation, Inc., | ||
27 | +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
28 | +# | ||
29 | + | ||
30 | +set -e | ||
31 | + | ||
32 | +if [ -r /etc/default/lxc ]; then | ||
33 | + . /etc/default/lxc | ||
34 | +fi | ||
35 | + | ||
36 | +extract_rootfs() | ||
37 | +{ | ||
38 | + tarball=$1 | ||
39 | + arch=$2 | ||
40 | + rootfs=$3 | ||
41 | + | ||
42 | + echo "Extracting $tarball ..." | ||
43 | + mkdir -p $rootfs | ||
44 | + (cd $rootfs && tar xfz $tarball --strip-components=2) | ||
45 | + return 0 | ||
46 | +} | ||
47 | + | ||
48 | +install_rootfs() | ||
49 | +{ | ||
50 | + rootfs=$1 | ||
51 | + tarball=$3 | ||
52 | + mkdir -p /var/lock/subsys/ | ||
53 | + | ||
54 | + ( | ||
55 | + flock -x 200 | ||
56 | + if [ $? -ne 0 ]; then | ||
57 | + echo "Cache repository is busy." | ||
58 | + return 1 | ||
59 | + fi | ||
60 | + | ||
61 | + extract_rootfs $tarball $arch $rootfs | ||
62 | + if [ $? -ne 0 ]; then | ||
63 | + echo "Failed to copy rootfs" | ||
64 | + return 1 | ||
65 | + fi | ||
66 | + | ||
67 | + return 0 | ||
68 | + | ||
69 | + ) 200>/var/lock/subsys/lxc | ||
70 | + | ||
71 | + return $? | ||
72 | +} | ||
73 | + | ||
74 | +copy_configuration() | ||
75 | +{ | ||
76 | + path=$1 | ||
77 | + rootfs=$2 | ||
78 | + name=$3 | ||
79 | + | ||
80 | + grep -q "^lxc.rootfs" $path/config 2>/dev/null || echo "lxc.rootfs = $rootfs" >> $path/config | ||
81 | + | ||
82 | + # if there is exactly one veth network entry, make sure it has an | ||
83 | + # associated hwaddr. | ||
84 | + nics=`grep -e '^lxc\.network\.type[ \t]*=[ \t]*veth' $path/config | wc -l` | ||
85 | + if [ $nics -eq 1 ]; then | ||
86 | + 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 | ||
87 | + fi | ||
88 | + | ||
89 | + if [ $? -ne 0 ]; then | ||
90 | + echo "Failed to add configuration" | ||
91 | + return 1 | ||
92 | + fi | ||
93 | + | ||
94 | + return 0 | ||
95 | +} | ||
96 | + | ||
97 | +post_process() | ||
98 | +{ | ||
99 | + rootfs=$1 | ||
100 | + | ||
101 | + # rmdir /dev/shm for containers that have /run/shm | ||
102 | + # I'm afraid of doing rm -rf $rootfs/dev/shm, in case it did | ||
103 | + # get bind mounted to the host's /run/shm. So try to rmdir | ||
104 | + # it, and in case that fails move it out of the way. | ||
105 | + if [ ! -L $rootfs/dev/shm ] && [ -d $rootfs/run/shm ] && [ -e $rootfs/dev/shm ]; then | ||
106 | + mv $rootfs/dev/shm $rootfs/dev/shm.bak | ||
107 | + ln -s /run/shm $rootfs/dev/shm | ||
108 | + fi | ||
109 | +} | ||
110 | + | ||
111 | +usage() | ||
112 | +{ | ||
113 | + cat <<EOF | ||
114 | +$1 -h|--help [-a|--arch] [--trim] [-d|--debug] [--rootfs <rootfs>] [-T|--tarball <rootfs-tarball> | ||
115 | +arch: the container architecture (e.g. amd64): defaults to host arch | ||
116 | +EOF | ||
117 | + return 0 | ||
118 | +} | ||
119 | + | ||
120 | +options=$(getopt -o a:b:hp:r:xn:FS:d:C -l arch:,help,path:,release:,trim,name:,flush-cache,auth-key:,debug:,tarball:,rootfs: -- "$@") | ||
121 | +if [ $? -ne 0 ]; then | ||
122 | + usage $(basename $0) | ||
123 | + exit 1 | ||
124 | +fi | ||
125 | +eval set -- "$options" | ||
126 | + | ||
127 | + | ||
128 | +arch=$(uname -m) | ||
129 | + | ||
130 | +# Code taken from debootstrap | ||
131 | +if [ -x /usr/bin/dpkg ] && /usr/bin/dpkg --print-architecture >/dev/null 2>&1; then | ||
132 | + arch=`/usr/bin/dpkg --print-architecture` | ||
133 | +elif type udpkg >/dev/null 2>&1 && udpkg --print-architecture >/dev/null 2>&1; then | ||
134 | + arch=`/usr/bin/udpkg --print-architecture` | ||
135 | +else | ||
136 | + arch=$(uname -m) | ||
137 | + if [ "$arch" = "i686" ]; then | ||
138 | + arch="i386" | ||
139 | + elif [ "$arch" = "x86_64" ]; then | ||
140 | + arch="amd64" | ||
141 | + elif [ "$arch" = "armv7l" ]; then | ||
142 | + arch="armel" | ||
143 | + fi | ||
144 | +fi | ||
145 | + | ||
146 | +debug=0 | ||
147 | +trim_container=0 | ||
148 | +hostarch=$arch | ||
149 | +while true | ||
150 | +do | ||
151 | + case "$1" in | ||
152 | + -h|--help) usage $0 && exit 0;; | ||
153 | + --rootfs) rootfs=$2; shift 2;; | ||
154 | + -p|--path) path=$2; shift 2;; | ||
155 | + -n|--name) name=$2; shift 2;; | ||
156 | + -T|--tarball) tarball=$2; shift 2;; | ||
157 | + -a|--arch) arch=$2; shift 2;; | ||
158 | + -S|--auth-key) auth_key=$2; shift 2;; | ||
159 | + -d|--debug) debug=1; shift 1;; | ||
160 | + --) shift 1; break ;; | ||
161 | + *) break ;; | ||
162 | + esac | ||
163 | +done | ||
164 | + | ||
165 | +if [ $debug -eq 1 ]; then | ||
166 | + set -x | ||
167 | +fi | ||
168 | + | ||
169 | +if [ "$arch" == "i686" ]; then | ||
170 | + arch=i386 | ||
171 | +fi | ||
172 | + | ||
173 | +if [ $hostarch = "i386" -a $arch = "amd64" ]; then | ||
174 | + echo "can't create amd64 container on i386" | ||
175 | + exit 1 | ||
176 | +fi | ||
177 | + | ||
178 | +if [ -z "$path" ]; then | ||
179 | + echo "'path' parameter is required" | ||
180 | + exit 1 | ||
181 | +fi | ||
182 | + | ||
183 | +if [ "$(id -u)" != "0" ]; then | ||
184 | + echo "This script should be run as 'root'" | ||
185 | + exit 1 | ||
186 | +fi | ||
187 | + | ||
188 | +# detect rootfs | ||
189 | +config="$path/config" | ||
190 | +# if $rootfs exists here, it was passed in with --rootfs | ||
191 | +if [ -z "$rootfs" ]; then | ||
192 | + if grep -q '^lxc.rootfs' $config 2>/dev/null ; then | ||
193 | + rootfs=`grep 'lxc.rootfs =' $config | awk -F= '{ print $2 }'` | ||
194 | + else | ||
195 | + rootfs=$path/rootfs | ||
196 | + fi | ||
197 | +fi | ||
198 | + | ||
199 | +install_rootfs $rootfs $tarball | ||
200 | +if [ $? -ne 0 ]; then | ||
201 | + echo "failed to extract rootfs" | ||
202 | + exit 1 | ||
203 | +fi | ||
204 | + | ||
205 | +copy_configuration $path $rootfs $name $arch | ||
206 | +if [ $? -ne 0 ]; then | ||
207 | + echo "failed write configuration file" | ||
208 | + exit 1 | ||
209 | +fi | ||
210 | + | ||
211 | +post_process $rootfs $trim_container | ||
212 | + | ||
213 | +echo "" | ||
214 | +echo "##" | ||
215 | +echo "# The default user is 'vagrant' with password 'vagrant'!" | ||
216 | +echo "# Use the 'sudo' command to run tasks as root in the container." | ||
217 | +echo "##" | ||
218 | +echo "" |
@@ -0,0 +1,40 @@ | @@ -0,0 +1,40 @@ | ||
1 | +#!/bin/sh | ||
2 | + | ||
3 | +set -ex | ||
4 | + | ||
5 | +fs="$1" | ||
6 | +if [ -z "$fs" ]; then | ||
7 | + echo "usage: $0 ROOTFS" | ||
8 | + exit 1 | ||
9 | +fi | ||
10 | + | ||
11 | +chroot $fs useradd vagrant || true | ||
12 | +mkdir -p $fs/home/vagrant | ||
13 | +chroot $fs chown vagrant:vagrant /home/vagrant | ||
14 | + | ||
15 | +mkdir -p $fs/root/.ssh | ||
16 | +chmod 700 $fs/root/.ssh | ||
17 | +cat > $fs/root/.ssh/authorized_keys << EOF | ||
18 | +ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzIw+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoPkcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NOTd0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcWyLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQ== vagrant insecure public key | ||
19 | +EOF | ||
20 | +chmod 600 $fs/root/.ssh/authorized_keys | ||
21 | + | ||
22 | +mkdir -p $fs/home/vagrant/.ssh | ||
23 | +chmod 700 $fs/home/vagrant/.ssh | ||
24 | +cat > $fs/home/vagrant/.ssh/authorized_keys << EOF | ||
25 | +ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzIw+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoPkcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NOTd0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcWyLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQ== vagrant insecure public key | ||
26 | +EOF | ||
27 | +chmod 600 $fs/home/vagrant/.ssh/authorized_keys | ||
28 | +chroot $fs chown -R vagrant:vagrant /home/vagrant/.ssh | ||
29 | + | ||
30 | +# vagrant needs passwordless sudo | ||
31 | +if [ -x $fs/usr/bin/apt-get ]; then | ||
32 | + chroot $fs apt-get install -qy sudo | ||
33 | +fi | ||
34 | +if [ -x $fs/usr/bin/yum ]; then | ||
35 | + chroot $fs yum install -y sudo | ||
36 | +fi | ||
37 | +cat > $fs/etc/sudoers.d/vagrant <<EOF | ||
38 | +vagrant ALL=(ALL) NOPASSWD:ALL | ||
39 | +EOF | ||
40 | +chmod 0440 $fs/etc/sudoers.d/vagrant |