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