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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 # configure the inittab
29 cat <<EOF > $rootfs/etc/inittab
31 si::sysinit:/etc/init.d/rcS
32 l0:0:wait:/etc/init.d/rc 0
33 l1:1:wait:/etc/init.d/rc 1
34 l2:2:wait:/etc/init.d/rc 2
35 l3:3:wait:/etc/init.d/rc 3
36 l4:4:wait:/etc/init.d/rc 4
37 l5:5:wait:/etc/init.d/rc 5
38 l6:6:wait:/etc/init.d/rc 6
39 # Normally not reached, but fallthrough in case of emergency.
40 z6:6:respawn:/sbin/sulogin
41 1:2345:respawn:/sbin/getty 38400 console
42 c1:12345:respawn:/sbin/getty 38400 tty1 linux
43 c2:12345:respawn:/sbin/getty 38400 tty2 linux
44 c3:12345:respawn:/sbin/getty 38400 tty3 linux
45 c4:12345:respawn:/sbin/getty 38400 tty4 linux
48 # disable selinux in debian
49 mkdir -p $rootfs/selinux
50 echo 0 > $rootfs/selinux/enforce
52 # configure the network using the dhcp
53 cat <<EOF > $rootfs/etc/network/interfaces
55 iface lo inet loopback
62 cat <<EOF > $rootfs/etc/hostname
66 # reconfigure some services
67 if [ -z "$LANG" ]; then
68 chroot $rootfs locale-gen en_US.UTF-8
69 chroot $rootfs update-locale LANG=en_US.UTF-8
71 chroot $rootfs locale-gen $LANG
72 chroot $rootfs update-locale LANG=$LANG
75 # remove pointless services in a container
76 chroot $rootfs /usr/sbin/update-rc.d -f umountfs remove
77 chroot $rootfs /usr/sbin/update-rc.d -f hwclock.sh remove
78 chroot $rootfs /usr/sbin/update-rc.d -f hwclockfirst.sh remove
80 echo "root:root" | chroot $rootfs chpasswd
81 echo "Root password is 'root', please change !"
102 # check the mini debian was not already downloaded
103 mkdir -p "$cache/partial-$arch"
104 if [ $? -ne 0 ]; then
105 echo "Failed to create '$cache/partial-$arch' directory"
109 # download a mini debian into a cache
110 echo "Downloading debian minimal ..."
111 debootstrap --verbose --variant=minbase --arch=$arch \
112 --include $packages \
113 squeeze $cache/partial-$arch http://ftp.hosteurope.de/mirror/ftp.debian.org/debian
114 if [ $? -ne 0 ]; then
115 echo "Failed to download the rootfs, aborting."
119 mv "$1/partial-$arch" "$1/rootfs-$arch"
120 echo "Download complete."
131 # make a local copy of the minidebian
132 echo -n "Copying rootfs to $rootfs..."
133 cp -a $cache/rootfs-$arch $rootfs || return 1
139 cache="/var/cache/lxc/debian"
141 mkdir -p /var/lock/subsys/
144 if [ $? -ne 0 ]; then
145 echo "Cache repository is busy."
150 if [ "$arch" == "x86_64" ]; then
154 if [ "$arch" == "i686" ]; then
158 echo "Checking cache download in $cache/rootfs-$arch ... "
159 if [ ! -e "$cache/rootfs-$arch" ]; then
160 download_debian $cache $arch
161 if [ $? -ne 0 ]; then
162 echo "Failed to download 'debian base'"
167 copy_debian $cache $arch $rootfs
168 if [ $? -ne 0 ]; then
169 echo "Failed to copy rootfs"
175 ) 200>/var/lock/subsys/lxc
186 cat <<EOF >> $path/config
190 lxc.cgroup.devices.deny = a
192 lxc.cgroup.devices.allow = c 1:3 rwm
193 lxc.cgroup.devices.allow = c 1:5 rwm
195 lxc.cgroup.devices.allow = c 5:1 rwm
196 lxc.cgroup.devices.allow = c 5:0 rwm
197 lxc.cgroup.devices.allow = c 4:0 rwm
198 lxc.cgroup.devices.allow = c 4:1 rwm
200 lxc.cgroup.devices.allow = c 1:9 rwm
201 lxc.cgroup.devices.allow = c 1:8 rwm
202 lxc.cgroup.devices.allow = c 136:* rwm
203 lxc.cgroup.devices.allow = c 5:2 rwm
205 lxc.cgroup.devices.allow = c 254:0 rwm
208 lxc.mount.entry=proc $rootfs/proc proc nodev,noexec,nosuid 0 0
209 lxc.mount.entry=devpts $rootfs/dev/pts devpts defaults 0 0
210 lxc.mount.entry=sysfs $rootfs/sys sysfs defaults 0 0
213 if [ $? -ne 0 ]; then
214 echo "Failed to add configuration"
223 cache="/var/cache/lxc/debian"
225 if [ ! -e $cache ]; then
229 # lock, so we won't purge while someone is creating a repository
233 echo "Cache repository is busy."
237 echo -n "Purging the download cache..."
238 rm --preserve-root --one-file-system -rf $cache && echo "Done." || exit 1
241 ) 200>/var/lock/subsys/lxc
247 $1 -h|--help -p|--path=<path> --clean
252 options=$(getopt -o hp:n:c -l help,path:,name:,clean -- "$@")
253 if [ $? -ne 0 ]; then
257 eval set -- "$options"
262 -h|--help) usage $0 && exit 0;;
263 -p|--path) path=$2; shift 2;;
264 -n|--name) name=$2; shift 2;;
265 -c|--clean) clean=$2; shift 2;;
266 --) shift 1; break ;;
271 if [ ! -z "$clean" -a -z "$path" ]; then
277 if [ $? -ne 0 ]; then
278 echo "'debootstrap' command is missing"
282 if [ -z "$path" ]; then
283 echo "'path' parameter is required"
287 if [ "$(id -u)" != "0" ]; then
288 echo "This script should be run as 'root'"
294 install_debian $rootfs
295 if [ $? -ne 0 ]; then
296 echo "failed to install debian"
300 configure_debian $rootfs $name
301 if [ $? -ne 0 ]; then
302 echo "failed to configure debian for a container"
306 copy_configuration $path $rootfs
307 if [ $? -ne 0 ]; then
308 echo "failed write configuration file"
312 if [ ! -z $clean ]; then