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