Hack to make older bootcds create initrds. Uses mkinitrd shell script.
[bootmanager.git] / source / mkinitrd.sh
1 #!/bin/sh
2 # --- T2-COPYRIGHT-NOTE-BEGIN ---
3 # This copyright note is auto-generated by ./scripts/Create-CopyPatch.
4
5 # T2 SDE: package/.../mkinitrd/mkinitrd.sh
6 # Copyright (C) 2005 - 2006 The T2 SDE Project
7
8 # More information can be found in the files COPYING and README.
9
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; version 2 of the License. A copy of the
13 # GNU General Public License can be found in the file COPYING.
14 # --- T2-COPYRIGHT-NOTE-END ---
15
16 set -e
17
18 if [ $UID != 0 ]; then
19         echo "Non root - exiting ..."
20         exit 1
21 fi
22
23 while [ "$1" ]; do
24   case $1 in
25         [0-9]*) kernelver="$1" ;;
26         -R) root="$2" ; shift ;;
27         *) echo "Usage: mkinitrd [ -R root ] [ kernelver ]"
28            exit 1 ;;
29   esac
30   shift
31 done
32
33 [ "$root" ] || root=""
34 [ "$kernelver" ] || kernelver=`uname -r`
35 [ "$moddir" ] || moddir="${root}/lib/modules/$kernelver"
36
37 echo "Kernel: $kernelver, module dir: $moddir"
38
39 if [ ! -d $moddir ]; then
40         echo "Module dir $moddir does not exist!"
41         exit 2
42 fi
43
44 sysmap=""
45 [ -f "${root}/boot/System.map-$kernelver" ] && sysmap="${root}/boot/System.map-$kernelver"
46
47 if [ -z "$sysmap" ]; then
48         echo "System.map_$kernelver not found!"
49         exit 2
50 fi
51
52 echo "System.map: $sysmap"
53
54 # check needed tools
55 #for x in cpio gzip ; do
56 #       if ! which $x >/dev/null ; then
57 #               echo "$x not found!"
58 #               exit 2
59 #       fi
60 #done
61
62 tmpdir=`mktemp`
63
64 # create basic structure
65 #
66 rm -rf $tmpdir >/dev/null
67
68 echo "Create dirtree ..."
69
70 mkdir -p $tmpdir/{dev,bin,sbin,proc,sys,lib/modules,lib/udev,etc/hotplug.d/default}
71 mknod $tmpdir/dev/console c 5 1
72
73 # copy the basic / rootfs kernel modules
74 #
75 echo "Copying kernel modules ..."
76
77 (
78   find $moddir/kernel -type f | grep \
79         -e reiserfs -e reiser4 -e ext2 -e ext3 -e /jfs -e /xfs \
80         -e isofs -e udf -e /unionfs -e ntfs -e fat -e dm-mod \
81         -e /ide/ -e /ata/ -e /scsi/ -e /message/ \
82         -e hci -e usb-storage -e sbp2 \
83         -e drivers/net/ -e '/ipv6\.' -e usbhid |
84   while read fn ; do
85
86         for x in $fn `modinfo $fn | grep depends |
87                  cut -d : -f 2- | sed -e 's/ //g' -e 's/,/ /g' `
88         do
89                 # expand to full name if it was a depend
90                 [ $x = ${x##*/} ] &&
91                 x=`find $moddir/kernel -name "$x.*o"`
92
93                 echo -n "${x##*/} "
94
95                 # strip $root prefix
96                 xt=${x##$root}
97
98                 mkdir -p `dirname $tmpdir/$xt`
99                 cp $x $tmpdir/$xt 2>/dev/null
100         done
101   done
102 ) | fold -s ; echo
103
104 # generate map files
105 #
106 /sbin/depmod -ae -b $tmpdir -F $sysmap $kernelver
107
108 echo "Injecting programs and configuration ..."
109
110 # copying config
111 #
112 cp -ar ${root}/etc/udev $tmpdir/etc/
113 # in theory all, but fat and currently only cdrom_id is needed ...
114 #cp -ar ${root}/lib/udev/cdrom_id $tmpdir/lib/udev/
115
116 # setup programs
117 #
118 for x in ${root}/sbin/{hotplug,udevd,modprobe,insmod} 
119 do
120         # sanity check
121         file $x | grep -q "dynamically linked" &&
122                 echo "Warning: $x is dynamically linked!"
123         cp $x $tmpdir/sbin/
124 done
125
126 x=${root}/sbin/insmod.old
127 if [ ! -e $x ]; then
128         echo "Warning: Skipped optional file $x!"
129 else
130         file $x | grep -q "dynamically linked" &&
131                 echo "Warning: $x is dynamically linked!"
132         cp $x $tmpdir/sbin/
133         ln -s insmod.old $tmpdir/sbin/modprobe.old
134 fi
135
136 ln -s /sbin/udev $tmpdir/etc/hotplug.d/default/10-udev.hotplug
137 cp ${root}/bin/bash $tmpdir/bin/sh
138
139 # static, tiny embutils and friends
140 #
141 #cp ${root}/usr/embutils/{mount,umount,rm,mv,mkdir,ln,ls,switch_root,sleep,losetup,chmod,cat,sed,mknod} \
142 #   $tmpdir/bin/
143 ln -s mv $tmpdir/bin/cp
144
145 cp ${root}/sbin/init $tmpdir/init
146
147 # Custom ACPI DSDT table
148 if test -f "${root}/boot/DSDT.aml"; then
149         echo "Adding local DSDT file: $dsdt"
150         cp ${root}/boot/DSDT.aml $tmpdir/DSDT.aml
151 fi
152
153 # create the cpio image
154 #
155 echo "Archiving ..."
156 ( cd $tmpdir
157   find . | cpio -o -H newc | gzip -c9 > ${root}/boot/initrd-$kernelver.img
158 )
159
160 # display the resulting image
161 #
162 du -sh ${root}/boot/initrd-$kernelver.img
163 rm -rf $tmpdir