initial commit: 1.5.0
[systemrescuecd.git] / portage-overlay / sys-apps / sysresccd-scripts / files / sysresccd-custom
1 #!/bin/bash
2 # Project page: http://www.sysresccd.org/
3 # (C) 2003-2008 Francois Dupoux
4 # This scipt is available under the GPL-2 license
5
6 ## HELP AND BASIC ARGUMENT PROCESSING
7 #####################################
8
9 usage()
10 {
11         cat <<EOF
12 sysresccd-custom: SystemRescueCd customization script for x86
13 Syntax:           sysresccd-custom <command> ...
14
15 Please, read the manual for help about how to use this script.
16 http://www.sysresccd.org/Sysresccd-manual-en_How_to_personalize_SystemRescueCd
17 This script must be executed outside of the chroot environment.
18
19 Commands (execute in that order):
20  1) extract                    Extract files from the squashfs into your disk
21  2) squashfs                   Recreate the compressed loopback squashfs image
22  3) setkmap <keymap-code>      Force a keymap to be loaded without prompt
23  4) isogen <cd_volume_name>    Generate the final bootable ISO image
24
25  -h|--help                     Display this screen
26
27 Distributed under the GNU Public License version 2 - http://www.sysresccd.org
28 EOF
29 }
30
31 ## MISC FUNCTIONS: Many utilities functions
32 ###########################################
33
34 # show the error message ($1 = first line of the message)
35 help_readman()
36 {
37         echo "$1"
38         echo "Please, read the manual for more help about this script"
39         echo "Web: http://www.sysresccd.org"
40         exit 1
41 }
42
43 ## Main
44 ###########################################
45
46 if [ "$1" = "-h" ] || [ "$1" = "--help" ]
47 then
48         usage
49         exit 1
50 fi
51
52 if [ "$(whoami)" != "root" ]
53 then
54         help_readman "$0: This script requires root privileges to operate."
55 fi
56
57 if [ ! -d "/mnt/custom" ]
58 then
59         help_readman "$0: You need to mount a partition with free space on /mnt/custom"
60         exit 1
61 fi
62
63 if grep -q " /mnt/custom " /proc/mounts
64 then
65         echo "/mnt/custom is mounted -> ok"
66 else
67         help_readman "$0: /mnt/custom is not mounted. Cannot continue."
68         exit 1
69 fi
70
71 PROG=${0}
72
73 ## ERROR HANDLING
74 #####################################
75
76 die()
77 {
78         if [ -n "$1" ]
79         then
80                 echo "${PROG}: error: $1"
81         else
82                 echo "${PROG}: aborting."
83         fi
84         exit 1
85 }
86
87 ## MISC FUNCTIONS: Many utilities functions
88 ###########################################
89
90 # $1 == MB required by the function
91 check_freespace()
92 {
93         SIZE=`(\df -m -P /mnt/custom) | grep " /mnt/custom$" | tail -n 1 | awk '{print $4}'`
94
95         if [ $SIZE -gt $1 ]
96         then
97                 echo "there is enough estimated free space here ($SIZE MB) -> ok"
98         else
99                 echo "$PROG: not enough room in /mnt/custom"
100                 help_readman "You only have $SIZE MB free, and the script needs at least $1 MB free"
101                 exit 1
102         fi
103 }
104
105 ## MAIN FUNCTIONS: Extracting the squashfs to the hard disk
106 ########################################################
107 do_extract_check()
108 {
109         # check for free space
110         check_freespace 900
111 }
112
113 do_extract()
114 {
115         # ---- check the original contents
116         for curfile in /livemnt/boot/sysrcd.dat /livemnt/boot/sysrcd.md5 /livemnt/boot/isolinux/rescuecd
117         do
118                 if [ ! -f "${curfile}" ]
119                 then
120                         die "File ${curfile} was not found. All the original sysresccd files must be available in /livemnt/boot"
121                 fi
122         done
123         
124         # ---- copy the original contents
125         mkdir -p /mnt/custom/customcd/isoroot/
126         rm -rf /mnt/custom/customcd/isoroot/*
127         
128         # ---- copy critical files and directories
129         for curfile in version isolinux 
130         do
131                 cp -a /livemnt/boot/${curfile} /mnt/custom/customcd/isoroot/ || die "copy: cannot copy ${curfile} to /mnt/custom/customcd/isoroot/"
132         done
133         
134         # ---- copy optionnal files and directories
135         for curfile in bootprog bootdisk ntpasswd
136         do
137                 if ! cp -a /livemnt/boot/${curfile} /mnt/custom/customcd/isoroot/ 2>/dev/null
138                 then
139                         echo "cannot copy ${curfile} to /mnt/custom/customcd/isoroot/ (non critical error)"
140                         rm -rf /mnt/custom/customcd/isoroot/${curfile}
141                         break
142                 fi
143         done
144         
145         # ---- extract files (/livemnt/squashfs is the mount point of sysrcd.dat)
146         mkdir -p /mnt/custom/customcd/files/
147         rm -rf /mnt/custom/customcd/files/*
148         cp -a /livemnt/squashfs/* /mnt/custom/customcd/files/ || die "cannot copy the files from the squashfs filesystem."
149 }
150
151 do_squashfs()
152 {
153         # check for free space
154         check_freespace 350
155
156         # prepare the directories
157         mkdir -p /mnt/custom/customcd/
158         mkdir -p /mnt/custom/customcd/files/
159         mkdir -p /mnt/custom/customcd/isoroot/
160         touch "/mnt/custom/customcd/files/customized"
161         
162         # check that the files have been extracted
163         if [ "$(ls -A /mnt/custom/customcd/files/ 2>/dev/null | wc -l)" -eq 0 ]
164         then
165                 die "squashfs: /mnt/custom/customcd/files/ is empty, your must extract the files first."
166         fi
167
168         # check that there are no remaining filesystems mounted
169         for curfs in proc
170         do
171                 curpath="/mnt/custom/customcd/files/${curfs}"
172                 dircnt="$(ls -A ${curpath} 2>/dev/null | wc -l)"
173                 if [ "${dircnt}" -gt 0 ]
174                 then
175                         die "squashfs: the directory ${curpath} must be empty"
176                 fi
177         done
178
179         rm -f /mnt/custom/customcd/isoroot/sysrcd.dat
180         cmd="mksquashfs /mnt/custom/customcd/files/ /mnt/custom/customcd/isoroot/sysrcd.dat"
181         echo "${cmd}"
182         ${cmd} || die
183         (cd /mnt/custom/customcd/isoroot/ ; md5sum sysrcd.dat > sysrcd.md5)
184
185         # Change permissions to allow the file to be sent by thttpd for PXE-boot
186         chmod 666 /mnt/custom/customcd/isoroot/sysrcd.{dat,md5}
187 }
188
189 ## MAIN FUNCTIONS: Force a keymap to be loaded without prompt
190 ########################################################
191 do_setkmap()
192 {
193         KEYMAP="${1}"
194
195         if [ -z "${KEYMAP}" ]
196         then
197                 die "do_setkmap: you must specify the keymap you want to use (eg: \"$0 setkmap uk\")"
198         fi
199
200         if [ ! -d "/mnt/custom/customcd/isoroot/isolinux" ]
201         then
202                 die "do_setkmap: you have to run command copy before setkmap"
203         fi
204
205         echo "Keymap to be loaded: ${KEYMAP}"
206
207         # Set keymap in isolinux.cfg
208         cp /mnt/custom/customcd/isoroot/isolinux/isolinux.cfg /mnt/custom/customcd/isoroot/isolinux/isolinux.bak
209         sed -i -r -e "s:setkmap=[a-z0-9]+ ::g ; s:append:append setkmap=${KEYMAP}:g" /mnt/custom/customcd/isoroot/isolinux/isolinux.cfg
210
211
212 ## MAIN FUNCTIONS: Create the new ISO image
213 ########################################################
214 do_isogen()
215 {
216         ISO_VOLUME="${1}"
217         curtime="$(date +%Y%m%d-%H%M)"
218
219         # check for free space
220         check_freespace 400
221
222         if [ -z "${ISO_VOLUME}" ]
223         then
224                 die "do_isogen: you must specify the name of the volume you want to use for the iso image."
225         fi
226
227         mkdir -p /mnt/custom/customcd/isofile/
228         rm -rf /mnt/custom/customcd/isofile/*.iso
229
230         if [ ! -d "/mnt/custom/customcd/isoroot/isolinux" ]
231         then
232                 die "do_isogen: you must create a squashfs filesystem before you run isogen"
233         fi
234
235         touch "/mnt/custom/customcd/isoroot/customized"
236
237         echo "Volume name of the CDRom: ${ISO_VOLUME}"
238
239         cmd="mkisofs -J -l -o /mnt/custom/customcd/isofile/sysresccd-${curtime}.iso \
240                 -b isolinux/isolinux.bin -c isolinux/boot.cat -input-charset utf-8 \
241                 -no-emul-boot -boot-load-size 4 -boot-info-table \
242                 -V \"${ISO_VOLUME}\" /mnt/custom/customcd/isoroot"
243         ${cmd}
244         res="$?"
245         echo "${cmd} --> $res"
246         if [ "${res}" != '0' ]
247         then
248                 die "mkisofs failed"
249         fi
250
251         md5sum /mnt/custom/customcd/isofile/sysresccd-${curtime}.iso > /mnt/custom/customcd/isofile/sysresccd-${curtime}.md5
252
253         echo "Final ISO image: /mnt/custom/customcd/isofile/sysresccd-${curtime}.iso"
254 }
255
256 ## MAIN SHELL FUNCTION
257 ########################################################
258
259 COMMAND="${1}"
260 shift
261 case "${COMMAND}" in
262         extract)
263                 do_extract_check "$@"
264                 do_extract
265                 ;;
266         extract-nosizecheck)
267                 do_extract "$@"
268                 ;;
269         squashfs)
270                 do_squashfs "$@"
271                 ;;
272         setkmap)
273                 do_setkmap "$@"
274                 ;;
275         isogen)
276                 do_isogen "$@"
277                 ;;
278         *)
279                 usage 
280                 exit 1
281                 ;;
282 esac
283 exit 0