Removing DEVFS support from genkernel since we don't support 2.4 kernels anymore...
[genkernel.git] / gen_initramfs.sh
1 #!/bin/bash
2
3 CPIO_ARGS="--quiet -o -H newc"
4
5 append_base_layout() {
6         if [ -d "${TEMP}/initramfs-base-temp" ]
7         then
8                 rm -rf "${TEMP}/initramfs-base-temp" > /dev/null
9         fi
10         mkdir -p ${TEMP}/initramfs-base-temp/dev
11         mkdir -p ${TEMP}/initramfs-base-temp/bin
12         mkdir -p ${TEMP}/initramfs-base-temp/etc
13         mkdir -p ${TEMP}/initramfs-base-temp/usr
14         mkdir -p ${TEMP}/initramfs-base-temp/proc
15         mkdir -p ${TEMP}/initramfs-base-temp/temp
16         mkdir -p ${TEMP}/initramfs-base-temp/sys
17         mkdir -p ${TEMP}/initramfs-temp/.initrd
18         mkdir -p ${TEMP}/initramfs-base-temp/var/lock/dmraid
19         mkdir -p ${TEMP}/initramfs-base-temp/sbin
20         mkdir -p ${TEMP}/initramfs-base-temp/usr/bin
21         mkdir -p ${TEMP}/initramfs-base-temp/usr/sbin
22         ln -s  lib  ${TEMP}/initramfs-base-temp/lib64
23
24         echo "/dev/ram0     /           ext2    defaults        0 0" > ${TEMP}/initramfs-base-temp/etc/fstab
25         echo "proc          /proc       proc    defaults    0 0" >> ${TEMP}/initramfs-base-temp/etc/fstab
26         
27         cd ${TEMP}/initramfs-base-temp/dev
28         mknod -m 660 console c 5 1
29         mknod -m 660 null c 1 3
30         mknod -m 600 tty1 c 4 1
31         cd "${TEMP}/initramfs-base-temp/"
32         find . -print | cpio ${CPIO_ARGS} --append -F "${CPIO}"
33         rm -rf "${TEMP}/initramfs-base-temp" > /dev/null
34 }
35
36 append_busybox() {
37         if [ -d "${TEMP}/initramfs-busybox-temp" ]
38         then
39                 rm -rf "${TEMP}/initramfs-busybox-temp" > /dev/null
40         fi
41         mkdir -p "${TEMP}/initramfs-busybox-temp/bin/" 
42
43         cp "${GK_SHARE}/generic/udhcpc.scripts" ${TEMP}/initramfs-busybox-temp/bin/
44         chmod +x "${TEMP}/initramfs-busybox-temp/bin/udhcpc.scripts"
45         tar -xjf "${BUSYBOX_BINCACHE}" -C "${TEMP}/initramfs-busybox-temp/bin" busybox ||
46                 gen_die 'Could not extract busybox bincache!'
47         chmod +x "${TEMP}/initramfs-busybox-temp/bin/busybox"
48
49         for i in '[' ash sh mount uname echo cut; do
50                 rm -f ${TEMP}/initramfs-busybox-temp/bin/$i > /dev/null
51                 ln ${TEMP}/initramfs-busybox-temp/bin/busybox ${TEMP}/initramfs-busybox-temp/bin/$i ||
52                         gen_die "Busybox error: could not link ${i}!"
53         done
54         
55         cd "${TEMP}/initramfs-busybox-temp/"
56         find . -print | cpio ${CPIO_ARGS} --append -F "${CPIO}"
57         rm -rf "${TEMP}/initramfs-busybox-temp" > /dev/null
58 }
59
60 append_blkid(){
61         if [ -d "${TEMP}/initramfs-blkid-temp" ]
62         then
63                 rm -r "${TEMP}/initramfs-blkid-temp/"
64         fi
65         cd ${TEMP}
66         mkdir -p "${TEMP}/initramfs-blkid-temp/bin/"
67         [ "${DISKLABEL}" -eq '1' ] && { /bin/bzip2 -dc "${BLKID_BINCACHE}" > "${TEMP}/initramfs-blkid-temp/bin/blkid" ||
68                 gen_die "Could not extract blkid binary cache!"; }
69         chmod a+x "${TEMP}/initramfs-blkid-temp/bin/blkid"
70         cd "${TEMP}/initramfs-blkid-temp/"
71         find . -print | cpio ${CPIO_ARGS} --append -F "${CPIO}"
72         rm -rf "${TEMP}/initramfs-blkid-temp" > /dev/null
73 }
74
75 #append_suspend(){
76 #       if [ -d "${TEMP}/initramfs-suspend-temp" ];
77 #       then
78 #               rm -r "${TEMP}/initramfs-suspend-temp/"
79 #       fi
80 #       print_info 1 'SUSPEND: Adding support (compiling binaries)...'
81 #       compile_suspend
82 #       mkdir -p "${TEMP}/initramfs-suspend-temp/"
83 #       /bin/tar -jxpf "${SUSPEND_BINCACHE}" -C "${TEMP}/initramfs-suspend-temp" ||
84 #               gen_die "Could not extract suspend binary cache!"
85 #       mkdir -p "${TEMP}/initramfs-suspend-temp/etc"
86 #       cp -f /etc/suspend.conf "${TEMP}/initramfs-suspend-temp/etc" ||
87 #               gen_die 'Could not copy /etc/suspend.conf'
88 #       cd "${TEMP}/initramfs-suspend-temp/"
89 #       find . -print | cpio ${CPIO_ARGS} --append -F "${CPIO}"
90 #       rm -r "${TEMP}/initramfs-suspend-temp/"
91 #}
92
93 append_dmraid(){
94         if [ -d "${TEMP}/initramfs-dmraid-temp" ]
95         then
96                 rm -r "${TEMP}/initramfs-dmraid-temp/"
97         fi
98         print_info 1 'DMRAID: Adding support (compiling binaries)...'
99         compile_dmraid
100         mkdir -p "${TEMP}/initramfs-dmraid-temp/"
101         /bin/tar -jxpf "${DMRAID_BINCACHE}" -C "${TEMP}/initramfs-dmraid-temp" ||
102                 gen_die "Could not extract dmraid binary cache!";
103         cd "${TEMP}/initramfs-dmraid-temp/"
104         find . -print | cpio ${CPIO_ARGS} --append -F "${CPIO}"
105         rm -r "${TEMP}/initramfs-dmraid-temp/"
106 }
107
108 append_lvm(){
109         if [ -d "${TEMP}/initramfs-lvm-temp" ]
110         then
111                 rm -r "${TEMP}/initramfs-lvm-temp/"
112         fi
113         cd ${TEMP}
114         mkdir -p "${TEMP}/initramfs-lvm-temp/bin/"
115         mkdir -p "${TEMP}/initramfs-lvm-temp/etc/lvm/"
116         if [ -e '/sbin/lvm' ] && LC_ALL="C" ldd /sbin/lvm|grep -q 'not a dynamic executable'
117         then
118                 print_info 1 '          LVM: Adding support (using local static binaries)...'
119                 cp /sbin/lvm "${TEMP}/initramfs-lvm-temp/bin/lvm" ||
120                         gen_die 'Could not copy over lvm!'
121         else
122                 print_info 1 '          LVM: Adding support (compiling binaries)...'
123                 compile_lvm
124                 /bin/tar -jxpf "${LVM_BINCACHE}" -C "${TEMP}/initramfs-lvm-temp" ||
125                         gen_die "Could not extract lvm binary cache!";
126                 mv ${TEMP}/initramfs-lvm-temp/sbin/lvm.static ${TEMP}/initramfs-lvm-temp/bin/lvm ||
127                         gen_die 'LVM error: Could not move lvm.static to lvm!'
128         fi
129         if [ -x /sbin/lvm ]
130         then
131 #               lvm dumpconfig 2>&1 > /dev/null || gen_die 'Could not copy over lvm.conf!'
132 #               ret=$?
133 #               if [ ${ret} != 0 ]
134 #               then
135                         cp /etc/lvm/lvm.conf "${TEMP}/initramfs-lvm-temp/etc/lvm/" ||
136                                 gen_die 'Could not copy over lvm.conf!'
137 #               else
138 #                       gen_die 'Could not copy over lvm.conf!'
139 #               fi
140         fi
141         cd "${TEMP}/initramfs-lvm-temp/"
142         find . -print | cpio ${CPIO_ARGS} --append -F "${CPIO}"
143         rm -r "${TEMP}/initramfs-lvm-temp/"
144 }
145
146 append_evms(){
147         if [ -d "${TEMP}/initramfs-evms-temp" ]
148         then
149                 rm -r "${TEMP}/initramfs-evms-temp/"
150         fi
151         mkdir -p "${TEMP}/initramfs-evms-temp/lib/evms"
152         mkdir -p "${TEMP}/initramfs-evms-temp/etc/"
153         mkdir -p "${TEMP}/initramfs-evms-temp/bin/"
154         mkdir -p "${TEMP}/initramfs-evms-temp/sbin/"
155         if [ "${EVMS}" -eq '1' ]
156         then
157                 print_info 1 '          EVMS: Adding support...'
158                 mkdir -p ${TEMP}/initramfs-evms-temp/lib
159                 cp -a /lib/ld-* "${TEMP}/initramfs-evms-temp/lib" \
160                         || gen_die 'Could not copy files for EVMS!'
161                 if [ -n "`ls /lib/libgcc_s*`" ]
162                 then
163                         cp -a /lib/libgcc_s* "${TEMP}/initramfs-evms-temp/lib" \
164                                 || gen_die 'Could not copy files for EVMS!'
165                 fi
166                 cp -a /lib/libc-* /lib/libc.* "${TEMP}/initramfs-evms-temp/lib" \
167                         || gen_die 'Could not copy files for EVMS!'
168                 cp -a /lib/libdl-* /lib/libdl.* "${TEMP}/initramfs-evms-temp/lib" \
169                         || gen_die 'Could not copy files for EVMS!'
170                 cp -a /lib/libpthread* "${TEMP}/initramfs-evms-temp/lib" \
171                         || gen_die 'Could not copy files for EVMS!'
172                 cp -a /lib/libuuid*so* "${TEMP}/initramfs-evms-temp/lib" \
173                         || gen_die 'Could not copy files for EVMS!'
174                 cp -a /lib/libevms*so* "${TEMP}/initramfs-evms-temp/lib" \
175                         || gen_die 'Could not copy files for EVMS!'
176                 cp -a /lib/evms "${TEMP}/initramfs-evms-temp/lib" \
177                         || gen_die 'Could not copy files for EVMS!'
178                 cp -a /lib/evms/* "${TEMP}/initramfs-evms-temp/lib/evms" \
179                         || gen_die 'Could not copy files for EVMS!'
180                 cp -a /etc/evms.conf "${TEMP}/initramfs-evms-temp/etc" \
181                         || gen_die 'Could not copy files for EVMS!'
182                 cp /sbin/evms_activate "${TEMP}/initramfs-evms-temp/sbin" \
183                         || gen_die 'Could not copy over evms_activate!'
184
185                 # Fix EVMS complaining that it can't find the swap utilities.
186                 # These are not required in the initramfs
187                 for swap_libs in "${TEMP}/initramfs-evms-temp/lib/evms/*/swap*.so"
188                 do
189                         rm ${swap_libs}
190                 done
191         fi
192         cd "${TEMP}/initramfs-evms-temp/"
193         find . -print | cpio ${CPIO_ARGS} --append -F "${CPIO}"
194         rm -r "${TEMP}/initramfs-evms-temp/"
195 }
196
197 append_mdadm(){
198         if [ -d "${TEMP}/initramfs-mdadm-temp" ]
199         then
200                 rm -r "${TEMP}/initramfs-mdadm-temp/"
201         fi
202         cd ${TEMP}
203         mkdir -p "${TEMP}/initramfs-mdadm-temp/etc/"
204         if [ "${MDADM}" -eq '1' ]
205         then
206                 cp -a /etc/mdadm.conf "${TEMP}/initramfs-mdadm-temp/etc" \
207                         || gen_die "Could not copy mdadm.conf!"
208         fi
209         cd "${TEMP}/initramfs-mdadm-temp/"
210         find . -print | cpio ${CPIO_ARGS} --append -F "${CPIO}"
211         rm -rf "${TEMP}/initramfs-mdadm-temp" > /dev/null
212 }
213
214 append_splash(){
215         if [ -x /usr/bin/splash_geninitramfs ] || [ -x /sbin/splash_geninitramfs ]
216         then
217                 [ -z "${SPLASH_THEME}" ] && [ -e /etc/conf.d/splash ] && source /etc/conf.d/splash
218                 [ -z "${SPLASH_THEME}" ] && SPLASH_THEME=default
219                 print_info 1 "  >> Installing splash [ using the ${SPLASH_THEME} theme ]..."
220                 if [ -d "${TEMP}/initramfs-splash-temp" ]
221                 then
222                         rm -r "${TEMP}/initramfs-splash-temp/"
223                 fi
224                 mkdir -p "${TEMP}/initramfs-splash-temp"
225                 cd /
226                 local tmp=""
227                 [ -n "${SPLASH_RES}" ] && tmp="-r ${SPLASH_RES}"
228                 splash_geninitramfs -c "${TEMP}/initramfs-splash-temp" ${tmp} ${SPLASH_THEME} || gen_die "Could not build splash cpio archive"
229                 if [ -e "/usr/share/splashutils/initrd.splash" ]; then
230                         mkdir -p "${TEMP}/initramfs-splash-temp/etc"
231                         cp -f "/usr/share/splashutils/initrd.splash" "${TEMP}/initramfs-splash-temp/etc"
232                 fi
233                 cd "${TEMP}/initramfs-splash-temp/"
234                 find . -print | cpio ${CPIO_ARGS} --append -F "${CPIO}" \
235                         || gen_die "compressing splash cpio"
236                 rm -r "${TEMP}/initramfs-splash-temp/"
237         else
238                 print_warning 1 '               >> No splash detected; skipping!'
239         fi
240 }
241
242 append_overlay(){
243         cd ${INITRAMFS_OVERLAY}
244         find . -print | cpio ${CPIO_ARGS} --append -F "${CPIO}"
245 }
246 print_list()
247 {
248         local x
249         for x in ${*}
250         do
251                 echo ${x}
252         done
253 }
254
255 append_modules() {
256         local group
257         local group_modules
258         local MOD_EXT=".ko"
259
260         print_info 2 "initramfs: >> Searching for modules..."
261         if [ "${INSTALL_MOD_PATH}" != '' ]
262         then
263           cd ${INSTALL_MOD_PATH}
264         else
265           cd /
266         fi
267         
268         if [ -d "${TEMP}/initramfs-modules-${KV}-temp" ]
269         then
270                 rm -r "${TEMP}/initramfs-modules-${KV}-temp/"
271         fi
272         mkdir -p "${TEMP}/initramfs-modules-${KV}-temp/lib/modules/${KV}"       
273         for i in `gen_dep_list`
274         do
275                 mymod=`find ./lib/modules/${KV} -name "${i}${MOD_EXT}" 2>/dev/null| head -n 1 `
276                 if [ -z "${mymod}" ]
277                 then
278                         print_warning 2 "Warning :: ${i}${MOD_EXT} not found; skipping..."
279                         continue;
280                 fi
281                 
282                 print_info 2 "initramfs: >> Copying ${i}${MOD_EXT}..."
283                 cp -ax --parents "${mymod}" "${TEMP}/initramfs-modules-${KV}-temp"
284         done
285         
286         cp -ax --parents ./lib/modules/${KV}/modules* ${TEMP}/initramfs-modules-${KV}-temp 2>/dev/null
287
288         mkdir -p "${TEMP}/initramfs-modules-${KV}-temp/etc/modules"
289         for group_modules in ${!MODULES_*}; do
290                 group="$(echo $group_modules | cut -d_ -f2 | tr "[:upper:]" "[:lower:]")"
291                 print_list ${!group_modules} > "${TEMP}/initramfs-modules-${KV}-temp/etc/modules/${group}"
292         done
293         cd "${TEMP}/initramfs-modules-${KV}-temp/"
294         find . | cpio ${CPIO_ARGS} --append -F "${CPIO}"
295         cd "${TEMP}"
296         rm -r "${TEMP}/initramfs-modules-${KV}-temp/"   
297 }
298
299 # check for static linked file with objdump
300 is_static() {
301         LANG="C" LC_ALL="C" objdump -T $1 2>&1 | grep "not a dynamic object" > /dev/null
302         return $?
303 }
304
305 append_auxilary() {
306         if [ -d "${TEMP}/initramfs-aux-temp" ]
307         then
308                 rm -r "${TEMP}/initramfs-aux-temp/"
309         fi
310         mkdir -p "${TEMP}/initramfs-aux-temp/etc"       
311         mkdir -p "${TEMP}/initramfs-aux-temp/sbin"      
312         if [ -f "${CMD_LINUXRC}" ]
313         then
314                 cp "${CMD_LINUXRC}" "${TEMP}/initramfs-aux-temp/init"
315                 print_info 2 "        >> Copying user specified linuxrc: ${CMD_LINUXRC} to init"
316         else    
317                 if [ -f "${GK_SHARE}/${ARCH}/linuxrc" ]
318                 then
319                         cp "${GK_SHARE}/${ARCH}/linuxrc" "${TEMP}/initramfs-aux-temp/init"
320                 else
321                         cp "${GK_SHARE}/generic/linuxrc" "${TEMP}/initramfs-aux-temp/init"
322                 fi
323         fi
324
325         # Make sure it's executable
326         chmod 0755 "${TEMP}/initramfs-aux-temp/init"
327
328         # Make a symlink to init .. incase we are bundled inside the kernel as one
329         # big cpio.
330         cd ${TEMP}/initramfs-aux-temp
331         ln -s init linuxrc
332 #       ln ${TEMP}/initramfs-aux-temp/init ${TEMP}/initramfs-aux-temp/linuxrc 
333
334         if [ -f "${GK_SHARE}/${ARCH}/initrd.scripts" ]
335         then
336                 cp "${GK_SHARE}/${ARCH}/initrd.scripts" "${TEMP}/initramfs-aux-temp/etc/initrd.scripts"
337         else    
338                 cp "${GK_SHARE}/generic/initrd.scripts" "${TEMP}/initramfs-aux-temp/etc/initrd.scripts"
339         fi
340
341         if [ -f "${GK_SHARE}/${ARCH}/initrd.defaults" ]
342         then
343                 cp "${GK_SHARE}/${ARCH}/initrd.defaults" "${TEMP}/initramfs-aux-temp/etc/initrd.defaults"
344         else
345                 cp "${GK_SHARE}/generic/initrd.defaults" "${TEMP}/initramfs-aux-temp/etc/initrd.defaults"
346         fi
347         
348         echo -n 'HWOPTS="$HWOPTS ' >> "${TEMP}/initramfs-aux-temp/etc/initrd.defaults"  
349         for group_modules in ${!MODULES_*}; do
350                 group="$(echo $group_modules | cut -d_ -f2 | tr "[:upper:]" "[:lower:]")"
351                 echo -n "${group} " >> "${TEMP}/initramfs-aux-temp/etc/initrd.defaults"
352         done
353         echo '"' >> "${TEMP}/initramfs-aux-temp/etc/initrd.defaults"    
354
355         if [ -f "${GK_SHARE}/${ARCH}/modprobe" ]
356         then
357                 cp "${GK_SHARE}/${ARCH}/modprobe" "${TEMP}/initramfs-aux-temp/sbin/modprobe"
358         else
359                 cp "${GK_SHARE}/generic/modprobe" "${TEMP}/initramfs-aux-temp/sbin/modprobe"
360         fi
361         if isTrue $CMD_DOKEYMAPAUTO
362         then
363                 echo 'MY_HWOPTS="${MY_HWOPTS} keymap"' >> ${TEMP}/initramfs-aux-temp/etc/initrd.defaults
364         fi
365         mkdir -p "${TEMP}/initramfs-aux-temp/lib/keymaps"
366         /bin/tar -C "${TEMP}/initramfs-aux-temp/lib/keymaps" -zxf "${GK_SHARE}/generic/keymaps.tar.gz"
367         if isTrue $CMD_SLOWUSB
368         then
369                 echo 'MY_HWOPTS="${MY_HWOPTS} slowusb"' >> ${TEMP}/initramfs-aux-temp/etc/initrd.defaults
370         fi
371         if isTrue ${LUKS}
372         then
373                 if is_static /bin/cryptsetup
374                 then
375                         print_info 1 "Including LUKS support"
376                         rm -f ${TEMP}/initramfs-aux-temp/sbin/cryptsetup
377                         cp /bin/cryptsetup ${TEMP}/initramfs-aux-temp/sbin/cryptsetup
378                         chmod +x "${TEMP}/initramfs-aux-temp/sbin/cryptsetup"
379                 elif is_static /sbin/cryptsetup
380                 then
381                         print_info 1 "Including LUKS support"
382                         rm -f ${TEMP}/initramfs-aux-temp/sbin/cryptsetup
383                         cp /sbin/cryptsetup ${TEMP}/initramfs-aux-temp/sbin/cryptsetup
384                 chmod +x "${TEMP}/initramfs-aux-temp/sbin/cryptsetup"
385
386                 else
387                         print_info 1 "LUKS support requires static cryptsetup at /bin/cryptsetup or /sbin/cryptsetup"
388                         print_info 1 "Not including LUKS support"
389                 fi
390         fi
391
392         cd ${TEMP}/initramfs-aux-temp/sbin && ln -s ../init init
393         cd ${TEMP}
394         chmod +x "${TEMP}/initramfs-aux-temp/init"
395         chmod +x "${TEMP}/initramfs-aux-temp/etc/initrd.scripts"
396         chmod +x "${TEMP}/initramfs-aux-temp/etc/initrd.defaults"
397         chmod +x "${TEMP}/initramfs-aux-temp/sbin/modprobe"
398         cd "${TEMP}/initramfs-aux-temp/"
399         find . -print | cpio ${CPIO_ARGS} --append -F "${CPIO}"
400         cd "${TEMP}"
401         rm -r "${TEMP}/initramfs-aux-temp/"     
402 }
403
404 append_data() {
405         local name=$1 var=$2
406         local func="append_${name}"
407
408         if [ $# -eq 1 ] || [ ${var} -eq 1 ]
409         then
410             print_info 1 "        >> Appending ${name} cpio data..."
411             ${func}
412         fi
413 }
414
415 create_initramfs() {
416         print_info 1 "initramfs: >> Initializing..."
417
418         # Create empty cpio
419         CPIO="${TMPDIR}/initramfs-${KV}"
420         echo | cpio ${CPIO_ARGS} -F "${CPIO}" 2>/dev/null \
421                 || gen_die "Could not create empty cpio at ${CPIO}"
422
423         append_data 'base_layout'
424         append_data 'auxilary'
425         append_data 'busybox' "${BUSYBOX}"
426         append_data 'lvm' "${LVM}"
427         append_data 'dmraid' "${DMRAID}"
428         append_data 'evms' "${EVMS}"
429         append_data 'mdadm' "${MDADM}"
430         
431         if [ "${NOINITRDMODULES}" -eq '0' ]
432         then
433                 append_data 'modules'
434         else
435                 print_info 1 "initramfs: Not copying modules..."
436         fi
437
438         append_data 'blkid' "${DISKLABEL}"
439         append_data 'splash' "${SPLASH}"
440
441         # This should always be appended last
442         if [ "${INITRAMFS_OVERLAY}" != '' ]
443         then
444                 append_data 'overlay'
445         fi
446
447         gzip -9 "${CPIO}"
448         mv -f "${CPIO}.gz" "${CPIO}"
449
450         if [ "${ENABLE_PEGASOS_HACKS}" = 'yes' ]
451         then
452                         # Pegasos hack for merging the initramfs into the kernel at compile time
453                         cp ${TMPDIR}/initramfs-${KV} ${KERNEL_DIR}/arch/powerpc/boot/ramdisk.image.gz &&
454                         rm ${TMPDIR}/initramfs-${KV}
455         elif [ ${BUILD_INITRAMFS} -eq '1' ]
456         then
457                 # Mips also mimics Pegasos to merge the initramfs into the kernel
458                 cp ${TMPDIR}/initramfs-${KV} ${KERNEL_DIR}/initramfs.cpio.gz
459                 gunzip -f ${KERNEL_DIR}/initramfs.cpio.gz
460         fi
461
462         if ! isTrue "${CMD_NOINSTALL}"
463         then
464                 if [ "${ENABLE_PEGASOS_HACKS}" != 'yes' ]
465                 then
466                         copy_image_with_preserve "initramfs" \
467                                 "${TMPDIR}/initramfs-${KV}" \
468                                 "initramfs-${KNAME}-${ARCH}-${KV}"
469                 fi
470         fi
471 }