Implemented a snapshot exclude list.
[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 "${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) + $( cat /proc/meminfo | grep Cached | head -n 1 | awk '/Cached/{print $2}' - ) )
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=$(cat /proc/mounts | grep -s " $(base_path ${todir}) " | awk '{print $1}' )
850                         freespace=$(df -k  | grep -s ${todev} | awk '{print $4}')
851                 else
852                         freespace=$( expr $(awk '/MemFree/{print $2}' /proc/meminfo) + $( cat /proc/meminfo | grep Cached | head -n 1 | awk '/Cached/{print $2}' - ))
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=$(cat /proc/mounts | awk '{print $2,$1}' | grep -es "^$( dirname ${snaploop} )" | cut -f2 -d ' ')
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                 fi
1155                 # homecow just mount something on /home, this should be generalized some way
1156                 homecow=$(find_cow_device "${home_persistence}" "${blacklistdev}")
1157                 root_snapdata=$(find_snap "${root_snapshot_label}" "${blacklistdev}")
1158                 # This second type should be removed when snapshot will get smarter,
1159                 # hence when "/etc/live-snapshot*list" will be supported also by
1160                 # ext2|ext3|jffs2 snapshot types.
1161                 home_snapdata=$(find_snap "${home_snapshot_label}" "${blacklistdev}")
1162
1163                 if [ -b "${cowprobe}" ]
1164                 then
1165                         cowdevice=${cowprobe}
1166                         cow_fstype=$(get_fstype "${cowprobe}")
1167                         cow_mountopt="rw,noatime"
1168                 else
1169                         log_warning_msg "Unable to find the persistent medium"
1170                         cowdevice="tmpfs"
1171                         cow_fstype="tmpfs"
1172                         cow_mountopt="rw,noatime,mode=755"
1173                 fi
1174         elif [ -n "${NFS_COW}" ] && [ -z "${NOPERSISTENT}" ]
1175         then
1176                 # check if there are any nfs options
1177                 if echo ${NFS_COW}|grep -q ','
1178                 then
1179                         nfs_cow_opts="-o nolock,$(echo ${NFS_COW}|cut -d, -f2-)"
1180                         nfs_cow=$(echo ${NFS_COW}|cut -d, -f1)
1181                 else
1182                         nfs_cow_opts="-o nolock"
1183                         nfs_cow=${NFS_COW}
1184                 fi
1185                 mac="$(get_mac)"
1186                 if [ -n "${mac}" ]
1187                 then
1188                         cowdevice=$(echo ${nfs_cow}|sed "s/client_mac_address/${mac}/")
1189                         cow_fstype="nfs"
1190                 else
1191                         panic "unable to determine mac address"
1192                 fi
1193         else
1194                 cowdevice="tmpfs"
1195                 cow_fstype="tmpfs"
1196                 cow_mountopt="rw,noatime,mode=755"
1197         fi
1198
1199         if [ "${cow_fstype}" = "nfs" ]
1200         then
1201                 log_begin_msg \
1202                         "Trying nfsmount ${nfs_cow_opts} ${cowdevice} /cow"
1203                 nfsmount ${nfs_cow_opts} ${cowdevice} /cow || \
1204                         panic "Can not mount ${cowdevice} (n: ${cow_fstype}) on /cow"
1205         else
1206                 mount -t ${cow_fstype} -o ${cow_mountopt} ${cowdevice} /cow || \
1207                         panic "Can not mount ${cowdevice} (o: ${cow_fstype}) on /cow"
1208         fi
1209
1210         rofscount=$(echo ${rofslist} |wc -w)
1211
1212         if [ -n "${EXPOSED_ROOT}" ]
1213         then
1214                 if [ ${rofscount} -ne 1 ]
1215                 then
1216                         panic "only one RO file system supported with exposedroot: ${rofslist}"
1217                 fi
1218                 exposedrootfs=${rofslist%% }
1219
1220                 mount --bind ${exposedrootfs} ${rootmnt} || \
1221                         panic "bind mount of ${exposedrootfs} failed"
1222
1223                 cow_dirs='/var/tmp /var/lock /var/run /var/log /var/spool
1224                         /home /var/lib/live'
1225
1226                 for dir in ${cow_dirs}; do
1227                         mkdir -p /cow${dir}
1228                         mount -t ${UNIONTYPE} \
1229                                 -o rw,noatime,dirs=/cow${dir}=rw:${exposedrootfs}${dir}=ro \
1230                                 ${UNIONTYPE} "${rootmnt}${dir}" || \
1231                                 panic "mount ${UNIONTYPE} on ${rootmnt}${dir} failed with option \
1232                                         rw,noatime,dirs=/cow${dir}=rw:${exposedrootfs}${dir}=ro"
1233                 done
1234         else
1235                 mount -t ${UNIONTYPE} -o noatime,dirs=/cow=rw:${rofsstring} \
1236                         ${UNIONTYPE} "${rootmnt}" || panic "mount ${UNIONTYPE} on \
1237                         ${rootmnt} failed with option noatime,dirs=/cow=rw:${rofsstring}"
1238         fi
1239
1240         # tmpfs file systems
1241         touch /etc/fstab
1242         mkdir -p "${rootmnt}/live"
1243         mount -t tmpfs tmpfs ${rootmnt}/live
1244
1245         # Adding other custom mounts
1246         if [ -n "${PERSISTENT}" ] && [ -z "${NOPERSISTENT}" ]
1247         then
1248                 # directly mount /home
1249                 # FIXME: add a custom mounts configurable system
1250
1251                 if [ -b "${homecow}" ]
1252                 then
1253                         mount -t $(get_fstype "${homecow}") -o rw,noatime "${homecow}" "${rootmnt}/home"
1254                         export HOMEMOUNTED=1 # used to proper calculate free space in do_snap_copy()
1255                 else
1256                         log_warning_msg "Unable to find the persistent home medium"
1257                 fi
1258
1259                 # Look for other snapshots to copy in
1260                 try_snap "${root_snapdata}" "${rootmnt}" "ROOT"
1261                 # This second type should be removed when snapshot grow smarter
1262                 try_snap "${home_snapdata}" "${rootmnt}/home" "HOME"
1263         fi
1264
1265         if [ -n "${SHOWMOUNTS}" ]
1266         then
1267                 for d in ${rofslist}
1268                 do
1269                         mkdir -p "${rootmnt}/live/${d##*/}"
1270
1271                         case d in
1272                                 *.dir)
1273                                         # do nothing # mount -o bind "${d}" "${rootmnt}/live/${d##*/}"
1274                                         ;;
1275
1276                                 *)
1277                                         mount -o move "${d}" "${rootmnt}/live/${d##*/}"
1278                                         ;;
1279                         esac
1280                 done
1281         fi
1282
1283         # shows cow fs on /cow for use by live-snapshot
1284         mkdir -p "${rootmnt}/live/cow"
1285         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"
1286 }
1287
1288 check_dev ()
1289 {
1290         sysdev="${1}"
1291         devname="${2}"
1292         skip_uuid_check="${3}"
1293
1294         if [ -z "${devname}" ]
1295         then
1296                 devname=$(sys2dev "${sysdev}")
1297         fi
1298
1299         if [ -d "${devname}" ]
1300         then
1301                 mount -o bind "${devname}" $mountpoint || continue
1302
1303                 if is_live_path $mountpoint
1304                 then
1305                         echo $mountpoint
1306                         return 0
1307                 else
1308                         umount $mountpoint
1309                 fi
1310         fi
1311
1312         if [ -n "${LIVE_MEDIA_OFFSET}" ]
1313         then
1314                 loopdevname=$(setup_loop "${devname}" "loop" "/sys/block/loop*" "${LIVE_MEDIA_OFFSET}" '')
1315                 devname="${loopdevname}"
1316         fi
1317
1318         fstype=$(get_fstype "${devname}")
1319
1320         if is_supported_fs ${fstype}
1321         then
1322                 mount -t ${fstype} -o ro,noatime "${devname}" ${mountpoint} || continue
1323
1324                 if is_live_path ${mountpoint} && \
1325                         ([ "${skip_uuid_check}" ] || matches_uuid ${mountpoint})
1326                 then
1327                         echo ${mountpoint}
1328                         return 0
1329                 else
1330                         umount ${mountpoint}
1331                 fi
1332         fi
1333
1334         if [ -n "${LIVE_MEDIA_OFFSET}" ]
1335         then
1336                 losetup -d "${loopdevname}"
1337         fi
1338
1339         return 1
1340 }
1341
1342 find_livefs ()
1343 {
1344         timeout="${1}"
1345
1346         # first look at the one specified in the command line
1347         if [ ! -z "${LIVE_MEDIA}" ]
1348         then
1349                 if check_dev "null" "${LIVE_MEDIA}" "skip_uuid_check"
1350                 then
1351                         return 0
1352                 fi
1353         fi
1354
1355         # don't start autodetection before timeout has expired
1356         if [ -n "${LIVE_MEDIA_TIMEOUT}" ]
1357         then
1358                 if [ "${timeout}" -lt "${LIVE_MEDIA_TIMEOUT}" ]
1359                 then
1360                         return 1
1361                 fi
1362         fi
1363
1364         # or do the scan of block devices
1365         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram | grep -v 'dm-' | grep -v fd )
1366         do
1367                 devname=$(sys2dev "${sysblock}")
1368                 fstype=$(get_fstype "${devname}")
1369
1370                 if /lib/udev/cdrom_id ${devname} > /dev/null
1371                 then
1372                         if check_dev "null" "${devname}"
1373                         then
1374                                 return 0
1375                         fi
1376                 elif is_nice_device "${sysblock}"
1377                 then
1378                         for dev in $(subdevices "${sysblock}")
1379                         do
1380                                 if check_dev "${dev}"
1381                                 then
1382                                         return 0
1383                                 fi
1384                         done
1385                 elif [ "${fstype}" = "squashfs" -o \
1386                         "${fstype}" = "ext2" -o \
1387                         "${fstype}" = "ext3" -o \
1388                         "${fstype}" = "jffs2" ]
1389                 then
1390                         # This is an ugly hack situation, the block device has
1391                         # an image directly on it.  It's hopefully
1392                         # live-initramfs, so take it and run with it.
1393                         ln -s "${devname}" "${devname}.${fstype}"
1394                         echo "${devname}.${fstype}"
1395                         return 0
1396                 fi
1397         done
1398
1399         return 1
1400 }
1401
1402 set_usplash_timeout ()
1403 {
1404         if [ -x /sbin/usplash_write ]
1405         then
1406                 /sbin/usplash_write "TIMEOUT 120"
1407         else if [ -x /sbin/splashy_update ] ; then
1408                 /sbin/splashy_update "TIMEOUT 120"
1409         fi ; fi
1410 }
1411
1412 mountroot ()
1413 {
1414         if [ -x /scripts/local-top/cryptroot ]; then
1415             /scripts/local-top/cryptroot
1416         fi
1417
1418         exec 6>&1
1419         exec 7>&2
1420         exec > live.log
1421         exec 2>&1
1422         tail -f live.log >&7 &
1423         tailpid="${!}"
1424
1425         # Ensure 'panic' function is overridden
1426         . /scripts/live-functions
1427
1428         Arguments
1429
1430         set_usplash_timeout
1431
1432         maybe_break live-premount
1433         log_begin_msg "Running /scripts/live-premount"
1434         run_scripts /scripts/live-premount
1435         log_end_msg
1436
1437         # Needed here too because some things (*cough* udev *cough*)
1438         # changes the timeout
1439
1440         set_usplash_timeout
1441
1442         if [ ! -z "${NETBOOT}" ] || [ ! -z "${FETCH}" ] || [ ! -z "${HTTPFS}" ] || [ ! -z "${FTPFS}" ]
1443         then
1444                 if do_netmount
1445                 then
1446                         livefs_root="${mountpoint}"
1447                 else
1448                         panic "Unable to find a live file system on the network"
1449                 fi
1450         else
1451                 if [ -n "${PLAIN_ROOT}" ] && [ -n "${ROOT}" ]
1452                 then
1453                         # Do a local boot from hd
1454                         livefs_root=${ROOT}
1455                 else
1456                         # Scan local devices for the image
1457                         for i in 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
1458                         do
1459                                 livefs_root=$(find_livefs ${i})
1460
1461                                 if [ -n "${livefs_root}" ]
1462                                 then
1463                                         break
1464                                 fi
1465
1466                                 sleep 1
1467                         done
1468                 fi
1469         fi
1470
1471         if [ -z "${livefs_root}" ]
1472         then
1473                 panic "Unable to find a medium containing a live file system"
1474         fi
1475
1476         if [ "${TORAM}" ]
1477         then
1478                 live_dest="ram"
1479         elif [ "${TODISK}" ]
1480         then
1481                 live_dest="${TODISK}"
1482         fi
1483
1484         if [ "${live_dest}" ]
1485         then
1486                 log_begin_msg "Copying live media to ${live_dest}"
1487                 copy_live_to "${livefs_root}" "${live_dest}"
1488                 log_end_msg
1489         fi
1490
1491         if [ -n "${MODULETORAMFILE}" ]
1492         then
1493                 setup_unionfs "${livefs_root}" "${rootmnt}"
1494         else
1495                 mac="$(get_mac)"
1496                 mac="$(echo ${mac} | sed 's/-//g')"
1497                 mount_images_in_directory "${livefs_root}" "${rootmnt}" "${mac}"
1498         fi
1499
1500         log_end_msg
1501
1502         maybe_break live-bottom
1503         log_begin_msg "Running /scripts/live-bottom"
1504
1505         run_scripts /scripts/live-bottom
1506         log_end_msg
1507
1508         exec 1>&6 6>&-
1509         exec 2>&7 7>&-
1510         kill ${tailpid}
1511         cp live.log "${rootmnt}/var/log/"
1512 }