vagrant-lxc 0.3.4 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,229 @@
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 $(dirname $rootfs)
44
+ (cd `dirname $rootfs` && tar xfz $tarball)
45
+ return 0
46
+ }
47
+
48
+ install_ubuntu()
49
+ {
50
+ rootfs=$1
51
+ release=$2
52
+ tarball=$3
53
+ mkdir -p /var/lock/subsys/
54
+
55
+ (
56
+ flock -x 200
57
+ if [ $? -ne 0 ]; then
58
+ echo "Cache repository is busy."
59
+ return 1
60
+ fi
61
+
62
+ extract_rootfs $tarball $arch $rootfs
63
+ if [ $? -ne 0 ]; then
64
+ echo "Failed to copy rootfs"
65
+ return 1
66
+ fi
67
+
68
+ return 0
69
+
70
+ ) 200>/var/lock/subsys/lxc
71
+
72
+ return $?
73
+ }
74
+
75
+ copy_configuration()
76
+ {
77
+ path=$1
78
+ rootfs=$2
79
+ name=$3
80
+
81
+ grep -q "^lxc.rootfs" $path/config 2>/dev/null || echo "lxc.rootfs = $rootfs" >> $path/config
82
+
83
+ # if there is exactly one veth network entry, make sure it has an
84
+ # associated hwaddr.
85
+ nics=`grep -e '^lxc\.network\.type[ \t]*=[ \t]*veth' $path/config | wc -l`
86
+ if [ $nics -eq 1 ]; then
87
+ 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
88
+ fi
89
+
90
+ if [ $? -ne 0 ]; then
91
+ echo "Failed to add configuration"
92
+ return 1
93
+ fi
94
+
95
+ return 0
96
+ }
97
+
98
+ post_process()
99
+ {
100
+ rootfs=$1
101
+
102
+ # rmdir /dev/shm for containers that have /run/shm
103
+ # I'm afraid of doing rm -rf $rootfs/dev/shm, in case it did
104
+ # get bind mounted to the host's /run/shm. So try to rmdir
105
+ # it, and in case that fails move it out of the way.
106
+ if [ ! -L $rootfs/dev/shm ] && [ -d $rootfs/run/shm ] && [ -e $rootfs/dev/shm ]; then
107
+ mv $rootfs/dev/shm $rootfs/dev/shm.bak
108
+ ln -s /run/shm $rootfs/dev/shm
109
+ fi
110
+ }
111
+
112
+ usage()
113
+ {
114
+ cat <<EOF
115
+ $1 -h|--help [-a|--arch] [--trim] [-d|--debug]
116
+ [-F | --flush-cache] [-r|--release <release>] [ -S | --auth-key <keyfile>]
117
+ release: the ubuntu release (e.g. precise): defaults to host release on ubuntu, otherwise uses latest LTS
118
+ trim: make a minimal (faster, but not upgrade-safe) container
119
+ arch: the container architecture (e.g. amd64): defaults to host arch
120
+ auth-key: SSH Public key file to inject into container
121
+ EOF
122
+ return 0
123
+ }
124
+
125
+ options=$(getopt -o a:b:hp:r:xn:FS:d:C -l arch:,help,path:,release:,trim,name:,flush-cache,auth-key:,debug:,tarball: -- "$@")
126
+ if [ $? -ne 0 ]; then
127
+ usage $(basename $0)
128
+ exit 1
129
+ fi
130
+ eval set -- "$options"
131
+
132
+ release=precise # Default to the last Ubuntu LTS release for non-Ubuntu systems
133
+ if [ -f /etc/lsb-release ]; then
134
+ . /etc/lsb-release
135
+ if [ "$DISTRIB_ID" = "Ubuntu" ]; then
136
+ release=$DISTRIB_CODENAME
137
+ fi
138
+ fi
139
+
140
+ arch=$(uname -m)
141
+
142
+ # Code taken from debootstrap
143
+ if [ -x /usr/bin/dpkg ] && /usr/bin/dpkg --print-architecture >/dev/null 2>&1; then
144
+ arch=`/usr/bin/dpkg --print-architecture`
145
+ elif type udpkg >/dev/null 2>&1 && udpkg --print-architecture >/dev/null 2>&1; then
146
+ arch=`/usr/bin/udpkg --print-architecture`
147
+ else
148
+ arch=$(uname -m)
149
+ if [ "$arch" = "i686" ]; then
150
+ arch="i386"
151
+ elif [ "$arch" = "x86_64" ]; then
152
+ arch="amd64"
153
+ elif [ "$arch" = "armv7l" ]; then
154
+ arch="armel"
155
+ fi
156
+ fi
157
+
158
+ debug=0
159
+ trim_container=0
160
+ hostarch=$arch
161
+ while true
162
+ do
163
+ case "$1" in
164
+ -h|--help) usage $0 && exit 0;;
165
+ -p|--path) path=$2; shift 2;;
166
+ -n|--name) name=$2; shift 2;;
167
+ -T|--tarball) tarball=$2; shift 2;;
168
+ -r|--release) release=$2; shift 2;;
169
+ -a|--arch) arch=$2; shift 2;;
170
+ -x|--trim) trim_container=1; shift 1;;
171
+ -S|--auth-key) auth_key=$2; shift 2;;
172
+ -d|--debug) debug=1; shift 1;;
173
+ --) shift 1; break ;;
174
+ *) break ;;
175
+ esac
176
+ done
177
+
178
+ if [ $debug -eq 1 ]; then
179
+ set -x
180
+ fi
181
+
182
+
183
+ if [ "$arch" == "i686" ]; then
184
+ arch=i386
185
+ fi
186
+
187
+ if [ $hostarch = "i386" -a $arch = "amd64" ]; then
188
+ echo "can't create amd64 container on i386"
189
+ exit 1
190
+ fi
191
+
192
+ if [ -z "$path" ]; then
193
+ echo "'path' parameter is required"
194
+ exit 1
195
+ fi
196
+
197
+ if [ "$(id -u)" != "0" ]; then
198
+ echo "This script should be run as 'root'"
199
+ exit 1
200
+ fi
201
+
202
+ # detect rootfs
203
+ config="$path/config"
204
+ if grep -q '^lxc.rootfs' $config 2>/dev/null ; then
205
+ rootfs=`grep 'lxc.rootfs =' $config | awk -F= '{ print $2 }'`
206
+ else
207
+ rootfs=$path/rootfs
208
+ fi
209
+
210
+ install_ubuntu $rootfs $release $tarball
211
+ if [ $? -ne 0 ]; then
212
+ echo "failed to install ubuntu $release"
213
+ exit 1
214
+ fi
215
+
216
+ copy_configuration $path $rootfs $name $arch
217
+ if [ $? -ne 0 ]; then
218
+ echo "failed write configuration file"
219
+ exit 1
220
+ fi
221
+
222
+ post_process $rootfs $release $trim_container
223
+
224
+ echo ""
225
+ echo "##"
226
+ echo "# The default user is 'vagrant' with password 'vagrant'!"
227
+ echo "# Use the 'sudo' command to run tasks as root in the container."
228
+ echo "##"
229
+ echo ""
@@ -0,0 +1,49 @@
1
+ lxc.network.type=veth
2
+ lxc.network.link=lxcbr0
3
+ lxc.network.flags=up
4
+
5
+ lxc.pivotdir = lxc_putold
6
+
7
+ lxc.devttydir = lxc
8
+ lxc.tty = 4
9
+ lxc.pts = 1024
10
+
11
+ lxc.arch = amd64
12
+ lxc.cap.drop = sys_module mac_admin mac_override
13
+
14
+ # When using LXC with apparmor, uncomment the next line to run unconfined:
15
+ #lxc.aa_profile = unconfined
16
+
17
+ lxc.cgroup.devices.deny = a
18
+ # Allow any mknod (but not using the node)
19
+ lxc.cgroup.devices.allow = c *:* m
20
+ lxc.cgroup.devices.allow = b *:* m
21
+ # /dev/null and zero
22
+ lxc.cgroup.devices.allow = c 1:3 rwm
23
+ lxc.cgroup.devices.allow = c 1:5 rwm
24
+ # consoles
25
+ lxc.cgroup.devices.allow = c 5:1 rwm
26
+ lxc.cgroup.devices.allow = c 5:0 rwm
27
+ #lxc.cgroup.devices.allow = c 4:0 rwm
28
+ #lxc.cgroup.devices.allow = c 4:1 rwm
29
+ # /dev/{,u}random
30
+ lxc.cgroup.devices.allow = c 1:9 rwm
31
+ lxc.cgroup.devices.allow = c 1:8 rwm
32
+ lxc.cgroup.devices.allow = c 136:* rwm
33
+ lxc.cgroup.devices.allow = c 5:2 rwm
34
+ # rtc
35
+ lxc.cgroup.devices.allow = c 254:0 rwm
36
+ #fuse
37
+ lxc.cgroup.devices.allow = c 10:229 rwm
38
+ #tun
39
+ lxc.cgroup.devices.allow = c 10:200 rwm
40
+ #full
41
+ lxc.cgroup.devices.allow = c 1:7 rwm
42
+ #hpet
43
+ lxc.cgroup.devices.allow = c 10:228 rwm
44
+ #kvm
45
+ lxc.cgroup.devices.allow = c 10:232 rwm
46
+
47
+ # mounts point
48
+ lxc.mount.entry = proc proc proc nodev,noexec,nosuid 0 0
49
+ lxc.mount.entry = sysfs sys sysfs defaults 0 0
@@ -0,0 +1,4 @@
1
+ {
2
+ "provider": "lxc",
3
+ "version": "3"
4
+ }
@@ -0,0 +1,195 @@
1
+ #!/bin/bash
2
+
3
+ # This is a modified version of /usr/share/lxc/templates/lxc-debian
4
+ # that comes with Ubuntu 13.04 changed to suit vagrant-lxc needs
5
+
6
+ set -e
7
+
8
+ if [ -r /etc/default/lxc ]; then
9
+ . /etc/default/lxc
10
+ fi
11
+
12
+ SUITE=${SUITE:-wheezy}
13
+ MIRROR=${MIRROR:-http://ftp.debian.org/debian}
14
+
15
+ configure_debian()
16
+ {
17
+ rootfs=$1
18
+ hostname=$2
19
+ release=$2
20
+
21
+ # squeeze only has /dev/tty and /dev/tty0 by default,
22
+ # therefore creating missing device nodes for tty1-4.
23
+ for tty in $(seq 1 4); do
24
+ if [ ! -e $rootfs/dev/tty$tty ]; then
25
+ mknod $rootfs/dev/tty$tty c 4 $tty
26
+ fi
27
+ done
28
+
29
+ # configure the inittab
30
+ cat <<EOF > $rootfs/etc/inittab
31
+ id:3:initdefault:
32
+ si::sysinit:/etc/init.d/rcS
33
+ l0:0:wait:/etc/init.d/rc 0
34
+ l1:1:wait:/etc/init.d/rc 1
35
+ l2:2:wait:/etc/init.d/rc 2
36
+ l3:3:wait:/etc/init.d/rc 3
37
+ l4:4:wait:/etc/init.d/rc 4
38
+ l5:5:wait:/etc/init.d/rc 5
39
+ l6:6:wait:/etc/init.d/rc 6
40
+ # Normally not reached, but fallthrough in case of emergency.
41
+ z6:6:respawn:/sbin/sulogin
42
+ 1:2345:respawn:/sbin/getty 38400 console
43
+ #c1:12345:respawn:/sbin/getty 38400 tty1 linux
44
+ c2:12345:respawn:/sbin/getty 38400 tty2 linux
45
+ c3:12345:respawn:/sbin/getty 38400 tty3 linux
46
+ c4:12345:respawn:/sbin/getty 38400 tty4 linux
47
+ p6::ctrlaltdel:/sbin/init 6
48
+ p0::powerfail:/sbin/init 0
49
+ EOF
50
+
51
+ # disable selinux in debian
52
+ mkdir -p $rootfs/selinux
53
+ echo 0 > $rootfs/selinux/enforce
54
+
55
+ # configure the network using the dhcp
56
+ cat <<EOF > $rootfs/etc/network/interfaces
57
+ auto lo
58
+ iface lo inet loopback
59
+
60
+ auto eth0
61
+ iface eth0 inet dhcp
62
+ EOF
63
+
64
+ # set the hostname
65
+ cat <<EOF > $rootfs/etc/hostname
66
+ $hostname
67
+ EOF
68
+
69
+ # set minimal hosts
70
+ cat <<EOF > $rootfs/etc/hosts
71
+ 127.0.0.1 localhost
72
+ 127.0.1.1 $hostname
73
+
74
+ # The following lines are desirable for IPv6 capable hosts
75
+ ::1 ip6-localhost ip6-loopback
76
+ fe00::0 ip6-localnet
77
+ ff00::0 ip6-mcastprefix
78
+ ff02::1 ip6-allnodes
79
+ ff02::2 ip6-allrouters
80
+ ff02::3 ip6-allhosts
81
+ EOF
82
+
83
+ # set default locale
84
+ cat <<EOF > $rootfs/etc/locale.gen
85
+ en_US.UTF-8 UTF-8
86
+ EOF
87
+ echo "default locale set to en_US.UTF-8 UTF-8"
88
+ chroot $rootfs locale-gen 'en_US.UTF-8' > /dev/null 2>&1
89
+ chroot $rootfs update-locale LANG='en_US.UTF-8'
90
+ echo 'update-locale done'
91
+
92
+ # remove pointless services in a container
93
+ chroot $rootfs /usr/sbin/update-rc.d -f checkroot.sh remove
94
+ chroot $rootfs /usr/sbin/update-rc.d -f umountfs remove
95
+ chroot $rootfs /usr/sbin/update-rc.d -f hwclock.sh remove
96
+ chroot $rootfs /usr/sbin/update-rc.d -f hwclockfirst.sh remove
97
+
98
+ echo "root:vagrant" | chroot $rootfs chpasswd
99
+
100
+ if ! (grep -q vagrant $rootfs/etc/passwd); then
101
+ chroot $rootfs useradd --create-home -s /bin/bash vagrant
102
+ echo "vagrant:vagrant" | chroot $rootfs chpasswd
103
+ chroot $rootfs adduser vagrant sudo >/dev/null 2>&1 || true
104
+ chroot $rootfs cp /etc/sudoers /etc/sudoers.orig >/dev/null 2>&1 || true
105
+ chroot $rootfs sed -i -e \
106
+ 's/%sudo\s\+ALL=(ALL\(:ALL\)\?)\s\+ALL/%sudo ALL=NOPASSWD:ALL/g' \
107
+ /etc/sudoers >/dev/null 2>&1 || true
108
+ fi
109
+
110
+ return 0
111
+ }
112
+
113
+ cleanup()
114
+ {
115
+ rm -rf ${cache}/partial
116
+ rm -rf ${cache}/rootfs
117
+ }
118
+
119
+ add_ssh_key()
120
+ {
121
+ user=$1
122
+
123
+ if [ -n "$auth_key" -a -f "$auth_key" ]; then
124
+ u_path="/home/${user}/.ssh"
125
+ root_u_path="$rootfs/$u_path"
126
+
127
+ mkdir -p $root_u_path
128
+ cp $auth_key "$root_u_path/authorized_keys"
129
+ chroot $rootfs chown -R ${user}: "$u_path"
130
+
131
+ echo "Inserted SSH public key from $auth_key into /home/${user}/.ssh/authorized_keys"
132
+ fi
133
+ }
134
+
135
+ disable_tmp_cleanup() {
136
+ rootfs=$1
137
+ chroot $rootfs /usr/sbin/update-rc.d -f checkroot-bootclean.sh remove
138
+ chroot $rootfs /usr/sbin/update-rc.d -f mountall-bootclean.sh remove
139
+ chroot $rootfs /usr/sbin/update-rc.d -f mountnfs-bootclean.sh remove
140
+ }
141
+
142
+ release=wheezy # Default to the last Debian stable release
143
+
144
+ arch=$(uname -m)
145
+
146
+ # Code taken from debootstrap
147
+ if [ -x /usr/bin/dpkg ] && /usr/bin/dpkg --print-architecture >/dev/null 2>&1; then
148
+ arch=`/usr/bin/dpkg --print-architecture`
149
+ elif type udpkg >/dev/null 2>&1 && udpkg --print-architecture >/dev/null 2>&1; then
150
+ arch=`/usr/bin/udpkg --print-architecture`
151
+ else
152
+ arch=$(uname -m)
153
+ if [ "$arch" = "i686" ]; then
154
+ arch="i386"
155
+ elif [ "$arch" = "x86_64" ]; then
156
+ arch="amd64"
157
+ elif [ "$arch" = "armv7l" ]; then
158
+ arch="armel"
159
+ fi
160
+ fi
161
+
162
+ if [ "$(id -u)" != "0" ]; then
163
+ echo "This script should be run as 'root'"
164
+ exit 1
165
+ fi
166
+
167
+ declare cache=`readlink -f .` \
168
+ arch=$1 \
169
+ release=$2 \
170
+ auth_key=$3
171
+
172
+ # detect rootfs
173
+ cache=`readlink -f .`
174
+ rootfs="${cache}/rootfs"
175
+
176
+ configure_debian $rootfs $release
177
+ if [ $? -ne 0 ]; then
178
+ echo "failed to configure debian $release for a container"
179
+ exit 1
180
+ fi
181
+
182
+ add_ssh_key vagrant
183
+
184
+ # vagrant and / or plugins might mount some shared folders under /tmp by default
185
+ # (like puppet manifests) and we need to make sure no shared folder gets its
186
+ # contents removed because of it. For more information, please check:
187
+ # https://github.com/fgrehm/vagrant-lxc/issues/68
188
+ disable_tmp_cleanup $rootfs
189
+
190
+ echo ""
191
+ echo "##"
192
+ echo "# The default user is 'vagrant' with password 'vagrant'!"
193
+ echo "# Use the 'sudo' command to run tasks as root in the container."
194
+ echo "##"
195
+ echo ""