Adding a set of patches submitted by Martin "Parmus" Parm; bugs #57748,
[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         if [ "${INSTALL_MOD_PATH}" != '' ]
119         then
120           cd ${INSTALL_MOD_PATH}
121         else
122           cd /
123         fi
124         for i in `gen_dep_list`
125         do
126                 mymod=`find ./lib/modules/${KV} -name "${i}${MOD_EXT}" | head -n 1`
127                 if [ -z "${mymod}" ]
128                 then
129                         print_warning 2 "Warning :: ${i}${MOD_EXT} not found; skipping..."
130                         continue;
131                 fi
132                 print_info 2 "initrd: >> Copying ${i}${MOD_EXT}..."
133                 cp -ax --parents "${mymod}" "${TEMP}/initrd-temp"
134         done
135
136         cp -ax --parents ./lib/modules/${KV}/modules* ${TEMP}/initrd-temp
137
138         mkdir -p "${TEMP}/initrd-temp/etc/modules"
139         for group_modules in ${!MODULES_*}; do
140                 group="$(echo $group_modules | cut -d_ -f2 | tr "[:upper:]" "[:lower:]")"
141                 print_list ${!group_modules} > "${TEMP}/initrd-temp/etc/modules/${group}"
142         done
143 }
144
145 create_initrd_aux() {
146         if [ -f "${GK_SHARE}/${ARCH}/linuxrc" ]
147         then
148                 cp "${GK_SHARE}/${ARCH}/linuxrc" "${TEMP}/initrd-temp/linuxrc"
149         else
150                 cp "${GK_SHARE}/generic/linuxrc" "${TEMP}/initrd-temp/linuxrc"
151         fi
152
153         if [ -f "${GK_SHARE}/${ARCH}/initrd.scripts" ]
154         then
155                 cp "${GK_SHARE}/${ARCH}/initrd.scripts" "${TEMP}/initrd-temp/etc/initrd.scripts"
156         else    
157                 cp "${GK_SHARE}/generic/initrd.scripts" "${TEMP}/initrd-temp/etc/initrd.scripts"
158         fi
159
160         if [ -f "${GK_SHARE}/${ARCH}/initrd.defaults" ]
161         then
162                 cp "${GK_SHARE}/${ARCH}/initrd.defaults" "${TEMP}/initrd-temp/etc/initrd.defaults"
163         else
164                 cp "${GK_SHARE}/generic/initrd.defaults" "${TEMP}/initrd-temp/etc/initrd.defaults"
165         fi
166         
167         echo -n 'HWOPTS="$HWOPTS ' >> "${TEMP}/initrd-temp/etc/initrd.defaults" 
168         for group_modules in ${!MODULES_*}; do
169                 group="$(echo $group_modules | cut -d_ -f2 | tr "[:upper:]" "[:lower:]")"
170                 echo -n "${group} " >> "${TEMP}/initrd-temp/etc/initrd.defaults"
171         done
172         echo "\"" >> "${TEMP}/initrd-temp/etc/initrd.defaults"  
173
174         if [ -f "${GK_SHARE}/${ARCH}/modprobe" ]
175         then
176                 cp "${GK_SHARE}/${ARCH}/modprobe" "${TEMP}/initrd-temp/sbin/modprobe"
177         else
178                 cp "${GK_SHARE}/generic/modprobe" "${TEMP}/initrd-temp/sbin/modprobe"
179         fi
180         if isTrue $CMD_DOKEYMAPAUTO
181         then
182                 echo 'MY_HWOPTS="${MY_HWOPTS} keymap"' >> ${TEMP}/initrd-temp/etc/initrd.defaults
183         fi
184         mkdir -p "${TEMP}/initrd-temp/lib/keymaps"
185         tar -C "${TEMP}/initrd-temp/lib/keymaps" -zxf "${GK_SHARE}/generic/keymaps.tar.gz"
186
187         cd ${TEMP}/initrd-temp/sbin && ln -s ../linuxrc init
188         cd ${OLDPWD}
189         chmod +x "${TEMP}/initrd-temp/linuxrc"
190         chmod +x "${TEMP}/initrd-temp/etc/initrd.scripts"
191         chmod +x "${TEMP}/initrd-temp/etc/initrd.defaults"
192         chmod +x "${TEMP}/initrd-temp/sbin/modprobe"
193 }
194
195 calc_initrd_size() {
196         local TEST
197         cd ${TEMP}/initrd-temp/
198         TEST=`du -sk 2> /dev/null` 
199         echo $TEST | cut "-d " -f1
200 }
201
202 create_initrd() {
203         local MOD_EXT
204
205         print_info 1 "initrd: >> Initializing..."
206         create_base_initrd_sys
207
208         if [ "${NOINITRDMODULES}" = '' ]
209         then
210                 print_info 1 "        >> Copying modules..."
211                 create_initrd_modules
212         else
213                 print_info 1 "initrd: Not copying modules..."
214         fi
215
216         print_info 1 "        >> Copying auxilary files..."
217         create_initrd_aux
218
219         INITRD_CALC_SIZE=`calc_initrd_size`
220         INITRD_SIZE=`expr ${INITRD_CALC_SIZE} + 100`
221         print_info 1 "        :: Size is at ${INITRD_SIZE}K"
222
223         print_info 1 "        >> Creating loopback filesystem..."
224         create_initrd_loop ${INITRD_SIZE}
225
226         print_info 1 "        >> Moving initrd files to the loopback..."
227         move_initrd_to_loop
228
229         print_info 1 "        >> Cleaning up and compressing the initrd..."
230         create_initrd_unmount_loop
231
232         if [ "${COMPRESS_INITRD}" ]
233         then
234                 gzip -f -9 ${TEMP}/initrd-${KV}
235                 mv ${TEMP}/initrd-${KV}.gz ${TEMP}/initrd-${KV}
236         fi
237
238         if [ "${BOOTSPLASH}" -eq "1" ]
239         then
240                 if [ -x /sbin/splash ]
241                 then
242                         [ -z "${BOOTSPLASH_THEME}" ] && [ -e /etc/conf.d/bootsplash.conf ] && source /etc/conf.d/bootsplash.conf
243                         [ -z "${BOOTSPLASH_THEME}" ] && [ -e /etc/conf.d/bootsplash ] && source /etc/conf.d/bootsplash
244                         [ -z "${BOOTSPLASH_THEME}" ] && BOOTSPLASH_THEME=default
245                         print_info 1 "        >> Installing bootsplash [ using the ${BOOTSPLASH_THEME} theme ]..."
246                         for bootRes in '800x600' '1024x768' '1280x1024' '1600x1200'
247                         do
248                                 if [ -f "/etc/bootsplash/${BOOTSPLASH_THEME}/config/bootsplash-${bootRes}.cfg" ]
249                                 then
250                                         /sbin/splash -s -f /etc/bootsplash/${BOOTSPLASH_THEME}/config/bootsplash-${bootRes}.cfg >> ${TEMP}/initrd-${KV} || gen_die "Error: could not copy ${bootRes} bootsplash!"
251                                 else
252                                         print_warning 1 "splash: Did not find a bootsplash for the ${bootRes} resolution..."
253                                 fi
254                         done
255                 else
256                         print_warning 1 "      >> No bootsplash detected; skipping!"
257                 fi
258         fi
259         if ! isTrue "${CMD_NOINSTALL}"
260         then
261                 cp ${TEMP}/initrd-${KV} /boot/initrd-${KV} || gen_die 'Could not copy the initrd to /boot!'
262         fi
263 }