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