Adding live-initramfs 1.91.6-1.
[live-boot-grml.git] / scripts / live
1 #!/bin/sh
2
3 # set -e
4
5 export PATH=/root/usr/bin:/root/usr/sbin:/root/bin:/root/sbin:/usr/bin:/usr/sbin:/bin:/sbin
6
7 echo "/root/lib" >> /etc/ld.so.conf
8 echo "/root/usr/lib" >> /etc/ld.so.conf
9
10 mountpoint=/live/image
11 LIVE_MEDIA_PATH="/live"
12
13 root_persistence="live-rw"
14 home_persistence="home-rw"
15 root_snapshot_label="live-sn"
16 home_snapshot_label="home-sn"
17
18 USERNAME=user
19 USERFULLNAME="Live user"
20 HOSTNAME=host
21 BUILD_SYSTEM=Custom
22
23 mkdir -p $mountpoint
24
25 [ -f /etc/live.conf ] && . /etc/live.conf
26 export USERNAME USERFULLNAME HOSTNAME BUILD_SYSTEM
27
28 . /scripts/live-helpers
29
30 if [ ! -f /live.vars ]; then
31     touch /live.vars
32 fi
33
34 parse_cmdline ()
35 {
36         PRESEEDS=
37
38         # looking for live-initramfs specifics options as kernel parameters
39
40         for x in $(cat /proc/cmdline)
41         do
42                 case $x in
43                         access=*)
44                                 ACCESS=${x#access=}
45                                 export ACCESS
46                                 ;;
47
48                         console=*)
49                                 DEFCONSOLE="${x#*=}"
50                                 export DEFCONFSOLE
51                                 ;;
52
53                         debug)
54                                 DEBUG="Yes"
55                                 export DEBUG
56
57                                 set -x
58                                 ;;
59
60                         hostname=*)
61                                 HOSTNAME=${x#hostname=}
62                                 LIVECONF="changed"
63                                 export HOSTNAME LIVECONF
64                                 ;;
65
66                         username=*)
67                                 USERNAME=${x#username=}
68                                 LIVECONF="changed"
69                                 export USERNAME LIVECONF
70                                 ;;
71
72                         userfullname=*)
73                                 USERFULLNAME=${x#userfullname=}
74                                 LIVECONF="changed"
75                                 export USERFULLNAME LIVECONF
76                                 ;;
77
78                         ip=*)
79                                 STATICIP=${x#ip=}
80
81                                 if [ -z "${STATICIP}" ]
82                                 then
83                                         STATICIP="frommedia"
84                                 fi
85
86                                 export STATICIP
87                                 ;;
88
89                         keyb=*|kbd-chooser/method=*)
90                                 KBD=${x#*=}
91                                 export KBD
92                                 ;;
93
94                         klayout=*|console-setup/layoutcode=*)
95                                 KLAYOUT=${x#*=}
96                                 export KLAYOUT
97                                 ;;
98
99                         kvariant=*|console-setup/variantcode=*)
100                                 KVARIANT=${x#*=}
101                                 export KVARIANT
102                                 ;;
103
104                         kmodel=*|console-setup/modelcode=*)
105                                 KMODEL=${x#*=}
106                                 export KMODEL
107                                 ;;
108
109                         koptions=*)
110                                 KOPTIONS=${x#koptions=}
111                                 export KOPTIONS
112                                 ;;
113
114                         live-getty)
115                                 LIVE_GETTY=1
116                                 export LIVE_GETTY
117                                 ;;
118
119                         live-media=*|bootfrom=*)
120                                 LIVE_MEDIA=${x#*=}
121                                 export LIVE_MEDIA
122                                 ;;
123
124                         live-media-encryption=*|encryption=*)
125                                 LIVE_MEDIA_ENCRYPTION=${x#*=}
126                                 export LIVE_MEDIA_ENCRYPTION
127                                 ;;
128
129                         live-media-offset=*)
130                                 LIVE_MEDIA_OFFSET=${x#live-media-offset=}
131                                 export LIVE_MEDIA_OFFSET
132                                 ;;
133
134                         live-media-path=*)
135                                 LIVE_MEDIA_PATH="${x#live-media-path=}"
136                                 export LIVE_MEDIA_PATH
137                                 ;;
138
139                         live-media-timeout=*)
140                                 LIVE_MEDIA_TIMEOUT=${x#live-media-timeout=}
141                                 export LIVE_MEDIA_TIMEOUT
142                                 ;;
143
144                         locale=*|debian-installer/locale=*)
145                                 LOCALE=${x#*=}
146                                 export LOCALE
147                                 ;;
148
149                         module=*)
150                                 MODULE=${x#module=}
151                                 export MODULE
152                                 ;;
153
154                         netboot=*)
155                                 NETBOOT=${x#netboot=}
156                                 export NETBOOT
157                                 ;;
158
159                         nfsopts=*)
160                                 NFSOPTS=${x#nfsopts=}
161                                 export NFSOPTS
162                                 ;;
163
164                         noautologin)
165                                 NOAUTOLOGIN="Yes"
166                                 export NOAUTOLOGIN
167                                 ;;
168
169                         noxautologin)
170                                 NOXAUTOLOGIN="Yes"
171                                 export NOXAUTOLOGIN
172                                 ;;
173
174                         nofastboot)
175                                 NOFASTBOOT="Yes"
176                                 export NOFASTBOOT
177                                 ;;
178
179                         nopersistent)
180                                 PERSISTENT=""
181                                 export PERSISTENT
182                                 ;;
183
184                         nosudo)
185                                 NOSUDO="Yes"
186                                 export NOSUDO
187                                 ;;
188
189                         noswap)
190                                 NOSWAP="Yes"
191                                 export NOSWAP
192                                 ;;
193
194                         persistent)
195                                 PERSISTENT="Yes"
196                                 export PERSISTENT
197                                 ;;
198
199                         preseed/file=*|file=*)
200                                 LOCATION="${x#*=}"
201                                 export LOCATION
202                                 ;;
203
204                         */*=*)
205                                 question="${x%%=*}"
206                                 value="${x#*=}"
207                                 PRESEEDS="${PRESEEDS}\"${question}=${value}\" "
208
209                                 export PRESEEDS
210                                 ;;
211
212                         showmounts)
213                                 SHOWMOUNTS="Yes"
214                                 export SHOWMOUNTS
215                                 ;;
216
217                         timezone=*)
218                                 TIMEZONE="${x#timezone=}"
219                                 export TIMEZONE
220                                 ;;
221
222                         todisk=*)
223                                 TODISK=${x#todisk=}
224                                 export TODISK
225                                 ;;
226
227                         toram)
228                                 TORAM="Yes"
229                                 export TORAM
230                                 ;;
231
232                         union=*)
233                                 UNIONTYPE="${x#union=}"
234                                 export UNIONTYPE
235                                 ;;
236
237                         utc=*)
238                                 UTC="${x#utc=}"
239                                 export UTC
240                                 ;;
241
242                         xdebconf)
243                                 XDEBCONF="Yes"
244                                 export XDEBCONF
245                                 ;;
246
247                         xvideomode=*)
248                                 XVIDEOMODE="${x#xvideomode=}"
249                                 export XVIDEOMODE
250                                 ;;
251                 esac
252         done
253
254         # sort of compatibility with netboot.h from linux docs
255         if [ -z "${NETBOOT}" ]
256         then
257                 if [ "${ROOT}" = "/dev/nfs" ]
258                 then
259                         NETBOOT="nfs"
260                         export NETBOOT
261                 elif [ "${ROOT}" = "/dev/cifs" ]
262                 then
263                         NETBOOT="cifs"
264                         export NETBOOT
265                 fi
266         fi
267
268         if [ -z "${MODULE}" ]
269         then
270                 MODULE="filesystem"
271                 export MODULE
272         fi
273
274         if [ -z "${UNIONTYPE}" ]
275         then
276                 UNIONTYPE="unionfs"
277                 export UNIONTYPE
278         fi
279 }
280
281 is_live_path()
282 {
283         DIRECTORY="${1}"
284
285         if [ -d "${DIRECTORY}"/"${LIVE_MEDIA_PATH}" ]
286         then
287                 for FILESYSTEM in squashfs ext2 ext3 xfs dir
288                 do
289                         if [ "`echo ${DIRECTORY}/${LIVE_MEDIA_PATH}/*.${FILESYSTEM}`" != "${DIRECTORY}/${LIVE_MEDIA_PATH}/*.${FILESYSTEM}" ]
290                         then
291                                 return 0
292                         fi
293                 done
294         fi
295
296         return 1
297 }
298
299 get_backing_device() {
300     case "$1" in
301         *.squashfs|*.ext2|*.ext3)
302             echo $(setup_loop "$1" "loop" "/sys/block/loop*" '0' "${LIVE_MEDIA_ENCRYPTION}")
303             ;;
304         *.dir)
305             echo "directory"
306             ;;
307         *)
308             panic "Unrecognized live filesystem: $1"
309             ;;
310     esac
311 }
312
313 match_files_in_dir() {
314     # Does any files match pattern $1 ?
315
316     local pattern="$1"
317     if [ "$(echo $pattern)" != "$pattern" ]; then
318         return 0
319     fi
320     return 1
321 }
322
323 mount_images_in_directory() {
324     directory="$1"
325     rootmnt="$2"
326     if match_files_in_dir "$directory/${LIVE_MEDIA_PATH}/*.squashfs" ||
327         match_files_in_dir "$directory/${LIVE_MEDIA_PATH}/*.ext2" ||
328         match_files_in_dir "$directory/${LIVE_MEDIA_PATH}/*.ext3" ||
329         match_files_in_dir "$directory/${LIVE_MEDIA_PATH}/*.dir"; then
330         setup_unionfs "$directory/${LIVE_MEDIA_PATH}" "$rootmnt"
331     else
332         :
333     fi
334 }
335
336 is_nice_device() {
337     sysfs_path="${1#/sys}"
338     if /lib/udev/path_id "${sysfs_path}" | grep -E -q "ID_PATH=(usb|pci-[^-]*-[ide|scsi|usb])"; then
339         return 0
340     fi
341     return 1
342 }
343
344 is_supported_fs () {
345     # FIXME: do something better like the scan of supported filesystems
346     fstype="${1}"
347     case ${fstype} in
348         vfat|iso9660|udf|ext2|ext3|ntfs)
349             return 0
350             ;;
351     esac
352     return 1
353 }
354
355 copy_live_to() {
356     copyfrom="${1}"
357     copytodev="${2}"
358     copyto="${copyfrom}_swap"
359
360     size=$(fs_size "" ${copyfrom} "used")
361
362     if [ "${copytodev}" = "ram" ]; then
363         # copying to ram:
364         freespace=$( expr $(awk '/MemFree/{print $2}' /proc/meminfo) + $( cat /proc/meminfo | grep Cached | head -n 1 | awk '/Cached/{print $2}' - ) )
365         mount_options="-o size=${size}k"
366         free_string="memory"
367         fstype="tmpfs"
368         dev="/dev/shm"
369     else
370         # it should be a writable block device
371         if [ -b "${copytodev}" ]; then
372             dev="${copytodev}"
373             free_string="space"
374             fstype=$(get_fstype "${dev}")
375             freespace=$(fs_size "${dev}")
376         else
377             [ "$quiet" != "y" ] && log_warning_msg "${copytodev} is not a block device."
378             return 1
379         fi
380     fi
381     if [ "${freespace}" -lt "${size}" ] ; then
382         [ "$quiet" != "y" ] && log_warning_msg "Not enough free ${free_string} (${freespace}k > ${size}k) to copy live media in ${copytodev}."
383         return 1
384     fi
385
386     # begin copying..
387     mkdir "${copyto}"
388     echo "mount -t ${fstype} ${mount_options} ${dev} ${copyto}"
389     mount -t "${fstype}" ${mount_options} "${dev}" "${copyto}"
390     cp -a ${copyfrom}/* ${copyto} # "cp -a" from busybox also copies hidden files
391     umount ${copyfrom}
392     mount -r --move ${copyto} ${copyfrom}
393     rmdir ${copyto}
394     return 0
395 }
396
397 do_netmount() {
398     rc=1
399
400     modprobe "${MP_QUIET}" af_packet # For DHCP
401
402     udevtrigger
403     udevsettle
404
405     ipconfig ${DEVICE} /tmp/net-${DEVICE}.conf | tee /netboot.config
406
407     if [ "${NFSROOT}" = "auto" ]; then
408         NFSROOT=${ROOTSERVER}:${ROOTPATH}
409     fi
410
411     # source relevant ipconfig output
412     . /tmp/net-${DEVICE}.conf
413     export HOSTNAME
414
415     if [ "${NFSROOT#*:}" = "$NFSROOT" ] && [ "$NETBOOT" != "cifs" ]; then
416         NFSROOT=${ROOTSERVER}:${NFSROOT}
417     fi
418
419     [ "$quiet" != "y" ] && log_begin_msg "Trying netboot from ${NFSROOT}"
420
421     if [ "${NETBOOT}" != "nfs" ] && do_cifsmount ; then
422         rc=0
423     elif do_nfsmount ; then
424         NETBOOT="nfs"
425         export NETBOOT
426         rc=0
427     fi
428
429     [ "$quiet" != "y" ] && log_end_msg
430     return ${rc}
431 }
432
433 do_nfsmount() {
434     rc=1
435     modprobe "${MP_QUIET}" nfs
436     if [ -z "${NFSOPTS}" ]; then
437         NFSOPTS=""
438     fi
439
440     [ "$quiet" != "y" ] && log_begin_msg "Trying nfsmount -o nolock -o ro ${NFSOPTS} ${NFSROOT} ${mountpoint}"
441     # FIXME: This for loop is an ugly HACK round an nfs bug
442     for i in 0 1 2 3 4 5 6 7 8 9 a b c d e f 10 11 12 13; do
443         nfsmount -o nolock -o ro ${NFSOPTS} "${NFSROOT}" "${mountpoint}" && rc=0 && break
444         sleep 1
445     done
446     return ${rc}
447 }
448
449 do_cifsmount() {
450     rc=1
451     if [ -x "/sbin/mount.cifs" ]; then
452         if [ -z "${NFSOPTS}" ]; then
453             CIFSOPTS="-ouser=root,password="
454         else
455             CIFSOPTS="${NFSOPTS}"
456         fi
457
458         [ "$quiet" != "y" ] && log_begin_msg "Trying mount.cifs ${NFSROOT} ${mountpoint} ${CIFSOPTS}"
459         modprobe "${MP_QUIET}" cifs
460
461         if mount.cifs "${NFSROOT}" "${mountpoint}" "${CIFSOPTS}" ; then
462             rc=0
463         fi
464     fi
465     return ${rc}
466 }
467
468 do_snap_copy ()
469 {
470     fromdev="${1}"
471     todir="${2}"
472     snap_type="${3}"
473
474     size=$(fs_size "${fromdev}" "" "used")
475
476     if [ -b "${fromdev}" ]; then
477         # look for free mem
478         if [ -n "${HOMEMOUNTED}" -a "${snap_type}" = "HOME" ]; then
479             todev=$(cat /proc/mounts | grep -s " $(base_path ${todir}) " | awk '{print $1}' )
480             freespace=$(df -k  | grep -s ${todev} | awk '{print $4}')
481         else
482             freespace=$( expr $(awk '/MemFree/{print $2}' /proc/meminfo) + $( cat /proc/meminfo | grep Cached | head -n 1 | awk '/Cached/{print $2}' - ))
483         fi
484
485         tomount="/mnt/tmpsnap"
486         if [ ! -d "${tomount}" ] ; then
487             mkdir -p "${tomount}"
488         fi
489
490         fstype=$(get_fstype "${fromdev}")
491         if [ -n "${fstype}" ]; then
492             # Copying stuff...
493             mount -t "${fstype}" -o ro "${fromdev}" "${tomount}"
494             cp -a "${tomount}"/* ${todir}
495             umount "${tomount}"
496         else
497             log_warning_msg "Unrecognized fstype: ${fstype} on ${fromdev}:${snap_type}"
498         fi
499
500         rmdir "${tomount}"
501         if echo ${fromdev} | grep -qs loop; then
502            losetup -d "${fromdev}"
503         fi
504         return 0
505     else
506         return 1
507         [ "$quiet" != "y" ] && log_warning_msg "Unable to find the snapshot ${snap_type} medium"
508     fi
509 }
510
511 try_snap ()
512 {
513     # Look for $snap_label.* in block devices and copy the contents to $snap_mount
514     #   and remember the device and filename for resync on exit in live-initramfs.init
515
516     snap_label="${1}"
517     snap_mount="${2}"
518     snap_type="${3}"
519
520     snapdata=$(find_files "${snap_label}.squashfs ${snap_label}.cpio.gz ${snap_label}.ext2 ${snap_label}.ext3")
521     if [ ! -z "${snapdata}" ]; then
522         snapdev="$(echo ${snapdata} | cut -f1 -d ' ')"
523         snapback="$(echo ${snapdata} | cut -f2 -d ' ')"
524         snapfile="$(echo ${snapdata} | cut -f3 -d ' ')"
525         if echo "${snapfile}" | grep -qs '\(squashfs\|ext2\|ext3\)'; then
526             # squashfs or ext2/ext3 snapshot
527             dev=$(get_backing_device "${snapback}/${snapfile}")
528             if ! do_snap_copy "${dev}" "${snap_mount}" "${snap_type}"; then
529                  log_warning_msg "Impossible to include the ${snapfile} Snapshot"
530                  return 1
531             fi
532         else
533             # cpio.gz snapshot
534             if ! (cd "${snap_mount}" && zcat "${snapback}/${snapfile}" | cpio -i -u -d 2>/dev/null) ; then
535                 log_warning_msg "Impossible to include the ${snapfile} Snapshot"
536                 return 1
537             fi
538         fi
539         umount "${snapback}"
540     else
541         dev=$(find_cow_device "${snap_label}")
542         if [ -b ${dev} ]; then
543             if echo "${dev}" | grep -qs loop; then
544                 # strange things happens, user confused?
545                 snaploop=$( losetup ${dev} | awk '{print $3}' | tr -d '()' )
546                 snapfile=$(basename ${snaploop})
547                 snapdev=$(cat /proc/mounts | awk '{print $2,$1}' | grep -es "^$( dirname ${snaploop} )" | cut -f2 -d ' ')
548             else
549                 snapdev="${dev}"
550             fi
551             if ! do_snap_copy "${dev}" "${snap_mount}" "${snap_type}" ; then
552                 log_warning_msg "Impossible to include the ${snap_label} Snapshot"
553                 return 1
554             else
555                 if [ -n "${snapfile}" ]; then
556                      # it was a loop device, user confused
557                      umount ${snapdev}
558                 fi
559             fi
560         else
561             log_warning_msg "Impossible to include the ${snap_label} Snapshot"
562             return 1
563         fi
564     fi
565     echo "export ${snap_type}SNAP="${snap_mount}":${snapdev}:${snapfile}" >> /etc/live.conf # for resync on reboot/halt
566     return 0
567 }
568
569 setup_unionfs() {
570     image_directory="$1"
571     rootmnt="$2"
572
573     modprobe "${MP_QUIET}" -b ${UNIONTYPE}
574
575     # run-init can't deal with images in a subdir, but we're going to
576     # move all of these away before it runs anyway.  No, we're not,
577     # put them in / since move-mounting them into / breaks mono and
578     # some other apps.
579
580     croot="/"
581
582     # Let's just mount the read-only file systems first
583     rofsstring=""
584     rofslist=""
585     if [ "${NETBOOT}" = "nfs" ] ; then
586         roopt="nfsro" # go aroung a bug in nfs-unionfs locking
587     else
588         roopt="ro"
589     fi
590
591         # Read image names from ${MODULE}.module if it exists
592         if [ -e "${image_directory}/filesystem.${MODULE}.module" ]
593         then
594                 for IMAGE in `cat ${image_directory}/filesystem.${MODULE}.module`
595                 do
596                         image_string="${image_string} ${image_directory}/${image}"
597                 done
598         elif [ -e "${image_directory}/${MODULE}.module" ]
599         then
600                 for IMAGE in `cat ${image_directory}/${MODULE}.module`
601                 do
602                         image_string="${image_string} ${image_directory}/${image}"
603                 done
604         else
605                 # ${MODULE}.module does not exist, create a list of images
606                 for FILESYSTEM in squashfs ext2 ext3 xfs dir
607                 do
608                         for IMAGE in "${image_directory}"/*."${FILESYSTEM}"
609                         do
610                                 if [ -e "${IMAGE}" ]
611                                 then
612                                         image_string="${image_string} ${IMAGE}"
613                                 fi
614                         done
615                 done
616
617                 # Now sort the list
618                 image_string="`echo ${image_string} | sed -e 's/ /\n/g' | sort `"
619         fi
620
621     mkdir -p "${croot}"
622     for image in ${image_string}; do
623         imagename=$(basename "${image}")
624         if [ -d "${image}" ]; then
625             # it is a plain directory: do nothing
626             rofsstring="${image}=${roopt}:${rofsstring}"
627             rofslist="${image} ${rofslist}"
628         elif [ -f "${image}" ]; then
629             backdev=$(get_backing_device "$image")
630             fstype=$(get_fstype "${backdev}")
631             if [ "${fstype}" = "unknown" ]; then
632                 panic "Unknown file system type on ${backdev} (${image})"
633              fi
634             mkdir -p "${croot}/${imagename}"
635             mount -t "${fstype}" -o ro "${backdev}" "${croot}/${imagename}" || panic "Can not mount $backdev ($image) on ${croot}/${imagename}" && rofsstring="${croot}/${imagename}=${roopt}:${rofsstring}" && rofslist="${croot}/${imagename} ${rofslist}"
636         fi
637     done
638     rofsstring=${rofsstring%:}
639
640     mkdir -p /cow
641     cowdevice="tmpfs"
642     cow_fstype="tmpfs"
643
644     # Looking for "${root_persistence}" device or file
645     if [ -n "${PERSISTENT}" ]; then
646         cowprobe=$(find_cow_device "${root_persistence}")
647         if [ -b "${cowprobe}" ]; then
648             cowdevice=${cowprobe}
649             cow_fstype=$(get_fstype "${cowprobe}")
650         else
651             [ "$quiet" != "y" ] && log_warning_msg "Unable to find the persistent medium"
652         fi
653     fi
654
655     mount ${cowdevice} -t ${cow_fstype} -o rw /cow || panic "Can not mount $cowdevice on /cow"
656
657     mount -t ${UNIONTYPE} -o dirs=/cow=rw:$rofsstring ${UNIONTYPE} "$rootmnt" || panic "${UNIONTYPE} mount failed"
658
659     # Adding other custom mounts
660     if [ -n "${PERSISTENT}" ]; then
661         # directly mount /home
662         # FIXME: add a custom mounts configurable system
663         homecow=$(find_cow_device "${home_persistence}" )
664         if [ -b "${homecow}" ]; then
665             mount -t $(get_fstype "${homecow}") -o rw "${homecow}" "${rootmnt}/home"
666             export HOMEMOUNTED=1 # used to proper calculate free space in do_snap_copy()
667         else
668             [ "$quiet" != "y" ] && log_warning_msg "Unable to find the persistent home medium"
669         fi
670         # Look for other snapshots to copy in
671         try_snap "${root_snapshot_label}" "${rootmnt}" "ROOT"
672         try_snap "${home_snapshot_label}" "${rootmnt}/home" "HOME"
673     fi
674
675     if [ -n "${SHOWMOUNTS}" ]; then
676         for d in ${rofslist}; do
677             mkdir -p "${rootmnt}/live/${d##*/}"
678             case d in
679                 *.dir) # do nothing # mount -o bind "${d}" "${rootmnt}/live/${d##*/}"
680                     ;;
681                 *) mount --move "${d}" "${rootmnt}/live/${d##*/}"
682                     ;;
683             esac
684         done
685     fi
686
687     # shows cow fs on /cow for use by live-snapshot
688     mkdir -p "${rootmnt}/live/cow"
689     mount -o bind /cow "${rootmnt}/live/cow"
690 }
691
692 check_dev ()
693 {
694     sysdev="${1}"
695     devname="${2}"
696     if [ -z "${devname}" ]; then
697         devname=$(sys2dev "${sysdev}")
698     fi
699
700     if [ -n "${LIVE_MEDIA_OFFSET}" ]; then
701         loopdevname=$(setup_loop "${devname}" "loop" "/sys/block/loop*" "${LIVE_MEDIA_OFFSET}" '')
702         devname="${loopdevname}"
703     fi
704
705     fstype=$(get_fstype "${devname}")
706     if is_supported_fs ${fstype}; then
707         mount -t ${fstype} -o ro "${devname}" $mountpoint || continue
708         if is_live_path $mountpoint; then
709             echo $mountpoint
710             return 0
711         else
712             umount $mountpoint
713         fi
714     fi
715
716     if [ -n "${LIVE_MEDIA_OFFSET}" ]; then
717         losetup -d "${loopdevname}"
718     fi
719     return 1
720 }
721
722 find_livefs() {
723     timeout="${1}"
724     # first look at the one specified in the command line
725     if [ ! -z "${LIVE_MEDIA}" ]; then
726         if check_dev "null" "${LIVE_MEDIA}"; then
727             return 0
728         fi
729     fi
730     # don't start autodetection before timeout has expired
731     if [ -n "${LIVE_MEDIA_TIMEOUT}" ]; then
732         if [ "${timeout}" -lt "${LIVE_MEDIA_TIMEOUT}" ]; then
733             return 1
734         fi
735     fi
736     # or do the scan of block devices
737     for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram); do
738         devname=$(sys2dev "${sysblock}")
739         fstype=$(get_fstype "${devname}")
740         if /lib/udev/cdrom_id ${devname} > /dev/null; then
741             if check_dev "null" "${devname}" ; then
742                 return 0
743             fi
744         elif is_nice_device "${sysblock}" ; then
745             for dev in $(subdevices "${sysblock}"); do
746                 if check_dev "${dev}" ; then
747                     return 0
748                 fi
749             done
750         elif [ "${fstype}" = "squashfs" -o \
751                 "${fstype}" = "ext2" -o \
752                 "${fstype}" = "ext3" ]; then
753             # This is an ugly hack situation, the block device has
754             # an image directly on it.  It's hopefully
755             # live-initramfs, so take it and run with it.
756             ln -s "${devname}" "${devname}.${fstype}"
757             echo "${devname}.${fstype}"
758             return 0
759         fi
760     done
761     return 1
762 }
763
764 pulsate() {
765     if [ -x /sbin/usplash_write ]; then
766         /sbin/usplash_write "PULSATE"
767     fi
768 }
769
770 set_usplash_timeout() {
771     if [ -x /sbin/usplash_write ]; then
772         /sbin/usplash_write "TIMEOUT 120"
773     fi
774 }
775
776 mountroot() {
777     exec 6>&1
778     exec 7>&2
779     exec > live.log
780     exec 2>&1
781
782     parse_cmdline
783
784     set_usplash_timeout
785     [ "$quiet" != "y" ] && log_begin_msg "Running /scripts/live-premount"
786     pulsate
787     run_scripts /scripts/live-premount
788     [ "$quiet" != "y" ] && log_end_msg
789
790     # Needed here too because some things (*cough* udev *cough*)
791     # changes the timeout
792
793     set_usplash_timeout
794
795     if [ ! -z "${NETBOOT}" ]; then
796         if do_netmount ; then
797             livefs_root="${mountpoint}"
798         else
799             panic "Unable to find a live file system on the network"
800         fi
801     else
802         # Scan local devices for the image
803         for i in 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19; do
804             livefs_root=$(find_livefs $i)
805             if [ -n "${livefs_root}" ]; then
806                 break
807             fi
808             sleep 1
809         done
810     fi
811
812     if [ -z "${livefs_root}" ]; then
813         panic "Unable to find a medium containing a live file system"
814     fi
815
816     if [ "${TORAM}" ]; then
817         live_dest="ram"
818     elif [ "${TODISK}" ]; then
819         live_dest="${TODISK}"
820     fi
821     if [ "${live_dest}" ]; then
822         log_begin_msg "Copying live media to ${live_dest}"
823         copy_live_to "${livefs_root}" "${live_dest}"
824         log_end_msg
825     fi
826
827     mount_images_in_directory "${livefs_root}" "${rootmnt}"
828
829     log_end_msg
830
831     maybe_break live-bottom
832     [ "$quiet" != "y" ] && log_begin_msg "Running /scripts/live-bottom"
833
834     pulsate
835     run_scripts /scripts/live-bottom
836     [ "$quiet" != "y" ] && log_end_msg
837
838     exec 1>&6 6>&-
839     exec 2>&7 7>&-
840     cp live.log "${rootmnt}/var/log/"
841 }