Adding live-initramfs 1.95.1-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                         url=*)
205                                 LOCATION="${x#url=}"
206
207                                 mount -n -o bind /sys /root/sys
208                                 mount -n -o bind /proc /root/proc
209                                 mount -n -o bind /dev /root/dev
210
211                                 mkdir -p /root/var/run/network
212                                 chroot /root ifup -a
213                                 chroot /root wget -P /tmp "$location"
214                                 chroot /root ifdown -a
215
216                                 umount /root/sys
217                                 umount /root/proc
218                                 umount /root/dev
219
220                                 LOCATION="/tmp/$(basename "$location")"
221                                 ;;
222
223                         */*=*)
224                                 question="${x%%=*}"
225                                 value="${x#*=}"
226                                 PRESEEDS="${PRESEEDS}\"${question}=${value}\" "
227
228                                 export PRESEEDS
229                                 ;;
230
231                         showmounts)
232                                 SHOWMOUNTS="Yes"
233                                 export SHOWMOUNTS
234                                 ;;
235
236                         timezone=*)
237                                 TIMEZONE="${x#timezone=}"
238                                 export TIMEZONE
239                                 ;;
240
241                         todisk=*)
242                                 TODISK=${x#todisk=}
243                                 export TODISK
244                                 ;;
245
246                         toram)
247                                 TORAM="Yes"
248                                 export TORAM
249                                 ;;
250
251                         union=*)
252                                 UNIONTYPE="${x#union=}"
253                                 export UNIONTYPE
254                                 ;;
255
256                         utc=*)
257                                 UTC="${x#utc=}"
258                                 export UTC
259                                 ;;
260
261                         xdebconf)
262                                 XDEBCONF="Yes"
263                                 export XDEBCONF
264                                 ;;
265
266                         xvideomode=*)
267                                 XVIDEOMODE="${x#xvideomode=}"
268                                 export XVIDEOMODE
269                                 ;;
270                 esac
271         done
272
273         # sort of compatibility with netboot.h from linux docs
274         if [ -z "${NETBOOT}" ]
275         then
276                 if [ "${ROOT}" = "/dev/nfs" ]
277                 then
278                         NETBOOT="nfs"
279                         export NETBOOT
280                 elif [ "${ROOT}" = "/dev/cifs" ]
281                 then
282                         NETBOOT="cifs"
283                         export NETBOOT
284                 fi
285         fi
286
287         if [ -z "${MODULE}" ]
288         then
289                 MODULE="filesystem"
290                 export MODULE
291         fi
292
293         if [ -z "${UNIONTYPE}" ]
294         then
295                 UNIONTYPE="unionfs"
296                 export UNIONTYPE
297         fi
298 }
299
300 is_live_path()
301 {
302         DIRECTORY="${1}"
303
304         if [ -d "${DIRECTORY}"/"${LIVE_MEDIA_PATH}" ]
305         then
306                 for FILESYSTEM in squashfs ext2 ext3 xfs dir
307                 do
308                         if [ "`echo ${DIRECTORY}/${LIVE_MEDIA_PATH}/*.${FILESYSTEM}`" != "${DIRECTORY}/${LIVE_MEDIA_PATH}/*.${FILESYSTEM}" ]
309                         then
310                                 return 0
311                         fi
312                 done
313         fi
314
315         return 1
316 }
317
318 get_backing_device() {
319     case "$1" in
320         *.squashfs|*.ext2|*.ext3)
321             echo $(setup_loop "$1" "loop" "/sys/block/loop*" '0' "${LIVE_MEDIA_ENCRYPTION}")
322             ;;
323         *.dir)
324             echo "directory"
325             ;;
326         *)
327             panic "Unrecognized live filesystem: $1"
328             ;;
329     esac
330 }
331
332 match_files_in_dir() {
333     # Does any files match pattern $1 ?
334
335     local pattern="$1"
336     if [ "$(echo $pattern)" != "$pattern" ]; then
337         return 0
338     fi
339     return 1
340 }
341
342 mount_images_in_directory() {
343     directory="$1"
344     rootmnt="$2"
345     if match_files_in_dir "$directory/${LIVE_MEDIA_PATH}/*.squashfs" ||
346         match_files_in_dir "$directory/${LIVE_MEDIA_PATH}/*.ext2" ||
347         match_files_in_dir "$directory/${LIVE_MEDIA_PATH}/*.ext3" ||
348         match_files_in_dir "$directory/${LIVE_MEDIA_PATH}/*.dir"; then
349         setup_unionfs "$directory/${LIVE_MEDIA_PATH}" "$rootmnt"
350     else
351         :
352     fi
353 }
354
355 is_nice_device() {
356     sysfs_path="${1#/sys}"
357     if /lib/udev/path_id "${sysfs_path}" | grep -E -q "ID_PATH=(usb|pci-[^-]*-[ide|scsi|usb])"; then
358         return 0
359     fi
360     return 1
361 }
362
363 is_supported_fs () {
364     # FIXME: do something better like the scan of supported filesystems
365     fstype="${1}"
366     case ${fstype} in
367         vfat|iso9660|udf|ext2|ext3|ntfs)
368             return 0
369             ;;
370     esac
371     return 1
372 }
373
374 copy_live_to() {
375     copyfrom="${1}"
376     copytodev="${2}"
377     copyto="${copyfrom}_swap"
378
379     size=$(fs_size "" ${copyfrom} "used")
380
381     if [ "${copytodev}" = "ram" ]; then
382         # copying to ram:
383         freespace=$( expr $(awk '/MemFree/{print $2}' /proc/meminfo) + $( cat /proc/meminfo | grep Cached | head -n 1 | awk '/Cached/{print $2}' - ) )
384         mount_options="-o size=${size}k"
385         free_string="memory"
386         fstype="tmpfs"
387         dev="/dev/shm"
388     else
389         # it should be a writable block device
390         if [ -b "${copytodev}" ]; then
391             dev="${copytodev}"
392             free_string="space"
393             fstype=$(get_fstype "${dev}")
394             freespace=$(fs_size "${dev}")
395         else
396             [ "$quiet" != "y" ] && log_warning_msg "${copytodev} is not a block device."
397             return 1
398         fi
399     fi
400     if [ "${freespace}" -lt "${size}" ] ; then
401         [ "$quiet" != "y" ] && log_warning_msg "Not enough free ${free_string} (${freespace}k > ${size}k) to copy live media in ${copytodev}."
402         return 1
403     fi
404
405     # begin copying..
406     mkdir "${copyto}"
407     echo "mount -t ${fstype} ${mount_options} ${dev} ${copyto}"
408     mount -t "${fstype}" ${mount_options} "${dev}" "${copyto}"
409     cp -a ${copyfrom}/* ${copyto} # "cp -a" from busybox also copies hidden files
410     umount ${copyfrom}
411     mount -r --move ${copyto} ${copyfrom}
412     rmdir ${copyto}
413     return 0
414 }
415
416 do_netmount() {
417     rc=1
418
419     modprobe "${MP_QUIET}" af_packet # For DHCP
420
421     udevtrigger
422     udevsettle
423
424     ipconfig ${DEVICE} /tmp/net-${DEVICE}.conf | tee /netboot.config
425
426     if [ "${NFSROOT}" = "auto" ]; then
427         NFSROOT=${ROOTSERVER}:${ROOTPATH}
428     fi
429
430     # source relevant ipconfig output
431     . /tmp/net-${DEVICE}.conf
432     export HOSTNAME
433
434     if [ "${NFSROOT#*:}" = "$NFSROOT" ] && [ "$NETBOOT" != "cifs" ]; then
435         NFSROOT=${ROOTSERVER}:${NFSROOT}
436     fi
437
438     [ "$quiet" != "y" ] && log_begin_msg "Trying netboot from ${NFSROOT}"
439
440     if [ "${NETBOOT}" != "nfs" ] && do_cifsmount ; then
441         rc=0
442     elif do_nfsmount ; then
443         NETBOOT="nfs"
444         export NETBOOT
445         rc=0
446     fi
447
448     [ "$quiet" != "y" ] && log_end_msg
449     return ${rc}
450 }
451
452 do_nfsmount() {
453     rc=1
454     modprobe "${MP_QUIET}" nfs
455     if [ -z "${NFSOPTS}" ]; then
456         NFSOPTS=""
457     fi
458
459     [ "$quiet" != "y" ] && log_begin_msg "Trying nfsmount -o nolock -o ro ${NFSOPTS} ${NFSROOT} ${mountpoint}"
460     # FIXME: This for loop is an ugly HACK round an nfs bug
461     for i in 0 1 2 3 4 5 6 7 8 9 a b c d e f 10 11 12 13; do
462         nfsmount -o nolock -o ro ${NFSOPTS} "${NFSROOT}" "${mountpoint}" && rc=0 && break
463         sleep 1
464     done
465     return ${rc}
466 }
467
468 do_cifsmount() {
469     rc=1
470     if [ -x "/sbin/mount.cifs" ]; then
471         if [ -z "${NFSOPTS}" ]; then
472             CIFSOPTS="-ouser=root,password="
473         else
474             CIFSOPTS="${NFSOPTS}"
475         fi
476
477         [ "$quiet" != "y" ] && log_begin_msg "Trying mount.cifs ${NFSROOT} ${mountpoint} ${CIFSOPTS}"
478         modprobe "${MP_QUIET}" cifs
479
480         if mount.cifs "${NFSROOT}" "${mountpoint}" "${CIFSOPTS}" ; then
481             rc=0
482         fi
483     fi
484     return ${rc}
485 }
486
487 do_snap_copy ()
488 {
489     fromdev="${1}"
490     todir="${2}"
491     snap_type="${3}"
492
493     size=$(fs_size "${fromdev}" "" "used")
494
495     if [ -b "${fromdev}" ]; then
496         # look for free mem
497         if [ -n "${HOMEMOUNTED}" -a "${snap_type}" = "HOME" ]; then
498             todev=$(cat /proc/mounts | grep -s " $(base_path ${todir}) " | awk '{print $1}' )
499             freespace=$(df -k  | grep -s ${todev} | awk '{print $4}')
500         else
501             freespace=$( expr $(awk '/MemFree/{print $2}' /proc/meminfo) + $( cat /proc/meminfo | grep Cached | head -n 1 | awk '/Cached/{print $2}' - ))
502         fi
503
504         tomount="/mnt/tmpsnap"
505         if [ ! -d "${tomount}" ] ; then
506             mkdir -p "${tomount}"
507         fi
508
509         fstype=$(get_fstype "${fromdev}")
510         if [ -n "${fstype}" ]; then
511             # Copying stuff...
512             mount -t "${fstype}" -o ro "${fromdev}" "${tomount}"
513             cp -a "${tomount}"/* ${todir}
514             umount "${tomount}"
515         else
516             log_warning_msg "Unrecognized fstype: ${fstype} on ${fromdev}:${snap_type}"
517         fi
518
519         rmdir "${tomount}"
520         if echo ${fromdev} | grep -qs loop; then
521            losetup -d "${fromdev}"
522         fi
523         return 0
524     else
525         return 1
526         [ "$quiet" != "y" ] && log_warning_msg "Unable to find the snapshot ${snap_type} medium"
527     fi
528 }
529
530 try_snap ()
531 {
532     # Look for $snap_label.* in block devices and copy the contents to $snap_mount
533     #   and remember the device and filename for resync on exit in live-initramfs.init
534
535     snap_label="${1}"
536     snap_mount="${2}"
537     snap_type="${3}"
538
539     snapdata=$(find_files "${snap_label}.squashfs ${snap_label}.cpio.gz ${snap_label}.ext2 ${snap_label}.ext3")
540     if [ ! -z "${snapdata}" ]; then
541         snapdev="$(echo ${snapdata} | cut -f1 -d ' ')"
542         snapback="$(echo ${snapdata} | cut -f2 -d ' ')"
543         snapfile="$(echo ${snapdata} | cut -f3 -d ' ')"
544         if echo "${snapfile}" | grep -qs '\(squashfs\|ext2\|ext3\)'; then
545             # squashfs or ext2/ext3 snapshot
546             dev=$(get_backing_device "${snapback}/${snapfile}")
547             if ! do_snap_copy "${dev}" "${snap_mount}" "${snap_type}"; then
548                  log_warning_msg "Impossible to include the ${snapfile} Snapshot"
549                  return 1
550             fi
551         else
552             # cpio.gz snapshot
553             if ! (cd "${snap_mount}" && zcat "${snapback}/${snapfile}" | cpio -i -u -d 2>/dev/null) ; then
554                 log_warning_msg "Impossible to include the ${snapfile} Snapshot"
555                 return 1
556             fi
557         fi
558         umount "${snapback}"
559     else
560         dev=$(find_cow_device "${snap_label}")
561         if [ -b ${dev} ]; then
562             if echo "${dev}" | grep -qs loop; then
563                 # strange things happens, user confused?
564                 snaploop=$( losetup ${dev} | awk '{print $3}' | tr -d '()' )
565                 snapfile=$(basename ${snaploop})
566                 snapdev=$(cat /proc/mounts | awk '{print $2,$1}' | grep -es "^$( dirname ${snaploop} )" | cut -f2 -d ' ')
567             else
568                 snapdev="${dev}"
569             fi
570             if ! do_snap_copy "${dev}" "${snap_mount}" "${snap_type}" ; then
571                 log_warning_msg "Impossible to include the ${snap_label} Snapshot"
572                 return 1
573             else
574                 if [ -n "${snapfile}" ]; then
575                      # it was a loop device, user confused
576                      umount ${snapdev}
577                 fi
578             fi
579         else
580             log_warning_msg "Impossible to include the ${snap_label} Snapshot"
581             return 1
582         fi
583     fi
584     echo "export ${snap_type}SNAP="${snap_mount}":${snapdev}:${snapfile}" >> /etc/live.conf # for resync on reboot/halt
585     return 0
586 }
587
588 setup_unionfs() {
589     image_directory="$1"
590     rootmnt="$2"
591
592     modprobe "${MP_QUIET}" -b ${UNIONTYPE}
593
594     # run-init can't deal with images in a subdir, but we're going to
595     # move all of these away before it runs anyway.  No, we're not,
596     # put them in / since move-mounting them into / breaks mono and
597     # some other apps.
598
599     croot="/"
600
601     # Let's just mount the read-only file systems first
602     rofsstring=""
603     rofslist=""
604     if [ "${NETBOOT}" = "nfs" ] ; then
605         roopt="nfsro" # go aroung a bug in nfs-unionfs locking
606     else
607         roopt="ro"
608     fi
609
610         # Read image names from ${MODULE}.module if it exists
611         if [ -e "${image_directory}/filesystem.${MODULE}.module" ]
612         then
613                 for IMAGE in `cat ${image_directory}/filesystem.${MODULE}.module`
614                 do
615                         image_string="${image_string} ${image_directory}/${image}"
616                 done
617         elif [ -e "${image_directory}/${MODULE}.module" ]
618         then
619                 for IMAGE in `cat ${image_directory}/${MODULE}.module`
620                 do
621                         image_string="${image_string} ${image_directory}/${image}"
622                 done
623         else
624                 # ${MODULE}.module does not exist, create a list of images
625                 for FILESYSTEM in squashfs ext2 ext3 xfs dir
626                 do
627                         for IMAGE in "${image_directory}"/*."${FILESYSTEM}"
628                         do
629                                 if [ -e "${IMAGE}" ]
630                                 then
631                                         image_string="${image_string} ${IMAGE}"
632                                 fi
633                         done
634                 done
635
636                 # Now sort the list
637                 image_string="`echo ${image_string} | sed -e 's/ /\n/g' | sort `"
638         fi
639
640     mkdir -p "${croot}"
641     for image in ${image_string}; do
642         imagename=$(basename "${image}")
643         if [ -d "${image}" ]; then
644             # it is a plain directory: do nothing
645             rofsstring="${image}=${roopt}:${rofsstring}"
646             rofslist="${image} ${rofslist}"
647         elif [ -f "${image}" ]; then
648             backdev=$(get_backing_device "$image")
649             fstype=$(get_fstype "${backdev}")
650             if [ "${fstype}" = "unknown" ]; then
651                 panic "Unknown file system type on ${backdev} (${image})"
652              fi
653             mkdir -p "${croot}/${imagename}"
654             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}"
655         fi
656     done
657     rofsstring=${rofsstring%:}
658
659     mkdir -p /cow
660     cowdevice="tmpfs"
661     cow_fstype="tmpfs"
662
663     # Looking for "${root_persistence}" device or file
664     if [ -n "${PERSISTENT}" ]; then
665         cowprobe=$(find_cow_device "${root_persistence}")
666         if [ -b "${cowprobe}" ]; then
667             cowdevice=${cowprobe}
668             cow_fstype=$(get_fstype "${cowprobe}")
669         else
670             [ "$quiet" != "y" ] && log_warning_msg "Unable to find the persistent medium"
671         fi
672     fi
673
674     mount ${cowdevice} -t ${cow_fstype} -o rw /cow || panic "Can not mount $cowdevice on /cow"
675
676     mount -t ${UNIONTYPE} -o dirs=/cow=rw:$rofsstring ${UNIONTYPE} "$rootmnt" || panic "${UNIONTYPE} mount failed"
677
678     # Adding other custom mounts
679     if [ -n "${PERSISTENT}" ]; then
680         # directly mount /home
681         # FIXME: add a custom mounts configurable system
682         homecow=$(find_cow_device "${home_persistence}" )
683         if [ -b "${homecow}" ]; then
684             mount -t $(get_fstype "${homecow}") -o rw "${homecow}" "${rootmnt}/home"
685             export HOMEMOUNTED=1 # used to proper calculate free space in do_snap_copy()
686         else
687             [ "$quiet" != "y" ] && log_warning_msg "Unable to find the persistent home medium"
688         fi
689         # Look for other snapshots to copy in
690         try_snap "${root_snapshot_label}" "${rootmnt}" "ROOT"
691         try_snap "${home_snapshot_label}" "${rootmnt}/home" "HOME"
692     fi
693
694     if [ -n "${SHOWMOUNTS}" ]; then
695         for d in ${rofslist}; do
696             mkdir -p "${rootmnt}/live/${d##*/}"
697             case d in
698                 *.dir) # do nothing # mount -o bind "${d}" "${rootmnt}/live/${d##*/}"
699                     ;;
700                 *) mount --move "${d}" "${rootmnt}/live/${d##*/}"
701                     ;;
702             esac
703         done
704     fi
705
706     # shows cow fs on /cow for use by live-snapshot
707     mkdir -p "${rootmnt}/live/cow"
708     mount -o bind /cow "${rootmnt}/live/cow"
709 }
710
711 check_dev ()
712 {
713     sysdev="${1}"
714     devname="${2}"
715     if [ -z "${devname}" ]; then
716         devname=$(sys2dev "${sysdev}")
717     fi
718
719     if [ -n "${LIVE_MEDIA_OFFSET}" ]; then
720         loopdevname=$(setup_loop "${devname}" "loop" "/sys/block/loop*" "${LIVE_MEDIA_OFFSET}" '')
721         devname="${loopdevname}"
722     fi
723
724     fstype=$(get_fstype "${devname}")
725     if is_supported_fs ${fstype}; then
726         mount -t ${fstype} -o ro "${devname}" $mountpoint || continue
727         if is_live_path $mountpoint; then
728             echo $mountpoint
729             return 0
730         else
731             umount $mountpoint
732         fi
733     fi
734
735     if [ -n "${LIVE_MEDIA_OFFSET}" ]; then
736         losetup -d "${loopdevname}"
737     fi
738     return 1
739 }
740
741 find_livefs() {
742     timeout="${1}"
743     # first look at the one specified in the command line
744     if [ ! -z "${LIVE_MEDIA}" ]; then
745         if check_dev "null" "${LIVE_MEDIA}"; then
746             return 0
747         fi
748     fi
749     # don't start autodetection before timeout has expired
750     if [ -n "${LIVE_MEDIA_TIMEOUT}" ]; then
751         if [ "${timeout}" -lt "${LIVE_MEDIA_TIMEOUT}" ]; then
752             return 1
753         fi
754     fi
755     # or do the scan of block devices
756     for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram); do
757         devname=$(sys2dev "${sysblock}")
758         fstype=$(get_fstype "${devname}")
759         if /lib/udev/cdrom_id ${devname} > /dev/null; then
760             if check_dev "null" "${devname}" ; then
761                 return 0
762             fi
763         elif is_nice_device "${sysblock}" ; then
764             for dev in $(subdevices "${sysblock}"); do
765                 if check_dev "${dev}" ; then
766                     return 0
767                 fi
768             done
769         elif [ "${fstype}" = "squashfs" -o \
770                 "${fstype}" = "ext2" -o \
771                 "${fstype}" = "ext3" ]; then
772             # This is an ugly hack situation, the block device has
773             # an image directly on it.  It's hopefully
774             # live-initramfs, so take it and run with it.
775             ln -s "${devname}" "${devname}.${fstype}"
776             echo "${devname}.${fstype}"
777             return 0
778         fi
779     done
780     return 1
781 }
782
783 pulsate() {
784     if [ -x /sbin/usplash_write ]; then
785         /sbin/usplash_write "PULSATE"
786     fi
787 }
788
789 set_usplash_timeout() {
790     if [ -x /sbin/usplash_write ]; then
791         /sbin/usplash_write "TIMEOUT 120"
792     fi
793 }
794
795 mountroot() {
796     exec 6>&1
797     exec 7>&2
798     exec > live.log
799     exec 2>&1
800
801     parse_cmdline
802
803     set_usplash_timeout
804     [ "$quiet" != "y" ] && log_begin_msg "Running /scripts/live-premount"
805     pulsate
806     run_scripts /scripts/live-premount
807     [ "$quiet" != "y" ] && log_end_msg
808
809     # Needed here too because some things (*cough* udev *cough*)
810     # changes the timeout
811
812     set_usplash_timeout
813
814     if [ ! -z "${NETBOOT}" ]; then
815         if do_netmount ; then
816             livefs_root="${mountpoint}"
817         else
818             panic "Unable to find a live file system on the network"
819         fi
820     else
821         # Scan local devices for the image
822         for i in 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19; do
823             livefs_root=$(find_livefs $i)
824             if [ -n "${livefs_root}" ]; then
825                 break
826             fi
827             sleep 1
828         done
829     fi
830
831     if [ -z "${livefs_root}" ]; then
832         panic "Unable to find a medium containing a live file system"
833     fi
834
835     if [ "${TORAM}" ]; then
836         live_dest="ram"
837     elif [ "${TODISK}" ]; then
838         live_dest="${TODISK}"
839     fi
840     if [ "${live_dest}" ]; then
841         log_begin_msg "Copying live media to ${live_dest}"
842         copy_live_to "${livefs_root}" "${live_dest}"
843         log_end_msg
844     fi
845
846     mount_images_in_directory "${livefs_root}" "${rootmnt}"
847
848     log_end_msg
849
850     maybe_break live-bottom
851     [ "$quiet" != "y" ] && log_begin_msg "Running /scripts/live-bottom"
852
853     pulsate
854     run_scripts /scripts/live-bottom
855     [ "$quiet" != "y" ] && log_end_msg
856
857     exec 1>&6 6>&-
858     exec 2>&7 7>&-
859     cp live.log "${rootmnt}/var/log/"
860 }