Merging casper 1.144.
[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
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 "${path}/.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 while loop is an ugly HACK round an nfs bug
803         i=0
804         while [ "$i" -lt 60 ]
805         do
806                 nfsmount -o nolock -o ro ${NFSOPTS} "${NFSROOT}" "${mountpoint}" && rc=0 && break
807                 sleep 1
808                 i="$(($i + 1))"
809         done
810
811         return ${rc}
812 }
813
814 do_cifsmount ()
815 {
816         rc=1
817
818         if [ -x "/sbin/mount.cifs" ]
819         then
820                 if [ -z "${NFSOPTS}" ]
821                 then
822                         CIFSOPTS="-ouser=root,password="
823                 else
824                         CIFSOPTS="${NFSOPTS}"
825                 fi
826
827                 log_begin_msg "Trying mount.cifs ${NFSROOT} ${mountpoint} ${CIFSOPTS}"
828                 modprobe -q cifs
829
830                 if mount.cifs "${NFSROOT}" "${mountpoint}" "${CIFSOPTS}"
831                 then
832                         rc=0
833                 fi
834         fi
835
836         return ${rc}
837 }
838
839 do_snap_copy ()
840 {
841         fromdev="${1}"
842         todir="${2}"
843         snap_type="${3}"
844         size=$(fs_size "${fromdev}" "" "used")
845
846         if [ -b "${fromdev}" ]
847         then
848                 # look for free mem
849                 if [ -n "${HOMEMOUNTED}" -a "${snap_type}" = "HOME" ]
850                 then
851                         todev=$(awk -v pat="$(base_path ${todir})" '$2 == pat { print $1 }' /proc/mounts)
852                         freespace=$(df -k | awk '/'${todev}'/{print $4}')
853                 else
854                         freespace=$( expr $(awk '/MemFree/{print $2}' /proc/meminfo) + $( awk '/\<Cached/{print $2}' /proc/meminfo))
855                 fi
856
857                 tomount="/mnt/tmpsnap"
858
859                 if [ ! -d "${tomount}" ]
860                 then
861                         mkdir -p "${tomount}"
862                 fi
863
864                 fstype=$(get_fstype "${fromdev}")
865
866                 if [ -n "${fstype}" ]
867                 then
868                         # Copying stuff...
869                         mount -o ro -t "${fstype}" "${fromdev}" "${tomount}" || log_warning_msg "Error in mount -t ${fstype} -o ro ${fromdev} ${tomount}"
870                         cp -a "${tomount}"/* ${todir}
871                         umount "${tomount}"
872                 else
873                         log_warning_msg "Unrecognized fstype: ${fstype} on ${fromdev}:${snap_type}"
874                 fi
875
876                 rmdir "${tomount}"
877
878                 if echo ${fromdev} | grep -qs loop
879                 then
880                         losetup -d "${fromdev}"
881                 fi
882
883                 return 0
884         else
885                 return 1
886
887                 log_warning_msg "Unable to find the snapshot ${snap_type} medium"
888         fi
889 }
890
891 find_snap ()
892 {
893         # Look for ${snap_label}.* in block devices
894         snap_label="${1}"
895
896         if [ "${PERSISTENT}" != "nofiles" ]
897         then
898                 # search for image files
899                 snapdata=$(find_files "${snap_label}.squashfs ${snap_label}.cpio.gz ${snap_label}.ext2 ${snap_label}.ext3 ${snap_label}.jffs2")
900         fi
901
902         if [ -z "${snapdata}" ]
903         then
904                 snapdata=$(find_cow_device "${snap_label}")
905         fi
906         echo "${snapdata}"
907 }
908
909 try_snap ()
910 {
911         # copy the contents of previously found snapshot to ${snap_mount}
912         # and remember the device and filename for resync on exit in live-initramfs.init
913
914         snapdata="${1}"
915         snap_mount="${2}"
916         snap_type="${3}"
917
918         if [ ! -z "${snapdata}" ]
919         then
920                 log_success_msg "found snapshot: ${snapdata}"
921                 snapdev="$(echo ${snapdata} | cut -f1 -d ' ')"
922                 snapback="$(echo ${snapdata} | cut -f2 -d ' ')"
923                 snapfile="$(echo ${snapdata} | cut -f3 -d ' ')"
924
925                 RES=""
926                 if ! try_mount "${snapdev}" "${snapback}" "ro"
927                 then
928                         break
929                 fi
930
931                 if echo "${snapfile}" | grep -qs '\(squashfs\|ext2\|ext3\|jffs2\)'
932                 then
933                         # squashfs, jffs2 or ext2/ext3 snapshot
934                         dev=$(get_backing_device "${snapback}/${snapfile}")
935
936                         do_snap_copy "${dev}" "${snap_mount}" "${snap_type}"
937                         RES=$?
938                 else
939                         # cpio.gz snapshot
940                         cd "${snap_mount}"
941                         zcat "${snapback}/${snapfile}" | /bin/cpio --extract --preserve-modification-time --no-absolute-filenames --sparse --unconditional --make-directories > /dev/null 2>&1
942                         RES=$?
943                         if [ "${RES}" != "0" ]
944                         then
945                                 log_warning_msg "failure to \"zcat ${snapback}/${snapfile} | /bin/cpio --extract --preserve-modification-time --no-absolute-filenames --sparse --unconditional --make-directories\""
946                         fi
947                         cd "${OLDPWD}"
948                 fi
949
950                 umount "${snapback}" ||  log_warning_msg "failure to \"umount ${snapback}\""
951
952                 if [ "${RES}" != "0" ]
953                 then
954                         log_warning_msg "Impossible to include the ${snapfile} Snapshot file"
955                 fi
956
957
958         elif [ -b "${snapdata}" ]
959         then
960                 # Try to find if it could be a snapshot partition
961                 dev="${snapdata}"
962                 log_success_msg "found snapshot device on ${dev}"
963                 if echo "${dev}" | grep -qs loop
964                 then
965                         # strange things happens, user confused?
966                         snaploop=$( losetup ${dev} | awk '{print $3}' | tr -d '()' )
967                         snapfile=$(basename ${snaploop})
968                         snapdev=$(awk -v pat="$( dirname ${snaploop})" '$2 == pat { print $1 }' /proc/mounts)
969                 else
970                         snapdev="${dev}"
971                 fi
972
973                 if ! do_snap_copy "${dev}" "${snap_mount}" "${snap_type}"
974                 then
975                         log_warning_msg "Impossible to include the ${snap_type} Snapshot"
976                         return 1
977                 else
978                         if [ -n "${snapfile}" ]
979                         then
980                                 # it was a loop device, user confused
981                                 umount ${snapdev}
982                         fi
983                 fi
984         else
985                 log_warning_msg "Impossible to include the ${snap_type} Snapshot"
986                 return 1
987         fi
988
989         echo "export ${snap_type}SNAP="${snap_mount}":${snapdev}:${snapfile}" >> /etc/live.conf # for resync on reboot/halt
990         return 0
991 }
992
993 setup_unionfs ()
994 {
995         image_directory="${1}"
996         rootmnt="${2}"
997         addimage_directory="${3}"
998         modprobe -q -b ${UNIONTYPE}
999
1000         # run-init can't deal with images in a subdir, but we're going to
1001         # move all of these away before it runs anyway.  No, we're not,
1002         # put them in / since move-mounting them into / breaks mono and
1003         # some other apps.
1004
1005         croot="/"
1006
1007         # Let's just mount the read-only file systems first
1008         rofsstring=""
1009         rofslist=""
1010         minor_kernel_version=$(uname -r|cut -c 5-|sed 's/[^0-9].*//')
1011
1012         if [ "${NETBOOT}" = "nfs" ] && [ "${minor_kernel_version}" -lt 22 ]
1013         then
1014                 # go aroung a bug in nfs-unionfs locking for unionfs <= 1.4
1015                 roopt="nfsro"
1016         elif [ "${UNIONTYPE}" = "aufs" ]
1017         then
1018                 roopt="rr"
1019         else
1020                 roopt="ro"
1021         fi
1022
1023         # Read image names from ${MODULE}.module if it exists
1024         if [ -e "${image_directory}/filesystem.${MODULE}.module" ]
1025         then
1026                 for IMAGE in $(cat ${image_directory}/filesystem.${MODULE}.module)
1027                 do
1028                         image_string="${image_string} ${image_directory}/${IMAGE}"
1029                 done
1030         elif [ -e "${image_directory}/${MODULE}.module" ]
1031         then
1032                 for IMAGE in $(cat ${image_directory}/${MODULE}.module)
1033                 do
1034                         image_string="${image_string} ${image_directory}/${IMAGE}"
1035                 done
1036         else
1037                 # ${MODULE}.module does not exist, create a list of images
1038                 for FILESYSTEM in squashfs ext2 ext3 xfs jffs2 dir
1039                 do
1040                         for IMAGE in "${image_directory}"/*."${FILESYSTEM}"
1041                         do
1042                                 if [ -e "${IMAGE}" ]
1043                                 then
1044                                         image_string="${image_string} ${IMAGE}"
1045                                 fi
1046                         done
1047                 done
1048
1049                 if [ -n "${addimage_directory}" ] && [ -d "${addimage_directory}" ]
1050                 then
1051                         for FILESYSTEM in squashfs ext2 ext3 xfs jffs2 dir
1052                         do
1053                                 for IMAGE in "${addimage_directory}"/*."${FILESYSTEM}"
1054                                 do
1055                                         if [ -e "${IMAGE}" ]
1056                                         then
1057                                                 image_string="${image_string} ${IMAGE}"
1058                                         fi
1059                                 done
1060                         done
1061                 fi
1062
1063                 # Now sort the list
1064                 image_string="$(echo ${image_string} | sed -e 's/ /\n/g' | sort )"
1065         fi
1066
1067         [ -n "${MODULETORAMFILE}" ] && image_string="${image_directory}/$(basename ${MODULETORAMFILE})"
1068
1069         mkdir -p "${croot}"
1070
1071         for image in ${image_string}
1072         do
1073                 imagename=$(basename "${image}")
1074
1075                 if [ -d "${image}" ]
1076                 then
1077                         # it is a plain directory: do nothing
1078                         rofsstring="${image}=${roopt}:${rofsstring}"
1079                         rofslist="${image} ${rofslist}"
1080                 elif [ -f "${image}" ]
1081                 then
1082                         if losetup --help 2>&1 | grep -q -- "-r\b"
1083                         then
1084                                 backdev=$(get_backing_device "${image}" "-r")
1085                         else
1086                                 backdev=$(get_backing_device "${image}")
1087                         fi
1088                         fstype=$(get_fstype "${backdev}")
1089
1090                         if [ "${fstype}" = "unknown" ]
1091                         then
1092                                 panic "Unknown file system type on ${backdev} (${image})"
1093                         fi
1094
1095                         if [ -z "${fstype}" ]
1096                         then
1097                                 fstype="${imagename##*.}"
1098                                 log_warning_msg "Unknown file system type on ${backdev} (${image}), assuming ${fstype}."
1099                         fi
1100
1101                         mkdir -p "${croot}/${imagename}"
1102                         log_begin_msg "Mounting \"${image}\" on \"${croot}${imagename}\" via \"${backdev}\""
1103                         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}"
1104                         log_end_msg
1105                 fi
1106         done
1107
1108         rofsstring=${rofsstring%:}
1109
1110         mkdir -p /cow
1111
1112         # Looking for "${root_persistence}" device or file
1113         if [ -n "${PERSISTENT}" ] && [ -z "${NOPERSISTENT}" ]
1114         then
1115                 # Load USB modules
1116                 num_block=$(ls -l /sys/block | wc -l)
1117                 for module in sd_mod uhci-hcd ehci-hcd ohci-hcd usb-storage
1118                 do
1119                         modprobe -q -b ${module}
1120                 done
1121
1122                 if [ -x /sbin/udevadm ]
1123                 then
1124                         # lenny
1125                         udevadm trigger
1126                         udevadm settle
1127                 else
1128                         # etch
1129                         udevtrigger
1130                         udevsettle
1131                 fi
1132
1133                 # For some reason, udevsettle does not block in this scenario,
1134                 # so we sleep for a little while.
1135                 #
1136                 # See https://bugs.launchpad.net/ubuntu/+source/casper/+bug/84591
1137                 for timeout in 5 4 3 2 1
1138                 do
1139                         sleep 1
1140
1141                         if [ $(ls -l /sys/block | wc -l) -gt ${num_block} ]
1142                         then
1143                                 break
1144                         fi
1145                 done
1146
1147                 # search for label and files (this could be hugely optimized)
1148                 cowprobe=$(find_cow_device "${root_persistence}")
1149                 if [ -b "${cowprobe}" ]
1150                 then
1151                         # Blacklist /cow device, to avoid inconsistent setups for overlapping snapshots
1152                         # makes sense to have both persistence for /cow and /home mounted, maybe also with
1153                         # snapshots to be sure to really store some e.g key config files,
1154                         # but not on the same media
1155                         blacklistdev="${cowprobe}"
1156                         PERSISTENCE_IS_ON="1"
1157                         export PERSISTENCE_IS_ON
1158                 fi
1159                 # homecow just mount something on /home, this should be generalized some way
1160                 homecow=$(find_cow_device "${home_persistence}" "${blacklistdev}")
1161                 if [ -b "${homecow}" ]
1162                 then
1163                         PERSISTENCE_IS_ON="1"
1164                         export PERSISTENCE_IS_ON
1165                 fi
1166                 root_snapdata=$(find_snap "${root_snapshot_label}" "${blacklistdev}")
1167                 # This second type should be removed when snapshot will get smarter,
1168                 # hence when "/etc/live-snapshot*list" will be supported also by
1169                 # ext2|ext3|jffs2 snapshot types.
1170                 home_snapdata=$(find_snap "${home_snapshot_label}" "${blacklistdev}")
1171
1172                 if [ -b "${cowprobe}" ]
1173                 then
1174                         cowdevice=${cowprobe}
1175                         cow_fstype=$(get_fstype "${cowprobe}")
1176                         cow_mountopt="rw,noatime"
1177                 else
1178                         log_warning_msg "Unable to find the persistent medium"
1179                         cowdevice="tmpfs"
1180                         cow_fstype="tmpfs"
1181                         cow_mountopt="rw,noatime,mode=755"
1182                 fi
1183         elif [ -n "${NFS_COW}" ] && [ -z "${NOPERSISTENT}" ]
1184         then
1185                 # check if there are any nfs options
1186                 if echo ${NFS_COW}|grep -q ','
1187                 then
1188                         nfs_cow_opts="-o nolock,$(echo ${NFS_COW}|cut -d, -f2-)"
1189                         nfs_cow=$(echo ${NFS_COW}|cut -d, -f1)
1190                 else
1191                         nfs_cow_opts="-o nolock"
1192                         nfs_cow=${NFS_COW}
1193                 fi
1194                 mac="$(get_mac)"
1195                 if [ -n "${mac}" ]
1196                 then
1197                         cowdevice=$(echo ${nfs_cow}|sed "s/client_mac_address/${mac}/")
1198                         cow_fstype="nfs"
1199                 else
1200                         panic "unable to determine mac address"
1201                 fi
1202         else
1203                 cowdevice="tmpfs"
1204                 cow_fstype="tmpfs"
1205                 cow_mountopt="rw,noatime,mode=755"
1206         fi
1207
1208         if [ "${cow_fstype}" = "nfs" ]
1209         then
1210                 log_begin_msg \
1211                         "Trying nfsmount ${nfs_cow_opts} ${cowdevice} /cow"
1212                 nfsmount ${nfs_cow_opts} ${cowdevice} /cow || \
1213                         panic "Can not mount ${cowdevice} (n: ${cow_fstype}) on /cow"
1214         else
1215                 mount -t ${cow_fstype} -o ${cow_mountopt} ${cowdevice} /cow || \
1216                         panic "Can not mount ${cowdevice} (o: ${cow_fstype}) on /cow"
1217         fi
1218
1219         rofscount=$(echo ${rofslist} |wc -w)
1220
1221         if [ -n "${EXPOSED_ROOT}" ]
1222         then
1223                 if [ ${rofscount} -ne 1 ]
1224                 then
1225                         panic "only one RO file system supported with exposedroot: ${rofslist}"
1226                 fi
1227                 exposedrootfs=${rofslist%% }
1228
1229                 mount --bind ${exposedrootfs} ${rootmnt} || \
1230                         panic "bind mount of ${exposedrootfs} failed"
1231
1232                 cow_dirs='/var/tmp /var/lock /var/run /var/log /var/spool
1233                         /home /var/lib/live'
1234
1235                 for dir in ${cow_dirs}; do
1236                         mkdir -p /cow${dir}
1237                         mount -t ${UNIONTYPE} \
1238                                 -o rw,noatime,dirs=/cow${dir}=rw:${exposedrootfs}${dir}=ro \
1239                                 ${UNIONTYPE} "${rootmnt}${dir}" || \
1240                                 panic "mount ${UNIONTYPE} on ${rootmnt}${dir} failed with option \
1241                                         rw,noatime,dirs=/cow${dir}=rw:${exposedrootfs}${dir}=ro"
1242                 done
1243         else
1244                 mount -t ${UNIONTYPE} -o noatime,dirs=/cow=rw:${rofsstring} \
1245                         ${UNIONTYPE} "${rootmnt}" || panic "mount ${UNIONTYPE} on \
1246                         ${rootmnt} failed with option noatime,dirs=/cow=rw:${rofsstring}"
1247         fi
1248
1249         # tmpfs file systems
1250         touch /etc/fstab
1251         mkdir -p "${rootmnt}/live"
1252         mount -t tmpfs tmpfs ${rootmnt}/live
1253
1254         # Adding other custom mounts
1255         if [ -n "${PERSISTENT}" ] && [ -z "${NOPERSISTENT}" ]
1256         then
1257                 # directly mount /home
1258                 # FIXME: add a custom mounts configurable system
1259
1260                 if [ -b "${homecow}" ]
1261                 then
1262                         mount -t $(get_fstype "${homecow}") -o rw,noatime "${homecow}" "${rootmnt}/home"
1263                         export HOMEMOUNTED=1 # used to proper calculate free space in do_snap_copy()
1264                 else
1265                         log_warning_msg "Unable to find the persistent home medium"
1266                 fi
1267
1268                 # Look for other snapshots to copy in
1269                 try_snap "${root_snapdata}" "${rootmnt}" "ROOT"
1270                 # This second type should be removed when snapshot grow smarter
1271                 try_snap "${home_snapdata}" "${rootmnt}/home" "HOME"
1272         fi
1273
1274         if [ -n "${SHOWMOUNTS}" ]
1275         then
1276                 for d in ${rofslist}
1277                 do
1278                         mkdir -p "${rootmnt}/live/${d##*/}"
1279
1280                         case d in
1281                                 *.dir)
1282                                         # do nothing # mount -o bind "${d}" "${rootmnt}/live/${d##*/}"
1283                                         ;;
1284
1285                                 *)
1286                                         mount -o move "${d}" "${rootmnt}/live/${d##*/}"
1287                                         ;;
1288                         esac
1289                 done
1290         fi
1291
1292         # shows cow fs on /cow for use by live-snapshot
1293         mkdir -p "${rootmnt}/live/cow"
1294         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"
1295 }
1296
1297 check_dev ()
1298 {
1299         sysdev="${1}"
1300         devname="${2}"
1301         skip_uuid_check="${3}"
1302
1303         if [ -z "${devname}" ]
1304         then
1305                 devname=$(sys2dev "${sysdev}")
1306         fi
1307
1308         if [ -d "${devname}" ]
1309         then
1310                 mount -o bind "${devname}" $mountpoint || continue
1311
1312                 if is_live_path $mountpoint
1313                 then
1314                         echo $mountpoint
1315                         return 0
1316                 else
1317                         umount $mountpoint
1318                 fi
1319         fi
1320
1321         if [ -n "${LIVE_MEDIA_OFFSET}" ]
1322         then
1323                 loopdevname=$(setup_loop "${devname}" "loop" "/sys/block/loop*" "${LIVE_MEDIA_OFFSET}" '')
1324                 devname="${loopdevname}"
1325         fi
1326
1327         fstype=$(get_fstype "${devname}")
1328
1329         if is_supported_fs ${fstype}
1330         then
1331                 mount -t ${fstype} -o ro,noatime "${devname}" ${mountpoint} || continue
1332
1333                 if is_live_path ${mountpoint} && \
1334                         ([ "${skip_uuid_check}" ] || matches_uuid ${mountpoint})
1335                 then
1336                         echo ${mountpoint}
1337                         return 0
1338                 else
1339                         umount ${mountpoint}
1340                 fi
1341         fi
1342
1343         if [ -n "${LIVE_MEDIA_OFFSET}" ]
1344         then
1345                 losetup -d "${loopdevname}"
1346         fi
1347
1348         return 1
1349 }
1350
1351 find_livefs ()
1352 {
1353         timeout="${1}"
1354
1355         # first look at the one specified in the command line
1356         if [ ! -z "${LIVE_MEDIA}" ]
1357         then
1358                 if check_dev "null" "${LIVE_MEDIA}" "skip_uuid_check"
1359                 then
1360                         return 0
1361                 fi
1362         fi
1363
1364         # don't start autodetection before timeout has expired
1365         if [ -n "${LIVE_MEDIA_TIMEOUT}" ]
1366         then
1367                 if [ "${timeout}" -lt "${LIVE_MEDIA_TIMEOUT}" ]
1368                 then
1369                         return 1
1370                 fi
1371         fi
1372
1373         # or do the scan of block devices
1374         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1375         do
1376                 devname=$(sys2dev "${sysblock}")
1377                 fstype=$(get_fstype "${devname}")
1378
1379                 if /lib/udev/cdrom_id ${devname} > /dev/null
1380                 then
1381                         if check_dev "null" "${devname}"
1382                         then
1383                                 return 0
1384                         fi
1385                 elif is_nice_device "${sysblock}"
1386                 then
1387                         for dev in $(subdevices "${sysblock}")
1388                         do
1389                                 if check_dev "${dev}"
1390                                 then
1391                                         return 0
1392                                 fi
1393                         done
1394                 elif [ "${fstype}" = "squashfs" -o \
1395                         "${fstype}" = "ext2" -o \
1396                         "${fstype}" = "ext3" -o \
1397                         "${fstype}" = "jffs2" ]
1398                 then
1399                         # This is an ugly hack situation, the block device has
1400                         # an image directly on it.  It's hopefully
1401                         # live-initramfs, so take it and run with it.
1402                         ln -s "${devname}" "${devname}.${fstype}"
1403                         echo "${devname}.${fstype}"
1404                         return 0
1405                 fi
1406         done
1407
1408         return 1
1409 }
1410
1411 set_usplash_timeout ()
1412 {
1413         if [ -x /sbin/usplash_write ]
1414         then
1415                 /sbin/usplash_write "TIMEOUT 120"
1416         else if [ -x /sbin/splashy_update ] ; then
1417                 /sbin/splashy_update "TIMEOUT 120"
1418         fi ; fi
1419 }
1420
1421 mountroot ()
1422 {
1423         if [ -x /scripts/local-top/cryptroot ]; then
1424             /scripts/local-top/cryptroot
1425         fi
1426
1427         exec 6>&1
1428         exec 7>&2
1429         exec > live.log
1430         exec 2>&1
1431         tail -f live.log >&7 &
1432         tailpid="${!}"
1433
1434         # Ensure 'panic' function is overridden
1435         . /scripts/live-functions
1436
1437         Arguments
1438
1439         set_usplash_timeout
1440
1441         maybe_break live-premount
1442         log_begin_msg "Running /scripts/live-premount"
1443         run_scripts /scripts/live-premount
1444         log_end_msg
1445
1446         # Needed here too because some things (*cough* udev *cough*)
1447         # changes the timeout
1448
1449         set_usplash_timeout
1450
1451         if [ ! -z "${NETBOOT}" ] || [ ! -z "${FETCH}" ] || [ ! -z "${HTTPFS}" ] || [ ! -z "${FTPFS}" ]
1452         then
1453                 if do_netmount
1454                 then
1455                         livefs_root="${mountpoint}"
1456                 else
1457                         panic "Unable to find a live file system on the network"
1458                 fi
1459         else
1460                 if [ -n "${PLAIN_ROOT}" ] && [ -n "${ROOT}" ]
1461                 then
1462                         # Do a local boot from hd
1463                         livefs_root=${ROOT}
1464                 else
1465                         # Scan local devices for the image
1466                         i=0
1467                         while [ "$i" -lt 60 ]
1468                         do
1469                                 livefs_root=$(find_livefs ${i})
1470
1471                                 if [ -n "${livefs_root}" ]
1472                                 then
1473                                         break
1474                                 fi
1475
1476                                 sleep 1
1477                                 i="$(($i + 1))"
1478                         done
1479                 fi
1480         fi
1481
1482         if [ -z "${livefs_root}" ]
1483         then
1484                 panic "Unable to find a medium containing a live file system"
1485         fi
1486
1487         if [ "${TORAM}" ]
1488         then
1489                 live_dest="ram"
1490         elif [ "${TODISK}" ]
1491         then
1492                 live_dest="${TODISK}"
1493         fi
1494
1495         if [ "${live_dest}" ]
1496         then
1497                 log_begin_msg "Copying live media to ${live_dest}"
1498                 copy_live_to "${livefs_root}" "${live_dest}"
1499                 log_end_msg
1500         fi
1501
1502         if [ -n "${MODULETORAMFILE}" ]
1503         then
1504                 setup_unionfs "${livefs_root}" "${rootmnt}"
1505         else
1506                 mac="$(get_mac)"
1507                 mac="$(echo ${mac} | sed 's/-//g')"
1508                 mount_images_in_directory "${livefs_root}" "${rootmnt}" "${mac}"
1509         fi
1510
1511         log_end_msg
1512
1513         maybe_break live-bottom
1514         log_begin_msg "Running /scripts/live-bottom"
1515
1516         run_scripts /scripts/live-bottom
1517         log_end_msg
1518
1519         exec 1>&6 6>&-
1520         exec 2>&7 7>&-
1521         kill ${tailpid}
1522         cp live.log "${rootmnt}/var/log/"
1523 }