ENHANCEMENT bug 83771. Add support for LABEL= and UUID=
[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                         cp -a /lib/ld-* "${TEMP}/initrd-temp/lib" || gen_die 'Could not copy files for EVMS2!'
169                         cp -a /lib/libc-* /lib/libc.* "${TEMP}/initrd-temp/lib" || gen_die 'Could not copy files for EVMS2!'
170                         cp -a /lib/libdl-* /lib/libdl.* "${TEMP}/initrd-temp/lib" || gen_die 'Could not copy files for EVMS2!'
171                         cp -a /lib/libpthread* "${TEMP}/initrd-temp/lib" || gen_die 'Could not copy files for EVMS2!'
172                         cp -a /lib/libuuid*so* "${TEMP}/initrd-temp/lib" || gen_die 'Could not copy files for EVMS2!'
173                         cp -a /lib/libevms*so* "${TEMP}/initrd-temp/lib" || gen_die 'Could not copy files for EVMS2!'
174                         cp -a /lib/evms "${TEMP}/initrd-temp/lib" || gen_die 'Could not copy files for EVMS2!'
175                         cp -a /lib/evms/* "${TEMP}/initrd-temp/lib/evms" || gen_die 'Could not copy files for EVMS2!'
176                         cp -a /etc/evms.conf "${TEMP}/initrd-temp/etc" || gen_die 'Could not copy files for EVMS2!'
177                         cp /sbin/evms_activate "${TEMP}/initrd-temp/bin/evms_activate" || gen_die 'Could not copy over vgscan!'
178                         # Fix EVMS2 complaining that it cant find the swap utilities.
179                         # These are not required in the initrd
180                         for swap_libs in "${TEMP}/initrd-temp/lib/evms/*/swap*.so"
181                         do
182                                 rm $swap_libs
183                         done
184                 fi
185         fi      
186
187         for i in '[' ash basename cat chroot clear cp dirname echo env false find \
188         grep gunzip gzip ln ls loadkmap losetup lsmod mkdir mknod more mount mv \
189         pivot_root ps awk pwd rm rmdir rmmod sed sh sleep tar test touch true umount uname \
190         xargs yes zcat chmod chown cut kill killall; do
191                 rm -f ${TEMP}/initrd-temp/bin/$i > /dev/null
192                 ln  ${TEMP}/initrd-temp/bin/busybox ${TEMP}/initrd-temp/bin/$i ||
193                         gen_die "Busybox error: could not link ${i}!"
194         done
195 }
196
197 print_list()
198 {
199         local x
200         for x in ${*}
201         do
202                 echo ${x}
203         done
204 }
205
206 create_initrd_modules() {
207         local group
208         local group_modules
209         
210         if [ "${PAT}" -gt "4" ]
211         then
212                 MOD_EXT=".ko"
213         else
214                 MOD_EXT=".o"
215         fi
216
217         print_info 2 "initrd: >> Searching for modules..."
218
219         if [ "${INSTALL_MOD_PATH}" != '' ]
220         then
221                 cd ${INSTALL_MOD_PATH}
222         else
223                 cd /
224         fi
225                                                                                                         
226         for i in `gen_dep_list`
227         do
228                 mymod=`find ./lib/modules/${KV} -name "${i}${MOD_EXT}" 2>/dev/null| head -n 1`
229                 if [ -z "${mymod}" ]
230                 then
231                         print_warning 2 "Warning :: ${i}${MOD_EXT} not found; skipping..."
232                         continue;
233                 fi
234                 print_info 2 "initrd: >> Copying ${i}${MOD_EXT}..."
235                 cp -ax --parents "${mymod}" "${TEMP}/initrd-temp"
236         done
237
238         cp -ax --parents ./lib/modules/${KV}/modules* ${TEMP}/initrd-temp 2>/dev/null
239
240         mkdir -p "${TEMP}/initrd-temp/etc/modules"
241         for group_modules in ${!MODULES_*}; do
242                 group="$(echo $group_modules | cut -d_ -f2 | tr "[:upper:]" "[:lower:]")"
243                 print_list ${!group_modules} > "${TEMP}/initrd-temp/etc/modules/${group}"
244         done
245 }
246
247 create_initrd_aux() {
248         if [ -f "${CMD_LINUXRC}" ]
249         then
250                 cp "${CMD_LINUXRC}" "${TEMP}/initrd-temp/linuxrc"
251                 print_info 2 "        >> Copying user specified linuxrc: ${CMD_LINUXRC}"
252         else    
253                 if [ -f "${GK_SHARE}/${ARCH}/linuxrc" ]
254                 then
255                         cp "${GK_SHARE}/${ARCH}/linuxrc" "${TEMP}/initrd-temp/linuxrc"
256                 else
257                         cp "${GK_SHARE}/generic/linuxrc" "${TEMP}/initrd-temp/linuxrc"
258                 fi
259         fi
260
261         if [ -f "${GK_SHARE}/${ARCH}/initrd.scripts" ]
262         then
263                 cp "${GK_SHARE}/${ARCH}/initrd.scripts" "${TEMP}/initrd-temp/etc/initrd.scripts"
264         else    
265                 cp "${GK_SHARE}/generic/initrd.scripts" "${TEMP}/initrd-temp/etc/initrd.scripts"
266         fi
267
268         if [ -f "${GK_SHARE}/${ARCH}/initrd.defaults" ]
269         then
270                 cp "${GK_SHARE}/${ARCH}/initrd.defaults" "${TEMP}/initrd-temp/etc/initrd.defaults"
271         else
272                 cp "${GK_SHARE}/generic/initrd.defaults" "${TEMP}/initrd-temp/etc/initrd.defaults"
273         fi
274         
275         echo -n 'HWOPTS="$HWOPTS ' >> "${TEMP}/initrd-temp/etc/initrd.defaults" 
276         for group_modules in ${!MODULES_*}; do
277                 group="$(echo $group_modules | cut -d_ -f2 | tr "[:upper:]" "[:lower:]")"
278                 echo -n "${group} " >> "${TEMP}/initrd-temp/etc/initrd.defaults"
279         done
280         echo '"' >> "${TEMP}/initrd-temp/etc/initrd.defaults"   
281
282         if [ -f "${GK_SHARE}/${ARCH}/modprobe" ]
283         then
284                 cp "${GK_SHARE}/${ARCH}/modprobe" "${TEMP}/initrd-temp/sbin/modprobe"
285         else
286                 cp "${GK_SHARE}/generic/modprobe" "${TEMP}/initrd-temp/sbin/modprobe"
287         fi
288         if isTrue $CMD_DOKEYMAPAUTO
289         then
290                 echo 'MY_HWOPTS="${MY_HWOPTS} keymap"' >> ${TEMP}/initrd-temp/etc/initrd.defaults
291         fi
292         mkdir -p "${TEMP}/initrd-temp/lib/keymaps"
293         /bin/tar -C "${TEMP}/initrd-temp/lib/keymaps" -zxf "${GK_SHARE}/generic/keymaps.tar.gz"
294
295         cd ${TEMP}/initrd-temp/sbin && ln -s ../linuxrc init
296         cd ${OLDPWD}
297         chmod +x "${TEMP}/initrd-temp/linuxrc"
298         chmod +x "${TEMP}/initrd-temp/etc/initrd.scripts"
299         chmod +x "${TEMP}/initrd-temp/etc/initrd.defaults"
300         chmod +x "${TEMP}/initrd-temp/sbin/modprobe"
301 }
302
303 calc_initrd_size() {
304         local TEST
305         cd ${TEMP}/initrd-temp/
306         TEST=`du -sk 2> /dev/null` 
307         echo $TEST | cut "-d " -f1
308 }
309
310 create_initrd() {
311         local MOD_EXT
312
313         print_info 1 "initrd: >> Initializing..."
314         create_base_initrd_sys
315
316         if [ "${NOINITRDMODULES}" = '' ]
317         then
318                 print_info 1 "        >> Copying modules..."
319                 create_initrd_modules
320         else
321                 print_info 1 "initrd: Not copying modules..."
322         fi
323
324         print_info 1 "        >> Copying auxilary files..."
325         create_initrd_aux
326
327         INITRD_CALC_SIZE=`calc_initrd_size`
328         INITRD_SIZE=`expr ${INITRD_CALC_SIZE} + 100`
329         print_info 1 "        :: Size is at ${INITRD_SIZE}K"
330         print_info 1 "        >> Creating loopback filesystem..."
331         create_initrd_loop ${INITRD_SIZE}
332
333         print_info 1 "        >> Moving initrd files to the loopback..."
334         move_initrd_to_loop
335
336         print_info 1 "        >> Cleaning up and compressing the initrd..."
337         create_initrd_unmount_loop
338
339         if [ "${COMPRESS_INITRD}" ]
340         then
341                 gzip -f -9 ${TEMP}/initrd-${KV}
342                 mv ${TEMP}/initrd-${KV}.gz ${TEMP}/initrd-${KV}
343         fi
344
345         if [ "${BOOTSPLASH}" -eq "1" ]
346         then
347                 if [ -x /sbin/splash ]
348                 then
349                         [ -z "${BOOTSPLASH_THEME}" ] && [ -e /etc/conf.d/bootsplash.conf ] && source /etc/conf.d/bootsplash.conf
350                         [ -z "${BOOTSPLASH_THEME}" ] && [ -e /etc/conf.d/bootsplash ] && source /etc/conf.d/bootsplash
351                         [ -z "${BOOTSPLASH_THEME}" ] && BOOTSPLASH_THEME=default
352                         print_info 1 "        >> Installing bootsplash [ using the ${BOOTSPLASH_THEME} theme ]..."
353                         for bootRes in '800x600' '1024x768' '1280x1024' '1600x1200'
354                         do
355                                 if [ -f "/etc/bootsplash/${BOOTSPLASH_THEME}/config/bootsplash-${bootRes}.cfg" ]
356                                 then
357                                         /sbin/splash -s -f /etc/bootsplash/${BOOTSPLASH_THEME}/config/bootsplash-${bootRes}.cfg >> ${TEMP}/initrd-${KV} ||
358                                                 gen_die "Error: could not copy ${bootRes} bootsplash!"
359                                 else
360                                         print_warning 1 "splash: Did not find a bootsplash for the ${bootRes} resolution..."
361                                 fi
362                         done
363                 else
364                         print_warning 1 '        >> No bootsplash detected; skipping!'
365                 fi
366         fi
367         if ! isTrue "${CMD_NOINSTALL}"
368         then
369                 cp ${TEMP}/initrd-${KV} /boot/initrd-${KNAME}-${ARCH}-${KV} ||
370                         gen_die 'Could not copy the initrd to /boot!'
371         fi
372         [ "${KERNEL_MAKE_DIRECTIVE}" == 'zImage.initrd' ] ||
373                 [ "${KERNEL_MAKE_DIRECTIVE_2}" == 'zImage.initrd' ] &&
374                 cp ${TEMP}/initrd-${KV} ${KERNEL_DIR}/arch/${ARCH}/boot/images/ramdisk.image.gz
375 }