9c747d5c4ab64456480082ac240c470220d2ef0c
[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
22 mkdir -p "${mountpoint}"
23
24 # Create /etc/mtab for debug purpose and future syncs
25 if [ ! -d /etc ]
26 then
27         mkdir /etc/
28 fi
29
30 if [ ! -f /etc/mtab ]
31 then
32         touch /etc/mtab
33 fi
34
35 [ -f /etc/live.conf ] && . /etc/live.conf
36 export USERNAME USERFULLNAME HOSTNAME
37
38 . /scripts/live-helpers
39
40 if [ ! -f /live.vars ]
41 then
42         touch /live.vars
43 fi
44
45 Arguments ()
46 {
47         PRESEEDS=""
48
49         for ARGUMENT in $(cat /proc/cmdline)
50         do
51                 case "${ARGUMENT}" in
52                         access=*)
53                                 ACCESS="${ARGUMENT#access=}"
54                                 export ACCESS
55                                 ;;
56
57                         console=*)
58                                 DEFCONSOLE="${ARGUMENT#*=}"
59                                 export DEFCONSOLE
60                                 ;;
61
62                         debug)
63                                 DEBUG="Yes"
64                                 export DEBUG
65
66                                 set -x
67                                 ;;
68
69                         fetch=*)
70                                 FETCH="${ARGUMENT#fetch=}"
71                                 export FETCH
72                                 ;;
73
74                         hook=*)
75                                 HOOK="${ARGUMENT#hook=}"
76                                 export HOOK
77                                 ;;
78
79                         ftpfs=*)
80                                 FTPFS="${ARGUMENT#ftpfs=}"
81                                 export FTPFS
82                                 ;;
83
84                         httpfs=*)
85                                 HTTPFS="${ARGUMENT#httpfs=}"
86                                 export HTTPFS
87                                 ;;
88
89                         hostname=*)
90                                 HOSTNAME="${ARGUMENT#hostname=}"
91                                 LIVECONF="changed"
92                                 export HOSTNAME LIVECONF
93                                 ;;
94
95                         username=*)
96                                 USERNAME="${ARGUMENT#username=}"
97                                 LIVECONF="changed"
98                                 export USERNAME LIVECONF
99                                 ;;
100
101                         userfullname=*)
102                                 USERFULLNAME="${ARGUMENT#userfullname=}"
103                                 LIVECONF="changed"
104                                 export USERFULLNAME LIVECONF
105                                 ;;
106
107                         ignore_uuid)
108                                 IGNORE_UUID="Yes"
109                                 export IGNORE_UUID
110                                 ;;
111
112                         ip=*)
113                                 STATICIP="${ARGUMENT#ip=}"
114
115                                 if [ -z "${STATICIP}" ]
116                                 then
117                                         STATICIP="frommedia"
118                                 fi
119
120                                 export STATICIP
121                                 ;;
122
123                         keyb=*|kbd-chooser/method=*)
124                                 KBD="${ARGUMENT#*=}"
125                                 export KBD
126                                 ;;
127
128                         klayout=*|console-setup/layoutcode=*)
129                                 KLAYOUT="${ARGUMENT#*=}"
130                                 export KLAYOUT
131                                 ;;
132
133                         kvariant=*|console-setup/variantcode=*)
134                                 KVARIANT="${ARGUMENT#*=}"
135                                 export KVARIANT
136                                 ;;
137
138                         kmodel=*|console-setup/modelcode=*)
139                                 KMODEL="${ARGUMENT#*=}"
140                                 export KMODEL
141                                 ;;
142
143                         koptions=*)
144                                 KOPTIONS="${ARGUMENT#koptions=}"
145                                 export KOPTIONS
146                                 ;;
147
148                         live-getty)
149                                 LIVE_GETTY="1"
150                                 export LIVE_GETTY
151                                 ;;
152
153                         live-media=*|bootfrom=*)
154                                 LIVE_MEDIA="${ARGUMENT#*=}"
155                                 export LIVE_MEDIA
156                                 ;;
157
158                         live-media-encryption=*|encryption=*)
159                                 LIVE_MEDIA_ENCRYPTION="${ARGUMENT#*=}"
160                                 export LIVE_MEDIA_ENCRYPTION
161                                 ;;
162
163                         live-media-offset=*)
164                                 LIVE_MEDIA_OFFSET="${ARGUMENT#live-media-offset=}"
165                                 export LIVE_MEDIA_OFFSET
166                                 ;;
167
168                         live-media-path=*)
169                                 LIVE_MEDIA_PATH="${ARGUMENT#live-media-path=}"
170                                 export LIVE_MEDIA_PATH
171                                 ;;
172
173                         live-media-timeout=*)
174                                 LIVE_MEDIA_TIMEOUT="${ARGUMENT#live-media-timeout=}"
175                                 export LIVE_MEDIA_TIMEOUT
176                                 ;;
177
178                         language=*|debian-installer/language=*)
179                                 language=${x#debian-installer/language=}
180                                 locale="$(lang2locale "$language")"
181                                 set_locale="true"
182                                 ;;
183
184                         locale=*|debian-installer/locale=*)
185                                 LOCALE="${ARGUMENT#*=}"
186                                 export LOCALE
187                                 ;;
188
189                         module=*)
190                                 MODULE="${ARGUMENT#module=}"
191                                 export MODULE
192                                 ;;
193
194                         netboot=*)
195                                 NETBOOT="${ARGUMENT#netboot=}"
196                                 export NETBOOT
197                                 ;;
198
199                         nfsopts=*)
200                                 NFSOPTS="${ARGUMENT#nfsopts=}"
201                                 export NFSOPTS
202                                 ;;
203
204                         nfscow=*)
205                                 NFS_COW="${ARGUMENT#nfscow=}"
206                                 export NFS_COW
207                                 ;;
208
209                         noaccessibility)
210                                 NOACCESSIBILITY="Yes"
211                                 export NOACCESSIBILITY
212                                 ;;
213
214                         noapparmor)
215                                 NOAPPARMOR="Yes"
216                                 export NOAPPARMOR
217                                 ;;
218
219                         noaptcdrom)
220                                 NOAPTCDROM="Yes"
221                                 export NOAPTCDROM
222                                 ;;
223
224                         noautologin)
225                                 NOAUTOLOGIN="Yes"
226                                 export NOAUTOLOGIN
227                                 ;;
228
229                         noxautologin)
230                                 NOXAUTOLOGIN="Yes"
231                                 export NOXAUTOLOGIN
232                                 ;;
233
234                         noconsolekeyboard)
235                                 NOCONSOLEKEYBOARD="Yes"
236                                 export NOCONSOLEKEYBOARD
237                                 ;;
238
239                         nofastboot)
240                                 NOFASTBOOT="Yes"
241                                 export NOFASTBOOT
242                                 ;;
243
244                         nofstab)
245                                 NOFSTAB="Yes"
246                                 export NOFSTAB
247                                 ;;
248
249                         nognomepanel)
250                                 NOGNOMEPANEL="Yes"
251                                 export NOGNOMEPANEL
252                                 ;;
253
254                         nohosts)
255                                 NOHOSTS="Yes"
256                                 export NOHOSTS
257                                 ;;
258
259                         nokpersonalizer)
260                                 NOKPERSONALIZER="Yes"
261                                 export NOKPERSONALIZER
262                                 ;;
263
264                         nolanguageselector)
265                                 NOLANGUAGESELECTOR="Yes"
266                                 export NOLANGUAGESELECTOR
267                                 ;;
268
269                         nolocales)
270                                 NOLOCALES="Yes"
271                                 export NOLOCALES
272                                 ;;
273
274                         nonetworking)
275                                 NONETWORKING="Yes"
276                                 export NONETWORKING
277                                 ;;
278
279                         nopowermanagement)
280                                 NOPOWERMANAGEMENT="Yes"
281                                 export NOPOWERMANAGEMENT
282                                 ;;
283
284                         noprogramcrashes)
285                                 NOPROGRAMCRASHES="Yes"
286                                 export NOPROGRAMCRASHES
287                                 ;;
288
289                         norestrictedmanager)
290                                 NORESTRICTEDMANAGER="Yes"
291                                 export NORESTRICTEDMANAGER
292                                 ;;
293
294                         nosudo)
295                                 NOSUDO="Yes"
296                                 export NOSUDO
297                                 ;;
298
299                         swapon)
300                                 SWAPON="Yes"
301                                 export SWAPON
302                                 ;;
303
304                         noupdatenotifier)
305                                 NOUPDATENOTIFIER="Yes"
306                                 export NOUPDATENOTIFIER
307                                 ;;
308
309                         nouser)
310                                 NOUSER="Yes"
311                                 export NOUSER
312                                 ;;
313
314                         noxautoconfig)
315                                 NOXAUTOCONFIG="Yes"
316                                 export NOXAUTOCONFIG
317                                 ;;
318
319                         noxscreensaver)
320                                 NOXSCREENSAVER="Yes"
321                                 export NOXSCREENSAVER
322                                 ;;
323
324                         persistent)
325                                 PERSISTENT="Yes"
326                                 export PERSISTENT
327                                 ;;
328
329                         persistent=*)
330                                 PERSISTENT="${ARGUMENT#persistent=}"
331                                 if [ -z "${PERSISTENT}" ]
332                                 then
333                                         PERSISTENT="Yes"
334                                 fi
335                                 export PERSISTENT
336                                 ;;
337
338                         nopersistent)
339                                 NOPERSISTENT="Yes"
340                                 export NOPERSISTENT
341                                 ;;
342
343                         preseed/file=*|file=*)
344                                 LOCATION="${ARGUMENT#*=}"
345                                 export LOCATION
346                                 ;;
347
348                         nopreseed)
349                                 NOPRESEED="Yes"
350                                 export NOPRESEED
351                                 ;;
352
353                         url=*)
354                                 location="${ARGUMENT#url=}"
355
356                                 mount -o bind /sys /root/sys
357                                 mount -o bind /proc /root/proc
358                                 mount -o bind /dev /root/dev
359
360                                 mkdir -p /root/var/run/network
361                                 chroot /root dhclient eth0
362                                 chroot /root wget -P /tmp "${location}"
363                                 chroot /root ifconfig eth0 down
364
365                                 umount /root/sys
366                                 umount /root/proc
367                                 umount /root/dev
368
369                                 LOCATION="/tmp/$(basename "${location}")"
370                                 ;;
371
372                         */*=*)
373                                 question="${ARGUMENT%%=*}"
374                                 value="${ARGUMENT#*=}"
375                                 PRESEEDS="${PRESEEDS}\"${question}=${value}\" "
376                                 export PRESEEDS
377                                 ;;
378
379                         showmounts)
380                                 SHOWMOUNTS="Yes"
381                                 export SHOWMOUNTS
382                                 ;;
383
384                         textonly)
385                                 TEXTONLY="Yes"
386                                 export TEXTONLY
387                                 ;;
388
389                         timezone=*)
390                                 TIMEZONE="${ARGUMENT#timezone=}"
391                                 export TIMEZONE
392                                 ;;
393
394                         notimezone)
395                                 NOTIMEZONE="Yes"
396                                 export NOTIMEZONE
397                                 ;;
398
399                         todisk=*)
400                                 TODISK="${ARGUMENT#todisk=}"
401                                 export TODISK
402                                 ;;
403
404                         toram)
405                                 TORAM="Yes"
406                                 export TORAM
407                                 ;;
408
409                         toram=*)
410                                 TORAM="Yes"
411                                 MODULETORAM="${ARGUMENT#toram=}"
412                                 export TORAM MODULETORAM
413                                 ;;
414
415                         exposedroot)
416                                 EXPOSED_ROOT="Yes"
417                                 export EXPOSED_ROOT
418                                 ;;
419
420                         plainroot)
421                                 PLAIN_ROOT="Yes"
422                                 export PLAIN_ROOT
423                                 ;;
424
425                         root=*)
426                                 ROOT="${ARGUMENT#root=}"
427                                 export ROOT
428                                 ;;
429
430                         union=*)
431                                 UNIONTYPE="${ARGUMENT#union=}"
432                                 export UNIONTYPE
433                                 ;;
434
435                         utc=*)
436                                 UTC="${ARGUMENT#utc=}"
437                                 export UTC
438                                 ;;
439
440                         xdebconf)
441                                 XDEBCONF="Yes"
442                                 export XDEBCONF
443                                 ;;
444
445                         xdriver=*)
446                                 XDRIVER="${ARGUMENT#xdriver=}"
447                                 export XDRIVER
448                                 ;;
449
450                         xvideomode=*)
451                                 XVIDEOMODE="${ARGUMENT#xvideomode=}"
452                                 export XVIDEOMODE
453                                 ;;
454                 esac
455         done
456
457         # sort of compatibility with netboot.h from linux docs
458         if [ -z "${NETBOOT}" ]
459         then
460                 if [ "${ROOT}" = "/dev/nfs" ]
461                 then
462                         NETBOOT="nfs"
463                         export NETBOOT
464                 elif [ "${ROOT}" = "/dev/cifs" ]
465                 then
466                         NETBOOT="cifs"
467                         export NETBOOT
468                 fi
469         fi
470
471         if [ -z "${MODULE}" ]
472         then
473                 MODULE="filesystem"
474                 export MODULE
475         fi
476
477         if [ -z "${UNIONTYPE}" ]
478         then
479                 UNIONTYPE="aufs"
480                 export UNIONTYPE
481         fi
482 }
483
484 is_live_path ()
485 {
486         DIRECTORY="${1}"
487
488         if [ -d "${DIRECTORY}"/"${LIVE_MEDIA_PATH}" ]
489         then
490                 for FILESYSTEM in squashfs ext2 ext3 xfs dir jffs2
491                 do
492                         if [ "$(echo ${DIRECTORY}/${LIVE_MEDIA_PATH}/*.${FILESYSTEM})" != "${DIRECTORY}/${LIVE_MEDIA_PATH}/*.${FILESYSTEM}" ]
493                         then
494                                 return 0
495                         fi
496                 done
497         fi
498
499         return 1
500 }
501
502 matches_uuid ()
503 {
504         if [ "${IGNORE_UUID}" ] || [ ! -e /conf/uuid.conf ]
505         then
506                 return 0
507         fi
508
509         path="${1}"
510         uuid="$(cat /conf/uuid.conf)"
511
512         for try_uuid_file in "${mountpoint}/.disk/live-uuid"*
513         do
514                 [ -e "${try_uuid_file}" ] || continue
515
516                 try_uuid="$(cat "${try_uuid_file}")"
517
518                 if [ "${uuid}" = "${try_uuid}" ]
519                 then
520                         return 0
521                 fi
522         done
523
524         return 1
525 }
526
527 get_backing_device ()
528 {
529         case "${1}" in
530                 *.squashfs|*.ext2|*.ext3|*.jffs2)
531                         echo $(setup_loop "${1}" "loop" "/sys/block/loop*" '0' "${LIVE_MEDIA_ENCRYPTION}" "${2}")
532                         ;;
533
534                 *.dir)
535                         echo "directory"
536                         ;;
537
538                 *)
539                         panic "Unrecognized live filesystem: ${1}"
540                         ;;
541         esac
542 }
543
544 match_files_in_dir ()
545 {
546         # Does any files match pattern ${1} ?
547         local pattern="${1}"
548
549         if [ "$(echo ${pattern})" != "${pattern}" ]
550         then
551                 return 0
552         fi
553
554         return 1
555 }
556
557 mount_images_in_directory ()
558 {
559         directory="${1}"
560         rootmnt="${2}"
561         mac="${3}"
562
563
564         if match_files_in_dir "${directory}/${LIVE_MEDIA_PATH}/*.squashfs" ||
565                 match_files_in_dir "${directory}/${LIVE_MEDIA_PATH}/*.ext2" ||
566                 match_files_in_dir "${directory}/${LIVE_MEDIA_PATH}/*.ext3" ||
567                 match_files_in_dir "${directory}/${LIVE_MEDIA_PATH}/*.jffs2" ||
568                 match_files_in_dir "${directory}/${LIVE_MEDIA_PATH}/*.dir"
569         then
570                 [ -n "${mac}" ] && adddirectory="${directory}/${LIVE_MEDIA_PATH}/${mac}"
571                 setup_unionfs "${directory}/${LIVE_MEDIA_PATH}" "${rootmnt}" "${adddirectory}"
572         else
573                 :
574         fi
575 }
576
577 is_nice_device ()
578 {
579         sysfs_path="${1#/sys}"
580
581         if /lib/udev/path_id "${sysfs_path}" | grep -E -q "ID_PATH=(usb|pci-[^-]*-[ide|scsi|usb])"
582         then
583                 return 0
584         elif echo "${sysfs_path}" | grep -q '^/block/vd[a-z]$'
585         then
586                 return 0
587         fi
588
589         return 1
590 }
591
592 copy_live_to ()
593 {
594         copyfrom="${1}"
595         copytodev="${2}"
596         copyto="${copyfrom}_swap"
597
598         if [ -z "${MODULETORAM}" ]
599         then
600                 size=$(fs_size "" ${copyfrom} "used")
601         else
602                 MODULETORAMFILE="${copyfrom}/${LIVE_MEDIA_PATH}/${MODULETORAM}"
603
604                 if [ -f "${MODULETORAMFILE}" ]
605                 then
606                         size=$( expr $(ls -la ${MODULETORAMFILE} | awk '{print $5}') / 1024 + 5000 )
607                 else
608                         log_warning_msg "Error: toram-module ${MODULETORAM} (${MODULETORAMFILE}) could not be read."
609                         return 1
610                 fi
611         fi
612
613         if [ "${copytodev}" = "ram" ]
614         then
615                 # copying to ram:
616                 freespace=$( expr $(awk '/MemFree/{print $2}' /proc/meminfo) + $( awk '/\<Cached/{print $2}' /proc/meminfo ) )
617                 mount_options="-o size=${size}k"
618                 free_string="memory"
619                 fstype="tmpfs"
620                 dev="/dev/shm"
621         else
622                 # it should be a writable block device
623                 if [ -b "${copytodev}" ]
624                 then
625                         dev="${copytodev}"
626                         free_string="space"
627                         fstype=$(get_fstype "${dev}")
628                         freespace=$(fs_size "${dev}")
629                 else
630                         log_warning_msg "${copytodev} is not a block device."
631                         return 1
632                 fi
633         fi
634
635         if [ "${freespace}" -lt "${size}" ]
636         then
637                 log_warning_msg "Not enough free ${free_string} (${freespace}k free, ${size}k needed) to copy live media in ${copytodev}."
638                 return 1
639         fi
640
641         # begin copying (or uncompressing)
642         mkdir "${copyto}"
643         echo "mount -t ${fstype} ${mount_options} ${dev} ${copyto}"
644         mount -t "${fstype}" ${mount_options} "${dev}" "${copyto}"
645
646         if [ "${extension}" = "tgz" ]
647         then
648                 cd "${copyto}"
649                 tar zxf "${copyfrom}/${LIVE_MEDIA_PATH}/$(basename ${FETCH})"
650                 rm -f "${copyfrom}/${LIVE_MEDIA_PATH}/$(basename ${FETCH})"
651                 mount -r -o move "${copyto}" "${rootmnt}"
652                 cd "${OLDPWD}"
653         else
654                 if [ -n "${MODULETORAMFILE}" ]
655                 then
656                         cp ${MODULETORAMFILE} ${copyto} # copy only the filesystem module
657                 else
658                         cp -a ${copyfrom}/* ${copyto}   # "cp -a" from busybox also copies hidden files
659                 fi
660
661                 livefs_root
662                 umount ${copyfrom}
663                 mount -r -o move ${copyto} ${copyfrom}
664         fi
665
666         rmdir ${copyto}
667         return 0
668 }
669
670 do_netmount ()
671 {
672         rc=1
673
674         modprobe -q af_packet # For DHCP
675
676         if [ -x /sbin/udevadm ]
677         then
678                 # lenny
679                 udevadm trigger
680                 udevadm settle
681         else
682                 # etch
683                 udevtrigger
684                 udevsettle
685         fi
686
687         ipconfig ${DEVICE} | tee /netboot.config
688
689         # source relevant ipconfig output
690         OLDHOSTNAME=${HOSTNAME}
691         . /tmp/net-${DEVICE}.conf
692         [ -z ${HOSTNAME} ] && HOSTNAME=${OLDHOSTNAME}
693         export HOSTNAME
694
695         if [ "${NFSROOT}" = "auto" ]
696         then
697                 NFSROOT=${ROOTSERVER}:${ROOTPATH}
698         fi
699
700         if ( [ -n "${FETCH}" ] || [ -n "${HTTPFS}" ] || [ -n "${FTPFS}" ] ) && do_httpmount
701         then
702                 rc=0
703                 return ${rc}
704         fi
705
706         if [ "${NFSROOT#*:}" = "${NFSROOT}" ] && [ "$NETBOOT" != "cifs" ]
707         then
708                 NFSROOT=${ROOTSERVER}:${NFSROOT}
709         fi
710
711         log_begin_msg "Trying netboot from ${NFSROOT}"
712
713         if [ "${NETBOOT}" != "nfs" ] && do_cifsmount
714         then
715                 rc=0
716         elif do_nfsmount
717         then
718                 NETBOOT="nfs"
719                 export NETBOOT
720                 rc=0
721         fi
722
723         log_end_msg
724         return ${rc}
725 }
726
727 do_httpmount ()
728 {
729         rc=1
730         dest="${mountpoint}/${LIVE_MEDIA_PATH}"
731         mount -t ramfs ram "${mountpoint}"
732         mkdir -p "${dest}"
733
734         for webfile in HTTPFS FTPFS FETCH
735         do
736                 url="$(eval echo \"\$\{${webfile}\}\")"
737                 extension="$(echo "${url}" | sed 's/\(.*\)\.\(.*\)/\2/')"
738
739                 if [ -n "$url" ]
740                 then
741                         case "${extension}" in
742                                 squashfs|tgz|tar)
743                                         if [ "${webfile}" = "FETCH" ]
744                                         then
745                                                 case "$url" in
746                                                         tftp*)
747                                                                 ip="$(dirname $url | sed -e 's|tftp://||g')"
748                                                                 log_begin_msg "Trying tftp -g -b 10240 -r $(basename ${url})  -l ${dest}/$(basename ${url}) $ip"
749                                                                 tftp -g -b 10240 -r $(basename ${url})  -l ${dest}/$(basename ${url}) $ip
750                                                         ;;
751
752                                                         *)
753                                                                 log_begin_msg "Trying wget ${url} -O ${dest}/$(basename ${url})"
754                                                                 wget "${url}" -O "${dest}/$(basename ${url})"
755                                                                 ;;
756                                                 esac
757                                         else
758                                                 log_begin_msg "Trying to mount ${url} on ${dest}/$(basename ${url})"
759                                                 if [ "${webfile}" = "FTPFS" ]
760                                                 then
761                                                         FUSE_MOUNT="curlftpfs"
762                                                         url="$(dirname ${url})"
763                                                 else
764                                                         FUSE_MOUNT="httpfs"
765                                                 fi
766                                                 modprobe fuse
767                                                 $FUSE_MOUNT "${url}" "${dest}"
768                                         fi
769                                         [ ${?} -eq 0 ] && rc=0
770                                         [ "${extension}" = "tgz" ] && live_dest="ram"
771                                         break
772                                         ;;
773
774                                 *)
775                                         log_begin_msg "Unrecognized archive extension for ${url}"
776                                         ;;
777                         esac
778                 fi
779         done
780
781         if [ ${rc} != 0 ]
782         then
783             umount "${mountpoint}"
784         fi
785
786         return ${rc}
787 }
788
789 do_nfsmount ()
790 {
791         rc=1
792
793         modprobe -q nfs
794
795         if [ -z "${NFSOPTS}" ]
796         then
797                 NFSOPTS=""
798         fi
799
800         log_begin_msg "Trying nfsmount -o nolock -o ro ${NFSOPTS} ${NFSROOT} ${mountpoint}"
801
802         # FIXME: This for loop is an ugly HACK round an nfs bug
803         for i in 0 1 2 3 4 5 6 7 8 9 a b c d e f 10 11 12 13
804         do
805                 nfsmount -o nolock -o ro ${NFSOPTS} "${NFSROOT}" "${mountpoint}" && rc=0 && break
806                 sleep 1
807         done
808
809         return ${rc}
810 }
811
812 do_cifsmount ()
813 {
814         rc=1
815
816         if [ -x "/sbin/mount.cifs" ]
817         then
818                 if [ -z "${NFSOPTS}" ]
819                 then
820                         CIFSOPTS="-ouser=root,password="
821                 else
822                         CIFSOPTS="${NFSOPTS}"
823                 fi
824
825                 log_begin_msg "Trying mount.cifs ${NFSROOT} ${mountpoint} ${CIFSOPTS}"
826                 modprobe -q cifs
827
828                 if mount.cifs "${NFSROOT}" "${mountpoint}" "${CIFSOPTS}"
829                 then
830                         rc=0
831                 fi
832         fi
833
834         return ${rc}
835 }
836
837 do_snap_copy ()
838 {
839         fromdev="${1}"
840         todir="${2}"
841         snap_type="${3}"
842         size=$(fs_size "${fromdev}" "" "used")
843
844         if [ -b "${fromdev}" ]
845         then
846                 # look for free mem
847                 if [ -n "${HOMEMOUNTED}" -a "${snap_type}" = "HOME" ]
848                 then
849                         todev=$(awk -v pat="$(base_path ${todir})" '$2 == pat { print $1 }' /proc/mounts)
850                         freespace=$(df -k | awk '/'${todev}'/{print $4}')
851                 else
852                         freespace=$( expr $(awk '/MemFree/{print $2}' /proc/meminfo) + $( awk '/\<Cached/{print $2}' /proc/meminfo))
853                 fi
854
855                 tomount="/mnt/tmpsnap"
856
857                 if [ ! -d "${tomount}" ]
858                 then
859                         mkdir -p "${tomount}"
860                 fi
861
862                 fstype=$(get_fstype "${fromdev}")
863
864                 if [ -n "${fstype}" ]
865                 then
866                         # Copying stuff...
867                         mount -o ro -t "${fstype}" "${fromdev}" "${tomount}" || log_warning_msg "Error in mount -t ${fstype} -o ro ${fromdev} ${tomount}"
868                         cp -a "${tomount}"/* ${todir}
869                         umount "${tomount}"
870                 else
871                         log_warning_msg "Unrecognized fstype: ${fstype} on ${fromdev}:${snap_type}"
872                 fi
873
874                 rmdir "${tomount}"
875
876                 if echo ${fromdev} | grep -qs loop
877                 then
878                         losetup -d "${fromdev}"
879                 fi
880
881                 return 0
882         else
883                 return 1
884
885                 log_warning_msg "Unable to find the snapshot ${snap_type} medium"
886         fi
887 }
888
889 find_snap ()
890 {
891         # Look for ${snap_label}.* in block devices
892         snap_label="${1}"
893
894         if [ "${PERSISTENT}" != "nofiles" ]
895         then
896                 # search for image files
897                 snapdata=$(find_files "${snap_label}.squashfs ${snap_label}.cpio.gz ${snap_label}.ext2 ${snap_label}.ext3 ${snap_label}.jffs2")
898         fi
899
900         if [ -z "${snapdata}" ]
901         then
902                 snapdata=$(find_cow_device "${snap_label}")
903         fi
904         echo "${snapdata}"
905 }
906
907 try_snap ()
908 {
909         # copy the contents of previously found snapshot to ${snap_mount}
910         # and remember the device and filename for resync on exit in live-initramfs.init
911
912         snapdata="${1}"
913         snap_mount="${2}"
914         snap_type="${3}"
915
916         if [ ! -z "${snapdata}" ]
917         then
918                 log_success_msg "found snapshot: ${snapdata}"
919                 snapdev="$(echo ${snapdata} | cut -f1 -d ' ')"
920                 snapback="$(echo ${snapdata} | cut -f2 -d ' ')"
921                 snapfile="$(echo ${snapdata} | cut -f3 -d ' ')"
922
923                 RES=""
924                 if ! try_mount "${snapdev}" "${snapback}" "ro"
925                 then
926                         break
927                 fi
928
929                 if echo "${snapfile}" | grep -qs '\(squashfs\|ext2\|ext3\|jffs2\)'
930                 then
931                         # squashfs, jffs2 or ext2/ext3 snapshot
932                         dev=$(get_backing_device "${snapback}/${snapfile}")
933
934                         do_snap_copy "${dev}" "${snap_mount}" "${snap_type}"
935                         RES=$?
936                 else
937                         # cpio.gz snapshot
938                         cd "${snap_mount}"
939                         zcat "${snapback}/${snapfile}" | /bin/cpio --extract --preserve-modification-time --no-absolute-filenames --sparse --unconditional --make-directories > /dev/null 2>&1
940                         RES=$?
941                         if [ "${RES}" != "0" ]
942                         then
943                                 log_warning_msg "failure to \"zcat ${snapback}/${snapfile} | /bin/cpio --extract --preserve-modification-time --no-absolute-filenames --sparse --unconditional --make-directories\""
944                         fi
945                         cd "${OLDPWD}"
946                 fi
947
948                 umount "${snapback}" ||  log_warning_msg "failure to \"umount ${snapback}\""
949
950                 if [ "${RES}" != "0" ]
951                 then
952                         log_warning_msg "Impossible to include the ${snapfile} Snapshot file"
953                 fi
954
955
956         elif [ -b "${snapdata}" ]
957         then
958                 # Try to find if it could be a snapshot partition
959                 dev="${snapdata}"
960                 log_success_msg "found snapshot device on ${dev}"
961                 if echo "${dev}" | grep -qs loop
962                 then
963                         # strange things happens, user confused?
964                         snaploop=$( losetup ${dev} | awk '{print $3}' | tr -d '()' )
965                         snapfile=$(basename ${snaploop})
966                         snapdev=$(awk -v pat="$( dirname ${snaploop})" '$2 == pat { print $1 }' /proc/mounts)
967                 else
968                         snapdev="${dev}"
969                 fi
970
971                 if ! do_snap_copy "${dev}" "${snap_mount}" "${snap_type}"
972                 then
973                         log_warning_msg "Impossible to include the ${snap_type} Snapshot"
974                         return 1
975                 else
976                         if [ -n "${snapfile}" ]
977                         then
978                                 # it was a loop device, user confused
979                                 umount ${snapdev}
980                         fi
981                 fi
982         else
983                 log_warning_msg "Impossible to include the ${snap_type} Snapshot"
984                 return 1
985         fi
986
987         echo "export ${snap_type}SNAP="${snap_mount}":${snapdev}:${snapfile}" >> /etc/live.conf # for resync on reboot/halt
988         return 0
989 }
990
991 setup_unionfs ()
992 {
993         image_directory="${1}"
994         rootmnt="${2}"
995         addimage_directory="${3}"
996         modprobe -q -b ${UNIONTYPE}
997
998         # run-init can't deal with images in a subdir, but we're going to
999         # move all of these away before it runs anyway.  No, we're not,
1000         # put them in / since move-mounting them into / breaks mono and
1001         # some other apps.
1002
1003         croot="/"
1004
1005         # Let's just mount the read-only file systems first
1006         rofsstring=""
1007         rofslist=""
1008         minor_kernel_version=$(uname -r|cut -c 5-|sed 's/[^0-9].*//')
1009
1010         if [ "${NETBOOT}" = "nfs" ] && [ "${minor_kernel_version}" -lt 22 ]
1011         then
1012                 # go aroung a bug in nfs-unionfs locking for unionfs <= 1.4
1013                 roopt="nfsro"
1014         elif [ "${UNIONTYPE}" = "aufs" ]
1015         then
1016                 roopt="rr"
1017         else
1018                 roopt="ro"
1019         fi
1020
1021         # Read image names from ${MODULE}.module if it exists
1022         if [ -e "${image_directory}/filesystem.${MODULE}.module" ]
1023         then
1024                 for IMAGE in $(cat ${image_directory}/filesystem.${MODULE}.module)
1025                 do
1026                         image_string="${image_string} ${image_directory}/${IMAGE}"
1027                 done
1028         elif [ -e "${image_directory}/${MODULE}.module" ]
1029         then
1030                 for IMAGE in $(cat ${image_directory}/${MODULE}.module)
1031                 do
1032                         image_string="${image_string} ${image_directory}/${IMAGE}"
1033                 done
1034         else
1035                 # ${MODULE}.module does not exist, create a list of images
1036                 for FILESYSTEM in squashfs ext2 ext3 xfs jffs2 dir
1037                 do
1038                         for IMAGE in "${image_directory}"/*."${FILESYSTEM}"
1039                         do
1040                                 if [ -e "${IMAGE}" ]
1041                                 then
1042                                         image_string="${image_string} ${IMAGE}"
1043                                 fi
1044                         done
1045                 done
1046
1047                 if [ -n "${addimage_directory}" ] && [ -d "${addimage_directory}" ]
1048                 then
1049                         for FILESYSTEM in squashfs ext2 ext3 xfs jffs2 dir
1050                         do
1051                                 for IMAGE in "${addimage_directory}"/*."${FILESYSTEM}"
1052                                 do
1053                                         if [ -e "${IMAGE}" ]
1054                                         then
1055                                                 image_string="${image_string} ${IMAGE}"
1056                                         fi
1057                                 done
1058                         done
1059                 fi
1060
1061                 # Now sort the list
1062                 image_string="$(echo ${image_string} | sed -e 's/ /\n/g' | sort )"
1063         fi
1064
1065         [ -n "${MODULETORAMFILE}" ] && image_string="${image_directory}/$(basename ${MODULETORAMFILE})"
1066
1067         mkdir -p "${croot}"
1068
1069         for image in ${image_string}
1070         do
1071                 imagename=$(basename "${image}")
1072
1073                 if [ -d "${image}" ]
1074                 then
1075                         # it is a plain directory: do nothing
1076                         rofsstring="${image}=${roopt}:${rofsstring}"
1077                         rofslist="${image} ${rofslist}"
1078                 elif [ -f "${image}" ]
1079                 then
1080                         if losetup --help 2>&1 | grep -q -- "-r\b"
1081                         then
1082                                 backdev=$(get_backing_device "${image}" "-r")
1083                         else
1084                                 backdev=$(get_backing_device "${image}")
1085                         fi
1086                         fstype=$(get_fstype "${backdev}")
1087
1088                         if [ "${fstype}" = "unknown" ]
1089                         then
1090                                 panic "Unknown file system type on ${backdev} (${image})"
1091                         fi
1092
1093                         if [ -z "${fstype}" ]
1094                         then
1095                                 fstype="${imagename##*.}"
1096                                 log_warning_msg "Unknown file system type on ${backdev} (${image}), assuming ${fstype}."
1097                         fi
1098
1099                         mkdir -p "${croot}/${imagename}"
1100                         log_begin_msg "Mounting \"${image}\" on \"${croot}${imagename}\" via \"${backdev}\""
1101                         mount -t "${fstype}" -o ro,noatime "${backdev}" "${croot}/${imagename}" || panic "Can not mount ${backdev} (${image}) on ${croot}/${imagename}" && rofsstring="${croot}/${imagename}=${roopt}:${rofsstring}" && rofslist="${croot}/${imagename} ${rofslist}"
1102                         log_end_msg
1103                 fi
1104         done
1105
1106         rofsstring=${rofsstring%:}
1107
1108         mkdir -p /cow
1109
1110         # Looking for "${root_persistence}" device or file
1111         if [ -n "${PERSISTENT}" ] && [ -z "${NOPERSISTENT}" ]
1112         then
1113                 # Load USB modules
1114                 num_block=$(ls -l /sys/block | wc -l)
1115                 for module in sd_mod uhci-hcd ehci-hcd ohci-hcd usb-storage
1116                 do
1117                         modprobe -q -b ${module}
1118                 done
1119
1120                 if [ -x /sbin/udevadm ]
1121                 then
1122                         # lenny
1123                         udevadm trigger
1124                         udevadm settle
1125                 else
1126                         # etch
1127                         udevtrigger
1128                         udevsettle
1129                 fi
1130
1131                 # For some reason, udevsettle does not block in this scenario,
1132                 # so we sleep for a little while.
1133                 #
1134                 # See https://bugs.launchpad.net/ubuntu/+source/casper/+bug/84591
1135                 for timeout in 5 4 3 2 1
1136                 do
1137                         sleep 1
1138
1139                         if [ $(ls -l /sys/block | wc -l) -gt ${num_block} ]
1140                         then
1141                                 break
1142                         fi
1143                 done
1144
1145                 # search for label and files (this could be hugely optimized)
1146                 cowprobe=$(find_cow_device "${root_persistence}")
1147                 if [ -b "${cowprobe}" ]
1148                 then
1149                         # Blacklist /cow device, to avoid inconsistent setups for overlapping snapshots
1150                         # makes sense to have both persistence for /cow and /home mounted, maybe also with
1151                         # snapshots to be sure to really store some e.g key config files,
1152                         # but not on the same media
1153                         blacklistdev="${cowprobe}"
1154                         PERSISTENCE_IS_ON="1"
1155                         export PERSISTENCE_IS_ON
1156                 fi
1157                 # homecow just mount something on /home, this should be generalized some way
1158                 homecow=$(find_cow_device "${home_persistence}" "${blacklistdev}")
1159                 if [ -b "${homecow}" ]
1160                 then
1161                         PERSISTENCE_IS_ON="1"
1162                         export PERSISTENCE_IS_ON
1163                 fi
1164                 root_snapdata=$(find_snap "${root_snapshot_label}" "${blacklistdev}")
1165                 # This second type should be removed when snapshot will get smarter,
1166                 # hence when "/etc/live-snapshot*list" will be supported also by
1167                 # ext2|ext3|jffs2 snapshot types.
1168                 home_snapdata=$(find_snap "${home_snapshot_label}" "${blacklistdev}")
1169
1170                 if [ -b "${cowprobe}" ]
1171                 then
1172                         cowdevice=${cowprobe}
1173                         cow_fstype=$(get_fstype "${cowprobe}")
1174                         cow_mountopt="rw,noatime"
1175                 else
1176                         log_warning_msg "Unable to find the persistent medium"
1177                         cowdevice="tmpfs"
1178                         cow_fstype="tmpfs"
1179                         cow_mountopt="rw,noatime,mode=755"
1180                 fi
1181         elif [ -n "${NFS_COW}" ] && [ -z "${NOPERSISTENT}" ]
1182         then
1183                 # check if there are any nfs options
1184                 if echo ${NFS_COW}|grep -q ','
1185                 then
1186                         nfs_cow_opts="-o nolock,$(echo ${NFS_COW}|cut -d, -f2-)"
1187                         nfs_cow=$(echo ${NFS_COW}|cut -d, -f1)
1188                 else
1189                         nfs_cow_opts="-o nolock"
1190                         nfs_cow=${NFS_COW}
1191                 fi
1192                 mac="$(get_mac)"
1193                 if [ -n "${mac}" ]
1194                 then
1195                         cowdevice=$(echo ${nfs_cow}|sed "s/client_mac_address/${mac}/")
1196                         cow_fstype="nfs"
1197                 else
1198                         panic "unable to determine mac address"
1199                 fi
1200         else
1201                 cowdevice="tmpfs"
1202                 cow_fstype="tmpfs"
1203                 cow_mountopt="rw,noatime,mode=755"
1204         fi
1205
1206         if [ "${cow_fstype}" = "nfs" ]
1207         then
1208                 log_begin_msg \
1209                         "Trying nfsmount ${nfs_cow_opts} ${cowdevice} /cow"
1210                 nfsmount ${nfs_cow_opts} ${cowdevice} /cow || \
1211                         panic "Can not mount ${cowdevice} (n: ${cow_fstype}) on /cow"
1212         else
1213                 mount -t ${cow_fstype} -o ${cow_mountopt} ${cowdevice} /cow || \
1214                         panic "Can not mount ${cowdevice} (o: ${cow_fstype}) on /cow"
1215         fi
1216
1217         rofscount=$(echo ${rofslist} |wc -w)
1218
1219         if [ -n "${EXPOSED_ROOT}" ]
1220         then
1221                 if [ ${rofscount} -ne 1 ]
1222                 then
1223                         panic "only one RO file system supported with exposedroot: ${rofslist}"
1224                 fi
1225                 exposedrootfs=${rofslist%% }
1226
1227                 mount --bind ${exposedrootfs} ${rootmnt} || \
1228                         panic "bind mount of ${exposedrootfs} failed"
1229
1230                 cow_dirs='/var/tmp /var/lock /var/run /var/log /var/spool
1231                         /home /var/lib/live'
1232
1233                 for dir in ${cow_dirs}; do
1234                         mkdir -p /cow${dir}
1235                         mount -t ${UNIONTYPE} \
1236                                 -o rw,noatime,dirs=/cow${dir}=rw:${exposedrootfs}${dir}=ro \
1237                                 ${UNIONTYPE} "${rootmnt}${dir}" || \
1238                                 panic "mount ${UNIONTYPE} on ${rootmnt}${dir} failed with option \
1239                                         rw,noatime,dirs=/cow${dir}=rw:${exposedrootfs}${dir}=ro"
1240                 done
1241         else
1242                 mount -t ${UNIONTYPE} -o noatime,dirs=/cow=rw:${rofsstring} \
1243                         ${UNIONTYPE} "${rootmnt}" || panic "mount ${UNIONTYPE} on \
1244                         ${rootmnt} failed with option noatime,dirs=/cow=rw:${rofsstring}"
1245         fi
1246
1247         # tmpfs file systems
1248         touch /etc/fstab
1249         mkdir -p "${rootmnt}/live"
1250         mount -t tmpfs tmpfs ${rootmnt}/live
1251
1252         # Adding other custom mounts
1253         if [ -n "${PERSISTENT}" ] && [ -z "${NOPERSISTENT}" ]
1254         then
1255                 # directly mount /home
1256                 # FIXME: add a custom mounts configurable system
1257
1258                 if [ -b "${homecow}" ]
1259                 then
1260                         mount -t $(get_fstype "${homecow}") -o rw,noatime "${homecow}" "${rootmnt}/home"
1261                         export HOMEMOUNTED=1 # used to proper calculate free space in do_snap_copy()
1262                 else
1263                         log_warning_msg "Unable to find the persistent home medium"
1264                 fi
1265
1266                 # Look for other snapshots to copy in
1267                 try_snap "${root_snapdata}" "${rootmnt}" "ROOT"
1268                 # This second type should be removed when snapshot grow smarter
1269                 try_snap "${home_snapdata}" "${rootmnt}/home" "HOME"
1270         fi
1271
1272         if [ -n "${SHOWMOUNTS}" ]
1273         then
1274                 for d in ${rofslist}
1275                 do
1276                         mkdir -p "${rootmnt}/live/${d##*/}"
1277
1278                         case d in
1279                                 *.dir)
1280                                         # do nothing # mount -o bind "${d}" "${rootmnt}/live/${d##*/}"
1281                                         ;;
1282
1283                                 *)
1284                                         mount -o move "${d}" "${rootmnt}/live/${d##*/}"
1285                                         ;;
1286                         esac
1287                 done
1288         fi
1289
1290         # shows cow fs on /cow for use by live-snapshot
1291         mkdir -p "${rootmnt}/live/cow"
1292         mount -o move /cow "${rootmnt}/live/cow" >/dev/null 2>&1 || mount -o bind /cow "${rootmnt}/live/cow" || log_warning_msg "Unable to move or bind /cow to ${rootmnt}/live/cow"
1293 }
1294
1295 check_dev ()
1296 {
1297         sysdev="${1}"
1298         devname="${2}"
1299         skip_uuid_check="${3}"
1300
1301         if [ -z "${devname}" ]
1302         then
1303                 devname=$(sys2dev "${sysdev}")
1304         fi
1305
1306         if [ -d "${devname}" ]
1307         then
1308                 mount -o bind "${devname}" $mountpoint || continue
1309
1310                 if is_live_path $mountpoint
1311                 then
1312                         echo $mountpoint
1313                         return 0
1314                 else
1315                         umount $mountpoint
1316                 fi
1317         fi
1318
1319         if [ -n "${LIVE_MEDIA_OFFSET}" ]
1320         then
1321                 loopdevname=$(setup_loop "${devname}" "loop" "/sys/block/loop*" "${LIVE_MEDIA_OFFSET}" '')
1322                 devname="${loopdevname}"
1323         fi
1324
1325         fstype=$(get_fstype "${devname}")
1326
1327         if is_supported_fs ${fstype}
1328         then
1329                 mount -t ${fstype} -o ro,noatime "${devname}" ${mountpoint} || continue
1330
1331                 if is_live_path ${mountpoint} && \
1332                         ([ "${skip_uuid_check}" ] || matches_uuid ${mountpoint})
1333                 then
1334                         echo ${mountpoint}
1335                         return 0
1336                 else
1337                         umount ${mountpoint}
1338                 fi
1339         fi
1340
1341         if [ -n "${LIVE_MEDIA_OFFSET}" ]
1342         then
1343                 losetup -d "${loopdevname}"
1344         fi
1345
1346         return 1
1347 }
1348
1349 find_livefs ()
1350 {
1351         timeout="${1}"
1352
1353         # first look at the one specified in the command line
1354         if [ ! -z "${LIVE_MEDIA}" ]
1355         then
1356                 if check_dev "null" "${LIVE_MEDIA}" "skip_uuid_check"
1357                 then
1358                         return 0
1359                 fi
1360         fi
1361
1362         # don't start autodetection before timeout has expired
1363         if [ -n "${LIVE_MEDIA_TIMEOUT}" ]
1364         then
1365                 if [ "${timeout}" -lt "${LIVE_MEDIA_TIMEOUT}" ]
1366                 then
1367                         return 1
1368                 fi
1369         fi
1370
1371         # or do the scan of block devices
1372         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram | grep -v 'dm-' | grep -v fd )
1373         do
1374                 devname=$(sys2dev "${sysblock}")
1375                 fstype=$(get_fstype "${devname}")
1376
1377                 if /lib/udev/cdrom_id ${devname} > /dev/null
1378                 then
1379                         if check_dev "null" "${devname}"
1380                         then
1381                                 return 0
1382                         fi
1383                 elif is_nice_device "${sysblock}"
1384                 then
1385                         for dev in $(subdevices "${sysblock}")
1386                         do
1387                                 if check_dev "${dev}"
1388                                 then
1389                                         return 0
1390                                 fi
1391                         done
1392                 elif [ "${fstype}" = "squashfs" -o \
1393                         "${fstype}" = "ext2" -o \
1394                         "${fstype}" = "ext3" -o \
1395                         "${fstype}" = "jffs2" ]
1396                 then
1397                         # This is an ugly hack situation, the block device has
1398                         # an image directly on it.  It's hopefully
1399                         # live-initramfs, so take it and run with it.
1400                         ln -s "${devname}" "${devname}.${fstype}"
1401                         echo "${devname}.${fstype}"
1402                         return 0
1403                 fi
1404         done
1405
1406         return 1
1407 }
1408
1409 set_usplash_timeout ()
1410 {
1411         if [ -x /sbin/usplash_write ]
1412         then
1413                 /sbin/usplash_write "TIMEOUT 120"
1414         else if [ -x /sbin/splashy_update ] ; then
1415                 /sbin/splashy_update "TIMEOUT 120"
1416         fi ; fi
1417 }
1418
1419 mountroot ()
1420 {
1421         if [ -x /scripts/local-top/cryptroot ]; then
1422             /scripts/local-top/cryptroot
1423         fi
1424
1425         exec 6>&1
1426         exec 7>&2
1427         exec > live.log
1428         exec 2>&1
1429         tail -f live.log >&7 &
1430         tailpid="${!}"
1431
1432         # Ensure 'panic' function is overridden
1433         . /scripts/live-functions
1434
1435         Arguments
1436
1437         set_usplash_timeout
1438
1439         maybe_break live-premount
1440         log_begin_msg "Running /scripts/live-premount"
1441         run_scripts /scripts/live-premount
1442         log_end_msg
1443
1444         # Needed here too because some things (*cough* udev *cough*)
1445         # changes the timeout
1446
1447         set_usplash_timeout
1448
1449         if [ ! -z "${NETBOOT}" ] || [ ! -z "${FETCH}" ] || [ ! -z "${HTTPFS}" ] || [ ! -z "${FTPFS}" ]
1450         then
1451                 if do_netmount
1452                 then
1453                         livefs_root="${mountpoint}"
1454                 else
1455                         panic "Unable to find a live file system on the network"
1456                 fi
1457         else
1458                 if [ -n "${PLAIN_ROOT}" ] && [ -n "${ROOT}" ]
1459                 then
1460                         # Do a local boot from hd
1461                         livefs_root=${ROOT}
1462                 else
1463                         # Scan local devices for the image
1464                         for i in 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
1465                         do
1466                                 livefs_root=$(find_livefs ${i})
1467
1468                                 if [ -n "${livefs_root}" ]
1469                                 then
1470                                         break
1471                                 fi
1472
1473                                 sleep 1
1474                         done
1475                 fi
1476         fi
1477
1478         if [ -z "${livefs_root}" ]
1479         then
1480                 panic "Unable to find a medium containing a live file system"
1481         fi
1482
1483         if [ "${TORAM}" ]
1484         then
1485                 live_dest="ram"
1486         elif [ "${TODISK}" ]
1487         then
1488                 live_dest="${TODISK}"
1489         fi
1490
1491         if [ "${live_dest}" ]
1492         then
1493                 log_begin_msg "Copying live media to ${live_dest}"
1494                 copy_live_to "${livefs_root}" "${live_dest}"
1495                 log_end_msg
1496         fi
1497
1498         if [ -n "${MODULETORAMFILE}" ]
1499         then
1500                 setup_unionfs "${livefs_root}" "${rootmnt}"
1501         else
1502                 mac="$(get_mac)"
1503                 mac="$(echo ${mac} | sed 's/-//g')"
1504                 mount_images_in_directory "${livefs_root}" "${rootmnt}" "${mac}"
1505         fi
1506
1507         log_end_msg
1508
1509         maybe_break live-bottom
1510         log_begin_msg "Running /scripts/live-bottom"
1511
1512         run_scripts /scripts/live-bottom
1513         log_end_msg
1514
1515         exec 1>&6 6>&-
1516         exec 2>&7 7>&-
1517         kill ${tailpid}
1518         cp live.log "${rootmnt}/var/log/"
1519 }