Support use of local mirror (like NFS) through mount bind
[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: Tue Sep 18 20:32:35 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 # source main configuration file:
29 LIVE_CONF=/etc/grml/grml-live.conf
30 . $LIVE_CONF
31
32 PN=$(basename $0)
33 # TMPFILE=$(mktemp)
34 # }}}
35
36 # clean exit {{{
37 bailout() {
38   # rm -f "$TMPFILE"
39   [ -n "$MIRROR_DIRECTORY" ] && umount "${CHROOT_TARGET}/${MIRROR_DIRECTORY}"
40   [ -n "$1" ] && EXIT="$1" || EXIT="1"
41   [ -n "$2" ] && echo "$2">&2
42   exit "$EXIT"
43 }
44 trap bailout 1 2 3 15
45 # }}}
46
47 # check for important variables {{{
48 [ -n "$GRML_FAI_CONFIG" ] || GRML_FAI_CONFIG=/etc/grml/fai
49 [ -n "$HOSTNAME" ] || HOSTNAME=grml
50 [ -n "$USERNAME" ] || USERNAME=grml
51 [ -n "$CLASSES" ]  || CLASSES="GRML,I386"
52 [ -n "$TARGET" ] || bailout 1 "${PN}: \$TARGET not specified. Please adjust $LIVE_CONF. Exiting."
53 # }}}
54
55 # usage information {{{
56 usage()
57 {
58   echo "
59 $PN - build process script for generating a (grml based) Linux Live-ISO
60
61 Usage: $PN [-c <classe[s]>] [-t <target_directory>] [-s <suite> [-Fvh]
62
63 Usage examples:
64
65     $PN
66     $PN -c GRMLBASE,GRML_X,I386 -t /grml/
67     $PN -c GRMLBASE,I386 -t /dev/shm/grml
68     $PN -c GRMLBASE,GRML_SMALL,I386
69     $PN -s sid -c GRMLBASE,I386
70
71 More details: man grml-live
72               /usr/share/doc/grml-live/grml-live.html
73
74 Please send your bug reports, feedback,.. to the grml-team.
75 http://grml.org/bugs/
76 "
77 }
78 # }}}
79
80 # command line parsing {{{
81
82 while getopts "c:s:t:Fhv" opt; do
83   case "$opt" in
84     c) CLASSES="$OPTARG" ;;
85     # f) FLAVOUR="$OPTARG" ;; # TODO
86     F) FORCE=1 ;;
87     h) usage ; bailout 0 ;;
88     s) SUITE="$OPTARG" ;;
89     t) TARGET="$OPTARG"
90        CHROOT_TARGET="$TARGET/grml_chroot"
91        BUILD_TARGET="$TARGET/grml_cd"
92        ISO_TARGET="$TARGET/grml_isos"
93        ;;
94     v) VERBOSE="-v" ;;
95     ?) echo "invalid option -$OPTARG" >&2; bailout 1 ;;
96   esac
97 done
98 shift $(($OPTIND - 1))  # set ARGV to the first not parsed commandline parameter
99
100 # }}}
101
102 # some misc checks before executing FAI {{{
103 [ -n "$CLASSES" ] || bailout 1 "Error: \$CLASSES unset, please set it in $LIVE_CONF or
104 specify it on the command line using the -c|--classes option."
105 [ -n "$TARGET" ] || bailout 1 "Error: \$TARGET unset, please set it in $LIVE_CONF or
106 specify it on the command line using the -t|--target option."
107 # }}}
108
109 # ask user whether the setup is ok {{{
110 if [ -z "$FORCE" ] ; then
111    echo
112    echo "$PN - check your configuration (or invoke using -F to force execution without prompting):"
113    echo
114    echo "  FAI classes:       $CLASSES"
115    echo "  main directory:    $TARGET"
116    [ -n "$CHROOT_TARGET" ] && echo "  chroot target:     $CHROOT_TARGET"
117    [ -n "$BUILD_TARGET" ]  && echo "  build target:      $BUILD_TARGET"
118    [ -n "$ISO_TARGET" ]    && echo "  ISO target:        $ISO_TARGET"
119    [ -n "$SUITE" ]         && echo "  Debian suite:      $SUITE"
120    [ -n "$FAI_ARGS" ]      && echo "  additional arguments for FAI: $FAI_ARGS"
121    [ -n "$VERBOSE" ]       && echo "  Using VERBOSE mode."
122    echo
123    echo -n "Is this ok for you? [y/N] "
124    read a
125    if ! [ "$a" = 'y' -o "$a" = 'Y' ] ; then
126       bailout 1 "Exiting as requested."
127    fi
128    echo
129 fi
130 # }}}
131
132 # on-the-fly configuration {{{
133 if [ -n "$MIRROR_DIRECTORY" ] ; then
134    if ! [ -d "$MIRROR_DIRECTORY/debian" ] ; then
135       echo "Sorry, $MIRROR_DIRECTORY/debian does not seem to exist. Exiting.">&2
136       bailout 1
137    fi
138    echo "$MIRROR_SOURCES" > /etc/grml/fai/apt/sources.list
139    if [ -n "$GRML_LIVE_SOURCES" ] ; then
140       echo "$GRML_LIVE_SOURCES" >> /etc/grml/fai/apt/sources.list
141    fi
142 elif [ -n "$GRML_LIVE_SOURCES" ] ; then
143    echo "$GRML_LIVE_SOURCES" > /etc/grml/fai/apt/sources.list
144 fi
145
146 if [ -n "$FAI_DEBOOTSTRAP" ] ; then
147    sed -i "s#^FAI_DEBOOTSTRAP=.*#FAI_DEBOOTSTRAP=\"$FAI_DEBOOTSTRAP\"#" /etc/grml/fai/make-fai-nfsroot.conf
148 fi
149
150 # does this suck? YES!
151 if [ -n "$SUITE" ] ; then
152    sed -ie "s/SUITE=.*/SUITE=\"$SUITE\"/" $LIVE_CONF
153
154    DIST="\|\ etch\ \|\ stable\ \|\ lenny\ \|\ testing\ \|\ sid\ \|\ unstable\ "
155    sed -ie "s/\(deb .\+\)\([ \t]+\)$DIST\([ \t]+\)\(main \)/\1\2 $SUITE \3\4/" $LIVE_CONF
156    sed -ie "s/\(deb .\+\)\([ \t]+\)$DIST\([ \t]+\)\(main \)/\1\2 $SUITE \3\4/" /etc/grml/fai/apt/sources.list
157
158    DIST='\"etch\|=\"stable=\"lenny=\"testing=\"sid=\"unstable'
159    sed -ie "s#FAI_DEBOOTSTRAP=$DIST#FAI_DEBOOTSTRAP=\"$SUITE#" $LIVE_CONF
160    sed -ie "s#FAI_DEBOOTSTRAP=$DIST#FAI_DEBOOTSTRAP=\"$SUITE#" /etc/grml/fai/make-fai-nfsroot.conf
161 fi
162 # }}}
163
164 # CHROOT_TARGET - execute FAI {{{
165 [ -n "$CHROOT_TARGET" ] || CHROOT_TARGET="$TARGET/grml_chroot"
166
167 if [ -d "$CHROOT_TARGET" ] ; then
168    echo "  [x] $CHROOT_TARGET exists already, skipping the stage 'fai dirnstall'"
169 else
170    mkdir -p "$CHROOT_TARGET" || bailout 5 "Problem with creating $CHROOT_TARGET for FAI"
171    if [ -n "${MIRROR_DIRECTORY}" ] ; then
172       mkdir -p "${CHROOT_TARGET}/${MIRROR_DIRECTORY}"
173       mount --bind "${MIRROR_DIRECTORY}" "${CHROOT_TARGET}/${MIRROR_DIRECTORY}"
174    fi
175    fai $VERBOSE -C "$GRML_FAI_CONFIG" -c"$CLASSES" dirinstall "$CHROOT_TARGET" $FAI_ARGS
176    umount $CHROOT_TARGET/proc 2>/dev/null || /bin/true
177    umount $CHROOT_TARGET/sys  2>/dev/null || /bin/true
178    [ -n "$MIRROR_DIRECTORY" ] && umount "${CHROOT_TARGET}/${MIRROR_DIRECTORY}"
179    # notice: 'fai dirinstall' does not seem to exit appropriate ->
180    # we need better error handling
181    if [ -r "$CHROOT_TARGET"/etc/grml_cd ] ; then
182       echo "  [*] Finished execution of stage 'fai dirinstall'"
183    else
184       echo "  [!] There was an error during execution of stage 'fai dirinstall'"
185       exit 1
186    fi
187 fi
188 # }}}
189
190 # BUILD_TARGET - execute arch specific stuff and squashfs {{{
191 [ -n "$BUILD_TARGET" ] || BUILD_TARGET="$TARGET/grml_cd"
192 mkdir -p "$BUILD_TARGET" || bailout 6 "Problem with creating $BUILD_TARGET for stage ARCH"
193
194 # i386:
195 [ -n "$ARCH" ] || ARCH="$(dpkg --print-architecture)"
196 if [ "$ARCH" = i386 ] ; then
197    if [ -d "$BUILD_TARGET"/boot/isolinux ] ; then
198       echo "  [x] $BUILD_TARGET/boot/isolinux exists already - skipping stage 'boot/isolinux'"
199       continue
200    else
201       # booting stuff:
202       mkdir -p "$BUILD_TARGET"/boot/isolinux
203       mkdir "$BUILD_TARGET"/GRML
204       cp /boot/memtest86+.bin                                        "$BUILD_TARGET"/boot/isolinux/memtest
205       cp "$CHROOT_TARGET"/boot/initrd*                               "$BUILD_TARGET"/boot/isolinux/initrd.gz
206       cp "$CHROOT_TARGET"/boot/vmlinuz*                              "$BUILD_TARGET"/boot/isolinux/linux26
207       cp /usr/lib/syslinux/chain.c32                                 "$BUILD_TARGET"/boot/isolinux/
208       cp /usr/lib/syslinux/isolinux.bin                              "$BUILD_TARGET"/boot/isolinux/
209       cp /usr/lib/syslinux/memdisk                                   "$BUILD_TARGET"/boot/isolinux/
210       cp /usr/lib/syslinux/menu.c32                                  "$BUILD_TARGET"/boot/isolinux/
211       cp /usr/share/grml-live/i386_files/boot/isolinux/allinone.img  "$BUILD_TARGET"/boot/isolinux/
212       cp /usr/share/grml-live/i386_files/boot/isolinux/balder10.imz  "$BUILD_TARGET"/boot/isolinux/
213       cp /usr/share/grml-live/i386_files/boot/isolinux/boot-beep.msg "$BUILD_TARGET"/boot/isolinux/
214       cp /usr/share/grml-live/i386_files/boot/isolinux/boot.msg      "$BUILD_TARGET"/boot/isolinux/
215       cp /usr/share/grml-live/i386_files/boot/isolinux/f*            "$BUILD_TARGET"/boot/isolinux/
216       cp /usr/share/grml-live/i386_files/boot/isolinux/isolinux.cfg  "$BUILD_TARGET"/boot/isolinux/
217       cp /usr/share/grml-live/i386_files/boot/isolinux/logo.16       "$BUILD_TARGET"/boot/isolinux/
218       cp /usr/share/grml-live/i386_files/boot/isolinux/syslinux.cfg  "$BUILD_TARGET"/boot/isolinux/
219       # autostart for Windows:
220       cp /usr/share/grml-live/windows/autostart/autorun.bat          "$BUILD_TARGET"/
221       cp /usr/share/grml-live/windows/autostart/autorun.inf          "$BUILD_TARGET"/
222       cp /usr/share/grml-live/windows/autostart/autorun.pif          "$BUILD_TARGET"/
223       cp /usr/share/grml-live/windows/autostart/cdrom.ico            "$BUILD_TARGET"/
224       # windows-binaries:
225       if [ -n "$WINDOWS_BINARIES" ] ; then
226          if [ -d "$BUILD_TARGET"/windows ] ; then
227             echo "  [x] $BUILD_TARGET/windows exists already - skipping stage 'WINDOWS_BINARIES'"
228             return 0
229          else
230             mkdir "$BUILD_TARGET"/windows
231             ( cd "$BUILD_TARGET"/windows
232               for file in pageant plink pscp psftp putty puttygen ; do
233                   wget ${WINDOWS_BINARIES}/${file}.exe
234               done )
235          fi
236       echo "  [*] Finished execution of stage 'WINDOWS_BINARIES'"
237       fi
238    echo "  [*] Finished execution of stage 'boot/isolinux'"
239    fi
240 # amd64:
241 elif [ "$ARCH" = amd64 ] ; then
242     echo 'Warning: gebi, it is your turn. :)'>&2
243 # ppc:
244 elif [ "$ARCH" = powerpc ] ; then
245     echo 'Warning: formorer, it is your turn. :)'>&2
246 # unsuported:
247 else
248    echo 'Warning: Unsupported ARCH, sorry. Want to support it? Contribute!'>&2
249 fi
250
251 if [ -d "$BUILD_TARGET"/live ] ; then
252    echo "  [x] $BUILD_TARGET/live exists already, skipping stage 'squashfs'"
253 else
254    mkdir "$BUILD_TARGET"/live
255    mksquashfs $CHROOT_TARGET/* $BUILD_TARGET/live/grml.squashfs -noappend
256 fi
257 echo "  [*] Finished execution of stage 'squashfs'"
258 # }}}
259
260 # ISO_TARGET - mkisofs {{{
261 [ -n "$ISO_TARGET" ] || ISO_TARGET="$TARGET/grml_isos"
262 if [ -d "$ISO_TARGET" ] ; then
263    echo "  [x] $ISO_TARGET exists already, skipping the stage 'iso build'"
264 else
265    mkdir -p "$ISO_TARGET" || bailout 6 "Problem with creating $ISO_TARGET for stage 'iso build'"
266    ( cd "$BUILD_TARGET" &&
267      mkisofs -V "Debian/etch grml" -publisher 'Michael Prokop <mika@grml.org>' \
268              -l -r -J -no-emul-boot -boot-load-size 4 -boot-info-table    \
269              -c boot/isolinux/boot.cat -b boot/isolinux/isolinux.bin      \
270              -o "$ISO_TARGET"/grml_0.0-1.iso .
271    )
272    echo "  [*] Finished execution of stage 'iso build'"
273 fi
274 # }}}
275
276 # finalize {{{
277 echo "  [*] Sucessfully finished execution of $PN"
278 bailout 0
279 # }}}
280
281 ## END OF FILE #################################################################
282 # vim:foldmethod=marker ts=2 ft=sh ai expandtab tw=80 sw=3