mailestone-2
[linux-based-stubdoms.git] / mk-ramdisk-common
blob193a9dbb824d92e1aeee42acc330564c2a1e7586
2 # This a simple implementaton of mkinitrd 
6 # Set the umask. For iscsi, the initrd can contain platintext
7 # password (chap secret), so only allow read by owner.
8 umask 077
10 target=$1/$2
12 TMPDIR="/tmp"
13 PROBE="yes"
14 MNTIMAGE=$1/initrd-$3
15 IMAGE=$1/initrd.img.$3
16 RCFILE=$MNTIMAGE/init
17 verbose=""
19 DSO_DEPS=""
20 LDSO=""
21 get_dso_deps() {
22     bin="$1" ; shift
23     DSO_DEPS=""
25     declare -a FILES
26     declare -a NAMES
28     # this is a hack, but the only better way requires binutils or elfutils
29     # be installed.  i.e., we need readelf to find the interpretter.
30     if [ -z "$LDSO" ]; then
31         for ldso in /lib*/ld*.so* ; do
32             [ -L $ldso ] && continue
33             [ -x $ldso ] || continue
34             $ldso --verify $bin >/dev/null 2>&1 || continue
35             LDSO="$ldso"
36         done
37     fi
39     # I still hate shell.
40     declare -i n=0
41     while read NAME I0 FILE ADDR I1 ; do
42         [ "$FILE" == "not" ] && FILE="$FILE $ADDR"
43         NAMES[$n]="$NAME"
44         FILES[$n]="$FILE"
45         let n++
46     done << EOF
47         $(LD_TRACE_PRELINKING=1 LD_WARN= LD_TRACE_LOADED_OBJECTS=1 \
48             $LDSO $bin 2>/dev/null)
49 EOF
51     [ ${#FILES[*]} -eq 0 ] && return 1
53     # we don't want the name of the binary in the list
54     if [ "${FILES[0]}" == "$bin" ]; then
55         FILES[0]=""
56         NAMES[0]=""
57         [ ${#FILES[*]} -eq 1 ] && return 1
58     fi
60     declare -i n=0
61     while [ $n -lt ${#FILES[*]} ]; do
62         FILE="${FILES[$n]}"
63         if [ "$FILE" == "not found" ]; then
64             cat 1>&2 <<EOF
65 There are missing files on your system.  The dynamic object $bin
66 requires ${NAMES[$n]} n order to properly function.  mkinitrd cannot continue.
67 EOF
68             exit 1
69         fi
70         case "$FILE" in
71             /lib*)
72                 TLIBDIR=`echo "$FILE" | sed 's,\(/lib[^/]*\)/.*$,\1,'`
73                 BASE=`basename "$FILE"`
74                 # Prefer nosegneg libs over direct segment accesses on i686.
75                 if [ -f "$TLIBDIR/i686/nosegneg/$BASE" ]; then
76                     FILE="$TLIBDIR/i686/nosegneg/$BASE"
77                 # Otherwise, prefer base libraries rather than their optimized
78                 # variants.
79                 elif [ -f "$TLIBDIR/$BASE" ]; then
80                     FILE="$TLIBDIR/$BASE"
81                 fi
82                 FILES[$n]="$FILE"
83                 ;;
84         esac
85         dynamic="yes"
86         let n++
87     done
89     DSO_DEPS="${FILES[@]}"
92 readlink() {
93     echo nash-readlink "$1" | /sbin/nash --force --quiet
96 indent_chars=""
97 inst() {
98     if [ "$#" != "2" -a "$#" != "3" ];then
99         echo "usage: inst <file> <root> [<destination file>]"
100         return 1
101     fi
102     local file="$1" ; shift
103     local root="${1%%/}/" ; shift
104     local dest="${1##/}" ; shift
105     [ -z "$dest" ] && dest="${file##/}"
107     local old_indent_chars=${indent_chars}
108     indent_chars="${indent_chars}  "
109     indent=${indent_chars:2}
111     mkdir -p "$root/$(dirname $dest)"
113     local RET=0
114     local target=""
115     [ -L "$file" ] && target=$(readlink "$file")
116     if [ -n "$target" -a "$dest" != "$target" ]; then
117         if [ -e "$root$dest" ]; then
118   #          echo "${indent}$root/$dest already exists"
119             RET=0
120         else
121             echo "${indent}$file -> $root$dest"
122             ln -sf "$target" "$root$dest"
124             inst "$target" "$root"
125             l=`echo "$x" | sed -n 's,\(/lib[^/]*\)/.*$,\1,p'`
126             if [ -n "$l" ]; then
127                 inst "$x" "$root" "$l"/`basename "$x"`
128             else
129                 inst "$x" "$root"
130             fi
131             RET=$?
132             indent_chars=${old_indent_chars}
133             return $RET
134         fi
135     fi
137     local SHEBANG=$(dd if="$file" bs=2 count=1 2>/dev/null)
138     if [ "$SHEBANG" == '#!' ]; then
139         # We're intentionally not playing the "what did this moron run
140         # in his shell script" game.  There's nothing but pain in that.
141         local interp=$(head -1 "$file" | sed 's/^#! *//')
142         inst "$interp" "$root"
143         RET=$?
144         indent_chars=${old_indent_chars}
145         return $RET
146     fi
148 #       echo "indent=${indent}, file=${file}"
150     if [ -e "$root$dest" ]; then
151    #     echo "${indent}$root$dest already exists"
152         RET=0
153     else
154         if [ -n "$target" -a -L "$target" ]; then
155             inst "$target" "$root"
156             RET=$?
157         else
158             echo "${indent}$file -> $root$dest"
159             cp -aL "$file" "$root$dest"
161             get_dso_deps "$file"
162             local DEPS="$DSO_DEPS"
163             for x in $DEPS ; do
164                 TLIBDIR=`echo "$x" | sed 's,\(/lib[^/]*\)/.*$,\1,'`
165                 BASE=`basename "$x"`
166                 inst "$x" "$root" "$TLIBDIR/$BASE"
167             done
168             RET=$?
169         fi
170     fi
171     indent_chars=${old_indent_chars}
172     return $RET
175 #if [ -z "$MNTIMAGE" -o -z "$IMAGE" ]; then
176 #    error "Error creating temporaries.  Try again"
177 #    exit 1
180 rm -fr $MNTIMAGE $IMAGE
182 mkdir -p $MNTIMAGE
183 mkdir -p $MNTIMAGE/bin
184 mkdir -p $MNTIMAGE/etc
185 mkdir -p $MNTIMAGE/dev
186 mkdir -p $MNTIMAGE/proc
187 mkdir -p $MNTIMAGE/sys
188 mkdir -p $MNTIMAGE/sysroot
189 ln -s bin $MNTIMAGE/sbin
190 cp -a $1/lib $MNTIMAGE/lib
192 echo "Building initrd in $MNTIMAGE"
193 inst /sbin/nash "$MNTIMAGE" /bin/nash
194 ln -s /sbin/nash $MNTIMAGE/sbin/modprobe
195 ln -s /lib/ld-2.10.2.so $MNTIMAGE/lib/ld-linux.so.2
197 inst /sbin/insmod "$MNTIMAGE" /bin/insmod
198 inst /sbin/rmmod "$MNTIMAGE" /bin/rmmod
200 # mknod'ing the devices instead of copying them works both with and
201 # without devfs...
202 mkdir $MNTIMAGE/dev/mapper
204 mknod $MNTIMAGE/dev/ram0 b 1 0
205 mknod $MNTIMAGE/dev/ram1 b 1 1
206 ln -sf ram1 $MNTIMAGE/dev/ram
208 mknod $MNTIMAGE/dev/null c 1 3
209 mknod $MNTIMAGE/dev/zero c 1 5
210 mknod $MNTIMAGE/dev/systty c 4 0
211 if ! echo "$(uname -m)" | grep -q "s390"; then 
212   for i in 0 1 2 3 4 5 6 7 8 9 10 11 12 ; do
213     mknod $MNTIMAGE/dev/tty$i c 4 $i
214   done
216 for i in 0 1 2 3 ; do
217     mknod $MNTIMAGE/dev/ttyS$i c 4 $(($i + 64))
218 done
219 mknod $MNTIMAGE/dev/tty c 5 0
220 mknod $MNTIMAGE/dev/console c 5 1
221 mknod $MNTIMAGE/dev/ptmx c 5 2
222 mknod $MNTIMAGE/dev/rtc c 10 135
223 if [ "$(uname -m)" == "ia64" ]; then
224     mknod $MNTIMAGE/dev/efirtc c 10 136
227 cemit()
229     cat >> $RCFILE
232 emit()
234     NONL=""
235     if [ "$1" == "-n" ]; then
236         NONL="-n"
237         shift
238     fi
239     echo $NONL "$@" >> $RCFILE
242 echo -n >| $RCFILE
243 cemit << EOF
244 #!/bin/nash
246 mount -t proc /proc /proc
247 setquiet
248 echo Mounting proc filesystem
249 echo Mounting sysfs filesystem
250 mount -t sysfs /sys /sys
251 echo Creating /dev
252 mount -o mode=0755 -t tmpfs /dev /dev
253 mkdir /dev/pts
254 mount -t devpts -o gid=5,mode=620 /dev/pts /dev/pts
255 mkdir /dev/shm
256 mkdir /dev/mapper
257 echo Creating initial device nodes
258 mknod /dev/null c 1 3
259 mknod /dev/zero c 1 5
260 mknod /dev/systty c 4 0
261 mknod /dev/tty c 5 0
262 mknod /dev/console c 5 1
263 mknod /dev/ptmx c 5 2
264 mknod /dev/rtc c 10 135
266 if [ "$(uname -m)" == "ia64" ]; then
267     emit "mknod /dev/efirtc c 10 136"
270 # XXX really we need to openvt too, in case someting changes the
271 # color palette and then changes vts on fbcon before gettys start.
272 # (yay, fbcon bugs!)
273 if ! echo "$(uname -m)" | grep -q "s390"; then 
274     for i in 0 1 2 3 4 5 6 7 8 9 10 11 12 ; do
275         emit "mknod /dev/tty$i c 4 $i"
276     done
279 for i in 0 1 2 3 ; do
280     emit "mknod /dev/ttyS$i c 4 $(($i + 64))"
281 done
283 emit "echo Setting up hotplug."
284 emit "hotplug"
286 emit "echo Creating block device nodes."
287 emit "mkblkdevs"
289 # HACK: module loading + device creation isn't necessarily synchronous...
290 # this will make sure that we have all of our devices before trying
291 # things like RAID or LVM
292 #emit "mkblkdevs"
294 emit "echo Execute customized programs"