4 # lxc: linux Container library
7 # Daniel Lezcano <daniel.lezcano@free.fr>
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.
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.
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 USA
23 MIRROR
=${MIRROR:-http://cdn.debian.net/debian}
24 LOCALSTATEDIR
="@LOCALSTATEDIR@"
25 LXC_TEMPLATE_CONFIG
="@LXCTEMPLATECONFIG@"
32 # squeeze only has /dev/tty and /dev/tty0 by default,
33 # therefore creating missing device nodes for tty1-4.
34 for tty
in $
(seq 1 4); do
35 if [ ! -e $rootfs/dev
/tty
$tty ]; then
36 mknod
$rootfs/dev
/tty
$tty c
4 $tty
40 # configure the inittab
41 cat <<EOF > $rootfs/etc/inittab
43 si::sysinit:/etc/init.d/rcS
44 l0:0:wait:/etc/init.d/rc 0
45 l1:1:wait:/etc/init.d/rc 1
46 l2:2:wait:/etc/init.d/rc 2
47 l3:3:wait:/etc/init.d/rc 3
48 l4:4:wait:/etc/init.d/rc 4
49 l5:5:wait:/etc/init.d/rc 5
50 l6:6:wait:/etc/init.d/rc 6
51 # Normally not reached, but fallthrough in case of emergency.
52 z6:6:respawn:/sbin/sulogin
53 1:2345:respawn:/sbin/getty 38400 console
54 c1:12345:respawn:/sbin/getty 38400 tty1 linux
55 c2:12345:respawn:/sbin/getty 38400 tty2 linux
56 c3:12345:respawn:/sbin/getty 38400 tty3 linux
57 c4:12345:respawn:/sbin/getty 38400 tty4 linux
58 p6::ctrlaltdel:/sbin/init 6
59 p0::powerfail:/sbin/init 0
62 # disable selinux in debian
63 mkdir
-p $rootfs/selinux
64 echo 0 > $rootfs/selinux
/enforce
66 # configure the network using the dhcp
67 cat <<EOF > $rootfs/etc/network/interfaces
69 iface lo inet loopback
76 cat <<EOF > $rootfs/etc/hostname
80 # reconfigure some services
81 if [ -z "$LANG" ]; then
82 chroot
$rootfs locale-gen en_US.UTF-8 UTF-8
83 chroot
$rootfs update-locale LANG
=en_US.UTF-8
85 encoding
=$
(echo $LANG | cut
-d.
-f2)
86 chroot
$rootfs sed -e "s/^# \(${LANG} ${encoding}\)/\1/" \
87 -i /etc
/locale.gen
2>/dev
/null
88 chroot
$rootfs locale-gen
$LANG $encoding
89 chroot
$rootfs update-locale LANG
=$LANG
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
98 # generate new SSH keys
99 if [ -x $rootfs/var
/lib
/dpkg
/info
/openssh-server.postinst
]; then
100 cat > $rootfs/usr
/sbin
/policy-rc.d
<< EOF
104 chmod +x
$rootfs/usr
/sbin
/policy-rc.d
106 if [ -f $rootfs/etc
/init
/ssh.conf
]; then
107 mv $rootfs/etc
/init
/ssh.conf
$rootfs/etc
/init
/ssh.conf.disabled
110 rm -f $rootfs/etc
/ssh
/ssh_host_
*key
*
112 DPKG_MAINTSCRIPT_PACKAGE
=openssh DPKG_MAINTSCRIPT_NAME
=postinst chroot
$rootfs /var
/lib
/dpkg
/info
/openssh-server.postinst configure
113 sed -i "s/root@$(hostname)/root@$hostname/g" $rootfs/etc
/ssh
/ssh_host_
*.pub
115 if [ -f "$rootfs/etc/init/ssh.conf.disabled" ]; then
116 mv $rootfs/etc
/init
/ssh.conf.disabled
$rootfs/etc
/init
/ssh.conf
119 rm -f $rootfs/usr
/sbin
/policy-rc.d
122 # set initial timezone as on host
123 if [ -f /etc
/timezone
]; then
124 cat /etc
/timezone
> $rootfs/etc
/timezone
125 chroot
$rootfs dpkg-reconfigure
-f noninteractive tzdata
126 elif [ -f /etc
/sysconfig
/clock ]; then
127 .
/etc
/sysconfig
/clock
128 echo $ZONE > $rootfs/etc
/timezone
129 chroot
$rootfs dpkg-reconfigure
-f noninteractive tzdata
131 echo "Timezone in container is not configured. Adjust it manually."
134 echo "root:root" | chroot
$rootfs chpasswd
135 echo "Root password is 'root', please change !"
142 rm -rf $cache/partial-
$release-$arch
143 rm -rf $cache/rootfs-
$release-$arch
163 trap cleanup EXIT SIGHUP SIGINT SIGTERM
164 # check the mini debian was not already downloaded
165 mkdir
-p "$cache/partial-$release-$arch"
166 if [ $?
-ne 0 ]; then
167 echo "Failed to create '$cache/partial-$release-$arch' directory"
171 # download a mini debian into a cache
172 echo "Downloading debian minimal ..."
173 debootstrap
--verbose --variant=minbase
--arch=$arch \
174 --include=$packages \
175 "$release" "$cache/partial-$release-$arch" $MIRROR
176 if [ $?
-ne 0 ]; then
177 echo "Failed to download the rootfs, aborting."
181 mv "$1/partial-$release-$arch" "$1/rootfs-$release-$arch"
182 echo "Download complete."
198 # make a local copy of the minidebian
199 echo -n "Copying rootfs to $rootfs..."
201 rsync
-Ha "$cache/rootfs-$release-$arch"/ $rootfs/ ||
return 1
207 cache
="$LOCALSTATEDIR/cache/lxc/debian"
211 mkdir
-p $LOCALSTATEDIR/lock
/subsys
/
214 if [ $?
-ne 0 ]; then
215 echo "Cache repository is busy."
219 echo "Checking cache download in $cache/rootfs-$release-$arch ... "
220 if [ ! -e "$cache/rootfs-$release-$arch" ]; then
221 download_debian
$cache $arch $release
222 if [ $?
-ne 0 ]; then
223 echo "Failed to download 'debian base'"
228 copy_debian
$cache $arch $rootfs $release
229 if [ $?
-ne 0 ]; then
230 echo "Failed to copy rootfs"
236 ) 9>$LOCALSTATEDIR/lock
/subsys
/lxc-debian
248 # Generate the configuration file
249 ## Create the fstab (empty by default)
252 # if there is exactly one veth network entry, make sure it has an
254 nics
=`grep -e '^lxc\.network\.type[ \t]*=[ \t]*veth' $path/config | wc -l`
255 if [ $nics -eq 1 ]; then
256 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
259 ## Add all the includes
260 echo "" >> $path/config
261 echo "# Common configuration" >> $path/config
262 if [ -e "${LXC_TEMPLATE_CONFIG}/debian.common.conf" ]; then
263 echo "lxc.include = ${LXC_TEMPLATE_CONFIG}/debian.common.conf" >> $path/config
265 if [ -e "${LXC_TEMPLATE_CONFIG}/debian.${release}.conf" ]; then
266 echo "lxc.include = ${LXC_TEMPLATE_CONFIG}/debian.${release}.conf" >> $path/config
269 ## Add the container-specific config
270 echo "" >> $path/config
271 echo "# Container specific configuration" >> $path/config
272 grep -q "^lxc.rootfs" $path/config
2>/dev
/null ||
echo "lxc.rootfs = $rootfs" >> $path/config
274 cat <<EOF >> $path/config
275 lxc.mount = $path/fstab
276 lxc.utsname = $hostname
280 if [ $?
-ne 0 ]; then
281 echo "Failed to add configuration"
290 cache
="$LOCALSTATEDIR/cache/lxc/debian"
292 if [ ! -e $cache ]; then
296 # lock, so we won't purge while someone is creating a repository
300 echo "Cache repository is busy."
304 echo -n "Purging the download cache..."
305 rm --preserve-root --one-file-system -rf $cache && echo "Done." ||
exit 1
308 ) 9>$LOCALSTATEDIR/lock
/subsys
/lxc-debian
314 $1 -h|--help -p|--path=<path> [-a|--arch] [-r|--release=<release>] [-c|--clean]
315 release: the debian release (e.g. wheezy): defaults to current stable
316 arch: the container architecture (e.g. amd64): defaults to host arch
321 options
=$
(getopt
-o hp
:n
:a
:r
:c
-l help,rootfs
:,path
:,name
:,arch
:,release
:,clean
-- "$@")
322 if [ $?
-ne 0 ]; then
326 eval set -- "$options"
328 if which dpkg
>/dev
/null
2>&1 ; then
329 arch
=$
(dpkg
--print-architecture)
332 if [ "$arch" = "i686" ]; then
334 elif [ "$arch" = "x86_64" ]; then
336 elif [ "$arch" = "armv7l" ]; then
345 -h|
--help) usage
$0 && exit 1;;
346 -p|
--path) path
=$2; shift 2;;
347 --rootfs) rootfs
=$2; shift 2;;
348 -a|
--arch) arch
=$2; shift 2;;
349 -r|
--release) release
=$2; shift 2;;
350 -n|
--name) name
=$2; shift 2;;
351 -c|
--clean) clean
=$2; shift 2;;
352 --) shift 1; break ;;
357 if [ ! -z "$clean" -a -z "$path" ]; then
362 if [ "$arch" = "i686" ]; then
366 if [ "$arch" = "x86_64" ]; then
370 if [ $hostarch = "i386" -a $arch = "amd64" ]; then
371 echo "can't create $arch container on $hostarch"
375 if [ $hostarch = "armhf" -o $hostarch = "armel" ] && \
376 [ $arch != "armhf" -a $arch != "armel" ]; then
377 echo "can't create $arch container on $hostarch"
381 if [ $hostarch = "powerpc" -a $arch != "powerpc" ]; then
382 echo "can't create $arch container on $hostarch"
387 if [ $?
-ne 0 ]; then
388 echo "'debootstrap' command is missing"
392 if [ -z "$path" ]; then
393 echo "'path' parameter is required"
397 if [ "$(id -u)" != "0" ]; then
398 echo "This script should be run as 'root'"
402 current_release
=`wget ${MIRROR}/dists/stable/Release -O - 2>/dev/null |\
403 head |awk '/^Codename: (.*)$/ { print $2; }'`
404 release
=${release:-${current_release}}
405 valid_releases
=('squeeze' 'wheezy' 'jessie' 'sid')
406 if [[ ! "${valid_releases[*]}" =~
(^|
[^
[:alpha
:]])$release([^
[:alpha
:]]|$
) ]]
408 echo "Invalid release ${release}, valid ones are: ${valid_releases[*]}"
413 config
="$path/config"
414 if [ -z "$rootfs" ]; then
415 if grep -q '^lxc.rootfs' $config 2>/dev
/null
; then
416 rootfs
=$
(awk -F= '/^lxc.rootfs =/{ print $2 }' $config)
423 install_debian
$rootfs $release $arch
424 if [ $?
-ne 0 ]; then
425 echo "failed to install debian"
429 configure_debian
$rootfs $name
430 if [ $?
-ne 0 ]; then
431 echo "failed to configure debian for a container"
435 copy_configuration
$path $rootfs $name $arch
436 if [ $?
-ne 0 ]; then
437 echo "failed write configuration file"
441 if [ ! -z $clean ]; then