aboutsummaryrefslogtreecommitdiff
blob: 0a91c58dc1b351ac0f5d2835759809e32b0baa11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
#!/bin/sh

. /etc/initrd.defaults
. /etc/initrd.scripts

splash() {
	return 0
}

[ -e /etc/initrd.splash ] && . /etc/initrd.splash

# Clean input/output
exec >${CONSOLE} <${CONSOLE} 2>&1

if [ "$$" != '1' ]
then
	echo '/linuxrc has to be run as the init process as the one'
	echo 'with a PID of 1. Try adding init="/linuxrc" to the'
	echo 'kernel command line or running "exec /linuxrc".'
	exit 1
fi

mount -t proc -o noexec,nosuid,nodev proc /proc >/dev/null 2>&1
mount -o remount,rw / >/dev/null 2>&1

# Prevent superfluous printks from being printed to the console
echo 1 > /proc/sys/kernel/printk

# Set up symlinks
/bin/busybox --install -s

if [ "$0" = '/init' ]
then
	[ -e /linuxrc ] && rm /linuxrc
fi

CMDLINE=$(cat /proc/cmdline)
# Scan CMDLINE for any specified real_root= or cdroot arguments
FAKE_ROOT=''
FAKE_INIT=''
FAKE_ROOTFLAGS=''
INIT_OPTS=''
ROOTFSTYPE='auto'
CRYPT_SILENT=0
QUIET=''

