Commit f01c7fef11926c5c8e738d7d137a080fd1bf0c3b

Authored by Antonio Terceiro
1 parent b33db222

Replace with template script copied from vagrant-lxc-base-boxes repo

Showing 1 changed file with 127 additions and 174 deletions   Show diff stats
utils/centos6-vagrant-lxc/lxc-template
1 #!/bin/bash 1 #!/bin/bash
2 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> 3 +# This is a modified version of /usr/share/lxc/templates/lxc-download
  4 +# that comes with ubuntu-lxc 1.0.0 stable from ppa changed to suit vagrant-lxc needs
15 # 5 #
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. 6 +# Copyright © 2014 Stéphane Graber <stgraber@ubuntu.com>
  7 +# Copyright © 2014 Fábio Rehm <fgrehm@gmail.com>
28 # 8 #
  9 +# This library is free software; you can redistribute it and/or
  10 +# modify it under the terms of the GNU Lesser General Public
  11 +# License as published by the Free Software Foundation; either
  12 +# version 2.1 of the License, or (at your option) any later version.
  13 +
  14 +# This library is distributed in the hope that it will be useful,
  15 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  16 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17 +# Lesser General Public License for more details.
  18 +
  19 +# You should have received a copy of the GNU Lesser General Public
  20 +# License along with this library; if not, write to the Free Software
  21 +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
  22 +# USA
  23 +
  24 +set -eu
  25 +
  26 +LXC_HOOK_DIR="/usr/share/lxc/hooks"
  27 +LXC_TEMPLATE_CONFIG="/usr/share/lxc/config"
  28 +
  29 +LXC_MAPPED_GID=
  30 +LXC_MAPPED_UID=
  31 +LXC_NAME=
  32 +LXC_PATH=
  33 +LXC_ROOTFS=
  34 +LXC_TARBALL=
  35 +LXC_CONFIG=
  36 +
  37 +usage() {
  38 + cat <<EOF
  39 +vagrant-lxc default template
29 40
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 41 +Required arguments:
  42 +[ --tarball <path> ]: The full path of the rootfs tarball
93 43
94 - return 0  
95 -} 44 +Optional arguments:
  45 +[ --config ]: Configuration file to be used when building the container
  46 +[ -h | --help ]: This help message
96 47
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 48 +LXC internal arguments (do not pass manually!):
  49 +[ --name <name> ]: The container name
  50 +[ --path <path> ]: The path to the container
  51 +[ --rootfs <rootfs> ]: The path to the container's rootfs
  52 +[ --mapped-uid <map> ]: A uid map (user namespaces)
  53 +[ --mapped-gid <map> ]: A gid map (user namespaces)
116 EOF 54 EOF
117 return 0 55 return 0
118 } 56 }
119 57
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: -- "$@") 58 +options=$(getopt -o h -l auth-key:,tarball:,config:,help:,name:,path:,rootfs:,mapped-uid:,mapped-gid: -- "$@")SS
  59 +
121 if [ $? -ne 0 ]; then 60 if [ $? -ne 0 ]; then
122 usage $(basename $0) 61 usage $(basename $0)
123 exit 1 62 exit 1
124 fi 63 fi
125 eval set -- "$options" 64 eval set -- "$options"
126 65
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 66 while true
150 do 67 do
151 case "$1" in 68 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 ;; 69 + -h|--help) usage $0 && exit 0;;
  70 + --config) LXC_CONFIG=$2; shift 2;;
  71 + --tarball) LXC_TARBALL=$2; shift 2;;
  72 + --name) LXC_NAME=$2; shift 2;;
  73 + --path) LXC_PATH=$2; shift 2;;
  74 + --rootfs) LXC_ROOTFS=$2; shift 2;;
  75 + --mapped-uid) LXC_MAPPED_UID=$2; shift 2;;
  76 + --mapped-gid) LXC_MAPPED_GID=$2; shift 2;;
  77 + *) break;;
162 esac 78 esac
163 done 79 done
164 80
165 -if [ $debug -eq 1 ]; then  
166 - set -x  
167 -fi  
168 -  
169 -if [ "$arch" == "i686" ]; then  
170 - arch=i386 81 +if [ -z "${LXC_NAME}" ]; then
  82 + echo "'name' parameter is required"
  83 + exit 1
171 fi 84 fi
172 85
173 -if [ $hostarch = "i386" -a $arch = "amd64" ]; then  
174 - echo "can't create amd64 container on i386" 86 +if [ -z "${LXC_TARBALL}" ]; then
  87 + echo "'tarball' parameter is required"
