92ef074787923fea190bc082637be41d344ad7a7
[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 check4progs mkisofs stat || exit 1
45
46 # allow overriding via environment:
47 if [ -z "$MKSQUASHFS" ] ; then
48   if which mksquashfs-lzma >/dev/null 2>&1 ; then
49     MKSQUASHFS=mksquashfs-lzma
50   elif which mksquashfs >/dev/null 2>&1 ; then
51     MKSQUASHFS=mksquashfs
52   else
53     echo "Error: neither mksquashfs-lzma nor mksquashfs present. Exiting."
54     exit 1
55   fi
56 fi
57 check4root || exit 1
58 # }}}
59
60 if [ x"$1" == x ]; then
61    echo "$0 - version $VERSION"
62    echo ""
63    echo "Usage: $0 destination.iso"
64    echo "  destination.iso should point to a path that is on a hard disk,"
65    echo "  you might want to mount some swap partitions or swap files"
66    echo "  first, because grml-live-remaster will need a lot ot RAM."
67    echo ""
68    echo "Please report bugs and feature requests: http://grml.org/bugs/"
69    exit 1
70 fi
71
72 if [ ! -d /remaster ]; then
73    mkdir -p /remaster/chroot /remaster/tmp /remaster/cdrom
74    mount -t tmpfs tmpfs /remaster/tmp
75    echo "#:# edit the following two lines to change the boot message" \
76            >/remaster/msg
77    echo "#:#" >>/remaster/msg
78    if [ -r /live/image/boot/isolinux/boot.msg ] ; then
79       sed 1,2d /live/image/boot/isolinux/boot.msg >>/remaster/msg
80    else
81       sed 1,2d /live/image/boot.msg >>/remaster/msg
82    fi
83 fi
84
85 SQUASHFS_FILE="$(find /live/image/live -name \*.squashfs | head -1)"
86 if ! grep -q "/remaster/cdrom squashfs" /proc/mounts ;  then
87    mount -t squashfs "$SQUASHFS_FILE" /remaster/cdrom -o ro,loop
88 fi
89
90 if ! grep -q "aufs /remaster/chroot" /proc/mounts ; then
91    mount -t aufs aufs /remaster/chroot -o br:/remaster/tmp=rw:/remaster/cdrom=rr
92 fi
93
94 for i in dev proc root sys tmp; do
95     mount --bind /$i /remaster/chroot/$i
96 done
97
98 echo "Now edit the contents of the live CD in this chrooted shell:"
99 chroot /remaster/chroot
100
101 for i in dev proc root sys tmp; do
102         umount /remaster/chroot/$i
103 done
104
105 ${GRML_LIVE_EDITOR} /remaster/msg
106
107 [ -d /remaster/iso ] || mkdir /remaster/iso
108
109 for i in /live/image/*; do
110     if [ ! $i == /live/image/live ]; then
111        cp -R $i /remaster/iso
112     fi
113 done
114
115 if [ -r /remaster/iso/boot/isolinux/boot.msg ] ; then
116    rm /remaster/iso/boot/isolinux/boot.msg
117 fi
118
119 # make sure we support usb sticks as well:
120 if [ -d /live/image/boot/isolinux ] ; then
121    BOOTSTUFF=/live/image/boot/isolinux
122 else
123    BOOTSTUFF=/live/image
124 fi
125
126 [ -d /remaster/iso/boot/isolinux ] || mkdir -p /remaster/iso/boot/isolinux
127
128 sed 3,4d "${BOOTSTUFF}"/boot.msg \
129         >/remaster/iso/boot/isolinux/boot.msg
130 sed 1,2d /remaster/msg >>/remaster/iso/boot/isolinux/boot.msg
131
132 mkdir /remaster/iso/live
133 $MKSQUASHFS /remaster/chroot /remaster/iso/live/"$(basename $SQUASHFS_FILE)"
134 umount /remaster/chroot /remaster/cdrom
135
136 if [ -f /remaster/iso/boot/isolinux/isolinux.bin ] ; then
137    ISOLINUX=boot/isolinux/isolinux.bin
138    ISOLINUX_BOOTCAT=boot/isolinux/boot.cat
139 else
140    ISOLINUX=isolinux.bin
141    ISOLINUX_BOOTCAT=boot.cat
142 fi
143
144 mkisofs -b $ISOLINUX -no-emul-boot -c $ISOLINUX_BOOTCAT \
145         -boot-info-table -boot-load-size 4 -no-pad \
146         -l -r -J -o "$1" /remaster/iso
147 # pad for partition table
148 siz=$($getfilesize "$1")
149 cyls=$((siz / 512 / 32 / 16 + 1))       # C=$cyls H=16 S=32 (= 256 KiB units)
150 siz=$((cyls * 16 * 32 * 512))           # size after padding
151 dd if=/dev/zero bs=1 count=1 seek=$((siz - 1)) of="$1" 2>/dev/null
152 rm -R /remaster/iso
153
154 # here is the place where we could apply bootgrub.mksh
155
156 echo ""
157 echo "ISO generation complete:"
158 ls --color -l "$1"
159 echo "If you want to customize your ISO, just call grml-live-remaster again."
160
161 ## END OF FILE #################################################################
162 # vim: ai filetype=sh expandtab