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