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