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