Support arbitrary grml2usb options in grml2iso
[grml2usb.git] / grml2iso
1 #!/usr/bin/env 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
10 # define function getfilesize before "set -e" {{{
11   if stat --help >/dev/null 2>&1; then
12     getfilesize='stat -c %s'        # GNU stat
13   else
14     getfilesize='stat -f %z'        # BSD stat
15   fi
16 # }}}
17
18 # adjust variables if necessary through environment {{{
19 # path to the grml2usb script you'd like to use
20   [ -n "$GRML2USB" ] || GRML2USB='grml2usb'
21 # work directory for creating the filesystem
22   [ -n "$WRKDIR" ]   || WRKDIR='/tmp/grml2iso.tmp'
23 # support mkisofs as well as genisoimage
24   if which mkisofs >/dev/null 2>&1; then
25     MKISOFS='mkisofs'
26   elif which genisoimage >/dev/null 2>&1; then
27     MKISOFS='genisoimage'
28   else
29     echo >&2 "Error: neither mkisofs nor genisoimage available - can not create ISO."
30     exit 1
31 fi
32 # }}}
33
34 # helper stuff {{{
35   set -e
36
37   usage() {
38     echo >&2 "Usage: $0 [OPTIONS] -o target.iso source1.iso [source2.iso ...]"
39     echo >&2 "
40 Options:
41      -b Boot Params      Additional boot parameters passed to grml2usb
42      -c Directory        Copy files from directory to generated iso
43      -f                  Force overwrite of existing target.iso
44      -r BootParam        Remove specified boot params.
45                          Could be specfied multiple times.
46      -p <grml2usb param> Add the specified parameter to the grml2usb
47                          commandline. For a list of valid parameters have
48                          a look at the grml2usb manpage.
49 "
50     [ -n "$1" ] && exit $1 || exit 1
51   }
52 # }}}
53
54 # command line handling {{{
55   [[ $# -gt 2 ]] || usage 1
56
57   ISOFILE=''
58   DIR=''
59   ADD_OPTS=''
60   FORCE=''
61   typeset -a GRML2USB_OPTS
62   while getopts fb:c:o:r:p: name; do
63     case $name in
64       o)   ISOFILE="$OPTARG";;
65       b)   GRML2USB_OPTS+=(--bootoptions="$OPTARG");;
66       c)   DIR="$OPTARG";;
67       f)   FORCE='true';;
68       r)   GRML2USB_OPTS+=(--remove-bootoption="$OPTARG");;
69       p)   GRML2USB_OPTS+=("$OPTARG");;
70       ?)   usage 2;;
71     esac
72   done
73
74 # make sure -o is specified
75   [ -n "$ISOFILE" ] || usage 1
76
77 # we don't to override any files by accident
78   if [ -e "$ISOFILE" -a ! -n "$FORCE" ]; then
79     echo "Error: target file $ISOFILE exists already." >&2
80     exit 1
81   fi
82
83   if [ ! -z "$DIR" -a ! -d "$DIR" ] ; then
84      echo "Error: specified parameter for -c is not a directory" >&2
85      exit 1
86   fi
87 # }}}
88
89 # we need root permissions for executing grml2usb {{{
90   if [[ $(id -u) != 0 ]]; then
91     echo >&2 "Error: please run $0 as root."
92     exit 1
93   fi
94 # }}}
95
96 # check for grml2usb {{{
97   if [ ! -x "$(which $GRML2USB)" ] || [ ! -x "$GRML2USB" ] ; then
98     echo >&2 "Error: Could not find grml2usb"
99     if [ -x "./$GRML2USB" ] ; then
100       echo >&2 "If you executed grml2iso from the grml2usb repository use"
101       echo >&2 "GRML2USB=./grml2usb $0 $*"
102     fi
103     exit 1
104   fi
105 # }}}
106
107 # variables {{{
108   ORIG_DIR="$(pwd)"
109   # note: grub-pc_1.96+20090603-1 seems to be b0rken
110   GRUB_VERSION="grub-pc_1.96+20080724-16"
111
112 # normalise path
113   case $ISOFILE in
114     /*) ;;
115     *) ISOFILE=$ORIG_DIR/$ISOFILE ;;
116   esac
117 # }}}
118
119 # create necessary stuff under WRKDIR {{{
120   [ -d "$WRKDIR" ] && WRKDIR_EXISTED='true' || WRKDIR_EXISTED='false'
121   rm -rf "$WRKDIR/cddir" "$WRKDIR/grub_tmp"
122   mkdir -p "$WRKDIR/cddir"
123 # }}}}
124
125 # execute grml2usb with all ISOs you'd like to install {{{
126   # remove all parameters
127   shift $(($OPTIND - 1))
128
129   $GRML2USB "${GRML2USB_OPTS[@]}" "$@" "$WRKDIR/cddir"
130 # }}}
131
132
133 # move syslinux to isolinux {{{
134   mv "$WRKDIR"/cddir/boot/syslinux "$WRKDIR"/cddir/boot/isolinux
135
136   cd "$WRKDIR/cddir"
137   echo "menu label ^Isolinux prompt" > boot/isolinux/promptname.cfg
138   echo "include hd.cfg" >> boot/isolinux/grmlmain.cfg
139 # }}}
140
141 # copy specified directory to cd {{{
142   if [ -n "$DIR" ] ; then
143      echo >&2 "Copying ${DIR} to generated iso"
144      rsync -a ${DIR}/ .
145   fi
146
147 # }}}
148
149 # generate the CD/DVD ISO {{{
150   $MKISOFS -V 'grml-multiboot' -l -r -J -no-pad \
151     -no-emul-boot -boot-load-size 4 -boot-info-table \
152     -b boot/isolinux/isolinux.bin -c boot/isolinux/boot.cat \
153     -o "$ISOFILE" .
154 # }}}
155
156 # pad the output ISO to multiples of 256 KiB for partition table support {{{
157   siz=$($getfilesize "$ISOFILE")
158   cyls=$(echo "$siz / 512 / 32 / 16 + 1" | bc)  # C=$cyls H=16 S=32
159   ofs=$(echo "$cyls * 16 * 32 * 512 - 1" | bc)  # padding offset (size - 1)
160   dd if=/dev/zero bs=1 count=1 seek=$ofs of="$ISOFILE" 2>/dev/null
161 # }}}
162
163
164 # cleanup {{{
165   cd "$ORIG_DIR"
166   sync
167   rm -rf "$WRKDIR/cddir" "$WRKDIR/grub_tmp"
168   [[ $WRKDIR_EXISTED = 'false' ]] && rmdir "$WRKDIR"
169   echo "Generated $ISOFILE"
170 # }}}
171
172 ## EOF #########################################################################
173 # vim:foldmethod=marker ts=2 ft=sh ai expandtab tw=80 sw=3