Fixed DNS issues with ubuntu-9.10
[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 add_ip_to_resolv_conf ()
180 {
181         if [ ! -z ${1} ]
182         then
183                 echo "nameserver $1" >> /etc/resolv.conf
184         fi
185 }
186
187 setup_static_ip ()
188 {
189         ifconfig lo 127.0.0.1 up
190         ifconfig "${DEVICE}" up
191         local myip=`echo ${STATICIP} | cut -d: -f1`
192         local mynm=`echo ${STATICIP} | cut -d: -f4`
193         local mygw=`echo ${STATICIP} | cut -d: -f3`
194         echo "myip=${myip} mynm=${mynm} mygw=${mygw}"
195         ifconfig "${DEVICE}" "${myip}" netmask "${mynm}"  
196         ifconfig "${DEVICE}"
197         route add default gw "${mygw}" 
198         return 0
199 }
200
201 setup_static_dns ()
202 {
203         if [ -z ${DNSIP} ]
204         then
205                 add_ip_to_resolv_conf "4.2.2.2" 
206                 add_ip_to_resolv_conf "128.255.1.3" 
207         else
208                 for x in $( echo "${DNS}" | sed 's/,/ /g')
209                 do
210                         add_ip_to_resolv_conf ${x} 
211                 done
212         fi
213         return 0
214 }
215
216
217 do_netmount() {
218     rc=1
219
220     modprobe "${MP_QUIET}" af_packet # For DHCP
221
222     /sbin/udevadm trigger
223     /sbin/udevadm settle
224
225         if [ -z ${BOOTIP} ] 
226         then    
227                 ipconfig ${DEVICE} /tmp/net-${DEVICE}.conf | tee /netboot.config
228                 
229                 # Adding DNS Entry
230                 local ipconf_dnsip=`cat /netboot.config | grep dns0 | cut -d':' -f3 | cut -d' ' -f2`
231                 if [ -z ${ipconf_dnsip}] || [ ${ipconf_dnsip} == '0.0.0.0']
232                 then
233                         setup_static_dns
234                 else
235                         add_ip_to_resolv_conf ${ipconf_dnsip}
236                 fi
237                 # source relevant ipconfig output
238                 OLDHOSTNAME=${HOSTNAME}
239                 . /tmp/net-${DEVICE}.conf
240                 [ -z ${HOSTNAME} ] && HOSTNAME=${OLDHOSTNAME}
241                 export HOSTNAME
242
243         else
244                 setup_static_ip 
245                 setup_static_dns
246         fi
247
248
249     if [ "${NFSROOT}" = "auto" ]; then
250         NFSROOT=${ROOTSERVER}:${ROOTPATH}
251     fi
252
253     [ "$quiet" != "y" ] && log_begin_msg "Trying netboot from ${NFSROOT}"
254
255     if [ "${NETBOOT}" == "http" ] && do_httpfsmount ; then
256          rc=0
257     elif [ "${NETBOOT}" == "iscsi" ] && do_iscsifsmount ; then
258          rc=0
259     elif [ "${NETBOOT}" != "nfs" ] && do_cifsmount ; then
260         rc=0
261     elif do_nfsmount ; then
262         NETBOOT="nfs"
263         export NETBOOT
264         rc=0
265     fi
266
267     [ "$quiet" != "y" ] && log_end_msg
268     return ${rc}
269 }
270
271
272
273 do_nfsmount() {
274     rc=1
275     modprobe "${MP_QUIET}" nfs
276     if [ -z "${NFSOPTS}" ]; then
277         NFSOPTS=""
278     fi
279
280     [ "$quiet" != "y" ] && log_begin_msg "Trying nfsmount -o nolock -o ro ${NFSOPTS} ${NFSROOT} ${mountpoint}"
281     # FIXME: This while loop is an ugly HACK round an nfs bug
282     i=0
283     while [ "$i" -lt 60 ]; do
284         nfsmount -o nolock -o ro ${NFSOPTS} "${NFSROOT}" "${mountpoint}" && rc=0 && break
285         sleep 1
286         i="$(($i + 1))"
287     done
288     return ${rc}
289 }
290
291 do_iscsifsmount ()
292 {
293         rc=1
294
295     echo "inside iscsimount" >> /mylogs 
296             
297     #insmod /modules/libcrc32c.ko
298     #insmod /modules/crc32c.ko
299     modprobe scsi-transport-iscsi
300     modprobe libiscsi
301     modprobe iscsi_tcp
302     mkdir /var/run
303     /sbin/iscsid
304     sleep 4
305     iscsiadm -m discovery -t st -p "${ISCSIFS}" >> /mylogs
306     sleep 4
307     targetname=`iscsiadm -m node | grep "${TARGET}" | cut -d' ' -f2`
308     iscsiadm -m node --targetname "${targetname}" --login >> /mylogs
309     sync
310         i=0
311 #   iscsiadm -m session -P 3 | grep "attached scsi disk"
312     export iscsidevice=/dev/disk/by-label/Ubuntu\\x209.10\\x20i386
313         while [ "$i" -lt 160 ]
314         do
315         ls -l "${iscsidevice}" && break
316                 i="$(($i + 1))"
317         echo "waiting for iscsi disk to appear $i" 
318                 sleep 1
319         sync
320         done
321    mount -t iso9660 "${iscsidevice}" "${mountpoint}"
322     rc=${?}
323     sync
324     sleep 2
325     rc=0
326     return $rc
327 }
328
329 do_httpfsmount() {
330     # objectives:
331     # Enable networking.
332     # Setup networking.
333     # Find the location from which we need to get the ISO.
334     # Mount the ISO.
335     rc=1
336
337         log_begin_msg "############# inside HTTPFS #########"
338         log_end_msg
339
340         echo "#################################"
341         echo  "passed on path is ${HTTPFS}" >> status
342         ISO_PATH="${HTTPFS}"
343         echo "The location of ubuntu iso  is $ISO_PATH" >> /output
344         echo "mounting CDROM" >> /output
345
346     issquash=`echo "$ISO_PATH" | grep "filesystem.squashfs"`
347     mytest=${?}
348     if [ "${mytest}" = "0" ]
349     then
350         mkdir -p ${mountpoint}/casper/
351             httpfs $ISO_PATH ${mountpoint}/casper/
352     else
353
354         mkdir /iso
355             httpfs $ISO_PATH /iso
356             FILEPATH=`ls /iso/*`
357             echo  "File path is $FILEPATH\n" >> /output
358             losetup -r /dev/loop0 $FILEPATH
359             mount -t iso9660 $FILEPATH "${mountpoint}" -o loop -o ro
360         fi
361
362         # test if knoppix is there
363         if test -d /cdrom/casper
364         then
365                 echo "successfully mounted the Image" >> /output
366                 #return 0
367                 rc=0
368         fi
369 #               /bin/sh
370         return ${rc}
371
372 }
373
374
375 do_cifsmount() {
376     rc=1
377     if [ -x "/sbin/mount.cifs" ]; then
378         if [ -z "${NFSOPTS}" ]; then
379             CIFSOPTS="-ouser=root,password="
380         else
381             CIFSOPTS="${NFSOPTS}"
382         fi
383
384         [ "$quiet" != "y" ] && log_begin_msg "Trying mount.cifs ${NFSROOT} ${mountpoint} ${CIFSOPTS}"
385         modprobe "${MP_QUIET}" cifs
386
387         if mount.cifs "${NFSROOT}" "${mountpoint}" "${CIFSOPTS}" ; then
388             rc=0
389         fi
390     fi
391     return ${rc}
392 }
393
394 do_snap_copy ()
395 {
396     fromdev="${1}"
397     todir="${2}"
398     snap_type="${3}"
399
400     size=$(fs_size "${fromdev}" "" "used")
401
402     if [ -b "${fromdev}" ]; then
403         # look for free mem
404         if [ -n "${HOMEMOUNTED}" -a "${snap_type}" = "HOME" ]; then
405             todev=$(cat /proc/mounts | grep -s " $(base_path ${todir}) " | awk '{print $1}' )
406             freespace=$(df -k  | grep -s ${todev} | awk '{print $4}')
407         else
408             freespace=$( expr $(awk '/MemFree/{print $2}' /proc/meminfo) + $( cat /proc/meminfo | grep Cached | head -n 1 | awk '/Cached/{print $2}' - ))
409         fi
410
411         tomount="/mnt/tmpsnap"
412         if [ ! -d "${tomount}" ] ; then
413             mkdir -p "${tomount}"
414         fi
415
416         fstype=$(get_fstype "${fromdev}")
417         if [ -n "${fstype}" ]; then
418             # Copying stuff...
419             mount -t "${fstype}" -o ro,noatime "${fromdev}" "${tomount}"
420             cp -a "${tomount}"/* ${todir}
421             umount "${tomount}"
422         else
423             log_warning_msg "Unrecognized fstype: ${fstype} on ${fromdev}:${snap_type}"
424         fi
425
426         rmdir "${tomount}"
427         if echo ${fromdev} | grep -qs loop; then
428            losetup -d "${fromdev}"
429         fi
430         return 0
431     else
432         return 1
433         [ "$quiet" != "y" ] && log_warning_msg "Unable to find the snapshot ${snap_type} medium"
434     fi
435 }
436
437 try_snap ()
438 {
439     # Look for $snap_label.* in block devices and copy the contents to $snap_mount
440     #   and remember the device and filename for resync on exit in casper.init
441
442     snap_label="${1}"
443     snap_mount="${2}"
444     snap_type="${3}"
445
446     snapdata=$(find_files "${snap_label}.squashfs ${snap_label}.cpio.gz ${snap_label}.ext2")
447     if [ ! -z "${snapdata}" ]; then
448         snapdev="$(echo ${snapdata} | cut -f1 -d ' ')"
449         snapback="$(echo ${snapdata} | cut -f2 -d ' ')"
450         snapfile="$(echo ${snapdata} | cut -f3 -d ' ')"
451         if echo "${snapfile}" | grep -qs '\(squashfs\|ext2\)'; then
452             # squashfs or ext2 snapshot
453             dev=$(get_backing_device "${snapback}/${snapfile}")
454             if ! do_snap_copy "${dev}" "${snap_mount}" "${snap_type}"; then
455                  log_warning_msg "Impossible to include the ${snapfile} Snapshot"
456                  return 1
457             fi
458         else
459             # cpio.gz snapshot
460             # Unfortunately klibc's cpio is incompatible with the rest of
461             # the world; everything else requires -u -d, while klibc doesn't
462             # implement them. Try to detect whether it's in use.
463             cpiopath="$(which cpio)" || true
464             if [ "$cpiopath" ] && grep -aq /lib/klibc "$cpiopath"; then
465                 cpioargs=
466             else
467                 cpioargs='-u -d'
468             fi
469             if ! (cd "${snap_mount}" && zcat "${snapback}/${snapfile}" | cpio -i $cpioargs 2>/dev/null) ; then
470                 log_warning_msg "Impossible to include the ${snapfile} Snapshot"
471                 return 1
472             fi
473         fi
474         umount "${snapback}"
475     else
476         dev=$(find_cow_device "${snap_label}")
477         if [ -b "${dev}" ]; then
478             if echo "${dev}" | grep -qs loop; then
479                 # strange things happens, user confused?
480                 snaploop=$( losetup ${dev} | awk '{print $3}' | tr -d '()' )
481                 snapfile=$(basename ${snaploop})
482                 snapdev=$(cat /proc/mounts | awk '{print $2,$1}' | grep -es "^$( dirname ${snaploop} )" | cut -f2 -d ' ')
483             else
484                 snapdev="${dev}"
485             fi
486             if ! do_snap_copy "${dev}" "${snap_mount}" "${snap_type}" ; then
487                 log_warning_msg "Impossible to include the ${snap_label} Snapshot"
488                 return 1
489             else
490                 if [ -n "${snapfile}" ]; then
491                      # it was a loop device, user confused
492                      umount ${snapdev}
493                 fi
494             fi
495         else
496             log_warning_msg "Impossible to include the ${snap_label} Snapshot"
497             return 1
498         fi
499     fi
500     echo "export ${snap_type}SNAP="/cow${snap_mount#$rootmnt}":${snapdev}:${snapfile}" >> /etc/casper.conf # for resync on reboot/halt
501     return 0
502 }
503
504 setup_unionfs() {
505     image_directory="$1"
506     rootmnt="$2"
507
508     case ${UNIONFS} in
509         aufs|unionfs)
510             modprobe "${MP_QUIET}" -b ${UNIONFS} || true
511             if ! cut -f2 /proc/filesystems | grep -q "^${UNIONFS}\$" && \
512                [ -x /bin/unionfs-fuse ]; then
513                 UNIONFS="unionfs-fuse"
514             fi
515             ;;
516     esac
517
518     # run-init can't deal with images in a subdir, but we're going to
519     # move all of these away before it runs anyway.  No, we're not,
520     # put them in / since move-mounting them into / breaks mono and
521     # some other apps.
522
523     croot="/"
524
525     # Let's just mount the read-only file systems first
526     rofsstring=""
527     rofslist=""
528     if [ "${NETBOOT}" = "nfs" ] ; then
529         roopt="nfsro" # work around a bug in nfs-unionfs locking
530     elif [ "${UNIONFS}" = "aufs" ]; then
531         roopt="rr"
532     elif [ "${UNIONFS}" = "unionfs-fuse" ]; then
533         roopt="RO"
534     else
535         roopt="ro"
536     fi
537
538     mkdir -p "${croot}"
539     for image_type in "ext2" "squashfs" "dir" ; do
540         for image in "${image_directory}"/*."${image_type}"; do
541             imagename=$(basename "${image}")
542             if [ -d "${image}" ]; then
543                 # it is a plain directory: do nothing
544                 rofsstring="${image}=${roopt}:${rofsstring}"
545                 rofslist="${image} ${rofslist}"
546             elif [ -f "${image}" ]; then
547                 backdev=$(get_backing_device "$image")
548                 fstype=$(get_fstype "${backdev}")
549                 if [ "${fstype}" = "unknown" ]; then
550                     panic "Unknown file system type on ${backdev} (${image})"
551                 fi
552                 mkdir -p "${croot}/${imagename}"
553                 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}"
554             fi
555         done
556     done
557     rofsstring=${rofsstring%:}
558
559     mkdir -p /cow
560     cowdevice="tmpfs"
561     cow_fstype="tmpfs"
562     cow_mountopt="rw,noatime,mode=755"
563
564     # Looking for "${root_persistence}" device or file
565     if [ -n "${PERSISTENT}" ]; then
566         cowprobe=$(find_cow_device "${root_persistence}")
567         if [ -b "${cowprobe}" ]; then
568             cowdevice=${cowprobe}
569             cow_fstype=$(get_fstype "${cowprobe}")
570             cow_mountopt="rw,noatime"
571         else
572             [ "$quiet" != "y" ] && log_warning_msg "Unable to find the persistent medium"
573         fi
574     fi
575
576     mount -t ${cow_fstype} -o ${cow_mountopt} ${cowdevice} /cow || panic "Can not mount $cowdevice on /cow"
577
578     case ${UNIONFS} in
579         unionfs-fuse)
580             (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")
581             mkdir -p /dev/.initramfs/varrun
582             pidof unionfs-fuse >> /dev/.initramfs/varrun/sendsigs.omit || true
583             ;;
584         *)
585             mount -t ${UNIONFS} -o noatime,dirs=/cow=rw:$rofsstring ${UNIONFS} "$rootmnt" || panic "${UNIONFS} mount failed"
586             ;;
587     esac
588
589     # Adding other custom mounts
590     if [ -n "${PERSISTENT}" ]; then
591         # directly mount /home
592         # FIXME: add a custom mounts configurable system
593         homecow=$(find_cow_device "${home_persistence}" )
594         if [ -b "${homecow}" ]; then
595             mount -t $(get_fstype "${homecow}") -o rw,noatime "${homecow}" "${rootmnt}/home"
596             export HOMEMOUNTED=1 # used to proper calculate free space in do_snap_copy()
597         else
598             [ "$quiet" != "y" ] && log_warning_msg "Unable to find the persistent home medium"
599         fi
600         # Look for other snapshots to copy in
601         try_snap "${root_snapshot_label}" "${rootmnt}" "ROOT"
602         try_snap "${home_snapshot_label}" "${rootmnt}/home" "HOME"
603     fi
604
605     if [ -n "${SHOWMOUNTS}" ]; then
606         for d in ${rofslist}; do
607             mkdir -p "${rootmnt}/${LIVE_MEDIA_PATH}/${d##*/}"
608             case d in
609                 *.dir) # do nothing # mount -o bind "${d}" "${rootmnt}/${LIVE_MEDIA_PATH}/${d##*/}"
610                     ;;
611                 *)
612                     if [ "${UNIONFS}" = unionfs-fuse ]; then
613                         mount -o bind "${d}" "${rootmnt}/${LIVE_MEDIA_PATH}/${d##*/}"
614                     else
615                         mount -o move "${d}" "${rootmnt}/${LIVE_MEDIA_PATH}/${d##*/}"
616                     fi
617                     ;;
618             esac
619         done
620         # shows cow fs on /cow for use by casper-snapshot
621         mkdir -p "${rootmnt}/cow"
622         mount -o bind /cow "${rootmnt}/cow"
623     fi
624
625     # move the first mount; no head in busybox-initramfs
626     for d in $(mount -t squashfs | cut -d\  -f 3); do
627         mkdir -p "${rootmnt}/rofs"
628         if [ "${UNIONFS}" = unionfs-fuse ]; then
629             mount -o bind "${d}" "${rootmnt}/rofs"
630         else
631             mount -o move "${d}" "${rootmnt}/rofs"
632         fi
633         break
634     done
635 }
636
637 check_dev ()
638 {
639     sysdev="${1}"
640     devname="${2}"
641     skip_uuid_check="${3}"
642     if [ -z "${devname}" ]; then
643         devname=$(sys2dev "${sysdev}")
644     fi
645
646     if [ -d "${devname}" ]; then
647         mount -o bind "${devname}" $mountpoint || continue
648         if is_casper_path $mountpoint; then
649             echo $mountpoint
650             return 0
651         else
652             umount $mountpoint
653         fi
654     fi
655     [ -e "$devname" ] || continue
656
657     if [ -n "${LIVEMEDIA_OFFSET}" ]; then
658         loopdevname=$(setup_loop "${devname}" "loop" "/sys/block/loop*" "${LIVEMEDIA_OFFSET}")
659         devname="${loopdevname}" 
660     fi
661
662     fstype=$(get_fstype "${devname}")
663     if is_supported_fs ${fstype}; then
664         devuid=$(blkid -o value -s UUID "$devname")
665         [ -n "$devuid" ] && grep -qs "\<$devuid\>" $tried && continue
666         mount -t ${fstype} -o ro,noatime "${devname}" $mountpoint || continue
667         [ -n "$devuid" ] && echo "$devuid" >> $tried
668         if is_casper_path $mountpoint && \
669            ([ "$skip_uuid_check" ] || matches_uuid $mountpoint); then
670             echo $mountpoint
671             return 0
672         else
673             umount $mountpoint
674         fi
675     fi
676
677     if [ -n "${LIVEMEDIA_OFFSET}" ]; then
678         losetup -d "${loopdevname}"
679     fi
680     return 1
681 }
682
683 find_livefs() {
684     timeout="${1}"
685     # first look at the one specified in the command line
686     if [ ! -z "${LIVEMEDIA}" ]; then
687         if check_dev "null" "${LIVEMEDIA}" "skip_uuid_check"; then
688             return 0
689         fi
690     fi
691     # don't start autodetection before timeout has expired
692     if [ -n "${LIVEMEDIA_TIMEOUT}" ]; then
693         if [ "${timeout}" -lt "${LIVEMEDIA_TIMEOUT}" ]; then
694             return 1
695         fi
696     fi
697     # or do the scan of block devices
698     for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|fd)"); do
699         devname=$(sys2dev "${sysblock}")
700         [ -e "$devname" ] || continue
701         fstype=$(get_fstype "${devname}")
702         if /lib/udev/cdrom_id ${devname} > /dev/null; then
703             if check_dev "null" "${devname}" ; then
704                 return 0
705             fi
706         elif is_nice_device "${sysblock}" ; then
707             for dev in $(subdevices "${sysblock}"); do
708                 if check_dev "${dev}" ; then
709                     return 0
710                 fi
711             done
712         elif [ "${fstype}" = "squashfs" -o \
713                 "${fstype}" = "ext4" -o \
714                 "${fstype}" = "ext3" -o \
715                 "${fstype}" = "ext2" ]; then
716             # This is an ugly hack situation, the block device has
717             # an image directly on it.  It's hopefully
718             # casper, so take it and run with it.
719             ln -s "${devname}" "${devname}.${fstype}"
720             echo "${devname}.${fstype}"
721             return 0
722         fi
723     done
724     return 1
725 }
726
727 set_usplash_timeout() {
728     if [ -x /sbin/usplash_write ]; then
729         /sbin/usplash_write "TIMEOUT 120"
730     fi
731 }
732
733 start_usplash_pulse() {
734     if [ -x /sbin/usplash_write ]; then
735         /sbin/usplash_write "PULSELOGO"
736     fi
737 }
738
739 mountroot() {
740     exec 6>&1
741     exec 7>&2
742     exec > casper.log
743     exec 2>&1
744     tail -f casper.log >&7 &
745     tailpid="$!"
746
747     parse_cmdline
748
749     set_usplash_timeout
750     start_usplash_pulse
751     [ "$quiet" != "y" ] && log_begin_msg "Running /scripts/casper-premount"
752     run_scripts /scripts/casper-premount
753     [ "$quiet" != "y" ] && log_end_msg
754
755     # Needed here too because some things (*cough* udev *cough*)
756     # changes the timeout
757
758     set_usplash_timeout
759
760     if [ ! -z "${NETBOOT}" ]; then
761         if do_netmount ; then
762             livefs_root="${mountpoint}"
763         else
764             panic "Unable to find a live file system on the network"
765         fi
766     else
767         # Scan local devices for the image
768         i=0
769         while [ "$i" -lt 60 ]; do
770             livefs_root=$(find_livefs $i)
771             if [ "${livefs_root}" ]; then
772                 break
773             fi
774             sleep 1
775             i="$(($i + 1))"
776         done
777     fi
778
779     if [ -z "${livefs_root}" ]; then
780         panic "Unable to find a medium containing a live file system"
781     fi
782
783     if [ "${TORAM}" ]; then
784         live_dest="ram"
785     elif [ "${TODISK}" ]; then
786         live_dest="${TODISK}"
787     fi
788     if [ "${live_dest}" ]; then
789         log_begin_msg "Copying live_media to ${live_dest}"
790         copy_live_to "${livefs_root}" "${live_dest}"
791         log_end_msg
792     fi
793
794     mount_images_in_directory "${livefs_root}" "${rootmnt}"
795
796     log_end_msg
797
798     # unionfs-fuse needs /dev to be bind-mounted for the duration of
799     # casper-bottom; udev's init script will take care of things after that
800     if [ "${UNIONFS}" = unionfs-fuse ]; then
801         mount -n -o bind /dev "${rootmnt}/dev"
802     fi
803
804     maybe_break casper-bottom
805     [ "$quiet" != "y" ] && log_begin_msg "Running /scripts/casper-bottom"
806
807     run_scripts /scripts/casper-bottom
808     [ "$quiet" != "y" ] && log_end_msg
809
810     if [ "${UNIONFS}" = unionfs-fuse ]; then
811         umount "${rootmnt}/dev"
812     fi
813
814 # This may still cause trouble with installing off of the livecd, but at least this will be more obvious
815 echo "iface eth0 inet manual #httpfs" >> "${rootmnt}/etc/network/interfaces"
816     exec 1>&6 6>&-
817     exec 2>&7 7>&-
818     kill "$tailpid"
819     cp casper.log "${rootmnt}/var/log/"
820     if [ -f /etc/casper.conf ]; then
821         cp /etc/casper.conf "${rootmnt}/etc/"
822     fi
823 }