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