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