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

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

debirf: install-kernel plugin now seems to be fully functional

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