summaryrefslogtreecommitdiffstats
path: root/initrd/mkdxsinitrd
blob: d1bf2e8dee83117148d5c061fb7ab4344a3f4d0a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#!/bin/bash
#
# Description:  universal (distro independent) generator for initial
#		ramdisks for linux diskless clients 
#
# Author(s):    Dirk von Suchodoletz <dirk@goe.net>, 16-01-2006
#		Blabla
#		Blub
#
# Copyright:    (c) 2003 - 2006 - RZ Universitaet Freiburg


INSTDIR=/tmp/dxs-instrd
DEBUG=1
LINRC=${INSTDIR}/init
COMETCEXCL="XF86Config*\nissue*\nmtab*\nfstab*\n"
COMDIRINDXS="/tmp/scratch /var/lib/nobody"

# distro specific settings read from configuration file
# SYSCONFDIR - system wide configuration settings in /etc - sysconfig in  
# SuSE and RedHat, default in Debian and Ubuntu, conf.d in Gentoo
# DISETCEXCL - list of files, wildcards to be excluded from /etc when using 
# bind mounts
# DIRINBINDMNT - lists of directories to be created in bind mounted rw
# part of the client filesystem
# RODIRSINRW - ReadOnly Directories in RW part of filesystem to save on 
# TempFS usage
# DISDIRINDXS - directories in client filesystem which should be present 
# anyhow
# DISBINDMNT - any code which is specific in bind mount environment
# RCDIRS - runlevel directories relative to /etc

SYSCONFDIR="<set in ~/distro-config/distro file>"
DISETCEXCL="<set in ~/distro-config/distro file>"
BINDMPTS="<set in ~/distro-config/distro file>"
DIRINBINDMNT="<set in ~/distro-config/distro file>"
RODIRSINRW="<set in ~/distro-config/distro file>"
DISDIRINDXS="<set in ~/distro-config/distro file>"
DISBINDMNT="<set in ~/distro-config/distro file>"
INITDIR="<set in ~/distro-config/distro file>"
INITBOOTD="<set in ~/distro-config/distro file>"
RCDIRS="<set in ~/distro-config/distro file>"
RCDIRSINRW="<set in ~/distro-config/distro file>"
INITSCRIPTS="<set in ~/distro-config/distro file>"
XF86CONFFILE="<set in ~/distro-config/distro file>"
XFONTPATH="<set in ~/distro-config/distro file>"
DEFAULTCOUNTRY="<set in ~/distro-config/distro file>"

#########################################################################
# End of global variable declaration, nothing needed to be changed at 
# default below that line

# copy some binary to given destination. Takes binary in $1 and 
# destination in $2
cobi()
{
local DEST=$2
local FPTB=`which $1`
if [ -z ${FPTB} ] ; then
  return 1;
else
  cp ${FPTB} ${INSTDIR}/${DEST}
fi
}

repco()
{
local SRC=$1
local DEST=$2
local HEADER=$3
# do not forget to define all variables which should be replaced within
# the start and configuration scripts
echo -e ${HEADER} >${DEST}
sed -e "s,@@@INSTDIR@@@,${INSTDIR},g" \
    -e "s,@@@NWMODULES@@@,${NWMODULES},g" \
    -e "s,@@@KERNVER@@@,${KERNVER},g" \
    -e "s,@@@COMDIRINDXS@@@,${COMDIRINDXS},g" \
    -e "s,@@@COMETCEXCL@@@,${COMETCEXCL},g" \
    -e "s,@@@DISTRO@@@,${DISTRO},g" \
    ${SRC} >> ${DEST}
}

# Cat from stdin to linuxrc, removing leading whitespace up to pipe symbol.
# Note that here documents can only be indented with tabs, not with spaces.
cat_linuxrc() {
    sed 's/^[ \t]*|//'>> ${LINRC}
    echo >> ${LINRC}
}


