Display version and grml_name in dialog
[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 # Latest change: Sat Sep 29 10:44:26 CEST 2007 [mika]
8 ################################################################################
9
10 # read configuration files, set some misc variables {{{
11
12 export LANG=C
13 export LC_ALL=C
14
15 # exit on any error:
16 set -e
17
18 # we need root permissions for the build-process:
19 if [ "$(id -u 2>/dev/null)" != 0 ] ; then
20    echo "Error: please run this script with uid 0 (root)." >&2
21    exit 1
22 fi
23
24 # make sure they are not set by default
25 VERBOSE=''
26 FORCE=''
27
28 if [ -r /etc/grml/lsb-functions ] ; then
29    . /etc/grml/lsb-functions
30 else
31    einfo()  { echo "  [*] $*" ;}
32    eerror() { echo "  [!] $*">&2 ;}
33    ewarn()  { echo "  [x] $*" ;}
34    eend()   { return 0 ;}
35 fi
36
37 # source main configuration file:
38 LIVE_CONF=/etc/grml/grml-live.conf
39 . $LIVE_CONF
40
41 PN=$(basename $0)
42 # }}}
43
44 # clean exit {{{
45 bailout() {
46   [ -n "$MIRROR_DIRECTORY" ] && umount "${CHROOT_TARGET}/${MIRROR_DIRECTORY}"
47   [ -n "$1" ] && EXIT="$1" || EXIT="1"
48   [ -n "$2" ] && eerror "$2">&2
49   exit "$EXIT"
50 }
51 trap bailout 1 2 3 15
52 # }}}
53
54 # check for important variables {{{
55 [ -n "$GRML_FAI_CONFIG" ] || GRML_FAI_CONFIG=/etc/grml/fai
56 [ -n "$HOSTNAME" ] || HOSTNAME=grml
57 [ -n "$USERNAME" ] || USERNAME=grml
58 [ -n "$CLASSES" ]  || CLASSES="GRML,I386"
59 [ -n "$BOOT_METHOD" ] || BOOT_METHOD='isolinux'
60 [ -n "$TARGET" ] || bailout 1 "${PN}: \$TARGET not specified. Please adjust $LIVE_CONF. Exiting."
61
62 [ -n "$VERSION" ]  || VERSION="0.0.1"
63 [ -n "$RELEASENAME" ] || RELEASENAME="grml-live rocks"
64 [ -n "$GRML_NAME" ] || GRML_NAME='grml'
65
66 [ -n "$LOGDIR" ] || LOGDIR="/var/log/fai/dirinstall/$HOSTNAME"
67 [ -d "$LOGDIR" ] || mkdir -p $LOGDIR
68 LOGFILE="$LOGDIR/grml-live.conf"
69 # }}}
70
71 # some important functions {{{
72
73 # log output:
74 # usage: log "string to log"
75 log() { echo "$*" >> $LOGFILE ; }
76
77 # cut string at character number int = $1
78 # usage: cut_string 5 "1234567890" will output "12345"
79 cut_string() {
80   [ -n "$2" ] || return 1
81   echo "$2" | head -c "$1"; echo -ne "\n"
82 }
83
84 # prepend int = $1 spaces before string = $2
85 # usage: extend_string_begin 5 "123" will output "  123"
86 extend_string_begin() {
87   [ -n "$2" ] || return 1
88   local COUNT="$(echo $2 | wc -c)"
89   local FILL="$(expr $COUNT - $1)"
90   while [ "$FILL" -gt 1 ] ; do
91     echo -n " "
92     local FILL=$(expr $FILL - 1)
93   done
94   while [ "$FILL" -lt 1 ] ; do
95     echo -n " "
96     local FILL=$(expr $FILL + 1)
97   done
98   echo "$2" | head -c "$1"; echo -ne "\n"
99 }
100
101 # append int = $1 spaces to string = $2
102 # usage: extend_string_begin 5 "123" will output "123  "
103 extend_string_end() {
104   [ -n "$2" ] || return 1
105   echo -n "$2" | head -c "$1"
106   local COUNT="$(echo $2 | wc -c)"
107   local FILL="$(expr $COUNT - $1)"
108   while [ "$FILL" -gt 1 ] ; do
109     echo -n " "
110     local FILL=$(expr $FILL - 1)
111   done
112   while [ "$FILL" -lt 1 ] ; do
113     echo -n " "
114     local FILL=$(expr $FILL + 1)
115   done
116   echo -ne "\n"
117 }
118 # }}}
119
120 # usage information {{{
121 usage()
122 {
123   echo "
124 $PN - build process script for generating a (grml based) Linux Live-ISO
125
126 Usage: $PN [-c <classe[s]>] [-g <grml_name>] [-i <iso_name> ] \\
127                  [-r <release_name>] [-s <suite>] [-t <target_directory>] \\
128                  [-v <version_number>] [-FVh]
129
130 Usage examples:
131
132     $PN
133     $PN -c GRMLBASE,GRML_X,I386 -t /grml/
134     $PN -c GRMLBASE,I386 -t /dev/shm/grml
135     $PN -c GRMLBASE,GRML_SMALL,I386 -g grml-small -v 1.0
136     $PN -c GRMLBASE,I386 -i grml_0.0-1.iso
137     $PN -c GRMLBASE,I386 -s sid -V
138
139 More details: man grml-live
140               /usr/share/doc/grml-live/grml-live.html
141
142 Please send your bug reports, feedback,.. to the grml-team.
143 http://grml.org/bugs/
144 "
145 }
146 # }}}
147
148 # command line parsing {{{
149
150 while getopts "c:g:i:r:s:t:v:FhV" opt; do
151   case "$opt" in
152     c) CLASSES="$OPTARG" ;;
153     g) GRML_NAME="$OPTARG" ;;
154     i) ISO_NAME="$OPTARG" ;;
155     r) RELEASENAME="$OPTARG" ;;
156     s) SUITE="$OPTARG" ;;
157     t) TARGET="$OPTARG"
158        CHROOT_TARGET="$TARGET/grml_chroot"
159        BUILD_TARGET="$TARGET/grml_cd"
160        ISO_TARGET="$TARGET/grml_isos"
161        ;;
162     v) VERSION="$OPTARG" ;;
163     F) FORCE=1 ;;
164     h) usage ; bailout 0 ;;
165     V) VERBOSE="-v" ;;
166     ?) echo "invalid option -$OPTARG" >&2; bailout 1 ;;
167   esac
168 done
169 shift $(($OPTIND - 1))  # set ARGV to the first not parsed commandline parameter
170
171 # }}}
172
173 # some misc checks before executing FAI {{{
174 [ -n "$CLASSES" ] || bailout 1 "Error: \$CLASSES unset, please set it in $LIVE_CONF or
175 specify it on the command line using the -c|--classes option."
176 [ -n "$TARGET" ] || bailout 1 "Error: \$TARGET unset, please set it in $LIVE_CONF or
177 specify it on the command line using the -t|--target option."
178 # }}}
179
180 # ask user whether the setup is ok {{{
181 if [ -z "$FORCE" ] ; then
182    echo
183    echo "${PN}: check your configuration (or use -F to force execution without prompting):"
184    echo
185    echo "  FAI classes:       $CLASSES"
186    echo "  main directory:    $TARGET"
187    [ -n "$CHROOT_TARGET" ] && echo "  chroot target:     $CHROOT_TARGET"
188    [ -n "$BUILD_TARGET" ]  && echo "  build target:      $BUILD_TARGET"
189    [ -n "$ISO_TARGET" ]    && echo "  ISO target:        $ISO_TARGET"
190    [ -n "$GRML_NAME" ]     && echo "  grml name:         $GRML_NAME"
191    [ -n "$VERSION" ]       && echo "  grml version:      $VERSION"
192    [ -n "$SUITE" ]         && echo "  Debian suite:      $SUITE"
193    [ -n "$BOOT_METHOD" ]   && echo "  Boot method:       $BOOT_METHOD"
194    [ -n "$FAI_ARGS" ]      && echo "  additional arguments for FAI: $FAI_ARGS"
195    [ -n "$VERBOSE" ]       && echo "  Using VERBOSE mode."
196    echo
197    echo -n "Is this ok for you? [y/N] "
198    read a
199    if ! [ "$a" = 'y' -o "$a" = 'Y' ] ; then
200       bailout 1 "Exiting as requested."
201    fi
202    echo
203
204    start_seconds=$(cut -d . -f 1 /proc/uptime)
205    log "------------------------------------------------------------------------------"
206    log "Starting grml-live run [$(date)]"
207 fi
208 # }}}
209
210 # on-the-fly configuration {{{
211 if [ -n "$MIRROR_DIRECTORY" ] ; then
212    if ! [ -d "$MIRROR_DIRECTORY/debian" ] ; then
213       log "Sorry, $MIRROR_DIRECTORY/debian does not seem to exist. Exiting. [$(date)]"
214       eerror "Sorry, $MIRROR_DIRECTORY/debian does not seem to exist. Exiting."
215       bailout 1
216    fi
217    echo "$MIRROR_SOURCES" > /etc/grml/fai/apt/sources.list
218    if [ -n "$GRML_LIVE_SOURCES" ] ; then
219       echo "$GRML_LIVE_SOURCES" >> /etc/grml/fai/apt/sources.list
220    fi
221 elif [ -n "$GRML_LIVE_SOURCES" ] ; then
222    echo "$GRML_LIVE_SOURCES" > /etc/grml/fai/apt/sources.list
223 fi
224
225 if [ -n "$FAI_DEBOOTSTRAP" ] ; then
226    sed -i "s#^FAI_DEBOOTSTRAP=.*#FAI_DEBOOTSTRAP=\"$FAI_DEBOOTSTRAP\"#" /etc/grml/fai/make-fai-nfsroot.conf
227 fi
228
229 # does this suck? YES!
230 if [ -n "$SUITE" ] ; then
231    sed -i "s/SUITE=.*/SUITE=\"$SUITE\"/" $LIVE_CONF
232
233    DIST="\|\ etch\ \|\ stable\ \|\ lenny\ \|\ testing\ \|\ sid\ \|\ unstable\ "
234    sed -i "s/\(deb .\+\)\([ \t]+\)$DIST\([ \t]+\)\(main \)/\1\2 $SUITE \3\4/" $LIVE_CONF
235    sed -i "s/\(deb .\+\)\([ \t]+\)$DIST\([ \t]+\)\(main \)/\1\2 $SUITE \3\4/" /etc/grml/fai/apt/sources.list
236
237    DIST='\"etch\|=\"stable=\"lenny=\"testing=\"sid=\"unstable'
238    sed -i "s#FAI_DEBOOTSTRAP=$DIST#FAI_DEBOOTSTRAP=\"$SUITE#" $LIVE_CONF
239    sed -i "s#FAI_DEBOOTSTRAP=$DIST#FAI_DEBOOTSTRAP=\"$SUITE#" /etc/grml/fai/make-fai-nfsroot.conf
240 fi
241 # }}}
242
243 # CHROOT_TARGET - execute FAI {{{
244 [ -n "$CHROOT_TARGET" ] || CHROOT_TARGET="$TARGET/grml_chroot"
245
246 if [ -d "$CHROOT_TARGET/bin" ] ; then
247    log "$CHROOT_TARGET exists already, skipping stage 'fai dirinstall'"
248    ewarn "$CHROOT_TARGET exists already, skipping stage 'fai dirinstall'" ; eend 0
249 else
250    mkdir -p "$CHROOT_TARGET" || bailout 5 "Problem with creating $CHROOT_TARGET for FAI"
251    if [ -n "${MIRROR_DIRECTORY}" ] ; then
252       mkdir -p "${CHROOT_TARGET}/${MIRROR_DIRECTORY}"
253       mount --bind "${MIRROR_DIRECTORY}" "${CHROOT_TARGET}/${MIRROR_DIRECTORY}"
254    fi
255    fai $VERBOSE -C "$GRML_FAI_CONFIG" -c"$CLASSES" -u "$HOSTNAME" dirinstall "$CHROOT_TARGET" $FAI_ARGS | tee -a $LOGFILE
256    umount $CHROOT_TARGET/proc 2>/dev/null || /bin/true
257    umount $CHROOT_TARGET/sys  2>/dev/null || /bin/true
258    [ -n "$MIRROR_DIRECTORY" ] && umount "${CHROOT_TARGET}/${MIRROR_DIRECTORY}"
259
260    # notice: 'fai dirinstall' does not seem to exit appropriate, so:
261    ERROR=''
262    if [ -r "/var/log/fai/dirinstall/$HOSTNAME/software.log" ] ; then
263       grep 'dpkg: error processing' /var/log/fai/dirinstall/$HOSTNAME/software.log >> $LOGFILE && ERROR=1
264    fi
265
266    if [ -r "/var/log/fai/dirinstall/$HOSTNAME/shell.log" ] ; then
267       grep 'FAILED with exit code' /var/log/fai/dirinstall/$HOSTNAME/shell.log >> $LOGFILE && ERROR=2
268    fi
269
270    if [ -n "$ERROR" ] ; then
271       log "There was an error during execution of stage 'fai dirinstall' [$(date)]"
272       eerror "There was an error during execution of stage 'fai dirinstall'"
273       echo "   Check out /var/log/fai/dirinstall/$HOSTNAME/ for details. [exit ${ERROR}]"
274       eend 1 ; exit 1
275    else
276       log "Finished execution of stage 'fai dirinstall' [$(date)]"
277       einfo "Finished execution of stage 'fai dirinstall'"
278    fi
279 fi
280 # }}}
281
282 # BUILD_TARGET - execute arch specific stuff and squashfs {{{
283 [ -n "$BUILD_TARGET" ] || BUILD_TARGET="$TARGET/grml_cd"
284 mkdir -p "$BUILD_TARGET" || bailout 6 "Problem with creating $BUILD_TARGET for stage ARCH"
285
286 [ -n "$ARCH" ] || ARCH="$(dpkg --print-architecture)"
287
288 # i386:
289 if [ "$ARCH" = i386 ] || [ "$ARCH" = amd64 ] ; then
290    if [ -d "$BUILD_TARGET"/boot ] ; then
291       log "$BUILD_TARGET/boot exists already, skipping stage 'boot'"
292       ewarn "$BUILD_TARGET/boot exists already, skipping stage 'boot'" ; eend 0
293    else
294       # booting stuff:
295       mkdir -p "$BUILD_TARGET"/boot/isolinux
296       cp /boot/memtest86+.bin                              "$BUILD_TARGET"/boot/isolinux/memtest
297       cp "$CHROOT_TARGET"/boot/initrd*                     "$BUILD_TARGET"/boot/isolinux/initrd.gz
298       cp "$CHROOT_TARGET"/boot/vmlinuz*                    "$BUILD_TARGET"/boot/isolinux/linux26
299       cp /usr/lib/syslinux/chain.c32                       "$BUILD_TARGET"/boot/isolinux/
300       cp /usr/lib/syslinux/isolinux.bin                    "$BUILD_TARGET"/boot/isolinux/
301       cp /usr/lib/syslinux/memdisk                         "$BUILD_TARGET"/boot/isolinux/
302       cp /usr/lib/syslinux/menu.c32                        "$BUILD_TARGET"/boot/isolinux/
303       cp /usr/share/grml-live/templates/boot/isolinux/*    "$BUILD_TARGET"/boot/isolinux/
304       cp /usr/share/grml-live/templates/boot/isolinux/*    "$BUILD_TARGET"/boot/isolinux/
305       cp -a /usr/share/grml-live/templates/boot/grub       "$BUILD_TARGET"/boot/
306
307       [ -d "$BUILD_TARGET"/GRML ] || mkdir "$BUILD_TARGET"/GRML
308       cp -a /usr/share/grml-live/templates/GRML/* "$BUILD_TARGET"/GRML/
309
310       # adjust boot splash information:
311       ISO_DATE="$(date +%Y-%m-%d)"
312       RELEASE_INFO="$GRML_NAME $VERSION - Release Codename $RELEASENAME"
313       RELEASE_INFO="$(cut_string 68 "$RELEASE_INFO")"
314       RELEASE_INFO="$(extend_string_end 68 "$RELEASE_INFO")"
315
316       sed -i "s/%RELEASE_INFO%/$GRML_NAME $VERSION - $RELEASENAME/" "$BUILD_TARGET"/GRML/grml-version
317       sed -i "s/%DATE%/$ISO_DATE/"             "$BUILD_TARGET"/GRML/grml-version
318
319       sed -i "s/%RELEASE_INFO%/$RELEASE_INFO/" "$BUILD_TARGET"/boot/isolinux/boot.msg
320       sed -i "s/%DATE%/$ISO_DATE/"             "$BUILD_TARGET"/boot/isolinux/boot.msg
321
322       sed -i "s/%RELEASE_INFO%/$RELEASE_INFO/" "$BUILD_TARGET"/boot/isolinux/boot-beep.msg
323       sed -i "s/%DATE%/$ISO_DATE/"             "$BUILD_TARGET"/boot/isolinux/boot-beep.msg
324
325       sed -i "s/%VERSION%/$VERSION/"           "$BUILD_TARGET"/boot/grub/menu.lst
326       sed -i "s/%GRML_NAME%/$GRML_NAME/"       "$BUILD_TARGET"/boot/grub/menu.lst
327
328       # autostart for Windows:
329       cp /usr/share/grml-live/templates/windows/autostart/* "$BUILD_TARGET"/
330       # windows-binaries:
331       if [ -n "$WINDOWS_BINARIES" ] ; then
332          if [ -f "$BUILD_TARGET"/windows/putty.exe ] ; then
333             log "$BUILD_TARGET/windows exists already, skipping stage 'WINDOWS_BINARIES'"
334             ewarn "$BUILD_TARGET/windows exists already, skipping stage 'WINDOWS_BINARIES'" ; eend 0
335          else
336             mkdir "$BUILD_TARGET"/windows
337             ( cd "$BUILD_TARGET"/windows
338               for file in pageant plink pscp psftp putty puttygen ; do
339                  wget -O ${file}.exe ${WINDOWS_BINARIES}/${file}.exe
340               done )
341          fi
342       log "Finished execution of stage 'WINDOWS_BINARIES' [$(date)]"
343       einfo "Finished execution of stage 'WINDOWS_BINARIES'" ; eend 0
344       fi
345    einfo "Finished execution of stage 'boot'" ; eend 0
346    fi
347 # ppc:
348 elif [ "$ARCH" = powerpc ] ; then
349     ewarn 'Warning: formorer, it is your turn. :)'>&2
350 # unsuported:
351 else
352    eerror 'Error: Unsupported ARCH, sorry. Want to support it? Contribute!' ; eend 1
353 fi
354
355 if [ -f "$BUILD_TARGET"/live/grml.squashfs ] ; then
356    log "$BUILD_TARGET/live exists already, skipping stage 'squashfs'"
357    ewarn "$BUILD_TARGET/live exists already, skipping stage 'squashfs'" ; eend 0
358 else
359    mkdir "$BUILD_TARGET"/live
360    mksquashfs $CHROOT_TARGET/* $BUILD_TARGET/live/grml.squashfs -noappend
361    log "Finished execution of stage 'squashfs' [$(date)]"
362    einfo "Finished execution of stage 'squashfs'" ; eend 0
363 fi
364
365 # create md5sum file:
366 ( cd $BUILD_TARGET/GRML &&
367 find .. -type f -not -name md5sums -exec md5sum {} \; > md5sums )
368 # }}}
369
370 # ISO_TARGET - mkisofs {{{
371 [ -n "$ISO_TARGET" ] || ISO_TARGET="$TARGET/grml_isos"
372 [ -n "$ISO_NAME" ] || ISO_NAME="$GRML_NAME_${VERSION}.iso"
373
374 if [ "$BOOT_METHOD" = "isolinux" ] ; then
375    BOOT_FILE="boot/isolinux/isolinux.bin -c boot/isolinux/boot.cat"
376 elif [ "$BOOT_METHOD" = "grub" ] ; then
377    BOOT_FILE="boot/grub/stage2_eltorito"
378 fi
379
380 if [ -f "${ISO_TARGET}/${ISO_NAME}" ] ; then
381    log "$ISO_TARGET exists already, skipping stage 'iso build'"
382    ewarn "$ISO_TARGET exists already, skipping stage 'iso build'" ; eend 0
383 else
384    mkdir -p "$ISO_TARGET" || bailout 6 "Problem with creating $ISO_TARGET for stage 'iso build'"
385    CURRENT_DIR=$(pwd)
386    cd "$BUILD_TARGET" &&
387    mkisofs -V "grml $VERSION" -publisher 'grml-live | grml.org' \
388            -l -r -J -no-emul-boot -boot-load-size 4 -boot-info-table    \
389            -b $BOOT_FILE \
390            -o "${ISO_TARGET}/${ISO_NAME}" . ; RC=$?
391    cd $CURRENT_DIR
392    if [ "$RC" = 0 ] ; then
393       log "Finished execution of stage 'iso build' [$(date)]"
394       einfo "Finished execution of stage 'iso build'" ; eend 0
395    else
396       log "There was an error ($RC) executing stage 'iso build' [$(date)]"
397       eerror "There was an error executing stage 'iso build'" ; eend 1
398       bailout $RC
399    fi
400 fi
401 # }}}
402
403 # finalize {{{
404 [ -n "$start_seconds" ] && SECONDS="$[$(cut -d . -f 1 /proc/uptime)-$start_seconds]"
405 einfo "Sucessfully finished execution of $PN [running ${SECONDS} seconds]" ; eend 0
406 log "Sucessfully finished execution of $PN [running ${SECONDS} seconds]"
407 log "------------------------------------------------------------------------------"
408 bailout 0
409 # }}}
410
411 ## END OF FILE #################################################################
412 # vim:foldmethod=marker ts=2 ft=sh ai expandtab tw=80 sw=3