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