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