~ubuntu-branches/ubuntu/quantal/lxc/quantal-201208301614

« back to all changes in this revision

Viewing changes to scripts/lxc-busybox.in

  • Committer: Bazaar Package Importer
  • Author(s): Guido Trotter
  • Date: 2010-06-28 10:15:48 UTC
  • mto: (1.1.4 upstream) (3.1.5 sid)
  • mto: This revision was merged to the branch mainline in revision 7.
  • Revision ID: james.westby@ubuntu.com-20100628101548-vexhggdo6x9cpwtk
ImportĀ upstreamĀ versionĀ 0.7.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/bin/bash
2
 
 
3
 
#
4
 
# lxc: linux Container library
5
 
 
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
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
 
 
23
 
install_busybox()
24
 
{
25
 
    rootfs=$1
26
 
    name=$2
27
 
    res=0
28
 
    tree="\
29
 
$rootfs/dev \
30
 
$rootfs/home \
31
 
$rootfs/root \
32
 
$rootfs/etc \
33
 
$rootfs/etc/init.d \
34
 
$rootfs/bin \
35
 
$rootfs/sbin \
36
 
$rootfs/proc \
37
 
$rootfs/mnt \
38
 
$rootfs/tmp \
39
 
$rootfs/var/log \
40
 
$rootfs/usr/share/udhcpc \
41
 
$rootfs/dev/shm"
42
 
 
43
 
    mkdir -p $tree || return 1
44
 
    chmod 755 $tree || return 1
45
 
 
46
 
    pushd $rootfs/dev > /dev/null || return 1
47
 
 
48
 
    # minimal devices needed for busybox
49
 
    mknod tty c 5 0       || res=1
50
 
    mknod console c 5 1   || res=1
51
 
    chmod 666 tty console || res=1
52
 
    mknod tty0 c 4 0      || res=1
53
 
    mknod tty1 c 4 0      || res=1
54
 
    mknod tty5 c 4 0      || res=1
55
 
    chmod 666 tty0        || res=1
56
 
    mknod ram0 b 1 0      || res=1
57
 
    chmod 600 ram0        || res=1
58
 
    mknod null c 1 3      || res=1
59
 
    chmod 666 null        || res=1
60
 
 
61
 
    popd > /dev/null
62
 
 
63
 
    # root user defined
64
 
    cat <<EOF >> $rootfs/etc/passwd
65
 
root:x:0:0:root:/root:/bin/sh
66
 
EOF
67
 
 
68
 
    cat <<EOF >> $rootfs/etc/group
69
 
root:x:0:root
70
 
EOF
71
 
 
72
 
    # empty password
73
 
    cat <<EOF >> $rootfs/etc/shadow
74
 
root:\$1\$NJJLsV0P\$Y/esfSXDNR88G/bZFzgWY1:14595:0:99999:7:::
75
 
EOF
76
 
 
77
 
    echo "empty password for root, don't forget to change it !"
78
 
 
79
 
    # mount everything
80
 
    cat <<EOF >> $rootfs/etc/init.d/rcS
81
 
#!/bin/sh
82
 
syslogd
83
 
mount -a
84
 
udhcpc
85
 
EOF
86
 
 
87
 
    # executable
88
 
    chmod 744 $rootfs/etc/init.d/rcS || return 1
89
 
 
90
 
    # mount points
91
 
    cat <<EOF >> $rootfs/etc/fstab
92
 
proc  /proc      proc    defaults     0      0
93
 
shm   /dev/shm   tmpfs   defaults     0      0
94
 
EOF
95
 
 
96
 
    # writable and readable for other
97
 
    chmod 644 $rootfs/etc/fstab || return 1
98
 
 
99
 
    # launch rcS first then make a console available
100
 
    # and propose a shell on the tty, the last one is
101
 
    # not needed
102
 
    cat <<EOF >> $rootfs/etc/inittab
103
 
::sysinit:/etc/init.d/rcS
104
 
::respawn:/bin/getty -L tty1 115200 vt100
105
 
::askfirst:/bin/sh
106
 
EOF
107
 
    # writable and readable for other
108
 
    chmod 644 $rootfs/etc/inittab || return 1
109
 
 
110
 
    cat <<EOF >> $rootfs/usr/share/udhcpc/default.script
111
 
#!/bin/sh
112
 
 
113
 
case "\$1" in
114
 
        deconfig)
