Annotation of /trunk/mkinitrd-magellan/mkinitrd.in
Parent Directory | Revision Log
Revision 1876 -
(hide annotations)
(download)
Wed Jul 11 09:57:18 2012 UTC (12 years, 3 months ago) by niro
File size: 24399 byte(s)
Wed Jul 11 09:57:18 2012 UTC (12 years, 3 months ago) by niro
File size: 24399 byte(s)
-include missing firmware blobs in initrd
1 | niro | 564 | #!/bin/bash |
2 | niro | 738 | # $Header: /home/cvsd/magellan-cvs/magellan-src/mkinitrd-magellan/mkinitrd.in,v 1.4 2008-06-11 22:11:40 niro Exp $ |
3 | niro | 564 | |
4 | # mkinitrd | ||
5 | # | ||
6 | # Copyright 2005 Red Hat, Inc. | ||
7 | # | ||
8 | # Written by Erik Troan <ewt@redhat.com> | ||
9 | # | ||
10 | # Contributors: | ||
11 | # Elliot Lee <sopwith@cuc.edu> | ||
12 | # Miguel de Icaza <miguel@nuclecu.unam.mx> | ||
13 | # Christian 'Dr. Disk' Hechelmann <drdisk@ds9.au.s.shuttle.de> | ||
14 | # Michael K. Johnson <johnsonm@redhat.com> | ||
15 | # Pierre Habraken <Pierre.Habraken@ujf-grenoble.fr> | ||
16 | # Jakub Jelinek <jakub@redhat.com> | ||
17 | # Carlo Arenas Belon (carenas@chasqui.lared.net.pe> | ||
18 | # Keith Owens <kaos@ocs.com.au> | ||
19 | # Bernhard Rosenkraenzer <bero@redhat.com> | ||
20 | # Matt Wilson <msw@redhat.com> | ||
21 | # Trond Eivind Glomsrød <teg@redhat.com> | ||
22 | # Jeremy Katz <katzj@redhat.com> | ||
23 | # Preston Brown <pbrown@redhat.com> | ||
24 | # Bill Nottingham <notting@redhat.com> | ||
25 | # Guillaume Cottenceau <gc@mandrakesoft.com> | ||
26 | # Peter Jones <pjones@redhat.com> | ||
27 | |||
28 | # heavily modified for magellan-linux by Niels Rogalla <niro@magellan-linux.de> | ||
29 | |||
30 | umask 0022 | ||
31 | export MALLOC_PERTURB_=204 | ||
32 | |||
33 | PATH=/sbin:/usr/sbin:/bin:/usr/bin:$PATH | ||
34 | export PATH | ||
35 | |||
36 | VERSION=%VERSIONTAG% | ||
37 | |||
38 | PROBE="yes" | ||
39 | MODULES="" | ||
40 | PREMODS="" | ||
41 | NOSTRIP=false | ||
42 | niro | 1876 | FIRMWARE_PATH="/lib/firmware" |
43 | FIRMWARE_FILES="" | ||
44 | niro | 564 | |
45 | [ -e /etc/conf.d/mkinitrd ] && . /etc/conf.d/mkinitrd | ||
46 | |||
47 | CONFMODS="${MODULES}" | ||
48 | MODULES="" | ||
49 | niro | 1180 | INITEXEC="/sbin/init" |
50 | niro | 564 | |
51 | compress=1 | ||
52 | allowmissing="" | ||
53 | target="" | ||
54 | kernel="" | ||
55 | force="" | ||
56 | verbose="" | ||
57 | img_vers="" | ||
58 | builtins="" | ||
59 | niro | 993 | modulefile="/etc/modules.conf" |
60 | modprobeconf="/etc/modprobe.d/*" | ||
61 | niro | 564 | rc=0 |
62 | |||
63 | IMAGESIZE=4000 | ||
64 | PRESCSIMODS="sd_mod" | ||
65 | fstab="/etc/fstab" | ||
66 | |||
67 | vg_list="" | ||
68 | |||
69 | die() { echo "Error: $@"; exit 1; } | ||
70 | add_initrd() { echo "$@" >> ${DEST}/init || die; } | ||
71 | |||
72 | DEST="$(mktemp -d)" | ||
73 | IMAGE="$(mktemp)" | ||
74 | |||
75 | usage() | ||
76 | { | ||
77 | echo "usage: $(basename $0) [--version] [--help] [-v] [-f] [--preload <module>]" | ||
78 | echo " [--force-ide-probe] [--force-scsi-probe | --omit-scsi-modules]" | ||
79 | echo " [--image-version] [--force-scsi-probe | --omit-raid-modules]" | ||
80 | echo " [--with=<module>] [--force-lvm-probe | --omit-lvm-modules]" | ||
81 | echo " [--builtin=<module>] [--initramfs | --loopback] [--no-strip]" | ||
82 | niro | 1180 | echo " [--fstab=<fstab>] [--nocompress] [--initexec=<init>]" |
83 | echo " <initrd-image> <kernel-version>" | ||
84 | niro | 564 | echo |
85 | echo " (ex: $(basename $0) /boot/initrd-2.2.5-15.img 2.2.5-15)" | ||
86 | |||
87 | exit 0 | ||
88 | } | ||
89 | |||
90 | moduledep() | ||
91 | { | ||
92 | if [ ! -f "/lib/modules/${kernel}/modules.dep" ] | ||
93 | then | ||
94 | die "No dep file found for kernel ${kernel}" | ||
95 | fi | ||
96 | |||
97 | echo "Looking for deps of module $1" | ||
98 | deps=$(awk 'BEGIN { searched=ARGV[2]; ARGV[2]=""; rc=1 } \ | ||
99 | niro | 1219 | function modname(filename) { match(filename, /\/([^\/]+)\.k?o.?g?z?:?$/, ret); return ret[1] } \ |
100 | niro | 564 | function show() { if (orig == searched) { print dep; orig=""; rc=0; exit } } \ |
101 | niro | 845 | /^.*kernel/ { show(); \ |
102 | niro | 564 | orig=modname($1); dep=""; \ |
103 | if ($2) { for (i = 2; i <= NF; i++) { dep=sprintf("%s %s", dep, modname($i)); } } } \ | ||
104 | /^ / { dep=sprintf("%s %s", dep, modname($1)); } \ | ||
105 | END { show(); exit(rc) }' /lib/modules/${kernel}/modules.dep $1) | ||
106 | [ -n "${deps}" ] && echo ":${deps}" || echo | ||
107 | } | ||
108 | |||
109 | findone() { /usr/bin/find "$@" | /bin/awk '{ print $1; exit; }'; } | ||
110 | findall() { /usr/bin/find "$@"; } | ||
111 | |||
112 | findmodule() | ||
113 | { | ||
114 | skiperrors="" | ||
115 | |||
116 | if [[ $1 = --skiperrors ]] | ||
117 | then | ||
118 | skiperrors=--skiperrors | ||
119 | shift | ||
120 | fi | ||
121 | |||
122 | local modName=$1 | ||
123 | |||
124 | if [ "${modName}" = "off" -o "${modName}" = "null" ] | ||
125 | then | ||
126 | return | ||
127 | fi | ||
128 | |||
129 | if [ $(echo ${modName} | cut -b1) = "-" ] | ||
130 | then | ||
131 | skiperrors=--skiperrors | ||
132 | modName=$(echo ${modName} | cut -b2-) | ||
133 | fi | ||
134 | |||
135 | case "${MODULES} " in | ||
136 | *"/${modName}.ko "*) return ;; | ||
137 | niro | 1219 | *"/${modName}.ko.gz "*) return ;; |
138 | niro | 564 | esac |
139 | |||
140 | if echo ${builtins} | egrep -q '(^| )'${modName}'( |$)' | ||
141 | then | ||
142 | echo "module ${modName} assumed to be built in" | ||
143 | return | ||
144 | fi | ||
145 | |||
146 | # special cases | ||
147 | if [[ ${modName} = i2o_block ]] | ||
148 | then | ||
149 | findmodule i2o_core | ||
150 | findmodule -i2o_pci | ||
151 | modName="i2o_block" | ||
152 | elif [[ ${modName} = ppa ]] | ||
153 | then | ||
154 | findmodule parport | ||
155 | findmodule parport_pc | ||
156 | modName="ppa" | ||
157 | elif [[ ${modName} = sbp2 ]] | ||
158 | then | ||
159 | findmodule ieee1394 | ||
160 | findmodule ohci1394 | ||
161 | modName="sbp2" | ||
162 | else | ||
163 | moduledep ${modName} | ||
164 | for i in ${deps} | ||
165 | do | ||
166 | findmodule ${i} | ||
167 | done | ||
168 | fi | ||
169 | |||
170 | niro | 1219 | for modExt in o.gz o ko ko.gz |
171 | niro | 564 | do |
172 | for modDir in /lib/modules/${kernel}/updates /lib/modules/${kernel} | ||
173 | do | ||
174 | if [ -d ${modDir} ] | ||
175 | then | ||
176 | fmPath=$(findone ${modDir} -name ${modName}.${modExt}) | ||
177 | if [ -f "${fmPath}" ] | ||
178 | then | ||
179 | break 2 | ||
180 | fi | ||
181 | fi | ||
182 | done | ||
183 | done | ||
184 | |||
185 | if [ ! -f ${fmPath} ] | ||
186 | then | ||
187 | if [ -n "${skiperrors}" ] | ||
188 | then | ||
189 | return | ||
190 | fi | ||
191 | |||
192 | # ignore the absence of the scsi modules | ||
193 | for n in ${PRESCSIMODS} | ||
194 | do | ||
195 | if [[ ${n} = ${modName} ]] | ||
196 | then | ||
197 | return | ||
198 | fi | ||
199 | done | ||
200 | |||
201 | if [ -n "${allowmissing}" ] | ||
202 | then | ||
203 | echo "WARNING: No module ${modName} found for kernel ${kernel}, continuing anyway" | ||
204 | return | ||
205 | fi | ||
206 | |||
207 | die "No module ${modName} found for kernel ${kernel}, aborting." | ||
208 | fi | ||
209 | |||
210 | # only need to add each module once | ||
211 | MODULES="${MODULES} ${fmPath}" | ||
212 | } | ||
213 | |||
214 | inst() | ||
215 | { | ||
216 | if [ "$#" != "2" ] | ||
217 | then | ||
218 | echo "usage: inst <file> <destination>" | ||
219 | return | ||
220 | fi | ||
221 | echo "$1 -> $2" | ||
222 | cp $1 $2 | ||
223 | } | ||
224 | |||
225 | findstoragedriver() | ||
226 | { | ||
227 | for device in $@ | ||
228 | do | ||
229 | case " ${handledevices} " in | ||
230 | *" ${device} "*) continue ;; | ||
231 | *) handleddevices="${handleddevices} ${device}" ;; | ||
232 | esac | ||
233 | |||
234 | if [[ "${device}" =~ "md[0-9]+" ]] | ||
235 | then | ||
236 | echo "Found RAID component ${device}" | ||
237 | handleraid "${device}" | ||
238 | continue | ||
239 | fi | ||
240 | echo "Looking for driver for device ${device}" | ||
241 | sysfs=$(findone /sys/block -name ${device} -type d) | ||
242 | [ -z "${sysfs}" ] && return | ||
243 | pushd ${sysfs} >/dev/null 2>&1 | ||
244 | while [ ! -L device ] | ||
245 | do | ||
246 | [[ ${PWD} = /sys ]] && continue 2 | ||
247 | cd .. | ||
248 | done | ||
249 | cd $(readlink ./device) | ||
250 | while [ ! -f modalias ] | ||
251 | do | ||
252 | [[ ${PWD} = /sys/devices ]] && continue 2 | ||
253 | cd .. | ||
254 | done | ||
255 | modalias=$(cat modalias) | ||
256 | for driver in $(modprobe --set-version ${kernel} --show-depends ${modalias} 2>/dev/null| awk '{ print gensub(".*/","","g",$2) }') | ||
257 | do | ||
258 | niro | 1219 | findmodule ${driver%%.*} |
259 | niro | 564 | done |
260 | popd >/dev/null 2>&1 | ||
261 | done | ||
262 | } | ||
263 | |||
264 | handleraid() | ||
265 | { | ||
266 | local start=0 | ||
267 | |||
268 | if [ -n "${noraid}" -o ! -f /proc/mdstat ] | ||
269 | then | ||
270 | return 0 | ||
271 | fi | ||
272 | |||
273 | levels=$(grep "^$1[ ]*:" /proc/mdstat | \ | ||
274 | awk '{ print $4 }') | ||
275 | devs=$(grep "^$1[ ]*:" /proc/mdstat | \ | ||
276 | awk '{ print gensub("\\[[0-9]*\\]","","g",gensub("^md.*raid[0-9]*","","1")) }') | ||
277 | |||
278 | for level in ${levels} | ||
279 | do | ||
280 | case ${level} in | ||
281 | linear) | ||
282 | findmodule linear | ||
283 | start=1 | ||
284 | ;; | ||
285 | multipath) | ||
286 | findmodule multipath | ||
287 | start=1 | ||
288 | ;; | ||
289 | raid[01456]) | ||
290 | findmodule ${level} | ||
291 | start=1 | ||
292 | ;; | ||
293 | *) | ||
294 | error "raid level ${level} (in /proc/mdstat) not recognized" | ||
295 | ;; | ||
296 | esac | ||
297 | done | ||
298 | findstoragedriver ${devs} | ||
299 | if [[ ${start} = 1 ]] | ||
300 | then | ||
301 | raiddevices="${raiddevices} $1" | ||
302 | fi | ||
303 | return ${start} | ||
304 | } | ||
305 | |||
306 | handlelvordev() | ||
307 | { | ||
308 | local vg=$(lvs --noheadings -o vg_name $1 2>/dev/null) | ||
309 | if [ -n "${vg}" ] | ||
310 | then | ||
311 | vg=$(echo ${vg}) # strip whitespace | ||
312 | case " ${vg_list} " in | ||
313 | *" ${vg} "*) | ||
314 | ;; | ||
315 | *) | ||
316 | vg_list="${vg_list} ${vg}" | ||
317 | for device in $(vgdisplay -v ${vg} 2>/dev/null | sed -n 's/PV Name//p') | ||
318 | do | ||
319 | findstoragedriver ${device##/dev/} | ||
320 | done | ||
321 | ;; | ||
322 | esac | ||
323 | else | ||
324 | findstoragedriver ${1##/dev/} | ||
325 | fi | ||
326 | } | ||
327 | |||
328 | niro | 897 | get_blkid() |
329 | { | ||
330 | local UUID | ||
331 | local SEC_TYPE | ||
332 | local TYPE | ||
333 | |||
334 | local dev="$1" | ||
335 | [[ -z ${dev} ]] && die "no dev given" | ||
336 | |||
337 | niro | 901 | # check if given device is already an UUID |
338 | if [[ ${dev/UUID=/}x != ${dev}x ]] | ||
339 | then | ||
340 | eval "${dev}" | ||
341 | else | ||
342 | eval $(busybox.mkinitrd blkid ${dev} | grep "${dev}:" | sed 's/.*:\ //') | ||
343 | fi | ||
344 | niro | 897 | echo ${UUID} |
345 | } | ||
346 | |||
347 | niro | 1876 | add_firmware() |
348 | { | ||
349 | local firmware="$1" | ||
350 | local i | ||
351 | |||
352 | # check if given firmware is already included in FIRMWARE_FILES | ||
353 | for i in ${FIRMWARE_FILES} | ||
354 | do | ||
355 | [[ ${firmware} = ${i} ]] && return 0 | ||
356 | done | ||
357 | |||
358 | # include firmware | ||
359 | FIRMWARE_FILES="${FIRMWARE_FILES} ${firmware}" | ||
360 | } | ||
361 | |||
362 | niro | 564 | # need r00t privs |
363 | [[ $(id -u) != 0 ]] && die "you must be r00t." | ||
364 | |||
365 | while [ $# -gt 0 ]; do | ||
366 | case $1 in | ||
367 | --fstab*) | ||
368 | if echo $1 | grep -q '=' ; then | ||
369 | fstab=$(echo $1 | sed 's/^--fstab=//') | ||
370 | else | ||
371 | fstab=$2 | ||
372 | shift | ||
373 | fi | ||
374 | ;; | ||
375 | |||
376 | --with-usb) withusb=yes ;; | ||
377 | |||
378 | --with*) | ||
379 | if echo $1 | grep -q '=' ; then | ||
380 | modname=$(echo $1 | sed 's/^--with=//') | ||
381 | else | ||
382 | modname=$2 | ||
383 | shift | ||
384 | fi | ||
385 | basicmodules="${basicmodules} ${modname}" | ||
386 | ;; | ||
387 | |||
388 | --builtin*) | ||
389 | if echo $1 | grep -q '=' ; then | ||
390 | modname=$(echo $1 | sed 's/^--builtin=//') | ||
391 | else | ||
392 | modname=$2 | ||
393 | shift | ||
394 | fi | ||
395 | builtins="${builtins} ${modname}" | ||
396 | ;; | ||
397 | |||
398 | --version) | ||
399 | echo "mkinitrd: version ${VERSION}" | ||
400 | exit 0 | ||
401 | ;; | ||
402 | |||
403 | -v) verbose=-v ;; | ||
404 | --nocompress) compress="" ;; | ||
405 | --ifneeded) | ||
406 | # legacy | ||
407 | ;; | ||
408 | -f) force=1 ;; | ||
409 | |||
410 | --preload*) | ||
411 | if echo $1 | grep -q '=' ; then | ||
412 | modname=$(echo $1 | sed 's/^--preload=//') | ||
413 | else | ||
414 | modname=$2 | ||
415 | shift | ||
416 | fi | ||
417 | PREMODS="${PREMODS} ${modname}" | ||
418 | ;; | ||
419 | |||
420 | --force-scsi-probe) forcescsi=1 ;; | ||
421 | |||
422 | --omit-scsi-modules) | ||
423 | PRESCSIMODS="" | ||
424 | noscsi=1 | ||
425 | ;; | ||
426 | --force-raid-probe) forceraid=1 ;; | ||
427 | --omit-raid-modules) noraid=1 ;; | ||
428 | --force-lvm-probe) forcelvm=1 ;; | ||
429 | --omit-lvm-modules) nolvm=1 ;; | ||
430 | --force-ide-probe) forceide=1 ;; | ||
431 | --image-version) img_vers=yes ;; | ||
432 | --allow-missing) allowmissing=yes ;; | ||
433 | --noresume) noresume=1 ;; | ||
434 | --help) usage -n ;; | ||
435 | --initramfs) IMAGE_TYPE=initramfs ;; | ||
436 | --loopback) IMAGE_TYPE=loopback ;; | ||
437 | --no-strip) NOSTRIP=true ;; | ||
438 | niro | 1180 | --initexec*) |
439 | if echo $1 | grep -q '=' ; then | ||
440 | INITEXEC=$(echo $1 | sed 's/^--initexec=//') | ||
441 | else | ||
442 | INITEXEC=$2 | ||
443 | shift | ||
444 | fi | ||
445 | ;; | ||
446 | niro | 564 | *) |
447 | if [ -z "${target}" ] | ||
448 | then | ||
449 | target=$1 | ||
450 | elif [ -z "${kernel}" ] | ||
451 | then | ||
452 | kernel=$1 | ||
453 | else | ||
454 | usage | ||
455 | fi | ||
456 | ;; | ||
457 | esac | ||
458 | |||
459 | shift | ||
460 | done | ||
461 | |||
462 | # enforce initramfs method as default | ||
463 | [[ -z ${IMAGE_TYPE} ]] && IMAGE_TYPE=initramfs | ||
464 | |||
465 | if [ -z "${target}" -o -z "${kernel}" ] | ||
466 | then | ||
467 | usage | ||
468 | fi | ||
469 | |||
470 | if [ -n "${img_vers}" ] | ||
471 | then | ||
472 | target="${target}-${kernel}" | ||
473 | fi | ||
474 | |||
475 | if [ -z "${force}" -a -f ${target} ] | ||
476 | then | ||
477 | die "${target} already exists." | ||
478 | fi | ||
479 | |||
480 | if [ -n "${forcescsi}" -a -n "${noscsi}" ] | ||
481 | then | ||
482 | die "Can't both force scsi probe and omit scsi modules" | ||
483 | fi | ||
484 | |||
485 | if [ -n "${forceraid}" -a -n "${noraid}" ] | ||
486 | then | ||
487 | die "Can't both force raid probe and omit raid modules" | ||
488 | fi | ||
489 | |||
490 | if [ -n "${forcelvm}" -a -n "${nolvm}" ] | ||
491 | then | ||
492 | die "Can't both force LVM probe and omit LVM modules" | ||
493 | fi | ||
494 | |||
495 | if [ ! -d /lib/modules/${kernel} ] | ||
496 | then | ||
497 | die "No modules available for kernel '${kernel}'." | ||
498 | fi | ||
499 | |||
500 | ############################## | ||
501 | ####### get modules ########## | ||
502 | |||
503 | echo "Creating ${IMAGE_TYPE} initrd image" | ||
504 | |||
505 | for n in ${PREMODS} | ||
506 | do | ||
507 | findmodule ${n} | ||
508 | done | ||
509 | |||
510 | needusb="" | ||
511 | if [ -n "${withusb}" -a "x${PROBE}" == "xyes" ] | ||
512 | then | ||
513 | # If / or /boot is on a USB device include the driver. With root by | ||
514 | # label we could still get some odd behaviors | ||
515 | for fs in / /boot | ||
516 | do | ||
517 | esc=$(echo ${fs} | sed 's,/,\\/,g') | ||
518 | niro | 1140 | dev=$(mount | awk "/ on ${esc} / { print \$1 }" | sed 's/[0-9]*$//') |
519 | if [ "$(echo ${dev} | cut -c6-7)" = sd ] | ||
520 | niro | 564 | then |
521 | niro | 1140 | if [ $(which hwinfo 2>/dev/null) ] |
522 | niro | 564 | then |
523 | niro | 1140 | if [[ -n $(hwinfo --disk --only ${dev} | grep 'Driver Modules' | grep 'usb[_|-]storage') ]] |
524 | niro | 564 | then |
525 | needusb=1 | ||
526 | fi | ||
527 | fi | ||
528 | fi | ||
529 | done | ||
530 | fi | ||
531 | |||
532 | if [ -n "${needusb}" -a "x${PROBE}" == "xyes" ] | ||
533 | then | ||
534 | drivers=$(awk '/^alias[[:space:]]+usb-controller[0-9]* / { print $3}' < ${modulefile}) | ||
535 | if [ -n "${drivers}" ] | ||
536 | then | ||
537 | for driver in ${drivers} | ||
538 | do | ||
539 | findmodule ${driver} | ||
540 | done | ||
541 | findmodule scsi_mod | ||
542 | findmodule sd_mod | ||
543 | findmodule usb-storage | ||
544 | fi | ||
545 | fi | ||
546 | |||
547 | if [ -n "${forcescsi}" -o -z "${noscsi}" -a "x${PROBE}" == "xyes" ] | ||
548 | then | ||
549 | if [ ! -f ${modulefile} ] | ||
550 | then | ||
551 | modulefile=/etc/conf.modules | ||
552 | fi | ||
553 | |||
554 | if [ -f ${modulefile} ] | ||
555 | then | ||
556 | scsimodules=$(grep "alias[[:space:]]\+scsi_hostadapter" ${modulefile} | grep -v '^[ ]*#' | LC_ALL=C sort -u | awk '{ print $3 }') | ||
557 | |||
558 | if [ -n "${scsimodules}" ] | ||
559 | then | ||
560 | for n in ${PRESCSIMODS} | ||
561 | do | ||
562 | findmodule ${n} | ||
563 | done | ||
564 | |||
565 | for n in ${scsimodules} | ||
566 | do | ||
567 | # for now allow scsi modules to come from anywhere. There are some | ||
568 | # RAID controllers with drivers in block/ | ||
569 | findmodule ${n} | ||
570 | done | ||
571 | fi | ||
572 | fi | ||
573 | fi | ||
574 | |||
575 | # If we have ide devices and module ide, do the right thing | ||
576 | ide=/proc/ide/ide* | ||
577 | if [ -n "${forceide}" -o -n "${ide}" -a "x${PROBE}" == "xyes" ] | ||
578 | then | ||
579 | findmodule -ide-disk | ||
580 | fi | ||
581 | |||
582 | # If we have dasd devices, include the necessary modules (S/390) | ||
583 | if [ "x${PROBE}" == "xyes" -a -d /proc/dasd ] | ||
584 | then | ||
585 | findmodule -dasd_mod | ||
586 | findmodule -dasd_eckd_mod | ||
587 | findmodule -dasd_fba_mod | ||
588 | fi | ||
589 | |||
590 | if [ "x${PROBE}" == "xyes" ] | ||
591 | then | ||
592 | # check to see if we need to set up a loopback filesystem | ||
593 | rootdev=$(awk '/^[ \t]*[^#]/ { if ($2 == "/") { print $1; }}' ${fstab}) | ||
594 | # check if it's root by label | ||
595 | if echo ${rootdev} | cut -c1-6 | grep -q "LABEL=" | ||
596 | then | ||
597 | majmin=$(stat -c "%t:%T" /dev/root) | ||
598 | if [ -n "${majmin}" ] | ||
599 | then | ||
600 | dev=$(findall /sys/block -name dev | while read device ; do \ | ||
601 | echo "${majmin}" | cmp -s ${device} && echo ${device} ; done \ | ||
602 | | sed -e 's,.*/\([^/]\+\)/dev,\1,' ) | ||
603 | if [ -n "${dev}" ]; then | ||
604 | echo "Found root device ${dev} for ${rootdev}" | ||
605 | rootdev=${dev} | ||
606 | fi | ||
607 | fi | ||
608 | fi | ||
609 | if echo ${rootdev} | cut -d/ -f3 | grep -q loop | ||
610 | then | ||
611 | key="^# $(echo ${rootdev} | cut -d/ -f3 | tr '[a-z]' '[A-Z]'):" | ||
612 | if ! grep "${key}" ${fstab} >> /dev/null | ||
613 | then | ||
614 | echo "The root filesystem is on a ${rootdev}, but there is no magic entry in ${fstab}" | ||
615 | echo "for this device. Consult the mkinitrd man page for more information" | ||
616 | echo "loopback root." | ||
617 | exit 1 | ||
618 | fi | ||
619 | |||
620 | line=$(grep "${key}" ${fstab}) | ||
621 | loopDev=$(echo ${line} | awk '{print $3}') | ||
622 | loopFs=$(echo ${line} | awk '{print $4}') | ||
623 | loopFile=$(echo ${line} | awk '{print $5}') | ||
624 | |||
625 | basicmodules="${basicmodules} -loop" | ||
626 | if [ "${loopFs}" = "vfat" -o "${loopFs}" = "msdos" ] | ||
627 | then | ||
628 | basicmodules="${basicmodules} -fat" | ||
629 | fi | ||
630 | basicmodules="${basicmodules} -${loopFs}" | ||
631 | # check if the root fs is on a logical volume | ||
632 | # this check must be last | ||
633 | else | ||
634 | handlelvordev ${rootdev} | ||
635 | fi | ||
636 | |||
637 | rootfs=$(awk '{ if ($1 !~ /^[ \t]*#/ && $2 == "/") { print $3; }}' ${fstab}) | ||
638 | rootopts=$(awk '{ if ($1 !~ /^[ \t]*#/ && $2 == "/") { print $4; }}' ${fstab}\ | ||
639 | | sed -e 's/^r[ow],//' -e 's/,r[ow],$//' -e 's/,r[ow],/,/' \ | ||
640 | -e 's/^r[ow]$/defaults/' -e 's/$/,ro/') | ||
641 | |||
642 | # in case the root filesystem is modular | ||
643 | findmodule -${rootfs} | ||
644 | |||
645 | # find the first swap dev which would get used for swsusp | ||
646 | swsuspdev=$(awk '/^[ \t]*[^#]/ { if ($3 == "swap") { print $1; }}' ${fstab} \ | ||
647 | | head -n 1) | ||
648 | if ! echo ${swsuspdev} | cut -c1-6 | grep -q "LABEL=" | ||
649 | then | ||
650 | handlelvordev ${swsupdev} | ||
651 | fi | ||
652 | fi | ||
653 | |||
654 | # If we use LVM or dm-based raid, include dm-mod | ||
655 | # XXX: dm not really supported yet. | ||
656 | testdm="" | ||
657 | [ -n "${vg_list}" ] && testdm="yes" | ||
658 | [ -n "${forceraid}" -o -n "${forcelvm}" ] && testdm="yes" | ||
659 | [ "x${PROBE}" == "xyes" -a \ | ||
660 | -n "$(test -z \"${nolvm}\" -o -z \"${noraid}\")" ] && testdm="yes" | ||
661 | |||
662 | if [ -n "${testdm}" ] | ||
663 | then | ||
664 | if [ -x /sbin/dmsetup -a -e /dev/mapper/control ] | ||
665 | then | ||
666 | dmout=$(/sbin/dmsetup ls 2>/dev/null) | ||
667 | if [ "${dmout}" != "No devices found" -a "${dmout}" != "" ] | ||
668 | then | ||
669 | findmodule -dm-mod | ||
670 | |||
671 | # DM requires all of these to be there in case someone used the | ||
672 | # feature. broken. (#132001) | ||
673 | findmodule -dm-mirror | ||
674 | findmodule -dm-zero | ||
675 | findmodule -dm-snapshot | ||
676 | fi | ||
677 | fi | ||
678 | fi | ||
679 | |||
680 | for n in ${basicmodules} | ||
681 | do | ||
682 | findmodule ${n} | ||
683 | done | ||
684 | |||
685 | for n in ${CONFMODS} | ||
686 | do | ||
687 | findmodule ${n} | ||
688 | done | ||
689 | |||
690 | echo "Using modules:${MODULES}" | ||
691 | |||
692 | ####### eof modules ########## | ||
693 | ############################## | ||
694 | |||
695 | |||
696 | # create basic filesystem layout | ||
697 | niro | 894 | install -d ${DEST}/{bin,dev,etc,lib,loopfs,proc,sbin,sys,sysroot,usr/bin,usr/sbin} || die |
698 | niro | 564 | |
699 | # install busybox | ||
700 | echo "$(which busybox.mkinitrd)" | ||
701 | install -m0755 $(which busybox.mkinitrd) ${DEST}/bin/busybox || die | ||
702 | for i in '[' ash basename cat cut clear cp dirname echo env false find \ | ||
703 | grep gunzip gzip ln ls loadkmap mkdir mknod more mount mv \ | ||
704 | ps pwd rm rmdir sh sleep test touch true umount uname \ | ||
705 | xargs yes chmod chown | ||
706 | do | ||
707 | # use soft links from now on; | ||
708 | # hardlinks in initramfs apears to be broken on newer kernels | ||
709 | # -> "request module: runaway loop modprobe binfmt-0000" | ||
710 | # see: http://www.gossamer-threads.com/lists/linux/kernel/728042 | ||
711 | ln -s /bin/busybox ${DEST}/bin/${i} || die "${i}" | ||
712 | done | ||
713 | |||
714 | # sbin related | ||
715 | for i in chroot init losetup modprobe pivot_root | ||
716 | do | ||
717 | niro | 894 | ln -s ${DEST}/bin/busybox ${DEST}/sbin/${i} || die "${i}" |
718 | niro | 564 | done |
719 | |||
720 | # install run-init | ||
721 | echo "$(which run-init.mkinitrd)" | ||
722 | install -m0755 $(which run-init.mkinitrd) ${DEST}/sbin/run-init || die | ||
723 | |||
724 | # install insmod.static | ||
725 | niro | 894 | #echo "$(which insmod.static)" |
726 | #install -m0755 $(which insmod.static) ${DEST}/sbin/insmod || die | ||
727 | niro | 564 | |
728 | niro | 703 | # install v86d for uvesafb support if installed |
729 | if which v86d &> /dev/null | ||
730 | then | ||
731 | niro | 894 | echo "$(which v86d)" |
732 | niro | 703 | install -m0755 $(which v86d) ${DEST}/sbin/v86d || die |
733 | fi | ||
734 | |||
735 | niro | 564 | # install lvm |
736 | if [ -n "${vg_list}" ] | ||
737 | then | ||
738 | inst /sbin/lvm.static "${DEST}/sbin/lvm" | ||
739 | if [ -f /etc/lvm/lvm.conf ] | ||
740 | then | ||
741 | cp ${verbose} --parents /etc/lvm/lvm.conf ${DEST}/ | ||
742 | fi | ||
743 | fi | ||
744 | |||
745 | # create some needed device nodes | ||
746 | mkdir ${DEST}/dev/pts || die | ||
747 | mkdir ${DEST}/dev/shm || die | ||
748 | mkdir ${DEST}/dev/mapper || die | ||
749 | mkdir ${DEST}/dev/misc || die | ||
750 | mkdir ${DEST}/dev/fb || die | ||
751 | mkdir ${DEST}/dev/vc || die | ||
752 | mknod ${DEST}/dev/console c 5 1 || die | ||
753 | mknod ${DEST}/dev/null c 1 3 || die | ||
754 | mknod ${DEST}/dev/ram b 1 1 || die | ||
755 | mknod ${DEST}/dev/rtc c 10 135 || die | ||
756 | mknod ${DEST}/dev/systty c 4 0 || die | ||
757 | mknod ${DEST}/dev/tty c 5 0 || die | ||
758 | mknod ${DEST}/dev/zero c 1 5 || die | ||
759 | for ((i=0; i < 13; i++)) | ||
760 | do | ||
761 | mknod ${DEST}/dev/tty${i} c 4 ${i} || die "tty${i}" | ||
762 | done | ||
763 | for ((i=64; i < 68; i++)) | ||
764 | do | ||
765 | mknod ${DEST}/dev/ttyS$((${i} - 64)) c 4 ${i} || die "ttyS$((${i} - 64))" | ||
766 | done | ||
767 | |||
768 | # create init script | ||
769 | :> ${DEST}/init || die | ||
770 | add_initrd '#!/bin/sh' | ||
771 | |||
772 | add_initrd 'mount -t proc proc /proc' | ||
773 | add_initrd 'mount -t sysfs sysfs /sys' | ||
774 | niro | 1792 | add_initrd "if [[ -z \$(grep '[[:space:]]/dev[[:space:]]' /proc/mounts | cut -d ' ' -f2) ]]" |
775 | add_initrd 'then' | ||
776 | add_initrd ' # many video drivers needed exec access' | ||
777 | add_initrd ' fstype=ramfs' | ||
778 | add_initrd ' # tmpfs was suggested by Greg Kroah-Hartman' | ||
779 | add_initrd " [[ \$(grep '[[:space:]]tmpfs' /proc/filesystems | cut -d \$'\\t' -f2) = tmpfs ]] && fstype=tmpfs" | ||
780 | add_initrd ' # mount devtmpfs if supported' | ||
781 | add_initrd " [[ \$(grep '[[:space:]]devtmpfs' /proc/filesystems | cut -d \$'\\t' -f2) = devtmpfs ]] && fstype=devtmpfs" | ||
782 | add_initrd ' mount -t ${fstype} dev /dev -o exec,nosuid,mode=0755,size=10M' | ||
783 | add_initrd 'fi' | ||
784 | niro | 564 | |
785 | [[ ${IMAGE_TYPE} = loopback ]] && add_initrd 'mount -t ramfs -o remount,rw /dev/ram /' | ||
786 | |||
787 | niro | 894 | # install all busybox applets |
788 | add_initrd 'busybox --install -s' | ||
789 | |||
790 | niro | 564 | # silencio |
791 | add_initrd 'echo 0 > /proc/sys/kernel/printk' | ||
792 | |||
793 | niro | 737 | add_initrd 'echo "-- Creating initial device nodes ..."' |
794 | niro | 894 | # populate dev |
795 | add_initrd 'mdev -s' | ||
796 | # handle hotplug events properly | ||
797 | add_initrd 'echo /sbin/mdev > /proc/sys/kernel/hotplug' | ||
798 | niro | 564 | |
799 | # loading detected modules with options | ||
800 | for MODULE in ${MODULES} | ||
801 | do | ||
802 | text="" | ||
803 | niro | 1219 | module=$(echo ${MODULE} | sed "s|.*/||" | sed -e "s/.k\?o.\?g\?z\?$//" -e "s/.gz//") |
804 | fullmodule=$(echo ${MODULE} | sed -e "s|.*/||" -e "s/.gz//") | ||
805 | niro | 564 | |
806 | niro | 993 | options=$(sed -n -e "s/^options[ ][ ]*${module}[ ][ ]*//p" ${modprobeconf} 2>/dev/null) |
807 | niro | 564 | |
808 | if [ -n "${options}" ] | ||
809 | then | ||
810 | echo "Adding module ${module}${text} with options ${options} ..." | ||
811 | else | ||
812 | echo "Adding module ${module}${text} ..." | ||
813 | fi | ||
814 | |||
815 | add_initrd "echo \"-- Loading ${fullmodule} module\"" | ||
816 | add_initrd "insmod /lib/${fullmodule} ${options}" | ||
817 | |||
818 | # Hack - we need a delay after loading usb-storage to give things | ||
819 | # time to settle down before we start looking a block devices | ||
820 | if [ "${module}" = "usb-storage" ] | ||
821 | then | ||
822 | add_initrd 'echo Waiting 8 seconds for driver initialization.' | ||
823 | add_initrd 'sleep 8' | ||
824 | fi | ||
825 | if [ "${module}" = "zfcp" -a -f /etc/zfcp.conf ] | ||
826 | then | ||
827 | add_initrd 'echo Waiting 2 seconds for driver initialization.' | ||
828 | add_initrd 'sleep 2' | ||
829 | cat /etc/zfcp.conf | grep -v "^#" | tr "A-Z" "a-z" | while read DEVICE SCSIID WWPN SCSILUN FCPLUN | ||
830 | do | ||
831 | add_initrd "echo -n ${WWPN} > /sys/bus/ccw/drivers/zfcp/${DEVICE/0x/}/port_add" | ||
832 | add_initrd "echo -n ${FCPLUN} > /sys/bus/ccw/drivers/zfcp/${DEVICE/0x/}/${WWPN}/unit_add" | ||
833 | add_initrd "echo -n 1 > /sys/bus/ccw/drivers/zfcp/${DEVICE/0x/}/online" | ||
834 | done | ||
835 | fi | ||
836 | niro | 1876 | |
837 | # get firmware files if neccessary | ||
838 | if [ -x $(which modinfo 2> /dev/null) ] | ||
839 | then | ||
840 | firmware_blobs=$(modinfo ${module}${text} | grep 'firmware:' | sed 's:^firmware\:.*[[:space:]]\(.*\):\1:') | ||
841 | if [[ ! -z ${firmware_blobs} ]] | ||
842 | then | ||
843 | for firmware in ${firmware_blobs} | ||
844 | do | ||
845 | add_firmware "${firmware}" | ||
846 | done | ||
847 | fi | ||
848 | else | ||
849 | echo "Warning 'modinfo' not found" | ||
850 | fi | ||
851 | niro | 564 | done |
852 | |||
853 | niro | 1515 | # respect rootdelay kernel param |
854 | add_initrd 'for i in $(cat /proc/cmdline); do case ${i} in rootdelay=*) ROOTDELAY="${i#*=}";; esac; done' | ||
855 | add_initrd 'if [ ! -z "${ROOTDELAY}" ]; then echo "-- Requested a rootdelay of ${ROOTDELAY} seconds ..."; sleep ${ROOTDELAY};fi' | ||
856 | |||
857 | niro | 564 | # lvm support |
858 | if [ -n "${vg_list}" ] | ||
859 | then | ||
860 | add_initrd 'echo Scanning logical volumes' | ||
861 | add_initrd 'lvm vgscan --ignorelockingfailure' | ||
862 | add_initrd 'echo Activating logical volumes' | ||
863 | add_initrd "lvm vgchange -ay --ignorelockingfailure ${vg_list}" | ||
864 | fi | ||
865 | |||
866 | # loopback support | ||
867 | if [ -n "${loopDev}" ] | ||
868 | then | ||
869 | # needed on the host system | ||
870 | install -d /initrd | ||
871 | cp -a ${loopDev} ${DEST}/dev || die | ||
872 | cp -a ${rootdev} ${DEST}/dev || die | ||
873 | add_initrd 'echo "Mounting device containing loopback root filesystem ..."' | ||
874 | add_initrd "mount -t ${loopFs} ${loopDev} /loopfs" | ||
875 | add_initrd "echo Setting up loopback device ${rootdev}" | ||
876 | add_initrd "losetup ${rootdev} /loopfs${loopFile}" | ||
877 | fi | ||
878 | |||
879 | # software suspend | ||
880 | if [ -z "${noresume}" ] | ||
881 | then | ||
882 | add_initrd "[ -x /bin/resume ] && resume ${swsuspdev}" | ||
883 | fi | ||
884 | |||
885 | # raid | ||
886 | if [ -n "${raiddevices}" ] | ||
887 | then | ||
888 | for dev in ${raiddevices} | ||
889 | do | ||
890 | cp -a /dev/${dev} ${DEST}/dev || die | ||
891 | add_initrd "raidautorun /dev/${dev}" | ||
892 | done | ||
893 | fi | ||
894 | |||
895 | niro | 1180 | # get the right initexec |
896 | add_initrd "INITEXEC=\"${INITEXEC}\"" | ||
897 | # support init=* from cmdline | ||
898 | niro | 1516 | add_initrd 'for i in $(cat /proc/cmdline); do case ${i} in init=*) INITEXEC="${i#*=}";; esac; done' |
899 | niro | 1180 | |
900 | niro | 564 | add_initrd 'echo "-- Mounting sysroot ..."' |
901 | add_initrd '. /etc/rootdev' | ||
902 | add_initrd 'echo "${device} /sysroot ${rootfs} defaults,ro 0 0" > /etc/fstab' | ||
903 | niro | 738 | add_initrd 'mount /sysroot || ash' |
904 | niro | 564 | |
905 | niro | 894 | # unset hotplug event-manager |
906 | add_initrd 'echo > /proc/sys/kernel/hotplug' | ||
907 | |||
908 | niro | 564 | # de-silencio |
909 | add_initrd 'echo 3 > /proc/sys/kernel/printk' | ||
910 | |||
911 | niro | 1188 | add_initrd 'BREAK_ENABLED=no' |
912 | niro | 1516 | add_initrd 'for i in $(cat /proc/cmdline); do case ${i} in break) BREAK_ENABLED=yes;; esac; done' |
913 | add_initrd 'if [ "${BREAK_ENABLED}" = "yes" ]; then echo "-- Break requested, type 'exit' to resume operation ..."; ash; fi' | ||
914 | niro | 1188 | |
915 | niro | 564 | add_initrd 'echo "-- Switching to real sysroot ..."' |
916 | niro | 1793 | add_initrd 'mount --move /dev /sysroot/dev' |
917 | add_initrd 'mount --move /sys /sysroot/sys' | ||
918 | add_initrd 'mount --move /proc /sysroot/proc' | ||
919 | niro | 564 | |
920 | niro | 1180 | [[ ${IMAGE_TYPE} = initramfs ]] && add_initrd 'exec run-init /sysroot ${INITEXEC} $@ </sysroot/dev/console >/sysroot/dev/console' |
921 | niro | 564 | if [[ ${IMAGE_TYPE} = loopback ]] |
922 | then | ||
923 | add_initrd 'cd /sysroot' | ||
924 | add_initrd 'pivot_root . initrd' | ||
925 | niro | 1180 | add_initrd "exec /sbin/chroot . sh -c 'umount -n /initrd >/dev/null 2>&1;exec -a init.new \${INITEXEC}'</dev/console >/dev/console 2>&1" |
926 | niro | 564 | fi |
927 | |||
928 | chmod +x ${DEST}/init || die | ||
929 | |||
930 | # setup root block device | ||
931 | [[ -z ${rootdev} ]] && die "no known rootdev found." | ||
932 | niro | 897 | echo "device='UUID=$(get_blkid ${rootdev})'" > ${DEST}/etc/rootdev || die |
933 | echo "rootfs='${rootfs}'" >> ${DEST}/etc/rootdev || die | ||
934 | niro | 564 | |
935 | # install detected modules | ||
936 | echo "Coping your modules to '${DEST}/lib' ..." | ||
937 | for MODULE in ${MODULES} | ||
938 | do | ||
939 | if [ -x /usr/bin/strip ] && [[ ${NOSTRIP} = false ]] | ||
940 | then | ||
941 | /usr/bin/strip -g ${verbose} ${MODULE} -o ${DEST}/lib/$(basename ${MODULE}) || die | ||
942 | niro | 1219 | # handle gzip compressed modules right |
943 | elif [[ ${MODULE##*.} = gz ]] | ||
944 | then | ||
945 | zcat ${verbose} ${MODULE} > ${DEST}/lib/$(basename ${MODULE} .gz) || die | ||
946 | niro | 564 | else |
947 | cp ${verbose} -a ${MODULE} ${DEST}/lib || die | ||
948 | fi | ||
949 | done | ||
950 | |||
951 | niro | 1876 | # install firmware if neccessary |
952 | if [[ ! -z ${FIRMWARE_FILES} ]] | ||
953 | then | ||
954 | for firmware in ${FIRMWARE_FILES} | ||
955 | do | ||
956 | if [ -e ${FIRMWARE_PATH}/${firmware} ] | ||
957 | then | ||
958 | echo "installing firmware '${firmware}'" | ||
959 | install ${verbose} -d ${DEST}/${FIRMWARE_PATH}/${firmware%/*} || die | ||
960 | cp ${verbose} -a ${FIRMWARE_PATH}/${firmware} ${DEST}/${FIRMWARE_PATH}/${firmware%/*} || die | ||
961 | fi | ||
962 | done | ||
963 | fi | ||
964 | |||
965 | niro | 564 | # create the image |
966 | echo "Creating initrd image '${target}' ..." | ||
967 | if [[ ${IMAGE_TYPE} = initramfs ]] | ||
968 | then | ||
969 | (cd "${DEST}" ; find . | cpio --quiet -o -H newc | gzip -9 >${IMAGE}) | ||
970 | cat ${IMAGE} > ${target} || die | ||
971 | fi | ||
972 | |||
973 | if [[ ${IMAGE_TYPE} = loopback ]] | ||
974 | then | ||
975 | MOUNTTMP="$(mktemp -d)" | ||
976 | |||
977 | # create ext2 img | ||
978 | dd if=/dev/zero of=${IMAGE} bs=1024 count=${IMAGESIZE} &> /dev/null || die | ||
979 | mke2fs -m 0 -i 1024 -F ${IMAGE} &> /dev/null || die | ||
980 | mount -o loop ${IMAGE} ${MOUNTTMP} || die | ||
981 | mv ${DEST}/* ${MOUNTTMP} || die | ||
982 | umount ${MOUNTTMP} || die | ||
983 | gzip -9 ${IMAGE} || die | ||
984 | cat ${IMAGE}.gz > ${target} || die | ||
985 | [ -d ${MOUNTTMP} ] && rm -rf ${MOUNTTMP} | ||
986 | |||
987 | # create inird dir for pivot_root | ||
988 | [ ! -d /initrd ] && { install -d /initrd || die; } | ||
989 | fi | ||
990 | |||
991 | # clean up | ||
992 | [ -d ${DEST} ] && rm -rf ${DEST} | ||
993 | [ -f ${IMAGE} ] && rm -f ${IMAGE} | ||
994 | |||
995 | # all went ok at this point | ||
996 | exit 0 |