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