115
 
                ip addr flush dev \$interface
116
 
                ;;
117
 
 
118
 
        renew|bound)
119
 
 
120
 
                # flush all the routes
121
 
                if [ -n "\$router" ]; then
122
 
                        ip route del default 2> /dev/null
123
 
                fi
124
 
 
125
 
                # check broadcast
126
 
                if [ -n "\$broadcast" ]; then
127
 
                        broadcast="broadcast \$broadcast"
128
 
                fi
129
 
 
130
 
                # add a new ip address
131
 
                ip addr add \$ip/\$mask \$broadcast dev \$interface
132
 
 
133
 
                if [ -n "\$router" ]; then
134
 
                        ip route add default via \$router dev \$interface
135
 
                fi
136
 
 
137
 
                [ -n "\$domain" ] && echo search \$domain > /etc/resolv.conf
138
 
                for i in \$dns ; do
139
 
                        echo nameserver \$i >> /etc/resolv.conf
140
 
                done
141
 
                ;;
142
 
esac
143
 
exit 0
144
 
EOF
145
 
 
146
 
    chmod 744 $rootfs/usr/share/udhcpc/default.script
147
 
 
148
 
    return $res
149
 
}
150
 
 
151
 
configure_busybox()
152
 
{
153
 
    rootfs=$1
154
 
 
155
 
    functions="\
156
 
        [ [[ addgroup adduser adjtimex ar arp arping ash awk basename \
157
 
        brctl bunzip2 bzcat bzip2 cal cat catv chattr chgrp chmod \
158
 
        chown chpasswd chpst chroot chrt chvt cksum clear cmp comm \
159
 
        cp cpio crond crontab cryptpw cut date dc dd deallocvt \
160
 
        delgroup deluser df dhcprelay diff dirname dmesg dnsd dos2unix \
161
 
        du dumpkmap dumpleases echo ed egrep eject env envdir envuidgid \
162
 
        ether-wake expand expr fakeidentd false fbset fdformat fdisk \
163
 
        fetchmail fgrep find findfs fold free freeramdisk fsck \
164
 
        fsck.minix ftpget ftpput fuser getopt getty grep gunzip gzip \
165
 
        halt hdparm head hexdump hostid hostname httpd hwclock id \
166
 
        ifconfig ifdown ifenslave ifup inetd init insmod install ip \
167
 
        ipaddr ipcalc ipcrm ipcs iplink iproute iprule iptunnel \
168
 
        kbd_mode kill killall killall5 klogd last length less linux32 \
169
 
        linux64 linuxrc ln loadfont loadkmap logger login logname \
170
 
        logread losetup lpd lpq lpr ls lsattr lsmod lzmacat makedevs \
171
 
        md5sum mdev mesg microcom mkdir mkfifo mkfs.minix mknod mkswap \
172
 
        mktemp modprobe more mount mountpoint msh mt mv nameif nc \
173
 
        netstat nice nmeter nohup nslookup od openvt passwd patch \
174
 
        pgrep pidof ping ping6 pipe_progress pivot_root pkill poweroff \
175
 
        printenv printf ps pscan pwd raidautorun rdate readahead \
176
 
        readlink readprofile realpath reboot renice reset resize rm \
177
 
        rmdir rmmod route rpm rpm2cpio run-parts runlevel runsv \
178
 
        runsvdir rx script sed sendmail seq setarch setconsole \
179
 
        setkeycodes setlogcons setsid setuidgid sh sha1sum slattach \
180
 
        sleep softlimit sort split start-stop-daemon stat strings \
181
 
        stty su sulogin sum sv svlogd swapoff swapon switch_root \
182
 
        sync sysctl syslogd tac tail tar taskset tcpsvd tee telnet \
183
 
        telnetd test tftp tftpd time top touch tr traceroute \
184
 
        true tty ttysize udhcpc udhcpd udpsvd umount uname uncompress \
185
 
        unexpand uniq unix2dos unlzma unzip uptime usleep uudecode \
186
 
        uuencode vconfig vi vlock watch watchdog wc wget which \
187
 
        who whoami xargs yes zcat zcip"
188
 
 
189
 
    type busybox >/dev/null
190
 
 
191
 
    if [ $? -ne 0 ]; then
192
 
        echo "busybox executable is not accessible"
193
 
        return 1
194
 
    fi
195
 
 
196
 
    # copy busybox in the rootfs
197
 
    cp $(which busybox) $rootfs/bin
198
 
    if [ $? -ne 0 ]; then
199
 
        echo "failed to copy busybox in the rootfs"
200
 
        return 1
201
 
    fi
202
 
 
203
 
    # do hardlink to busybox for the different commands
204
 
    for i in $functions; do ln $rootfs/bin/busybox $rootfs/bin/$i; done
205
 
 
206
 
    # relink /sbin/init
207
 
    ln $rootfs/bin/busybox $rootfs/sbin/init
208
 
 
209
 
    # passwd exec must be setuid
210
 
    chmod +s $rootfs/bin/passwd
211
 
 
212
 
    return 0
213
 
}
214
 
 
215
 
copy_configuration()
216
 
{
217
 
    path=$1
218
 
    rootfs=$2
219
 
    name=$3
220
 
 
221
 
cat <<EOF >> $path/config
222
 
lxc.utsname = $name
223
 
lxc.tty = 1
224
 
lxc.rootfs = $rootfs
225
 
lxc.cgroup.devices.deny = a
226
 
# /dev/null and zero
227
 
lxc.cgroup.devices.allow = c 1:3 rwm
228
 
lxc.cgroup.devices.allow = c 1:5 rwm
229
 
# consoles
230
 
lxc.cgroup.devices.allow = c 5:1 rwm
231
 
lxc.cgroup.devices.allow = c 5:0 rwm
232
 
lxc.cgroup.devices.allow = c 4:0 rwm
233
 
lxc.cgroup.devices.allow = c 4:1 rwm
234
 
# /dev/{,u}random
235
 
lxc.cgroup.devices.allow = c 1:9 rwm
236
 
lxc.cgroup.devices.allow = c 1:8 rwm
237
 
lxc.cgroup.devices.allow = c 136:* rwm
238
 
lxc.cgroup.devices.allow = c 5:2 rwm
239
 
# rtc
240
 
lxc.cgroup.devices.allow = c 254:0 rwm
241
 
EOF
242
 
}
243
 
 
244
 
usage()
245
 
{
246
 
    cat <<EOF
247
 
$1 -h|--help -p|--path=<path>
248
 
EOF
249
 
    return 0
250
 
}
251
 
 
252
 
options=$(getopt -o hp:n: -l help,path:,name: -- "$@")
253
 
if [ $? -ne 0 ]; then
254
 
        usage $(basename $0)
255
 
        exit 1
256
 
fi
257
 
eval set -- "$options"
258
 
 
259
 
while true
260
 
do
261
 
    case "$1" in
262
 
        -h|--help)      usage $0 && exit 0;;
263
 
        -p|--path)      path=$2; shift 2;;
264
 
        -n|--name)      name=$2; shift 2;;
265
 
        --)             shift 1; break ;;
266
 
        *)              break ;;
267
 
    esac
268
 
done
269
 
 
270
 
if [ "$(id -u)" != "0" ]; then
271
 
    echo "This script should be run as 'root'"
272
 
    exit 1
273
 
fi
274
 
 
275
 
if [ -z "$path" ]; then
276
 
    echo "'path' parameter is required"
277
 
    exit 1
278
 
fi
279
 
 
280
 
rootfs=$path/rootfs
281
 
 
282
 
install_busybox $rootfs $name
283
 
if [ $? -ne 0 ]; then
284
 
    echo "failed to install busybox's rootfs"
285
 
    exit 1
286
 
fi
287
 
 
288
 
configure_busybox $rootfs
289
 
if [ $? -ne 0 ]; then
290
 
    echo "failed to configure busybox template"
291
 
    exit 1
292
 
fi
293
 
 
294
 
copy_configuration $path $rootfs $name
295
 
if [ $? -ne 0 ]; then
296
 
    echo "failed to write configuration file"
297
 
    exit 1
298
 
fi
299