Fix #79502; thanks to rocket for the patch.
[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         ln -s bin ${TEMP}/initrd-temp/sbin
41         ln -s ../bin ${TEMP}/initrd-temp/usr/bin
42         ln -s ../bin ${TEMP}/initrd-temp/usr/sbin
43
44         echo "/dev/ram0     /           ext2    defaults" > ${TEMP}/initrd-temp/etc/fstab
45         echo "proc          /proc       proc    defaults    0 0" >> ${TEMP}/initrd-temp/etc/fstab
46
47         echo "REGISTER        .*           MKOLDCOMPAT" > ${TEMP}/initrd-temp/etc/devfsd.conf
48         echo "UNREGISTER      .*           RMOLDCOMPAT" >> ${TEMP}/initrd-temp/etc/devfsd.conf
49         echo "REGISTER        .*           MKNEWCOMPAT" >> ${TEMP}/initrd-temp/etc/devfsd.conf
50         echo "UNREGISTER      .*           RMNEWCOMPAT" >> ${TEMP}/initrd-temp/etc/devfsd.conf
51
52         cd ${TEMP}/initrd-temp/dev
53         MAKEDEV std
54         MAKEDEV console
55
56         cp "${BUSYBOX_BINCACHE}" "${TEMP}/initrd-temp/bin/busybox.bz2" ||
57                 gen_die 'Could not copy busybox from bincache!'
58         bunzip2 "${TEMP}/initrd-temp/bin/busybox.bz2" ||
59                 gen_die 'Could not uncompress busybox!'
60         chmod +x "${TEMP}/initrd-temp/bin/busybox"
61
62         if [ "${NOINITRDMODULES}" = '' ]
63         then
64                 if [ "${PAT}" -gt "4" ]
65                 then
66                         cp "${MODULE_INIT_TOOLS_BINCACHE}" "${TEMP}/initrd-temp/bin/insmod.static.bz2" ||
67                                 gen_die 'Could not copy insmod.static from bincache!'
68                 else
69                         cp "${MODUTILS_BINCACHE}" "${TEMP}/initrd-temp/bin/insmod.static.bz2" ||
70                                 gen_die 'Could not copy insmod.static from bincache'
71                 fi
72
73                 bunzip2 "${TEMP}/initrd-temp/bin/insmod.static.bz2" ||
74                         gen_die 'Could not uncompress insmod.static!'
75                 mv "${TEMP}/initrd-temp/bin/insmod.static" "${TEMP}/initrd-temp/bin/insmod"
76                 chmod +x "${TEMP}/initrd-temp/bin/insmod"
77         fi
78
79         cp "${DEVFSD_BINCACHE}" "${TEMP}/initrd-temp/bin/devfsd.bz2" || gen_die "could not copy devfsd executable from bincache"
80         bunzip2 "${TEMP}/initrd-temp/bin/devfsd.bz2" || gen_die "could not uncompress devfsd"
81         chmod +x "${TEMP}/initrd-temp/bin/devfsd"
82
83         [ "${UDEV}" -eq '1' ] && { tar -jxpf "${UDEV_BINCACHE}" -C "${TEMP}/initrd-temp" ||
84                 gen_die "Could not extract udev binary cache!"; }
85                 ln -sf "./udev" "${TEMP}/initrd-temp/bin/udevstart" ||
86                         gen_die 'Could not symlink udev -> udevstart!'
87
88 # We make our own devfsd.conf these days, the default one doesn't work with the stripped
89 # down devfsd we use with dietlibc
90 #       cp "${DEVFSD_CONF_BINCACHE}" "${TEMP}/initrd-temp/etc/devfsd.conf.bz2" ||
91 #               gen_die "could not copy devfsd.conf from bincache"
92 #       bunzip2 "${TEMP}/initrd-temp/etc/devfsd.conf.bz2" ||
93 #               gen_die "could not uncompress devfsd.conf"
94
95         # LVM2
96         if [ "${LVM2}" -eq '1' ]
97         then
98                 if [ -e '/sbin/lvm' ] && ldd /sbin/lvm|grep -q 'not a dynamic executable';
99                 then
100                         print_info 1 'LVM2: Adding support (using local static binaries)...'
101                         cp /sbin/lvm "${TEMP}/initrd-temp/bin/lvm" ||
102                                 gen_die 'Could not copy over lvm!'
103                         ln "${TEMP}/initrd-temp/bin/lvm" "${TEMP}/initrd-temp/bin/vgscan" ||
104                                 gen_die 'Could not symlink lvm -> vgscan!'
105                         ln "${TEMP}/initrd-temp/bin/lvm" "${TEMP}/initrd-temp/bin/vgchange" ||
106                                 gen_die 'Could not symlink lvm -> vgchange!'
107                 else
108                         print_info 1 'LVM2: Adding support (compiling binaries)...'
109                         compile_lvm2
110
111                         tar -jxpf "${LVM2_BINCACHE}" -C "${TEMP}/initrd-temp" ||
112                                 gen_die "Could not extract lvm2 binary cache!";
113                         mv ${TEMP}/initrd-temp/bin/lvm.static ${TEMP}/initrd-temp/bin/lvm ||
114                                 gen_die 'LVM2 error: Could not move lvm.static to lvm!'
115                         for i in vgchange vgscan; do
116                                 ln  ${TEMP}/initrd-temp/bin/lvm ${TEMP}/initrd-temp/bin/$i ||
117                                         gen_die "LVM2 error: Could not link ${i}!"
118                         done
119                 fi      
120         else # Deprecation warning; remove in a few versions.
121                 if [ -e '/sbin/lvm' ]
122                 then
123                         if ldd /sbin/lvm|grep -q 'not a dynamic executable';
124                         then
125                                 print_warning 1 'LVM2: For support, use --lvm2!'
126                         fi
127                 fi
128         fi
129         
130         # EVMS2
131         if [ "${CMD_EVMS2}" -eq '1' ]
132         then
133                 if [ -e '/sbin/evms_activate' ]
134                 then
135                         print_info 1 'EVMS2: Adding support...' 
136                         mkdir -p ${TEMP}/initrd-temp/lib
137                         cp -a /lib/ld-* "${TEMP}/initrd-temp/lib" || gen_die 'Could not copy files for EVMS2!'
138                         cp -a /lib/libc-* /lib/libc.* "${TEMP}/initrd-temp/lib" || gen_die 'Could not copy files for EVMS2!'
139                         cp -a /lib/libdl-* /lib/libdl.* "${TEMP}/initrd-temp/lib" || gen_die 'Could not copy files for EVMS2!'
140                         cp -a /lib/libpthread* "${TEMP}/initrd-temp/lib" || gen_die 'Could not copy files for EVMS2!'
141                         cp -a /lib/libuuid*so* "${TEMP}/initrd-temp/lib" || gen_die 'Could not copy files for EVMS2!'
142                         cp -a /lib/libevms*so* "${TEMP}/initrd-temp/lib" || gen_die 'Could not copy files for EVMS2!'
143                         cp -a /lib/evms "${TEMP}/initrd-temp/lib" || gen_die 'Could not copy files for EVMS2!'
144                         cp -a /lib/evms/* "${TEMP}/initrd-temp/lib/evms" || gen_die 'Could not copy files for EVMS2!'
145                         cp -a /etc/evms.conf "${TEMP}/initrd-temp/etc" || gen_die 'Could not copy files for EVMS2!'
146                         cp /sbin/evms_activate "${TEMP}/initrd-temp/bin/evms_activate" || gen_die 'Could not copy over vgscan!'
147                         # Fix EVMS2 complaining that it cant find the swap utilities.
148                         # These are not required in the initrd
149                         for swap_libs in "${TEMP}/initrd-temp/lib/evms/*/swap*.so"
150                         do
151                                 rm $swap_libs
152                         done
153                 fi
154         fi      
155
156         for i in '[' ash basename cat chroot clear cp dirname echo env false find \
157         grep gunzip gzip ln ls loadkmap losetup lsmod mkdir mknod more mount mv \
158         pivot_root ps awk pwd rm rmdir rmmod sed sh sleep tar test touch true umount uname \
159         xargs yes zcat chmod chown cut kill killall; do
160                 rm -f ${TEMP}/initrd-temp/bin/$i > /dev/null
161                 ln  ${TEMP}/initrd-temp/bin/busybox ${TEMP}/initrd-temp/bin/$i ||
162                         gen_die "Busybox error: could not link ${i}!"
163         done
164 }
165
166 print_list()
167 {
168         local x
169         for x in ${*}
170         do
171                 echo ${x}
172         done
173 }
174
175 create_initrd_modules() {
176         local group
177         local group_modules
178         
179         if [ "${PAT}" -gt "4" ]
180         then
181                 MOD_EXT=".ko"
182         else
183                 MOD_EXT=".o"
184         fi
185
186         print_info 2 "initrd: >> Searching for modules..."
187         if [ "${INSTALL_MOD_PATH}" != '' ]
188         then
189           cd ${INSTALL_MOD_PATH}
190         else
191           cd /
192         fi
193         for i in `gen_dep_list`
194         do
195                 mymod=`find ./lib/modules/${KV} -name "${i}${MOD_EXT}" | head -n 1`
196                 if [ -z "${mymod}" ]
197                 then
198                         print_warning 2 "Warning :: ${i}${MOD_EXT} not found; skipping..."
199                         continue;
200                 fi
201                 print_info 2 "initrd: >> Copying ${i}${MOD_EXT}..."
202                 cp -ax --parents "${mymod}" "${TEMP}/initrd-temp"
203         done
204
205         cp -ax --parents ./lib/modules/${KV}/modules* ${TEMP}/initrd-temp
206
207         mkdir -p "${TEMP}/initrd-temp/etc/modules"
208         for group_modules in ${!MODULES_*}; do
209                 group="$(echo $group_modules | cut -d_ -f2 | tr "[:upper:]" "[:lower:]")"
210                 print_list ${!group_modules} > "${TEMP}/initrd-temp/etc/modules/${group}"
211         done
212 }
213
214 create_initrd_aux() {
215         if [ -f "${CMD_LINUXRC}" ]
216         then
217                 cp "${CMD_LINUXRC}" "${TEMP}/initrd-temp/linuxrc"
218                 print_info 2 "        >> Copying user specified linuxrc: ${CMD_LINUXRC}"
219         else    
220                 if [ -f "${GK_SHARE}/${ARCH}/linuxrc" ]
221                 then
222                         cp "${GK_SHARE}/${ARCH}/linuxrc" "${TEMP}/initrd-temp/linuxrc"
223                 else
224                         cp "${GK_SHARE}/generic/linuxrc" "${TEMP}/initrd-temp/linuxrc"
225                 fi
226         fi
227
228         if [ -f "${GK_SHARE}/${ARCH}/initrd.scripts" ]
229         then
230                 cp "${GK_SHARE}/${ARCH}/initrd.scripts" "${TEMP}/initrd-temp/etc/initrd.scripts"
231         else    
232                 cp "${GK_SHARE}/generic/initrd.scripts" "${TEMP}/initrd-temp/etc/initrd.scripts"
233         fi
234
235         if [ -f "${GK_SHARE}/${ARCH}/initrd.defaults" ]
236         then
237                 cp "${GK_SHARE}/${ARCH}/initrd.defaults" "${TEMP}/initrd-temp/etc/initrd.defaults"
238         else
239                 cp "${GK_SHARE}/generic/initrd.defaults" "${TEMP}/initrd-temp/etc/initrd.defaults"
240         fi
241         
242         echo -n 'HWOPTS="$HWOPTS ' >> "${TEMP}/initrd-temp/etc/initrd.defaults" 
243         for group_modules in ${!MODULES_*}; do
244                 group="$(echo $group_modules | cut -d_ -f2 | tr "[:upper:]" "[:lower:]")"
245                 echo -n "${group} " >> "${TEMP}/initrd-temp/etc/initrd.defaults"
246         done
247         echo '"' >> "${TEMP}/initrd-temp/etc/initrd.defaults"   
248
249         if [ -f "${GK_SHARE}/${ARCH}/modprobe" ]
250         then
251                 cp "${GK_SHARE}/${ARCH}/modprobe" "${TEMP}/initrd-temp/sbin/modprobe"
252         else
253                 cp "${GK_SHARE}/generic/modprobe" "${TEMP}/initrd-temp/sbin/modprobe"
254         fi
255         if isTrue $CMD_DOKEYMAPAUTO
256         then
257                 echo 'MY_HWOPTS="${MY_HWOPTS} keymap"' >> ${TEMP}/initrd-temp/etc/initrd.defaults
258         fi
259         mkdir -p "${TEMP}/initrd-temp/lib/keymaps"
260         tar -C "${TEMP}/initrd-temp/lib/keymaps" -zxf "${GK_SHARE}/generic/keymaps.tar.gz"
261
262         cd ${TEMP}/initrd-temp/sbin && ln -s ../linuxrc init
263         cd ${OLDPWD}
264         chmod +x "${TEMP}/initrd-temp/linuxrc"
265         chmod +x "${TEMP}/initrd-temp/etc/initrd.scripts"
266         chmod +x "${TEMP}/initrd-temp/etc/initrd.defaults"
267         chmod +x "${TEMP}/initrd-temp/sbin/modprobe"
268 }
269
270 calc_initrd_size() {
271         local TEST
272         cd ${TEMP}/initrd-temp/
273         TEST=`du -sk 2> /dev/null` 
274         echo $TEST | cut "-d " -f1
275 }
276
277 create_initrd() {
278         local MOD_EXT
279
280         print_info 1 "initrd: >> Initializing..."
281         create_base_initrd_sys
282
283         if [ "${NOINITRDMODULES}" = '' ]
284         then
285                 print_info 1 "        >> Copying modules..."
286                 create_initrd_modules
287         else
288                 print_info 1 "initrd: Not copying modules..."
289         fi
290
291         print_info 1 "        >> Copying auxilary files..."
292         create_initrd_aux
293
294         INITRD_CALC_SIZE=`calc_initrd_size`
295         INITRD_SIZE=`expr ${INITRD_CALC_SIZE} + 100`
296         print_info 1 "        :: Size is at ${INITRD_SIZE}K"
297
298         print_info 1 "        >> Creating loopback filesystem..."
299         create_initrd_loop ${INITRD_SIZE}
300
301         print_info 1 "        >> Moving initrd files to the loopback..."
302         move_initrd_to_loop
303
304         print_info 1 "        >> Cleaning up and compressing the initrd..."
305         create_initrd_unmount_loop
306
307         if [ "${COMPRESS_INITRD}" ]
308         then
309                 gzip -f -9 ${TEMP}/initrd-${KV}
310                 mv ${TEMP}/initrd-${KV}.gz ${TEMP}/initrd-${KV}
311         fi
312
313         if [ "${BOOTSPLASH}" -eq "1" ]
314         then
315                 if [ -x /sbin/splash ]
316                 then
317                         [ -z "${BOOTSPLASH_THEME}" ] && [ -e /etc/conf.d/bootsplash.conf ] && source /etc/conf.d/bootsplash.conf
318                         [ -z "${BOOTSPLASH_THEME}" ] && [ -e /etc/conf.d/bootsplash ] && source /etc/conf.d/bootsplash
319                         [ -z "${BOOTSPLASH_THEME}" ] && BOOTSPLASH_THEME=default
320                         print_info 1 "        >> Installing bootsplash [ using the ${BOOTSPLASH_THEME} theme ]..."
321                         for bootRes in '800x600' '1024x768' '1280x1024' '1600x1200'
322                         do
323                                 if [ -f "/etc/bootsplash/${BOOTSPLASH_THEME}/config/bootsplash-${bootRes}.cfg" ]
324                                 then
325                                         /sbin/splash -s -f /etc/bootsplash/${BOOTSPLASH_THEME}/config/bootsplash-${bootRes}.cfg >> ${TEMP}/initrd-${KV} ||
326                                                 gen_die "Error: could not copy ${bootRes} bootsplash!"
327                                 else
328                                         print_warning 1 "splash: Did not find a bootsplash for the ${bootRes} resolution..."
329                                 fi
330                         done
331                 else
332                         print_warning 1 '        >> No bootsplash detected; skipping!'
333                 fi
334         fi
335         if ! isTrue "${CMD_NOINSTALL}"
336         then
337                 cp ${TEMP}/initrd-${KV} /boot/initrd-${KV} ||
338                         gen_die 'Could not copy the initrd to /boot!'
339         fi
340 }