Print warning if grml2usb is not found 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 # 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
88 # we need root permissions for executing grml2usb {{{
89   if [[ $(id -u) != 0 ]]; then
90     echo >&2 "Error: please run $0 as root."
91     exit 1
92   fi
93 # }}}
94
95 # check for grml2usb {{{
96   if [ ! -x "$(which $GRML2USB)" ] || [ ! -x "$GRML2USB" ] ; then
97     echo >&2 "Error: Could not find grml2usb"
98     if [ -x "./$GRML2USB" ] ; then
99       echo >&2 "If you executed grml2iso from the grml2usb repository use"
100       echo >&2 "GRML2USB=./grml2usb $0 $*"
101     fi
102     exit 1
103   fi
104 # }}}
105
106 # variables {{{
107   ORIG_DIR="$(pwd)"
108   # note: grub-pc_1.96+20090603-1 seems to be b0rken
109   GRUB_VERSION="grub-pc_1.96+20080724-16"
110
111 # normalise path
112   case $ISOFILE in
113     /*) ;;
114     *) ISOFILE=$ORIG_DIR/$ISOFILE ;;
115   esac
116 # }}}
117
118 # create necessary stuff under WRKDIR {{{
119   [ -d "$WRKDIR" ] && WRKDIR_EXISTED='true' || WRKDIR_EXISTED='false'
120   rm -rf "$WRKDIR/cddir" "$WRKDIR/grub_tmp"
121   mkdir -p "$WRKDIR/cddir"
122 # }}}}
123
124 # execute grml2usb with all ISOs you'd like to install {{{
125   # remove all parameters
126   shift $(($OPTIND - 1))
127
128   $GRML2USB "${REMOVE_OPTS[@]}" ${ADD_OPTS:+"$ADD_OPTS"} "$@" "$WRKDIR/cddir"
129 # }}}
130
131
132 # move syslinux to isolinux {{{
133   mv "$WRKDIR"/cddir/boot/syslinux "$WRKDIR"/cddir/boot/isolinux
134
135   cd "$WRKDIR/cddir"
136   echo "menu label ^Isolinux prompt" > boot/isolinux/promptname.cfg
137   echo "include hd.cfg" >> boot/isolinux/grmlmain.cfg
138 # }}}
139
140 # copy specified directory to cd {{{
141   if [ -n "$DIR" ] ; then
142      echo >&2 "Copying ${DIR} to generated iso"
143      rsync -a ${DIR}/ .
144   fi
145
146 # }}}
147
148 # generate the CD/DVD ISO {{{
149   $MKISOFS -V 'grml-multiboot' -l -r -J -no-pad \
150     -no-emul-boot -boot-load-size 4 -boot-info-table \
151     -b boot/isolinux/isolinux.bin -c boot/isolinux/boot.cat \
152     -o "$ISOFILE" .
153 # }}}
154
155 # pad the output ISO to multiples of 256 KiB for partition table support {{{
156   siz=$($getfilesize "$ISOFILE")
157   cyls=$(echo "$siz / 512 / 32 / 16 + 1" | bc)  # C=$cyls H=16 S=32
158   ofs=$(echo "$cyls * 16 * 32 * 512 - 1" | bc)  # padding offset (size - 1)
159   dd if=/dev/zero bs=1 count=1 seek=$ofs of="$ISOFILE" 2>/dev/null
160 # }}}
161
162
163 # cleanup {{{
164   cd "$ORIG_DIR"
165   sync
166   rm -rf "$WRKDIR/cddir" "$WRKDIR/grub_tmp"
167   [[ $WRKDIR_EXISTED = 'false' ]] && rmdir "$WRKDIR"
168   echo "Generated $ISOFILE"
169 # }}}
170
171 ## EOF #########################################################################
172 # vim:foldmethod=marker ts=2 ft=sh ai expandtab tw=80 sw=3