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