# Resolve dynamic library dependencies. Returns a list of symbolic links
# to shared objects and shared object files for the binaries in $*.
# stolen from SuSE mkinitrd
shared_object_files() {
    local LDD CHROOT initrd_libs lib_files lib_links lib link

    LDD=`which ldd`
    if [ ! -x $LDD ]; then
	error 2 "I need execution permission on ldd ($LDD)."
    fi

    initrd_libs=( $(
	$LDD "$@" \
	| sed -ne 's:\t\(.* => \)\?\(/.*\) (0x[0-9a-f]*):\2:p'
    ) )

    # Evil hack: On some systems we have generic as well as optimized
    # libraries, but the optimized libraries may not work with all
    # kernel versions (e.g., the NPTL glibc libraries don't work with
    # a 2.4 kernel). Use the generic versions of the libraries in the
    # initrd (and guess the name).
    local n optimized
    for ((n=0; $n<${#initrd_libs[@]}; n++)); do
	lib=${initrd_libs[$n]}
	optimized="$(echo "$lib" | sed -e 's:.*/\([^/]\+/\)[^/]\+$:\1:')"
	lib=${lib/$optimized/}
	if [ "${optimized:0:3}" != "lib" -a -f "$lib" ]; then
	    #echo "[Using $lib instead of ${initrd_libs[$n]}]" >&2
	    initrd_libs[$n]="${lib/$optimized/}"
	fi
    done

    for lib in "${initrd_libs[@]}"; do
	case "$lib" in
	linux-gate*)
	    # This library is mapped into the process by the kernel
	    # for vsyscalls (i.e., syscalls that don't need a user/
	    # kernel address space transition) in 2.6 kernels.
	    continue ;;
	/*)
	    lib="${lib:1}" ;;
	*)
	    # Library could not be found.
	    oops 7 "Dynamic library $lib not found"
	    continue ;;
	esac

	while [ -L "/$lib" ]; do
	    echo $lib
	    link="$(readlink "/$lib")"
	    if [ x"${link:0:1}" == x"/" ]; then
	        lib=${link#/}
	    else
	        lib="${lib%/*}/$link"
	    fi
	done
	echo $lib
    done \
    | sort -u
}

# Resolve module dependencies and parameters. Returns a list of modules and
# their parameters.
resolve_modules() {
    local kernel_version=$1 module
    shift

    for module in "$@"; do
	local with_modprobe_conf
	module=${module%.o}  # strip trailing ".o" just in case.
	module=${module%.ko}  # strip trailing ".ko" just in case.
	if [ -e /etc/modprobe.conf ]; then
	    with_modprobe_conf="-C /etc/modprobe.conf"
	fi
	module_list=$(/sbin/modprobe $with_modprobe_conf \
	    --set-version $kernel_version --ignore-install \
	    --show-depends $module 2> /dev/null \
	    | sed -ne 's:.*insmod /\?::p' )
	if [ -z "$module_list" ]; then
	    oops 7 "Cannot determine dependencies of module $module." \
		"Is modules.dep up to date?"
	fi
	echo "$module_list"
    done \
    | awk ' # filter duplicates: we must not reorder modules here!
	NF == 0     { next }
	$1 in seen  { next }
		    { seen[$1]=1
		      print
		    }
    '
    rm -f $temp
}



#########################################################################
# End of function declaration

while getopts :hgMRk:i:m:b:d:o:s:Sut:D:I:V:a: a ; do
    case $a in
	\:|\?)	case $OPTARG in
		k)  echo "-k requires kernel list parameter"
		    ;;
		i)  echo "-i requires initrd list parameter"
		    ;;
		m)  echo "-m requires module list parameter"
		    ;;
		b)  echo "-b requires boot dir parameter"
		    ;;
		d)  echo "-d requires root device parameter"
		    ;;
		s)  echo "-s requires image size(s)"
		    ;;
		t)  echo "-t requires tmp dir parameter"
		    ;;
		D)  echo "-D requires dhcp interface parameter"
		    ;;
		I)  echo "-I requires network interface parameter"
		    ;;
		a)  echo "-a requires a DSDT parameter"
		    ;;
		V)  echo "-V requires an executable to run inside linuxrc"
		    ;;
		*)  echo "Unknown option: -$OPTARG"
		    echo "Try mkinitrd -h"
		    ;;
	    esac
	    exit 1
	    ;;
	g)  use_glibc=1
	    ;;
	k)  kernel_images=$OPTARG
	    ;;
	i)  initrd_images=$OPTARG
	    ;;
	m)  modules=$OPTARG
	    modules_set=1
	    ;;
	b)  boot_dir=$OPTARG
	    ;;
	d)  rootdev=$OPTARG
	    ;;
	s)  splash=$OPTARG
	    ;;
	t)  tmp_dir=$OPTARG
	    ;;
	D)  interface=$OPTARG
	    interface=${interface#/dev/}
	    use_dhcp=1
	    ;;
 	I)  interface=$OPTARG
 	    interface=${interface#/dev/}
 	    use_ipconfig=1
	    ;;
	a)  acpi_dsdt="$OPTARG"
	    ;;
	S)  use_selinux=1
	    ;;
	V)  vendor_init_script="$OPTARG"
	    ;;
	R)  mkinit_type="rd"
	    ;;
	u)  sysfs_root=1
	    use_udev=
	    ;;
	h)  usage
	    ;;
    esac
done
shift $(expr $OPTIND - 1)


#########################################################################
# End of parameter, argument interpretation

rm -rf ${INSTDIR}
mkdir -p ${INSTDIR}/{dev,proc,tmp,mnt,root,bin,sbin,sys,lib}
mkdir -p ${INSTDIR}/usr/share
# needed for KNOPPIX hwsetup not to confuse with ${SYSCONFDIR}
mkdir -p ${INSTDIR}/etc/sysconfig

# source configuration file for the distribution used
DISTRO=suse
case "${DISTRO}.${DISVER}" in
  SuSE*|suse*|Suse*|SuSe*)
	DISTRO=suse-10.0
        # hotplug/udev style and stuff
	cp -a /etc/udev /etc/hotplug ${INSTDIR}/etc
	cp -a /etc/sysconfig/hardware  ${INSTDIR}/etc/sysconfig
	;;
  Debian*|debian*|Sarge*|sarge*)
	DISTRO=debian
	;;
  Gentoo*|gentoo*)
	DISTRO=gentoo
	;;
esac
cp distro-specs/${DISTRO}/config ${INSTDIR}/etc/sysconfig
cp distro-specs/${DISTRO}/functions-${DISTRO} ${INSTDIR}/etc/distro-functions
cp -a distro-specs/${DISTRO}/files ${INSTDIR}/etc/sysconfig

#case ${DEBUG} 

#if no klibc
for bbins in ash bash sh; do
  if cobi ${bbins} bin ; then 
    LRCSH=/bin/${bbins}
    ln -fs /bin/${bbins} ${INSTDIR}/bin/sh
    [ "${bbins}" != "bash" ] && ln -fs /bin/${bbins} ${INSTDIR}/bin/bash
    break
  else echo "Binary not found (${bbins})"; fi
done

# if no klibc
for bbins in ip ifconfig; do
  if cobi ${bbins} bin
  then 
    LRCIP=/bin/${bbins} 
    [ "${LRCIP}" = "ifconfig" ] && cobi route bin
    break
  else echo "Binary not found"; fi
done

# hwsetup and common init.d stuff from KNOPPIX, for DXS
cp -a initrd-stuff/* ${INSTDIR}


# distro dependet stuff for etc, booting !?

# needed standard binaries
for bbins in \
    cat chmod chown chroot cp expr killall ln mkdir modprobe \
    mount portmap pivot_root rmmod sed tar umount
  do
  cobi ${bbins} bin || echo "Program ${bbins} not found"
done

# one of the dhcp clients
for dhcp in dhclient dhcpcd pump ipconfig ; do
  which $dhcp && break;
done
cobi $dhcp bin
mkdir -p ${INSTDIR}/var/{lib,run}

# debug binaries
for bbins in \
    date ls lsmod lsof ps rm strace time
  do
  cobi ${bbins} bin || echo "Program ${bbins} not found"
done

# style of hotplug/udev/dev (for etc directories see above!!)
for bbins in udev udevstart
  do
  cobi ${bbins} bin || echo "Program ${bbins} not found"
done

# if nfs
mkdir -p ${INSTDIR}/var/lib/nfs/state

# if nbd
# cobi nbd-client bin

#if nfs - dann modul suchen, kopieren 

case "${DISTRO}" in 
  debian*)
    cp /lib/libnss_compat.so.2 ${INSTDIR}/lib;;
esac

# add needed libraries depending on choosen binaries
for lib in `shared_object_files ${INSTDIR}/bin/*` ; do
  baselib=`basename ${lib}`
  test -e  ${INSTDIR}/lib/${baselib} || \
    cp -a /${lib} ${INSTDIR}/lib
done

# add kernel modules and dependency files
KERNVER=2.6.13-15.7-default
#KERNVER=2.6.12-10-386
NWMODULES="e100 tg3 via-rhine"
FSMODULES="nbd nfs"

for module in `resolve_modules ${KERNVER} af_packet ${NWMODULES} ${FSMODULES}`; do
  mpath=${INSTDIR}/${module%/*}
  mkdir -p ${mpath}
  cp /$module ${mpath}
done
cp /lib/modules/${KERNVER}/modules.* ${INSTDIR}/lib/modules/${KERNVER}

DESC="#!${LRCSH}\n# Description:  universal (distro independent) initial \
ramdisk for\n#\t\tlinux diskless clients.\n\t\t#variable replacement done \
by $0\n#\n# Author(s):    Dirk von Suchodoletz <dirk@goe.net>, 13-12-2005\n\
#\t\tN.N.\n#\t\tN.N.\n# Copyright:    (c) 2003, 2006 - RZ Universitaet \
Freiburg\n"
repco initrd-stuff/init ${INSTDIR}/init ${DESC}
chmod 755 ${INSTDIR}/init ${INSTDIR}/bin/servconfig \
  ${INSTDIR}/bin/hwautocfg ${INSTDIR}/bin/dhcpmkconfig
DESC="# Description: universal (distro independent) fcuntion definition\n\
# Author(s):    Dirk von Suchodoletz <dirk@goe.net>, 07-01-2006\n\
#\t\tN.N.\n#\t\tN.N.\n# Copyright:    (c) 2006 - RZ Universitaet \
Freiburg\n" 
repco initrd-stuff/etc/functions ${INSTDIR}/etc/functions ${DESC}

#########################################################################
# End of file copy procedures


# finally create the compressed initial ramdisk cpio archive
pushd . > /dev/null 2>&1
  cd ${INSTDIR}
  find . ! -name "*~" | cpio -H newc --create | gzip -9 > /tmp/dxsinitrd.gz
popd > /dev/null 2>&1