vagrant-lxc 0.3.3 → 0.3.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,363 @@
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 the host in case it is not set so that sudo does not complain about the host
70
+ if ! (grep -q $hostname $rootfs/etc/hosts); then
71
+ chroot $rootfs sed -i -e \
72
+ "s/^127.0.0.1\(\s\+\)localhost$/127.0.0.1\1localhost\n127.0.0.1\1${hostname}/g" \
73
+ /etc/hosts >/dev/null 2>&1 || true
74
+ fi
75
+
76
+ # set default locale
77
+ cat <<EOF > $rootfs/etc/locale.gen
78
+ en_US.UTF-8 UTF-8
79
+ EOF
80
+ echo "default locale set to en_US.UTF-8 UTF-8"
81
+ chroot $rootfs locale-gen 'en_US.UTF-8' > /dev/null 2>&1
82
+ chroot $rootfs update-locale LANG='en_US.UTF-8'
83
+ echo 'update-locale done'
84
+
85
+ # remove pointless services in a container
86
+ chroot $rootfs /usr/sbin/update-rc.d -f checkroot.sh remove
87
+ chroot $rootfs /usr/sbin/update-rc.d -f umountfs remove
88
+ chroot $rootfs /usr/sbin/update-rc.d -f hwclock.sh remove
89
+ chroot $rootfs /usr/sbin/update-rc.d -f hwclockfirst.sh remove
90
+
91
+ echo "root:vagrant" | chroot $rootfs chpasswd
92
+
93
+ if ! (grep -q vagrant $rootfs/etc/passwd); then
94
+ chroot $rootfs useradd --create-home -s /bin/bash vagrant
95
+ echo "vagrant:vagrant" | chroot $rootfs chpasswd
96
+ chroot $rootfs adduser vagrant sudo >/dev/null 2>&1 || true
97
+ chroot $rootfs cp /etc/sudoers /etc/sudoers.orig >/dev/null 2>&1 || true
98
+ chroot $rootfs sed -i -e \
99
+ 's/%sudo\s\+ALL=(ALL\(:ALL\)\?)\s\+ALL/%sudo ALL=NOPASSWD:ALL/g' \
100
+ /etc/sudoers >/dev/null 2>&1 || true
101
+ fi
102
+
103
+ return 0
104
+ }
105
+
106
+ cleanup()
107
+ {
108
+ rm -rf ${cache}/partial
109
+ rm -rf ${cache}/rootfs
110
+ }
111
+
112
+ extract_rootfs()
113
+ {
114
+ tarball=$1
115
+ arch=$2
116
+ rootfs=$3
117
+
118
+ echo "Extracting $tarball ..."
119
+ mkdir -p $(dirname $rootfs)
120
+ # Make sure the rootfs does not exist before extracting
121
+ rm -rf $rootfs
122
+ (cd `dirname $rootfs` && tar xfz $tarball)
123
+ return 0
124
+ }
125
+
126
+ install_debian()
127
+ {
128
+ rootfs=$1
129
+ release=$2
130
+ tarball=$3
131
+ mkdir -p /var/lock/subsys/
132
+
133
+ (
134
+ flock -x 200
135
+ if [ $? -ne 0 ]; then
136
+ echo "Cache repository is busy."
137
+ return 1
138
+ fi
139
+
140
+ extract_rootfs $tarball $arch $rootfs
141
+ if [ $? -ne 0 ]; then
142
+ echo "Failed to copy rootfs"
143
+ return 1
144
+ fi
145
+
146
+ return 0
147
+
148
+ ) 200>/var/lock/subsys/lxc
149
+
150
+ return $?
151
+ }
152
+
153
+ copy_configuration()
154
+ {
155
+ path=$1
156
+ rootfs=$2
157
+ name=$3
158
+
159
+ # if there is exactly one veth network entry, make sure it has an
160
+ # associated hwaddr.
161
+ nics=`grep -e '^lxc\.network\.type[ \t]*=[ \t]*veth' $path/config | wc -l`
162
+ if [ $nics -eq 1 ]; then
163
+ grep -q "^lxc.network.hwaddr" $path/config || cat <<EOF >> $path/config
164
+ lxc.network.hwaddr = 00:16:3e:$(openssl rand -hex 3| sed 's/\(..\)/\1:/g; s/.$//')
165
+ EOF
166
+ fi
167
+
168
+ grep -q "^lxc.rootfs" $path/config 2>/dev/null || echo "lxc.rootfs = $rootfs" >> $path/config
169
+ cat <<EOF >> $path/config
170
+ lxc.tty = 4
171
+ lxc.pts = 1024
172
+ lxc.utsname = ${name}
173
+
174
+ # When using LXC with apparmor, uncomment the next line to run unconfined:
175
+ #lxc.aa_profile = unconfined
176
+
177
+ lxc.cgroup.devices.deny = a
178
+ # Allow any mknod (but not using the node)
179
+ lxc.cgroup.devices.allow = c *:* m
180
+ lxc.cgroup.devices.allow = b *:* m
181
+ # /dev/null and zero
182
+ lxc.cgroup.devices.allow = c 1:3 rwm
183
+ lxc.cgroup.devices.allow = c 1:5 rwm
184
+ # consoles
185
+ lxc.cgroup.devices.allow = c 5:1 rwm
186
+ lxc.cgroup.devices.allow = c 5:0 rwm
187
+ lxc.cgroup.devices.allow = c 4:0 rwm
188
+ lxc.cgroup.devices.allow = c 4:1 rwm
189
+ # /dev/{,u}random
190
+ lxc.cgroup.devices.allow = c 1:9 rwm
191
+ lxc.cgroup.devices.allow = c 1:8 rwm
192
+ lxc.cgroup.devices.allow = c 136:* rwm
193
+ lxc.cgroup.devices.allow = c 5:2 rwm
194
+ # rtc
195
+ lxc.cgroup.devices.allow = c 254:0 rwm
196
+ #fuse
197
+ lxc.cgroup.devices.allow = c 10:229 rwm
198
+ #tun
199
+ lxc.cgroup.devices.allow = c 10:200 rwm
200
+ #full
201
+ lxc.cgroup.devices.allow = c 1:7 rwm
202
+ #hpet
203
+ lxc.cgroup.devices.allow = c 10:228 rwm
204
+ #kvm
205
+ lxc.cgroup.devices.allow = c 10:232 rwm
206
+
207
+ # mounts point
208
+ lxc.mount.entry = proc proc proc nodev,noexec,nosuid 0 0
209
+ lxc.mount.entry = sysfs sys sysfs defaults 0 0
210
+ EOF
211
+
212
+ if [ $? -ne 0 ]; then
213
+ echo 'failed to add configuration'
214
+ return 1
215
+ fi
216
+
217
+ }
218
+
219
+
220
+ add_ssh_key()
221
+ {
222
+ user=$1
223
+
224
+ if [ -n "$auth_key" -a -f "$auth_key" ]; then
225
+ u_path="/home/${user}/.ssh"
226
+ root_u_path="$rootfs/$u_path"
227
+
228
+ mkdir -p $root_u_path
229
+ cp $auth_key "$root_u_path/authorized_keys"
230
+ chroot $rootfs chown -R ${user}: "$u_path"
231
+
232
+ echo "Inserted SSH public key from $auth_key into /home/${user}/.ssh/authorized_keys"
233
+ fi
234
+ }
235
+
236
+ disable_tmp_cleanup() {
237
+ rootfs=$1
238
+ chroot $rootfs /usr/sbin/update-rc.d -f checkroot-bootclean.sh remove
239
+ chroot $rootfs /usr/sbin/update-rc.d -f mountall-bootclean.sh remove
240
+ chroot $rootfs /usr/sbin/update-rc.d -f mountnfs-bootclean.sh remove
241
+ }
242
+
243
+ usage()
244
+ {
245
+ cat <<EOF
246
+ $1 -h|--help [-a|--arch] [-d|--debug]
247
+ [-F | --flush-cache] [-r|--release <release>] [ -S | --auth-key <keyfile>]
248
+ release: the debian release (e.g. wheezy): defaults to host release on debian, otherwise uses latest stable
249
+ arch: the container architecture (e.g. amd64): defaults to host arch
250
+ auth-key: SSH Public key file to inject into container
251
+ EOF
252
+ return 0
253
+ }
254
+
255
+ options=$(getopt -o a:b:hp:r:xn:Fd:C -l arch:,help,path:,release:,name:,flush-cache,auth-key:,debug:,tarball: -- "$@")
256
+ if [ $? -ne 0 ]; then
257
+ usage $(basename $0)
258
+ exit 1
259
+ fi
260
+ eval set -- "$options"
261
+
262
+ release=wheezy # Default to the last Debian stable release
263
+
264
+ arch=$(uname -m)
265
+
266
+ # Code taken from debootstrap
267
+ if [ -x /usr/bin/dpkg ] && /usr/bin/dpkg --print-architecture >/dev/null 2>&1; then
268
+ arch=`/usr/bin/dpkg --print-architecture`
269
+ elif type udpkg >/dev/null 2>&1 && udpkg --print-architecture >/dev/null 2>&1; then
270
+ arch=`/usr/bin/udpkg --print-architecture`
271
+ else
272
+ arch=$(uname -m)
273
+ if [ "$arch" = "i686" ]; then
274
+ arch="i386"
275
+ elif [ "$arch" = "x86_64" ]; then
276
+ arch="amd64"
277
+ elif [ "$arch" = "armv7l" ]; then
278
+ arch="armel"
279
+ fi
280
+ fi
281
+
282
+ debug=0
283
+ hostarch=$arch
284
+ while true
285
+ do
286
+ case "$1" in
287
+ -h|--help) usage $0 && exit 0;;
288
+ -p|--path) path=$2; shift 2;;
289
+ -n|--name) name=$2; shift 2;;
290
+ -T|--tarball) tarball=$2; shift 2;;
291
+ -r|--release) release=$2; shift 2;;
292
+ -S|--auth-key) auth_key=$2; shift 2;;
293
+ -a|--arch) arch=$2; shift 2;;
294
+ -d|--debug) debug=1; shift 1;;
295
+ --) shift 1; break ;;
296
+ *) break ;;
297
+ esac
298
+ done
299
+
300
+ if [ $debug -eq 1 ]; then
301
+ set -x
302
+ fi
303
+
304
+
305
+ if [ "$arch" == "i686" ]; then
306
+ arch=i386
307
+ fi
308
+
309
+ if [ $hostarch = "i386" -a $arch = "amd64" ]; then
310
+ echo "can't create amd64 container on i386"
311
+ exit 1
312
+ fi
313
+
314
+ if [ -z "$path" ]; then
315
+ echo "'path' parameter is required"
316
+ exit 1
317
+ fi
318
+
319
+ if [ "$(id -u)" != "0" ]; then
320
+ echo "This script should be run as 'root'"
321
+ exit 1
322
+ fi
323
+
324
+ # detect rootfs
325
+ config="$path/config"
326
+ if grep -q '^lxc.rootfs' $config 2>/dev/null ; then
327
+ rootfs=`grep 'lxc.rootfs =' $config | awk -F= '{ print $2 }'`
328
+ else
329
+ rootfs=$path/rootfs
330
+ fi
331
+
332
+ install_debian $rootfs $release $tarball
333
+ if [ $? -ne 0 ]; then
334
+ echo "failed to install debian $release"
335
+ exit 1
336
+ fi
337
+
338
+ configure_debian $rootfs $release
339
+ if [ $? -ne 0 ]; then
340
+ echo "failed to configure debian $release for a container"
341
+ exit 1
342
+ fi
343
+
344
+ copy_configuration $path $rootfs $name
345
+ if [ $? -ne 0 ]; then
346
+ echo "failed write configuration file"
347
+ exit 1
348
+ fi
349
+
350
+ add_ssh_key vagrant
351
+
352
+ # vagrant and / or plugins might mount some shared folders under /tmp by default
353
+ # (like puppet manifests) and we need to make sure no shared folder gets its
354
+ # contents removed because of it. For more information, please check:
355
+ # https://github.com/fgrehm/vagrant-lxc/issues/68
356
+ disable_tmp_cleanup $rootfs
357
+
358
+ echo ""
359
+ echo "##"
360
+ echo "# The default user is 'vagrant' with password 'vagrant'!"
361
+ echo "# Use the 'sudo' command to run tasks as root in the container."
362
+ echo "##"
363
+ echo ""
@@ -0,0 +1,9 @@
1
+ {
2
+ "provider": "lxc",
3
+ "version": "2",
4
+
5
+ "template-opts": {
6
+ "--arch": "ARCH",
7
+ "--release": "RELEASE"
8
+ }
9
+ }
@@ -35,6 +35,17 @@ EOF
35
35
  download_ubuntu()
