Support module options set in /etc/module.d, amend cmdline options patch
[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 -o noexec,nosuid,nodev 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 fi
33
34 quiet_kmsg
35
36 CMDLINE=$(cat /proc/cmdline)
37 # Scan CMDLINE for any specified real_root= or cdroot arguments
38 FAKE_ROOT=''
39 REAL_ROOTFLAGS=''
40 ROOTFSTYPE='auto'
41 CRYPT_SILENT=0
42 mkdir -p /etc/cmdline /etc/modprobe.d
43 for x in ${CMDLINE}
44 do
45         case "${x}" in
46                 real_root=*)
47                         REAL_ROOT=${x#*=}
48                 ;;
49                 root=*)
50                         FAKE_ROOT=${x#*=}
51                 ;;
52                 subdir=*)
53                         SUBDIR=${x#*=}
54                 ;;
55                 real_init=*)
56                         REAL_INIT=${x#*=}
57                 ;;
58                 init_opts=*)
59                         INIT_OPTS=${x#*=}
60                 ;;
61                 # Livecd options
62                 cdroot)
63                         CDROOT=1
64                 ;;
65                 cdroot=*)
66                         CDROOT=1
67                         CDROOT_DEV=${x#*=}
68                 ;;
69                 cdroot_type=*)
70                         CDROOT_TYPE=${x#*=}
71                 ;;
72                 # Start livecd loop, looptype options
73                 loop=*)
74                         LOOP=${x#*=}
75                 ;;
76                 looptype=*)
77                         LOOPTYPE=${x#*=}
78                 ;;
79                 isoboot=*)
80                         ISOBOOT=${x#*=}
81                 ;;
82                 # Start Volume manager options
83                 dolvm)
84                         USE_LVM_NORMAL=1
85                 ;;
86                 dolvm2)
87                         bad_msg 'Using dolvm2 is deprecated, use dolvm, instead.'
88                         USE_LVM_NORMAL=1
89                 ;;
90                 domdadm)
91                         USE_MDADM=1
92                 ;;
93                 dodmraid)
94                         USE_DMRAID_NORMAL=1
95                 ;;
96                 dodmraid=*)
97                         DMRAID_OPTS=${x#*=}
98                         USE_DMRAID_NORMAL=1
99                 ;;
100                 dozfs*)
101                         USE_ZFS=1
102
103                         if [ "${x#*=}" = 'force' ]
104                         then
105                                 ZPOOL_FORCE=-f
106                         fi
107                 ;;
108                 # Debug Options
109                 debug)
110                         DEBUG='yes'
111                 ;;
112                 # Scan delay options
113                 scandelay=*)
114                         SDELAY=${x#*=}
115                 ;;
116                 scandelay)
117                         SDELAY=3
118                 ;;
119                 # Module no-loads
120                 doload=*)
121                         MDOLIST=${x#*=}
122                         MDOLIST=$(echo ${MDOLIST} | sed -e 's/,/ /g')
123                 ;;
124                 nodetect)
125                         NODETECT=1
126                 ;;
127                 noload=*)
128                         MLIST=${x#*=}
129                         MLIST="$(echo ${MLIST} | sed -e 's/,/ /g')"
130                         export MLIST
131                 ;;
132                 # Redirect output to a specific tty
133                 CONSOLE=*|console=*)
134                         CONSOLE=${x#*=}
135                         CONSOLE=$(basename ${CONSOLE})
136 #                       exec >${CONSOLE} <${CONSOLE} 2>&1
137                 ;;
138                 # /dev/md
139                 lvmraid=*)
140                         RAID_DEVICES="${x#*=}"
141                         RAID_DEVICES="$(echo ${RAID_DEVICES} | sed -e 's/,/ /g')"
142                         USE_LVM_NORMAL=1
143                 ;;
144                 part=*)
145                         MDPART=${x#*=}
146                 ;;
147                 # NFS
148                 ip=*)
149                         IP=${x#*=}
150                 ;;
151                 nfsroot=*)
152                         NFSROOT=${x#*=}
153                 ;;
154                 # iSCSI
155                 iscsi_initiatorname=*)
156                         ISCSI_INITIATORNAME=${x#*=}
157                 ;;
158                 iscsi_target=*)
159                         ISCSI_TARGET=${x#*=}
160                 ;;
161                 iscsi_tgpt=*)
162                         ISCSI_TGPT=${x#*=}
163                 ;;
164                 iscsi_address=*)
165                         ISCSI_ADDRESS=${x#*=}
166                 ;;
167                 iscsi_port=*)
168                         ISCSI_PORT=${x#*=}
169                 ;;
170                 iscsi_username=*)
171                         ISCSI_USERNAME=${x#*=}
172                 ;;
173                 iscsi_password=*)
174                         ISCSI_PASSWORD=${x#*=}
175                 ;;
176                 iscsi_username_in=*)
177                         ISCSI_USERNAME_IN=${x#*=}
178                 ;;
179                 iscsi_password_in=*)
180                         ISCSI_PASSWORD_IN=${x#*=}
181                 ;;
182                 iscsi_debug=*)
183                         ISCSI_DEBUG=${x#*=}
184                 ;;
185                 iscsi_noibft)
186                         ISCSI_NOIBFT=1
187                 ;;
188                 # Crypto
189                 crypt_root=*)
190                         CRYPT_ROOT=${x#*=}
191                 ;;
192                 crypt_swap=*)
193                         CRYPT_SWAP=${x#*=}
194                 ;;
195                 root_key=*)
196                         CRYPT_ROOT_KEY=${x#*=}
197                 ;;
198                 root_keydev=*)
199                         CRYPT_ROOT_KEYDEV=${x#*=}
200                 ;;
201                 root_trim=*)
202                         CRYPT_ROOT_TRIM=${x#*=}
203                 ;;
204
205                 swap_key=*)
206                         CRYPT_SWAP_KEY=${x#*=}
207                 ;;
208                 swap_keydev=*)
209                         CRYPT_SWAP_KEYDEV=${x#*=}
210                 ;;
211                 real_resume=*|resume=*)
212                         REAL_RESUME=${x#*=}
213                 ;;
214                 noresume)
215                         NORESUME=1
216                 ;;
217                 crypt_silent)
218                         CRYPT_SILENT=1
219                 ;;
220                 real_rootflags=*)
221                         REAL_ROOTFLAGS=${x#*=}
222                 ;;
223                 rootfstype=*)
224                         ROOTFSTYPE=${x#*=}
225                 ;;
226                 keymap=*)
227                         keymap=${x#*=}
228                 ;;
229                 aufs)
230                         USE_AUFS_NORMAL=1
231                 ;;
232                 unionfs)
233                         if [ ! -x /sbin/unionfs ]
234                         then
235                                 USE_UNIONFS_NORMAL=0
236                                 bad_msg 'unionfs binary not found: aborting use of unionfs!'
237                         else
238                                 USE_UNIONFS_NORMAL=1
239                         fi
240                         ;;
241                 nounionfs)
242                         USE_UNIONFS_NORMAL=0
243                         ;;
244                 *=*)
245                         case "${x%%=*}" in
246                         *.*)
247                                 echo "${x#*.}" >> "/etc/module_options/${x%%.*}.conf"
248                         ;;
249                         esac
250                 ;;
251         esac
252 done
253
254 if [ -z "${REAL_ROOT}" -a \( "${CDROOT}" = '0' \)  -a \( "${FAKE_ROOT}" != "/dev/ram0" \) ]
255 then
256         REAL_ROOT="${FAKE_ROOT}"
257 fi
258
259 # Set variables based on the value of REAL_ROOT
260 case "${REAL_ROOT}" in
261         ZFS=*)
262                 ZFS_POOL=${REAL_ROOT#*=}
263                 ZFS_POOL=${ZFS_POOL%%/*}
264                 USE_ZFS=1
265         ;;
266         ZFS)
267                 USE_ZFS=1
268         ;;
269 esac
270
271 # Verify that it is safe to use ZFS
272 if [ "USE_ZFS" = "1" ]
273 then
274         if [ -x /sbin/zfs -a -x /sbin/zpool ]
275         then
276                 MY_HWOPTS="${MY_HWOPTS} zfs"
277         else
278                 USE_ZFS=0
279
280                 [ -x /sbin/zfs ] || bad_msg '/sbin/zfs not found!'
281                 [ -x /sbin/zpool ] || bad_msg '/sbin/zpool not found!'
282                 bad_msg 'Aborting use of zfs!'
283         fi
284 fi
285
286 splash 'init'
287
288 cmdline_hwopts
289
290 # Mount devfs
291 mount_devfs
292
293 # Mount sysfs
294 mount_sysfs
295
296 # Start mdev
297 if [ "${KV_2_6_OR_GREATER}" ]
298 then
299         good_msg 'Activating mdev'
300
301         # Serialize hotplug events
302         touch /dev/mdev.seq
303
304         # Setup hotplugging for firmware loading
305         echo /sbin/mdev > /proc/sys/kernel/hotplug
306
307         # Setup mdev netlink socket daemon
308         ( cd /sys && mdev -s ) || bad_msg "Failed to receive dynamic updates from mdev"
309 fi
310
311 # Load modules listed in MY_HWOPTS if /lib/modules exists for the running kernel
312 if [ -z "${DO_modules}" ]
313 then
314         good_msg 'Skipping module load; disabled via commandline'
315 elif [ -d "/lib/modules/${KV}" ]
316 then
317         good_msg 'Loading modules'
318         # Load appropriate kernel modules
319         if [ "${NODETECT}" != '1' ]
320         then
321                 for modules in ${MY_HWOPTS}
322                 do
323                         modules_scan ${modules}
324                 done
325         fi
326         # Always eval doload=...
327         modules_load ${MDOLIST}
328 else
329         good_msg 'Skipping module load; no modules in the ramdisk!'
330 fi
331
332 # Apply scan delay if specified
333 sdelay
334
335 # Setup slow USB bits
336 setup_slowusb
337
338 cd /
339
340 # if doslowusb is passed, pause other 10 seconds here, after mdev load
341 [ "${DO_slowusb}" ] && sleep 10
342
343 # Start iSCSI
344 if [ -e /bin/iscsistart ]
345 then
346         startiscsi
347 fi
348
349 # Setup btrfs, see bug 303529
350 setup_btrfsctl
351
352 # Setup md device nodes if they dont exist
353 setup_md_device
354
355 # Scan volumes
356 startVolumes
357
358 setup_keymap
359
360 # Initialize LUKS root device except for livecd's
361 if [ "${CDROOT}" != 1 ]
362 then
363         startLUKS
364         if [ "${NORESUME}" != '1' ] && [ -n "${REAL_RESUME}" ]
365         then
366                 case "${REAL_RESUME}" in
367                         LABEL=*|UUID=*)
368
369                                 RESUME_DEV=""
370                                 retval=1
371
372                                 if [ ${retval} -ne 0 ]; then
373                                         RESUME_DEV=$(findfs "${REAL_RESUME}" 2>/dev/null)
374                                         retval=$?
375                                 fi
376
377                                 if [ ${retval} -ne 0 ]; then
378                                         RESUME_DEV=$(busybox findfs "${REAL_RESUME}" 2>/dev/null)
379                                         retval=$?
380                                 fi
381
382                                 if [ ${retval} -ne 0 ]; then
383                                         RESUME_DEV=$(blkid -o device -l -t "${REAL_RESUME}")
384                                         retval=$?
385                                 fi
386
387                                 if [ ${retval} -eq 0 ] && [ -n "${RESUME_DEV}" ]; then
388                                         good_msg "Detected real_resume=${RESUME_DEV}"
389                                         REAL_RESUME="${RESUME_DEV}"
390                                 fi
391                                 ;;
392                 esac
393
394                 do_resume
395         fi
396 fi
397
398 mkdir -p "${NEW_ROOT}"
399 CHROOT="${NEW_ROOT}"
400
401 # Run debug shell if requested
402 rundebugshell
403
404 if [ "${CDROOT}" = '1' ]
405 then
406         good_msg "Making tmpfs for ${NEW_ROOT}"
407         mount -n -t tmpfs tmpfs "${NEW_ROOT}"
408
409         for i in dev mnt mnt/livecd mnt/key tmp tmp/.initrd mnt/gentoo sys
410         do
411                 mkdir -p "${NEW_ROOT}/${i}"
412                 chmod 755 "${NEW_ROOT}/${i}"
413         done
414         [ ! -d "${CDROOT_PATH}" ] && mkdir -p "${CDROOT_PATH}"
415         [ ! -e "${NEW_ROOT}/dev/null" ] && mknod "${NEW_ROOT}"/dev/null c 1 3
416         [ ! -e "${NEW_ROOT}/dev/console" ] && mknod "${NEW_ROOT}"/dev/console c 5 1
417
418         # For SGI LiveCDs ...
419         if [ "${LOOPTYPE}" = "sgimips" ]
420         then
421                 [ ! -e "${NEW_ROOT}/dev/sr0" ] && mknod "${NEW_ROOT}/dev/sr0" b 11 0
422                 [ ! -e "${NEW_ROOT}/dev/loop0" ] && mknod "${NEW_ROOT}/dev/loop0" b 7 0
423         fi
424
425         # Required for splash to work.  Not an issue with the initrd as this
426         # device isn't created there and is not needed.
427         if [ -e /dev/tty1 ]
428         then
429                 [ ! -e "${NEW_ROOT}/dev/tty1" ] && mknod "${NEW_ROOT}/dev/tty1" c 4 1
430         fi
431
432         if [ "${REAL_ROOT}" != "/dev/nfs" ] && [ "${LOOPTYPE}" != "sgimips" ]
433         then
434                 bootstrapCD
435         fi
436
437         if [ "${REAL_ROOT}" = '' ]
438         then
439                 echo -n -e "${WARN}>>${NORMAL}${BOLD} No bootable medium found. Waiting for new devices"
440
441                 COUNTER=0
442                 while [ ${COUNTER} -lt 3 ]; do
443                         sleep 3
444                         echo -n '.'
445                         let COUNTER=${COUNTER}+1
446                 done
447
448                 sleep 1
449                 echo -e "${NORMAL}"
450                 bootstrapCD
451         fi
452
453         if [ "${REAL_ROOT}" = '' ]
454         then
455                 # Undo stuff
456                 umount  "${NEW_ROOT}/dev" 2>/dev/null
457                 umount  "${NEW_ROOT}/sys" 2>/dev/null
458                 umount /sys 2>/dev/null
459
460                 umount  "${NEW_ROOT}"
461                 rm -rf  "${NEW_ROOT}/*"
462
463                 bad_msg 'Could not find CD to boot, something else needed!'
464                 CDROOT=0
465         fi
466 fi
467
468 # Determine root device
469 good_msg 'Determining root device...'
470 while true
471 do
472         while [ "${got_good_root}" != '1' ]
473         do
474                 case "${REAL_ROOT}" in
475                         LABEL=*|UUID=*)
476
477                                 ROOT_DEV=""
478                                 retval=1
479
480                                 if [ ${retval} -ne 0 ]; then
481                                         ROOT_DEV=$(findfs "${REAL_ROOT}" 2>/dev/null)
482                                         retval=$?
483                                 fi
484
485                                 if [ ${retval} -ne 0 ]; then
486                                         ROOT_DEV=$(busybox findfs "${REAL_ROOT}" 2>/dev/null)
487                                         retval=$?
488                                 fi
489
490                                 if [ ${retval} -ne 0 ]; then
491                                         ROOT_DEV=$(blkid -o device -l -t "${REAL_ROOT}")
492                                         retval=$?
493                                 fi
494
495                                 if [ ${retval} -eq 0 ] && [ -n "${ROOT_DEV}" ]; then
496                                         good_msg "Detected real_root=${ROOT_DEV}"
497                                         REAL_ROOT="${ROOT_DEV}"
498                                 else
499                                         prompt_user "REAL_ROOT" "root block device"
500                                         got_good_root=0
501                                         continue
502                                 fi
503                                 ;;
504                         ZFS*)
505                                 if [ "${USE_ZFS}" = '0' ]; then
506                                         prompt_user "REAL_ROOT" "root block device"
507                                         continue
508                                 fi
509                                         
510                                 ROOT_DEV="${REAL_ROOT#*=}"
511                                 if [ "${ROOT_DEV}" != 'ZFS' ] 
512                                 then
513                                         if [ "$(zfs get type -o value -H ${ROOT_DEV})" = 'filesystem' ]
514                                         then
515                                                 got_good_root=1;
516                                                 REAL_ROOT=${ROOT_DEV}
517                                                 ROOTFSTYPE=zfs
518                                         else
519                                                 bad_msg "${ROOT_DEV} is not a filesystem"
520                                                 prompt_user "REAL_ROOT" "root block device"
521                                                 got_good_root=0
522                                                 continue
523                                         fi
524                                 else
525                                         BOOTFS=$(/sbin/zpool list -H -o bootfs)
526                                         if [ "${BOOTFS}" != '-' ]
527                                         then
528
529                                                 for i in ${BOOTFS}
530                                                 do
531
532                                                         zfs get type ${i} > /dev/null
533                                                         retval=$?
534
535                                                         if [ ${retval} -eq 0 ]; then
536                                                                 got_good_root=1
537                                                                 REAL_ROOT=${i}
538                                                                 ROOTFSTYPE=zfs
539                                                                 break
540                                                         fi      
541                                                 
542                                                 done;
543
544                                         else
545                                                 got_good_root=0
546                                         fi
547
548                                 fi
549
550                                 if [ ${got_good_root} -ne 1 ]; then
551                                         prompt_user "REAL_ROOT" "root block device"
552                                         got_good_root=0
553                                 fi
554                                         
555                                 continue
556                                 ;;
557                 esac
558
559                 if [ "${REAL_ROOT}" = '' ]
560                 then
561                         # No REAL_ROOT determined/specified. Prompt user for root block device.
562                         prompt_user "REAL_ROOT" "root block device"
563                         got_good_root=0
564
565                 # Check for a block device or /dev/nfs
566                 elif [ -b "${REAL_ROOT}" ] || [ "${REAL_ROOT}" = "/dev/nfs" ]
567                 then
568                         got_good_root=1
569
570                 else
571                         bad_msg "Block device ${REAL_ROOT} is not a valid root device..."
572                         REAL_ROOT=""
573                         got_good_root=0
574                 fi
575         done
576
577
578         if [ "${CDROOT}" = 1 -a "${got_good_root}" = '1' -a "${REAL_ROOT}" != "/dev/nfs" ]
579         then
580                 # CD already mounted; no further checks necessary
581                 break
582         elif [ "${LOOPTYPE}" = "sgimips" ]
583         then
584                 # sgimips mounts the livecd root partition directly
585                 # there is no isofs filesystem to worry about
586                 break
587         else
588                 good_msg "Mounting $REAL_ROOT as root..."
589
590                 if [ "${ROOTFSTYPE}" = 'zfs' ]
591                 then
592                         if [ "zfs get -H -o value mountpoint ${REAL_ROOT}" = 'legacy' ]
593                         then
594                                 MOUNT_STATE=rw
595                         else
596                                 MOUNT_STATE=rw,zfsutil
597                         fi
598                 else
599                         MOUNT_STATE=ro
600                 fi
601
602                 # Try to mount the device as ${NEW_ROOT}
603                 if [ "${REAL_ROOT}" = '/dev/nfs' ]; then
604                         findnfsmount
605                 else
606                         # If $REAL_ROOT is a symlink
607                         # Resolve it like util-linux mount does
608                         [ -L ${REAL_ROOT} ] && REAL_ROOT=`readlink ${REAL_ROOT}`
609                         # mount ro so fsck doesn't barf later
610                         if [ "${REAL_ROOTFLAGS}" = '' ]; then
611                                 good_msg "Using mount -t ${ROOTFSTYPE} -o ${MOUNT_STATE}"
612                                 mount -t ${ROOTFSTYPE} -o ${MOUNT_STATE} ${REAL_ROOT} ${NEW_ROOT}
613                         else
614                                 good_msg "Using mount -t ${ROOTFSTYPE} -o ${MOUNT_STATE},${REAL_ROOTFLAGS}"
615                                 mount -t ${ROOTFSTYPE} -o ${MOUNT_STATE},${REAL_ROOTFLAGS} ${REAL_ROOT} ${NEW_ROOT}
616                         fi
617                 fi
618
619                 # If mount is successful break out of the loop
620                 # else not a good root and start over.
621                 if [ "$?" = '0' ]
622                 then
623                         if [ -d ${NEW_ROOT}/dev -a -x ${NEW_ROOT}/sbin/init ] || [ "${REAL_ROOT}" = "/dev/nfs" ]
624                         then
625                                 break
626                         else
627                                 bad_msg "The filesystem mounted at ${REAL_ROOT} does not appear to be a valid /, try again"
628                                 got_good_root=0
629                                 REAL_ROOT=''
630                         fi
631                 else
632                         bad_msg "Could not mount specified ROOT, try again"
633                         got_good_root=0
634                         REAL_ROOT=''
635                 fi
636         fi
637 done
638 # End determine root device
639
640 #verbose_kmsg
641
642 # If CD root is set determine the looptype to boot
643 if [ "${CDROOT}" = '1' ]
644 then
645         good_msg 'Determining looptype ...'
646         cd "${NEW_ROOT}"
647
648         # Find loop and looptype
649         [ -z "${LOOP}" ] && find_loop
650         [ -z "${LOOPTYPE}" ] && find_looptype
651
652         cache_cd_contents
653
654         # If encrypted, find key and mount, otherwise mount as usual
655         if [ -n "${CRYPT_ROOT}" ]
656         then
657                 CRYPT_ROOT_KEY="$(head -n 1 "${CDROOT_PATH}"/livecd)"
658                 CRYPT_ROOT='/dev/loop0'
659                 good_msg 'You booted an encrypted livecd' "${CRYPT_SILENT}"
660
661                 losetup /dev/loop0 "${CDROOT_PATH}/${LOOPEXT}${LOOP}"
662                 test_success 'Preparing loop filesystem'
663
664                 startLUKS
665
666                 case ${LOOPTYPE} in
667                         normal)
668                                 MOUNTTYPE="ext2"
669                                 ;;
670                         *)
671                                 MOUNTTYPE="${LOOPTYPE}"
672                                 ;;
673                 esac
674                 mount -t "${MOUNTTYPE}" -o ro /dev/mapper/root "${NEW_ROOT}/mnt/livecd"
675                 test_success 'Mount filesystem'
676                 FS_LOCATION='mnt/livecd'
677         # Setup the loopback mounts, if unencrypted
678         else
679                 if [ "${LOOPTYPE}" = 'normal' ]
680                 then
681                         good_msg 'Mounting loop filesystem'
682                         mount -t ext2 -o loop,ro "${CDROOT_PATH}/${LOOPEXT}${LOOP}" "${NEW_ROOT}/mnt/livecd"
683                         test_success 'Mount filesystem'
684                         FS_LOCATION='mnt/livecd'
685                 elif [ "${LOOPTYPE}" = 'squashfs' ]
686                 then
687                         if [ "${USE_AUFS_NORMAL}" != '1' ]; then
688                                 good_msg 'Mounting squashfs filesystem'
689                                 _CACHED_SQUASHFS_PATH="${NEW_ROOT}/mnt/${LOOP}"
690                                 _squashfs_path="${CDROOT_PATH}/${LOOPEXT}${LOOP}"  # Default to uncached
691                                 # Upgrade to cached version if possible
692                                 [ "${DO_cache}" -a -f "${_CACHED_SQUASHFS_PATH}" ] \
693                                                 && _squashfs_path=${_CACHED_SQUASHFS_PATH}
694                                 mount -t squashfs -o loop,ro "${_squashfs_path}" "${NEW_ROOT}/mnt/livecd" || {
695                                         bad_msg "Squashfs filesystem could not be mounted, dropping into shell."
696                                         if [ -e /proc/filesystems ]; then
697                                                 fgrep -q squashfs /proc/filesystems || \
698                                                         bad_msg "HINT: Your kernel does not know filesystem \"squashfs\"."
699                                         fi
700                                         do_rundebugshell
701                                 }
702                         else
703                                 good_msg 'Mounting squashfs & aufs filesystems'
704                                 setup_squashfs_aufs
705                                 test_success 'Mount filesystem'
706                         fi
707                         FS_LOCATION='mnt/livecd'
708                 elif [ "${LOOPTYPE}" = 'gcloop' ]
709                 then
710                         good_msg 'Mounting gcloop filesystem'
711                         echo ' ' | losetup -E 19 -e ucl-0 -p0 "${NEW_ROOT}/dev/loop0" "${CDROOT_PATH}/${LOOPEXT}${LOOP}"
712                         test_success 'losetup the loop device'
713
714                         mount -t ext2 -o ro "${NEW_ROOT}/dev/loop0" "${NEW_ROOT}/mnt/livecd"
715                         test_success 'Mount the losetup loop device'
716                         FS_LOCATION='mnt/livecd'
717                 elif [ "${LOOPTYPE}" = 'zisofs' ]
718                 then
719                         FS_LOCATION="${CDROOT_PATH/\/}/${LOOPEXT}${LOOP}"
720                 elif [ "${LOOPTYPE}" = 'noloop' ]
721                 then
722                         FS_LOCATION="${CDROOT_PATH/\/}"
723                 elif [ "${LOOPTYPE}" = 'sgimips' ]
724                 then
725                         # getdvhoff finds the starting offset (in bytes) of the squashfs
726                         # partition on the cdrom and returns this offset for losetup
727                         #
728                         # All currently supported SGI Systems use SCSI CD-ROMs, so
729                         # so we know that the CD-ROM is usually going to be /dev/sr0.
730                         #
731                         # We use the value given to losetup to set /dev/loop0 to point
732                         # to the liveCD root partition, and then mount /dev/loop0 as
733                         # the LiveCD rootfs
734                         good_msg 'Locating the SGI LiveCD Root Partition'
735                         echo ' ' | \
736                                 losetup -o $(getdvhoff "${NEW_ROOT}${REAL_ROOT}" 0) \
737                                         "${NEW_ROOT}${CDROOT_DEV}" \
738                                         "${NEW_ROOT}${REAL_ROOT}"
739                         test_success 'losetup /dev/sr0 /dev/loop0'
740
741                         good_msg 'Mounting the Root Partition'
742                         mount -t squashfs -o ro "${NEW_ROOT}${CDROOT_DEV}" "${NEW_ROOT}/mnt/livecd"
743                         test_success 'mount /dev/loop0 /'
744                         FS_LOCATION='mnt/livecd'
745                 fi
746         fi
747
748
749
750         # Unpacking additional packages from NFS mount
751         # This is useful for adding kernel modules to /lib
752         # We do this now, so that additional packages can add whereever they want.
753         if [ "${REAL_ROOT}" = '/dev/nfs' ]
754         then
755                 if [ -e "${CDROOT_PATH}/add" ]
756                 then
757                                 for targz in $(ls ${CDROOT_PATH}/add/*.tar.gz)
758                                 do
759                                         tarname=$(basename ${targz})
760                                         good_msg "Adding additional package ${tarname}"
761                                         (cd ${NEW_ROOT} ; /bin/tar -xzf ${targz})
762                                 done
763                 fi
764         fi
765
766
767         if [ "${USE_UNIONFS_NORMAL}" = '1' ]
768         then
769                 setup_unionfs ${NEW_ROOT} /${FS_LOCATION}
770                 CHROOT=/union
771         elif [ "${USE_AUFS_NORMAL}" != '1' ]; then
772
773         good_msg "Copying read-write image contents to tmpfs"
774         # Copy over stuff that should be writable
775         (cd "${NEW_ROOT}/${FS_LOCATION}"; cp -a ${ROOT_TREES} "${NEW_ROOT}") || {
776                 bad_msg "Copying failed, dropping into a shell."
777                 do_rundebugshell
778         }
779
780         # Now we do the links.
781         for x in ${ROOT_LINKS}
782         do
783                 if [ -L "${NEW_ROOT}/${FS_LOCATION}/${x}" ]
784                 then
785                         ln -s "$(readlink ${NEW_ROOT}/${FS_LOCATION}/${x})" "${x}" 2>/dev/null
786                 else
787                         # List all subdirectories of x
788                         find "${NEW_ROOT}/${FS_LOCATION}/${x}" -type d 2>/dev/null | while read directory
789                         do
790                                 # Strip the prefix of the FS_LOCATION
791                                 directory="${directory#${NEW_ROOT}/${FS_LOCATION}/}"
792
793                                 # Skip this directory if we already linked a parent directory
794                                 if [ "${current_parent}" != '' ]; then
795                                         var=$(echo "${directory}" | grep "^${current_parent}")
796                                         if [ "${var}" != '' ]; then
797                                                 continue
798                                         fi
799                                 fi
800                                 # Test if the directory exists already
801                                 if [ -e "/${NEW_ROOT}/${directory}" ]
802                                 then
803                                         # It does exist, link all the individual files
804                                         for file in $(ls /${NEW_ROOT}/${FS_LOCATION}/${directory})
805                                         do
806                                         if [ ! -d "/${NEW_ROOT}/${FS_LOCATION}/${directory}/${file}" ] && [ ! -e "${NEW_ROOT}/${directory}/${file}" ]; then
807                                                         ln -s "/${FS_LOCATION}/${directory}/${file}" "${directory}/${file}" 2> /dev/null
808                                                 fi
809                                         done
810                                 else
811                                         # It does not exist, make a link to the livecd
812                                         ln -s "/${FS_LOCATION}/${directory}" "${directory}" 2>/dev/null
813                                         current_parent="${directory}"
814                                 fi
815                         done
816                 fi
817         done
818
819         mkdir initramfs proc tmp sys run 2>/dev/null
820         chmod 1777 tmp
821
822         fi
823
824         #UML=$(cat /proc/cpuinfo|grep UML|sed -e 's|model name.*: ||')
825         #if [ "${UML}" = 'UML' ]
826         #then
827         #       # UML Fixes
828         #       good_msg 'Updating for uml system'
829         #fi
830
831         # Let Init scripts know that we booted from CD
832         export CDBOOT
833         CDBOOT=1
834 else
835         if [ "${USE_UNIONFS_NORMAL}" = '1' ]
836         then
837                 mkdir /union_changes
838                 mount -t tmpfs tmpfs /union_changes
839                 setup_unionfs /union_changes ${NEW_ROOT}
840                 mkdir -p ${UNION}/tmp/.initrd
841         fi
842 fi
843
844 # Mount the additional things as required by udev & systemd
845 if [ -f ${NEW_ROOT}/etc/initramfs.mounts ]; then
846         fslist=$(get_mounts_list)
847 else
848         fslist="/usr" 
849 fi
850
851 for fs in $fslist; do
852         dev=$(get_mount_device $fs)
853         [ -z "${dev}" ] && continue
854         # Resolve it like util-linux mount does
855         [ -L ${dev} ] && dev=`readlink ${dev}`
856         # In this case, it's probably part of the filesystem
857         # and not a mountpoint
858         [ -z "$dev" ] && continue
859         fstype=$(get_mount_fstype $fs)
860         if get_mount_options $fs | fgrep -q bind ; then
861                 opts='bind'
862                 dev=${NEW_ROOT}${dev}
863         else
864                 # ro must be trailing, and the options will always contain at least 'defaults'
865                 opts="$(get_mount_options $fs | strip_mount_options),ro"
866         fi
867         mnt=${NEW_ROOT}${fs}
868         cmd="mount -t $fstype -o $opts $dev $mnt"
869         good_msg "Mounting $dev as ${fs}: $cmd"
870         if ! $cmd; then
871                 bad_msg "Unable to mount $dev for $fs"
872         fi
873 done
874
875 # Execute script on the cdrom just before boot to update things if necessary
876 cdupdate
877
878 if [ "${SUBDIR}" != '' -a -e "${CHROOT}/${SUBDIR}" ]
879 then
880         good_msg "Entering ${SUBDIR} to boot"
881         CHROOT="${CHROOT}/${SUBDIR}"
882 fi
883
884 verbose_kmsg
885
886 echo -ne "${GOOD}>>${NORMAL}${BOLD} Booting (initramfs)${NORMAL}"
887
888 cd "${CHROOT}"
889 mkdir "${CHROOT}/proc" "${CHROOT}/sys" "${CHROOT}/run" 2>/dev/null
890 echo -ne "${BOLD}.${NORMAL}"
891
892 # If devtmpfs is mounted, try move it to the new root
893 # If that fails, try to unmount all possible mounts of devtmpfs as stuff breaks otherwise
894 for fs in /dev /sys /proc
895 do
896         if grep -qs "$fs" /proc/mounts
897         then
898                 if ! mount --move $fs "${CHROOT}"$fs
899                 then
900                         umount $fs || echo '*: Failed to move and unmount the ramdisk $fs!'
901                 fi
902         fi
903 done
904
905 if [ ! -e "${CHROOT}/dev/console" ] || [ ! -e "${CHROOT}/dev/null" ]
906 then
907         echo -ne "${BAD}>>${NORMAL}${BOLD} ERROR: your real /dev is missing files required to boot (console and null)${NORMAL}"
908 elif [ -e /etc/initrd.splash -a ! -e "${CHROOT}/dev/tty1" ]
909 then
910         echo -ne "${BAD}>>${NORMAL}${BOLD} ERROR: your real /dev is missing tty1, which is required for splash${NORMAL}"
911 fi
912
913 echo -e "${BOLD}.${NORMAL}"
914
915 exec /sbin/switch_root -c "/dev/console" "${CHROOT}" "${REAL_INIT:-/sbin/init}" "${INIT_OPTS}"
916
917 # If we get here, something bad has happened
918 splash 'verbose'
919
920 echo "A fatal error has probably occured since ${REAL_INIT:-/sbin/init} did not"
921 echo "boot correctly. Trying to open a shell..."
922 echo
923 exec /bin/bash
924 exec /bin/sh
925 exec /bin/ash
926 exec /bin/dash
927 exec sh