Implement template system for boot/isolinux
[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: Wed Sep 19 23:41:52 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 # TMPFILE=$(mktemp)
43 # }}}
44
45 # clean exit {{{
46 bailout() {
47   # rm -f "$TMPFILE"
48   [ -n "$MIRROR_DIRECTORY" ] && umount "${CHROOT_TARGET}/${MIRROR_DIRECTORY}"
49   [ -n "$1" ] && EXIT="$1" || EXIT="1"
50   [ -n "$2" ] && eerror "$2">&2
51   exit "$EXIT"
52 }
53 trap bailout 1 2 3 15
54 # }}}
55
56 # check for important variables {{{
57 [ -n "$GRML_FAI_CONFIG" ] || GRML_FAI_CONFIG=/etc/grml/fai
58 [ -n "$HOSTNAME" ] || HOSTNAME=grml
59 [ -n "$USERNAME" ] || USERNAME=grml
60 [ -n "$CLASSES" ]  || CLASSES="GRML,I386"
61 [ -n "$TARGET" ] || bailout 1 "${PN}: \$TARGET not specified. Please adjust $LIVE_CONF. Exiting."
62
63 [ -n "$VERSION" ]  || VERSION="0.1"
64 [ -n "$CODENAME" ] || CODENAME="grml-live rocks"
65
66 [ -n "$LOGDIR" ] || LOGDIR="/var/log/fai/dirinstall/$HOSTNAME"
67 [ -d "$LOGDIR" ] || mkdir $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]>] [-t <target_directory>] [-s <suite> [-Fvh]
127
128 Usage examples:
129
130     $PN
131     $PN -c GRMLBASE,GRML_X,I386 -t /grml/
132     $PN -c GRMLBASE,I386 -t /dev/shm/grml
133     $PN -c GRMLBASE,GRML_SMALL,I386
134     $PN -s sid -c GRMLBASE,I386
135
136 More details: man grml-live
137               /usr/share/doc/grml-live/grml-live.html
138
139 Please send your bug reports, feedback,.. to the grml-team.
140 http://grml.org/bugs/
141 "
142 }
143 # }}}
144
145 # command line parsing {{{
146
147 while getopts "c:i:s:t:Fhv" opt; do
148   case "$opt" in
149     c) CLASSES="$OPTARG" ;;
150     F) FORCE=1 ;;
151     h) usage ; bailout 0 ;;
152     i) ISO_NAME="$OPTARG" ;;
153     s) SUITE="$OPTARG" ;;
154     t) TARGET="$OPTARG"
155        CHROOT_TARGET="$TARGET/grml_chroot"
156        BUILD_TARGET="$TARGET/grml_cd"
157        ISO_TARGET="$TARGET/grml_isos"
158        ;;
159     v) VERBOSE="-v" ;;
160     ?) echo "invalid option -$OPTARG" >&2; bailout 1 ;;
161   esac
162 done
163 shift $(($OPTIND - 1))  # set ARGV to the first not parsed commandline parameter
164
165 # }}}
166
167 # some misc checks before executing FAI {{{
168 [ -n "$CLASSES" ] || bailout 1 "Error: \$CLASSES unset, please set it in $LIVE_CONF or
169 specify it on the command line using the -c|--classes option."
170 [ -n "$TARGET" ] || bailout 1 "Error: \$TARGET unset, please set it in $LIVE_CONF or
171 specify it on the command line using the -t|--target option."
172 # }}}
173
174 # ask user whether the setup is ok {{{
175 if [ -z "$FORCE" ] ; then
176    echo
177    echo "${PN}: check your configuration (or use -F to force execution without prompting):"
178    echo
179    echo "  FAI classes:       $CLASSES"
180    echo "  main directory:    $TARGET"
181    [ -n "$CHROOT_TARGET" ] && echo "  chroot target:     $CHROOT_TARGET"
182    [ -n "$BUILD_TARGET" ]  && echo "  build target:      $BUILD_TARGET"
183    [ -n "$ISO_TARGET" ]    && echo "  ISO target:        $ISO_TARGET"
184    [ -n "$SUITE" ]         && echo "  Debian suite:      $SUITE"
185    [ -n "$FAI_ARGS" ]      && echo "  additional arguments for FAI: $FAI_ARGS"
186    [ -n "$VERBOSE" ]       && echo "  Using VERBOSE mode."
187    echo
188    echo -n "Is this ok for you? [y/N] "
189    read a
190    if ! [ "$a" = 'y' -o "$a" = 'Y' ] ; then
191       bailout 1 "Exiting as requested."
192    fi
193    echo
194
195    start_seconds=$(cut -d . -f 1 /proc/uptime)
196    log "------------------------------------------------------------------------------"
197    log "Starting grml-live run [$(date)]"
198 fi
199 # }}}
200
201 # on-the-fly configuration {{{
202 if [ -n "$MIRROR_DIRECTORY" ] ; then
203    if ! [ -d "$MIRROR_DIRECTORY/debian" ] ; then
204       eerror "Sorry, $MIRROR_DIRECTORY/debian does not seem to exist. Exiting."
205       log "Sorry, $MIRROR_DIRECTORY/debian does not seem to exist. Exiting. [$(date)]"
206       bailout 1
207    fi
208    echo "$MIRROR_SOURCES" > /etc/grml/fai/apt/sources.list
209    if [ -n "$GRML_LIVE_SOURCES" ] ; then
210       echo "$GRML_LIVE_SOURCES" >> /etc/grml/fai/apt/sources.list
211    fi
212 elif [ -n "$GRML_LIVE_SOURCES" ] ; then
213    echo "$GRML_LIVE_SOURCES" > /etc/grml/fai/apt/sources.list
214 fi
215
216 if [ -n "$FAI_DEBOOTSTRAP" ] ; then
217    sed -i "s#^FAI_DEBOOTSTRAP=.*#FAI_DEBOOTSTRAP=\"$FAI_DEBOOTSTRAP\"#" /etc/grml/fai/make-fai-nfsroot.conf
218 fi
219
220 # does this suck? YES!
221 if [ -n "$SUITE" ] ; then
222    sed -i "s/SUITE=.*/SUITE=\"$SUITE\"/" $LIVE_CONF
223
224    DIST="\|\ etch\ \|\ stable\ \|\ lenny\ \|\ testing\ \|\ sid\ \|\ unstable\ "
225    sed -i "s/\(deb .\+\)\([ \t]+\)$DIST\([ \t]+\)\(main \)/\1\2 $SUITE \3\4/" $LIVE_CONF
226    sed -i "s/\(deb .\+\)\([ \t]+\)$DIST\([ \t]+\)\(main \)/\1\2 $SUITE \3\4/" /etc/grml/fai/apt/sources.list
227
228    DIST='\"etch\|=\"stable=\"lenny=\"testing=\"sid=\"unstable'
229    sed -i "s#FAI_DEBOOTSTRAP=$DIST#FAI_DEBOOTSTRAP=\"$SUITE#" $LIVE_CONF
230    sed -i "s#FAI_DEBOOTSTRAP=$DIST#FAI_DEBOOTSTRAP=\"$SUITE#" /etc/grml/fai/make-fai-nfsroot.conf
231 fi
232 # }}}
233
234 # CHROOT_TARGET - execute FAI {{{
235 [ -n "$CHROOT_TARGET" ] || CHROOT_TARGET="$TARGET/grml_chroot"
236
237 if [ -d "$CHROOT_TARGET/bin" ] ; then
238    ewarn "$CHROOT_TARGET exists already, skipping stage 'fai dirinstall'" ; eend 0
239    log "$CHROOT_TARGET exists already, skipping stage 'fai dirinstall'"
240 else
241    mkdir -p "$CHROOT_TARGET" || bailout 5 "Problem with creating $CHROOT_TARGET for FAI"
242    if [ -n "${MIRROR_DIRECTORY}" ] ; then
243       mkdir -p "${CHROOT_TARGET}/${MIRROR_DIRECTORY}"
244       mount --bind "${MIRROR_DIRECTORY}" "${CHROOT_TARGET}/${MIRROR_DIRECTORY}"
245    fi
246    fai $VERBOSE -C "$GRML_FAI_CONFIG" -c"$CLASSES" dirinstall "$CHROOT_TARGET" $FAI_ARGS | tee -a $LOGFILE
247    umount $CHROOT_TARGET/proc 2>/dev/null || /bin/true
248    umount $CHROOT_TARGET/sys  2>/dev/null || /bin/true
249    [ -n "$MIRROR_DIRECTORY" ] && umount "${CHROOT_TARGET}/${MIRROR_DIRECTORY}"
250
251    # notice: 'fai dirinstall' does not seem to exit appropriate, so:
252    ERROR=''
253    if [ -r "/var/log/fai/dirinstall/$HOSTNAME/software.log" ] ; then
254       grep 'dpkg: error processing' /var/log/fai/dirinstall/$HOSTNAME/software.log >> $LOGFILE && ERROR=1
255    fi
256
257    if [ -r "/var/log/fai/dirinstall/$HOSTNAME/shell.log" ] ; then
258       grep 'FAILED with exit code' /var/log/fai/dirinstall/$HOSTNAME/shell.log >> $LOGFILE && ERROR=2
259    fi
260
261    if [ -n "$ERROR" ] ; then
262       eerror "There was an error during execution of stage 'fai dirinstall'"
263       echo "      Check out /var/log/fai/dirinstall/$HOSTNAME/... for details. [exit ${ERROR}]"
264       log "There was an error during execution of stage 'fai dirinstall' [$(date)]"
265       eend 1 ; exit 1
266    else
267       einfo "Finished execution of stage 'fai dirinstall'"
268       log "Finished execution of stage 'fai dirinstall' [$(date)]"
269    fi
270 fi
271 # }}}
272
273 # BUILD_TARGET - execute arch specific stuff and squashfs {{{
274 [ -n "$BUILD_TARGET" ] || BUILD_TARGET="$TARGET/grml_cd"
275 mkdir -p "$BUILD_TARGET" || bailout 6 "Problem with creating $BUILD_TARGET for stage ARCH"
276
277 # i386:
278 [ -n "$ARCH" ] || ARCH="$(dpkg --print-architecture)"
279 if [ "$ARCH" = i386 ] ; then
280    if [ -d "$BUILD_TARGET"/boot/isolinux ] ; then
281       ewarn "$BUILD_TARGET/boot exists already, skipping stage 'boot/isolinux'" ; eend 0
282       log "$BUILD_TARGET/boot exists already, skipping stage 'boot/isolinux'"
283    else
284       # booting stuff:
285       mkdir -p "$BUILD_TARGET"/boot/isolinux
286       [ -d "$BUILD_TARGET"/GRML ] || mkdir "$BUILD_TARGET"/GRML
287       cp /boot/memtest86+.bin                                        "$BUILD_TARGET"/boot/isolinux/memtest
288       cp "$CHROOT_TARGET"/boot/initrd*                               "$BUILD_TARGET"/boot/isolinux/initrd.gz
289       cp "$CHROOT_TARGET"/boot/vmlinuz*                              "$BUILD_TARGET"/boot/isolinux/linux26
290       cp /usr/lib/syslinux/chain.c32                                 "$BUILD_TARGET"/boot/isolinux/
291       cp /usr/lib/syslinux/isolinux.bin                              "$BUILD_TARGET"/boot/isolinux/
292       cp /usr/lib/syslinux/memdisk                                   "$BUILD_TARGET"/boot/isolinux/
293       cp /usr/lib/syslinux/menu.c32                                  "$BUILD_TARGET"/boot/isolinux/
294       cp /usr/share/grml-live/i386_files/boot/isolinux/allinone.img  "$BUILD_TARGET"/boot/isolinux/
295       cp /usr/share/grml-live/i386_files/boot/isolinux/balder10.imz  "$BUILD_TARGET"/boot/isolinux/
296       cp /usr/share/grml-live/i386_files/boot/isolinux/boot-beep.msg "$BUILD_TARGET"/boot/isolinux/
297       cp /usr/share/grml-live/i386_files/boot/isolinux/boot.msg      "$BUILD_TARGET"/boot/isolinux/
298       cp /usr/share/grml-live/i386_files/boot/isolinux/f*            "$BUILD_TARGET"/boot/isolinux/
299       cp /usr/share/grml-live/i386_files/boot/isolinux/isolinux.cfg  "$BUILD_TARGET"/boot/isolinux/
300       cp /usr/share/grml-live/i386_files/boot/isolinux/logo.16       "$BUILD_TARGET"/boot/isolinux/
301       cp /usr/share/grml-live/i386_files/boot/isolinux/syslinux.cfg  "$BUILD_TARGET"/boot/isolinux/
302
303       # adjust boot splash information:
304       ISO_DATE="$(date +%Y-%m-%d)"
305       VERSION="$(cut_string 5 "$VERSION")" ; VERSION="$(extend_string_end 5 "$VERSION")"
306       CODENAME="$(cut_string 30 "$CODENAME")" ; CODENAME="$(extend_string_end 30 "$CODENAME")"
307
308       sed -i "s/%VERSION%/$VERSION/"   "$BUILD_TARGET"/boot/isolinux/boot.msg
309       sed -i "s/%CODENAME%/$CODENAME/" "$BUILD_TARGET"/boot/isolinux/boot.msg
310       sed -i "s/%DATE%/$ISO_DATE/"     "$BUILD_TARGET"/boot/isolinux/boot.msg
311
312       sed -i "s/%VERSION%/$VERSION/"   "$BUILD_TARGET"/boot/isolinux/boot-beep.msg
313       sed -i "s/%CODENAME%/$CODENAME/" "$BUILD_TARGET"/boot/isolinux/boot-beep.msg
314       sed -i "s/%DATE%/$ISO_DATE/"     "$BUILD_TARGET"/boot/isolinux/boot-beep.msg
315
316       # autostart for Windows:
317       cp /usr/share/grml-live/windows/autostart/autorun.bat          "$BUILD_TARGET"/
318       cp /usr/share/grml-live/windows/autostart/autorun.inf          "$BUILD_TARGET"/
319       cp /usr/share/grml-live/windows/autostart/autorun.pif          "$BUILD_TARGET"/
320       cp /usr/share/grml-live/windows/autostart/cdrom.ico            "$BUILD_TARGET"/
321       # windows-binaries:
322       if [ -n "$WINDOWS_BINARIES" ] ; then
323          if [ -f "$BUILD_TARGET"/windows/putty.exe ] ; then
324             ewarn "$BUILD_TARGET/windows exists already, skipping stage 'WINDOWS_BINARIES'" ; eend 0
325             log "$BUILD_TARGET/windows exists already, skipping stage 'WINDOWS_BINARIES'"
326          else
327             mkdir "$BUILD_TARGET"/windows
328             ( cd "$BUILD_TARGET"/windows
329               for file in pageant plink pscp psftp putty puttygen ; do
330                  wget -O ${file}.exe ${WINDOWS_BINARIES}/${file}.exe
331               done )
332          fi
333       einfo "Finished execution of stage 'WINDOWS_BINARIES'" ; eend 0
334       log "Finished execution of stage 'WINDOWS_BINARIES' [$(date)]"
335       fi
336    einfo "Finished execution of stage 'boot/isolinux'" ; eend 0
337    fi
338 # amd64:
339 elif [ "$ARCH" = amd64 ] ; then
340     ewarn 'Warning: gebi, it is your turn. :)'>&2
341 # ppc:
342 elif [ "$ARCH" = powerpc ] ; then
343     ewarn 'Warning: formorer, it is your turn. :)'>&2
344 # unsuported:
345 else
346    eerror 'Error: Unsupported ARCH, sorry. Want to support it? Contribute!' ; eend 1
347 fi
348
349 if [ -f "$BUILD_TARGET"/live/grml.squashfs ] ; then
350    ewarn "$BUILD_TARGET/live exists already, skipping stage 'squashfs'" ; eend 0
351    log "$BUILD_TARGET/live exists already, skipping stage 'squashfs'"
352 else
353    mkdir "$BUILD_TARGET"/live
354    mksquashfs $CHROOT_TARGET/* $BUILD_TARGET/live/grml.squashfs -noappend
355    einfo "Finished execution of stage 'squashfs'" ; eend 0
356    log "Finished execution of stage 'squashfs' [$(date)]"
357 fi
358 # }}}
359
360 # ISO_TARGET - mkisofs {{{
361 [ -n "$ISO_TARGET" ] || ISO_TARGET="$TARGET/grml_isos"
362 if [ -d "$ISO_TARGET" ] ; then
363    ewarn "$ISO_TARGET exists already, skipping stage 'iso build'" ; eend 0
364    log "$ISO_TARGET exists already, skipping stage 'iso build'"
365 else
366    mkdir -p "$ISO_TARGET" || bailout 6 "Problem with creating $ISO_TARGET for stage 'iso build'"
367    [ -n "$ISO_NAME" ] || ISO_NAME="grml_0.0-1.iso"
368    ( cd "$BUILD_TARGET" &&
369      mkisofs -V "Debian/etch grml" -publisher 'grml-live | grml.org' \
370              -l -r -J -no-emul-boot -boot-load-size 4 -boot-info-table    \
371              -c boot/isolinux/boot.cat -b boot/isolinux/isolinux.bin      \
372              -o "${ISO_TARGET}"/"${ISO_NAME}" .
373    )
374    einfo "Finished execution of stage 'iso build'" ; eend 0
375    log "Finished execution of stage 'iso build' [$(date)]"
376 fi
377 # }}}
378
379 # finalize {{{
380 SECONDS="$[$(cut -d . -f 1 /proc/uptime)-$start_seconds]"
381 einfo "Sucessfully finished execution of $PN [running ${SECONDS} seconds]" ; eend 0
382 log "Sucessfully finished execution of $PN [running ${SECONDS} seconds]"
383 log "------------------------------------------------------------------------------"
384 bailout 0
385 # }}}
386
387 ## END OF FILE #################################################################
388 # vim:foldmethod=marker ts=2 ft=sh ai expandtab tw=80 sw=3