Fix evms bugs with missing sbin for an initrd
[genkernel.git] / gen_initrd.sh
1 #!/bin/bash
2
3 # create_initrd_loop(size)
4 create_initrd_loop() {
5         local inodes
6         [ "$#" -ne '1' ] && gen_die 'create_initrd_loop(): Not enough arguments!'
7         mkdir -p ${TEMP}/initrd-mount ||
8                 gen_die 'Could not create loopback mount directory!'
9         dd if=/dev/zero of=${TEMP}/initrd-${KV} bs=1k count=${1} >> "${DEBUGFILE}" 2>&1 ||
10                 gen_die "Could not zero initrd-${KV}"
11         mke2fs -F -N500 -q "${TEMP}/initrd-${KV}" >> "${DEBUGFILE}" 2>&1 ||
12                 gen_die "Could not format initrd-${KV}!"
13         mount -t ext2 -o loop "${TEMP}/initrd-${KV}" "${TEMP}/initrd-mount" >> "${DEBUGFILE}" 2>&1 ||
14                 gen_die 'Could not mount the initrd filesystem!'
15 }
16
17 create_initrd_unmount_loop()
18 {
19         cd ${TEMP}
20         umount "${TEMP}/initrd-mount" ||
21                 gen_die 'Could not unmount initrd system!'
22 }
23
24 move_initrd_to_loop()
25 {
26         cd "${TEMP}/initrd-temp"
27         mv * "${TEMP}/initrd-mount" >> ${DEBUGFILE} 2>&1
28 }
29
30 create_base_initrd_sys() {
31         rm -rf "${TEMP}/initrd-temp" > /dev/null
32         mkdir -p ${TEMP}/initrd-temp/dev
33         mkdir -p ${TEMP}/initrd-temp/bin
34         mkdir -p ${TEMP}/initrd-temp/etc
35         mkdir -p ${TEMP}/initrd-temp/usr
36         mkdir -p ${TEMP}/initrd-temp/proc
37         mkdir -p ${TEMP}/initrd-temp/temp
38         mkdir -p ${TEMP}/initrd-temp/sys
39         mkdir -p ${TEMP}/initrd-temp/.initrd
40         mkdir -p ${TEMP}/initrd-temp/var/lock/dmraid
41         ln -s bin ${TEMP}/initrd-temp/sbin
42         ln -s ../bin ${TEMP}/initrd-temp/usr/bin
43         ln -s ../bin ${TEMP}/initrd-temp/usr/sbin
44
45         echo "/dev/ram0     /           ext2    defaults" > ${TEMP}/initrd-temp/etc/fstab
46         echo "proc          /proc       proc    defaults    0 0" >> ${TEMP}/initrd-temp/etc/fstab
47
48         if [ "${NODEVFSD}" = '' ]
49         then
50                 echo "REGISTER        .*           MKOLDCOMPAT" > ${TEMP}/initrd-temp/etc/devfsd.conf
51                 echo "UNREGISTER      .*           RMOLDCOMPAT" >> ${TEMP}/initrd-temp/etc/devfsd.conf
52                 echo "REGISTER        .*           MKNEWCOMPAT" >> ${TEMP}/initrd-temp/etc/devfsd.conf
53                 echo "UNREGISTER      .*           RMNEWCOMPAT" >> ${TEMP}/initrd-temp/etc/devfsd.conf
54         fi
55
56         cd ${TEMP}/initrd-temp/dev
57         MAKEDEV std
58         MAKEDEV console
59
60         if [ "${DISKLABEL}" -eq '1' ]; then
61                 cp "${BLKID_BINCACHE}" "${TEMP}/initrd-temp/bin/blkid.bz2" ||
62                         gen_die 'Could not copy blkid from bincache!'
63                 bunzip2 "${TEMP}/initrd-temp/bin/blkid.bz2" ||
64                         gen_die 'Could not uncompress blkid!'
65                 chmod +x "${TEMP}/initrd-temp/bin/blkid"
66         fi
67
68         cp "${BUSYBOX_BINCACHE}" "${TEMP}/initrd-temp/bin/busybox.bz2" ||
69                 gen_die 'Could not copy busybox from bincache!'
70         bunzip2 "${TEMP}/initrd-temp/bin/busybox.bz2" ||
71                 gen_die 'Could not uncompress busybox!'
72         chmod +x "${TEMP}/initrd-temp/bin/busybox"
73
74         if [ "${NOINITRDMODULES}" = '' ]
75         then
76                 if [ "${PAT}" -gt "4" ]
77                 then
78                         cp "${MODULE_INIT_TOOLS_BINCACHE}" "${TEMP}/initrd-temp/bin/insmod.static.bz2" ||
79                                 gen_die 'Could not copy insmod.static from bincache!'
80                 else
81                         cp "${MODUTILS_BINCACHE}" "${TEMP}/initrd-temp/bin/insmod.static.bz2" ||
82                                 gen_die 'Could not copy insmod.static from bincache'
83                 fi
84
85                 bunzip2 "${TEMP}/initrd-temp/bin/insmod.static.bz2" ||
86                         gen_die 'Could not uncompress insmod.static!'
87                 mv "${TEMP}/initrd-temp/bin/insmod.static" "${TEMP}/initrd-temp/bin/insmod"
88                 chmod +x "${TEMP}/initrd-temp/bin/insmod"
89         fi
90
91         # devfsd
92         if [ "${NODEVFSD}" = '' ]
93         then
94                 cp "${DEVFSD_BINCACHE}" "${TEMP}/initrd-temp/bin/devfsd.bz2" || gen_die 'Could not copy devfsd executable from bincache!'
95                 bunzip2 "${TEMP}/initrd-temp/bin/devfsd.bz2" || gen_die 'Could not uncompress devfsd!'
96                 chmod +x "${TEMP}/initrd-temp/bin/devfsd"
97         fi
98
99         # udev
100         if [ "${UDEV}" -eq '1' ]
101         then
102                 /bin/tar -jxpf "${UDEV_BINCACHE}" -C "${TEMP}/initrd-temp" || gen_die 'Could not extract udev binary cache!'
103                 ln -sf "./udev" "${TEMP}/initrd-temp/bin/udevstart" ||  gen_die 'Could not symlink udev -> udevstart!'
104         fi
105         
106         #unionfs modules
107         if [ "${UNIONFS}" -eq '1' ]
108         then
109                 print_info 1 'UNIONFS MODULES: Adding support (compiling)...'
110                 compile_unionfs_modules
111                 /bin/tar -jxpf "${UNIONFS_MODULES_BINCACHE}" -C "${TEMP}/initrd-temp" ||
112                         gen_die "Could not extract unionfs modules binary cache!";
113         fi
114         
115         #unionfs utils
116         if [ "${UNIONFS}" -eq '1' ]
117         then
118                 print_info 1 'UNIONFS TOOLS: Adding support (compiling)...'
119                 compile_unionfs_utils
120                 /bin/tar -jxpf "${UNIONFS_BINCACHE}" -C "${TEMP}/initrd-temp" ||
121                         gen_die "Could not extract unionfs tools binary cache!";
122         fi
123
124
125         # DMRAID 
126         if [ "${DMRAID}" = '1' ]
127         then
128                 print_info 1 'DMRAID: Adding support (compiling binaries)...'
129                 compile_dmraid
130                 /bin/tar -jxpf "${DMRAID_BINCACHE}" -C "${TEMP}/initrd-temp" ||
131                         gen_die "Could not extract dmraid binary cache!";
132         fi
133
134         # LVM2
135         if [ "${LVM2}" = '1' ]
136         then
137                 if [ -e '/sbin/lvm' ] && ldd /sbin/lvm|grep -q 'not a dynamic executable';
138                 then
139                         print_info 1 'LVM2: Adding support (using local static binaries)...'
140                         cp /sbin/lvm "${TEMP}/initrd-temp/bin/lvm" ||
141                                 gen_die 'Could not copy over lvm!'
142                         ln "${TEMP}/initrd-temp/bin/lvm" "${TEMP}/initrd-temp/bin/vgscan" ||
143                                 gen_die 'Could not symlink lvm -> vgscan!'
144                         ln "${TEMP}/initrd-temp/bin/lvm" "${TEMP}/initrd-temp/bin/vgchange" ||
145                                 gen_die 'Could not symlink lvm -> vgchange!'
146                 else
147                         print_info 1 'LVM2: Adding support (compiling binaries)...'
148                         compile_lvm2
149
150                         /bin/tar -jxpf "${LVM2_BINCACHE}" -C "${TEMP}/initrd-temp" ||
151                                 gen_die "Could not extract lvm2 binary cache!";
152                         mv ${TEMP}/initrd-temp/bin/lvm.static ${TEMP}/initrd-temp/bin/lvm ||
153                                 gen_die 'LVM2 error: Could not move lvm.static to lvm!'
154                         for i in vgchange vgscan; do
155                                 ln  ${TEMP}/initrd-temp/bin/lvm ${TEMP}/initrd-temp/bin/$i ||
156                                         gen_die "LVM2 error: Could not link ${i}!"
157                         done
158                 fi      
159         fi
160         
161         # EVMS2
162         if [ "${EVMS2}" = '1' ]
163         then
164                 if [ -e '/sbin/evms_activate' ]
165                 then
166                         print_info 1 'EVMS2: Adding support...' 
167                         mkdir -p ${TEMP}/initrd-temp/lib
168                         mkdir -p ${TEMP}/initrd-temp/sbin
169                         mkdir -p ${TEMP}/initrd-temp/etc
170                         cp -a /lib/ld-* "${TEMP}/initrd-temp/lib" || gen_die 'Could not copy files for EVMS2!'
171                         cp -a /lib/libc-* /lib/libc.* "${TEMP}/initrd-temp/lib" || gen_die 'Could not copy files for EVMS2!'
172                         cp -a /lib/libdl-* /lib/libdl.* "${TEMP}/initrd-temp/lib" || gen_die 'Could not copy files for EVMS2!'
173                         cp -a /lib/libpthread* "${TEMP}/initrd-temp/lib" || gen_die 'Could not copy files for EVMS2!'
174                         cp -a /lib/libuuid*so* "${TEMP}/initrd-temp/lib" || gen_die 'Could not copy files for EVMS2!'
175                         cp -a /lib/libevms*so* "${TEMP}/initrd-temp/lib" || gen_die 'Could not copy files for EVMS2!'
176                         cp -a /lib/evms "${TEMP}/initrd-temp/lib" || gen_die 'Could not copy files for EVMS2!'
177                         cp -a /lib/evms/* "${TEMP}/initrd-temp/lib/evms" || gen_die 'Could not copy files for EVMS2!'
178                         cp -a /etc/evms.conf "${TEMP}/initrd-temp/etc" || gen_die 'Could not copy files for EVMS2!'
179                         cp /sbin/evms_activate "${TEMP}/initrd-temp/sbin/evms_activate" || gen_die 'Could not copy over evms_activate!'
180                         # Fix EVMS2 complaining that it cant find the swap utilities.
181                         # These are not required in the initrd
182                         for swap_libs in "${TEMP}/initrd-temp/lib/evms/*/swap*.so"
183                         do
184                                 rm ${swap_libs}
185                         done
186                 fi
187         fi      
188
189         for i in '[' ash basename cat chroot clear cp dirname echo env false find \
190         grep gunzip gzip ln ls loadkmap losetup lsmod mkdir mknod more mount mv \
191         pivot_root ps awk pwd rm rmdir rmmod sed sh sleep tar test touch true umount uname \
192         xargs yes zcat chmod chown cut kill killall; do
193                 rm -f ${TEMP}/initrd-temp/bin/$i > /dev/null
194                 ln  ${TEMP}/initrd-temp/bin/busybox ${TEMP}/initrd-temp/bin/$i ||
195                         gen_die "Busybox error: could not link ${i}!"
196         done
197 }
198
199 print_list()
200 {
201         local x
202         for x in ${*}
203         do
204                 echo ${x}
205         done
206 }
207
208 create_initrd_modules() {
209         local group
210         local group_modules
211         
212         if [ "${PAT}" -gt "4" ]
213         then
214                 MOD_EXT=".ko"
215         else
216                 MOD_EXT=".o"
217         fi
218
219         print_info 2 "initrd: >> Searching for modules..."
220
221         if [ "${INSTALL_MOD_PATH}" != '' ]
222         then
223                 cd ${INSTALL_MOD_PATH}
224         else
225                 cd /
226         fi
227                                                                                                         
228         for i in `gen_dep_list`
229         do
230                 mymod=`find ./lib/modules/${KV} -name "${i}${MOD_EXT}" 2>/dev/null| head -n 1`
231                 if [ -z "${mymod}" ]
232                 then
233                         print_warning 2 "Warning :: ${i}${MOD_EXT} not found; skipping..."
234                         continue;
235                 fi
236                 print_info 2 "initrd: >> Copying ${i}${MOD_EXT}..."
237                 cp -ax --parents "${mymod}" "${TEMP}/initrd-temp"
238         done
239
240         cp -ax --parents ./lib/modules/${KV}/modules* ${TEMP}/initrd-temp 2>/dev/null
241
242         mkdir -p "${TEMP}/initrd-temp/etc/modules"
243         for group_modules in ${!MODULES_*}; do
244                 group="$(echo $group_modules | cut -d_ -f2 | tr "[:upper:]" "[:lower:]")"
245                 print_list ${!group_modules} > "${TEMP}/initrd-temp/etc/modules/${group}"
246         done
247 }
248
249 create_initrd_aux() {
250         if [ -f "${CMD_LINUXRC}" ]
251         then
252                 cp "${CMD_LINUXRC}" "${TEMP}/initrd-temp/linuxrc"
253                 print_info 2 "        >> Copying user specified linuxrc: ${CMD_LINUXRC}"
254         else    
255                 if [ -f "${GK_SHARE}/${ARCH}/linuxrc" ]
256                 then
257                         cp "${GK_SHARE}/${ARCH}/linuxrc" "${TEMP}/initrd-temp/linuxrc"
258                 else
259                         cp "${GK_SHARE}/generic/linuxrc" "${TEMP}/initrd-temp/linuxrc"
260                 fi
261         fi
262
263         if [ -f "${GK_SHARE}/${ARCH}/initrd.scripts" ]
264         then
265                 cp "${GK_SHARE}/${ARCH}/initrd.scripts" "${TEMP}/initrd-temp/etc/initrd.scripts"
266         else    
267                 cp "${GK_SHARE}/generic/initrd.scripts" "${TEMP}/initrd-temp/etc/initrd.scripts"
268         fi
269
270         if [ -f "${GK_SHARE}/${ARCH}/initrd.defaults" ]
271         then
272                 cp "${GK_SHARE}/${ARCH}/initrd.defaults" "${TEMP}/initrd-temp/etc/initrd.defaults"
273         else
274                 cp "${GK_SHARE}/generic/initrd.defaults" "${TEMP}/initrd-temp/etc/initrd.defaults"
275         fi
276         
277         echo -n 'HWOPTS="$HWOPTS ' >> "${TEMP}/initrd-temp/etc/initrd.defaults" 
278         for group_modules in ${!MODULES_*}; do
279                 group="$(echo $group_modules | cut -d_ -f2 | tr "[:upper:]" "[:lower:]")"
280                 echo -n "${group} " >> "${TEMP}/initrd-temp/etc/initrd.defaults"
281         done
282         echo '"' >> "${TEMP}/initrd-temp/etc/initrd.defaults"   
283
284         if [ -f "${GK_SHARE}/${ARCH}/modprobe" ]
285         then
286                 cp "${GK_SHARE}/${ARCH}/modprobe" "${TEMP}/initrd-temp/sbin/modprobe"
287         else
288                 cp "${GK_SHARE}/generic/modprobe" "${TEMP}/initrd-temp/sbin/modprobe"
289         fi
290         if isTrue $CMD_DOKEYMAPAUTO
291         then
292                 echo 'MY_HWOPTS="${MY_HWOPTS} keymap"' >> ${TEMP}/initrd-temp/etc/initrd.defaults
293         fi
294         mkdir -p "${TEMP}/initrd-temp/lib/keymaps"
295         /bin/tar -C "${TEMP}/initrd-temp/lib/keymaps" -zxf "${GK_SHARE}/generic/keymaps.tar.gz"
296
297         cd ${TEMP}/initrd-temp/sbin && ln -s ../linuxrc init
298         cd ${OLDPWD}
299         chmod +x "${TEMP}/initrd-temp/linuxrc"
300         chmod +x "${TEMP}/initrd-temp/etc/initrd.scripts"
301         chmod +x "${TEMP}/initrd-temp/etc/initrd.defaults"
302         chmod +x "${TEMP}/initrd-temp/sbin/modprobe"
303 }
304
305 calc_initrd_size() {
306         local TEST
307         cd ${TEMP}/initrd-temp/
308         TEST=`du -sk 2> /dev/null` 
309         echo $TEST | cut "-d " -f1
310 }
311
312 create_initrd() {
313         local MOD_EXT
314
315         print_info 1 "initrd: >> Initializing..."
316         create_base_initrd_sys
317
318         if [ "${NOINITRDMODULES}" = '' ]
319         then
320                 print_info 1 "        >> Copying modules..."
321                 create_initrd_modules
322         else
323                 print_info 1 "initrd: Not copying modules..."
324         fi
325
326         print_info 1 "        >> Copying auxilary files..."
327         create_initrd_aux
328
329         INITRD_CALC_SIZE=`calc_initrd_size`
330         INITRD_SIZE=`expr ${INITRD_CALC_SIZE} + 100`
331         print_info 1 "        :: Size is at ${INITRD_SIZE}K"
332         print_info 1 "        >> Creating loopback filesystem..."
333         create_initrd_loop ${INITRD_SIZE}
334
335         print_info 1 "        >> Moving initrd files to the loopback..."
336         move_initrd_to_loop
337
338         print_info 1 "        >> Cleaning up and compressing the initrd..."
339         create_initrd_unmount_loop
340
341         if [ "${COMPRESS_INITRD}" ]
342         then
343                 gzip -f -9 ${TEMP}/initrd-${KV}
344                 mv ${TEMP}/initrd-${KV}.gz ${TEMP}/initrd-${KV}
345         fi
346
347         if [ "${BOOTSPLASH}" -eq "1" ]
348         then
349                 if [ -x /sbin/splash ]
350                 then
351                         [ -z "${BOOTSPLASH_THEME}" ] && [ -e /etc/conf.d/bootsplash.conf ] && source /etc/conf.d/bootsplash.conf
352                         [ -z "${BOOTSPLASH_THEME}" ] && [ -e /etc/conf.d/bootsplash ] && source /etc/conf.d/bootsplash
353                         [ -z "${BOOTSPLASH_THEME}" ] && BOOTSPLASH_THEME=default
354                         print_info 1 "        >> Installing bootsplash [ using the ${BOOTSPLASH_THEME} theme ]..."
355                         for bootRes in '800x600' '1024x768' '1280x1024' '1600x1200'
356                         do
357                                 if [ -f "/etc/bootsplash/${BOOTSPLASH_THEME}/config/bootsplash-${bootRes}.cfg" ]
358                                 then
359                                         /sbin/splash -s -f /etc/bootsplash/${BOOTSPLASH_THEME}/config/bootsplash-${bootRes}.cfg >> ${TEMP}/initrd-${KV} ||
360                                                 gen_die "Error: could not copy ${bootRes} bootsplash!"
361                                 else
362                                         print_warning 1 "splash: Did not find a bootsplash for the ${bootRes} resolution..."
363                                 fi
364                         done
365                 else
366                         print_warning 1 '        >> No bootsplash detected; skipping!'
367                 fi
368         fi
369         if ! isTrue "${CMD_NOINSTALL}"
370         then
371                 cp ${TEMP}/initrd-${KV} /boot/initrd-${KNAME}-${ARCH}-${KV} ||
372                         gen_die 'Could not copy the initrd to /boot!'
373         fi
374         [ "${KERNEL_MAKE_DIRECTIVE}" == 'zImage.initrd' ] ||
375                 [ "${KERNEL_MAKE_DIRECTIVE_2}" == 'zImage.initrd' ] &&
376                 cp ${TEMP}/initrd-${KV} ${KERNEL_DIR}/arch/${ARCH}/boot/images/ramdisk.image.gz
377 }