91a3396b2e703fb879041465765d949da7cfe9b3
[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                 ## for "module.param[=val]" we creating
245                 ## /etc/modprobe.d/module.param.conf: "options module param[=val]"
246                 ## /etc/cmdline/module.param:"param[=val]"
247                 ## first for CONFIG_MODPROBE_SMALL=n, next for script modprobe
248                 *=*)
249                         case "${x%%=*}" in
250                         *.*)
251                                 y="${x%%[=/]*}"
252                                 echo "options ${x%%.*} ${x#*.}" >"/etc/modprobe.d/$y.conf"
253                                 echo "${x#*.}" >"/etc/cmdline/$y"
254                         ;;
255                         esac
256                 ;;
257                 *.*)
258                         echo "options ${x%%.*} ${x#*.}" >"/etc/modprobe.d/${x%%/*}.conf"
259                         echo "${x#*.}" >"/etc/cmdline/${x%%/*}"
260                 ;;
261         esac
262 done
263
264 if [ -z "${REAL_ROOT}" -a \( "${CDROOT}" = '0' \)  -a \( "${FAKE_ROOT}" != "/dev/ram0" \) ]
265 then
266         REAL_ROOT="${FAKE_ROOT}"
267 fi
268
269 # Set variables based on the value of REAL_ROOT
270 case "${REAL_ROOT}" in
271         ZFS=*)
272                 ZFS_POOL=${REAL_ROOT#*=}
273                 ZFS_POOL=${ZFS_POOL%%/*}
274                 USE_ZFS=1
275         ;;
276         ZFS)
277                 USE_ZFS=1
278         ;;
279 esac
280
281 # Verify that it is safe to use ZFS
282 if [ "USE_ZFS" = "1" ]
283 then
284         if [ -x /sbin/zfs -a -x /sbin/zpool ]
285         then
286                 MY_HWOPTS="${MY_HWOPTS} zfs"
287         else
288                 USE_ZFS=0
289
290                 [ -x /sbin/zfs ] || bad_msg '/sbin/zfs not found!'
291                 [ -x /sbin/zpool ] || bad_msg '/sbin/zpool not found!'
292                 bad_msg 'Aborting use of zfs!'
293         fi
294 fi
295
296 splash 'init'
297
298 cmdline_hwopts
299
300 # Mount devfs
301 mount_devfs
302
303 # Mount sysfs
304 mount_sysfs
305
306 # Start mdev
307 if [ "${KV_2_6_OR_GREATER}" ]
308 then
309         good_msg 'Activating mdev'
310
311         # Serialize hotplug events
312         touch /dev/mdev.seq
313
314         # Setup hotplugging for firmware loading
315         echo /sbin/mdev > /proc/sys/kernel/hotplug
316
317         # Setup mdev netlink socket daemon
318         ( cd /sys && mdev -s ) || bad_msg "Failed to receive dynamic updates from mdev"
319 fi
320
321 # Load modules listed in MY_HWOPTS if /lib/modules exists for the running kernel
322 if [ -z "${DO_modules}" ]
323 then
324         good_msg 'Skipping module load; disabled via commandline'
325 elif [ -d "/lib/modules/${KV}" ]
326 then
327         good_msg 'Loading modules'
328         # Load appropriate kernel modules
329         if [ "${NODETECT}" != '1' ]
330         then
331                 for modules in ${MY_HWOPTS}
332                 do
333                         modules_scan ${modules}
334                 done
335         fi
336         # Always eval doload=...
337         modules_load ${MDOLIST}
338 else
339         good_msg 'Skipping module load; no modules in the ramdisk!'
340 fi
341
342 # Apply scan delay if specified
343 sdelay
344
345 # Setup slow USB bits
346 setup_slowusb
347
348 cd /
349
350 # if doslowusb is passed, pause other 10 seconds here, after mdev load
351 [ "${DO_slowusb}" ] && sleep 10
352
353 # Start iSCSI
354 if [ -e /bin/iscsistart ]
355 then
356         startiscsi
357 fi
358
359 # Setup btrfs, see bug 303529
360 setup_btrfsctl
361
362 # Setup md device nodes if they dont exist
363 setup_md_device
364
365 # Scan volumes
366 startVolumes
367
368 setup_keymap
369
370 # Initialize LUKS root device except for livecd's
371 if [ "${CDROOT}" != 1 ]
372 then
373         startLUKS
374         if [ "${NORESUME}" != '1' ] && [ -n "${REAL_RESUME}" ]
375         then
376                 case "${REAL_RESUME}" in
377                         LABEL=*|UUID=*)
378
379                                 RESUME_DEV=""
380                                 retval=1
381
382                                 if [ ${retval} -ne 0 ]; then
383                                         RESUME_DEV=$(findfs "${REAL_RESUME}" 2>/dev/null)
384                                         retval=$?
385                                 fi
386
387                                 if [ ${retval} -ne 0 ]; then
388                                         RESUME_DEV=$(busybox findfs "${REAL_RESUME}" 2>/dev/null)
389                                         retval=$?
390                                 fi
391
392                                 if [ ${retval} -ne 0 ]; then
393                                         RESUME_DEV=$(blkid -o device -l -t "${REAL_RESUME}")
394                                         retval=$?
395                                 fi
396
397                                 if [ ${retval} -eq 0 ] && [ -n "${RESUME_DEV}" ]; then
398                                         good_msg "Detected real_resume=${RESUME_DEV}"
399                                         REAL_RESUME="${RESUME_DEV}"
400                                 fi
401                                 ;;
402                 esac
403
404                 do_resume
405         fi
406 fi
407
408 mkdir -p "${NEW_ROOT}"
409 CHROOT="${NEW_ROOT}"
410
411 # Run debug shell if requested
412 rundebugshell
413
414 if [ "${CDROOT}" = '1' ]
415 then
416         good_msg "Making tmpfs for ${NEW_ROOT}"
417         mount -n -t tmpfs tmpfs "${NEW_ROOT}"
418
419         for i in dev mnt mnt/livecd mnt/key tmp tmp/.initrd mnt/gentoo sys
420         do
421                 mkdir -p "${NEW_ROOT}/${i}"
422                 chmod 755 "${NEW_ROOT}/${i}"
423         done
424         [ ! -d "${CDROOT_PATH}" ] && mkdir -p "${CDROOT_PATH}"
425         [ ! -e "${NEW_ROOT}/dev/null" ] && mknod "${NEW_ROOT}"/dev/null c 1 3
426         [ ! -e "${NEW_ROOT}/dev/console" ] && mknod "${NEW_ROOT}"/dev/console c 5 1
427
428         # For SGI LiveCDs ...
429         if [ "${LOOPTYPE}" = "sgimips" ]
430         then
431                 [ ! -e "${NEW_ROOT}/dev/sr0" ] && mknod "${NEW_ROOT}/dev/sr0" b 11 0
432                 [ ! -e "${NEW_ROOT}/dev/loop0" ] && mknod "${NEW_ROOT}/dev/loop0" b 7 0
433         fi
434
435         # Required for splash to work.  Not an issue with the initrd as this
436         # device isn't created there and is not needed.
437         if [ -e /dev/tty1 ]
438         then
439                 [ ! -e "${NEW_ROOT}/dev/tty1" ] && mknod "${NEW_ROOT}/dev/tty1" c 4 1
440         fi
441
442         if [ "${REAL_ROOT}" != "/dev/nfs" ] && [ "${LOOPTYPE}" != "sgimips" ]
443         then
444                 bootstrapCD
445         fi
446
447         if [ "${REAL_ROOT}" = '' ]
448         then
449                 echo -n -e "${WARN}>>${NORMAL}${BOLD} No bootable medium found. Waiting for new devices"
450
451                 COUNTER=0
452                 while [ ${COUNTER} -lt 3 ]; do
453                         sleep 3
454                         echo -n '.'
455                         let COUNTER=${COUNTER}+1
456                 done
457
458                 sleep 1
459                 echo -e "${NORMAL}"
460                 bootstrapCD
461         fi
462
463         if [ "${REAL_ROOT}" = '' ]
464         then
465                 # Undo stuff
466                 umount  "${NEW_ROOT}/dev" 2>/dev/null
467                 umount  "${NEW_ROOT}/sys" 2>/dev/null
468                 umount /sys 2>/dev/null
469
470                 umount  "${NEW_ROOT}"
471                 rm -rf  "${NEW_ROOT}/*"
472
473                 bad_msg 'Could not find CD to boot, something else needed!'
474                 CDROOT=0
475         fi
476 fi
477
478 # Determine root device
479 good_msg 'Determining root device...'
480 while true
481 do
482         while [ "${got_good_root}" != '1' ]
483         do
484                 case "${REAL_ROOT}" in
485                         LABEL=*|UUID=*)
486
487                                 ROOT_DEV=""
488                                 retval=1
489
490                                 if [ ${retval} -ne 0 ]; then
491                                         ROOT_DEV=$(findfs "${REAL_ROOT}" 2>/dev/null)
492                                         retval=$?
493                                 fi
494
495                                 if [ ${retval} -ne 0 ]; then
496                                         ROOT_DEV=$(busybox findfs "${REAL_ROOT}" 2>/dev/null)
497                                         retval=$?
498                                 fi
499
500                                 if [ ${retval} -ne 0 ]; then
501                                         ROOT_DEV=$(blkid -o device -l -t "${REAL_ROOT}")
502                                         retval=$?
503                                 fi
504
505                                 if [ ${retval} -eq 0 ] && [ -n "${ROOT_DEV}" ]; then
506                                         good_msg "Detected real_root=${ROOT_DEV}"
507                                         REAL_ROOT="${ROOT_DEV}"
508                                 else
509                                         prompt_user "REAL_ROOT" "root block device"
510                                         got_good_root=0
511                                         continue
512                                 fi
513                                 ;;
514                         ZFS*)
515                                 if [ "${USE_ZFS}" = '0' ]; then
516                                         prompt_user "REAL_ROOT" "root block device"
517                                         continue
518                                 fi
519                                         
520                                 ROOT_DEV="${REAL_ROOT#*=}"
521                                 if [ "${ROOT_DEV}" != 'ZFS' ] 
522                                 then
523                                         if [ "$(zfs get type -o value -H ${ROOT_DEV})" = 'filesystem' ]
524                                         then
525                                                 got_good_root=1;
526                                                 REAL_ROOT=${ROOT_DEV}
527                                                 ROOTFSTYPE=zfs
528                                         else
529                                                 bad_msg "${ROOT_DEV} is not a filesystem"
530                                                 prompt_user "REAL_ROOT" "root block device"
531                                                 got_good_root=0
532                                                 continue
533                                         fi
534                                 else
535                                         BOOTFS=$(/sbin/zpool list -H -o bootfs)
536                                         if [ "${BOOTFS}" != '-' ]
537                                         then
538
539                                                 for i in ${BOOTFS}
540                                                 do
541
542                                                         zfs get type ${i} > /dev/null
543                                                         retval=$?
544
545                                                         if [ ${retval} -eq 0 ]; then
546                                                                 got_good_root=1
547                                                                 REAL_ROOT=${i}
548                                                                 ROOTFSTYPE=zfs
549                                                                 break
550                                                         fi      
551                                                 
552                                                 done;
553
554                                         else
555                                                 got_good_root=0
556                                         fi
557
558                                 fi
559
560                                 if [ ${got_good_root} -ne 1 ]; then
561                                         prompt_user "REAL_ROOT" "root block device"
562                                         got_good_root=0
563                                 fi
564                                         
565                                 continue
566                                 ;;
567                 esac
568
569                 if [ "${REAL_ROOT}" = '' ]
570                 then
571                         # No REAL_ROOT determined/specified. Prompt user for root block device.
572                         prompt_user "REAL_ROOT" "root block device"
573                         got_good_root=0
574
575                 # Check for a block device or /dev/nfs
576                 elif [ -b "${REAL_ROOT}" ] || [ "${REAL_ROOT}" = "/dev/nfs" ]
577                 then
578                         got_good_root=1
579
580                 else
581                         bad_msg "Block device ${REAL_ROOT} is not a valid root device..."
582                         REAL_ROOT=""
583                         got_good_root=0
584                 fi
585         done
586
587
588         if [ "${CDROOT}" = 1 -a "${got_good_root}" = '1' -a "${REAL_ROOT}" != "/dev/nfs" ]
589         then
590                 # CD already mounted; no further checks necessary
591                 break
592         elif [ "${LOOPTYPE}" = "sgimips" ]
593         then
594                 # sgimips mounts the livecd root partition directly
595                 # there is no isofs filesystem to worry about
596                 break
597         else
598                 good_msg "Mounting $REAL_ROOT as root..."
599
600                 if [ "${ROOTFSTYPE}" = 'zfs' ]
601                 then
602                         if [ "zfs get -H -o value mountpoint ${REAL_ROOT}" = 'legacy' ]
603                         then
604                                 MOUNT_STATE=rw
605                         else
606                                 MOUNT_STATE=rw,zfsutil
607                         fi
608                 else
609                         MOUNT_STATE=ro
610                 fi
611
612                 # Try to mount the device as ${NEW_ROOT}
613                 if [ "${REAL_ROOT}" = '/dev/nfs' ]; then
614                         findnfsmount
615                 else
616                         # If $REAL_ROOT is a symlink
617                         # Resolve it like util-linux mount does
618                         [ -L ${REAL_ROOT} ] && REAL_ROOT=`readlink ${REAL_ROOT}`
619                         # mount ro so fsck doesn't barf later
620                         if [ "${REAL_ROOTFLAGS}" = '' ]; then
621                                 good_msg "Using mount -t ${ROOTFSTYPE} -o ${MOUNT_STATE}"
622                                 mount -t ${ROOTFSTYPE} -o ${MOUNT_STATE} ${REAL_ROOT} ${NEW_ROOT}
623                         else
624                                 good_msg "Using mount -t ${ROOTFSTYPE} -o ${MOUNT_STATE},${REAL_ROOTFLAGS}"
625                                 mount -t ${ROOTFSTYPE} -o ${MOUNT_STATE},${REAL_ROOTFLAGS} ${REAL_ROOT} ${NEW_ROOT}
626                         fi
627                 fi
628
629                 # If mount is successful break out of the loop
630                 # else not a good root and start over.
631                 if [ "$?" = '0' ]
632                 then
633                         if [ -d ${NEW_ROOT}/dev -a -x ${NEW_ROOT}/sbin/init ] || [ "${REAL_ROOT}" = "/dev/nfs" ]
634                         then
635                                 break
636                         else
637                                 bad_msg "The filesystem mounted at ${REAL_ROOT} does not appear to be a valid /, try again"
638                                 got_good_root=0
639                                 REAL_ROOT=''
640                         fi
641                 else
642                         bad_msg "Could not mount specified ROOT, try again"
643                         got_good_root=0
644                         REAL_ROOT=''
645                 fi
646         fi
647 done
648 # End determine root device
649
650 #verbose_kmsg
651
652 # If CD root is set determine the looptype to boot
653 if [ "${CDROOT}" = '1' ]
654 then
655         good_msg 'Determining looptype ...'
656         cd "${NEW_ROOT}"
657
658         # Find loop and looptype
659         [ -z "${LOOP}" ] && find_loop
660         [ -z "${LOOPTYPE}" ] && find_looptype
661
662         cache_cd_contents
663
664         # If encrypted, find key and mount, otherwise mount as usual
665         if [ -n "${CRYPT_ROOT}" ]
666         then
667                 CRYPT_ROOT_KEY="$(head -n 1 "${CDROOT_PATH}"/livecd)"
668                 CRYPT_ROOT='/dev/loop0'
669                 good_msg 'You booted an encrypted livecd' "${CRYPT_SILENT}"
670
671                 losetup /dev/loop0 "${CDROOT_PATH}/${LOOPEXT}${LOOP}"
672                 test_success 'Preparing loop filesystem'
673
674                 startLUKS
675
676                 case ${LOOPTYPE} in
677                         normal)
678                                 MOUNTTYPE="ext2"
679                                 ;;
680                         *)
681                                 MOUNTTYPE="${LOOPTYPE}"
682                                 ;;
683                 esac
684                 mount -t "${MOUNTTYPE}" -o ro /dev/mapper/root "${NEW_ROOT}/mnt/livecd"
685                 test_success 'Mount filesystem'
686                 FS_LOCATION='mnt/livecd'
687         # Setup the loopback mounts, if unencrypted
688         else
689                 if [ "${LOOPTYPE}" = 'normal' ]
690                 then
691                         good_msg 'Mounting loop filesystem'
692                         mount -t ext2 -o loop,ro "${CDROOT_PATH}/${LOOPEXT}${LOOP}" "${NEW_ROOT}/mnt/livecd"
693                         test_success 'Mount filesystem'
694                         FS_LOCATION='mnt/livecd'
695                 elif [ "${LOOPTYPE}" = 'squashfs' ]
696                 then
697                         if [ "${USE_AUFS_NORMAL}" != '1' ]; then
698                                 good_msg 'Mounting squashfs filesystem'
699                                 _CACHED_SQUASHFS_PATH="${NEW_ROOT}/mnt/${LOOP}"
700                                 _squashfs_path="${CDROOT_PATH}/${LOOPEXT}${LOOP}"  # Default to uncached
701                                 # Upgrade to cached version if possible
702                                 [ "${DO_cache}" -a -f "${_CACHED_SQUASHFS_PATH}" ] \
703                                                 && _squashfs_path=${_CACHED_SQUASHFS_PATH}
704                                 mount -t squashfs -o loop,ro "${_squashfs_path}" "${NEW_ROOT}/mnt/livecd" || {
705                                         bad_msg "Squashfs filesystem could not be mounted, dropping into shell."
706                                         if [ -e /proc/filesystems ]; then
707                                                 fgrep -q squashfs /proc/filesystems || \
708                                                         bad_msg "HINT: Your kernel does not know filesystem \"squashfs\"."
709                                         fi
710                                         do_rundebugshell
711                                 }
712                         else
713                                 good_msg 'Mounting squashfs & aufs filesystems'
714                                 setup_squashfs_aufs
715                                 test_success 'Mount filesystem'
716                         fi
717                         FS_LOCATION='mnt/livecd'
718                 elif [ "${LOOPTYPE}" = 'gcloop' ]
719                 then
720                         good_msg 'Mounting gcloop filesystem'
721                         echo ' ' | losetup -E 19 -e ucl-0 -p0 "${NEW_ROOT}/dev/loop0" "${CDROOT_PATH}/${LOOPEXT}${LOOP}"
722                         test_success 'losetup the loop device'
723
724                         mount -t ext2 -o ro "${NEW_ROOT}/dev/loop0" "${NEW_ROOT}/mnt/livecd"
725                         test_success 'Mount the losetup loop device'
726                         FS_LOCATION='mnt/livecd'
727                 elif [ "${LOOPTYPE}" = 'zisofs' ]
728                 then
729                         FS_LOCATION="${CDROOT_PATH/\/}/${LOOPEXT}${LOOP}"
730                 elif [ "${LOOPTYPE}" = 'noloop' ]
731                 then
732                         FS_LOCATION="${CDROOT_PATH/\/}"
733                 elif [ "${LOOPTYPE}" = 'sgimips' ]
734                 then
735                         # getdvhoff finds the starting offset (in bytes) of the squashfs
736                         # partition on the cdrom and returns this offset for losetup
737                         #
738                         # All currently supported SGI Systems use SCSI CD-ROMs, so
739                         # so we know that the CD-ROM is usually going to be /dev/sr0.
740                         #
741                         # We use the value given to losetup to set /dev/loop0 to point
742                         # to the liveCD root partition, and then mount /dev/loop0 as
743                         # the LiveCD rootfs
744                         good_msg 'Locating the SGI LiveCD Root Partition'
745                         echo ' ' | \
746                                 losetup -o $(getdvhoff "${NEW_ROOT}${REAL_ROOT}" 0) \
747                                         "${NEW_ROOT}${CDROOT_DEV}" \
748                                         "${NEW_ROOT}${REAL_ROOT}"
749                         test_success 'losetup /dev/sr0 /dev/loop0'
750
751                         good_msg 'Mounting the Root Partition'
752                         mount -t squashfs -o ro "${NEW_ROOT}${CDROOT_DEV}" "${NEW_ROOT}/mnt/livecd"
753                         test_success 'mount /dev/loop0 /'
754                         FS_LOCATION='mnt/livecd'
755                 fi
756         fi
757
758
759
760         # Unpacking additional packages from NFS mount
761         # This is useful for adding kernel modules to /lib
762         # We do this now, so that additional packages can add whereever they want.
763         if [ "${REAL_ROOT}" = '/dev/nfs' ]
764         then
765                 if [ -e "${CDROOT_PATH}/add" ]
766                 then
767                                 for targz in $(ls ${CDROOT_PATH}/add/*.tar.gz)
768                                 do
769                                         tarname=$(basename ${targz})
770                                         good_msg "Adding additional package ${tarname}"
771                                         (cd ${NEW_ROOT} ; /bin/tar -xzf ${targz})
772                                 done
773                 fi
774         fi
775
776
777         if [ "${USE_UNIONFS_NORMAL}" = '1' ]
778         then
779                 setup_unionfs ${NEW_ROOT} /${FS_LOCATION}
780                 CHROOT=/union
781         elif [ "${USE_AUFS_NORMAL}" != '1' ]; then
782
783         good_msg "Copying read-write image contents to tmpfs"
784         # Copy over stuff that should be writable
785         (cd "${NEW_ROOT}/${FS_LOCATION}"; cp -a ${ROOT_TREES} "${NEW_ROOT}") || {
786                 bad_msg "Copying failed, dropping into a shell."
787                 do_rundebugshell
788         }
789
790         # Now we do the links.
791         for x in ${ROOT_LINKS}
792         do
793                 if [ -L "${NEW_ROOT}/${FS_LOCATION}/${x}" ]
794                 then
795                         ln -s "$(readlink ${NEW_ROOT}/${FS_LOCATION}/${x})" "${x}" 2>/dev/null
796                 else
797                         # List all subdirectories of x
798                         find "${NEW_ROOT}/${FS_LOCATION}/${x}" -type d 2>/dev/null | while read directory
799                         do
800                                 # Strip the prefix of the FS_LOCATION
801                                 directory="${directory#${NEW_ROOT}/${FS_LOCATION}/}"
802
803                                 # Skip this directory if we already linked a parent directory
804                                 if [ "${current_parent}" != '' ]; then
805                                         var=$(echo "${directory}" | grep "^${current_parent}")
806                                         if [ "${var}" != '' ]; then
807                                                 continue
808                                         fi
809                                 fi
810                                 # Test if the directory exists already
811                                 if [ -e "/${NEW_ROOT}/${directory}" ]
812                                 then
813                                         # It does exist, link all the individual files
814                                         for file in $(ls /${NEW_ROOT}/${FS_LOCATION}/${directory})
815                                         do
816                                         if [ ! -d "/${NEW_ROOT}/${FS_LOCATION}/${directory}/${file}" ] && [ ! -e "${NEW_ROOT}/${directory}/${file}" ]; then
817                                                         ln -s "/${FS_LOCATION}/${directory}/${file}" "${directory}/${file}" 2> /dev/null
818                                                 fi
819                                         done
820                                 else
821                                         # It does not exist, make a link to the livecd
822                                         ln -s "/${FS_LOCATION}/${directory}" "${directory}" 2>/dev/null
823                                         current_parent="${directory}"
824                                 fi
825                         done
826                 fi
827         done
828
829         mkdir initramfs proc tmp sys run 2>/dev/null
830         chmod 1777 tmp
831
832         fi
833
834         #UML=$(cat /proc/cpuinfo|grep UML|sed -e 's|model name.*: ||')
835         #if [ "${UML}" = 'UML' ]
836         #then
837         #       # UML Fixes
838         #       good_msg 'Updating for uml system'
839         #fi
840
841         # Let Init scripts know that we booted from CD
842         export CDBOOT
843         CDBOOT=1
844 else
845         if [ "${USE_UNIONFS_NORMAL}" = '1' ]
846         then
847                 mkdir /union_changes
848                 mount -t tmpfs tmpfs /union_changes
849                 setup_unionfs /union_changes ${NEW_ROOT}
850                 mkdir -p ${UNION}/tmp/.initrd
851         fi
852 fi
853
854 # Mount the additional things as required by udev & systemd
855 if [ -f ${NEW_ROOT}/etc/initramfs.mounts ]; then
856         fslist=$(get_mounts_list)
857 else
858         fslist="/usr" 
859 fi
860
861 for fs in $fslist; do
862         dev=$(get_mount_device $fs)
863         [ -z "${dev}" ] && continue
864         # Resolve it like util-linux mount does
865         [ -L ${dev} ] && dev=`readlink ${dev}`
866         # In this case, it's probably part of the filesystem
867         # and not a mountpoint
868         [ -z "$dev" ] && continue
869         fstype=$(get_mount_fstype $fs)
870         if get_mount_options $fs | fgrep -q bind ; then
871                 opts='bind'
872                 dev=${NEW_ROOT}${dev}
873         else
874                 # ro must be trailing, and the options will always contain at least 'defaults'
875                 opts="$(get_mount_options $fs | strip_mount_options),ro"
876         fi
877         mnt=${NEW_ROOT}${fs}
878         cmd="mount -t $fstype -o $opts $dev $mnt"
879         good_msg "Mounting $dev as ${fs}: $cmd"
880         if ! $cmd; then
881                 bad_msg "Unable to mount $dev for $fs"
882         fi
883 done
884
885 # Execute script on the cdrom just before boot to update things if necessary
886 cdupdate
887
888 if [ "${SUBDIR}" != '' -a -e "${CHROOT}/${SUBDIR}" ]
889 then
890         good_msg "Entering ${SUBDIR} to boot"
891         CHROOT="${CHROOT}/${SUBDIR}"
892 fi
893
894 verbose_kmsg
895
896 echo -ne "${GOOD}>>${NORMAL}${BOLD} Booting (initramfs)${NORMAL}"
897
898 cd "${CHROOT}"
899 mkdir "${CHROOT}/proc" "${CHROOT}/sys" "${CHROOT}/run" 2>/dev/null
900 echo -ne "${BOLD}.${NORMAL}"
901
902 # If devtmpfs is mounted, try move it to the new root
903 # If that fails, try to unmount all possible mounts of devtmpfs as stuff breaks otherwise
904 for fs in /dev /sys /proc
905 do
906         if grep -qs "$fs" /proc/mounts
907         then
908                 if ! mount --move $fs "${CHROOT}"$fs
909                 then
910                         umount $fs || echo '*: Failed to move and unmount the ramdisk $fs!'
911                 fi
912         fi
913 done
914
915 if [ ! -e "${CHROOT}/dev/console" ] || [ ! -e "${CHROOT}/dev/null" ]
916 then
917         echo -ne "${BAD}>>${NORMAL}${BOLD} ERROR: your real /dev is missing files required to boot (console and null)${NORMAL}"
918 elif [ -e /etc/initrd.splash -a ! -e "${CHROOT}/dev/tty1" ]
919 then
920         echo -ne "${BAD}>>${NORMAL}${BOLD} ERROR: your real /dev is missing tty1, which is required for splash${NORMAL}"
921 fi
922
923 echo -e "${BOLD}.${NORMAL}"
924
925 exec /sbin/switch_root -c "/dev/console" "${CHROOT}" "${REAL_INIT:-/sbin/init}" "${INIT_OPTS}"
926
927 # If we get here, something bad has happened
928 splash 'verbose'
929
930 echo "A fatal error has probably occured since ${REAL_INIT:-/sbin/init} did not"
931 echo "boot correctly. Trying to open a shell..."
932 echo
933 exec /bin/bash
934 exec /bin/sh
935 exec /bin/ash
936 exec /bin/dash
937 exec sh