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