36
36
  {
37
37
  packages=vim,ssh,curl,wget,bash-completion,manpages,man-db,psmisc
38
+
39
+ # Try to guess a list of langpacks to install
40
+ langpacks="language-pack-en"
41
+
42
+ if which dpkg >/dev/null 2>&1; then
43
+ langpacks=`(echo $langpacks &&
44
+ dpkg -l | grep -E "^ii language-pack-[a-z]* " |
45
+ cut -d ' ' -f3) | sort -u`
46
+ fi
47
+ packages="$packages,$(echo $langpacks | sed 's/ /,/g')"
48
+
38
49
  echo "installing packages: $packages"
39
50
 
40
51
  trap cleanup EXIT SIGHUP SIGINT SIGTERM
@@ -1,7 +1,7 @@
1
1
  #!/bin/bash
2
2
 
3
3
  # This is a modified version of /usr/share/lxc/templates/lxc-ubuntu
4
- # that comes with Ubuntu 12.10 changed to suit vagrant-lxc needs
4
+ # that comes with Ubuntu 13.04 changed to suit vagrant-lxc needs
5
5
 
6
6
  #
7
7
  # template script for generating ubuntu container for LXC
@@ -36,8 +36,8 @@ fi
36
36
  configure_ubuntu()
37
37
  {
38
38
  rootfs=$1
39
- release=$3
40
- hostname='quantal64'
39
+ release=$2
40
+ hostname=$2
41
41
 
42
42
  # configure the network using the dhcp
43
43
  cat <<EOF > $rootfs/etc/network/interfaces
@@ -83,6 +83,10 @@ EOF
83
83
  echo "vagrant:vagrant" | chroot $rootfs chpasswd
84
84
  fi
85
85
 
86
+ # make sure we have the current locale defined in the container
87
+ chroot $rootfs locale-gen en_US.UTF-8
88
+ chroot $rootfs update-locale LANG=en_US.UTF-8
89
+
86
90
  return 0
87
91
  }
88
92
 
@@ -108,8 +112,6 @@ finalize_user()
108
112
 
109
113
  chroot $rootfs cp /etc/sudoers /etc/sudoers.orig >/dev/null 2>&1 || true
110
114
  chroot $rootfs sed -i -e 's/%sudo\s\+ALL=(ALL:ALL)\s\+ALL/%sudo ALL=NOPASSWD:ALL/g' /etc/sudoers >/dev/null 2>&1 || true
111
- chroot $rootfs locale-gen en_US en_US.UTF-8 hu_HU hu_HU.UTF-8 >/dev/null 2>&1 || true
112
- chroot $rootfs dpkg-reconfigure locales >/dev/null 2>&1 || true
113
115
 
114
116
  if [ -n "$auth_key" -a -f "$auth_key" ]; then
115
117
  u_path="/home/${user}/.ssh"
@@ -202,7 +204,6 @@ copy_configuration()
202
204
  rootfs=$2
203
205
  name=$3
204
206
  arch=$4
205
- release=$5
206
207
 
207
208
  if [ $arch = "i386" ]; then
208
209
  arch="i686"
@@ -217,24 +218,23 @@ copy_configuration()
217
218
  # associated hwaddr.
218
219
  nics=`grep -e '^lxc\.network\.type[ \t]*=[ \t]*veth' $path/config | wc -l`
219
220
  if [ $nics -eq 1 ]; then
220
- grep -q "^lxc.network.hwaddr" $path/config || cat <<EOF >> $path/config
221
- lxc.network.hwaddr = 00:16:3e:$(openssl rand -hex 3| sed 's/\(..\)/\1:/g; s/.$//')
222
- EOF
221
+ 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
223
222
  fi
224
223
 
225
224
  grep -q "^lxc.rootfs" $path/config 2>/dev/null || echo "lxc.rootfs = $rootfs" >> $path/config
226
225
  cat <<EOF >> $path/config
227
- lxc.utsname = $name
226
+ lxc.mount = $path/fstab
227
+ lxc.pivotdir = lxc_putold
228
228
 
229
229
  lxc.devttydir =$ttydir
230
230
  lxc.tty = 4
231
231
  lxc.pts = 1024
232
- lxc.mount = $path/fstab
232
+
233
+ lxc.utsname = $name
233
234
  lxc.arch = $arch
234
235
  lxc.cap.drop = sys_module mac_admin mac_override
235
- lxc.pivotdir = lxc_putold
236
236
 
237
- # uncomment the next line to run the container unconfined:
237
+ # When using LXC with apparmor, uncomment the next line to run unconfined:
238
238
  #lxc.aa_profile = unconfined
239
239
 
240
240
  lxc.cgroup.devices.deny = a
@@ -350,15 +350,6 @@ EOF
350
350
  # /lib/init/fstab: cleared out for bare-bones lxc
351
351
  EOF
352
352
 
353
- # reconfigure some services
354
- if [ -z "$LANG" ]; then
355
- chroot $rootfs locale-gen en_US.UTF-8
356
- chroot $rootfs update-locale LANG=en_US.UTF-8
357
- else
358
- chroot $rootfs locale-gen $LANG
359
- chroot $rootfs update-locale LANG=$LANG
360
- fi
361
-
362
353
  # remove pointless services in a container
363
354
  chroot $rootfs /usr/sbin/update-rc.d -f ondemand remove
364
355
 
@@ -551,7 +542,7 @@ if [ $? -ne 0 ]; then
551
542
  exit 1
552
543
  fi
553
544
 
554
- copy_configuration $path $rootfs $name $arch $release
545
+ copy_configuration $path $rootfs $name $arch
555
546
  if [ $? -ne 0 ]; then
556
547
  echo "failed write configuration file"
557
548
  exit 1