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.
@@ -65,13 +65,19 @@ EOF
65
65
  cat <<EOF > $rootfs/etc/hostname
66
66
  $hostname
67
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
68
+ # set minimal hosts
69
+ cat <<EOF > $rootfs/etc/hosts
70
+ 127.0.0.1 localhost
71
+ 127.0.1.1 $hostname
72
+
73
+ # The following lines are desirable for IPv6 capable hosts
74
+ ::1 ip6-localhost ip6-loopback
75
+ fe00::0 ip6-localnet
76
+ ff00::0 ip6-mcastprefix
77
+ ff02::1 ip6-allnodes
78
+ ff02::2 ip6-allrouters
79
+ ff02::3 ip6-allhosts
80
+ EOF
75
81
 
76
82
  # set default locale
77
83
  cat <<EOF > $rootfs/etc/locale.gen
@@ -117,8 +123,6 @@ extract_rootfs()
117
123
 
118
124
  echo "Extracting $tarball ..."
119
125
  mkdir -p $(dirname $rootfs)
120
- # Make sure the rootfs does not exist before extracting
121
- rm -rf $rootfs
122
126
  (cd `dirname $rootfs` && tar xfz $tarball)
123
127
  return 0
124
128
  }
@@ -0,0 +1,368 @@
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
+ configure_ubuntu()
37
+ {
38
+ rootfs=$1
39
+ release=$2
40
+ hostname=$2
41
+
42
+ # configure the network using the dhcp
43
+ cat <<EOF > $rootfs/etc/network/interfaces
44
+ # This file describes the network interfaces available on your system
45
+ # and how to activate them. For more information, see interfaces(5).
46
+
47
+ # The loopback network interface
48
+ auto lo
49
+ iface lo inet loopback
50
+
51
+ auto eth0
52
+ iface eth0 inet dhcp
53
+ EOF
54
+
55
+ # set the hostname
56
+ cat <<EOF > $rootfs/etc/hostname
57
+ $hostname
58
+ EOF
59
+ # set minimal hosts
60
+ cat <<EOF > $rootfs/etc/hosts
61
+ 127.0.0.1 localhost
62
+ 127.0.1.1 $hostname
63
+
64
+ # The following lines are desirable for IPv6 capable hosts
65
+ ::1 ip6-localhost ip6-loopback
66
+ fe00::0 ip6-localnet
67
+ ff00::0 ip6-mcastprefix
68
+ ff02::1 ip6-allnodes
69
+ ff02::2 ip6-allrouters
70
+ ff02::3 ip6-allhosts
71
+ EOF
72
+
73
+ if [ ! -f $rootfs/etc/init/container-detect.conf ]; then
74
+ # suppress log level output for udev
75
+ sed -i "s/=\"err\"/=0/" $rootfs/etc/udev/udev.conf
76
+
77
+ # remove jobs for consoles 5 and 6 since we only create 4 consoles in
78
+ # this template
79
+ rm -f $rootfs/etc/init/tty{5,6}.conf
80
+ fi
81
+
82
+ if ! (grep -q vagrant $rootfs/etc/passwd); then
83
+ chroot $rootfs useradd --create-home -s /bin/bash vagrant
84
+ echo "vagrant:vagrant" | chroot $rootfs chpasswd
85
+ fi
86
+
87
+ # make sure we have the current locale defined in the container
88
+ chroot $rootfs locale-gen en_US.UTF-8
89
+ chroot $rootfs update-locale LANG=en_US.UTF-8
90
+
91
+ return 0
92
+ }
93
+
94
+ # finish setting up the user in the container by injecting ssh key and
95
+ # adding sudo group membership.
96
+ # passed-in user is 'vagrant'
97
+ finalize_user()
98
+ {
99
+ user=$1
100
+
101
+ sudo_version=$(chroot $rootfs dpkg-query -W -f='${Version}' sudo)
102
+
103
+ if chroot $rootfs dpkg --compare-versions $sudo_version gt "1.8.3p1-1"; then
104
+ groups="sudo"
105
+ else
106
+ groups="sudo admin"
107
+ fi
108
+
109
+ for group in $groups; do
110
+ chroot $rootfs groupadd --system $group >/dev/null 2>&1 || true
111
+ chroot $rootfs adduser ${user} $group >/dev/null 2>&1 || true
112
+ done
113
+
114
+ chroot $rootfs cp /etc/sudoers /etc/sudoers.orig >/dev/null 2>&1 || true
115
+ 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
116
+
117
+ if [ -n "$auth_key" -a -f "$auth_key" ]; then
118
+ u_path="/home/${user}/.ssh"
119
+ root_u_path="$rootfs/$u_path"
120
+
121
+ mkdir -p $root_u_path
122
+ cp $auth_key "$root_u_path/authorized_keys"
123
+ chroot $rootfs chown -R ${user}: "$u_path"
124
+
125
+ echo "Inserted SSH public key from $auth_key into /home/${user}/.ssh/authorized_keys"
126
+ fi
127
+ return 0
128
+ }
129
+
130
+ write_sourceslist()
131
+ {
132
+ # $1 => path to the rootfs
133
+ # $2 => architecture we want to add
134
+ # $3 => whether to use the multi-arch syntax or not
135
+
136
+ case $2 in
137
+ amd64|i386)
138
+ MIRROR=${MIRROR:-http://archive.ubuntu.com/ubuntu}
139
+ SECURITY_MIRROR=${SECURITY_MIRROR:-http://security.ubuntu.com/ubuntu}
140
+ ;;
141
+ *)
142
+ MIRROR=${MIRROR:-http://ports.ubuntu.com/ubuntu-ports}
143
+ SECURITY_MIRROR=${SECURITY_MIRROR:-http://ports.ubuntu.com/ubuntu-ports}
144
+ ;;
145
+ esac
146
+ if [ -n "$3" ]; then
147
+ cat >> "$1/etc/apt/sources.list" << EOF
148
+ deb [arch=$2] $MIRROR ${release} main restricted universe multiverse
149
+ deb [arch=$2] $MIRROR ${release}-updates main restricted universe multiverse
150
+ deb [arch=$2] $SECURITY_MIRROR ${release}-security main restricted universe multiverse
151
+ EOF
152
+ else
153
+ cat >> "$1/etc/apt/sources.list" << EOF
154
+ deb $MIRROR ${release} main restricted universe multiverse
155
+ deb $MIRROR ${release}-updates main restricted universe multiverse
156
+ deb $SECURITY_MIRROR ${release}-security main restricted universe multiverse
157
+ EOF
158
+ fi
159
+ }
160
+
161
+ trim()
162
+ {
163
+ rootfs=$1
164
+ release=$2
165
+
166
+ # provide the lxc service
167
+ cat <<EOF > $rootfs/etc/init/lxc.conf
168
+ # fake some events needed for correct startup other services
169
+
170
+ description "Container Upstart"
171
+
172
+ start on startup
173
+
174
+ script
175
+ rm -rf /var/run/*.pid
176
+ rm -rf /var/run/network/*
177
+ /sbin/initctl emit stopped JOB=udevtrigger --no-wait
178
+ /sbin/initctl emit started JOB=udev --no-wait
179
+ end script
180
+ EOF
181
+
182
+ # fix buggus runlevel with sshd
183
+ cat <<EOF > $rootfs/etc/init/ssh.conf
184
+ # ssh - OpenBSD Secure Shell server
185
+ #
186
+ # The OpenSSH server provides secure shell access to the system.
187
+
188
+ description "OpenSSH server"
189
+
190
+ start on filesystem
191
+ stop on runlevel [!2345]
192
+
193
+ expect fork
194
+ respawn
195
+ respawn limit 10 5
196
+ umask 022
197
+ # replaces SSHD_OOM_ADJUST in /etc/default/ssh
198
+ oom never
199
+
200
+ pre-start script
201
+ test -x /usr/sbin/sshd || { stop; exit 0; }
202
+ test -e /etc/ssh/sshd_not_to_be_run && { stop; exit 0; }
203
+ test -c /dev/null || { stop; exit 0; }
204
+
205
+ mkdir -p -m0755 /var/run/sshd
206
+ end script
207
+
208
+ # if you used to set SSHD_OPTS in /etc/default/ssh, you can change the
209
+ # 'exec' line here instead
210
+ exec /usr/sbin/sshd
211
+ EOF
212
+
213
+ cat <<EOF > $rootfs/etc/init/console.conf
214
+ # console - getty
215
+ #
216
+ # This service maintains a console on tty1 from the point the system is
217
+ # started until it is shut down again.
218
+
219
+ start on stopped rc RUNLEVEL=[2345]
220
+ stop on runlevel [!2345]
221
+
222
+ respawn
223
+ exec /sbin/getty -8 38400 /dev/console
224
+ EOF
225
+
226
+ cat <<EOF > $rootfs/lib/init/fstab
227
+ # /lib/init/fstab: cleared out for bare-bones lxc
228
+ EOF
229
+
230
+ # remove pointless services in a container
231
+ chroot $rootfs /usr/sbin/update-rc.d -f ondemand remove
232
+
233
+ chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls u*.conf); do mv $f $f.orig; done'
234
+ chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls tty[2-9].conf); do mv $f $f.orig; done'
235
+ chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls plymouth*.conf); do mv $f $f.orig; done'
236
+ chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls hwclock*.conf); do mv $f $f.orig; done'
237
+ chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls module*.conf); do mv $f $f.orig; done'
238
+
239
+ # if this isn't lucid, then we need to twiddle the network upstart bits :(
240
+ if [ $release != "lucid" ]; then
241
+ sed -i 's/^.*emission handled.*$/echo Emitting lo/' $rootfs/etc/network/if-up.d/upstart
242
+ fi
243
+ }
244
+
245
+ post_process()
246
+ {
247
+ rootfs=$1
248
+ release=$2
249
+ trim_container=$3
250
+
251
+ if [ $trim_container -eq 1 ]; then
252
+ trim $rootfs $release
253
+ elif [ ! -f $rootfs/etc/init/container-detect.conf ]; then
254
+ # Make sure we have a working resolv.conf
255
+ cresolvonf="${rootfs}/etc/resolv.conf"
256
+ mv $cresolvonf ${cresolvonf}.lxcbak
257
+ cat /etc/resolv.conf > ${cresolvonf}
258
+
259
+ # for lucid, if not trimming, then add the ubuntu-virt
260
+ # ppa and install lxcguest
261
+ if [ $release = "lucid" ]; then
262
+ chroot $rootfs apt-get update
263
+ chroot $rootfs apt-get install --force-yes -y python-software-properties
264
+ chroot $rootfs add-apt-repository ppa:ubuntu-virt/ppa
265
+ fi
266
+
267
+ chroot $rootfs apt-get update
268
+ chroot $rootfs apt-get install --force-yes -y lxcguest
269
+
270
+ # Restore old resolv.conf
271
+ rm -f ${cresolvonf}
272
+ mv ${cresolvonf}.lxcbak ${cresolvonf}
273
+ fi
274
+
275
+ # If the container isn't running a native architecture, setup multiarch
276
+ if [ -x "$(ls -1 ${rootfs}/usr/bin/qemu-*-static 2>/dev/null)" ]; then
277
+ dpkg_version=$(chroot $rootfs dpkg-query -W -f='${Version}' dpkg)
278
+ if chroot $rootfs dpkg --compare-versions $dpkg_version ge "1.16.2"; then
279
+ chroot $rootfs dpkg --add-architecture ${hostarch}
280
+ else
281
+ mkdir -p ${rootfs}/etc/dpkg/dpkg.cfg.d
282
+ echo "foreign-architecture ${hostarch}" > ${rootfs}/etc/dpkg/dpkg.cfg.d/lxc-multiarch
283
+ fi
284
+
285
+ # Save existing value of MIRROR and SECURITY_MIRROR
286
+ DEFAULT_MIRROR=$MIRROR
287
+ DEFAULT_SECURITY_MIRROR=$SECURITY_MIRROR
288
+
289
+ # Write a new sources.list containing both native and multiarch entries
290
+ > ${rootfs}/etc/apt/sources.list
291
+ write_sourceslist $rootfs $arch "native"
292
+
293
+ MIRROR=$DEFAULT_MIRROR
294
+ SECURITY_MIRROR=$DEFAULT_SECURITY_MIRROR
295
+ write_sourceslist $rootfs $hostarch "multiarch"
296
+
297
+ # Finally update the lists and install upstart using the host architecture
298
+ chroot $rootfs apt-get update
299
+ chroot $rootfs apt-get install --force-yes -y --no-install-recommends upstart:${hostarch} mountall:${hostarch} iproute:${hostarch} isc-dhcp-client:${hostarch}
300
+ fi
301
+
302
+ # rmdir /dev/shm for containers that have /run/shm
303
+ # I'm afraid of doing rm -rf $rootfs/dev/shm, in case it did
304
+ # get bind mounted to the host's /run/shm. So try to rmdir
305
+ # it, and in case that fails move it out of the way.
306
+ if [ ! -L $rootfs/dev/shm ] && [ -d $rootfs/run/shm ] && [ -e $rootfs/dev/shm ]; then
307
+ mv $rootfs/dev/shm $rootfs/dev/shm.bak
308
+ ln -s /run/shm $rootfs/dev/shm
309
+ fi
310
+ }
311
+
312
+ release=precise # Default to the last Ubuntu LTS release for non-Ubuntu systems
313
+ if [ -f /etc/lsb-release ]; then
314
+ . /etc/lsb-release
315
+ if [ "$DISTRIB_ID" = "Ubuntu" ]; then
316
+ release=$DISTRIB_CODENAME
317
+ fi
318
+ fi
319
+
320
+ arch=$(uname -m)
321
+
322
+ # Code taken from debootstrap
323
+ if [ -x /usr/bin/dpkg ] && /usr/bin/dpkg --print-architecture >/dev/null 2>&1; then
324
+ arch=`/usr/bin/dpkg --print-architecture`
325
+ elif type udpkg >/dev/null 2>&1 && udpkg --print-architecture >/dev/null 2>&1; then
326
+ arch=`/usr/bin/udpkg --print-architecture`
327
+ else
328
+ arch=$(uname -m)
329
+ if [ "$arch" = "i686" ]; then
330
+ arch="i386"
331
+ elif [ "$arch" = "x86_64" ]; then
332
+ arch="amd64"
333
+ elif [ "$arch" = "armv7l" ]; then
334
+ arch="armel"
335
+ fi
336
+ fi
337
+
338
+
339
+ if [ "$(id -u)" != "0" ]; then
340
+ echo "This script should be run as 'root'"
341
+ exit 1
342
+ fi
343
+
344
+ declare cache=`readlink -f .` \
345
+ arch=$1 \
346
+ release=$2 \
347
+ auth_key=$3
348
+
349
+ # detect rootfs
350
+ cache=`readlink -f .`
351
+ rootfs="${cache}/rootfs"
352
+
353
+ configure_ubuntu $rootfs $release
354
+ if [ $? -ne 0 ]; then
355
+ echo "failed to configure ubuntu $release for a container"
356
+ exit 1
357
+ fi
358
+
359
+ post_process $rootfs $release $trim_container
360
+
361
+ finalize_user vagrant
362
+
363
+ echo ""
364
+ echo "##"
365
+ echo "# The default user is 'vagrant' with password 'vagrant'!"
366
+ echo "# Use the 'sudo' command to run tasks as root in the container."
367
+ echo "##"
368
+ echo ""
@@ -67,6 +67,7 @@ fe00::0 ip6-localnet
67
67
  ff00::0 ip6-mcastprefix
68
68
  ff02::1 ip6-allnodes
69
69
  ff02::2 ip6-allrouters
70
+ ff02::3 ip6-allhosts
70
71
  EOF
71
72
 
72
73
  if [ ! -f $rootfs/etc/init/container-detect.conf ]; then
@@ -165,8 +166,6 @@ extract_rootfs()
165
166
 
166
167
  echo "Extracting $tarball ..."
167
168
  mkdir -p $(dirname $rootfs)
168
- # Make sure the rootfs does not exist before extracting
169
- rm -rf $rootfs
170
169
  (cd `dirname $rootfs` && tar xfz $tarball)
171
170
  return 0
172
171
  }
@@ -3,7 +3,7 @@
3
3
 
4
4
  require 'pathname'
5
5
  BASE_URL = 'http://dl.dropbox.com/u/13510779'
6
- LAST_RELEASE_DATE = '2013-05-08'
6
+ LAST_RELEASE_DATE = '2013-07-12'
7
7
  LOCAL_BOXES_PATH = Pathname('../boxes/output').expand_path
8
8
  def lxc_box_url(release_name)
9
9
  file_name = "lxc-#{release_name}-amd64-#{LAST_RELEASE_DATE}.box"
@@ -29,10 +29,12 @@ BOXES = {
29
29
  },
30
30
  squeeze: {
31
31
  lxc_url: lxc_box_url('squeeze'),
32
- vbox_url: 'http://f.willianfernandes.com.br/vagrant-boxes/DebianSqueeze64.box'
32
+ # https://gist.github.com/henare/1964037
33
+ vbox_url: 'http://dl.dropbox.com/u/174733/debian-squeeze-64.box'
33
34
  },
34
35
  wheezy: {
35
36
  lxc_url: lxc_box_url('wheezy'),
37
+ vbox_url: 'http://puppet-vagrant-boxes.puppetlabs.com/debian-70rc1-x64-vbox4210.box'
36
38
  },
37
39
  sid: {
38
40
  lxc_url: lxc_box_url('sid'),
@@ -41,10 +43,15 @@ BOXES = {
41
43
 
42
44
  Vagrant.require_plugin 'vagrant-lxc'
43
45
  Vagrant.require_plugin 'vagrant-cachier'
46
+ Vagrant.require_plugin 'vagrant-pristine'
44
47
 
45
48
  Vagrant.configure("2") do |config|
46
49
  config.vm.synced_folder "../", "/vagrant", id: 'vagrant-root', nfs: true
47
50
 
51
+ config.cache.scope = :machine
52
+ config.cache.auto_detect = true
53
+ config.cache.enable_nfs = true
54
+
48
55
  ip_suffix = 30
49
56
  BOXES.each do |box_name, box_config|
50
57
  config.vm.define(box_name.to_sym) do |vm_config|
@@ -67,15 +74,12 @@ Vagrant.configure("2") do |config|
67
74
  if box_config[:lxc_url]
68
75
  vm_config.vm.provider :lxc do |lxc, lxc_config|
69
76
  lxc_config.vm.box_url = box_config[:lxc_url]
70
- lxc_config.vm.hostname = 'lxc-dev-box' unless %w(squeeze wheezy sid).include? box_name.to_s
77
+ lxc_config.vm.hostname = 'lxc-dev-box'
71
78
 
72
79
  # Required to boot nested containers
73
80
  lxc.customize 'aa_profile', 'unconfined' unless %w(squeeze wheezy sid).include? box_name.to_s
74
81
  end
75
82
  end
76
-
77
- vm_config.cache.enable :apt
78
- vm_config.cache.enable :gem
79
83
  end
80
84
  end
81
85