>> 3.0.2c. Added more PCMCIA modules to the x86 module list; and added the bug
[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 || gen_die 'Could not create loopback mount directory!'
8         dd if=/dev/zero of=${TEMP}/initrd-${KV} bs=1k count=${1} >> "${DEBUGFILE}" 2>&1 || gen_die "Could not zero initrd-${KV}"
9         mke2fs -F -N500 -q "${TEMP}/initrd-${KV}" >> "${DEBUGFILE}" 2>&1 || gen_die "Could not format initrd-${KV}!"
10         mount -t ext2 -o loop "${TEMP}/initrd-${KV}" "${TEMP}/initrd-mount" >> "${DEBUGFILE}" 2>&1 || gen_die 'Could not mount the initrd filesystem!'
11 }
12
13 create_initrd_unmount_loop()
14 {
15         cd ${TEMP}
16         umount "${TEMP}/initrd-mount" || gen_die 'Could not unmount initrd system!'
17 }
18
19 move_initrd_to_loop()
20 {
21         cd "${TEMP}/initrd-temp"
22         mv * "${TEMP}/initrd-mount" >> ${DEBUGFILE} 2>&1
23 }
24
25 create_base_initrd_sys() {
26         rm -rf "${TEMP}/initrd-temp" > /dev/null
27         mkdir -p ${TEMP}/initrd-temp/dev
28         mkdir -p ${TEMP}/initrd-temp/bin
29         mkdir -p ${TEMP}/initrd-temp/etc
30         mkdir -p ${TEMP}/initrd-temp/usr
31         mkdir -p ${TEMP}/initrd-temp/proc
32         mkdir -p ${TEMP}/initrd-temp/temp
33         mkdir -p ${TEMP}/initrd-temp/.initrd
34         ln -s bin ${TEMP}/initrd-temp/sbin
35         ln -s ../bin ${TEMP}/initrd-temp/usr/bin
36         ln -s ../bin ${TEMP}/initrd-temp/usr/sbin
37         echo "/dev/ram0     /           ext2    defaults" > ${TEMP}/initrd-temp/etc/fstab
38         echo "proc          /proc       proc    defaults    0 0" >> ${TEMP}/initrd-temp/etc/fstab
39
40         echo "REGISTER        .*           MKOLDCOMPAT" > ${TEMP}/initrd-temp/etc/devfsd.conf
41         echo "UNREGISTER      .*           RMOLDCOMPAT" >> ${TEMP}/initrd-temp/etc/devfsd.conf
42         echo "REGISTER        .*           MKNEWCOMPAT" >> ${TEMP}/initrd-temp/etc/devfsd.conf
43         echo "UNREGISTER      .*           RMNEWCOMPAT" >> ${TEMP}/initrd-temp/etc/devfsd.conf
44
45         cd ${TEMP}/initrd-temp/dev
46         MAKEDEV std
47         MAKEDEV console
48
49         cp "${BUSYBOX_BINCACHE}" "${TEMP}/initrd-temp/bin/busybox.bz2" || gen_die "could not copy busybox from bincache"
50         bunzip2 "${TEMP}/initrd-temp/bin/busybox.bz2" || gen_die "could not uncompress busybox"
51         chmod +x "${TEMP}/initrd-temp/bin/busybox"
52
53         if [ "${NOINITRDMODULES}" = '' ]
54         then
55                 if [ "${PAT}" -gt "4" ]
56                 then
57                         cp "${MODULE_INIT_TOOLS_BINCACHE}" "${TEMP}/initrd-temp/bin/insmod.static.bz2" || gen_die "could not copy insmod.static from bincache"
58                 else
59                         cp "${MODUTILS_BINCACHE}" "${TEMP}/initrd-temp/bin/insmod.static.bz2" || gen_die "could not copy insmod.static from bincache"
60                 fi
61
62                 bunzip2 "${TEMP}/initrd-temp/bin/insmod.static.bz2" || gen_die "could not uncompress insmod.static"
63                 mv "${TEMP}/initrd-temp/bin/insmod.static" "${TEMP}/initrd-temp/bin/insmod"
64                 chmod +x "${TEMP}/initrd-temp/bin/insmod"
65         fi
66
67         cp "${DEVFSD_BINCACHE}" "${TEMP}/initrd-temp/bin/devfsd.bz2" || gen_die "could not copy devfsd executable from bincache"
68         bunzip2 "${TEMP}/initrd-temp/bin/devfsd.bz2" || gen_die "could not uncompress devfsd"
69         chmod +x "${TEMP}/initrd-temp/bin/devfsd"
70
71 # We make our own devfsd.conf these days, the default one doesn't work with the stripped
72 # down devfsd we use with dietlibc
73 #       cp "${DEVFSD_CONF_BINCACHE}" "${TEMP}/initrd-temp/etc/devfsd.conf.bz2" || gen_die "could not copy devfsd.conf from bincache"
74 #       bunzip2 "${TEMP}/initrd-temp/etc/devfsd.conf.bz2" || gen_die "could not uncompress devfsd.conf"
75
76         # LVM2
77         if [ -e '/sbin/vgscan.static' -a -e '/sbin/vgchange.static' ]
78         then
79                 if [ "${CMD_NOLVM2}" -ne '1' ]
80                 then
81                         cp /sbin/vgscan.static "${TEMP}/initrd-temp/bin/vgscan" || gen_die 'Could not copy over vgscan!'
82                         cp /sbin/vgchange.static "${TEMP}/initrd-temp/bin/vgchange" || gen_die 'Could not copy over vgchange!'
83                 fi
84 #       else
85 #               print_warning 1 "initrd: No LVM2 static binaries found; skipping support..."
86         fi
87
88         for i in '[' ash basename cat chroot clear cp dirname echo env false find \
89         grep gunzip gzip ln ls loadkmap losetup lsmod mkdir mknod more mount mv \
90         pivot_root ps awk pwd rm rmdir rmmod sed sh sleep tar test touch true umount uname \
91         xargs yes zcat chmod chown cut kill killall; do
92                 rm -f ${TEMP}/initrd-temp/bin/$i > /dev/null
93                 ln  ${TEMP}/initrd-temp/bin/busybox ${TEMP}/initrd-temp/bin/$i || gen_die "Busybox error: could not link ${i}!"
94         done
95 }
96
97 print_list()
98 {
99         local x
100         for x in ${*}
101         do
102                 echo ${x}
103         done
104 }
105
106 create_initrd_modules() {
107         local group
108         local group_modules
109         
110         if [ "${PAT}" -gt "4" ]
111         then
112                 MOD_EXT=".ko"
113         else
114                 MOD_EXT=".o"
115         fi
116
117         print_info 2 "initrd: >> Searching for modules..."
118         for i in `gen_dep_list`
119         do
120                 mymod=`find /lib/modules/${KV} -name "${i}${MOD_EXT}" | head -n 1`
121                 if [ -z "${mymod}" ]
122                 then
123                         print_warning 2 "Warning :: ${i}${MOD_EXT} not found; skipping..."
124                         continue;
125                 fi
126                 print_info 2 "initrd: >> Copying ${i}${MOD_EXT}..."
127                 cp -ax --parents "${mymod}" "${TEMP}/initrd-temp"
128         done
129
130         cp -ax --parents /lib/modules/${KV}/modules* ${TEMP}/initrd-temp
131
132         mkdir -p "${TEMP}/initrd-temp/etc/modules"
133         for group_modules in ${!MODULES_*}; do
134                 group="$(echo $group_modules | cut -d_ -f2 | tr "[:upper:]" "[:lower:]")"
135                 print_list ${!group_modules} > "${TEMP}/initrd-temp/etc/modules/${group}"
136         done
137 }
138
139 create_initrd_aux() {
140         if [ -f "${GK_SHARE}/${ARCH}/linuxrc" ]
141         then
142                 cp "${GK_SHARE}/${ARCH}/linuxrc" "${TEMP}/initrd-temp/linuxrc"
143         else
144                 cp "${GK_SHARE}/generic/linuxrc" "${TEMP}/initrd-temp/linuxrc"
145         fi
146
147         if [ -f "${GK_SHARE}/${ARCH}/initrd.scripts" ]
148         then
149                 cp "${GK_SHARE}/${ARCH}/initrd.scripts" "${TEMP}/initrd-temp/etc/initrd.scripts"
150         else    
151                 cp "${GK_SHARE}/generic/initrd.scripts" "${TEMP}/initrd-temp/etc/initrd.scripts"
152         fi
153
154         if [ -f "${GK_SHARE}/${ARCH}/initrd.defaults" ]
155         then
156                 cp "${GK_SHARE}/${ARCH}/initrd.defaults" "${TEMP}/initrd-temp/etc/initrd.defaults"
157         else
158                 cp "${GK_SHARE}/generic/initrd.defaults" "${TEMP}/initrd-temp/etc/initrd.defaults"
159         fi
160         
161         echo -n 'HWOPTS="$HWOPTS ' >> "${TEMP}/initrd-temp/etc/initrd.defaults" 
162         for group_modules in ${!MODULES_*}; do
163                 group="$(echo $group_modules | cut -d_ -f2 | tr "[:upper:]" "[:lower:]")"
164                 echo -n "${group} " >> "${TEMP}/initrd-temp/etc/initrd.defaults"
165         done
166         echo "\"" >> "${TEMP}/initrd-temp/etc/initrd.defaults"  
167
168         if [ -f "${GK_SHARE}/${ARCH}/modprobe" ]
169         then
170                 cp "${GK_SHARE}/${ARCH}/modprobe" "${TEMP}/initrd-temp/sbin/modprobe"
171         else
172                 cp "${GK_SHARE}/generic/modprobe" "${TEMP}/initrd-temp/sbin/modprobe"
173         fi
174         if isTrue $CMD_DOKEYMAPAUTO
175         then
176                 echo 'MY_HWOPTS="${MY_HWOPTS} keymap"' >> ${TEMP}/initrd-temp/etc/initrd.defaults
177         fi
178         mkdir -p "${TEMP}/initrd-temp/lib/keymaps"
179         tar -C "${TEMP}/initrd-temp/lib/keymaps" -zxf "${GK_SHARE}/generic/keymaps.tar.gz"
180
181         cd ${TEMP}/initrd-temp/sbin && ln -s ../linuxrc init
182         cd ${OLDPWD}
183         chmod +x "${TEMP}/initrd-temp/linuxrc"
184         chmod +x "${TEMP}/initrd-temp/etc/initrd.scripts"
185         chmod +x "${TEMP}/initrd-temp/etc/initrd.defaults"
186         chmod +x "${TEMP}/initrd-temp/sbin/modprobe"
187 }
188
189 calc_initrd_size() {
190         local TEST
191         cd ${TEMP}/initrd-temp/
192         TEST=`du -sk 2> /dev/null` 
193         echo $TEST | cut "-d " -f1
194 }
195
196 create_initrd() {
197         local MOD_EXT
198
199         print_info 1 "initrd: >> Initializing..."
200         create_base_initrd_sys
201
202         if [ "${NOINITRDMODULES}" = '' ]
203         then
204                 print_info 1 "        >> Copying modules..."
205                 create_initrd_modules
206         else
207                 print_info 1 "initrd: Not copying modules..."
208         fi
209
210         print_info 1 "        >> Copying auxilary files..."
211         create_initrd_aux
212
213         INITRD_CALC_SIZE=`calc_initrd_size`
214         INITRD_SIZE=`expr ${INITRD_CALC_SIZE} + 100`
215         print_info 1 "        :: Size is at ${INITRD_SIZE}K"
216
217         print_info 1 "        >> Creating loopback filesystem..."
218         create_initrd_loop ${INITRD_SIZE}
219
220         print_info 1 "        >> Moving initrd files to the loopback..."
221         move_initrd_to_loop
222
223         print_info 1 "        >> Cleaning up and compressing the initrd..."
224         create_initrd_unmount_loop
225
226         if [ "${COMPRESS_INITRD}" ]
227         then
228                 gzip -f -9 ${TEMP}/initrd-${KV}
229                 mv ${TEMP}/initrd-${KV}.gz ${TEMP}/initrd-${KV}
230         fi
231
232         if [ "${BOOTSPLASH}" -eq "1" ]
233         then
234                 if [ -x /sbin/splash ]
235                 then
236                         [ -z "${BOOTSPLASH_THEME}" ] && [ -e /etc/conf.d/bootsplash.conf ] && source /etc/conf.d/bootsplash.conf
237                         [ -z "${BOOTSPLASH_THEME}" ] && [ -e /etc/conf.d/bootsplash ] && source /etc/conf.d/bootsplash
238                         [ -z "${BOOTSPLASH_THEME}" ] && BOOTSPLASH_THEME=default
239                         print_info 1 "        >> Installing bootsplash [ using the ${BOOTSPLASH_THEME} theme ]..."
240                         for bootRes in '800x600' '1024x768' '1280x1024' '1600x1200'
241                         do
242                                 if [ -f "/etc/bootsplash/${BOOTSPLASH_THEME}/config/bootsplash-${bootRes}.cfg" ]
243                                 then
244                                         /sbin/splash -s -f /etc/bootsplash/${BOOTSPLASH_THEME}/config/bootsplash-${bootRes}.cfg >> ${TEMP}/initrd-${KV} || gen_die "Error: could not copy ${bootRes} bootsplash!"
245                                 else
246                                         print_warning 1 "splash: Did not find a bootsplash for the ${bootRes} resolution..."
247                                 fi
248                         done
249                 else
250                         print_warning 1 "      >> No bootsplash detected; skipping!"
251                 fi
252         fi
253         if ! isTrue "${CMD_NOINSTALL}"
254         then
255                 cp ${TEMP}/initrd-${KV} /boot/initrd-${KV} || gen_die 'Could not copy the initrd to /boot!'
256         fi
257 }