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