initial commit: 1.5.0
[systemrescuecd.git] / overlay-squashfs-sparc / usr / sbin / sysresccd-custom
1 #!/bin/bash
2 # Project page: http://www.sysresccd.org/
3 # By Francois Dupoux
4 # (C) 2003-2007 Francois Dupoux
5 # This scipt is available under the GPL-2 license.
6
7 ## HELP AND BASIC ARGUMENT PROCESSING
8 #####################################
9
10 usage() 
11 {
12   cat <<EOF
13 sysresccd-custom: SystemRescueCd customization script for SPARC
14 Syntax:           sysresccd-custom <command> ...
15
16 Please, read the manual for help about how to use this script.
17 http://www.sysresccd.org/Sysresccd-manual-en_How_to_personalize_SystemRescueCd
18
19 Commands (execute in that order):
20  1) extract                    Extract files from the squashfs into your hard disk
21  2) squashfs                   Create the new compressed loopback squashfs filesystem
22  3) setkmap <keymap-code>      Force a keymap to be loaded without prompt at startup
23  4) isogen <cd_volume_name>    Generate the final bootable ISO image
24
25  -h|--help                     Display this screen
26
27 Copyright 2003-2007 Francois Dupoux. (http://www.sysresccd.org)
28 Distributed under the GNU Public License version 2
29 EOF
30 }
31
32 ## MISC FUNCTIONS: Many utilities functions
33 ###########################################
34
35 # show the error message ($1 = first line of the message)
36 help_readman()
37 {
38         echo "$1"
39         echo "Please, read the manual for more help about this script"
40         echo "Web: http://www.sysresccd.org"
41         exit 1
42 }
43
44 ## Main
45 ###########################################
46
47 if [ "$1" = "-h" ] || [ "$1" = "--help" ]
48 then
49         usage
50         exit 1
51 fi
52
53 if [ "`whoami`" != "root" ]
54 then
55         help_readman "$0: This script requires root privileges to operate."
56         #exit 1
57 fi
58
59 if [ ! -d "/mnt/custom" ]
60 then
61         help_readman "$0: You need to mount a partition with free space on /mnt/custom"
62         exit 1
63 fi
64
65 cat /proc/mounts | grep " /mnt/custom " > /dev/null
66 if [ $? -eq 0 ]; then
67         echo "/mnt/custom is mounted -> ok"
68 else
69         help_readman "$0: /mnt/custom is not mounted. Cannot continue."
70         exit 1
71 fi
72
73 PROG=${0}
74 ISO_VOLUME="${2}"
75 KEYMAP="${2}"
76
77 ## ERROR HANDLING
78 #####################################
79
80 die()
81 {
82         if [ -n "$1" ]
83         then
84                 echo "${PROG}: error: $1"
85         else
86                 echo "${PROG}: aborting."
87         fi
88         exit 1
89 }
90
91 extract_die()
92 {
93         if [ -n "$1" ]
94         then
95                 echo "do_extract: error: $1"
96         else
97                 echo "do_extract: aborting."
98         fi
99         exit 1
100 }
101
102 squashfs_die()
103 {
104         #umount /mnt/custom/customcd/cloop/files/
105         if [ -n "$1" ]
106         then
107                 echo "do_squashfs: error: $1"
108         else
109                 echo "do_squashfs: aborting."
110         fi
111         exit 1
112 }
113
114 isogen_die()
115 {
116         if [ -n "$1" ]
117         then
118                 echo "do_isogen: error: $1"
119         else
120                 echo "do_isogen: aborting."
121         fi
122         exit 1
123 }
124
125 setkmap_die()
126 {
127         if [ -n "$1" ]
128         then
129                 echo "do_setkmap: error: $1"
130         else
131                 echo "do_setkmap: aborting."
132         fi
133         exit 1
134 }
135
136 ## MISC FUNCTIONS: Many utilities functions
137 ###########################################
138
139 # $1 == MB required by the function
140 check_freespace()
141 {
142         SIZE=`(\df -m -P) | grep " /mnt/custom$" | tail -n 1 | awk '{print $4}'`
143
144         if [ $SIZE -gt $1 ]; then
145                 echo "there is enough estimated free space here ($SIZE MB) -> ok"
146         else
147                 echo "$PROG: not enough room in /mnt/custom"
148                 help_readman "You only have $SIZE MB free, and the script needs at least $1 MB free"
149                 exit 1
150         fi
151 }
152
153 # check the original SystemRescueCd disc is mounted
154 check_origcdmounted()
155 {
156         grep docache /proc/cmdline > /dev/null
157         if [ $? -eq 1 ]; then
158                 echo "docache not used -> ok"
159         fi
160
161         CMDLINE="`cat /proc/cmdline`"
162         for x in $CMDLINE
163         do
164                 if [ "$x" = "docache" ]
165                 then
166                         docache_error=0
167                         cat /proc/mounts | grep " /mnt/cdrom " > /dev/null
168                         if [ $? -eq 1 ]; then
169                                 docache_error=1
170                         fi
171
172                         if [ $docache_error -eq 0 ]; then
173                                 if [ ! -f /mnt/cdrom/sysrcd.dat ]; then
174                                         docache_error=1
175                                 fi
176                                 if [ ! -d /mnt/cdrom/boot ]; then
177                                         docache_error=1
178                                 fi
179                                 if [ ! -f /mnt/cdrom/boot/silo.conf ]; then
180                                         docache_error=1
181                                 fi
182                         fi
183
184                         if [ $docache_error -eq 1 ]; then
185                                 echo "$0: When using docache option you must mount the "
186                                 help_readman "original version of SystemRescueCd."
187                                 exit 1
188                         else
189                                 echo "docache used and original /mnt/cdrom is mounted -> ok"
190                         fi
191
192                 fi
193         done
194 }
195
196 ## MAIN FUNCTIONS: Extracting the squashfs to the hard disk
197 ########################################################
198 do_extract_check()
199 {
200         # check for free space
201         check_freespace 500
202 }
203
204 do_extract()
205 {
206         # check the original cd is mounted
207         check_origcdmounted
208
209         # copy the boot files
210         mkdir -p /mnt/custom/customcd/isoroot/
211         rm -rf /mnt/custom/customcd/isoroot/*
212         cp -a /mnt/cdrom/boot /mnt/custom/customcd/isoroot/ || extract_die
213
214         # extract files
215         mkdir -p /mnt/custom/customcd/files/
216         rm -rf /mnt/custom/customcd/files/*
217         cp -dpR /mnt/livecd/* /mnt/custom/customcd/files/ || extract_die
218 }
219
220 ## MAIN FUNCTIONS: Create the new squashfs image from files
221 ########################################################
222 do_squashfs()
223 {
224         # check for free space
225         check_freespace 350
226
227         mkdir -p /mnt/custom/customcd/
228         mkdir -p /mnt/custom/customcd/files/
229         mkdir -p /mnt/custom/customcd/isoroot/
230         touch "/mnt/custom/customcd/files/customized"
231
232         mksquashfs /mnt/custom/customcd/files/ /mnt/custom/customcd/isoroot/sysrcd.dat || squashfs_die
233         (cd /mnt/custom/customcd/isoroot/ ; md5sum sysrcd.dat > sysrcd.md5)
234
235         # Change permissions to allow the file to be sent by thttpd for PXE-boot
236         chmod 666 /mnt/custom/customcd/isoroot/sysrcd.dat
237         chmod 666 /mnt/custom/customcd/isoroot/sysrcd.md5
238 }
239
240 ## MAIN FUNCTIONS: Force a keymap to be loaded without prompt
241 ########################################################
242 do_setkmap()
243 {
244         if [ "$KEYMAP" = "" ]
245         then
246                 usage
247                 setkmap_die
248         fi
249
250         if [ ! -d "/mnt/custom/customcd/isoroot/boot" ]
251         then
252                 help_readman "$PROG: You have to run command prepiso before setkmap"
253                 exit 1
254         fi
255
256         echo "Keymap to be loaded: ${KEYMAP}"
257
258         # Set keymap in silo.conf
259         cp /mnt/custom/customcd/isoroot/boot/silo.conf /mnt/custom/customcd/isoroot/boot/silo.bak
260         sed -e "s:append:append setkmap=${KEYMAP}:g" /mnt/custom/customcd/isoroot/boot/silo.bak > /mnt/custom/customcd/isoroot/boot/silo.conf
261 }
262
263 ## MAIN FUNCTIONS: Create the new ISO image
264 ########################################################
265 do_isogen()
266 {
267         # check for free space
268         check_freespace 200
269
270         if [ "$ISO_VOLUME" = "" ]
271         then
272                 usage
273                 isogen_die
274         fi
275
276         mkdir -p /mnt/custom/customcd/isofile/
277         rm -rf /mnt/custom/customcd/isofile/*
278
279         if [ ! -d "/mnt/custom/customcd/isoroot/boot" ]
280         then
281                 help_readman "$PROG: You have to run command squashfs before isogen"
282                 exit 1
283         fi
284
285         touch "/mnt/custom/customcd/isoroot/customized"
286
287         echo "Volume name of the CDRom: ${ISO_VOLUME}"
288
289         mkisofs -G /boot/isofs.b -J -V "${ISO_VOLUME}" -B ... -r \
290                 -o /mnt/custom/customcd/isofile/sysresccd-new.iso \
291                 /mnt/custom/customcd/isoroot || isogen_die
292
293         md5sum /mnt/custom/customcd/isofile/sysresccd-new.iso > /mnt/custom/customcd/isofile/sysresccd-new.md5
294
295         echo "Final ISO image: /mnt/custom/customcd/isofile/sysresccd-new.iso"
296 }
297
298
299 ## MAIN SHELL FUNCTION
300 ########################################################
301
302 case "$1" in
303     extract)
304         do_extract_check
305         do_extract;;
306     extract-nosizecheck)
307         do_extract;;
308     squashfs)
309         do_squashfs;;
310     setkmap)
311         do_setkmap;;
312     isogen)
313         do_isogen;;
314     *)
315         usage 
316         exit 1
317         ;;
318 esac
319 exit 0