Removed all references to an initrd and renamed most initr{d,amfs} stuff to simply...
[genkernel.git] / defaults / linuxrc
1 #!/bin/sh
2
3 . /etc/initrd.defaults
4 . /etc/initrd.scripts
5
6 splash() {
7         return 0
8 }
9
10 [ -e /etc/initrd.splash ] && . /etc/initrd.splash
11
12 # Clean input/output
13 exec >${CONSOLE} <${CONSOLE} 2>&1
14
15 if [ "$$" != '1' ]
16 then
17         echo '/linuxrc has to be run as the init process as the one'
18         echo 'with a PID of 1. Try adding init="/linuxrc" to the'
19         echo 'kernel command line or running "exec /linuxrc".'
20         exit 1
21 fi
22
23 mount -t proc proc /proc >/dev/null 2>&1
24 mount -o remount,rw / >/dev/null 2>&1
25
26 # Set up symlinks
27 /bin/busybox --install -s
28
29 if [ "$0" = '/init' ]
30 then
31         [ -e /linuxrc ] && rm /linuxrc
32          
33         if [ -e /bin/lvm ]
34         then
35                 ln -s /bin/lvm /bin/vgscan
36                 ln -s /bin/lvm /bin/vgchange
37         fi
38 fi
39
40 quiet_kmsg
41
42 CMDLINE=`cat /proc/cmdline`
43 # Scan CMDLINE for any specified real_root= or cdroot arguments
44 REAL_ROOT=''
45 FAKE_ROOT=''
46 REAL_ROOTFLAGS=''
47 CRYPT_SILENT=0
48 for x in ${CMDLINE}
49 do
50         case "${x}" in
51                 real_root\=*)
52                         REAL_ROOT=`parse_opt "${x}"`
53                 ;;
54                 root\=*)
55                         FAKE_ROOT=`parse_opt "${x}"`
56                 ;;
57                 subdir\=*)
58                         SUBDIR=`parse_opt "${x}"`
59                 ;;
60                 real_init\=*)
61                         REAL_INIT=`parse_opt "${x}"`
62                 ;;
63                 init_opts\=*)
64                         INIT_OPTS=`parse_opt "${x}"`
65                 ;;
66                 # Livecd options
67                 cdroot)
68                         CDROOT=1
69                 ;;
70                 cdroot\=*)
71                         CDROOT=1
72                         CDROOT_DEV=`parse_opt "${x}"`
73                 ;;
74                 cdroot_type\=*)
75                         CDROOT_TYPE=`parse_opt "${x}"`
76                 ;;
77                 # Start livecd loop, looptype options
78                 loop\=*)
79                         LOOP=`parse_opt "${x}"`
80                 ;;
81                 looptype\=*)
82                         LOOPTYPE=`parse_opt "${x}"`
83                 ;;
84                 # Start Volume manager options 
85                 dolvm)
86                         USE_LVM_NORMAL=1
87                 ;;
88                 dolvm2)
89                         bad_msg 'Using dolvm2 is deprecated, use dolvm, instead.'
90                         USE_LVM_NORMAL=1
91                 ;;
92                 domdadm)
93                         USE_MDADM=1
94                 ;;
95                 dodmraid)
96                         USE_DMRAID_NORMAL=1
97                 ;;
98                 dodmraid\=*)
99                         DMRAID_OPTS=`parse_opt "${x}"`
100                         USE_DMRAID_NORMAL=1
101                 ;;
102                 doevms)
103                         USE_EVMS_NORMAL=1
104                 ;;
105                 doevms2)
106                         bad_msg 'Using doevms2 is deprecated, use doevms, instead.'
107                         USE_EVMS_NORMAL=1
108                 ;;
109                 # Debug Options
110                 debug)
111                         DEBUG='yes'
112                 ;;
113                 # Scan delay options 
114                 scandelay\=*)
115                         SDELAY=`parse_opt "${x}"`
116                 ;;
117                 scandelay)
118                         SDELAY=10
119                 ;;
120                 # Module no-loads
121                 doload\=*)
122                         MDOLIST=`parse_opt "${x}"`
123                         MDOLIST="`echo ${MDOLIST} | sed -e 's/,/ /g'`"
124                 ;;
125                 nodetect)
126                         NODETECT=1
127                 ;;
128                 noload\=*)
129                         MLIST=`parse_opt "${x}"`
130                         MLIST="`echo ${MLIST} | sed -e 's/,/ /g'`"
131                         export MLIST
132                 ;;
133                 # Redirect output to a specific tty
134                 CONSOLE\=*\|console\=*)
135                         CONSOLE=`parse_opt "${x}"`
136                         exec >${CONSOLE} <${CONSOLE} 2>&1
137                 ;;
138                 # /dev/md
139                 lvmraid\=*)
140                         RAID_DEVICES="`parse_opt ${x}`"
141                         RAID_DEVICES="`echo ${RAID_DEVICES} | sed -e 's/,/ /g'`"
142                 ;;
143                 part\=*)
144                         MDPART=`parse_opt "${x}"`
145                 ;;
146                 # NFS
147                 ip\=*)
148                         IP=`parse_opt "${x}"`
149                 ;;
150                 nfsroot\=*)
151                         NFSROOT=`parse_opt "${x}"`
152                 ;;
153                 crypt_root\=*)
154                         CRYPT_ROOT=`parse_opt "${x}"`
155                 ;;
156                 crypt_swap\=*)
157                         CRYPT_SWAP=`parse_opt "${x}"`
158                 ;;
159                 root_key\=*)
160                         CRYPT_ROOT_KEY=`parse_opt "${x}"`
161                 ;;
162                 root_keydev\=*)
163                         CRYPT_ROOT_KEYDEV=`parse_opt "${x}"`
164                 ;;
165                 swap_key\=*)
166                         CRYPT_SWAP_KEY=`parse_opt "${x}"`
167                 ;;
168                 swap_keydev\=*)
169                         CRYPT_SWAP_KEYDEV=`parse_opt "${x}"`
170                 ;;
171                 real_resume\=*)
172                         REAL_RESUME=`parse_opt "${x}"`
173                 ;;
174                 noresume)
175                         NORESUME=1
176                 ;;
177                 crypt_silent)
178                         CRYPT_SILENT=1
179                 ;;
180                 real_rootflags\=*)
181                         REAL_ROOTFLAGS=`parse_opt "${x}"`
182                 ;;
183                 keymap\=*)
184                         keymap=`parse_opt "${x}"`
185                 ;;
186         esac
187 done
188
189 if [ -z "${REAL_ROOT}" -a \( "${CDROOT}" -eq 0 \)  -a \( "${FAKE_ROOT}" != "/dev/ram0" \) ]
190 then
191         REAL_ROOT="${FAKE_ROOT}"        
192 fi
193
194 splash 'init'
195
196 detect_sbp2_devices
197 cmdline_hwopts
198
199 # Mount sysfs
200 mount_sysfs
201
202 # Setup hotplugging for firmware loading
203 setup_hotplug
204
205 # Load modules listed in MY_HWOPTS if /lib/modules exists for the running kernel
206 if [ -z "${DO_modules}" ]
207 then
208         good_msg 'Skipping module load; disabled via commandline'
209 elif [ -d "/lib/modules/${KV}" ]
210 then
211         good_msg 'Loading modules'
212         # Load appropriate kernel modules
213         if [ "${NODETECT}" != '1' ]
214         then
215                 for modules in ${MY_HWOPTS}
216                 do
217                         modules_scan ${modules}
218                 done
219         fi
220         # Always eval doload=...
221         modules_load ${MDOLIST}
222 else
223         good_msg 'Skipping module load; no modules in the ramdisk!'
224 fi
225
226 # Delay if needed for USB hardware
227 sdelay
228
229 # Start device manager
230 start_dev_mgr
231
232 # Setup md device nodes if they dont exist
233 setup_md_device
234
235 # Scan volumes
236 startVolumes
237
238 setup_keymap
239
240 # Initialize LUKS root device except for livecd's
241 if [ "${CDROOT}" != 1 ]
242 then
243         startLUKS
244         if [ "${NORESUME}" != '1' ] && [ -n "${REAL_RESUME}" ]
245         then
246                 swsusp_resume
247 #               suspend_resume
248                 tuxonice_resume
249         fi
250 fi
251
252 mkdir -p "${NEW_ROOT}"
253 CHROOT="${NEW_ROOT}"
254
255 # Run debug shell if requested
256 rundebugshell
257
258 if [ "${CDROOT}" = '1' ]
259 then
260         good_msg "Making tmpfs for ${NEW_ROOT}"
261         mount -t tmpfs tmpfs "${NEW_ROOT}"
262         
263         for i in dev mnt mnt/cdrom mnt/livecd mnt/key tmp tmp/.initrd mnt/gentoo sys
264         do
265                 mkdir -p "${NEW_ROOT}/${i}"
266                 chmod 755 "${NEW_ROOT}/${i}"
267         done
268         [ ! -e "${NEW_ROOT}/dev/null" ] && mknod "${NEW_ROOT}"/dev/null c 1 3
269         [ ! -e "${NEW_ROOT}/dev/console" ] && mknod "${NEW_ROOT}"/dev/console c 5 1
270
271         # For SGI LiveCDs ...
272         if [ "${LOOPTYPE}" = "sgimips" ]
273         then
274                 [ ! -e "${NEW_ROOT}/dev/sr0" ] && mknod "${NEW_ROOT}/dev/sr0" b 11 0
275                 [ ! -e "${NEW_ROOT}/dev/loop0" ] && mknod "${NEW_ROOT}/dev/loop0" b 7 0
276         fi
277
278         # Required for splash to work.  Not an issue with the initrd as this
279         # device isn't created there and is not needed.
280         if [ -e /dev/tty1 ]
281         then
282                 [ ! -e "${NEW_ROOT}/dev/tty1" ] && mknod "${NEW_ROOT}/dev/tty1" c 4 1
283         fi
284
285         if [ "${REAL_ROOT}" != "/dev/nfs" ] && [ "${LOOPTYPE}" != "sgimips" ]
286         then
287                 bootstrapCD
288         fi
289
290         if [ "${REAL_ROOT}" = '' ]
291         then
292                 echo -n -e "${WARN}>>${NORMAL}${BOLD} No bootable medium found. Waiting for new devices"
293                 
294                 COUNTER=0
295                 while [ ${COUNTER} -lt 3 ]; do
296                         sleep 3
297                         echo -n '.'
298                         let COUNTER=${COUNTER}+1
299                 done
300
301                 sleep 1
302                 echo -e "${NORMAL}"
303                 bootstrapCD
304         fi
305
306         if [ "${REAL_ROOT}" = '' ]
307         then
308                 # Undo stuff
309                 umount  "${NEW_ROOT}/dev" 2>/dev/null
310                 umount  "${NEW_ROOT}/sys" 2>/dev/null
311                 umount /sys 2>/dev/null
312
313                 umount  "${NEW_ROOT}"
314                 rm -rf  "${NEW_ROOT}/*"
315
316                 bad_msg 'Could not find CD to boot, something else needed!'
317                 CDROOT=0
318         fi
319 fi
320
321 # Determine root device
322 good_msg 'Determining root device...'
323 while true
324 do
325         while [ "${got_good_root}" != '1' ]
326         do
327                 case "${REAL_ROOT}" in
328                         LABEL\=*|UUID\=*)
329                         
330                                 ROOT_DEV=""
331                                 retval=1
332                                 
333                                 if [ "${retval}" -ne '0' ]; then
334                                         ROOT_DEV=`findfs "${REAL_ROOT}" 2>/dev/null`
335                                         retval=$?
336                                 fi
337                                 
338                                 if [ "$retval" -ne '0' ]; then
339                                         ROOT_DEV=`busybox findfs "${REAL_ROOT}" 2>/dev/null`
340                                         retval=$?
341                                 fi
342                                 
343                                 if [ "${retval}" -ne '0' ]; then
344                                         ROOT_DEV=`blkid -t "${REAL_ROOT}" | cut -d ":" -f 1 2>/dev/null`
345                                         retval=$?
346                                 fi
347                                 
348                                 if [ "${retval}" -eq '0' ] && [ -n "${ROOT_DEV}" ]; then
349                                         good_msg "Detected real_root=${ROOT_DEV}"
350                                         REAL_ROOT="${ROOT_DEV}"
351                                 else
352                                         whereis "REAL_ROOT" "root block device"
353                                         got_good_root=0
354                                         continue
355                                 fi
356                                 ;;
357                 esac
358                                 
359                 if [ "${REAL_ROOT}" = '' ]
360                 then
361                         # No REAL_ROOT determined/specified. Prompt user for root block device.
362                         whereis "REAL_ROOT" "root block device"
363                         got_good_root=0
364
365                 # Check for a block device or /dev/nfs
366                 elif [ -b "${REAL_ROOT}" ] || [ "${REAL_ROOT}" = "/dev/nfs" ]
367                 then
368                         got_good_root=1
369
370                 else
371                         bad_msg "Block device ${REAL_ROOT} is not a valid root device..."
372                         REAL_ROOT=""
373                         got_good_root=0
374                 fi
375         done
376
377
378         if [ "${CDROOT}" = 1 -a "${got_good_root}" = '1' -a "${REAL_ROOT}" != "/dev/nfs" ]
379         then
380                 # CD already mounted; no further checks necessary
381                 break
382         elif [ "${LOOPTYPE}" = "sgimips" ]
383         then
384                 # sgimips mounts the livecd root partition directly
385                 # there is no isofs filesystem to worry about
386                 break
387         else
388                 good_msg "Mounting root..."
389
390                 # Try to mount the device as ${NEW_ROOT}
391                 if [ "${REAL_ROOT}" = '/dev/nfs' ]; then
392                         findnfsmount
393                 else
394                         # mount ro so fsck doesn't barf later
395                         if [ "${REAL_ROOTFLAGS}" = '' ]; then
396                                 mount -o ro ${REAL_ROOT} ${NEW_ROOT}
397                         else
398                                 good_msg "Using mount -o ro,${REAL_ROOTFLAGS}"
399                                 mount -o ro,${REAL_ROOTFLAGS} ${REAL_ROOT} ${NEW_ROOT}
400                         fi
401                 fi
402                 
403                 # If mount is successful break out of the loop 
404                 # else not a good root and start over.
405                 if [ "$?" = '0' ]
406                 then
407                         if [ -d ${NEW_ROOT}/dev -a -x ${NEW_ROOT}/sbin/init ]
408                         then
409                                 break
410                         else
411                                 bad_msg "The filesystem mounted at ${REAL_ROOT} does not appear to be a valid /, try again"
412                                 got_good_root=0
413                                 REAL_ROOT=''
414                         fi
415                 else
416                         bad_msg "Could not mount specified ROOT, try again"
417                         got_good_root=0
418                         REAL_ROOT=''
419                 fi
420         fi
421 done
422 # End determine root device
423
424 #verbose_kmsg
425
426 # If CD root is set determine the looptype to boot
427 if [ "${CDROOT}" = '1' ]
428 then
429         good_msg 'Determining looptype ...'
430         cd "${NEW_ROOT}"
431
432         # Find loop and looptype
433         [ -z "${LOOP}" ] && find_loop
434         [ -z "${LOOPTYPE}" ] && find_looptype
435
436         cache_cd_contents
437
438         # If encrypted, find key and mount, otherwise mount as usual
439         if [ -n "${CRYPT_ROOT}" ] 
440         then
441                 CRYPT_ROOT_KEY="$(head -n 1 ${NEW_ROOT}/mnt/cdrom/livecd)"
442                 CRYPT_ROOT='/dev/loop0'
443                 good_msg 'You booted an encrypted livecd' "${CRYPT_SILENT}"
444
445                 losetup /dev/loop0 "${NEW_ROOT}/mnt/cdrom/${LOOPEXT}${LOOP}"
446                 test_success 'Preparing loop filesystem'
447
448                 startLUKS
449
450                 case ${LOOPTYPE} in
451                         normal)
452                                 MOUNTTYPE="ext2"
453                                 ;;
454                         *)
455                                 MOUNTTYPE="${LOOPTYPE}"
456                                 ;;
457                 esac
458                 mount -t "${MOUNTTYPE}" -o ro /dev/mapper/root "${NEW_ROOT}/mnt/livecd"
459                 test_success 'Mount filesystem'
460                 FS_LOCATION='mnt/livecd'
461         # Setup the loopback mounts, if unencrypted
462         else
463                 if [ "${LOOPTYPE}" = 'normal' ]
464                 then
465                         good_msg 'Mounting loop filesystem'
466                         mount -t ext2 -o loop,ro "${NEW_ROOT}/mnt/cdrom/${LOOPEXT}${LOOP}" "${NEW_ROOT}/mnt/livecd"
467                         test_success 'Mount filesystem'
468                         FS_LOCATION='mnt/livecd'
469                 elif [ "${LOOPTYPE}" = 'squashfs' ]
470                 then
471                         good_msg 'Mounting squashfs filesystem'
472                         mount -t squashfs -o loop,ro "${NEW_ROOT}/mnt/cdrom/${LOOPEXT}${LOOP}" "${NEW_ROOT}/mnt/livecd"
473                         test_success 'Mount filesystem'
474                         FS_LOCATION='mnt/livecd'
475                 elif [ "${LOOPTYPE}" = 'gcloop' ]
476                 then
477                         good_msg 'Mounting gcloop filesystem'
478                         echo ' ' | losetup -E 19 -e ucl-0 -p0 "${NEW_ROOT}/dev/loop0" "${NEW_ROOT}/mnt/cdrom/${LOOPEXT}${LOOP}"
479                         test_success 'losetup the loop device'
480
481                         mount -t ext2 -o ro "${NEW_ROOT}/dev/loop0" "${NEW_ROOT}/mnt/livecd"
482                         test_success 'Mount the losetup loop device'
483                         FS_LOCATION='mnt/livecd'
484                 elif [ "${LOOPTYPE}" = 'zisofs' ]
485                 then
486                         FS_LOCATION="mnt/cdrom/${LOOPEXT}${LOOP}"
487                 elif [ "${LOOPTYPE}" = 'noloop' ]
488                 then
489                         FS_LOCATION='mnt/cdrom'
490                 elif [ "${LOOPTYPE}" = 'sgimips' ]
491                 then
492                         # getdvhoff finds the starting offset (in bytes) of the squashfs
493                         # partition on the cdrom and returns this offset for losetup
494                         #
495                         # All currently supported SGI Systems use SCSI CD-ROMs, so
496                         # so we know that the CD-ROM is usually going to be /dev/sr0.
497                         #
498                         # We use the value given to losetup to set /dev/loop0 to point
499                         # to the liveCD root partition, and then mount /dev/loop0 as
500                         # the LiveCD rootfs
501                         good_msg 'Locating the SGI LiveCD Root Partition'
502                         echo ' ' | \
503                                 losetup -o $(getdvhoff "${NEW_ROOT}${REAL_ROOT}" 0) \
504                                         "${NEW_ROOT}${CDROOT_DEV}" \
505                                         "${NEW_ROOT}${REAL_ROOT}"
506                         test_success 'losetup /dev/sr0 /dev/loop0'
507         
508                         good_msg 'Mounting the Root Partition'
509                         mount -t squashfs -o ro "${NEW_ROOT}${CDROOT_DEV}" "${NEW_ROOT}/mnt/livecd"
510                         test_success 'mount /dev/loop0 /'
511                         FS_LOCATION='mnt/livecd'
512                 fi
513         fi
514
515         # Unpacking additional packages from NFS mount
516         # This is useful for adding kernel modules to /lib
517         # We do this now, so that additional packages can add whereever they want.
518         if [ "${REAL_ROOT}" = '/dev/nfs' ]
519         then
520                 if [ -e "${CHROOT}/mnt/cdrom/add" ]
521                 then
522                                 for targz in `ls ${CHROOT}/mnt/cdrom/add/*.tar.gz`
523                                 do      
524                                         tarname=`basename ${targz}`
525                                         good_msg "Adding additional package ${tarname}"
526                                         (cd ${CHROOT} ; /bin/tar -xzf ${targz})
527                                 done
528                 fi
529         fi
530
531         
532         good_msg "Copying read-write image contents to tmpfs"
533         # Copy over stuff that should be writable
534         (cd "${NEW_ROOT}/${FS_LOCATION}"; cp -a ${ROOT_TREES} "${NEW_ROOT}")
535
536         # Now we do the links.
537         for x in ${ROOT_LINKS}
538         do
539                 if [ -L "${NEW_ROOT}/${FS_LOCATION}/${x}" ]
540                 then
541                         ln -s "`readlink ${NEW_ROOT}/${FS_LOCATION}/${x}`" "${x}" 2>/dev/null
542                 else
543                         # List all subdirectories of x
544                         find "${NEW_ROOT}/${FS_LOCATION}/${x}" -type d 2>/dev/null | while read directory
545                         do
546                                 # Strip the prefix of the FS_LOCATION
547                                 directory="${directory#${NEW_ROOT}/${FS_LOCATION}/}"
548
549                                 # Skip this directory if we already linked a parent directory
550                                 if [ "${curent_parrent}" != '' ]; then
551                                         var=`echo "${directory}" | grep "^${curent_parrent}"`
552                                         if [ "${var}" != '' ]; then
553                                                 continue
554                                         fi
555                                 fi
556                                 # Test if the directory exists already
557                                 if [ -e "/${NEW_ROOT}/${directory}" ]
558                                 then
559                                         # It does exist, link all the individual files
560                                         for file in `ls /${NEW_ROOT}/${FS_LOCATION}/${directory}`
561                                         do
562                                         if [ ! -d "/${NEW_ROOT}/${FS_LOCATION}/${directory}/${file}" ] && [ ! -e "${NEW_ROOT}/${directory}/${file}" ]; then
563                                                         ln -s "/${FS_LOCATION}/${directory}/${file}" "${directory}/${file}" 2> /dev/null
564                                                 fi
565                                         done
566                                 else
567                                         # It does not exist, make a link to the livecd
568                                         ln -s "/${FS_LOCATION}/${directory}" "${directory}" 2>/dev/null
569                                         current_parent="${directory}"
570                                 fi
571                         done
572                 fi
573         done
574
575         if [ "${DO_slowusb}" ] || [ "${FORCE_slowusb}" ]
576         then
577                 sleep 10
578         fi
579         mkdir initramfs proc tmp sys 2>/dev/null
580         chmod 1777 tmp
581
582         #UML=`cat /proc/cpuinfo|grep UML|sed -e 's|model name.*: ||'`
583         #if [ "${UML}" = 'UML' ]
584         #then
585         #       # UML Fixes
586         #       good_msg 'Updating for uml system'
587         #fi
588
589         # Let Init scripts know that we booted from CD
590         export CDBOOT
591         CDBOOT=1
592         check_slowusb
593         if [ "${DO_slowusb}" ] || [ "${FORCE_slowusb}" ]
594         then
595                 sleep 10
596         fi
597 fi
598
599 # Execute script on the cdrom just before boot to update things if necessary
600 cdupdate
601
602 if [ "${SUBDIR}" != '' -a -e "${CHROOT}/${SUBDIR}" ]
603 then
604         good_msg "Entering ${SUBDIR} to boot"
605         CHROOT="${CHROOT}/${SUBDIR}"
606 fi
607
608 verbose_kmsg
609
610 echo -ne "${GOOD}>>${NORMAL}${BOLD} Booting (initramfs)${NORMAL}"
611 if [ ! -e "${CHROOT}/dev/console" ] || [ ! -e "${CHROOT}/dev/null" ]
612 then
613         echo -ne "${BAD}>>${NORMAL}${BOLD} ERROR: your /dev is missing files required to boot (console and null)${NORMAL}"
614 elif [ -e /etc/initrd.splash -a ! -e "${CHROOT}/dev/tty1" ]
615 then
616         echo -ne "${BAD}>>${NORMAL}${BOLD} ERROR: your /dev is missing tty1, which is required for splash${NORMAL}"
617 fi
618
619 cd "${CHROOT}"
620 mkdir "${CHROOT}/proc" "${CHROOT}/sys" 2>/dev/null
621 echo -ne "${BOLD}.${NORMAL}"
622 umount /sys || echo '*: Failed to unmount the ramdisk /sys!'
623 umount /proc || echo '*: Failed to unmount the ramdisk /proc!'
624 echo -e "${BOLD}.${NORMAL}"
625
626 exec switch_root -c "/dev/console" "${CHROOT}" "${REAL_INIT:-/sbin/init}" "${INIT_OPTS}"
627
628 # If we get here, something bad has happened
629 splash 'verbose'
630
631 echo 'A fatal error has probably occured since /sbin/init did not'
632 echo 'boot correctly. Trying to open a shell...'
633 echo
634 exec /bin/bash
635 exec /bin/sh
636 exec /bin/ash
637 exec sh