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