mkdir -p /etc/cmdline /etc/modprobe.d
for x in ${CMDLINE}
do
	case "${x}" in
		real_root=*)
			REAL_ROOT=${x#*=}
		;;
		root=*)
			FAKE_ROOT=${x#*=}
		;;
		subdir=*)
			SUBDIR=${x#*=}
		;;
		real_init=*)
			REAL_INIT=${x#*=}
		;;
		init=*)
			FAKE_INIT=${x#*=}
		;;
		init_opts=*)
			INIT_OPTS=${x#*=}
		;;
		# Livecd options
		cdroot)
			CDROOT=1
		;;
		cdroot=*)
			CDROOT=1
			CDROOT_DEV=${x#*=}
		;;
		cdroot_type=*)
			CDROOT_TYPE=${x#*=}
		;;
		cdroot_marker=*)
			CDROOT_MARKER=${x#*=}
		;;
		# Start livecd loop, looptype options
		loop=*)
			LOOP=${x#*=}
		;;
		looptype=*)
			LOOPTYPE=${x#*=}
		;;
		isoboot=*)
			ISOBOOT=${x#*=}
		;;
		# Start Volume manager options
		dolvm)
			USE_LVM_NORMAL=1
		;;
		dolvm2)
			bad_msg 'Using dolvm2 is deprecated, use dolvm, instead.'
			USE_LVM_NORMAL=1
		;;
		domdadm)
			USE_MDADM=1
		;;
		dodmraid)
			USE_DMRAID_NORMAL=1
		;;
		dodmraid=*)
			DMRAID_OPTS=${x#*=}
			USE_DMRAID_NORMAL=1
		;;
		domultipath) 
			good_msg "Booting with multipath activated." 
			USE_MULTIPATH_NORMAL=1 
		;;
		dozfs*)
			USE_ZFS=1

			if [ "${x#*=}" = 'force' ]
			then
				ZPOOL_FORCE=-f
			fi
		;;
		quiet)
			QUIET=1
		;;
		# Debug Options
		debug)
			DEBUG='yes'
		;;
		# Scan delay options
		scandelay=*)
			SDELAY=${x#*=}
		;;
		scandelay)
			SDELAY=3
		;;
		# Module no-loads
		doload=*)
			MDOLIST=${x#*=}
			MDOLIST=$(echo ${MDOLIST} | sed -e 's/,/ /g')
		;;
		nodetect)
			NODETECT=1
		;;
		noload=*)
			MLIST=${x#*=}
			MLIST="$(echo ${MLIST} | sed -e 's/,/ /g')"
			export MLIST
		;;
		# Redirect output to a specific tty
		CONSOLE=*|console=*)
			CONSOLE=${x#*=}
			CONSOLE=$(basename ${CONSOLE})
#			exec >${CONSOLE} <${CONSOLE} 2>&1
		;;
		# /dev/md
		lvmraid=*)
			RAID_DEVICES="${x#*=}"
			RAID_DEVICES="$(echo ${RAID_DEVICES} | sed -e 's/,/ /g')"
			USE_LVM_NORMAL=1
		;;
		part=*)
			MDPART=${x#*=}
		;;
		part|partitionable)
			MDPART=1
		;;
		# NFS
		ip=*)
			IP=${x#*=}
		;;
		nfsroot=*)
			NFSROOT=${x#*=}
		;;
		# iSCSI
		iscsi_initiatorname=*)
			ISCSI_INITIATORNAME=${x#*=}
		;;
		iscsi_target=*)
			ISCSI_TARGET=${x#*=}
		;;
		iscsi_tgpt=*)
			ISCSI_TGPT=${x#*=}
		;;
		iscsi_address=*)
			ISCSI_ADDRESS=${x#*=}
		;;
		iscsi_port=*)
			ISCSI_PORT=${x#*=}
		;;
		iscsi_username=*)
			ISCSI_USERNAME=${x#*=}
		;;
		iscsi_password=*)
			ISCSI_PASSWORD=${x#*=}
		;;
		iscsi_username_in=*)
			ISCSI_USERNAME_IN=${x#*=}
		;;
		iscsi_password_in=*)
			ISCSI_PASSWORD_IN=${x#*=}
		;;
		iscsi_debug=*)
			ISCSI_DEBUG=${x#*=}
		;;
		iscsi_noibft)
			ISCSI_NOIBFT=1
		;;
		# Crypto
		crypt_root=*)
			CRYPT_ROOT=${x#*=}
		;;
		crypt_swap=*)
			CRYPT_SWAP=${x#*=}
		;;
		root_key=*)
			CRYPT_ROOT_KEY=${x#*=}
		;;
		root_keydev=*)
			CRYPT_ROOT_KEYDEV=${x#*=}
		;;
		root_trim=*)
			CRYPT_ROOT_TRIM=${x#*=}
		;;

		swap_key=*)
			CRYPT_SWAP_KEY=${x#*=}
		;;
		swap_keydev=*)
			CRYPT_SWAP_KEYDEV=${x#*=}
		;;
		real_resume=*|resume=*)
			REAL_RESUME=${x#*=}
		;;
		noresume)
			NORESUME=1
		;;
		crypt_silent)
			CRYPT_SILENT=1
		;;
		real_rootflags=*)
			REAL_ROOTFLAGS=${x#*=}
		;;
		rootflags=*)
			FAKE_ROOTFLAGS=${x#*=}
		;;
		rootfstype=*)
			ROOTFSTYPE=${x#*=}
		;;
		keymap=*)
			keymap=${x#*=}
		;;
		aufs)
			USE_AUFS_NORMAL=1
		;;
		aufs\=*)
			USE_AUFS_NORMAL=1
			echo ${x#*=} | grep , &>/dev/null

			if [ "$?" -eq '0' ]
			then
				UID=${x#*,}
				AUFS=${x%,*}
			else
				AUFS=${x#*=}
			fi
		;;
		aufs.changes\=*)
                        USE_AUFS_NORMAL=1
                        echo ${x#*=} | grep , &>/dev/null

                        if [ "$?" -eq '0' ]
                        then
                                UID=${x#*,}
                                AUFS=${x%,*}
                        else
                                AUFS=${x#*=}
                        fi
                ;;
		aufs.persistent)
			USE_AUFS_NORMAL=1
			AUFS="detect"
		;;
		# Allow user to specify the modules location
		aufs.modules\=*)
			MODULESD=${x#*=}
		;;
		unionfs)
			if [ ! -x /sbin/unionfs ]
			then
				USE_UNIONFS_NORMAL=0
				bad_msg 'unionfs binary not found: aborting use of unionfs!'
			else
				USE_UNIONFS_NORMAL=1
			fi
			;;
		nounionfs)
			USE_UNIONFS_NORMAL=0
			;;
	esac
done

quiet_kmsg

if [ "${CDROOT}" = '0' ]
then
	if [ -z "${REAL_ROOT}" -a "${FAKE_ROOT}" != "/dev/ram0" ]
	then
		REAL_ROOT="${FAKE_ROOT}"
	fi
	if [ -z "${REAL_INIT}" -a "${FAKE_INIT}" != "/linuxrc" ]
	then
		REAL_INIT="${FAKE_INIT}"
	fi
	if [ -z "${REAL_ROOTFLAGS}" ]
	then
		REAL_ROOTFLAGS="${FAKE_ROOTFLAGS}"
	fi
fi

# Set variables based on the value of REAL_ROOT
case "${REAL_ROOT}" in
	ZFS=*)
		ZFS_POOL=${REAL_ROOT#*=}
		ZFS_POOL=${ZFS_POOL%%/*}
		USE_ZFS=1
	;;
	ZFS)
		USE_ZFS=1
	;;
esac

# Verify that it is safe to use ZFS
if [ "USE_ZFS" = "1" ]
then
	for i in /sbin/zfs /sbin/zpool
	do
		if [ ! -x ${i} ]
		then
			USE_ZFS=0
			bad_msg 'Aborting use of zfs because ${i} not found!'
			break
		fi
	done

	[ "USE_ZFS" = "1" ] && MY_HWOPTS="${MY_HWOPTS} zfs"
fi

splash 'init'

cmdline_hwopts

# Mount devfs
mount_devfs

# Mount sysfs
mount_sysfs

# Initialize mdev
good_msg 'Activating mdev'

# Serialize hotplug events
touch /dev/mdev.seq

# Setup hotplugging for firmware loading
echo /sbin/mdev > /proc/sys/kernel/hotplug

# Load modules listed in MY_HWOPTS if /lib/modules exists for the running kernel
if [ -z "${DO_modules}" ]
then
	good_msg 'Skipping module load; disabled via commandline'
elif [ -d "/lib/modules/${KV}" ]
then
	good_msg 'Loading modules'
	# Load appropriate kernel modules
	if [ "${NODETECT}" != '1' ]
	then
		for modules in ${MY_HWOPTS}
		do
			modules_scan ${modules}
		done
	fi
	# Always eval doload=...
	modules_load ${MDOLIST}
else
	good_msg 'Skipping module load; no modules in the ramdisk!'
fi

# Ensure that device nodes are properly configured
mdev -s || bad_msg "mdev -s failed"

# Apply scan delay if specified
sdelay

cd /

# Start iSCSI
if [ -e /bin/iscsistart ]
then
	startiscsi
fi

# Setup btrfs, see bug 303529
setup_btrfsctl

# Setup md device nodes if they dont exist
setup_md_device

# Scan volumes
startVolumes

setup_keymap

# Initialize LUKS root device except for livecd's
if [ "${CDROOT}" != 1 ]
then
	startLUKS
	if [ "${NORESUME}" != '1' ] && [ -n "${REAL_RESUME}" ]
	then
		case "${REAL_RESUME}" in
			LABEL=*|UUID=*)

				RESUME_DEV=""
				retval=1

				if [ ${retval} -ne 0 ]; then
					RESUME_DEV=$(findfs "${REAL_RESUME}" 2>/dev/null)
					retval=$?
				fi

				if [ ${retval} -ne 0 ]; then
					RESUME_DEV=$(busybox findfs "${REAL_RESUME}" 2>/dev/null)
					retval=$?
				fi

				if [ ${retval} -ne 0 ]; then
					RESUME_DEV=$(blkid -o device -l -t "${REAL_RESUME}")
					retval=$?
				fi

				if [ ${retval} -eq 0 ] && [ -n "${RESUME_DEV}" ]; then
					good_msg "Detected real_resume=${RESUME_DEV}"
					REAL_RESUME="${RESUME_DEV}"
				fi
				;;
		esac

		do_resume
	fi
fi

mkdir -p "${NEW_ROOT}"
CHROOT="${NEW_ROOT}"

# Run debug shell if requested
rundebugshell "before setting up the root filesystem"

if [ "${CDROOT}" = '1' ]
then
	setup_aufs
	if [ "${USE_AUFS_NORMAL}" -eq '1' ]
	then
		CHROOT=${UNION}
	else
		CHROOT=${NEW_ROOT}
		good_msg "Making tmpfs for ${NEW_ROOT}"
		mount -n -t tmpfs tmpfs ${NEW_ROOT}

		for i in dev mnt ${CDROOT_PATH} proc run mnt/livecd mnt/key tmp tmp/.initrd mnt/gentoo sys
		do
			mkdir -p "${NEW_ROOT}/${i}"
			chmod 755 "${NEW_ROOT}/${i}"
		done
		[ ! -d "${CDROOT_PATH}" ] && mkdir -p "${CDROOT_PATH}"
		[ ! -e "${NEW_ROOT}/dev/null" ] && mknod -m 666 "${NEW_ROOT}"/dev/null c 1 3
		[ ! -e "${NEW_ROOT}/dev/zero" ] && mknod -m 666 "${NEW_ROOT}"/dev/zero c 1 5
		[ ! -e "${NEW_ROOT}/dev/console" ] && mknod -m 600 "${NEW_ROOT}"/dev/console c 5 1
		[ ! -e "${NEW_ROOT}/dev/ttyS0" ] && mknod -m 660 "${NEW_ROOT}"/dev/ttyS0 c 4 64
	fi

	# For SGI LiveCDs ...
	if [ "${LOOPTYPE}" = "sgimips" ]
	then
		[ ! -e "${NEW_ROOT}/dev/sr0" ] && mknod "${NEW_ROOT}/dev/sr0" b 11 0
		[ ! -e "${NEW_ROOT}/dev/loop0" ] && mknod "${NEW_ROOT}/dev/loop0" b 7 0
	fi

	# Required for splash to work.  Not an issue with the initrd as this
	# device isn't created there and is not needed.
	for minor in 0 1 ; do
		[ ! -e "${NEW_ROOT}/dev/$minor" ] && mknod -m 600 "${NEW_ROOT}/dev/tty$minor" c 4 $minor
	done

	if [ "${REAL_ROOT}" != "/dev/nfs" ] && [ "${LOOPTYPE}" != "sgimips" ] && [ "${USE_AUFS_NORMAL}" != '1' ]
	then
		bootstrapCD
	fi

	if [ "${REAL_ROOT}" = '' ]
	then
		warn_msg "No bootable medium found. Waiting for new devices..."
		COUNTER=0
		while [ ${COUNTER} -lt 3 ]; do
			sleep 3
			let COUNTER=${COUNTER}+1
		done
		sleep 1
		bootstrapCD
	fi

	if [ "${REAL_ROOT}" = '' ]
	then
		# Undo stuff
		umount  "${NEW_ROOT}/dev" 2>/dev/null
		umount  "${NEW_ROOT}/sys" 2>/dev/null
		umount /sys 2>/dev/null

		umount  "${NEW_ROOT}"
		rm -rf  "${NEW_ROOT}/*"

		bad_msg 'Could not find CD to boot, something else needed!'
		CDROOT=0
	fi
fi

# Determine root device
good_msg 'Determining root device...'
while true
do
	while [ "${got_good_root}" != '1' ]
	do
		case "${REAL_ROOT}" in
			LABEL=*|UUID=*)

				ROOT_DEV=""
				retval=1

				if [ ${retval} -ne 0 ]; then
					ROOT_DEV=$(findfs "${REAL_ROOT}" 2>/dev/null)
					retval=$?
				fi

				if [ ${retval} -ne 0 ]; then
					ROOT_DEV=$(busybox findfs "${REAL_ROOT}" 2>/dev/null)
					retval=$?
				fi

				if [ ${retval} -ne 0 ]; then
					ROOT_DEV=$(blkid -o device -l -t "${REAL_ROOT}")
					retval=$?
				fi

				if [ ${retval} -eq 0 ] && [ -n "${ROOT_DEV}" ]; then
					good_msg "Detected real_root=${ROOT_DEV}"
					REAL_ROOT="${ROOT_DEV}"
				else
					prompt_user "REAL_ROOT" "root block device"
					got_good_root=0
					continue
				fi
				;;
			ZFS*)
				if [ "${USE_ZFS}" = '0' ]; then
					prompt_user "REAL_ROOT" "root block device"
					continue
				fi
					
				ROOT_DEV="${REAL_ROOT#*=}"
				if [ "${ROOT_DEV}" != 'ZFS' ] 
				then
					if [ "$(zfs get type -o value -H ${ROOT_DEV})" = 'filesystem' ]
					then
						got_good_root=1;
						REAL_ROOT=${ROOT_DEV}
						ROOTFSTYPE=zfs
					else
						bad_msg "${ROOT_DEV} is not a filesystem"
						prompt_user "REAL_ROOT" "root block device"
						got_good_root=0
						continue
					fi
				else
					BOOTFS=$(/sbin/zpool list -H -o bootfs)
					if [ "${BOOTFS}" != '-' ]
					then

						for i in ${BOOTFS}
						do

							zfs get type ${i} > /dev/null
							retval=$?

							if [ ${retval} -eq 0 ];	then
								got_good_root=1
								REAL_ROOT=${i}
								ROOTFSTYPE=zfs
								break
							fi	
						
						done;

					else
						got_good_root=0
					fi

				fi

				if [ ${got_good_root} -ne 1 ]; then
					prompt_user "REAL_ROOT" "root block device"
					got_good_root=0
				fi
					
				continue
				;;
		esac

		if [ "${REAL_ROOT}" = '' ]
		then
			# No REAL_ROOT determined/specified. Prompt user for root block device.
			prompt_user "REAL_ROOT" "root block device"
			got_good_root=0

		# Check for a block device or /dev/nfs
		elif [ -b "${REAL_ROOT}" ] || [ "${REAL_ROOT}" = "/dev/nfs" ]
		then
			got_good_root=1

		else
			bad_msg "Block device ${REAL_ROOT} is not a valid root device..."
			REAL_ROOT=""
			got_good_root=0
		fi
	done


	if [ "${CDROOT}" = 1 -a "${got_good_root}" = '1' -a "${REAL_ROOT}" != "/dev/nfs" ]
	then
		# CD already mounted; no further checks necessary
		break
	elif [ "${LOOPTYPE}" = "sgimips" ]
	then
		# sgimips mounts the livecd root partition directly
		# there is no isofs filesystem to worry about
		break
	else
		good_msg "Mounting $REAL_ROOT as root..."

		if [ "${ROOTFSTYPE}" = 'zfs' ]
		then
			if [ "zfs get -H -o value mountpoint ${REAL_ROOT}" = 'legacy' ]
			then
				MOUNT_STATE=rw
			else
				MOUNT_STATE=rw,zfsutil
			fi
		else
			MOUNT_STATE=ro
		fi

		# Try to mount the device as ${NEW_ROOT}
		if [ "${REAL_ROOT}" = '/dev/nfs' ]; then
			findnfsmount
		else
			# If $REAL_ROOT is a symlink
			# Resolve it like util-linux mount does
			[ -L ${REAL_ROOT} ] && REAL_ROOT=`readlink ${REAL_ROOT}`
			# mount ro so fsck doesn't barf later
			if [ "${REAL_ROOTFLAGS}" = '' ]; then
				good_msg "Using mount -t ${ROOTFSTYPE} -o ${MOUNT_STATE}"
				mount -t ${ROOTFSTYPE} -o ${MOUNT_STATE} ${REAL_ROOT} ${NEW_ROOT}
			else
				good_msg "Using mount -t ${ROOTFSTYPE} -o ${MOUNT_STATE},${REAL_ROOTFLAGS}"
				mount -t ${ROOTFSTYPE} -o ${MOUNT_STATE},${REAL_ROOTFLAGS} ${REAL_ROOT} ${NEW_ROOT}
			fi
		fi

		# If mount is successful break out of the loop
		# else not a good root and start over.
		if [ "$?" = '0' ]
		then
			if [ -d ${NEW_ROOT}/dev -a -x "${NEW_ROOT}${REAL_INIT:-/sbin/init}" ] || [ "${REAL_ROOT}" = "/dev/nfs" ]
			then
				break
			else
				bad_msg "The filesystem mounted at ${REAL_ROOT} does not appear to be a valid /, try again"
				got_good_root=0
				REAL_ROOT=''
			fi
		else
			bad_msg "Could not mount specified ROOT, try again"
			got_good_root=0
			REAL_ROOT=''
		fi
	fi
done
# End determine root device

#verbose_kmsg

# If CD root is set determine the looptype to boot
if [ "${CDROOT}" = '1' ]
then
	good_msg 'Determining looptype ...'
	cd "${NEW_ROOT}"

	# Find loop and looptype
	[ -z "${LOOP}" ] && find_loop
	[ -z "${LOOPTYPE}" ] && find_looptype

	cache_cd_contents

	# If encrypted, find key and mount, otherwise mount as usual
	if [ -n "${CRYPT_ROOT}" ]
	then
		CRYPT_ROOT_KEY="$(head -n 1 "${CDROOT_PATH}"/${CDROOT_MARKER})"
		CRYPT_ROOT='/dev/loop0'
		good_msg 'You booted an encrypted livecd' "${CRYPT_SILENT}"

		losetup /dev/loop0 "${CDROOT_PATH}/${LOOPEXT}${LOOP}"
		test_success 'Preparing loop filesystem'

		startLUKS

		case ${LOOPTYPE} in
			normal)
				MOUNTTYPE="ext2"
				;;
			*)
				MOUNTTYPE="${LOOPTYPE}"
				;;
		esac
		mount -t "${MOUNTTYPE}" -o ro /dev/mapper/root "${NEW_ROOT}/mnt/livecd"
		test_success 'Mount filesystem'
		FS_LOCATION='mnt/livecd'
	# Setup the loopback mounts, if unencrypted
	else # if [ -n "${CRYPT_ROOT}" ]
		if [ "${LOOPTYPE}" = 'normal' ]
		then
			good_msg 'Mounting loop filesystem'
			mount -t ext2 -o loop,ro "${CDROOT_PATH}/${LOOPEXT}${LOOP}" "${NEW_ROOT}/mnt/livecd"
			test_success 'Mount filesystem'
			FS_LOCATION='mnt/livecd'
		elif [ "${LOOPTYPE}" = 'squashfs' ]
		then
			if [ "${USE_AUFS_NORMAL}" != '1' ]; then
				good_msg 'Mounting squashfs filesystem'
				_CACHED_SQUASHFS_PATH="${NEW_ROOT}/mnt/${LOOP}"
				_squashfs_path="${CDROOT_PATH}/${LOOPEXT}${LOOP}"  # Default to uncached
				# Upgrade to cached version if possible
				[ "${DO_cache}" -a -f "${_CACHED_SQUASHFS_PATH}" ] \
						&& _squashfs_path=${_CACHED_SQUASHFS_PATH}
				mount -t squashfs -o loop,ro "${_squashfs_path}" "${NEW_ROOT}/mnt/livecd" || {
					bad_msg "Squashfs filesystem could not be mounted, dropping into shell."
					if [ -e /proc/filesystems ]; then
						fgrep -q squashfs /proc/filesystems || \
							bad_msg "HINT: Your kernel does not know filesystem \"squashfs\"."
					fi
					do_rundebugshell
				}
			else
				good_msg 'Mounting squashfs & aufs filesystems'
				setup_squashfs_aufs
				test_success 'Mount filesystem'
			fi
			FS_LOCATION='mnt/livecd'
		elif [ "${LOOPTYPE}" = 'gcloop' ]
		then
			good_msg 'Mounting gcloop filesystem'
			echo ' ' | losetup -E 19 -e ucl-0 -p0 "${NEW_ROOT}/dev/loop0" "${CDROOT_PATH}/${LOOPEXT}${LOOP}"
			test_success 'losetup the loop device'

			mount -t ext2 -o ro "${NEW_ROOT}/dev/loop0" "${NEW_ROOT}/mnt/livecd"
			test_success 'Mount the losetup loop device'
			FS_LOCATION='mnt/livecd'
		elif [ "${LOOPTYPE}" = 'zisofs' ]
		then
			FS_LOCATION="${CDROOT_PATH/\/}/${LOOPEXT}${LOOP}"
		elif [ "${LOOPTYPE}" = 'noloop' ]
		then
			FS_LOCATION="${CDROOT_PATH/\/}"
		elif [ "${LOOPTYPE}" = 'sgimips' ]
		then
			# getdvhoff finds the starting offset (in bytes) of the squashfs
			# partition on the cdrom and returns this offset for losetup
			#
			# All currently supported SGI Systems use SCSI CD-ROMs, so
			# so we know that the CD-ROM is usually going to be /dev/sr0.
			#
			# We use the value given to losetup to set /dev/loop0 to point
			# to the liveCD root partition, and then mount /dev/loop0 as
			# the LiveCD rootfs
			good_msg 'Locating the SGI LiveCD Root Partition'
			echo ' ' | \
				losetup -o $(getdvhoff "${NEW_ROOT}${REAL_ROOT}" 0) \
					"${NEW_ROOT}${CDROOT_DEV}" \
					"${NEW_ROOT}${REAL_ROOT}"
			test_success 'losetup /dev/sr0 /dev/loop0'

			good_msg 'Mounting the Root Partition'
			mount -t squashfs -o ro "${NEW_ROOT}${CDROOT_DEV}" "${NEW_ROOT}/mnt/livecd"
			test_success 'mount /dev/loop0 /'
			FS_LOCATION='mnt/livecd'
		fi
	fi # if [ -n "${CRYPT_ROOT}" ]

	if [ "${USE_AUFS_NORMAL}" -eq '1' ]
	then
		union_insert_dir ${UNION} ${NEW_ROOT}/${FS_LOCATION}

		# Make sure fstab notes livecd is mounted ro.  Makes system skip remount which fails on aufs dirs.
		# TODO: remounting all tmpfs as RO seems weird, can we be more specific?
		sed -e 's|\(.*\s/\s*tmpfs\s*\)defaults\(.*\)|\1defaults,ro\2|' /${UNION}/etc/fstab > /${UNION}/etc/fstab.new
		mv /${UNION}/etc/fstab.new /${UNION}/etc/fstab

                # RC_NO_UMOUNTS variable for a clean shutdown/reboot
                test ! $(grep 'RC_NO_UMOUNTS="/newroot|/newroot/mnt/aufs|\
/newroot/mnt/changesdev|/mnt/livecd|/mnt/cdrom|/.unions/\
memory|/.unions/memory/xino"' "${UNION}"/etc/rc.conf) &&
                        echo "RC_NO_UMOUNTS=\"/newroot|/newroot/mnt/aufs|"\
"/newroot/mnt/changesdev|/mnt/livecd|/mnt/cdrom|"\
"/.unions/memory|/.unions/memory/xino\"">> "${UNION}"/etc/rc.conf

                # Fstab change for aufs
                test ! $(grep aufs "${UNION}"/etc/fstab) &&
                        echo "aufs / aufs defaults 0 0" > "${UNION}"/etc/fstab

		warn_msg "Adding all modules in $MODULESD/modules/"
		if [ -z "${MODULESD}" ]
		then
			union_insert_modules ${CDROOT_PATH}
		else
			mkdir ${NEW_ROOT}/mnt/modulesd
			mount "${MODULESD}" ${NEW_ROOT}/mnt/modulesd
			union_insert_modules ${NEW_ROOT}/mnt/modulesd
		fi

                mkdir -p "${UNION}"/newroot/mnt/aufs
                test ${AUFS_CHANGES} && test ! -d "${UNION}"/newroot/mnt/changesdev &&
                        mkdir "${UNION}"/newroot/mnt/changesdev
	fi

	# Unpacking additional packages from NFS mount
	# This is useful for adding kernel modules to /lib
	# We do this now, so that additional packages can add whereever they want.
	if [ "${REAL_ROOT}" = '/dev/nfs' ]
	then
		if [ -e "${CDROOT_PATH}/add" ]
		then
				for targz in $(ls ${CDROOT_PATH}/add/*.tar.gz)
				do
					tarname=$(basename ${targz})
					good_msg "Adding additional package ${tarname}"
					(cd ${NEW_ROOT} ; /bin/tar -xzf ${targz})
				done
		fi
	fi


	if [ "${USE_UNIONFS_NORMAL}" = '1' ]
	then
		setup_unionfs ${NEW_ROOT} /${FS_LOCATION}
		CHROOT=/union
	else
		#XXX Note to potential reviewers. diff formats this section very very oddly. Be sure to review this hunk after applied, do NOT simply read the diff
		if [ ! "${USE_AUFS_NORMAL}" -eq '1' ]
		then
			good_msg "Copying read-write image contents to tmpfs"
			# Copy over stuff that should be writable
			(cd "${NEW_ROOT}/${FS_LOCATION}"; cp -a ${ROOT_TREES} "${NEW_ROOT}") || {
				bad_msg "Copying failed, dropping into a shell."
				do_rundebugshell
			}

			# Now we do the links.
			for x in ${ROOT_LINKS}
			do
				if [ -L "${NEW_ROOT}/${FS_LOCATION}/${x}" ]
				then
					ln -s "$(readlink ${NEW_ROOT}/${FS_LOCATION}/${x})" "${x}" 2>/dev/null
				else
					# List all subdirectories of x
					find "${NEW_ROOT}/${FS_LOCATION}/${x}" -type d 2>/dev/null | while read directory
					do
						# Strip the prefix of the FS_LOCATION
						directory="${directory#${NEW_ROOT}/${FS_LOCATION}/}"

						# Skip this directory if we already linked a parent directory
						if [ "${current_parent}" != '' ]; then
							var=$(echo "${directory}" | grep "^${current_parent}")
							if [ "${var}" != '' ]; then
								continue
							fi
						fi
						# Test if the directory exists already
						if [ -e "/${NEW_ROOT}/${directory}" ]
						then
							# It does exist, link all the individual files
							for file in $(ls /${NEW_ROOT}/${FS_LOCATION}/${directory})
							do
								if [ ! -d "/${NEW_ROOT}/${FS_LOCATION}/${directory}/${file}" ] && [ ! -e "${NEW_ROOT}/${directory}/${file}" ]; then
									ln -s "/${FS_LOCATION}/${directory}/${file}" "${directory}/${file}" 2> /dev/null
								fi
							done
						else
							# It does not exist, make a link to the livecd
							ln -s "/${FS_LOCATION}/${directory}" "${directory}" 2>/dev/null
							current_parent="${directory}"
						fi # if [ -e "/${NEW_ROOT}/${directory}" ] ... else
					done # while read directory
				fi # if [ -L "${NEW_ROOT}/${FS_LOCATION}/${x}" ] ... else
			done # for x in ${ROOT_LINKS}
			mkdir -p initramfs proc tmp run sys 2>/dev/null
			chmod 1777 tmp

		fi # if [ ! "${USE_AUFS_NORMAL}" -eq '1' ]
		#XXX: end extremely confusing hunk

		# have handy /mnt/cdrom (CDROOT_PATH) as well
		_new_cdroot="${NEW_ROOT}${CDROOT_PATH}"
		[ ! -d "${_new_cdroot}" ] && mkdir -p "${_new_cdroot}"
		mount --bind "${CDROOT_PATH}" "${_new_cdroot}"

	fi # if [ "${USE_UNIONFS_NORMAL}" = '1' ] ... else

	#UML=$(cat /proc/cpuinfo|grep UML|sed -e 's|model name.*: ||')
	#if [ "${UML}" = 'UML' ]
	#then
	#	# UML Fixes
	#	good_msg 'Updating for uml system'
	#fi

	# Let Init scripts know that we booted from CD
	export CDBOOT
	CDBOOT=1
else # if [ "${CDROOT}" = '1' ]
	if [ "${USE_UNIONFS_NORMAL}" = '1' ]
	then
		mkdir /union_changes
		mount -t tmpfs tmpfs /union_changes
		setup_unionfs /union_changes ${NEW_ROOT}
		mkdir -p ${UNION}/tmp/.initrd
	fi
	if [ "${USE_AUFS_NORMAL}" -eq '1' ]
	then
		union_insert_dir ${UNION} ${NEW_ROOT}
		mkdir -p ${UNION}/tmp/.initrd
	fi

fi # if [ "${CDROOT}" = '1' ]

# Mount the additional things as required by udev & systemd
if [ -f ${NEW_ROOT}/etc/initramfs.mounts ]; then
	fslist=$(get_mounts_list)
else
	fslist="/usr" 
fi

for fs in $fslist; do
	dev=$(get_mount_device $fs)
	[ -z "${dev}" ] && continue
	# Resolve it like util-linux mount does
	[ -L ${dev} ] && dev=`readlink ${dev}`
	# In this case, it's probably part of the filesystem
	# and not a mountpoint
	[ -z "$dev" ] && continue
	fstype=$(get_mount_fstype $fs)
	if get_mount_options $fs | fgrep -q bind ; then
		opts='bind'
		dev=${NEW_ROOT}${dev}
	else
		# ro must be trailing, and the options will always contain at least 'defaults'
		opts="$(get_mount_options $fs | strip_mount_options),ro"
	fi
	mnt=${NEW_ROOT}${fs}
	cmd="mount -t $fstype -o $opts $dev $mnt"
	good_msg "Mounting $dev as ${fs}: $cmd"
	if ! $cmd; then
		bad_msg "Unable to mount $dev for $fs"
	fi
done # for fs in $fslist; do

# Execute script on the cdrom just before boot to update things if necessary
cdupdate

if [ "${SUBDIR}" != '' -a -e "${CHROOT}/${SUBDIR}" ]
then
	good_msg "Entering ${SUBDIR} to boot"
	CHROOT="${CHROOT}/${SUBDIR}"
fi

verbose_kmsg

if [ "${USE_AUFS_NORMAL}" -eq '1' ]
then
	mkdir -p /${CHROOT}/.unions/memory
	mount -o move /memory /${CHROOT}/.unions/memory 
        test_success "Failed to move aufs /memory into the system root"
        for i in mnt/gentoo mnt/livecd /mnt/overlay ${CDROOT_PATH}
	do
		mkdir -p ${CHROOT}/$i
		chmod 755 ${CHROOT}/$i
	done
	# This will prevent from putting junk on the CHANGESDEV
	str=""
	for i in tmp var/tmp usr/portage/distfiles
	do
		mkdir -p ${CHROOT}/$i
		chmod 755 ${CHROOT}/$i
		mount -t tmpfs tmpfs ${CHROOT}/$i
		str="${str} ${i}"
	done
	warn_msg "${str}are mounted in ram"
	warn_msg "consider saving important files elsewhere..."
	read -t 3 UNUSEDVAL
        for i in ${CDROOT_PATH} ${overlay} ${static}; do
                mount --move ${NEW_ROOT}${i} ${CHROOT}${i}
        done
fi

good_msg "Booting (initramfs)"

cd "${CHROOT}"
mkdir "${CHROOT}/proc" "${CHROOT}/sys" "${CHROOT}/run" 2>/dev/null

# If devtmpfs is mounted, try move it to the new root
# If that fails, try to unmount all possible mounts of
# devtmpfs as stuff breaks otherwise
for fs in /dev /sys /proc
do
	if grep -qs "$fs" /proc/mounts
	then
		if ! mount --move $fs "${CHROOT}"$fs
		then
			umount $fs || \
			bad_msg "Failed to move and unmount the ramdisk $fs!"
		fi
	fi
done

if [ ! -e "${CHROOT}/dev/console" ] || [ ! -e "${CHROOT}/dev/null" ]
then
	bad_msg "ERROR: your real /dev is missing console and null"
elif [ -e /etc/initrd.splash -a ! -e "${CHROOT}/dev/tty1" ]
then
	bad_msg "ERROR: your real /dev is missing tty1, required for splash"
fi

# Run debug shell if requested
rundebugshell "before entering switch_root"

exec /sbin/switch_root -c "/dev/console" "${CHROOT}" "${REAL_INIT:-/sbin/init}" "${INIT_OPTS}"

# If we get here, something bad has happened
splash 'verbose'

bad_msg "A fatal error has occured since ${REAL_INIT:-/sbin/init} did not"
bad_msg "boot correctly. Trying to open a shell..."

exec /bin/bash
exec /bin/sh
exec /bin/ash
exec /bin/dash
exec sh