175 exit 1 88 exit 1
176 fi 89 fi
177 90
178 -if [ -z "$path" ]; then 91 +if [ -z "${LXC_PATH}" ]; then
179 echo "'path' parameter is required" 92 echo "'path' parameter is required"
180 exit 1 93 exit 1
181 fi 94 fi
182 95
183 -if [ "$(id -u)" != "0" ]; then  
184 - echo "This script should be run as 'root'" 96 +if [ -z "${LXC_CONFIG}" ]; then
  97 + echo "'config' parameter is required"
185 exit 1 98 exit 1
186 fi 99 fi
187 100
188 -# detect rootfs  
189 -config="$path/config"  
190 -# if $rootfs exists here, it was passed in with --rootfs  
191 -if [ -z "$rootfs" ]; then 101 +# if $LXC_ROOTFS exists here, it was passed in with --rootfs
  102 +if [ -z "${LXC_ROOTFS}" ]; then
  103 + config=${LXC_PATH}/config
192 if grep -q '^lxc.rootfs' $config 2>/dev/null ; then 104 if grep -q '^lxc.rootfs' $config 2>/dev/null ; then
193 - rootfs=`grep 'lxc.rootfs =' $config | awk -F= '{ print $2 }'` 105 + LXC_ROOTFS=`grep 'lxc.rootfs =' $config | awk -F= '{ print $2 }'`
194 else 106 else
195 - rootfs=$path/rootfs 107 + LXC_ROOTFS=$LXC_PATH/rootfs
  108 + echo "lxc.rootfs = ${LXC_ROOTFS}" >> $config
196 fi 109 fi
197 fi 110 fi
198 111
199 -install_rootfs $rootfs $tarball  
200 -if [ $? -ne 0 ]; then  
201 - echo "failed to extract rootfs"  
202 - exit 1  
203 -fi 112 +# Unpack the rootfs
  113 +echo "Unpacking the rootfs"
204 114
205 -copy_configuration $path $rootfs $name $arch  
206 -if [ $? -ne 0 ]; then  
207 - echo "failed write configuration file"  
208 - exit 1 115 +mkdir -p /var/lock/subsys
  116 +(
  117 + flock -x 200
  118 + if [ $? -ne 0 ]; then
  119 + echo "Cache repository is busy."
  120 + exit 1
  121 + fi
  122 +
  123 + mkdir -p ${LXC_ROOTFS}
  124 + (cd ${LXC_ROOTFS} && tar xfz ${LXC_TARBALL} --strip-components=2)
  125 + if [ $? -ne 0 ]; then
  126 + echo "Failed to extract rootfs"
  127 + exit 1
  128 + fi
  129 +
  130 +) 200>/var/lock/subsys/lxc
  131 +
  132 +mkdir -p ${LXC_ROOTFS}/dev/pts/
  133 +
  134 +## Extract all the network config entries
  135 +sed -i -e "/lxc.network/{w ${LXC_PATH}/config-network" -e "d}" \
  136 + ${LXC_PATH}/config
  137 +
  138 +## Extract any other config entry
  139 +sed -i -e "/lxc./{w ${LXC_PATH}/config-auto" -e "d}" ${LXC_PATH}/config
  140 +
  141 +## Add the container-specific config
  142 +echo "" >> ${LXC_PATH}/config
  143 +echo "##############################################" >> ${LXC_PATH}/config
  144 +echo "# Container specific configuration (automatically set)" >> ${LXC_PATH}/config
  145 +if [ -e "${LXC_PATH}/config-auto" ]; then
  146 + cat ${LXC_PATH}/config-auto >> ${LXC_PATH}/config
  147 + rm ${LXC_PATH}/config-auto
  148 +fi
  149 +echo "lxc.utsname = ${LXC_NAME}" >> ${LXC_PATH}/config
  150 +
  151 +## Re-add the previously removed network config
  152 +if [ -e "${LXC_PATH}/config-network" ]; then
  153 + echo "" >> ${LXC_PATH}/config
  154 + echo "##############################################" >> ${LXC_PATH}/config
  155 + echo "# Network configuration (automatically set)" >> ${LXC_PATH}/config
  156 + cat ${LXC_PATH}/config-network >> ${LXC_PATH}/config
  157 + rm ${LXC_PATH}/config-network
209 fi 158 fi
210 159
211 -post_process $rootfs $trim_container 160 +## Append the defaults
  161 +echo "" >> ${LXC_PATH}/config
  162 +echo "##############################################" >> ${LXC_PATH}/config
  163 +echo "# vagrant-lxc base box specific configuration" >> ${LXC_PATH}/config
  164 +cat ${LXC_CONFIG} >> ${LXC_PATH}/config
  165 +
  166 +# Empty section for lxc.customize calls from vagrantfile
  167 +echo "" >> ${LXC_PATH}/config
  168 +echo "##############################################" >> ${LXC_PATH}/config
  169 +echo "# vagrant-lxc container specific configuration" >> ${LXC_PATH}/config
212 170
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 "" 171 +exit 0