make more generic
[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 "invalid use of create_initrd_loop"
7         mkdir -p ${TEMP}/initrd-mount || gen_die "could not create loopback mount dir"
8         dd if=/dev/zero of=${TEMP}/initrd-loop bs=1k count=${1} >> "${DEBUGFILE}" 2>&1 || gen_die "could not zero initrd-loop"
9         mke2fs -F -N500 -q "${TEMP}/initrd-loop" >> "${DEBUGFILE}" 2>&1 || gen_die "could not format initrd-loop"
10         mount -t ext2 -o loop "${TEMP}/initrd-loop" "${TEMP}/initrd-mount" >> "${DEBUGFILE}" 2>&1 || gen_die "could not mount 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}
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         mkdir -p ${TEMP}/initrd-temp/new_root
35         mkdir -p ${TEMP}/initrd-temp/keymaps
36         ln -s bin ${TEMP}/initrd-temp/sbin
37         ln -s ../bin ${TEMP}/initrd-temp/usr/bin
38         ln -s ../bin ${TEMP}/initrd-temp/usr/sbin
39         echo "/dev/ram0     /           ext2    defaults" > ${TEMP}/initrd-temp/etc/fstab
40         echo "proc          /proc       proc    defaults    0 0" >> ${TEMP}/initrd-temp/etc/fstab
41
42         echo "REGISTER        .*           MKOLDCOMPAT" > ${TEMP}/initrd-temp/etc/devfsd.conf
43         echo "UNREGISTER      .*           RMOLDCOMPAT" >> ${TEMP}/initrd-temp/etc/devfsd.conf
44         echo "REGISTER        .*           MKNEWCOMPAT" >> ${TEMP}/initrd-temp/etc/devfsd.conf
45         echo "UNREGISTER      .*           RMNEWCOMPAT" >> ${TEMP}/initrd-temp/etc/devfsd.conf
46
47
48         cd ${TEMP}/initrd-temp/dev
49         MAKEDEV std
50         MAKEDEV console
51
52         cp "${BUSYBOX_BINCACHE}" "${TEMP}/initrd-temp/bin/busybox.bz2" || gen_die "could not copy busybox from bincache"
53         bunzip2 "${TEMP}/initrd-temp/bin/busybox.bz2" || gen_die "could not uncompress busybox"
54         chmod +x "${TEMP}/initrd-temp/bin/busybox"
55
56         if [ "${PAT}" -gt "4" ]
57         then
58                 cp "${MODULE_INIT_TOOLS_BINCACHE}" "${TEMP}/initrd-temp/bin/insmod.static.bz2" || gen_die "could not copy insmod.static from bincache"
59         else
60                 cp "${MODUTILS_BINCACHE}" "${TEMP}/initrd-temp/bin/insmod.static.bz2" || gen_die "could not copy insmod.static from bincache"
61         fi
62
63         bunzip2 "${TEMP}/initrd-temp/bin/insmod.static.bz2" || gen_die "could not uncompress insmod.static"
64         mv "${TEMP}/initrd-temp/bin/insmod.static" "${TEMP}/initrd-temp/bin/insmod"
65         chmod +x "${TEMP}/initrd-temp/bin/insmod"
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         for i in '[' ash basename cat chroot clear cp dirname echo env false find \
77         grep gunzip gzip insmod ln ls loadkmap losetup lsmod mkdir mknod more mount mv \
78         pivot_root ps awk pwd rm rmdir rmmod sh sleep tar test touch true umount uname \
79         xargs yes zcat chmod chown cut kill killall; do
80                 rm -f ${TEMP}/initrd-temp/bin/$i > /dev/null
81                 ln  ${TEMP}/initrd-temp/bin/busybox ${TEMP}/initrd-temp/bin/$i || gen_die "could not link ${i}"
82         done
83 }
84
85 create_initrd_modules() {
86         if [ "${PAT}" -gt "4" ]
87         then
88                 MOD_EXT=".ko"
89         else
90                 MOD_EXT=".o"
91         fi
92 #       local modc i mods mymod
93 #       for modc in storage firewire ataraid pcmcia usb
94 #       do
95 #       for 
96 #               mkdir -p ${TEMP}/initrd-temp/lib/modules/${modc}
97 #               mods=`echo $modc | tr [:lower:] [:upper:]`_MODULES
98 #               eval mymods=\$$mods
99 #               for i in ${mymods}
100                 for i in `gen_dep_list`
101                 do
102                         print_info 2 "$i : module searching" 1 0
103                         mymod=`find /lib/modules/${KV} -name "${i}${MOD_EXT}"`
104                         if [ -z "${mymod}" ]
105                         then
106                                 print_info 2 "Warning : ${i}${MOD_EXT} not found; skipping..."
107                                 continue;
108                         fi
109                         print_info 2 "copying ${mymod} to initrd"
110                         cp -ax --parents "${mymod}" "${TEMP}/initrd-temp"
111                 done
112 #       done
113
114         cp -ax --parents /lib/modules/${KV}/modules* ${TEMP}/initrd-temp
115
116         print_info 1 "WARNING WARNING: Brad, Dont forget to output modules files to autoload"
117 #       cat ${GK_SHARE}/${ARCH}/linuxrc | sed   -e "s/%%STORAGE_MODULES%%/${STORAGE_MODULES}/" \
118 #                                               -e "s/%%FIREWIRE_MODULES%%/${FIREWIRE_MODULES}/" \
119 #                                               -e "s/%%ATARAID_MODULES%%/${ATARAID_MODULES}/" \
120 #                                               -e "s/%%PCMCIA_MODULES%%/${PCMCIA_MODULES}/" \
121 #                                               -e "s/%%USB_MODULES%%/${USB_MODULES}/" \
122 #                                               > ${TEMP}/initrd-temp/linuxrc
123
124         cp "${GK_SHARE}/generic/linuxrc" "${TEMP}/initrd-temp/linuxrc"
125         cp "${GK_SHARE}/generic/initrd.scripts" "${TEMP}/initrd-temp/etc/initrd.scripts"
126         cp "${GK_SHARE}/generic/initrd.defaults" "${TEMP}/initrd-temp/etc/initrd.defaults"
127         cp "${GK_SHARE}/generic/modprobe" "${TEMP}/initrd-temp/sbin/modprobe"
128         chmod +x "${TEMP}/initrd-temp/linuxrc"
129         chmod +x "${TEMP}/initrd-temp/etc/initrd.scripts"
130         chmod +x "${TEMP}/initrd-temp/etc/initrd.defaults"
131         chmod +x "${TEMP}/initrd-temp/sbin/modprobe"
132 }
133
134 calc_initrd_size() {
135         local TEST
136         cd ${TEMP}/initrd-temp/
137         TEST=`du -sk 2> /dev/null` 
138         echo $TEST | cut "-d " -f1
139 }
140
141 create_initrd() {
142         local MOD_EXT
143
144         print_info 1 "initrd: creating base system"
145         create_base_initrd_sys
146
147         print_info 1 "initrd: copying modules"
148         create_initrd_modules
149
150         print_info 1 "initrd: calculating initrd size"
151         INITRD_CALC_SIZE=`calc_initrd_size`
152
153         print_info 1 "initrd: calculated size ${INITRD_CALC_SIZE} + 100k slop for fs overhead"
154         INITRD_SIZE=`expr ${INITRD_CALC_SIZE} + 100`
155
156         print_info 1 "initrd: real size ${INITRD_SIZE}"
157
158         print_info 1 "initrd: creating loopback filesystem"
159         create_initrd_loop ${INITRD_SIZE}
160
161         print_info 1 "initrd: moving initrd fs to loopback"
162         move_initrd_to_loop
163
164         print_info 1 "initrd: cleaning up and compressing initrd"
165         create_initrd_unmount_loop
166
167         if [ "${COMPRESS_INITRD}" ]
168         then
169                 gzip -f -9 ${TEMP}/initrd-loop
170                 mv ${TEMP}/initrd-loop.gz ${TEMP}/initrd-loop
171         fi
172
173         if [ "${BOOTSPLASH}" -eq "1" ]
174         then
175                 print_info 1 "initrd: copying bootsplash"
176                 /sbin/splash -s -f /etc/bootsplash/gentoo/config/bootsplash-800x600.cfg >> ${TEMP}/initrd-loop || gen_die "could not copy 800x600 bootsplash"
177                 /sbin/splash -s -f /etc/bootsplash/gentoo/config/bootsplash-1024x768.cfg >> ${TEMP}/initrd-loop || gen_die "could not copy 1024x768 bootsplash"
178                 /sbin/splash -s -f /etc/bootsplash/gentoo/config/bootsplash-1280x1024.cfg >> ${TEMP}/initrd-loop || gen_die "could not copy 1280x1024 bootsplash"
179                 /sbin/splash -s -f /etc/bootsplash/gentoo/config/bootsplash-1600x1200.cfg >> ${TEMP}/initrd-loop || gen_die "could not copy 1600x1200 bootsplash"
180         fi
181         cp ${TEMP}/initrd-loop /boot/initrd-${KV} || gen_die "could not copy initrd to boot"
182 }
183