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