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