Added an ia64 config.
[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
86 # We make our own devfsd.conf these days, the default one doesn't work with the stripped
87 # down devfsd we use with dietlibc
88 #       cp "${DEVFSD_CONF_BINCACHE}" "${TEMP}/initrd-temp/etc/devfsd.conf.bz2" ||
89 #               gen_die "could not copy devfsd.conf from bincache"
90 #       bunzip2 "${TEMP}/initrd-temp/etc/devfsd.conf.bz2" ||
91 #               gen_die "could not uncompress devfsd.conf"
92
93         # LVM2
94         if [ -e '/sbin/vgscan.static' -a -e '/sbin/vgchange.static' ]
95         then
96                 if [ "${CMD_NOLVM2}" != '1' ]
97                 then
98                         cp /sbin/vgscan.static "${TEMP}/initrd-temp/bin/vgscan" ||
99                                 gen_die 'Could not copy over vgscan!'
100                         cp /sbin/vgchange.static "${TEMP}/initrd-temp/bin/vgchange" ||
101                                 gen_die 'Could not copy over vgchange!'
102                 fi
103 #       else
104 #               print_warning 1 "initrd: No LVM2 static binaries found; skipping support..."
105         fi
106
107         for i in '[' ash basename cat chroot clear cp dirname echo env false find \
108         grep gunzip gzip ln ls loadkmap losetup lsmod mkdir mknod more mount mv \
109         pivot_root ps awk pwd rm rmdir rmmod sed sh sleep tar test touch true umount uname \
110         xargs yes zcat chmod chown cut kill killall; do
111                 rm -f ${TEMP}/initrd-temp/bin/$i > /dev/null
112                 ln  ${TEMP}/initrd-temp/bin/busybox ${TEMP}/initrd-temp/bin/$i ||
113                         gen_die "Busybox error: could not link ${i}!"
114         done
115 }
116
117 print_list()
118 {
119         local x
120         for x in ${*}
121         do
122                 echo ${x}
123         done
124 }
125
126 create_initrd_modules() {
127         local group
128         local group_modules
129         
130         if [ "${PAT}" -gt "4" ]
131         then
132                 MOD_EXT=".ko"
133         else
134                 MOD_EXT=".o"
135         fi
136
137         print_info 2 "initrd: >> Searching for modules..."
138         if [ "${INSTALL_MOD_PATH}" != '' ]
139         then
140           cd ${INSTALL_MOD_PATH}
141         else
142           cd /
143         fi
144         for i in `gen_dep_list`
145         do
146                 mymod=`find ./lib/modules/${KV} -name "${i}${MOD_EXT}" | head -n 1`
147                 if [ -z "${mymod}" ]
148                 then
149                         print_warning 2 "Warning :: ${i}${MOD_EXT} not found; skipping..."
150                         continue;
151                 fi
152                 print_info 2 "initrd: >> Copying ${i}${MOD_EXT}..."
153                 cp -ax --parents "${mymod}" "${TEMP}/initrd-temp"
154         done
155
156         cp -ax --parents ./lib/modules/${KV}/modules* ${TEMP}/initrd-temp
157
158         mkdir -p "${TEMP}/initrd-temp/etc/modules"
159         for group_modules in ${!MODULES_*}; do
160                 group="$(echo $group_modules | cut -d_ -f2 | tr "[:upper:]" "[:lower:]")"
161                 print_list ${!group_modules} > "${TEMP}/initrd-temp/etc/modules/${group}"
162         done
163 }
164
165 create_initrd_aux() {
166         if [ -f "${GK_SHARE}/${ARCH}/linuxrc" ]
167         then
168                 cp "${GK_SHARE}/${ARCH}/linuxrc" "${TEMP}/initrd-temp/linuxrc"
169         else
170                 cp "${GK_SHARE}/generic/linuxrc" "${TEMP}/initrd-temp/linuxrc"
171         fi
172
173         if [ -f "${GK_SHARE}/${ARCH}/initrd.scripts" ]
174         then
175                 cp "${GK_SHARE}/${ARCH}/initrd.scripts" "${TEMP}/initrd-temp/etc/initrd.scripts"
176         else    
177                 cp "${GK_SHARE}/generic/initrd.scripts" "${TEMP}/initrd-temp/etc/initrd.scripts"
178         fi
179
180         if [ -f "${GK_SHARE}/${ARCH}/initrd.defaults" ]
181         then
182                 cp "${GK_SHARE}/${ARCH}/initrd.defaults" "${TEMP}/initrd-temp/etc/initrd.defaults"
183         else
184                 cp "${GK_SHARE}/generic/initrd.defaults" "${TEMP}/initrd-temp/etc/initrd.defaults"
185         fi
186         
187         echo -n 'HWOPTS="$HWOPTS ' >> "${TEMP}/initrd-temp/etc/initrd.defaults" 
188         for group_modules in ${!MODULES_*}; do
189                 group="$(echo $group_modules | cut -d_ -f2 | tr "[:upper:]" "[:lower:]")"
190                 echo -n "${group} " >> "${TEMP}/initrd-temp/etc/initrd.defaults"
191         done
192         echo "\"" >> "${TEMP}/initrd-temp/etc/initrd.defaults"  
193
194         if [ -f "${GK_SHARE}/${ARCH}/modprobe" ]
195         then
196                 cp "${GK_SHARE}/${ARCH}/modprobe" "${TEMP}/initrd-temp/sbin/modprobe"
197         else
198                 cp "${GK_SHARE}/generic/modprobe" "${TEMP}/initrd-temp/sbin/modprobe"
199         fi
200         if isTrue $CMD_DOKEYMAPAUTO
201         then
202                 echo 'MY_HWOPTS="${MY_HWOPTS} keymap"' >> ${TEMP}/initrd-temp/etc/initrd.defaults
203         fi
204         mkdir -p "${TEMP}/initrd-temp/lib/keymaps"
205         tar -C "${TEMP}/initrd-temp/lib/keymaps" -zxf "${GK_SHARE}/generic/keymaps.tar.gz"
206
207         cd ${TEMP}/initrd-temp/sbin && ln -s ../linuxrc init
208         cd ${OLDPWD}
209         chmod +x "${TEMP}/initrd-temp/linuxrc"
210         chmod +x "${TEMP}/initrd-temp/etc/initrd.scripts"
211         chmod +x "${TEMP}/initrd-temp/etc/initrd.defaults"
212         chmod +x "${TEMP}/initrd-temp/sbin/modprobe"
213 }
214
215 calc_initrd_size() {
216         local TEST
217         cd ${TEMP}/initrd-temp/
218         TEST=`du -sk 2> /dev/null` 
219         echo $TEST | cut "-d " -f1
220 }
221
222 create_initrd() {
223         local MOD_EXT
224
225         print_info 1 "initrd: >> Initializing..."
226         create_base_initrd_sys
227
228         if [ "${NOINITRDMODULES}" = '' ]
229         then
230                 print_info 1 "        >> Copying modules..."
231                 create_initrd_modules
232         else
233                 print_info 1 "initrd: Not copying modules..."
234         fi
235
236         print_info 1 "        >> Copying auxilary files..."
237         create_initrd_aux
238
239         INITRD_CALC_SIZE=`calc_initrd_size`
240         INITRD_SIZE=`expr ${INITRD_CALC_SIZE} + 100`
241         print_info 1 "        :: Size is at ${INITRD_SIZE}K"
242
243         print_info 1 "        >> Creating loopback filesystem..."
244         create_initrd_loop ${INITRD_SIZE}
245
246         print_info 1 "        >> Moving initrd files to the loopback..."
247         move_initrd_to_loop
248
249         print_info 1 "        >> Cleaning up and compressing the initrd..."
250         create_initrd_unmount_loop
251
252         if [ "${COMPRESS_INITRD}" ]
253         then
254                 gzip -f -9 ${TEMP}/initrd-${KV}
255                 mv ${TEMP}/initrd-${KV}.gz ${TEMP}/initrd-${KV}
256         fi
257
258         if [ "${BOOTSPLASH}" -eq "1" ]
259         then
260                 if [ -x /sbin/splash ]
261                 then
262                         [ -z "${BOOTSPLASH_THEME}" ] && [ -e /etc/conf.d/bootsplash.conf ] && source /etc/conf.d/bootsplash.conf
263                         [ -z "${BOOTSPLASH_THEME}" ] && [ -e /etc/conf.d/bootsplash ] && source /etc/conf.d/bootsplash
264                         [ -z "${BOOTSPLASH_THEME}" ] && BOOTSPLASH_THEME=default
265                         print_info 1 "        >> Installing bootsplash [ using the ${BOOTSPLASH_THEME} theme ]..."
266                         for bootRes in '800x600' '1024x768' '1280x1024' '1600x1200'
267                         do
268                                 if [ -f "/etc/bootsplash/${BOOTSPLASH_THEME}/config/bootsplash-${bootRes}.cfg" ]
269                                 then
270                                         /sbin/splash -s -f /etc/bootsplash/${BOOTSPLASH_THEME}/config/bootsplash-${bootRes}.cfg >> ${TEMP}/initrd-${KV} ||
271                                                 gen_die "Error: could not copy ${bootRes} bootsplash!"
272                                 else
273                                         print_warning 1 "splash: Did not find a bootsplash for the ${bootRes} resolution..."
274                                 fi
275                         done
276                 else
277                         print_warning 1 '        >> No bootsplash detected; skipping!'
278                 fi
279         fi
280         if ! isTrue "${CMD_NOINSTALL}"
281         then
282                 cp ${TEMP}/initrd-${KV} /boot/initrd-${KV} ||
283                         gen_die 'Could not copy the initrd to /boot!'
284         fi
285 }