source: trunk/debirf/fs/usr/bin/make-debirf @ 778

Last change on this file since 778 was 778, checked in by jrollins, 5 years ago

debirf: see changelog (moved to GPLv3)

  • Property svn:executable set to *
File size: 10.4 KB
Line 
1#!/bin/bash -e
2
3# make-debirf: script to build debirf system.
4#
5# The debirf scripts were written by
6# Jameson Rollins <jrollins@fifthhorseman.net>
7# and
8# Daniel Kahn Gillmor <dkg-debian.org@fifthhorseman.net>.
9#
10# They are Copyright 2007, and are all released under the GPL,
11# version 3 or later.
12
13###############################################################
14### VARIABLES
15
16CMD=$(basename $0)
17
18DEBIRF_COMMON=${DEBIRF_COMMON:-"/usr/share/debirf/common"}
19source "$DEBIRF_COMMON"
20
21export FAKECHROOT_BUILD=false
22STAGE_ROOT=true
23STAGE_PLUGINS=true
24STAGE_INITRD=true
25
26###############################################################
27### FUNCTIONS
28
29usage() {
30    cat <<EOF
31Usage: $CMD [options] profile [kernel.deb]
32Create a debirf system.  The 'profile' is a path to a debirf profile
33directory.  If no kernel is specified, make-debirf will use the
34latest kernel for the specified suite.  If a path to a kernel deb is
35specified on the command line, that will be installed instead.
36
37options:
38  -h|--help          this help message
39  -f|--fakechroot    fakechroot build (other wise requires being root)
40  -c|--check-vars    check variables before make
41  -n|--new           create new root, even if old one exists
42  -o|--overwrite     debootstrap on top of old root if it exists
43  -s|--skip          skip debootstrap step if old root exists
44  -i|--initrd-only   do not create root, just create initramfs
45EOF
46}
47
48usage_profile() {
49    cat <<EOF
50It looks like your profile is not correctly formed.  You should begin
51by copying and modifying one of the example debirf profiles.
52On Debian, you should run:
53cp -r /usr/share/doc/debirf/example-profiles/<profile-name> .
54EOF
55}
56
57failure() {
58    echo "$1" >&2
59    exit ${2:-'1'}
60}
61
62create_debootstrap() {
63    mkdir -p "$DEBIRF_ROOT"
64    if [ "$FAKECHROOT_BUILD" = 'true' ] ; then
65        fakeroot -s "$DEBIRF_FAKEROOT_STATE" fakechroot /usr/sbin/debootstrap --variant=fakechroot "$DEBIRF_DISTRO" "$DEBIRF_ROOT" "$DEBIRF_MIRROR"
66    else
67        /usr/sbin/debootstrap "$DEBIRF_DISTRO" "$DEBIRF_ROOT" "$DEBIRF_MIRROR"
68    fi
69}
70
71run_plugins() {
72    # export all the DEBIRF_* environment variables:
73    for var in ${!DEBIRF_*}; do
74        export $var
75    done
76    if [ "$FAKECHROOT_BUILD" = 'true' ] ; then
77        fakeroot -i "$DEBIRF_FAKEROOT_STATE" -s "$DEBIRF_FAKEROOT_STATE" run-parts --verbose "$DEBIRF_PLUGINS"
78    else
79        run-parts --verbose "$DEBIRF_PLUGINS"
80    fi
81}
82
83## create_initrd functions take the name of the targeted initrd as
84## their first argument.
85# stupid simple method
86create_initrd_stupid_simple() {
87    #ln sbin/init to /init
88    ln -sf /sbin/init "$DEBIRF_ROOT/init"
89
90    if [ "$FAKECHROOT_BUILD" = 'true' ] ; then
91        # create a proper dev tree, based on the host systems dev tree
92        fakeroot -i "$DEBIRF_FAKEROOT_STATE" -s "$DEBIRF_FAKEROOT_STATE" bash -c 'rm -rf '"$DEBIRF_ROOT"'/dev; (cd / && find dev | cpio --create) | (cd '"$DEBIRF_ROOT"'/ && cpio --extract)'   
93        #fakeroot -i "$DEBIRF_FAKEROOT_STATE" -s "$DEBIRF_FAKEROOT_STATE" bash -c 'rm -rf '"$DEBIRF_ROOT"'/dev; mkdir '"$DEBIRF_ROOT"'/dev'
94
95        # create root image
96        ( cd "$DEBIRF_ROOT" && find * | fakeroot cpio --create -H newc | gzip ) > "$1"
97    else
98        # create root image
99        ( cd "$DEBIRF_ROOT" && find * | cpio --create -H newc | gzip ) > "$1"
100    fi
101}
102
103# nested cpio archives
104create_initrd_nested() {
105    local util lib
106    local NEST_ROOT="$DEBIRF_BUILDD"/nest
107
108    # make the nested root
109    rm -rf "$NEST_ROOT"
110    mkdir -p "$NEST_ROOT"/{bin,lib}
111
112    # copy needed executables into nest
113    cp -f /bin/{busybox,cpio} "$NEST_ROOT"/bin/
114    for util in sh mkdir mount ls gunzip ; do
115        ln "$NEST_ROOT"/bin/busybox "$NEST_ROOT"/bin/"$util"
116    done
117    cp -f /usr/lib/klibc/bin/run-init "$NEST_ROOT"/bin/
118
119    # copy in needed libraries
120    for lib in $(ldd "$NEST_ROOT"/bin/* | egrep '*.so.[[:digit:]]+ \(0x[[:xdigit:]]{8}\)$' | sed -r 's|.*[[:space:]](/[^[[:space:]]*)[[:space:]]\(0x[[:xdigit:]]{8}\)$|\1|' | sort -u) ; do
121        # pull libraries from most basic place libraries can live
122        # (avoid arch change between build env and debirf)
123        lib=/lib/$(basename "$lib")
124        echo -e "$lib\n$(readlink -f $lib)" | cpio --pass-through --make-directories "$NEST_ROOT"/
125    done
126    cp -f /lib/klibc-* "$NEST_ROOT"/lib/
127
128    # create nest init
129    cat > "$NEST_ROOT"/init <<EOF
130#!/bin/sh
131mkdir /newroot
132mount -t tmpfs tmpfs /newroot
133cd /newroot
134# specify /bin/cpio so that it gets used instead of the busybox builtin
135gunzip - </rootfs.cgz | /bin/cpio -i
136echo unpacking rootfs...
137exec /bin/run-init . /sbin/init <./dev/console >./dev/console
138EOF
139    chmod a+x "$NEST_ROOT"/init
140
141    msg "creating rootfs.cgz..."
142    create_initrd_stupid_simple "$NEST_ROOT"/rootfs.cgz
143
144    msg "creating wrapper cgz..."
145    ( cd "$NEST_ROOT" && find * | fakeroot cpio --create -H newc | gzip ) > "$1"
146}
147
148# tweak initrd method: untested, probably doesn't work right yet.  soon!
149create_initrd_tweak_initrd() {
150    cat "$DEBIRF_ROOT/scripts/debirf" <<EOF
151mountroot() {
152 rootmnt="/"
153}
154EOF
155    cat > "$DEBIRF_ROOT/bin/run-init" <<EOF
156#!/bin/sh
157if [ "$1" != "/" ] ; then
158  exec /usr/lib/klibc/bin/run-init "$@"
159else
160  MOUNT_POINT="$1"
161  INIT="$2"
162  shift 2
163  exec "$INIT" "$@"
164fi
165EOF
166    # create root image
167    cp initrd.img* "$1"
168    ( cd "$DEBIRF_ROOT" && find * | cpio -H newc --create  | gzip ) >> "$1"
169}
170
171# unionfs method: also untested and probably not working.
172create_initrd_unionfs() {
173    #ln sbin/init to /init
174    ln -sf /sbin/init "$DEBIRF_ROOT/init"
175   
176    # add unionfs kernel module and init script
177    debirf_exec apt-get update
178    debirf_exec apt-get install unionfs-modules-"$KERNAVAIL" unionfs-tools
179
180    cat >"$DEBIRF_ROOT/etc/init.d/root-unionfs" <<EOF
181#!/bin/bash
182
183mount -t tmpfs tmpfs /tmp
184mount -t unionfs dirs=/=ro:/tmp=rw unionfs /
185EOF
186
187    ln -sf ../init.d/root-unionfs /etc/rcS.d/S00root-unionfs
188
189    # create root image
190    ( cd "$DEBIRF_ROOT" && find * | cpio -H newc --create  | gzip ) > "$1"
191}
192
193###############################################################
194### MAIN
195
196# option parsing
197TEMP=$(getopt --options -hfcnosi --longoptions help,fakechroot,check-vars,new,overwrite,skip,initrd-only -n "$CMD" -- "$@")
198
199if [ $? != 0 ] ; then
200    echo "Invalid options." >&2
201    usage
202    exit 1
203fi
204
205# Note the quotes around `$TEMP': they are essential!
206eval set -- "$TEMP"
207
208while true ; do
209    case "$1" in
210        -h|--help)
211            usage
212            exit 0
213            ;;
214        -f|--fakechroot)
215            echo "Using fakechroot build."
216            FAKECHROOT_BUILD=true
217            shift 1
218            ;;
219        -c|--check-vars)
220            CHECK_VARS=true
221            shift 1
222            ;;
223        -n|--new)
224            WRITE_MODE=rewrite
225            shift 1
226            ;;
227        -o|--overwrite)
228            WRITE_MODE=overwrite
229            shift 1
230            ;;
231        -s|--skip)
232            WRITE_MODE=skip
233            shift 1
234            ;;
235        -i|--initrd-only)
236            STAGE_ROOT=false
237            STAGE_PLUGINS=false
238            shift 1
239            ;;
240        --)
241            shift
242            ;;
243        *)
244            if (( $# < 1 )) ; then
245                echo "Improper number of input arguments."
246                usage
247                exit 1
248            fi
249            DEBIRF_PROFILE="$1"
250            if [ "$2" ] ; then
251                DEBIRF_KERNEL_PACKAGE="$2"
252            fi
253            break
254            ;;
255    esac
256done
257
258# source debirf.conf defaults
259source /usr/share/debirf/debirf.conf.defaults
260
261# check profile
262if [ -d "$DEBIRF_PROFILE" ] ; then
263    echo "Loading profile '$DEBIRF_PROFILE'..."
264    DEBIRF_CONF="$DEBIRF_PROFILE/debirf.conf"
265    DEBIRF_PLUGINS="$DEBIRF_PROFILE/plugins"
266else
267    echo "$DEBIRF_PROFILE not found."
268    usage_profile
269    exit 1
270fi
271
272# source profile debirf.conf
273if [ -f "$DEBIRF_CONF" ] ; then
274    source "$DEBIRF_CONF"
275else
276    echo "$DEBIRF_CONF not found."
277    usage_profile
278    exit 1
279fi
280
281# check plugins directory
282if [ ! -d "$DEBIRF_PLUGINS" ] ; then
283    echo "$DEBIRF_PLUGINS does not exist."
284    usage_profile
285    exit 1
286fi
287if [ $(ls "$DEBIRF_PLUGINS" -1 | wc -l) = 0 ] ; then
288    echo "$DEBIRF_PLUGINS is empty."
289    usage_profile
290    exit 1
291fi
292
293# check buildd
294if [ -z "$DEBIRF_BUILDD" ] ; then
295    failure "DEBIRF_BUILDD is not set."
296fi
297
298# check debirf root
299if [ -z "$DEBIRF_ROOT" ] ; then
300    failure "DEBIRF_ROOT is not set."
301fi
302
303# set fakechroot save file
304DEBIRF_FAKEROOT_STATE="$DEBIRF_BUILDD/.fakechroot-$DEBIRF_LABEL.save"
305
306# check variables
307if [ "$CHECK_VARS" ] ; then
308    echo "Debirf variables:"
309    for var in ${!DEBIRF_*}; do
310        if [ $var ] ; then
311            export $var
312        else
313            failure "Variable '$var' not properly set."
314        fi
315    done
316    env | /bin/grep "^DEBIRF_"
317    read -p "enter to continue: " OK
318fi
319
320### BUILD ROOT
321if [ "$STAGE_ROOT" = 'true' ] ; then
322
323# check if specified kernel package exists
324if [ ! -f "$DEBIRF_KERNEL_PACKAGE" ] ; then
325    failure "Kernel package '$DEBIRF_KERNEL_PACKAGE' not found."
326fi
327
328# determine write mode
329if [ -d "$DEBIRF_ROOT" ] ; then
330    echo "Directory $DEBIRF_ROOT already exists."
331    if [ -z "$WRITE_MODE" ] ; then
332        echo "Select one of the following:"
333        select foo in 'new' 'overwrite' 'skip' 'exit' ; do
334            case "$foo" in
335                'new')
336                    WRITE_MODE=rewrite
337                    ;;
338                'overwrite')
339                    WRITE_MODE=overwrite
340                    ;;
341                'skip')
342                    WRITE_MODE=skip
343                    ;;
344                *)
345                    failure "aborting."
346                    ;;
347            esac
348            break
349        done
350    fi
351else
352    WRITE_MODE=new
353fi
354case "$WRITE_MODE" in
355    'new')
356        msg "creating debirf root..."
357        create_debootstrap
358        ;;
359    'rewrite')
360        msg "clearing old debirf root..."
361        rm -rf "$DEBIRF_ROOT"
362        msg "creating debirf root..."
363        create_debootstrap
364        ;;
365    'overwrite')
366        msg "overwriting old debirf root..."
367        create_debootstrap
368        ;;
369    'skip')
370        msg "skipping debootstrap..."
371        ;;
372    *)
373        failure "aborting."
374        ;;
375esac
376else
377    echo "Not building root."
378fi
379### END BUILD ROOT
380
381
382### RUN PLUGINS
383if [ "$STAGE_PLUGINS" = 'true' ] ; then
384    setup_debirf_info
385    msg "executing plugins..."
386    run_plugins
387    msg "plugins complete."
388else
389    echo "Not running plugins."
390fi
391### END RUN PLUGINS
392
393
394### BUILD INITRD
395if [ "$STAGE_INITRD" = 'true' ] ; then
396    if [ ! -d "$DEBIRF_ROOT" ] ; then
397        failure "Root not found."
398    fi
399    # determine initrd name
400    KERNEL_VERS=$(ls -1 "$DEBIRF_ROOT/lib/modules" | head -n1)
401    DEBIRF_INITRD="${DEBIRF_LABEL}_${DEBIRF_DISTRO}_${KERNEL_VERS}.cgz"
402
403    msg "creating debirf initrd ('$DEBIRF_METHOD')..."
404    create_initrd_${DEBIRF_METHOD} "$DEBIRF_BUILDD/$DEBIRF_INITRD"
405
406    # final output
407    DEBIRF_KERNEL=$(ls "$DEBIRF_BUILDD" | grep "vmlinu" | grep "$KERNEL_VERS$")
408    msg "debirf initrd created."
409    if [ "$DEBIRF_BUILDD/$DEBIRF_KERNEL" ] ; then
410        msg "kernel: $DEBIRF_BUILDD/$DEBIRF_KERNEL"
411    fi
412    msg "initrd: $DEBIRF_BUILDD/$DEBIRF_INITRD"
413else
414    echo "Not creating initramfs."
415fi
416### END BUILD INITRD
Note: See TracBrowser for help on using the repository browser.