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