remove default boot entry for additional isos
[grml2usb.git] / grml2iso
1 #!/bin/bash
2 # Filename:      grml2iso
3 # Purpose:       create a multiboot grml ISO using grml2usb
4 # Authors:       Michael Prokop <mika@grml.org>,
5 #                Thorsten Glaser <tg@mirbsd.org>
6 # Bug-Reports:   see http://grml.org/bugs/
7 # License:       This file is licensed under the GPL v2 or any later version.
8 ################################################################################
9 # TODO:
10 # * support isolinux as bootloader on the ISO
11 # * support setting grml2usb options (e.g. --bootoptions=...)
12 ################################################################################
13
14 # define function getfilesize before "set -e" {{{
15   if stat --help >/dev/null 2>&1; then
16     getfilesize='stat -c %s'        # GNU stat
17   else
18     getfilesize='stat -f %z'        # BSD stat
19   fi
20 # }}}
21
22 # adjust variables if necessary through environment {{{
23 # path to the grml2usb script you'd like to use
24   [ -n "$GRML2USB" ] || GRML2USB='grml2usb'
25 # work directory for creating the filesystem
26   [ -n "$WRKDIR" ]   || WRKDIR='/tmp/grml2iso.tmp'
27 # }}}
28
29 # helper stuff {{{
30   set -e
31
32   usage() {
33     echo >&2 "Usage: $0 -o target.iso source1.iso [source2.iso ...]"
34     [ -n "$1" ] && exit $1 || exit 1
35   }
36 # }}}
37
38 # command line handling {{{
39   [[ $# -gt 2 ]] || usage 1
40
41   ISOFILE=''
42   while getopts ao: name; do
43     case $name in
44       o)   ISOFILE="$OPTARG";;
45       ?)   usage 2;;
46     esac
47   done
48
49 # make sure -o is specified
50   [ -n "$ISOFILE" ] || usage 1
51
52 # we don't to override any files by accident
53   if [ -e "$ISOFILE" ]; then
54     echo "Error: target file $ISOFILE exists already." >&2
55     exit 1
56   fi
57
58   shift $(($OPTIND - 1))
59 # }}}
60
61 # we need root permissions for executing grml2usb {{{
62   if [[ $(id -u) != 0 ]]; then
63     echo >&2 "Error: please run $0 as root."
64     exit 1
65   fi
66 # }}}
67
68 # variables {{{
69   ORIG_DIR="$(pwd)"
70   # note: grub-pc_1.96+20090603-1 seems to be b0rken
71   GRUB_VERSION="grub-pc_1.96+20080724-16"
72
73 # normalise path
74   case $ISOFILE in
75     /*) ;;
76     *) ISOFILE=$ORIG_DIR/$ISOFILE ;;
77   esac
78 # }}}
79
80 # create necessary stuff under WRKDIR {{{
81   [ -d "$WRKDIR" ] && WRKDIR_EXISTED='true' || WRKDIR_EXISTED='false'
82   rm -rf "$WRKDIR/cddir" "$WRKDIR/grub_tmp"
83   mkdir -p "$WRKDIR/cddir" "$WRKDIR/grub_tmp"
84 # }}}}
85
86 # execute grml2usb with all ISOs you'd like to install {{{
87   $GRML2USB "$@" "$WRKDIR/cddir"
88 # }}}
89
90 # install grub2 files {{{
91 # as we don't want to rely on a grub2 installation on the
92 # running system let's grab it from the net
93   if which dpkg >/dev/null 2>&1 ; then
94      ARCH="$(dpkg --print-architecture)"
95   else
96     [[ $(uname -m) == x86_64 ]] && ARCH=amd64 || ARCH=i386
97   fi
98   GRUB_DEB="${GRUB_VERSION}_${ARCH}.deb"
99
100   cd "$WRKDIR"/grub_tmp/
101   wget http://ftp.de.debian.org/debian/pool/main/g/grub2/"$GRUB_DEB"
102   ar x "$GRUB_DEB"
103   tar xzf data.tar.gz
104   ./usr/bin/grub-mkimage -d usr/lib/grub/i386-pc \
105     -o "$WRKDIR"/cddir/boot/grub/core.img biosdisk iso9660
106
107   for a in usr/lib/grub/i386-pc/{*.mod,efiemu??.o,command.lst,moddep.lst,fs.lst,handler.lst,parttool.lst}; do
108     test -e $a && cp $a "$WRKDIR"/cddir/boot/grub/
109   done
110
111   cat usr/lib/grub/i386-pc/cdboot.img "$WRKDIR"/cddir/boot/grub/core.img \
112     > "$WRKDIR"/cddir/boot/grub/eltorito.img
113   cd "$WRKDIR/cddir"
114 # }}}
115
116 # generate the CD/DVD ISO {{{
117   mkisofs -V 'grml-multiboot' -l -r -J -no-pad \
118     -no-emul-boot -boot-load-size 4 -boot-info-table \
119     -b boot/grub/eltorito.img -c boot/grub/eltorito.cat \
120     -o "$ISOFILE" .
121 # }}}
122
123 # pad the output ISO to multiples of 256 KiB for partition table support {{{
124   siz=$($getfilesize "$ISOFILE")
125   cyls=$(echo "$siz / 512 / 32 / 16 + 1" | bc)  # C=$cyls H=16 S=32
126   ofs=$(echo "$cyls * 16 * 32 * 512 - 1" | bc)  # padding offset (size - 1)
127   dd if=/dev/zero bs=1 count=1 seek=$ofs of="$ISOFILE" 2>/dev/null
128 # }}}
129
130 # create a manifold-boot image with a partition table, if possible {{{
131   if mksh -c true 2>/dev/null && \
132     test -e /usr/share/grml-live/scripts/bootgrub.mksh; then
133       echo "Creating a manifold-boot ISO image"
134       echo 1 63 | \
135         mksh /usr/share/grml-live/scripts/bootgrub.mksh -A -M 1 -p 0x83 -g $cyls:16:32 | \
136         cat - boot/grub/core.img | \
137         dd conv=notrunc of="$ISOFILE" conv=notrunc 2>/dev/null
138   else
139       echo "Cannot find mksh or bootgrub.mksh, not generating manifold-boot ISO image"
140   fi
141 # }}}
142
143 # cleanup {{{
144   cd "$ORIG_DIR"
145   sync
146   rm -rf "$WRKDIR/cddir" "$WRKDIR/grub_tmp"
147   [[ $WRKDIR_EXISTED = 'false' ]] && rmdir "$WRKDIR"
148   echo "Generated $ISOFILE"
149 # }}}
150
151 ## EOF #########################################################################
152 # vim:foldmethod=marker ts=2 ft=sh ai expandtab tw=80 sw=3