f6d96b95babf8f3f1321500ab41c650445495e81
[grml2usb.git] / grml2iso
1 #!/bin/bash
2 ################################################################################
3 # Filename: grml2iso
4 # Purpose:  create a multiboot ISO using grml2usb
5 # Author:   Michael Prokop <mika@grml.org>,
6 #           Thorsten Glaser <tg@mirbsd.org>
7 ################################################################################
8 # TODO:
9 # * generalise the script so we can provide it as part of the grml2usb suite
10 # * improve error handling
11 # * support isolinux as bootloader on the ISO
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 usage() {
22   echo >&2 "Usage: $0 target.iso source1.iso [source2.iso ...]"
23   exit 1
24 }
25
26 set -e
27 set -u
28
29 # ********* PLEASE ADJUST! *******************************************
30 # specify path to the grml2usb script you'd like to use
31 GRML2USB='grml2usb'
32 # work directory for creating the filesystem
33 WRKDIR='/dev/shm/grml2iso.tmp'
34 # ********* PLEASE ADJUST! *******************************************
35
36 test $# -lt 2 && usage
37 if test -e "$1"; then
38   echo >&2 Error: target file "'$1'" already exists.
39   usage
40 fi
41 ISOFILE=$1; shift
42
43 # variables
44   ORIG_DIR="$(pwd)"
45   # note: grub-pc_1.96+20090603-1_i386.deb seems to be b0rken
46   GRUB_DEB="grub-pc_1.96+20080724-16_i386.deb"
47
48 # normalise path (in mksh I’d just use the realpath builtin…)
49 case $ISOFILE in
50 /*) ;;
51 *) ISOFILE=$ORIG_DIR/$ISOFILE ;;
52 esac
53
54 # create necessary stuff under WRKDIR
55   rm -rf "$WRKDIR/cddir" "$WRKDIR/grub_tmp"
56   mkdir -p "$WRKDIR/cddir" "$WRKDIR/grub_tmp"
57
58 # execute grml2usb with all ISOs you'd like to install
59   $GRML2USB "$@" "$WRKDIR/cddir"
60
61 # install grub2 files
62 # as we don't want to rely on a grub2 installation on the
63 # running system let's grab it from the net
64   cd "$WRKDIR"/grub_tmp/
65   wget http://ftp.de.debian.org/debian/pool/main/g/grub2/"$GRUB_DEB"
66   ar x "$GRUB_DEB"
67   tar xzf data.tar.gz
68   ./usr/bin/grub-mkimage -d usr/lib/grub/i386-pc \
69     -o "$WRKDIR"/cddir/boot/grub/core.img biosdisk iso9660
70
71   for a in usr/lib/grub/i386-pc/{*.mod,efiemu??.o,command.lst,moddep.lst,fs.lst,handler.lst,parttool.lst}; do
72     test -e $a && cp $a "$WRKDIR"/cddir/boot/grub/
73   done
74
75   cat usr/lib/grub/i386-pc/cdboot.img "$WRKDIR"/cddir/boot/grub/core.img \
76     > "$WRKDIR"/cddir/boot/grub/eltorito.img
77   cd "$WRKDIR/cddir"
78
79 # finally generate the CD/DVD-iso
80   mkisofs -V 'grml-multiboot' -l -r -J -no-pad \
81     -no-emul-boot -boot-load-size 4 -boot-info-table \
82     -b boot/grub/eltorito.img -c boot/grub/eltorito.cat \
83     -o "$ISOFILE" .
84
85 # pad the output ISO to multiples of 256 KiB for partition table support
86   siz=$($getfilesize "$ISOFILE")
87   cyls=$(echo "$siz / 512 / 32 / 16 + 1" | bc)  # C=$cyls H=16 S=32
88   ofs=$(echo "$cyls * 16 * 32 * 512 - 1" | bc)  # padding offset (size - 1)
89   dd if=/dev/zero bs=1 count=1 seek=$ofs of="$ISOFILE" 2>/dev/null
90
91 # create a manifold-boot image with a partition table, if possible
92   if mksh -c true 2>/dev/null && \
93     test -e /usr/share/grml-live/scripts/bootgrub.mksh; then
94       echo "Creating a manifold-boot ISO image"
95       echo 1 63 | \
96         mksh /usr/share/grml-live/scripts/bootgrub.mksh -A -M 1 -p 0x83 -g $cyls:16:32 | \
97         cat - boot/grub/core.img | \
98         dd conv=notrunc of="$ISOFILE" conv=notrunc 2>/dev/null
99   else
100       echo "Cannot find mksh or bootgrub.mksh, not generating manifold-boot ISO image"
101   fi
102
103 # cleanup
104   cd "$ORIG_DIR"
105   sync
106   rm -rf "$WRKDIR/cddir" "$WRKDIR/grub_tmp"
107
108 # we are done :)
109   echo "Generated $ISOFILE"
110
111 ## EOF #########################################################################