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