Support execution without touching the chroot at all using option "-B"
[grml-live.git] / grml-live
1 #!/bin/bash
2 # Filename:      grml-live
3 # Purpose:       build process script for generating a (grml based) Linux Live-ISO
4 # Authors:       grml-team (grml.org), (c) Michael Prokop <mika@grml.org>
5 # Bug-Reports:   see http://grml.org/bugs/
6 # License:       This file is licensed under the GPL v2 or any later version.
7 ################################################################################
8
9 # read configuration files, set some misc variables {{{
10
11 export LANG=C
12 export LC_ALL=C
13
14 # exit on any error:
15 set -e
16
17 GRML_LIVE_VERSION='0.9.7'
18 PN="$(basename $0)"
19 CMDLINE="$0 $@"
20 ISO_DATE="$(date +%Y-%m-%d)"
21
22 # usage information {{{
23 usage()
24 {
25   echo "
26 $PN - build process script for generating a (grml based) Linux Live-ISO
27
28 Usage: $PN [options, see as follows]
29
30    -a <architecture>       architecture; available values: i386 and amd64
31    -b                      build the ISO without updating the chroot via FAI
32    -B                      build the ISO without touching the chroot (skips cleanup)
33    -c <classe[s]>          classes to be used for building the ISO via FAI
34    -C <configfile>         configuration file for grml-live
35    -F                      force execution without prompting
36    -g <grml_name>]         set the grml flavour name
37    -h                      display short usage information and exit
38    -i <iso_name>           name of ISO
39    -o <output_directory>   main output directory of the build process
40    -q                      skip mksquashfs
41    -r                      release name
42    -s <suite>              Debian suite; values: etch, lenny, sid
43    -t <template_directory> place of the templates
44    -u                      update existing chroot instead of rebuilding it from scratch
45    -v <version_number>     specify version number of the release
46    -V                      increase verbosity in the build process
47    -z                      use ZLIB instead of LZMA compression (depends on
48                            squashfs-tools version)
49
50 Usage examples:
51
52     $PN
53     $PN -c GRMLBASE,GRML_MEDIUM,I386 -o /dev/shm/grml
54     $PN -c GRMLBASE,GRML_SMALL,REMOVE_DOCS,I386 -g grml-small -v 1.0
55     $PN -c GRMLBASE,GRML_FULL,I386 -i grml_0.0-1.iso -v 0.0-1
56     $PN -c GRMLBASE,GRML_FULL,I386 -s sid -V -r 'grml-live rocks'
57
58 More details: man grml-live + /usr/share/doc/grml-live/grml-live.html
59               http://grml.org/grml-live/
60
61 Please send your bug reports and feedback to the grml-team: http://grml.org/bugs/
62 "
63 }
64
65 # make sure it's possible to get usage information without being
66 # root or actually executing the script
67 if [ "$1" = '-h' -o "$1" = '--help' ] ; then
68    usage
69    [ "$(id -u 2>/dev/null)" != 0 ] && echo "Please notice that this script requires root permissions."
70    exit 0
71 fi
72 # }}}
73
74 # we need root permissions for the build-process:
75 if [ "$(id -u 2>/dev/null)" != 0 ] ; then
76    echo "Error: please run this script with uid 0 (root)." >&2
77    exit 1
78 fi
79
80 if [ -r /var/run/fai/FAI_INSTALLATION_IN_PROGRESS ] ; then
81    echo "/usr/sbin/fai already running or was aborted before.">&2
82    echo "You may remove /var/run/fai/FAI_INSTALLATION_IN_PROGRESS and try again.">&2
83    exit 1
84 fi
85
86 # see #449236
87 if [ -r /var/run/fai/fai_softupdate_is_running ] ; then
88    echo "/usr/sbin/fai softupdate already running or was aborted before.">&2
89    echo "You may remove /var/run/fai/fai_softupdate_is_running and try again.">&2
90    exit 1
91 fi
92
93 # make sure they are not set by default
94 VERBOSE=''
95 FORCE=''
96 UPDATE=''
97 BUILD_ONLY=''
98 BUILD_DIRTY=''
99 HOSTNAME=''
100
101 if [ -r /etc/grml/lsb-functions ] ; then
102    . /etc/grml/lsb-functions
103 else
104    einfo()  { echo "  [*] $*" ;}
105    eerror() { echo "  [!] $*">&2 ;}
106    ewarn()  { echo "  [x] $*" ;}
107    eend()   { return 0 ;}
108 fi
109
110 # source main configuration file:
111 LIVE_CONF=/etc/grml/grml-live.conf
112 . $LIVE_CONF
113
114 # }}}
115
116 # clean exit {{{
117 bailout() {
118   rm -f /var/run/fai/fai_softupdate_is_running \
119         /var/run/fai/FAI_INSTALLATION_IN_PROGRESS
120   [ -n "$MIRROR_DIRECTORY" ] && umount "${CHROOT_OUTPUT}/${MIRROR_DIRECTORY}"
121   [ -n "$1" ] && EXIT="$1" || EXIT="1"
122   [ -n "$2" ] && eerror "$2">&2
123   log "------------------------------------------------------------------------------"
124   exit "$EXIT"
125 }
126 trap bailout 1 2 3 3 6 9 14 15
127 # }}}
128
129 # check for important variables {{{
130 [ -n "$GRML_FAI_CONFIG" ] || GRML_FAI_CONFIG=/etc/grml/fai
131 [ -n "$HOSTNAME" ] || HOSTNAME=grml
132 [ -n "$USERNAME" ] || USERNAME=grml
133 [ -n "$CLASSES" ]  || CLASSES="GRML,I386"
134 [ -n "$BOOT_METHOD" ] || BOOT_METHOD='isolinux'
135 [ -n "$OUTPUT" ] || bailout 1 "${PN}: \$OUTPUT not specified. Please adjust $LIVE_CONF. Exiting."
136
137 [ -n "$VERSION" ]  || VERSION="0.0.1"
138 [ -n "$RELEASENAME" ] || RELEASENAME="grml-live rocks"
139 [ -n "$GRML_NAME" ] || GRML_NAME='grml'
140
141 # logfile:
142 if [ -z "$LOGFILE" ] ; then
143    LOGFILE=/var/log/grml-live.log
144 fi
145 touch $LOGFILE
146 chown root:adm $LOGFILE
147 chmod 664 $LOGFILE
148
149 NFSROOT_CONF=/etc/grml/fai/make-fai-nfsroot.conf
150
151 # }}}
152
153 # some important functions {{{
154
155 # log output:
156 # usage: log "string to log"
157 log() { echo "$*" >> $LOGFILE ; }
158
159 # cut string at character number int = $1
160 # usage: cut_string 5 "1234567890" will output "12345"
161 cut_string() {
162   [ -n "$2" ] || return 1
163   echo "$2" | head -c "$1"; echo -ne "\n"
164 }
165
166 # prepend int = $1 spaces before string = $2
167 # usage: extend_string_begin 5 "123" will output "  123"
168 extend_string_begin() {
169   [ -n "$2" ] || return 1
170   local COUNT="$(echo $2 | wc -c)"
171   local FILL="$(expr $COUNT - $1)"
172   while [ "$FILL" -gt 1 ] ; do
173     echo -n " "
174     local FILL=$(expr $FILL - 1)
175   done
176   while [ "$FILL" -lt 1 ] ; do
177     echo -n " "
178     local FILL=$(expr $FILL + 1)
179   done
180   echo "$2" | head -c "$1"; echo -ne "\n"
181 }
182
183 # append int = $1 spaces to string = $2
184 # usage: extend_string_begin 5 "123" will output "123  "
185 extend_string_end() {
186   [ -n "$2" ] || return 1
187   echo -n "$2" | head -c "$1"
188   local COUNT="$(echo $2 | wc -c)"
189   local FILL="$(expr $COUNT - $1)"
190   while [ "$FILL" -gt 1 ] ; do
191     echo -n " "
192     local FILL=$(expr $FILL - 1)
193   done
194   while [ "$FILL" -lt 1 ] ; do
195     echo -n " "
196     local FILL=$(expr $FILL + 1)
197   done
198   echo -ne "\n"
199 }
200 # }}}
201
202 # read local (non-packaged) configuration {{{
203 LOCAL_CONFIG=/etc/grml/grml-live.local
204 if [ -r "$LOCAL_CONFIG" ] ; then
205    log "Sourcing $LOCAL_CONFIG"
206    . $LOCAL_CONFIG
207 else
208    log "No $LOCAL_CONFIG found, not sourcing it"
209    LOCAL_CONFIG=''
210 fi
211 # }}}
212
213 # command line parsing {{{
214 while getopts "a:C:c:g:i:o:r:s:t:v:bBFuqVz" opt; do
215   case "$opt" in
216     a) ARCH="$OPTARG" ;;
217     b) BUILD_ONLY=1 ;;
218     B) BUILD_DIRTY=1 ;;
219     c) CLASSES="$OPTARG" ;;
220     C) CONFIG="$OPTARG" ;;
221     g) GRML_NAME="$OPTARG" ;;
222     i) ISO_NAME="$OPTARG" ;;
223     o) OUTPUT="$OPTARG"
224        CHROOT_OUTPUT="$OUTPUT/grml_chroot"
225        BUILD_OUTPUT="$OUTPUT/grml_cd"
226        ISO_OUTPUT="$OUTPUT/grml_isos"
227        ;;
228     q) SKIP_MKSQUASHFS=1 ;;
229     r) RELEASENAME="$OPTARG" ;;
230     s) SUITE="$OPTARG" ;;
231     t) TEMPLATE_DIRECTORY="$OPTARG";;
232     v) VERSION="$OPTARG" ;;
233     F) FORCE=1 ;;
234     u) UPDATE=1 ;;
235     V) VERBOSE="-v" ;;
236     z) SQUASHFS_ZLIB="-nolzma" ;;
237     ?) echo "invalid option -$OPTARG" >&2; bailout 1 ;;
238   esac
239 done
240 shift $(($OPTIND - 1))  # set ARGV to the first not parsed commandline parameter
241 # }}}
242
243 # some misc checks before executing FAI {{{
244 [ -n "$CLASSES" ] || bailout 1 "Error: \$CLASSES unset, please set it in $LIVE_CONF or
245 specify it on the command line using the -c option."
246 [ -n "$OUTPUT" ] || bailout 1 "Error: \$OUTPUT unset, please set it in $LIVE_CONF or
247 specify it on the command line using the -o option."
248
249 # trim characters that are known to cause problems inside $GRML_NAME;
250 # for example isolinux does not like '-' inside the directory name
251 [ -n "$GRML_NAME" ] && export SHORT_GRML_NAME="$(echo $GRML_NAME | tr -d ',./;\- ')"
252
253 # export variables to have them available in fai scripts:
254 [ -n "$GRML_NAME" ]   && export GRML_NAME="$GRML_NAME"
255 [ -n "$RELEASENAME" ] && export RELEASENAME="$RELEASENAME"
256 # }}}
257
258 # clean/zero grml-live logfile {{{
259 if [ -n "$ZERO_LOGFILE" ] ; then
260    echo -n > $LOGFILE
261 fi
262 # }}}
263
264 # clean/zero/remove old FAI directory {{{
265 if [ -n "$ZERO_FAI_LOGFILE" ] ; then
266    if [ -d /var/log/fai/"$HOSTNAME" ] ; then
267       rm -rf /var/log/fai/"$HOSTNAME"/"$(readlink /var/log/fai/"$HOSTNAME"/last)"
268       rm -rf /var/log/fai/"$HOSTNAME"/"$(readlink /var/log/fai/"$HOSTNAME"/last-dirinstall)"
269       rm -rf /var/log/fai/"$HOSTNAME"/"$(readlink /var/log/fai/"$HOSTNAME"/last-softupdate)"
270       rm -f /var/log/fai/"$HOSTNAME"/last \
271             /var/log/fai/"$HOSTNAME"/last-dirinstall \
272             /var/log/fai/"$HOSTNAME"/last-softupdate
273    fi
274 fi
275 # }}}
276
277 # ask user whether the setup is ok {{{
278 if [ -z "$FORCE" ] ; then
279    echo
280    echo "${PN} [${GRML_LIVE_VERSION}]: check your configuration (or use -F to force execution):"
281    echo
282    echo "  FAI classes:       $CLASSES"
283    [ -r "$LOCAL_CONFIG" ]       && echo "  local config:      /etc/grml/grml-live.local"
284    [ -n "$CONFIG" ]             && echo "  configuration:     $CONFIG"
285    echo "  main directory:    $OUTPUT"
286    [ -n "$CHROOT_OUTPUT" ]      && echo "  chroot target:     $CHROOT_OUTPUT"
287    [ -n "$BUILD_OUTPUT" ]       && echo "  build target:      $BUILD_OUTPUT"
288    [ -n "$ISO_OUTPUT" ]         && echo "  ISO target:        $ISO_OUTPUT"
289    [ -n "$GRML_NAME" ]          && echo "  grml name:         $GRML_NAME"
290    [ -n "$RELEASENAME" ]        && echo "  release name:      $RELEASENAME"
291    [ -n "$VERSION" ]            && echo "  grml version:      $VERSION"
292    [ -n "$SUITE" ]              && echo "  Debian suite:      $SUITE"
293    [ -n "$ARCH" ]               && echo "  Architecture:      $ARCH"
294    [ -n "$BOOT_METHOD" ]        && echo "  Boot method:       $BOOT_METHOD"
295    [ -n "$TEMPLATE_DIRECTORY" ] && echo "  Template files:    $TEMPLATE_DIRECTORY"
296    [ -n "$FAI_ARGS" ]           && echo "  additional arguments for FAI: $FAI_ARGS"
297    [ -n "$LOGFILE" ]            && echo "  Logging to file:   $LOGFILE"
298    [ -n "$SQUASHFS_ZLIB" ]      && echo "  Using ZLIB (instead of LZMA) compression."
299    [ -n "$SQUASHFS_OPTIONS" ]   && echo "  Using SQUASHFS_OPTIONS ${SQUASHFS_OPTIONS}"
300    [ -n "$VERBOSE" ]            && echo "  Using VERBOSE mode."
301    [ -n "$UPDATE" ]             && echo "  Executing UPDATE instead of fresh installation."
302    [ -n "$SKIP_MKSQUASHFS" ]    && echo "  Skipping creation of SQUASHFS file."
303    [ -n "$BUILD_ONLY" ]         && echo "  Executing BUILD_ONLY instead of fresh installation or UPDATE."
304    [ -n "$BUILD_DIRTY" ]        && echo "  Executing BUILD_DIRTY to leave chroot untouched."
305    echo
306    echo -n "Is this ok for you? [y/N] "
307    read a
308    if ! [ "$a" = 'y' -o "$a" = 'Y' ] ; then
309       bailout 1 "Exiting as requested."
310    fi
311    echo
312 fi
313
314 if [ -n "$CONFIG" ] ; then
315    if ! [ -f "$CONFIG" ] ; then
316       log "Sorry, $CONFIG could not be read. Exiting. [$(date)]"
317       eerror "Sorry, $CONFIG could not be read. Exiting."
318       bailout 1
319    else
320       log "Sourcing $CONFIG"
321       . $CONFIG
322    fi
323 fi
324
325 start_seconds=$(cut -d . -f 1 /proc/uptime)
326 log "------------------------------------------------------------------------------"
327 log "Starting grml-live [${GRML_LIVE_VERSION}] run on $(date)"
328 log "Executed grml-live command line:"
329 log "$CMDLINE"
330
331 einfo "Logging actions to logfile $LOGFILE"
332 # }}}
333
334 # on-the-fly configuration {{{
335 if [ -n "$MIRROR_DIRECTORY" ] ; then
336    if ! [ -d "$MIRROR_DIRECTORY/debian" ] ; then
337       log "Sorry, $MIRROR_DIRECTORY/debian does not seem to exist. Exiting. [$(date)]"
338       eerror "Sorry, $MIRROR_DIRECTORY/debian does not seem to exist. Exiting."
339       bailout 1
340    fi
341    echo "$MIRROR_SOURCES" > /etc/grml/fai/apt/sources.list
342    if [ -n "$GRML_LIVE_SOURCES" ] ; then
343       echo "$GRML_LIVE_SOURCES" >> /etc/grml/fai/apt/sources.list
344    fi
345 elif [ -n "$GRML_LIVE_SOURCES" ] ; then
346    echo "$GRML_LIVE_SOURCES" > /etc/grml/fai/apt/sources.list
347 fi
348
349 if [ -n "$FAI_DEBOOTSTRAP" ] ; then
350    sed -i "s#^FAI_DEBOOTSTRAP=.*#FAI_DEBOOTSTRAP=\"$FAI_DEBOOTSTRAP\"#" $NFSROOT_CONF
351 fi
352
353 # does this suck? YES!
354 if [ -n "$SUITE" ] ; then
355
356    for file in "$LIVE_CONF" "$CONFIG" "$LOCAL_CONFIG" ; do
357        if [ -n "$file" ] ; then
358           sed -i "s/SUITE=.*/SUITE=\"$SUITE\"/" $LIVE_CONF
359           DIST="\|\ etch\ \|\ stable\ \|\ lenny\ \|\ testing\ \|\ sid\ \|\ unstable\ "
360           sed -i "s/\(deb .\+\)\([ \t]+\)$DIST\([ \t]+\)\(main \)/\1\2 $SUITE \3\4/" $file
361        fi
362    done
363
364    sed -i "s/\(deb .\+\)\([ \t]+\)$DIST\([ \t]+\)\(main \)/\1\2 $SUITE \3\4/" /etc/grml/fai/apt/sources.list
365    # notice: activate grml-live pool only if we are building against unstable:
366    if grep -qe unstable -qe sid /etc/grml/fai/apt/sources.list ; then
367       grep -q 'grml-live.*main' /etc/grml/fai/apt/sources.list || \
368       grep grml-stable /etc/grml/fai/apt/sources.list | \
369            sed 's/grml-stable/grml-live/' >> /etc/grml/fai/apt/sources.list
370    else
371       grep -q 'grml-live.*main' /etc/grml/fai/apt/sources.list && \
372       sed -i 's/.*grml-live.*/# removed grml-live repository/' /etc/grml/fai/apt/sources.list
373    fi
374
375    for file in "$LIVE_CONF" "$CONFIG" "$LOCAL_CONFIG" ; do
376        if [ -n "$file" ] ; then
377           sed -i "s|FAI_DEBOOTSTRAP=\"[a-z]* |FAI_DEBOOTSTRAP=\"$SUITE |" "$file"
378        fi
379    done
380    sed -i "s|FAI_DEBOOTSTRAP=\"[a-z]* |FAI_DEBOOTSTRAP=\"$SUITE |" $NFSROOT_CONF
381 fi
382
383 # set $ARCH
384 [ -n "$ARCH" ] || ARCH="$(dpkg --print-architecture)"
385 if grep -q -- 'FAI_DEBOOTSTRAP_OPTS.*--arch' $NFSROOT_CONF ; then
386    sed -i "s/--arch [a-z0-9]* /--arch $ARCH /" $NFSROOT_CONF
387 else
388    sed -i "s|FAI_DEBOOTSTRAP_OPTS=\"\(.*\)|FAI_DEBOOTSTRAP_OPTS=\"--arch $ARCH \1|" $NFSROOT_CONF
389 fi
390 # }}}
391
392 # CHROOT_OUTPUT - execute FAI {{{
393 if [ -n "$BUILD_DIRTY" ]; then
394   einfo "Skipping FAI" ; eend 0
395 else
396    [ -n "$CHROOT_OUTPUT" ] || CHROOT_OUTPUT="$OUTPUT/grml_chroot"
397
398    if [ -n "$UPDATE" -o -n "$BUILD_ONLY" ] ; then
399       FAI_ACTION=softupdate
400    else
401       FAI_ACTION=dirinstall
402    fi
403
404    if [ -n "$UPDATE" -o -n "$BUILD_ONLY" ] ; then
405       if ! [ -r "$CHROOT_OUTPUT/etc/grml_version" ] ; then
406          log "Error: does not look like you have a working chroot. Updating/building not possible."
407          eerror "Error: does not look like you have a working chroot. Updating/building not possible. (Drop -u/-b option?)"
408          eend 1
409          bailout 20
410       fi
411    fi
412
413    if [ -d "$CHROOT_OUTPUT/bin" -a -z "$UPDATE" -a -z "$BUILD_ONLY" ] ; then
414       log "$CHROOT_OUTPUT exists already, skipping stage 'fai dirinstall'"
415       ewarn "$CHROOT_OUTPUT exists already, skipping stage 'fai dirinstall'" ; eend 0
416    else
417       mkdir -p "$CHROOT_OUTPUT" || bailout 5 "Problem with creating $CHROOT_OUTPUT for FAI"
418
419       if [ -n "${MIRROR_DIRECTORY}" ] ; then
420          mkdir -p "${CHROOT_OUTPUT}/${MIRROR_DIRECTORY}"
421          mount --bind "${MIRROR_DIRECTORY}" "${CHROOT_OUTPUT}/${MIRROR_DIRECTORY}"
422       fi
423
424       log "Executed FAI command line:"
425       log "BUILD_ONLY=$BUILD_ONLY fai $VERBOSE -C $GRML_FAI_CONFIG -c$CLASSES -u $HOSTNAME $FAI_ACTION $CHROOT_OUTPUT $FAI_ARGS"
426       BUILD_ONLY="$BUILD_ONLY" fai $VERBOSE -C "$GRML_FAI_CONFIG" -c"$CLASSES" -u \
427       "$HOSTNAME" $FAI_ACTION "$CHROOT_OUTPUT" $FAI_ARGS | tee -a $LOGFILE
428       RC="$PIPESTATUS" # notice: bash-only
429
430       if [ "$RC" != 0 ] ; then
431          log "Error while executing fai [exit code ${RC}]. Exiting."
432          eerror "Error while executing fai [exit code ${RC}]. Exiting." ; eend 1
433          bailout 1
434       else
435          log "Setting /etc/grml_version to $GRML_NAME $VERSION Release Codename $RELEASENAME [$ISO_DATE]"
436          echo "$GRML_NAME $VERSION Release Codename $RELEASENAME [$ISO_DATE]" > $CHROOT_OUTPUT/etc/grml_version
437          chmod 644 $CHROOT_OUTPUT/etc/grml_version
438          einfo "Rebuilding initramfs"
439          # make sure new /etc/grml_version reaches the initramfs:
440          chroot $CHROOT_OUTPUT update-initramfs -u -t
441          eend $?
442       fi
443
444       # Remove all FAI logs from chroot if class RELEASE is used:
445       if [ -f "$CHROOT_OUTPUT"/etc/grml_fai_release ] ; then
446          rm -rf "$CHROOT_OUTPUT"/var/log/fai/*
447       fi
448
449       # make sure we don't leave any mounts - FAI doesn't remove them always
450       umount $CHROOT_OUTPUT/proc 2>/dev/null || /bin/true
451       umount $CHROOT_OUTPUT/sys  2>/dev/null || /bin/true
452       umount $CHROOT_OUTPUT/dev/pts 2>/dev/null || /bin/true
453       umount $CHROOT_OUTPUT/dev 2>/dev/null || /bin/true
454
455       [ -n "$MIRROR_DIRECTORY" ] && umount "${CHROOT_OUTPUT}/${MIRROR_DIRECTORY}"
456
457       # notice: 'fai dirinstall' does not seem to exit appropriate, so:
458       ERROR=''
459       CHECKLOG=/var/log/fai/$HOSTNAME/last
460       if [ -r "$CHECKLOG/software.log" ] ; then
461          # 1 errors during executing of commands
462          grep 'dpkg: error processing' $CHECKLOG/software.log >> $LOGFILE && ERROR=1
463          grep 'E: Method http has died unexpectedly!' $CHECKLOG/software.log >> $LOGFILE && ERROR=2
464          grep 'ERROR: chroot' $CHECKLOG/software.log >> $LOGFILE && ERROR=3
465          grep 'E: Failed to fetch' $CHECKLOG/software.log >> $LOGFILE && ERROR=4
466          grep 'Unable to write mmap - msync (28 No space left on device)' $CHECKLOG/software.log >> $LOGFILE && ERROR=5
467       fi
468
469       if [ -r "$CHECKLOG/shell.log" ] ; then
470          grep 'FAILED with exit code' $CHECKLOG/shell.log >> $LOGFILE && ERROR=2
471       fi
472
473       if [ -n "$ERROR" ] ; then
474          log "There was an error [${ERROR}] during execution of stage 'fai dirinstall' [$(date)]"
475          eerror "There was an error during execution of stage 'fai dirinstall'"
476          echo "   Check out ${CHECKLOG}/ for details. [exit ${ERROR}]"
477          eend 1
478          bailout 1
479       else
480          log "Finished execution of stage 'fai dirinstall' [$(date)]"
481          einfo "Finished execution of stage 'fai dirinstall'"
482       fi
483
484    fi
485 fi # BUILD_DIRTY?
486 # }}}
487
488 # BUILD_OUTPUT - execute arch specific stuff and squashfs {{{
489 [ -n "$BUILD_OUTPUT" ] || BUILD_OUTPUT="$OUTPUT/grml_cd"
490 mkdir -p "$BUILD_OUTPUT" || bailout 6 "Problem with creating $BUILD_OUTPUT for stage ARCH"
491
492 # i386:
493 if [ "$ARCH" = i386 ] || [ "$ARCH" = amd64 ] ; then
494    if [ -d "$BUILD_OUTPUT"/boot -a -z "$UPDATE" -a -z "$BUILD_ONLY" ] ; then
495       log "$BUILD_OUTPUT/boot exists already, skipping stage 'boot'"
496       ewarn "$BUILD_OUTPUT/boot exists already, skipping stage 'boot'" ; eend 0
497    else
498       # booting stuff:
499       [ -d "$BUILD_OUTPUT"/boot/isolinux ] || mkdir -p "$BUILD_OUTPUT"/boot/isolinux
500       [ -d "$BUILD_OUTPUT"/boot/"${SHORT_GRML_NAME}" ] || mkdir -p "$BUILD_OUTPUT"/boot/"${SHORT_GRML_NAME}"
501
502       if [ -z "$NO_ADDONS" ] ; then
503          [ -d "$BUILD_OUTPUT"/boot/addons   ] || mkdir -p "$BUILD_OUTPUT"/boot/addons
504          cp /boot/memtest86+.bin "$BUILD_OUTPUT"/boot/addons/memtest
505       fi
506
507       # if we don't have an initrd we a) can't boot and b) there was an error
508       # during build, so check for the file:
509       INITRD="$(ls $CHROOT_OUTPUT/boot/initrd* 2>/dev/null| grep -v '.bak$' | sort -r | head -1)"
510       if [ -n "$INITRD" ] ; then
511          cp $INITRD "$BUILD_OUTPUT"/boot/"${SHORT_GRML_NAME}"/initrd.gz
512          find $CHROOT_OUTPUT/boot/ -name initrd\*.bak -exec rm {} \;
513       else
514          log "No initrd found inside $CHROOT_OUTPUT/boot/ - Exiting"
515          eerror "No initrd found inside $CHROOT_OUTPUT/boot/ - Exiting" ; eend 1
516          bailout 10
517       fi
518
519       KERNEL_IMAGE="$(ls $CHROOT_OUTPUT/boot/vmlinuz* 2>/dev/null | sort -r | head -1)"
520       if [ -n "$KERNEL_IMAGE" ] ; then
521          cp "$KERNEL_IMAGE" "$BUILD_OUTPUT"/boot/"${SHORT_GRML_NAME}"/linux26
522       else
523          log "No kernel found inside $CHROOT_OUTPUT/boot/ - Exiting"
524          eerror "No kernel found inside $CHROOT_OUTPUT/boot/ - Exiting" ; eend 1
525          bailout 11
526       fi
527
528       [ -n "$TEMPLATE_DIRECTORY" ] || TEMPLATE_DIRECTORY='/usr/share/grml-live/templates'
529       if ! [ -d "${TEMPLATE_DIRECTORY}"/boot ] ; then
530          log "${TEMPLATE_DIRECTORY}/boot does not exist. Exiting."
531          eerror "${TEMPLATE_DIRECTORY}/boot does not exist. Exiting." ; eend 1
532          bailout 8
533       fi
534
535       cp ${TEMPLATE_DIRECTORY}/boot/isolinux/*  "$BUILD_OUTPUT"/boot/isolinux/
536
537       if [ -z "$NO_ADDONS" ] ; then
538          cp ${TEMPLATE_DIRECTORY}/boot/addons/*    "$BUILD_OUTPUT"/boot/addons/
539       fi
540
541       if ! [ -d "${BUILD_OUTPUT}/boot/grub" ] ; then
542          cp -a ${TEMPLATE_DIRECTORY}/boot/grub  "$BUILD_OUTPUT"/boot/
543       fi
544
545       if ! [ -d "${TEMPLATE_DIRECTORY}"/GRML ] ; then
546          log "${TEMPLATE_DIRECTORY}/GRML does not exist. Exiting."
547          eerror "${TEMPLATE_DIRECTORY}/GRML does not exist. Exiting." ; eend 1
548          bailout 9
549       fi
550
551       [ -d "$BUILD_OUTPUT"/GRML ] || mkdir "$BUILD_OUTPUT"/GRML
552       cp -a ${TEMPLATE_DIRECTORY}/GRML/* "$BUILD_OUTPUT"/GRML/
553
554       # adjust boot splash information:
555       RELEASE_INFO="$GRML_NAME $VERSION - Release Codename $RELEASENAME"
556       RELEASE_INFO="$(cut_string 68 "$RELEASE_INFO")"
557       RELEASE_INFO="$(extend_string_end 68 "$RELEASE_INFO")"
558
559       sed -i "s/%RELEASE_INFO%/$GRML_NAME $VERSION - $RELEASENAME/" "$BUILD_OUTPUT"/GRML/grml-version
560       sed -i "s/%DATE%/$ISO_DATE/"             "$BUILD_OUTPUT"/GRML/grml-version
561
562       sed -i "s/%RELEASE_INFO%/$RELEASE_INFO/" "$BUILD_OUTPUT"/boot/isolinux/boot.msg
563       sed -i "s/%DATE%/$ISO_DATE/"             "$BUILD_OUTPUT"/boot/isolinux/boot.msg
564
565       sed -i "s/%GRML_NAME%/$SHORT_GRML_NAME/" "$BUILD_OUTPUT"/boot/isolinux/isolinux.cfg
566       sed -i "s/%GRML_NAME%/$SHORT_GRML_NAME/" "$BUILD_OUTPUT"/boot/isolinux/syslinux.cfg
567
568       sed -i "s/%RELEASE_INFO%/$RELEASE_INFO/" "$BUILD_OUTPUT"/boot/isolinux/boot-beep.msg
569       sed -i "s/%DATE%/$ISO_DATE/"             "$BUILD_OUTPUT"/boot/isolinux/boot-beep.msg
570
571       sed -i "s/%VERSION%/$VERSION/"           "$BUILD_OUTPUT"/boot/grub/menu.lst
572       sed -i "s/%GRML_NAME%/$SHORT_GRML_NAME/" "$BUILD_OUTPUT"/boot/grub/menu.lst
573
574       # make sure the squashfs filename is set accordingly:
575       GRML_NAME_SQUASHFS="$GRML_NAME.squashfs"
576       sed -i "s/%GRML_NAME_SQUASHFS%/$GRML_NAME_SQUASHFS/" "$BUILD_OUTPUT"/boot/isolinux/isolinux.cfg
577       sed -i "s/%GRML_NAME_SQUASHFS%/$GRML_NAME_SQUASHFS/" "$BUILD_OUTPUT"/boot/isolinux/syslinux.cfg
578
579       GRML_NAME_SQUASHFS="$(cut_string 20 "$GRML_NAME_SQUASHFS")"
580       GRML_NAME_SQUASHFS="$(extend_string_end 20 "$GRML_NAME_SQUASHFS")"
581       sed -i "s/%GRML_NAME_SQUASHFS%/$GRML_NAME_SQUASHFS/" "$BUILD_OUTPUT"/boot/isolinux/f4
582
583       # autostart for Windows:
584       if [ -d "${TEMPLATE_DIRECTORY}/windows/autostart/" ] ; then
585          cp ${TEMPLATE_DIRECTORY}/windows/autostart/* "$BUILD_OUTPUT"/
586       fi
587
588       # windows-binaries:
589       if [ -n "$WINDOWS_BINARIES" ] ; then
590          if [ -f "$BUILD_OUTPUT"/windows/putty.exe ] ; then
591             log "$BUILD_OUTPUT/windows exists already, skipping stage 'WINDOWS_BINARIES'"
592             ewarn "$BUILD_OUTPUT/windows exists already, skipping stage 'WINDOWS_BINARIES'" ; eend 0
593          else
594             if ! [ -d "$BUILD_OUTPUT"/windows ] ; then
595                mkdir "$BUILD_OUTPUT"/windows
596                ( cd "$BUILD_OUTPUT"/windows
597                  for file in pageant plink pscp psftp putty puttygen ; do
598                     wget -O ${file}.exe ${WINDOWS_BINARIES}/${file}.exe
599                     md5sum ${file}.exe > ${file}.exe.md5
600                  done )
601             fi
602          fi
603       log "Finished execution of stage 'WINDOWS_BINARIES' [$(date)]"
604       einfo "Finished execution of stage 'WINDOWS_BINARIES'" ; eend 0
605       fi
606    einfo "Finished execution of stage 'boot'" ; eend 0
607    fi
608 # ppc:
609 elif [ "$ARCH" = powerpc ] ; then
610     ewarn 'Warning: formorer, it is your turn. :)'>&2
611 # unsuported:
612 else
613    eerror 'Error: Unsupported ARCH, sorry. Want to support it? Contribute!' ; eend 1
614 fi
615
616 if [ -f "$BUILD_OUTPUT"/live/${GRML_NAME}.squashfs -a -z "$UPDATE" -a -z "$BUILD_ONLY" -a -z "$BUILD_DIRTY" ] ; then
617    log "$BUILD_OUTPUT/live exists already, skipping stage 'squashfs'"
618    ewarn "$BUILD_OUTPUT/live exists already, skipping stage 'squashfs'" ; eend 0
619 elif [ -f "$BUILD_OUTPUT"/live/${GRML_NAME}.squashfs -a -n "$SKIP_MKSQUASHFS" ] ; then
620    log "$BUILD_OUTPUT/live exists already, skipping stage 'squashfs' as requested"
621    ewarn "$BUILD_OUTPUT/live exists already, skipping stage 'squashfs' as requested" ; eend 0
622 else
623    [ -d "$BUILD_OUTPUT"/live ] || mkdir "$BUILD_OUTPUT"/live
624    # make sure we don't leave (even an empty) base.tgz:
625    [ -f "$CHROOT_OUTPUT/base.tgz" ] && rm -f "$CHROOT_OUTPUT/base.tgz"
626
627    # make sure mksquashfs can handle the according option:
628    if [ -n "$SQUASHFS_ZLIB" ] ; then
629       mksquashfs --help 2>&1 | grep -q -- "$SQUASHFS_ZLIB" || SQUASHFS_ZLIB=''
630    fi
631
632    if echo "$SQUASHFS_OPTIONS" | grep -q -- "-nolzma" ; then
633       if ! mksquashfs --help 2>&1 | grep -q -- '-nolzma' ; then
634          ewarn "mksquashfs does NOT support the nolzma option, just using default zlib mode."
635          SQUASHFS_OPTIONS="$(echo $SQUASHFS_OPTIONS | sed 's/-nolzma//g')"
636          eend 0
637       fi
638    fi
639
640    if echo "$SQUASHFS_OPTIONS" | grep -q -- "-lzma" ; then
641       if ! mksquashfs --help 2>&1 | grep -q -- '-lzma' ; then
642          ewarn "mksquashfs does NOT support the lzma option, falling back to zlib mode."
643          SQUASHFS_OPTIONS="$(echo $SQUASHFS_OPTIONS | sed 's/-lzma//g')"
644          eend 0
645       fi
646    fi
647
648    # support exclusion of files via exclude-file:
649    if [ -n "$SQUASHFS_EXCLUDES_FILE" -a "$SQUASHFS_EXCLUDES_FILE" ] ; then
650       SQUASHFS_OPTIONS="$SQUASHFS_OPTIONS -ef $SQUASHFS_EXCLUDES_FILE"
651    fi
652
653    # get rid of unnecessary files when building grml-small for final release:
654    if echo "$CLASSES" | grep -q GRML_SMALL ; then
655       SQUASHFS_OPTIONS="$SQUASHFS_OUTPUT -e initrd.img* vmlinuz*"
656    fi
657
658    SQUASHFS_OUTPUT="$(mktemp -t grml-live.XXXXXX)"
659    log "mksquashfs $CHROOT_OUTPUT/* $BUILD_OUTPUT/live/${GRML_NAME}.squashfs -noappend $SQUASHFS_OPTIONS $SQUASHFS_ZLIB"
660    if mksquashfs $CHROOT_OUTPUT/* $BUILD_OUTPUT/live/"${GRML_NAME}".squashfs \
661       -noappend $SQUASHFS_OPTIONS $SQUASHFS_ZLIB 2>"${SQUASHFS_OUTPUT}" ; then
662       echo "${GRML_NAME}.squashfs" > $BUILD_OUTPUT/live/filesystem.module
663       log "Finished execution of stage 'squashfs' [$(date)]"
664       einfo "Finished execution of stage 'squashfs'" ; eend 0
665       rm -f "${SQUASHFS_OUTPUT}"
666    else
667       log "There was an error executing stage 'squashfs' [$(date)]:"
668       log "$(cat $SQUASHFS_OUTPUT)"
669       eerror "There was an error executing stage 'squashfs':" ; eend 1
670       cat "${SQUASHFS_OUTPUT}"
671       rm -f "${SQUASHFS_OUTPUT}"
672       bailout
673    fi
674 fi
675
676 # create md5sum file:
677 ( cd $BUILD_OUTPUT/GRML &&
678 find .. -type f -not -name md5sums -not -name isolinux.bin -exec md5sum {} \; > md5sums )
679 # }}}
680
681 # ISO_OUTPUT - mkisofs {{{
682 [ -n "$ISO_OUTPUT" ] || ISO_OUTPUT="$OUTPUT/grml_isos"
683 [ -n "$ISO_NAME" ] || ISO_NAME="${GRML_NAME}_${VERSION}.iso"
684
685 if [ "$BOOT_METHOD" = "isolinux" ] ; then
686    BOOT_FILE="boot/isolinux/isolinux.bin -c boot/isolinux/boot.cat"
687 elif [ "$BOOT_METHOD" = "grub" ] ; then
688    BOOT_FILE="boot/grub/stage2_eltorito"
689 fi
690
691 if [ -f "${ISO_OUTPUT}/${ISO_NAME}" -a -z "$UPDATE" -a -z "$BUILD_ONLY" -a -z "$BUILD_DIRTY" ] ; then
692    log "$ISO_OUTPUT exists already, skipping stage 'iso build'"
693    ewarn "$ISO_OUTPUT exists already, skipping stage 'iso build'" ; eend 0
694 else
695    mkdir -p "$ISO_OUTPUT" || bailout 6 "Problem with creating $ISO_OUTPUT for stage 'iso build'"
696
697    CURRENT_DIR=$(pwd)
698    if cd "$BUILD_OUTPUT" ; then
699       log "mkisofs -V '${GRML_NAME} ${VERSION}' -publisher 'grml-live | grml.org' -l -r -J -no-emul-boot -boot-load-size 4 -boot-info-table -b $BOOT_FILE -o ${ISO_OUTPUT}/${ISO_NAME} ."
700       mkisofs -V "${GRML_NAME} ${VERSION}" -publisher 'grml-live | grml.org' \
701               -l -r -J -no-emul-boot -boot-load-size 4 -boot-info-table    \
702               -b $BOOT_FILE \
703               -o "${ISO_OUTPUT}/${ISO_NAME}" . ; RC=$?
704
705       # generate md5sum of ISO if we are using class 'RELEASE':
706       case $CLASSES in *RELEASE*)
707          [ "$RC" = 0 ] && \
708          ( cd $ISO_OUTPUT && \
709          md5sum ${ISO_NAME} > ${ISO_NAME}.md5 && \
710          touch -r ${ISO_NAME} ${ISO_NAME}.md5 )
711          ;;
712       esac
713
714       cd $CURRENT_DIR
715    fi
716
717    if [ "$RC" = 0 ] ; then
718       log "Finished execution of stage 'iso build' [$(date)]"
719       einfo "Finished execution of stage 'iso build'" ; eend 0
720    else
721       log "There was an error ($RC) executing stage 'iso build' [$(date)]"
722       eerror "There was an error executing stage 'iso build'" ; eend 1
723       bailout $RC
724    fi
725 fi
726 # }}}
727
728 # finalize {{{
729 [ -n "$start_seconds" ] && SECONDS="$[$(cut -d . -f 1 /proc/uptime)-$start_seconds]" || SECONDS="unknown"
730 einfo "Sucessfully finished execution of $PN [running ${SECONDS} seconds]" ; eend 0
731 log "Sucessfully finished execution of $PN [running ${SECONDS} seconds]"
732 bailout 0
733 # }}}
734
735 ## END OF FILE #################################################################
736 # vim:foldmethod=marker ts=2 ft=sh ai expandtab tw=80 sw=3