4
# template script for generating fedora container for LXC
8
# lxc: linux Container library
11
# Daniel Lezcano <daniel.lezcano@free.fr>
13
# This library is free software; you can redistribute it and/or
14
# modify it under the terms of the GNU Lesser General Public
15
# License as published by the Free Software Foundation; either
16
# version 2.1 of the License, or (at your option) any later version.
18
# This library is distributed in the hope that it will be useful,
19
# but WITHOUT ANY WARRANTY; without even the implied warranty of
20
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21
# Lesser General Public License for more details.
23
# You should have received a copy of the GNU Lesser General Public
24
# License along with this library; if not, write to the Free Software
25
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34
# disable selinux in fedora
35
mkdir -p $rootfs/selinux
36
echo 0 > $rootfs/selinux/enforce
38
# configure the network using the dhcp
39
cat <<EOF > $rootfs/etc/network/interfaces
41
iface lo inet loopback
48
cat <<EOF > $rootfs/etc/hostname
52
cat <<EOF > $rootfs/etc/hosts
53
127.0.0.1 localhost $hostname
56
# provide the lxc service
57
cat <<EOF > $rootfs/etc/init/lxc.conf
58
# fake some events needed for correct startup other services
60
description "Container Upstart"
66
rm -rf /var/run/network/*
67
/sbin/initctl emit stopped JOB=udevtrigger --no-wait
68
/sbin/initctl emit started JOB=udev --no-wait
72
cat <<EOF > $rootfs/etc/init/console.conf
75
# This service maintains a console on tty1 from the point the system is
76
# started until it is shut down again.
78
start on stopped rc RUNLEVEL=[2345]
79
stop on runlevel [!2345]
82
exec /sbin/getty -8 38400 /dev/console
85
cat <<EOF > $rootfs/lib/init/fstab
86
# /lib/init/fstab: lxc system fstab
87
none /spu spufs gid=spu,optional 0 0
88
none /tmp none defaults 0 0
89
none /var/lock tmpfs nodev,noexec,nosuid,showthrough 0 0
90
none /lib/init/rw tmpfs mode=0755,nosuid,optional 0 0
93
# reconfigure some services
94
if [ -z "$LANG" ]; then
95
chroot $rootfs locale-gen en_US.UTF-8
96
chroot $rootfs update-locale LANG=en_US.UTF-8
98
chroot $rootfs locale-gen $LANG
99
chroot $rootfs update-locale LANG=$LANG
102
# remove pointless services in a container
103
chroot $rootfs /usr/sbin/update-rc.d -f ondemand remove
105
chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls u*.conf); do mv $f $f.orig; done'
106
chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls tty[2-9].conf); do mv $f $f.orig; done'
107
chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls plymouth*.conf); do mv $f $f.orig; done'
108
chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls hwclock*.conf); do mv $f $f.orig; done'
109
chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls module*.conf); do mv $f $f.orig; done'
111
echo "Please change root-password !"
112
echo "root:root" | chroot $rootfs chpasswd
122
# check the mini fedora was not already downloaded
123
mkdir -p "$cache/partial-$arch"
124
if [ $? -ne 0 ]; then
125
echo "Failed to create '$cache/partial-$arch' directory"
129
# download a mini fedora into a cache
130
echo "Downloading fedora minimal ..."
131
febootstrap $DISTRO $cache/partial-$arch
132
if [ $? -ne 0 ]; then
133
echo "Failed to download the rootfs, aborting."
137
mv "$1/partial-$arch" "$1/rootfs-$arch"
138
echo "Download complete."
149
# make a local copy of the minifedora
150
echo -n "Copying rootfs to $rootfs ..."
151
cp -a $cache/rootfs-$arch $rootfs || return 1
157
cache="/var/cache/lxc/fedora"
159
mkdir -p /var/lock/subsys/
162
if [ $? -ne 0 ]; then
163
echo "Cache repository is busy."
169
echo "Checking cache download in $cache/rootfs-$arch ... "
170
if [ ! -e "$cache/rootfs-$arch" ]; then
171
download_fedora $cache $arch
172
if [ $? -ne 0 ]; then
173
echo "Failed to download 'fedora base'"
178
echo "Copy $cache/rootfs-$arch to $rootfs ... "
179
copy_fedora $cache $arch $rootfs
180
if [ $? -ne 0 ]; then
181
echo "Failed to copy rootfs"
187
) 200>/var/lock/subsys/lxc
198
cat <<EOF >> $path/config
204
lxc.mount = $path/fstab
206
lxc.console = /dev/console
208
lxc.cgroup.devices.deny = a
210
lxc.cgroup.devices.allow = c 1:3 rwm
211
lxc.cgroup.devices.allow = c 1:5 rwm
213
lxc.cgroup.devices.allow = c 5:1 rwm
214
lxc.cgroup.devices.allow = c 5:0 rwm
215
lxc.cgroup.devices.allow = c 4:0 rwm
216
lxc.cgroup.devices.allow = c 4:1 rwm
218
lxc.cgroup.devices.allow = c 1:9 rwm
219
lxc.cgroup.devices.allow = c 1:8 rwm
220
lxc.cgroup.devices.allow = c 136:* rwm
221
lxc.cgroup.devices.allow = c 5:2 rwm
223
lxc.cgroup.devices.allow = c 254:0 rwm
226
cat <<EOF > $path/fstab
227
proc $rootfs/proc proc nodev,noexec,nosuid 0 0
228
devpts $rootfs/dev/pts devpts defaults 0 0
229
sysfs $rootfs/sys sysfs defaults 0 0
232
if [ $? -ne 0 ]; then
233
echo "Failed to add configuration"
242
cache="/var/cache/lxc/fedora"
244
if [ ! -e $cache ]; then
248
# lock, so we won't purge while someone is creating a repository
252
echo "Cache repository is busy."
256
echo -n "Purging the download cache..."
257
rm --preserve-root --one-file-system -rf $cache && echo "Done." || exit 1
260
) 200>/var/lock/subsys/lxc
266
$1 -h|--help -p|--path=<path> --clean
271
options=$(getopt -o hp:n:c -l help,path:,name:,clean -- "$@")
272
if [ $? -ne 0 ]; then
276
eval set -- "$options"
281
-h|--help) usage $0 && exit 0;;
282
-p|--path) path=$2; shift 2;;
283
-n|--name) name=$2; shift 2;;
284
-c|--clean) clean=$2; shift 2;;
285
--) shift 1; break ;;
290
if [ ! -z "$clean" -a -z "$path" ]; then
296
if [ $? -ne 0 ]; then
297
echo "'febootstrap' command is missing"
301
if [ -z "$path" ]; then
302
echo "'path' parameter is required"
306
if [ "$(id -u)" != "0" ]; then
307
echo "This script should be run as 'root'"
313
install_fedora $rootfs
314
if [ $? -ne 0 ]; then
315
echo "failed to install fedora"
319
configure_fedora $rootfs $name
320
if [ $? -ne 0 ]; then
321
echo "failed to configure fedora for a container"
325
copy_configuration $path $rootfs $name
326
if [ $? -ne 0 ]; then
327
echo "failed write configuration file"
331
if [ ! -z $clean ]; then