grml-live-remaster: Support mkisofs + genisoimage
[grml-live.git] / remaster / grml-live-remaster
1 #!/bin/sh
2 # Filename:      grml-live-remaster
3 # Purpose:       remaster a grml from the live cd
4 # Authors:       grml-team (grml.org),
5 #                (c) Michael Schierl <schierlm@gmx.de>,
6 #                (c) Michael Prokop <mika@grml.org>,
7 #                (c) Thorsten Glaser <tg@mirbsd.org>
8 # Bug-Reports:   see http://grml.org/bugs/
9 # License:       This file is licensed under the GPL v2 or any later version.
10 ################################################################################
11 # DISCLAIMER:
12 # this script currently lacks LOTS of error checking code... any help welcome...
13 ################################################################################
14
15 # define function getfilesize before "set -e"
16 if stat --help >/dev/null 2>&1; then
17   getfilesize='stat -c %s'      # GNU stat
18 else
19   getfilesize='stat -f %z'      # BSD stat
20 fi
21
22 set -e # exit on any error
23
24 VERSION='0.0.3'
25 GRML_LIVE_EDITOR=${VISUAL:-${EDITOR:-vi}}
26
27 # source core functions {{{
28 if ! [ -r /etc/grml/lsb-functions ] || ! [ -r /etc/grml/script-functions ] ; then
29   echo "Error: could not read /etc/grml/lsb-functions and/or /etc/grml/script-functions." >&2
30   echo "Error: $0 can run only in Grml live session. Exiting." >&2
31   exit 1
32 fi
33
34 . /etc/grml/lsb-functions
35 . /etc/grml/script-functions
36 # }}}
37
38 if ! isgrmlcd ; then
39   echo "Error: $0 can run only in Grml live session. Exiting." >&2
40   exit 1
41 fi
42
43 # make sure we have what we need {{{
44 if check4progs mkisofs >/dev/null 2>&1 ; then
45   MKISO=mkisofs
46 fi
47
48 if check4progs genisoimage >/dev/null 2>&1 ; then
49   MKISO=genisoimage
50 fi
51
52 if [ -z "$MKISO" ] ; then
53   echo "Error: neither mkisofs nor genisoimage available. Exiting." >&2
54   exit 1
55 fi
56
57 check4progs stat || exit 1
58
59 # allow overriding via environment:
60 if [ -z "$MKSQUASHFS" ] ; then
61   if which mksquashfs-lzma >/dev/null 2>&1 ; then
62     MKSQUASHFS=mksquashfs-lzma
63   elif which mksquashfs >/dev/null 2>&1 ; then
64     MKSQUASHFS=mksquashfs
65   else
66     echo "Error: neither mksquashfs-lzma nor mksquashfs present. Exiting."
67     exit 1
68   fi
69 fi
70 check4root || exit 1
71 # }}}
72
73 if [ x"$1" == x ]; then
74    echo "$0 - version $VERSION"
75    echo ""
76    echo "Usage: $0 destination.iso"
77    echo "  destination.iso should point to a path that is on a hard disk,"
78    echo "  you might want to mount some swap partitions or swap files"
79    echo "  first, because grml-live-remaster will need a lot ot RAM."
80    echo ""
81    echo "Please report bugs and feature requests: http://grml.org/bugs/"
82    exit 1
83 fi
84
85 if [ ! -d /remaster ]; then
86    mkdir -p /remaster/chroot /remaster/tmp /remaster/cdrom
87    mount -t tmpfs tmpfs /remaster/tmp
88    echo "#:# edit the following two lines to change the boot message" \
89            >/remaster/msg
90    echo "#:#" >>/remaster/msg
91    if [ -r /live/image/boot/isolinux/boot.msg ] ; then
92       sed 1,2d /live/image/boot/isolinux/boot.msg >>/remaster/msg
93    else
94       sed 1,2d /live/image/boot.msg >>/remaster/msg
95    fi
96 fi
97
98 SQUASHFS_FILE="$(find /live/image/live -name \*.squashfs | head -1)"
99 if ! grep -q "/remaster/cdrom squashfs" /proc/mounts ;  then
100    mount -t squashfs "$SQUASHFS_FILE" /remaster/cdrom -o ro,loop
101 fi
102
103 if ! grep -q "aufs /remaster/chroot" /proc/mounts ; then
104    mount -t aufs aufs /remaster/chroot -o br:/remaster/tmp=rw:/remaster/cdrom=rr
105 fi
106
107 for i in dev proc root sys tmp; do
108     mount --bind /$i /remaster/chroot/$i
109 done
110
111 echo "Now edit the contents of the live CD in this chrooted shell:"
112 chroot /remaster/chroot
113
114 for i in dev proc root sys tmp; do
115         umount /remaster/chroot/$i
116 done
117
118 ${GRML_LIVE_EDITOR} /remaster/msg
119
120 [ -d /remaster/iso ] || mkdir /remaster/iso
121
122 for i in /live/image/*; do
123     if [ ! $i == /live/image/live ]; then
124        cp -R $i /remaster/iso
125     fi
126 done
127
128 if [ -r /remaster/iso/boot/isolinux/boot.msg ] ; then
129    rm /remaster/iso/boot/isolinux/boot.msg
130 fi
131
132 # make sure we support usb sticks as well:
133 if [ -d /live/image/boot/isolinux ] ; then
134    BOOTSTUFF=/live/image/boot/isolinux
135 else
136    BOOTSTUFF=/live/image
137 fi
138
139 [ -d /remaster/iso/boot/isolinux ] || mkdir -p /remaster/iso/boot/isolinux
140
141 sed 3,4d "${BOOTSTUFF}"/boot.msg \
142         >/remaster/iso/boot/isolinux/boot.msg
143 sed 1,2d /remaster/msg >>/remaster/iso/boot/isolinux/boot.msg
144
145 mkdir /remaster/iso/live
146 $MKSQUASHFS /remaster/chroot /remaster/iso/live/"$(basename $SQUASHFS_FILE)"
147 umount /remaster/chroot /remaster/cdrom
148
149 if [ -f /remaster/iso/boot/isolinux/isolinux.bin ] ; then
150    ISOLINUX=boot/isolinux/isolinux.bin
151    ISOLINUX_BOOTCAT=boot/isolinux/boot.cat
152 else
153    ISOLINUX=isolinux.bin
154    ISOLINUX_BOOTCAT=boot.cat
155 fi
156
157 $MKISO -b $ISOLINUX -no-emul-boot -c $ISOLINUX_BOOTCAT \
158         -boot-info-table -boot-load-size 4 -no-pad \
159         -l -r -J -o "$1" /remaster/iso
160 # pad for partition table
161 siz=$($getfilesize "$1")
162 cyls=$((siz / 512 / 32 / 16 + 1))       # C=$cyls H=16 S=32 (= 256 KiB units)
163 siz=$((cyls * 16 * 32 * 512))           # size after padding
164 dd if=/dev/zero bs=1 count=1 seek=$((siz - 1)) of="$1" 2>/dev/null
165 rm -R /remaster/iso
166
167 # here is the place where we could apply bootgrub.mksh
168
169 echo ""
170 echo "ISO generation complete:"
171 ls --color -l "$1"
172 echo "If you want to customize your ISO, just call grml-live-remaster again."
173
174 ## END OF FILE #################################################################
175 # vim: ai filetype=sh expandtab