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