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