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

Last change on this file since 775 was 775, checked in by jrollins, 6 years ago

debirf: see changelog

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