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