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