Fixed the iscsifs issue with ubuntu
[people/warthog9/bko.git] / bko / live / ubuntu / U9 / casper
1 #!/bin/sh
2
3 # set -e
4
5 export PATH=/usr/bin:/usr/sbin:/bin:/sbin
6
7 mountpoint=/cdrom
8
9 root_persistence="casper-rw"
10 home_persistence="home-rw"
11 root_snapshot_label="casper-sn"
12 home_snapshot_label="home-sn"
13 export RESCUEBREAK="No"
14
15 USERNAME=casper
16 USERFULLNAME="Live session user"
17 HOST=live
18 BUILD_SYSTEM=Custom
19
20 mkdir -p $mountpoint
21
22 [ -f /etc/casper.conf ] && . /etc/casper.conf
23 export USERNAME USERFULLNAME HOST BUILD_SYSTEM
24
25 . /scripts/casper-helpers
26
27 if [ ! -f /casper.vars ]; then
28     touch /casper.vars
29 fi
30
31 parse_cmdline() {
32     for x in $(cat /proc/cmdline); do
33         case $x in
34             showmounts|show-cow)
35                 export SHOWMOUNTS='Yes' ;;
36             persistent)
37                 export PERSISTENT="Yes" ;;
38             nopersistent)
39                 export PERSISTENT="" ;;
40             rescuebreak)
41                 export RESCUEBREAK="Yes" ;;
42             union=*)
43                 export UNIONFS="${x#union=}";;
44             ip*)
45                 STATICIP=${x#ip=}
46                 if [ "${STATICIP}" = "" ]; then
47                     STATICIP="frommedia"
48                 fi
49                 export STATICIP ;;
50             ignore_uuid)
51                 IGNORE_UUID="Yes" ;;
52         esac
53     done
54     if [ "${UNIONFS}" = "" ]; then
55         export UNIONFS="aufs"
56     fi
57 }
58
59 is_casper_path() {
60     path=$1
61     if [ -d "$path/casper" ]; then
62         if [ "$(echo $path/casper/*.squashfs)" != "$path/casper/*.squashfs" ] ||
63             [ "$(echo $path/casper/*.ext2)" != "$path/casper/*.ext2" ] ||
64             [ "$(echo $path/casper/*.dir)" != "$path/casper/*.dir" ]; then
65             return 0
66         fi
67     fi
68     return 1
69 }
70
71 matches_uuid() {
72     if [ "$IGNORE_UUID" ] || [ ! -e /conf/uuid.conf ]; then
73         return 0
74     fi
75     path="$1"
76     uuid="$(cat /conf/uuid.conf)"
77     for try_uuid_file in "$path/.disk/casper-uuid"*; do
78         [ -e "$try_uuid_file" ] || continue
79         try_uuid="$(cat "$try_uuid_file")"
80         if [ "$uuid" = "$try_uuid" ]; then
81             return 0
82         fi
83     done
84     return 1
85 }
86
87 get_backing_device() {
88     case "$1" in
89         *.squashfs|*.ext2)
90             echo $(setup_loop "$1" "loop" "/sys/block/loop*")
91             ;;
92         *.dir)
93             echo "directory"
94             ;;
95         *)
96             panic "Unrecognized casper filesystem: $1"
97             ;;
98     esac
99 }
100
101 match_files_in_dir() {
102     # Does any files match pattern $1 ?
103
104     local pattern="$1"
105     if [ "$(echo $pattern)" != "$pattern" ]; then
106         return 0
107     fi
108     return 1
109 }
110
111 mount_images_in_directory() {
112     directory="$1"
113     rootmnt="$2"
114     if match_files_in_dir "$directory/casper/*.squashfs" ||
115         match_files_in_dir "$directory/casper/*.ext2" ||
116         match_files_in_dir "$directory/casper/*.dir"; then
117         setup_unionfs "$directory/casper" "$rootmnt"
118     else
119         :
120     fi
121 }
122
123 is_nice_device() {
124     sysfs_path="${1#/sys}"
125     if /lib/udev/path_id "${sysfs_path}" | grep -E -q "ID_PATH=(usb|pci-[^-]*-(ide|scsi|usb)|platform-mmc)"; then
126         return 0
127     fi
128     return 1
129 }
130
131 copy_live_to() {
132     copyfrom="${1}"
133     copytodev="${2}"
134     copyto="${copyfrom}_swap"
135
136     size=$(fs_size "" ${copyfrom} "used")
137
138     if [ "${copytodev}" = "ram" ]; then
139         # copying to ram:
140         freespace=$( expr $(awk '/MemFree/{print $2}' /proc/meminfo) + $( cat /proc/meminfo | grep Cached | head -n 1 | awk '/Cached/{print $2}' - ) )
141         mount_options="-o size=${size}k"
142         free_string="memory"
143         fstype="tmpfs"
144         dev="/dev/shm"
145     else
146         # it should be a writable block device
147         if [ -b "${copytodev}" ]; then
148             dev="${copytodev}"
149             free_string="space"
150             fstype=$(get_fstype "${dev}")
151             freespace=$(fs_size "${dev}")
152         else
153             [ "$quiet" != "y" ] && log_warning_msg "${copytodev} is not a block device."
154             return 1
155         fi
156     fi
157     if [ "${freespace}" -lt "${size}" ] ; then
158         [ "$quiet" != "y" ] && log_warning_msg "Not enough free ${free_string} (${freespace}k > ${size}k) to copy live media in ${copytodev}."
159         return 1
160     fi
161
162     # begin copying..
163     mkdir "${copyto}"
164     echo "mount -t ${fstype} ${mount_options} ${dev} ${copyto}"
165     mount -t "${fstype}" ${mount_options} "${dev}" "${copyto}"
166     cp -a ${copyfrom}/* ${copyto} # "cp -a" from busybox also copies hidden files
167     umount ${copyfrom}
168     mount -r -o move ${copyto} ${copyfrom}
169     rmdir ${copyto}
170     return 0
171 }
172
173 add_ip_to_resolv_conf ()
174 {
175         if [ ! -z ${1} ]
176         then
177                 echo "nameserver $1" >> /etc/resolv.conf
178         fi
179 }
180
181 setup_static_ip ()
182 {
183         ifconfig lo 127.0.0.1 up
184         ifconfig "${DEVICE}" up
185         local myip=`echo ${STATICIP} | cut -d: -f1`
186         local mynm=`echo ${STATICIP} | cut -d: -f4`
187         local mygw=`echo ${STATICIP} | cut -d: -f3`
188         echo "myip=${myip} mynm=${mynm} mygw=${mygw}"
189         ifconfig "${DEVICE}" "${myip}" netmask "${mynm}"  
190         ifconfig "${DEVICE}"
191         route add default gw "${mygw}" 
192         return 0
193 }
194
195 setup_static_dns ()
196 {
197         if [ -z ${DNSIP} ]
198         then
199                 add_ip_to_resolv_conf "4.2.2.2" 
200                 add_ip_to_resolv_conf "128.255.1.3" 
201         else
202                 for x in $( echo "${DNS}" | sed 's/,/ /g')
203                 do
204                         add_ip_to_resolv_conf ${x} 
205                 done
206         fi
207         return 0
208 }
209
210 do_netmount() {
211     rc=1
212
213     
214     modprobe "${MP_QUIET}" af_packet # For DHCP
215
216     /sbin/udevadm trigger
217     /sbin/udevadm settle
218
219
220         if [ -z ${BOOTIP} ] 
221         then    
222                 ipconfig ${DEVICE} /tmp/net-${DEVICE}.conf | tee /netboot.config
223                 
224                 # Adding DNS Entry
225                 local ipconf_dnsip=`cat /netboot.config | grep dns0 | cut -d':' -f3 | cut -d' ' -f2`
226                 if [ -z ${ipconf_dnsip}] || [ ${ipconf_dnsip} == '0.0.0.0']
227                 then
228                         setup_static_dns
229                 else
230                         add_ip_to_resolv_conf ${ipconf_dnsip}
231                 fi
232                 # source relevant ipconfig output
233                 OLDHOSTNAME=${HOSTNAME}
234                 . /tmp/net-${DEVICE}.conf
235                 [ -z ${HOSTNAME} ] && HOSTNAME=${OLDHOSTNAME}
236                 export HOSTNAME
237
238         else
239                 setup_static_ip 
240                 setup_static_dns
241         fi
242
243
244     if [ "${NFSROOT}" = "auto" ]; then
245         NFSROOT=${ROOTSERVER}:${ROOTPATH}
246     fi
247     log_begin_msg "inside netboot, will use ${NETBOOT} and httproot as ${HTTPFS}"
248
249     [ "$quiet" != "y" ] && log_begin_msg "Trying netboot from ${NFSROOT}"
250
251     if [ "${NETBOOT}" == "http" ] && do_httpfsmount ; then
252         rc=0
253     elif [ "${NETBOOT}" == "iscsi" ] && do_iscsifsmount ; then
254         rc=0
255     elif [ "${NETBOOT}" != "nfs" ] && do_cifsmount ; then
256         rc=0
257     elif do_nfsmount ; then
258         NETBOOT="nfs"
259         export NETBOOT
260         rc=0
261     fi
262
263     [ "$quiet" != "y" ] && log_end_msg
264
265     return ${rc}
266 }
267
268 do_nfsmount() {
269     rc=1
270     modprobe "${MP_QUIET}" nfs
271     if [ -z "${NFSOPTS}" ]; then
272         NFSOPTS=""
273     fi
274
275     [ "$quiet" != "y" ] && log_begin_msg "Trying nfsmount -o nolock -o ro ${NFSOPTS} ${NFSROOT} ${mountpoint}"
276     # FIXME: This while loop is an ugly HACK round an nfs bug
277     i=0
278     while [ "$i" -lt 60 ]; do
279         nfsmount -o nolock -o ro ${NFSOPTS} "${NFSROOT}" "${mountpoint}" && rc=0 && break
280         sleep 1
281         i="$(($i + 1))"
282     done
283     return ${rc}
284 }
285
286 do_iscsifsmount ()
287 {
288         rc=1
289
290     echo "inside iscsimount" >> /mylogs 
291             
292     insmod /modules/libcrc32c.ko
293     insmod /modules/crc32c.ko
294     modprobe scsi-transport-iscsi
295     modprobe libiscsi
296     modprobe iscsi_tcp
297     mkdir /var/run
298     /sbin/iscsid
299     sleep 4
300     iscsiadm -m discovery -t st -p "${ISCSIFS}" >> /mylogs
301     sleep 4
302     targetname=`iscsiadm -m node | grep "${TARGET}" | cut -d' ' -f2`
303     iscsiadm -m node --targetname "${targetname}" --login >> /mylogs
304     sync
305         i=0
306 #   iscsiadm -m session -P 3 | grep "attached scsi disk"
307     export iscsidevice=/dev/disk/by-label/Ubuntu\\x209.04\\x20i386
308         while [ "$i" -lt 160 ]
309         do
310         ls -l "${iscsidevice}" && break
311                 i="$(($i + 1))"
312         echo "waiting for iscsi disk to appear $i" 
313                 sleep 1
314         sync
315         done
316    mount -t iso9660 "${iscsidevice}" "${mountpoint}"
317     rc=${?}
318     sync
319     sleep 2
320     rc=0
321     return $rc
322 }
323
324
325 do_httpfsmount() {
326     # objectives: 
327     # Enable networking.
328     # Setup networking.
329     # Find the location from which we need to get the ISO.
330     # Mount the ISO.
331     rc=1
332
333         echo  "passed on path is ${HTTPFS}" >> status
334         ISO_PATH="${HTTPFS}"
335         echo "The location of ubuntu iso  is $ISO_PATH" >> /output
336         echo "mounting CDROM" >> /output
337
338     issquash=`echo "$ISO_PATH" | grep "filesystem.squashfs"`
339     mytest=${?}
340     if [ "${mytest}" = "0" ]
341     then 
342         mkdir -p ${mountpoint}/casper/
343             httpfs $ISO_PATH ${mountpoint}/casper/
344     else
345
346         mkdir /iso
347             httpfs $ISO_PATH /iso
348             FILEPATH=`ls /iso/*`
349             echo  "File path is $FILEPATH\n" >> /output
350             losetup -r /dev/loop0 $FILEPATH
351             mount -t iso9660 $FILEPATH "${mountpoint}" -o loop -o ro
352         fi
353
354         # test if knoppix is there
355         if test -d /cdrom/casper
356         then
357                 echo "successfully mounted the Image" >> /output
358                 return 0
359         fi
360         return ${rc}
361
362 }
363
364
365 do_cifsmount() {
366     rc=1
367     if [ -x "/sbin/mount.cifs" ]; then
368         if [ -z "${NFSOPTS}" ]; then
369             CIFSOPTS="-ouser=root,password="
370         else
371             CIFSOPTS="${NFSOPTS}"
372         fi
373
374         [ "$quiet" != "y" ] && log_begin_msg "Trying mount.cifs ${NFSROOT} ${mountpoint} ${CIFSOPTS}"
375         modprobe "${MP_QUIET}" cifs
376
377         if mount.cifs "${NFSROOT}" "${mountpoint}" "${CIFSOPTS}" ; then
378             rc=0
379         fi
380     fi
381     return ${rc}
382 }
383
384 do_snap_copy ()
385 {
386     fromdev="${1}"
387     todir="${2}"
388     snap_type="${3}"
389
390     size=$(fs_size "${fromdev}" "" "used")
391
392     if [ -b "${fromdev}" ]; then
393         # look for free mem
394         if [ -n "${HOMEMOUNTED}" -a "${snap_type}" = "HOME" ]; then
395             todev=$(cat /proc/mounts | grep -s " $(base_path ${todir}) " | awk '{print $1}' )
396             freespace=$(df -k  | grep -s ${todev} | awk '{print $4}')
397         else
398             freespace=$( expr $(awk '/MemFree/{print $2}' /proc/meminfo) + $( cat /proc/meminfo | grep Cached | head -n 1 | awk '/Cached/{print $2}' - ))
399         fi
400
401         tomount="/mnt/tmpsnap"
402         if [ ! -d "${tomount}" ] ; then
403             mkdir -p "${tomount}"
404         fi
405
406         fstype=$(get_fstype "${fromdev}")
407         if [ -n "${fstype}" ]; then
408             # Copying stuff...
409             mount -t "${fstype}" -o ro,noatime "${fromdev}" "${tomount}"
410             cp -a "${tomount}"/* ${todir}
411             umount "${tomount}"
412         else
413             log_warning_msg "Unrecognized fstype: ${fstype} on ${fromdev}:${snap_type}"
414         fi
415
416         rmdir "${tomount}"
417         if echo ${fromdev} | grep -qs loop; then
418            losetup -d "${fromdev}"
419         fi
420         return 0
421     else
422         return 1
423         [ "$quiet" != "y" ] && log_warning_msg "Unable to find the snapshot ${snap_type} medium"
424     fi
425 }
426
427 try_snap ()
428 {
429     # Look for $snap_label.* in block devices and copy the contents to $snap_mount
430     #   and remember the device and filename for resync on exit in casper.init
431
432     snap_label="${1}"
433     snap_mount="${2}"
434     snap_type="${3}"
435
436     snapdata=$(find_files "${snap_label}.squashfs ${snap_label}.cpio.gz ${snap_label}.ext2")
437     if [ ! -z "${snapdata}" ]; then
438         snapdev="$(echo ${snapdata} | cut -f1 -d ' ')"
439         snapback="$(echo ${snapdata} | cut -f2 -d ' ')"
440         snapfile="$(echo ${snapdata} | cut -f3 -d ' ')"
441         if echo "${snapfile}" | grep -qs '\(squashfs\|ext2\)'; then
442             # squashfs or ext2 snapshot
443             dev=$(get_backing_device "${snapback}/${snapfile}")
444             if ! do_snap_copy "${dev}" "${snap_mount}" "${snap_type}"; then
445                  log_warning_msg "Impossible to include the ${snapfile} Snapshot"
446                  return 1
447             fi
448         else
449             # cpio.gz snapshot
450             if ! (cd "${snap_mount}" && zcat "${snapback}/${snapfile}" | cpio -i -u -d 2>/dev/null) ; then
451                 log_warning_msg "Impossible to include the ${snapfile} Snapshot"
452                 return 1
453             fi
454         fi
455         umount "${snapback}"
456     else
457         dev=$(find_cow_device "${snap_label}")
458         if [ -b ${dev} ]; then
459             if echo "${dev}" | grep -qs loop; then
460                 # strange things happens, user confused?
461                 snaploop=$( losetup ${dev} | awk '{print $3}' | tr -d '()' )
462                 snapfile=$(basename ${snaploop})
463                 snapdev=$(cat /proc/mounts | awk '{print $2,$1}' | grep -es "^$( dirname ${snaploop} )" | cut -f2 -d ' ')
464             else
465                 snapdev="${dev}"
466             fi
467             if ! do_snap_copy "${dev}" "${snap_mount}" "${snap_type}" ; then
468                 log_warning_msg "Impossible to include the ${snap_label} Snapshot"
469                 return 1
470             else
471                 if [ -n "${snapfile}" ]; then
472                      # it was a loop device, user confused
473                      umount ${snapdev}
474                 fi
475             fi
476         else
477             log_warning_msg "Impossible to include the ${snap_label} Snapshot"
478             return 1
479         fi
480     fi
481     echo "export ${snap_type}SNAP="${snap_mount}":${snapdev}:${snapfile}" >> /etc/casper.conf # for resync on reboot/halt
482     return 0
483 }
484
485 setup_unionfs() {
486     image_directory="$1"
487     rootmnt="$2"
488
489     modprobe "${MP_QUIET}" -b ${UNIONFS}
490
491     # run-init can't deal with images in a subdir, but we're going to
492     # move all of these away before it runs anyway.  No, we're not,
493     # put them in / since move-mounting them into / breaks mono and
494     # some other apps.
495
496     croot="/"
497
498     # Let's just mount the read-only file systems first
499     rofsstring=""
500     rofslist=""
501     if [ "${NETBOOT}" = "nfs" ] ; then
502         roopt="nfsro" # go aroung a bug in nfs-unionfs locking
503     elif [ "${UNIONFS}" = "aufs" ]; then
504         roopt="rr"
505     else
506         roopt="ro"
507     fi
508
509     mkdir -p "${croot}"
510     for image_type in "ext2" "squashfs" "dir" ; do
511         for image in "${image_directory}"/*."${image_type}"; do
512             imagename=$(basename "${image}")
513             if [ -d "${image}" ]; then
514                 # it is a plain directory: do nothing
515                 rofsstring="${image}=${roopt}:${rofsstring}"
516                 rofslist="${image} ${rofslist}"
517             elif [ -f "${image}" ]; then
518                 backdev=$(get_backing_device "$image")
519                 fstype=$(get_fstype "${backdev}")
520                 if [ "${fstype}" = "unknown" ]; then
521                     panic "Unknown file system type on ${backdev} (${image})"
522                 fi
523                 mkdir -p "${croot}/${imagename}"
524                 mount -t "${fstype}" -o ro,noatime "${backdev}" "${croot}/${imagename}" || panic "Can not mount $backdev ($image) on ${croot}/${imagename}" && rofsstring="${croot}/${imagename}=${roopt}:${rofsstring}" && rofslist="${croot}/${imagename} ${rofslist}"
525             fi
526         done
527     done
528     rofsstring=${rofsstring%:}
529
530     mkdir -p /cow
531     cowdevice="tmpfs"
532     cow_fstype="tmpfs"
533     cow_mountopt="rw,noatime,mode=755"
534
535     # Looking for "${root_persistence}" device or file
536     if [ -n "${PERSISTENT}" ]; then
537         cowprobe=$(find_cow_device "${root_persistence}")
538         if [ -b "${cowprobe}" ]; then
539             cowdevice=${cowprobe}
540             cow_fstype=$(get_fstype "${cowprobe}")
541             cow_mountopt="rw,noatime"
542         else
543             [ "$quiet" != "y" ] && log_warning_msg "Unable to find the persistent medium"
544         fi
545     fi
546
547     mount -t ${cow_fstype} -o ${cow_mountopt} ${cowdevice} /cow || panic "Can not mount $cowdevice on /cow"
548
549     mount -t ${UNIONFS} -o noatime,dirs=/cow=rw:$rofsstring ${UNIONFS} "$rootmnt" || panic "${UNIONFS} mount failed"
550
551     # Adding other custom mounts
552     if [ -n "${PERSISTENT}" ]; then
553         # directly mount /home
554         # FIXME: add a custom mounts configurable system
555         homecow=$(find_cow_device "${home_persistence}" )
556         if [ -b "${homecow}" ]; then
557             mount -t $(get_fstype "${homecow}") -o rw,noatime "${homecow}" "${rootmnt}/home"
558             export HOMEMOUNTED=1 # used to proper calculate free space in do_snap_copy()
559         else
560             [ "$quiet" != "y" ] && log_warning_msg "Unable to find the persistent home medium"
561         fi
562         # Look for other snapshots to copy in
563         try_snap "${root_snapshot_label}" "${rootmnt}" "ROOT"
564         try_snap "${home_snapshot_label}" "${rootmnt}/home" "HOME"
565     fi
566
567     if [ -n "${SHOWMOUNTS}" ]; then
568         for d in ${rofslist}; do
569             mkdir -p "${rootmnt}/casper/${d##*/}"
570             case d in
571                 *.dir) # do nothing # mount -o bind "${d}" "${rootmnt}/casper/${d##*/}"
572                     ;;
573                 *) mount -o move "${d}" "${rootmnt}/casper/${d##*/}"
574                     ;;
575             esac
576         done
577         # shows cow fs on /cow for use by casper-snapshot
578         mkdir -p "${rootmnt}/cow"
579         mount -o bind /cow "${rootmnt}/cow"
580     fi
581
582     # move the first mount; no head in busybox-initramfs
583     for d in $(mount -t squashfs | cut -d\  -f 3); do
584         mkdir -p "${rootmnt}/rofs"
585         mount -o move "${d}" "${rootmnt}/rofs"
586         break
587     done
588 }
589
590 check_dev ()
591 {
592     sysdev="${1}"
593     devname="${2}"
594     skip_uuid_check="${3}"
595     if [ -z "${devname}" ]; then
596         devname=$(sys2dev "${sysdev}")
597     fi
598
599     if [ -d "${devname}" ]; then
600         mount -o bind "${devname}" $mountpoint || continue
601         if is_casper_path $mountpoint; then
602             echo $mountpoint
603             return 0
604         else
605             umount $mountpoint
606         fi
607     fi
608
609     if [ -n "${LIVEMEDIA_OFFSET}" ]; then
610         loopdevname=$(setup_loop "${devname}" "loop" "/sys/block/loop*" "${LIVEMEDIA_OFFSET}")
611         devname="${loopdevname}" 
612     fi
613
614     fstype=$(get_fstype "${devname}")
615     if is_supported_fs ${fstype}; then
616         mount -t ${fstype} -o ro,noatime "${devname}" $mountpoint || continue
617         if is_casper_path $mountpoint && \
618            ([ "$skip_uuid_check" ] || matches_uuid $mountpoint); then
619             echo $mountpoint
620             return 0
621         else
622             umount $mountpoint
623         fi
624     fi
625
626     if [ -n "${LIVEMEDIA_OFFSET}" ]; then
627         losetup -d "${loopdevname}"
628     fi
629     return 1
630 }
631
632 find_livefs() {
633     timeout="${1}"
634     # first look at the one specified in the command line
635     if [ ! -z "${LIVEMEDIA}" ]; then
636         if check_dev "null" "${LIVEMEDIA}" "skip_uuid_check"; then
637             return 0
638         fi
639     fi
640     # don't start autodetection before timeout has expired
641     if [ -n "${LIVEMEDIA_TIMEOUT}" ]; then
642         if [ "${timeout}" -lt "${LIVEMEDIA_TIMEOUT}" ]; then
643             return 1
644         fi
645     fi
646     # or do the scan of block devices
647     for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|fd)"); do
648         devname=$(sys2dev "${sysblock}")
649         fstype=$(get_fstype "${devname}")
650         if /lib/udev/cdrom_id ${devname} > /dev/null; then
651             if check_dev "null" "${devname}" ; then
652                 return 0
653             fi
654         elif is_nice_device "${sysblock}" ; then
655             for dev in $(subdevices "${sysblock}"); do
656                 if check_dev "${dev}" ; then
657                     return 0
658                 fi
659             done
660         elif [ "${fstype}" = "squashfs" -o \
661                 "${fstype}" = "ext4" -o \
662                 "${fstype}" = "ext3" -o \
663                 "${fstype}" = "ext2" ]; then
664             # This is an ugly hack situation, the block device has
665             # an image directly on it.  It's hopefully
666             # casper, so take it and run with it.
667             ln -s "${devname}" "${devname}.${fstype}"
668             echo "${devname}.${fstype}"
669             return 0
670         fi
671     done
672     return 1
673 }
674
675 set_usplash_timeout() {
676     if [ -x /sbin/usplash_write ]; then
677         /sbin/usplash_write "TIMEOUT 120"
678     fi
679 }
680
681 rescue_break_shell ()
682 {
683         if [ "${RESCUEBREAK}" = "Yes" ]; 
684         then
685                 echo "#################################################################"
686                 echo "#### giving rescue shell, you can execute any commands here #####"
687                 echo "#### When done, type exit, boot process will resume :-)     #####"
688                 echo "#################################################################"
689                 /bin/sh -i
690                 echo "#################################################################"
691                 echo "####            Resuming Boot Process                       #####"
692                 echo "#################################################################"
693         fi
694 }
695
696 mountroot() {
697     exec 6>&1
698     exec 7>&2
699     exec > casper.log
700     exec 2>&1
701
702     parse_cmdline
703
704     set_usplash_timeout
705     [ "$quiet" != "y" ] && log_begin_msg "Running /scripts/casper-premount"
706     run_scripts /scripts/casper-premount
707     [ "$quiet" != "y" ] && log_end_msg
708
709     # Needed here too because some things (*cough* udev *cough*)
710     # changes the timeout
711
712     set_usplash_timeout
713
714     if [ ! -z "${NETBOOT}" ]; then
715         if do_netmount ; then
716             livefs_root="${mountpoint}"
717         else
718             panic "Unable to find a live file system on the network"
719         fi
720     else
721         # Scan local devices for the image
722         i=0
723         while [ "$i" -lt 60 ]; do
724             livefs_root=$(find_livefs $i)
725             if [ "${livefs_root}" ]; then
726                 break
727             fi
728             sleep 1
729             i="$(($i + 1))"
730         done
731     fi
732
733     if [ "${RESCUEBREAK}" = "Yes" ]; then
734         rescue_break_shell
735     fi
736
737     if [ -z "${livefs_root}" ]; then
738         panic "Unable to find a medium containing a live file system"
739     fi
740
741     if [ "${TORAM}" ]; then
742         live_dest="ram"
743     elif [ "${TODISK}" ]; then
744         live_dest="${TODISK}"
745     fi
746     if [ "${live_dest}" ]; then
747         log_begin_msg "Copying live_media to ${live_dest}"
748         copy_live_to "${livefs_root}" "${live_dest}"
749         log_end_msg
750     fi
751
752     mount_images_in_directory "${livefs_root}" "${rootmnt}"
753
754     log_end_msg
755
756     maybe_break casper-bottom
757     [ "$quiet" != "y" ] && log_begin_msg "Running /scripts/casper-bottom"
758
759     run_scripts /scripts/casper-bottom
760     [ "$quiet" != "y" ] && log_end_msg
761
762     exec 1>&6 6>&-
763     exec 2>&7 7>&-
764     cp casper.log "${rootmnt}/var/log/"
765 }