]>
Commit | Line | Data |
---|---|---|
bad69158 | 1 | #!/bin/bash |
7afc269d | 2 | |
fb7460fe DL |
3 | # |
4 | # lxc: linux Container library | |
06388011 | 5 | |
fb7460fe DL |
6 | # Authors: |
7 | # Daniel Lezcano <daniel.lezcano@free.fr> | |
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 | |
14d9c0f0 | 16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
fb7460fe | 17 | # Lesser General Public License for more details. |
06388011 | 18 | |
fb7460fe DL |
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 | |
250b1eec | 21 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
06388011 | 22 | |
8ec981fc | 23 | # Detect use under userns (unsupported) |
c63c04fc | 24 | for arg in "$@"; do |
96283b54 SG |
25 | [ "$arg" = "--" ] && break |
26 | if [ "$arg" = "--mapped-uid" -o "$arg" = "--mapped-gid" ]; then | |
8ec981fc SG |
27 | echo "This template can't be used for unprivileged containers." 1>&2 |
28 | echo "You may want to try the \"download\" template instead." 1>&2 | |
29 | exit 1 | |
30 | fi | |
31 | done | |
32 | ||
207bf0e4 SG |
33 | # Make sure the usual locations are in PATH |
34 | export PATH=$PATH:/usr/sbin:/usr/bin:/sbin:/bin | |
35 | ||
5d20559c G |
36 | MIRROR=${MIRROR:-http://http.debian.net/debian} |
37 | SECURITY_MIRROR=${SECURITY_MIRROR:-http://security.debian.org/} | |
00fe5e1d SG |
38 | LOCALSTATEDIR="@LOCALSTATEDIR@" |
39 | LXC_TEMPLATE_CONFIG="@LXCTEMPLATECONFIG@" | |
2a7c16dc | 40 | |
fb7460fe DL |
41 | configure_debian() |
42 | { | |
43 | rootfs=$1 | |
44 | hostname=$2 | |
45 | ||
4e0eb765 DB |
46 | # squeeze only has /dev/tty and /dev/tty0 by default, |
47 | # therefore creating missing device nodes for tty1-4. | |
48 | for tty in $(seq 1 4); do | |
14d9c0f0 SG |
49 | if [ ! -e $rootfs/dev/tty$tty ]; then |
50 | mknod $rootfs/dev/tty$tty c 4 $tty | |
51 | fi | |
4e0eb765 DB |
52 | done |
53 | ||
fb7460fe DL |
54 | # configure the inittab |
55 | cat <<EOF > $rootfs/etc/inittab | |
06388011 | 56 | id:3:initdefault: |
57 | si::sysinit:/etc/init.d/rcS | |
58 | l0:0:wait:/etc/init.d/rc 0 | |
59 | l1:1:wait:/etc/init.d/rc 1 | |
60 | l2:2:wait:/etc/init.d/rc 2 | |
61 | l3:3:wait:/etc/init.d/rc 3 | |
62 | l4:4:wait:/etc/init.d/rc 4 | |
63 | l5:5:wait:/etc/init.d/rc 5 | |
64 | l6:6:wait:/etc/init.d/rc 6 | |
65 | # Normally not reached, but fallthrough in case of emergency. | |
66 | z6:6:respawn:/sbin/sulogin | |
67 | 1:2345:respawn:/sbin/getty 38400 console | |
b0a33c1e | 68 | c1:12345:respawn:/sbin/getty 38400 tty1 linux |
69 | c2:12345:respawn:/sbin/getty 38400 tty2 linux | |
70 | c3:12345:respawn:/sbin/getty 38400 tty3 linux | |
71 | c4:12345:respawn:/sbin/getty 38400 tty4 linux | |
f79d43bb | 72 | p6::ctrlaltdel:/sbin/init 6 |
6bf8daf9 | 73 | p0::powerfail:/sbin/init 0 |
06388011 | 74 | EOF |
06388011 | 75 | |
23e88083 HA |
76 | # symlink mtab |
77 | [ -e "$rootfs/etc/mtab" ] && rm $rootfs/etc/mtab | |
78 | ln -s /proc/self/mounts $rootfs/etc/mtab | |
79 | ||
fb7460fe DL |
80 | # disable selinux in debian |
81 | mkdir -p $rootfs/selinux | |
82 | echo 0 > $rootfs/selinux/enforce | |
06388011 | 83 | |
fb7460fe DL |
84 | # configure the network using the dhcp |
85 | cat <<EOF > $rootfs/etc/network/interfaces | |
86 | auto lo | |
87 | iface lo inet loopback | |
06388011 | 88 | |
fb7460fe DL |
89 | auto eth0 |
90 | iface eth0 inet dhcp | |
06388011 | 91 | EOF |
1846e71a | 92 | |
fb7460fe DL |
93 | # set the hostname |
94 | cat <<EOF > $rootfs/etc/hostname | |
95 | $hostname | |
96 | EOF | |
06388011 | 97 | |
fb7460fe | 98 | # reconfigure some services |
f1fa1a08 | 99 | if [ -z "$LANG" ]; then |
14d9c0f0 SG |
100 | chroot $rootfs locale-gen en_US.UTF-8 UTF-8 |
101 | chroot $rootfs update-locale LANG=en_US.UTF-8 | |
f1fa1a08 | 102 | else |
90ccc878 | 103 | encoding=$(echo $LANG | cut -d. -f2) |
09da66e6 | 104 | chroot $rootfs sed -e "s/^# \(${LANG} ${encoding}\)/\1/" \ |
cd44154c | 105 | -i /etc/locale.gen 2> /dev/null |
90ccc878 | 106 | chroot $rootfs locale-gen $LANG $encoding |
14d9c0f0 | 107 | chroot $rootfs update-locale LANG=$LANG |
f1fa1a08 | 108 | fi |
06388011 | 109 | |
fb7460fe | 110 | # remove pointless services in a container |
2b75b644 CN |
111 | chroot $rootfs /usr/sbin/update-rc.d -f checkroot.sh disable |
112 | chroot $rootfs /usr/sbin/update-rc.d -f umountfs disable | |
113 | chroot $rootfs /usr/sbin/update-rc.d -f hwclock.sh disable | |
114 | chroot $rootfs /usr/sbin/update-rc.d -f hwclockfirst.sh disable | |
19d618b1 | 115 | |
ce68d5b4 SG |
116 | # generate new SSH keys |
117 | if [ -x $rootfs/var/lib/dpkg/info/openssh-server.postinst ]; then | |
118 | cat > $rootfs/usr/sbin/policy-rc.d << EOF | |
119 | #!/bin/sh | |
120 | exit 101 | |
121 | EOF | |
122 | chmod +x $rootfs/usr/sbin/policy-rc.d | |
123 | ||
124 | if [ -f $rootfs/etc/init/ssh.conf ]; then | |
125 | mv $rootfs/etc/init/ssh.conf $rootfs/etc/init/ssh.conf.disabled | |
126 | fi | |
127 | ||
128 | rm -f $rootfs/etc/ssh/ssh_host_*key* | |
129 | ||
130 | DPKG_MAINTSCRIPT_PACKAGE=openssh DPKG_MAINTSCRIPT_NAME=postinst chroot $rootfs /var/lib/dpkg/info/openssh-server.postinst configure | |
131 | sed -i "s/root@$(hostname)/root@$hostname/g" $rootfs/etc/ssh/ssh_host_*.pub | |
132 | ||
133 | if [ -f "$rootfs/etc/init/ssh.conf.disabled" ]; then | |
134 | mv $rootfs/etc/init/ssh.conf.disabled $rootfs/etc/init/ssh.conf | |
135 | fi | |
136 | ||
137 | rm -f $rootfs/usr/sbin/policy-rc.d | |
138 | fi | |
139 | ||
f7365a24 | 140 | # set initial timezone as on host |
141 | if [ -f /etc/timezone ]; then | |
0a3673e8 | 142 | cat /etc/timezone > $rootfs/etc/timezone |
f7365a24 | 143 | chroot $rootfs dpkg-reconfigure -f noninteractive tzdata |
144 | elif [ -f /etc/sysconfig/clock ]; then | |
17abf278 | 145 | . /etc/sysconfig/clock |
f7365a24 | 146 | echo $ZONE > $rootfs/etc/timezone |
147 | chroot $rootfs dpkg-reconfigure -f noninteractive tzdata | |
148 | else | |
149 | echo "Timezone in container is not configured. Adjust it manually." | |
150 | fi | |
151 | ||
19d618b1 DL |
152 | echo "root:root" | chroot $rootfs chpasswd |
153 | echo "Root password is 'root', please change !" | |
154 | ||
155 | return 0 | |
06388011 | 156 | } |
157 | ||
177f2cd2 AD |
158 | write_sourceslist() |
159 | { | |
160 | local rootfs="$1"; shift | |
161 | local release="$1"; shift | |
162 | local arch="$1"; shift | |
163 | ||
164 | local prefix="deb" | |
165 | if [ -n "${arch}" ]; then | |
166 | prefix="deb [arch=${arch}]" | |
167 | fi | |
168 | ||
169 | cat >> "${rootfs}/etc/apt/sources.list" << EOF | |
5d20559c G |
170 | ${prefix} $MIRROR ${release} main contrib non-free |
171 | ${prefix} $SECURITY_MIRROR ${release}/updates main contrib non-free | |
177f2cd2 AD |
172 | EOF |
173 | } | |
174 | ||
175 | install_packages() | |
176 | { | |
177 | local rootfs="$1"; shift | |
178 | local packages="$*" | |
179 | ||
180 | chroot ${rootfs} apt-get update | |
181 | if [ -n "${packages}" ]; then | |
182 | chroot ${rootfs} apt-get install --force-yes -y --no-install-recommends ${packages} | |
183 | fi | |
184 | } | |
185 | ||
a9bf60ba AT |
186 | configure_debian_systemd() |
187 | { | |
188 | path=$1 | |
189 | rootfs=$2 | |
2b75b644 | 190 | |
2b75b644 CN |
191 | # this only works if we have getty@.service to manipulate |
192 | if [ -f ${rootfs}/lib/systemd/system/getty\@.service ]; then | |
193 | sed -e 's/^ConditionPathExists=/# ConditionPathExists=/' \ | |
194 | -e 's/After=dev-%i.device/After=/' \ | |
195 | < ${rootfs}/lib/systemd/system/getty\@.service \ | |
196 | > ${rootfs}/etc/systemd/system/getty\@.service | |
a9bf60ba AT |
197 | fi |
198 | ||
2b75b644 CN |
199 | # just in case systemd is not installed |
200 | mkdir -p ${rootfs}/{lib,etc}/systemd/system | |
201 | mkdir -p ${rootfs}/etc/systemd/system/getty.target.wants | |
a9bf60ba AT |
202 | |
203 | # This function has been copied and adapted from lxc-fedora | |
204 | rm -f ${rootfs}/etc/systemd/system/default.target | |
4de03d37 | 205 | chroot ${rootfs} ln -s /dev/null /etc/systemd/system/udev.service |
2b75b644 | 206 | chroot ${rootfs} ln -s /dev/null /etc/systemd/system/systemd-udevd.service |
a9bf60ba AT |
207 | chroot ${rootfs} ln -s /lib/systemd/system/multi-user.target /etc/systemd/system/default.target |
208 | # Make systemd honor SIGPWR | |
209 | chroot ${rootfs} ln -s /lib/systemd/system/halt.target /etc/systemd/system/sigpwr.target | |
a9bf60ba AT |
210 | # Setup getty service on the 4 ttys we are going to allow in the |
211 | # default config. Number should match lxc.tty | |
212 | ( cd ${rootfs}/etc/systemd/system/getty.target.wants | |
213 | for i in 1 2 3 4 ; do ln -sf ../getty\@.service getty@tty${i}.service; done ) | |
214 | ||
215 | return 0 | |
216 | } | |
217 | ||
f1ccde27 SH |
218 | cleanup() |
219 | { | |
b269b8ad LV |
220 | rm -rf $cache/partial-$release-$arch |
221 | rm -rf $cache/rootfs-$release-$arch | |
f1ccde27 SH |
222 | } |
223 | ||
fb7460fe DL |
224 | download_debian() |
225 | { | |
226 | packages=\ | |
227 | ifupdown,\ | |
228 | locales,\ | |
229 | libui-dialog-perl,\ | |
230 | dialog,\ | |
06a1e1db | 231 | isc-dhcp-client,\ |
fb7460fe DL |
232 | netbase,\ |
233 | net-tools,\ | |
234 | iproute,\ | |
235 | openssh-server | |
236 | ||
237 | cache=$1 | |
238 | arch=$2 | |
b269b8ad | 239 | release=$3 |
fb7460fe | 240 | |
f1ccde27 | 241 | trap cleanup EXIT SIGHUP SIGINT SIGTERM |
fb7460fe | 242 | # check the mini debian was not already downloaded |
b269b8ad | 243 | mkdir -p "$cache/partial-$release-$arch" |
fb7460fe | 244 | if [ $? -ne 0 ]; then |
b269b8ad | 245 | echo "Failed to create '$cache/partial-$release-$arch' directory" |
14d9c0f0 | 246 | return 1 |
81f6a40a RT |
247 | fi |
248 | ||
fb7460fe DL |
249 | # download a mini debian into a cache |
250 | echo "Downloading debian minimal ..." | |
251 | debootstrap --verbose --variant=minbase --arch=$arch \ | |
14d9c0f0 | 252 | --include=$packages \ |
b269b8ad | 253 | "$release" "$cache/partial-$release-$arch" $MIRROR |
fb7460fe | 254 | if [ $? -ne 0 ]; then |
14d9c0f0 SG |
255 | echo "Failed to download the rootfs, aborting." |
256 | return 1 | |
c952d1b9 | 257 | fi |
cd830f33 | 258 | |
b269b8ad | 259 | mv "$1/partial-$release-$arch" "$1/rootfs-$release-$arch" |
fb7460fe | 260 | echo "Download complete." |
f1ccde27 SH |
261 | trap EXIT |
262 | trap SIGINT | |
263 | trap SIGTERM | |
264 | trap SIGHUP | |
cd830f33 | 265 | |
fb7460fe | 266 | return 0 |
c952d1b9 | 267 | } |
bad69158 | 268 | |
fb7460fe | 269 | copy_debian() |
c952d1b9 | 270 | { |
fb7460fe DL |
271 | cache=$1 |
272 | arch=$2 | |
273 | rootfs=$3 | |
b269b8ad | 274 | release=$4 |
bad69158 | 275 | |
fb7460fe DL |
276 | # make a local copy of the minidebian |
277 | echo -n "Copying rootfs to $rootfs..." | |
6d8ac56b | 278 | mkdir -p $rootfs |
b269b8ad | 279 | rsync -Ha "$cache/rootfs-$release-$arch"/ $rootfs/ || return 1 |
fb7460fe | 280 | return 0 |
c952d1b9 | 281 | } |
282 | ||
fb7460fe DL |
283 | install_debian() |
284 | { | |
00fe5e1d | 285 | cache="$LOCALSTATEDIR/cache/lxc/debian" |
fb7460fe | 286 | rootfs=$1 |
b269b8ad | 287 | release=$2 |
1927a6be | 288 | arch=$3 |
00fe5e1d | 289 | mkdir -p $LOCALSTATEDIR/lock/subsys/ |
fb7460fe | 290 | ( |
17abf278 | 291 | flock -x 9 |
14d9c0f0 SG |
292 | if [ $? -ne 0 ]; then |
293 | echo "Cache repository is busy." | |
294 | return 1 | |
295 | fi | |
7afc269d | 296 | |
b269b8ad LV |
297 | echo "Checking cache download in $cache/rootfs-$release-$arch ... " |
298 | if [ ! -e "$cache/rootfs-$release-$arch" ]; then | |
299 | download_debian $cache $arch $release | |
14d9c0f0 SG |
300 | if [ $? -ne 0 ]; then |
301 | echo "Failed to download 'debian base'" | |
302 | return 1 | |
303 | fi | |
304 | fi | |
c952d1b9 | 305 | |
b269b8ad | 306 | copy_debian $cache $arch $rootfs $release |
14d9c0f0 SG |
307 | if [ $? -ne 0 ]; then |
308 | echo "Failed to copy rootfs" | |
309 | return 1 | |
310 | fi | |
c952d1b9 | 311 | |
14d9c0f0 | 312 | return 0 |
c952d1b9 | 313 | |
00fe5e1d | 314 | ) 9>$LOCALSTATEDIR/lock/subsys/lxc-debian |
85cbaa06 | 315 | |
fb7460fe | 316 | return $? |
bad69158 | 317 | } |
318 | ||
fb7460fe DL |
319 | copy_configuration() |
320 | { | |
321 | path=$1 | |
322 | rootfs=$2 | |
16501521 | 323 | hostname=$3 |
1927a6be | 324 | arch=$4 |
bad69158 | 325 | |
00fe5e1d | 326 | # Generate the configuration file |
aea1cd3c SG |
327 | # if there is exactly one veth network entry, make sure it has an |
328 | # associated hwaddr. | |
329 | nics=`grep -e '^lxc\.network\.type[ \t]*=[ \t]*veth' $path/config | wc -l` | |
330 | if [ $nics -eq 1 ]; then | |
331 | 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 | |
332 | fi | |
333 | ||
00fe5e1d SG |
334 | ## Add all the includes |
335 | echo "" >> $path/config | |
336 | echo "# Common configuration" >> $path/config | |
337 | if [ -e "${LXC_TEMPLATE_CONFIG}/debian.common.conf" ]; then | |
338 | echo "lxc.include = ${LXC_TEMPLATE_CONFIG}/debian.common.conf" >> $path/config | |
339 | fi | |
340 | if [ -e "${LXC_TEMPLATE_CONFIG}/debian.${release}.conf" ]; then | |
341 | echo "lxc.include = ${LXC_TEMPLATE_CONFIG}/debian.${release}.conf" >> $path/config | |
342 | fi | |
343 | ||
344 | ## Add the container-specific config | |
345 | echo "" >> $path/config | |
346 | echo "# Container specific configuration" >> $path/config | |
cd44154c | 347 | grep -q "^lxc.rootfs" $path/config 2> /dev/null || echo "lxc.rootfs = $rootfs" >> $path/config |
00fe5e1d | 348 | |
fb7460fe | 349 | cat <<EOF >> $path/config |
16501521 | 350 | lxc.utsname = $hostname |
00fe5e1d | 351 | lxc.arch = $arch |
fb7460fe | 352 | EOF |
bad69158 | 353 | |
fb7460fe | 354 | if [ $? -ne 0 ]; then |
14d9c0f0 SG |
355 | echo "Failed to add configuration" |
356 | return 1 | |
bad69158 | 357 | fi |
358 | ||
359 | return 0 | |
360 | } | |
361 | ||
177f2cd2 AD |
362 | post_process() |
363 | { | |
364 | local rootfs="$1"; shift | |
365 | local release="$1"; shift | |
366 | local arch="$1"; shift | |
367 | local hostarch="$1"; shift | |
368 | local packages="$*" | |
369 | ||
370 | # Disable service startup | |
371 | cat > ${rootfs}/usr/sbin/policy-rc.d << EOF | |
372 | #!/bin/sh | |
373 | exit 101 | |
374 | EOF | |
375 | chmod +x ${rootfs}/usr/sbin/policy-rc.d | |
376 | ||
377 | # If the container isn't running a native architecture, setup multiarch | |
378 | if [ "${arch}" != "${hostarch}" ]; then | |
379 | mkdir -p ${rootfs}/etc/dpkg/dpkg.cfg.d | |
380 | echo "foreign-architecture ${hostarch}" > ${rootfs}/etc/dpkg/dpkg.cfg.d/lxc-multiarch | |
381 | fi | |
382 | ||
383 | # Write a new sources.list containing both native and multiarch entries | |
384 | > ${rootfs}/etc/apt/sources.list | |
385 | if [ "${arch}" = "${hostarch}" ]; then | |
386 | write_sourceslist ${rootfs} ${release} ${arch} | |
387 | else | |
388 | write_sourceslist ${rootfs} ${release} | |
389 | fi | |
390 | ||
391 | # Install Packages in container | |
392 | if [ -n "${packages}" ]; then | |
393 | local pack_list="`echo ${packages} | sed 's/,/ /g'`" | |
394 | echo "Installing packages: ${pack_list}" | |
395 | install_packages ${rootfs} ${pack_list} | |
396 | fi | |
397 | ||
398 | # Re-enable service startup | |
399 | rm ${rootfs}/usr/sbin/policy-rc.d | |
400 | } | |
401 | ||
fb7460fe DL |
402 | clean() |
403 | { | |
00fe5e1d | 404 | cache="$LOCALSTATEDIR/cache/lxc/debian" |
bad69158 | 405 | |
fb7460fe | 406 | if [ ! -e $cache ]; then |
14d9c0f0 | 407 | exit 0 |
bad69158 | 408 | fi |
409 | ||
410 | # lock, so we won't purge while someone is creating a repository | |
411 | ( | |
17abf278 | 412 | flock -x 9 |
14d9c0f0 SG |
413 | if [ $? != 0 ]; then |
414 | echo "Cache repository is busy." | |
415 | exit 1 | |
416 | fi | |
bad69158 | 417 | |
14d9c0f0 SG |
418 | echo -n "Purging the download cache..." |
419 | rm --preserve-root --one-file-system -rf $cache && echo "Done." || exit 1 | |
420 | exit 0 | |
bad69158 | 421 | |
00fe5e1d | 422 | ) 9>$LOCALSTATEDIR/lock/subsys/lxc-debian |
bad69158 | 423 | } |
424 | ||
fb7460fe DL |
425 | usage() |
426 | { | |
427 | cat <<EOF | |
9cbffb9f TP |
428 | Template specific options can be passed to lxc-create after a '--' like this: |
429 | ||
430 | lxc-create --name=NAME [-lxc-create-options] -- [-template-options] | |
431 | ||
432 | Usage: $1 -h|--help -p|--path=<path> [-c|--clean] [-a|--arch=<arch>] [-r|--release=<release>] | |
433 | [--mirror=<mirror>] [--security-mirror=<security mirror>] | |
434 | [--package=<package_name1,package_name2,...>] | |
435 | ||
436 | Options : | |
437 | ||
438 | -h, --help print this help text | |
439 | -p, --path=PATH directory where config and rootfs of this VM will be kept | |
440 | -a, --arch=ARCH The container architecture. Can be one of: i686, x86_64, | |
441 | amd64, armhf, armel, powerpc. Defaults to host arch. | |
442 | -r, --release=RELEASE Debian release. Can be one of: squeeze, wheezy, jessie, sid. | |
443 | Defaults to current stable. | |
444 | --mirror=MIRROR Debian mirror to use during installation. Overrides the MIRROR | |
445 | environment variable (see below). | |
446 | --security-mirror=SECURITY_MIRROR | |
447 | Debian mirror to use for security updates. Overrides the | |
448 | SECURITY_MIRROR environment variable (see below). | |
449 | --packages=PACKAGE_NAME1,PACKAGE_NAME2,... | |
450 | List of additional packages to install. Comma separated, without space. | |
451 | -c, --clean only clean up the cache and terminate | |
452 | ||
453 | Environment variables: | |
454 | ||
455 | MIRROR The Debian package mirror to use. See also the --mirror switch above. | |
456 | Defaults to '$MIRROR' | |
457 | SECURITY_MIRROR The Debian package security mirror to use. See also the --security-mirror switch above. | |
458 | Defaults to '$SECURITY_MIRROR' | |
459 | ||
fb7460fe DL |
460 | EOF |
461 | return 0 | |
462 | } | |
463 | ||
177f2cd2 | 464 | options=$(getopt -o hp:n:a:r:c -l arch:,clean,help,mirror:,name:,packages:,path:,release:,rootfs:,security-mirror: -- "$@") |
fb7460fe DL |
465 | if [ $? -ne 0 ]; then |
466 | usage $(basename $0) | |
14d9c0f0 | 467 | exit 1 |
fb7460fe DL |
468 | fi |
469 | eval set -- "$options" | |
470 | ||
cd44154c | 471 | if which dpkg > /dev/null 2>&1 ; then |
1927a6be LV |
472 | arch=$(dpkg --print-architecture) |
473 | else | |
474 | arch=$(uname -m) | |
475 | if [ "$arch" = "i686" ]; then | |
476 | arch="i386" | |
477 | elif [ "$arch" = "x86_64" ]; then | |
478 | arch="amd64" | |
479 | elif [ "$arch" = "armv7l" ]; then | |
480 | arch="armhf" | |
481 | fi | |
482 | fi | |
483 | hostarch=$arch | |
484 | ||
fb7460fe DL |
485 | while true |
486 | do | |
487 | case "$1" in | |
177f2cd2 AD |
488 | -h|--help) usage $0 && exit 1;; |
489 | --) shift 1; break ;; | |
490 | ||
491 | -a|--arch) arch=$2; shift 2;; | |
e4d4da62 | 492 | -c|--clean) clean=1; shift 1;; |
177f2cd2 AD |
493 | --mirror) MIRROR=$2; shift 2;; |
494 | -n|--name) name=$2; shift 2;; | |
495 | --packages) packages=$2; shift 2;; | |
496 | -p|--path) path=$2; shift 2;; | |
497 | -r|--release) release=$2; shift 2;; | |
498 | --rootfs) rootfs=$2; shift 2;; | |
499 | --security-mirror) SECURITY_MIRROR=$2; shift 2;; | |
500 | *) break ;; | |
fb7460fe DL |
501 | esac |
502 | done | |
503 | ||
504 | if [ ! -z "$clean" -a -z "$path" ]; then | |
505 | clean || exit 1 | |
506 | exit 0 | |
507 | fi | |
508 | ||
17abf278 | 509 | if [ "$arch" = "i686" ]; then |
1927a6be LV |
510 | arch=i386 |
511 | fi | |
512 | ||
17abf278 | 513 | if [ "$arch" = "x86_64" ]; then |
1927a6be LV |
514 | arch=amd64 |
515 | fi | |
516 | ||
517 | if [ $hostarch = "i386" -a $arch = "amd64" ]; then | |
518 | echo "can't create $arch container on $hostarch" | |
519 | exit 1 | |
520 | fi | |
521 | ||
522 | if [ $hostarch = "armhf" -o $hostarch = "armel" ] && \ | |
523 | [ $arch != "armhf" -a $arch != "armel" ]; then | |
524 | echo "can't create $arch container on $hostarch" | |
525 | exit 1 | |
526 | fi | |
527 | ||
528 | if [ $hostarch = "powerpc" -a $arch != "powerpc" ]; then | |
529 | echo "can't create $arch container on $hostarch" | |
530 | exit 1 | |
531 | fi | |
532 | ||
fb7460fe DL |
533 | type debootstrap |
534 | if [ $? -ne 0 ]; then | |
535 | echo "'debootstrap' command is missing" | |
536 | exit 1 | |
537 | fi | |
538 | ||
539 | if [ -z "$path" ]; then | |
540 | echo "'path' parameter is required" | |
541 | exit 1 | |
542 | fi | |
543 | ||
544 | if [ "$(id -u)" != "0" ]; then | |
545 | echo "This script should be run as 'root'" | |
546 | exit 1 | |
c01d62f2 | 547 | fi |
bad69158 | 548 | |
b3d3f3c6 | 549 | current_release=`wget ${MIRROR}/dists/stable/Release -O - 2> /dev/null | head |awk '/^Codename: (.*)$/ { print $2; }'` |
b269b8ad LV |
550 | release=${release:-${current_release}} |
551 | valid_releases=('squeeze' 'wheezy' 'jessie' 'sid') | |
b3d3f3c6 | 552 | if [[ ! "${valid_releases[*]}" =~ (^|[^[:alpha:]])$release([^[:alpha:]]|$) ]]; then |
b269b8ad LV |
553 | echo "Invalid release ${release}, valid ones are: ${valid_releases[*]}" |
554 | exit 1 | |
555 | fi | |
556 | ||
1881820a SH |
557 | # detect rootfs |
558 | config="$path/config" | |
1897e3bc | 559 | if [ -z "$rootfs" ]; then |
cd44154c | 560 | if grep -q '^lxc.rootfs' $config 2> /dev/null ; then |
853d58fd | 561 | rootfs=$(awk -F= '/^lxc.rootfs =/{ print $2 }' $config) |
1897e3bc SH |
562 | else |
563 | rootfs=$path/rootfs | |
564 | fi | |
1881820a SH |
565 | fi |
566 | ||
1927a6be | 567 | install_debian $rootfs $release $arch |
fb7460fe DL |
568 | if [ $? -ne 0 ]; then |
569 | echo "failed to install debian" | |
570 | exit 1 | |
571 | fi | |
572 | ||
573 | configure_debian $rootfs $name | |
574 | if [ $? -ne 0 ]; then | |
575 | echo "failed to configure debian for a container" | |
576 | exit 1 | |
577 | fi | |
578 | ||
1927a6be | 579 | copy_configuration $path $rootfs $name $arch |
fb7460fe DL |
580 | if [ $? -ne 0 ]; then |
581 | echo "failed write configuration file" | |
582 | exit 1 | |
583 | fi | |
584 | ||
a9bf60ba AT |
585 | configure_debian_systemd $path $rootfs |
586 | ||
177f2cd2 AD |
587 | post_process ${rootfs} ${release} ${arch} ${hostarch} ${packages} |
588 | ||
227c5600 | 589 | if [ ! -z "$clean" ]; then |
fb7460fe DL |
590 | clean || exit 1 |
591 | exit 0 | |
592 | fi |