Forwardporting and extending usage of rsync when booting with toram based on a patch...
[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                         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-)"
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         log_begin_msg "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                         if [ -x /bin/rsync ]
690                         then
691                                 echo " * Copying $MODULETORAMFILE to RAM" 1>/dev/console
692                                 rsync -a --progress ${MODULETORAMFILE} ${copyto} 1>/dev/console # copy only the filesystem module
693                         else
694                                 cp ${MODULETORAMFILE} ${copyto} # copy only the filesystem module
695                         fi
696                 else
697                         if [ -x /bin/rsync ]
698                         then
699                                 echo " * Copying whole medium to RAM" 1>/dev/console
700                                 rsync -a --progress ${copyfrom}/* ${copyto} 1>/dev/console  # "cp -a" from busybox also copies hidden files
701                         else
702                                 mkdir -p ${copyto}/${LIVE_MEDIA_PATH}
703                                 cp -a ${copyfrom}/${LIVE_MEDIA_PATH}/* ${copyto}/${LIVE_MEDIA_PATH}   # "cp -a" from busybox also copies hidden files
704                         fi
705                 fi
706
707                 umount ${copyfrom}
708                 mount -r -o move ${copyto} ${copyfrom}
709         fi
710
711         rmdir ${copyto}
712         return 0
713 }
714
715 do_netmount ()
716 {
717         rc=1
718
719         modprobe -q af_packet # For DHCP
720
721         if [ -x /sbin/udevadm ]
722         then
723                 # lenny
724                 udevadm trigger
725                 udevadm settle
726         else
727                 # etch
728                 udevtrigger
729                 udevsettle
730         fi
731
732         ipconfig ${DEVICE} | tee /netboot.config
733
734         # source relevant ipconfig output
735         OLDHOSTNAME=${HOSTNAME}
736         . /tmp/net-${DEVICE}.conf
737         [ -z ${HOSTNAME} ] && HOSTNAME=${OLDHOSTNAME}
738         export HOSTNAME
739
740         # Check if we have a network device at all
741         if ! ls /sys/class/net/eth0 > /dev/null 2>&1 && \
742            ! ls /sys/class/net/wlan0 > /dev/null 2>&1 && \
743            ! ls /sys/class/net/ath0 > /dev/null 2>&1 && \
744            ! ls /sys/class/net/ra0 > /dev/null 2>&1
745         then
746                 panic "No supported network device found, maybe a non-mainline driver is required."
747         fi
748
749         if [ "${NFSROOT}" = "auto" ]
750         then
751                 NFSROOT=${ROOTSERVER}:${ROOTPATH}
752         fi
753
754         if ( [ -n "${FETCH}" ] || [ -n "${HTTPFS}" ] || [ -n "${FTPFS}" ] ) && do_httpmount
755         then
756                 rc=0
757                 return ${rc}
758         fi
759
760         if [ "${NFSROOT#*:}" = "${NFSROOT}" ] && [ "$NETBOOT" != "cifs" ]
761         then
762                 NFSROOT=${ROOTSERVER}:${NFSROOT}
763         fi
764
765         log_begin_msg "Trying netboot from ${NFSROOT}"
766
767         if [ "${NETBOOT}" != "nfs" ] && do_cifsmount
768         then
769                 rc=0
770         elif do_nfsmount
771         then
772                 NETBOOT="nfs"
773                 export NETBOOT
774                 rc=0
775         fi
776
777         log_end_msg
778         return ${rc}
779 }
780
781 do_httpmount ()
782 {
783         rc=1
784         dest="${mountpoint}/${LIVE_MEDIA_PATH}"
785         mount -t ramfs ram "${mountpoint}"
786         mkdir -p "${dest}"
787
788         for webfile in HTTPFS FTPFS FETCH
789         do
790                 url="$(eval echo \"\$\{${webfile}\}\")"
791                 extension="$(echo "${url}" | sed 's/\(.*\)\.\(.*\)/\2/')"
792
793                 if [ -n "$url" ]
794                 then
795                         case "${extension}" in
796                                 squashfs|tgz|tar)
797                                         if [ "${webfile}" = "FETCH" ]
798                                         then
799                                                 case "$url" in
800                                                         tftp*)
801                                                                 ip="$(dirname $url | sed -e 's|tftp://||g' -e 's|/.*$||g')"
802                                                                 rfile="$(echo $url | sed -e "s|tftp://$ip||g")"
803                                                                 lfile="$(basename $url)"
804                                                                 log_begin_msg "Trying tftp -g -b 10240 -r $rfile -l ${dest}/$lfile $ip"
805                                                                 tftp -g -b 10240 -r $rfile -l ${dest}/$lfile $ip
806                                                         ;;
807
808                                                         *)
809                                                                 log_begin_msg "Trying wget ${url} -O ${dest}/$(basename ${url})"
810                                                                 wget "${url}" -O "${dest}/$(basename ${url})"
811                                                                 ;;
812                                                 esac
813                                         else
814                                                 log_begin_msg "Trying to mount ${url} on ${dest}/$(basename ${url})"
815                                                 if [ "${webfile}" = "FTPFS" ]
816                                                 then
817                                                         FUSE_MOUNT="curlftpfs"
818                                                         url="$(dirname ${url})"
819                                                 else
820                                                         FUSE_MOUNT="httpfs"
821                                                 fi
822                                                 modprobe fuse
823                                                 $FUSE_MOUNT "${url}" "${dest}"
824                                         fi
825                                         [ ${?} -eq 0 ] && rc=0
826                                         [ "${extension}" = "tgz" ] && live_dest="ram"
827                                         break
828                                         ;;
829
830                                 *)
831                                         log_begin_msg "Unrecognized archive extension for ${url}"
832                                         ;;
833                         esac
834                 fi
835         done
836
837         if [ ${rc} != 0 ]
838         then
839             umount "${mountpoint}"
840         fi
841
842         return ${rc}
843 }
844
845 do_nfsmount ()
846 {
847         rc=1
848
849         modprobe -q nfs
850
851         if [ -z "${NFSOPTS}" ]
852         then
853                 NFSOPTS=""
854         fi
855
856         log_begin_msg "Trying nfsmount -o nolock -o ro ${NFSOPTS} ${NFSROOT} ${mountpoint}"
857
858         # FIXME: This while loop is an ugly HACK round an nfs bug
859         i=0
860         while [ "$i" -lt 60 ]
861         do
862                 nfsmount -o nolock -o ro ${NFSOPTS} "${NFSROOT}" "${mountpoint}" && rc=0 && break
863                 sleep 1
864                 i="$(($i + 1))"
865         done
866
867         return ${rc}
868 }
869
870 do_cifsmount ()
871 {
872         rc=1
873
874         if [ -x "/sbin/mount.cifs" ]
875         then
876                 if [ -z "${NFSOPTS}" ]
877                 then
878                         CIFSOPTS="-ouser=root,password="
879                 else
880                         CIFSOPTS="${NFSOPTS}"
881                 fi
882
883                 log_begin_msg "Trying mount.cifs ${NFSROOT} ${mountpoint} ${CIFSOPTS}"
884                 modprobe -q cifs
885
886                 if mount.cifs "${NFSROOT}" "${mountpoint}" "${CIFSOPTS}"
887                 then
888                         rc=0
889                 fi
890         fi
891
892         return ${rc}
893 }
894
895 do_snap_copy ()
896 {
897         fromdev="${1}"
898         todir="${2}"
899         snap_type="${3}"
900         size=$(fs_size "${fromdev}" "" "used")
901
902         if [ -b "${fromdev}" ]
903         then
904                 # look for free mem
905                 if [ -n "${HOMEMOUNTED}" -a "${snap_type}" = "HOME" ]
906                 then
907                         todev=$(awk -v pat="$(base_path ${todir})" '$2 == pat { print $1 }' /proc/mounts)
908                         freespace=$(df -k | awk '/'${todev}'/{print $4}')
909                 else
910                         freespace=$( expr $(awk '/MemFree/{print $2}' /proc/meminfo) + $( awk '/\<Cached/{print $2}' /proc/meminfo))
911                 fi
912
913                 tomount="/mnt/tmpsnap"
914
915                 if [ ! -d "${tomount}" ]
916                 then
917                         mkdir -p "${tomount}"
918                 fi
919
920                 fstype=$(get_fstype "${fromdev}")
921
922                 if [ -n "${fstype}" ]
923                 then
924                         # Copying stuff...
925                         mount -o ro -t "${fstype}" "${fromdev}" "${tomount}" || log_warning_msg "Error in mount -t ${fstype} -o ro ${fromdev} ${tomount}"
926                         cp -a "${tomount}"/* ${todir}
927                         umount "${tomount}"
928                 else
929                         log_warning_msg "Unrecognized fstype: ${fstype} on ${fromdev}:${snap_type}"
930                 fi
931
932                 rmdir "${tomount}"
933
934                 if echo ${fromdev} | grep -qs loop
935                 then
936                         losetup -d "${fromdev}"
937                 fi
938
939                 return 0
940         else
941                 return 1
942
943                 log_warning_msg "Unable to find the snapshot ${snap_type} medium"
944         fi
945 }
946
947 find_snap ()
948 {
949         # Look for ${snap_label}.* in block devices
950         snap_label="${1}"
951
952         if [ "${PERSISTENT}" != "nofiles" ]
953         then
954                 # search for image files
955                 snapdata=$(find_files "${snap_label}.squashfs ${snap_label}.cpio.gz ${snap_label}.ext2 ${snap_label}.ext3 ${snap_label}.ext4 ${snap_label}.jffs2")
956         fi
957
958         if [ -z "${snapdata}" ]
959         then
960                 snapdata=$(find_cow_device "${snap_label}")
961         fi
962         echo "${snapdata}"
963 }
964
965 try_snap ()
966 {
967         # copy the contents of previously found snapshot to ${snap_mount}
968         # and remember the device and filename for resync on exit in live-initramfs.init
969
970         snapdata="${1}"
971         snap_mount="${2}"
972         snap_type="${3}"
973
974         if [ -n "${snapdata}" ] && [ ! -b "${snapdata}" ]
975         then
976                 log_success_msg "found snapshot: ${snapdata}"
977                 snapdev="$(echo ${snapdata} | cut -f1 -d ' ')"
978                 snapback="$(echo ${snapdata} | cut -f2 -d ' ')"
979                 snapfile="$(echo ${snapdata} | cut -f3 -d ' ')"
980
981                 RES=""
982                 if ! try_mount "${snapdev}" "${snapback}" "ro"
983                 then
984                         break
985                 fi
986
987                 if echo "${snapfile}" | grep -qs '\(squashfs\|ext2\|ext3\|ext4\|jffs2\)'
988                 then
989                         # squashfs, jffs2 or ext2/ext3/ext4 snapshot
990                         dev=$(get_backing_device "${snapback}/${snapfile}")
991
992                         do_snap_copy "${dev}" "${snap_mount}" "${snap_type}"
993                         RES=$?
994                 else
995                         # cpio.gz snapshot
996                         cd "${snap_mount}"
997                         zcat "${snapback}/${snapfile}" | /bin/cpio --extract --preserve-modification-time --no-absolute-filenames --sparse --unconditional --make-directories > /dev/null 2>&1
998                         RES=$?
999                         if [ "${RES}" != "0" ]
1000                         then
1001                                 log_warning_msg "failure to \"zcat ${snapback}/${snapfile} | /bin/cpio --extract --preserve-modification-time --no-absolute-filenames --sparse --unconditional --make-directories\""
1002                         fi
1003                         cd "${OLDPWD}"
1004                 fi
1005
1006                 umount "${snapback}" ||  log_warning_msg "failure to \"umount ${snapback}\""
1007
1008                 if [ "${RES}" != "0" ]
1009                 then
1010                         log_warning_msg "Impossible to include the ${snapfile} Snapshot file"
1011                 fi
1012
1013         elif [ -b "${snapdata}" ]
1014         then
1015                 # Try to find if it could be a snapshot partition
1016                 dev="${snapdata}"
1017                 log_success_msg "found snapshot ${snap_type} device on ${dev}"
1018                 if echo "${dev}" | grep -qs loop
1019                 then
1020                         # strange things happens, user confused?
1021                         snaploop=$( losetup ${dev} | awk '{print $3}' | tr -d '()' )
1022                         snapfile=$(basename ${snaploop})
1023                         snapdev=$(awk -v pat="$( dirname ${snaploop})" '$2 == pat { print $1 }' /proc/mounts)
1024                 else
1025                         snapdev="${dev}"
1026                 fi
1027
1028                 if ! do_snap_copy "${dev}" "${snap_mount}" "${snap_type}"
1029                 then
1030                         log_warning_msg "Impossible to include the ${snap_type} Snapshot"
1031                         return 1
1032                 else
1033                         if [ -n "${snapfile}" ]
1034                         then
1035                                 # it was a loop device, user confused
1036                                 umount ${snapdev}
1037                         fi
1038                 fi
1039         else
1040                 log_warning_msg "Impossible to include the ${snap_type} Snapshot"
1041                 return 1
1042         fi
1043
1044         echo "export ${snap_type}SNAP="${snap_mount}":${snapdev}:${snapfile}" >> /etc/live.conf # for resync on reboot/halt
1045         return 0
1046 }
1047
1048 setup_unionfs ()
1049 {
1050         image_directory="${1}"
1051         rootmnt="${2}"
1052         addimage_directory="${3}"
1053         modprobe -q -b ${UNIONTYPE}
1054
1055         # run-init can't deal with images in a subdir, but we're going to
1056         # move all of these away before it runs anyway.  No, we're not,
1057         # put them in / since move-mounting them into / breaks mono and
1058         # some other apps.
1059
1060         croot="/"
1061
1062         # Let's just mount the read-only file systems first
1063         rofsstring=""
1064         rofslist=""
1065         minor_kernel_version=$(uname -r|cut -c 5-|sed 's/[^0-9].*//')
1066
1067         if [ "${NETBOOT}" = "nfs" ] && [ "${minor_kernel_version}" -lt 22 ]
1068         then
1069                 # go aroung a bug in nfs-unionfs locking for unionfs <= 1.4
1070                 roopt="nfsro"
1071         elif [ "${UNIONTYPE}" = "aufs" ]
1072         then
1073                 roopt="rr"
1074         else
1075                 roopt="ro"
1076         fi
1077
1078         if [ -z "${PLAIN_ROOT}" ]
1079         then
1080                 # Read image names from ${MODULE}.module if it exists
1081                 if [ -e "${image_directory}/filesystem.${MODULE}.module" ]
1082                 then
1083                         for IMAGE in $(cat ${image_directory}/filesystem.${MODULE}.module)
1084                         do
1085                                 image_string="${image_string} ${image_directory}/${IMAGE}"
1086                         done
1087                 elif [ -e "${image_directory}/${MODULE}.module" ]
1088                 then
1089                         for IMAGE in $(cat ${image_directory}/${MODULE}.module)
1090                         do
1091                                 image_string="${image_string} ${image_directory}/${IMAGE}"
1092                         done
1093                 else
1094                         # ${MODULE}.module does not exist, create a list of images
1095                         for FILESYSTEM in squashfs ext2 ext3 ext4 xfs jffs2 dir
1096                         do
1097                                 for IMAGE in "${image_directory}"/*."${FILESYSTEM}"
1098                                 do
1099                                         if [ -e "${IMAGE}" ]
1100                                         then
1101                                                 image_string="${image_string} ${IMAGE}"
1102                                         fi
1103                                 done
1104                         done
1105
1106                         if [ -n "${addimage_directory}" ] && [ -d "${addimage_directory}" ]
1107                         then
1108                                 for FILESYSTEM in squashfs ext2 ext3 ext4 xfs jffs2 dir
1109                                 do
1110                                         for IMAGE in "${addimage_directory}"/*."${FILESYSTEM}"
1111                                         do
1112                                                 if [ -e "${IMAGE}" ]
1113                                                 then
1114                                                         image_string="${image_string} ${IMAGE}"
1115                                                 fi
1116                                         done
1117                                 done
1118                         fi
1119
1120                         # Now sort the list
1121                         image_string="$(echo ${image_string} | sed -e 's/ /\n/g' | sort )"
1122                 fi
1123
1124         [ -n "${MODULETORAMFILE}" ] && image_string="${image_directory}/$(basename ${MODULETORAMFILE})"
1125
1126                 mkdir -p "${croot}"
1127
1128                 for image in ${image_string}
1129                 do
1130                         imagename=$(basename "${image}")
1131
1132                         export image devname
1133                         maybe_break live-realpremount
1134                         log_begin_msg "Running /scripts/live-realpremount"
1135                         run_scripts /scripts/live-realpremount
1136                         log_end_msg
1137
1138                         if [ -d "${image}" ]
1139                         then
1140                                 # it is a plain directory: do nothing
1141                                 rofsstring="${image}=${roopt}:${rofsstring}"
1142                                 rofslist="${image} ${rofslist}"
1143                         elif [ -f "${image}" ]
1144                         then
1145                                 if losetup --help 2>&1 | grep -q -- "-r\b"
1146                                 then
1147                                         backdev=$(get_backing_device "${image}" "-r")
1148                                 else
1149                                         backdev=$(get_backing_device "${image}")
1150                                 fi
1151                                 fstype=$(get_fstype "${backdev}")
1152
1153                                 if [ "${fstype}" = "unknown" ]
1154                                 then
1155                                         panic "Unknown file system type on ${backdev} (${image})"
1156                                 fi
1157
1158                                 if [ -z "${fstype}" ]
1159                                 then
1160                                         fstype="${imagename##*.}"
1161                                         log_warning_msg "Unknown file system type on ${backdev} (${image}), assuming ${fstype}."
1162                                 fi
1163
1164                                 mkdir -p "${croot}/${imagename}"
1165                                 log_begin_msg "Mounting \"${image}\" on \"${croot}${imagename}\" via \"${backdev}\""
1166                                 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}"
1167                                 log_end_msg
1168                         fi
1169                 done
1170         else
1171                 # we have a plain root system
1172                 mkdir -p "${croot}/filesystem"
1173                 log_begin_msg "Mounting \"${image_directory}\" on \"${croot}/filesystem\""
1174                 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}"
1175                 # probably broken:
1176                 mount -o bind ${croot}/filesystem $mountpoint
1177                 log_end_msg
1178         fi
1179
1180         rofsstring=${rofsstring%:}
1181
1182         mkdir -p /cow
1183
1184         # Looking for "${root_persistence}" device or file
1185         if [ -n "${PERSISTENT}" ] && [ -z "${NOPERSISTENT}" ]
1186         then
1187                 # Load USB modules
1188                 num_block=$(ls -l /sys/block | wc -l)
1189                 for module in sd_mod uhci-hcd ehci-hcd ohci-hcd usb-storage
1190                 do
1191                         modprobe -q -b ${module}
1192                 done
1193
1194                 if [ -x /sbin/udevadm ]
1195                 then
1196                         # lenny
1197                         udevadm trigger
1198                         udevadm settle
1199                 else
1200                         # etch
1201                         udevtrigger
1202                         udevsettle
1203                 fi
1204
1205                 # For some reason, udevsettle does not block in this scenario,
1206                 # so we sleep for a little while.
1207                 #
1208                 # See https://bugs.launchpad.net/ubuntu/+source/casper/+bug/84591
1209                 for timeout in 5 4 3 2 1
1210                 do
1211                         sleep 1
1212
1213                         if [ $(ls -l /sys/block | wc -l) -gt ${num_block} ]
1214                         then
1215                                 break
1216                         fi
1217                 done
1218
1219                 # search for label and files (this could be hugely optimized)
1220                 cowprobe=$(find_cow_device "${root_persistence}")
1221                 if [ -b "${cowprobe}" ]
1222                 then
1223                         # Blacklist /cow device, to avoid inconsistent setups for overlapping snapshots
1224                         # makes sense to have both persistence for /cow and /home mounted, maybe also with
1225                         # snapshots to be sure to really store some e.g key config files,
1226                         # but not on the same media
1227                         blacklistdev="${cowprobe}"
1228                         PERSISTENCE_IS_ON="1"
1229                         export PERSISTENCE_IS_ON
1230                 fi
1231                 # homecow just mount something on /home, this should be generalized some way
1232                 homecow=$(find_cow_device "${home_persistence}" "${blacklistdev}")
1233                 if [ -b "${homecow}" ]
1234                 then
1235                         PERSISTENCE_IS_ON="1"
1236                         export PERSISTENCE_IS_ON
1237                 fi
1238                 root_snapdata=$(find_snap "${root_snapshot_label}" "${blacklistdev}")
1239                 # This second type should be removed when snapshot will get smarter,
1240                 # hence when "/etc/live-snapshot*list" will be supported also by
1241                 # ext2|ext3|ext4|jffs2 snapshot types.
1242                 home_snapdata=$(find_snap "${home_snapshot_label}" "${blacklistdev}")
1243
1244                 if [ -b "${cowprobe}" ]
1245                 then
1246                         cowdevice=${cowprobe}
1247                         cow_fstype=$(get_fstype "${cowprobe}")
1248                         cow_mountopt="rw,noatime"
1249
1250                         if [ "${FORCEPERSISTENTFSCK}" = "Yes" ]
1251                         then
1252                                 fsck -y ${cowdevice}
1253                         fi
1254                 else
1255                         log_warning_msg "Unable to find the persistent medium"
1256                         cowdevice="tmpfs"
1257                         cow_fstype="tmpfs"
1258                         cow_mountopt="rw,noatime,mode=755"
1259                 fi
1260         elif [ -n "${NFS_COW}" ] && [ -z "${NOPERSISTENT}" ]
1261         then
1262                 # check if there are any nfs options
1263                 if echo ${NFS_COW}|grep -q ','
1264                 then
1265                         nfs_cow_opts="-o nolock,$(echo ${NFS_COW}|cut -d, -f2-)"
1266                         nfs_cow=$(echo ${NFS_COW}|cut -d, -f1)
1267                 else
1268                         nfs_cow_opts="-o nolock"
1269                         nfs_cow=${NFS_COW}
1270                 fi
1271                 mac="$(get_mac)"
1272                 if [ -n "${mac}" ]
1273                 then
1274                         cowdevice=$(echo ${nfs_cow}|sed "s/client_mac_address/${mac}/")
1275                         cow_fstype="nfs"
1276                 else
1277                         panic "unable to determine mac address"
1278                 fi
1279         else
1280                 cowdevice="tmpfs"
1281                 cow_fstype="tmpfs"
1282                 cow_mountopt="rw,noatime,mode=755"
1283         fi
1284
1285         if [ "${cow_fstype}" = "nfs" ]
1286         then
1287                 log_begin_msg \
1288                         "Trying nfsmount ${nfs_cow_opts} ${cowdevice} /cow"
1289                 nfsmount ${nfs_cow_opts} ${cowdevice} /cow || \
1290                         panic "Can not mount ${cowdevice} (n: ${cow_fstype}) on /cow"
1291         else
1292                 mount -t ${cow_fstype} -o ${cow_mountopt} ${cowdevice} /cow || \
1293                         panic "Can not mount ${cowdevice} (o: ${cow_fstype}) on /cow"
1294         fi
1295
1296         rofscount=$(echo ${rofslist} |wc -w)
1297
1298         if [ -n "${EXPOSED_ROOT}" ]
1299         then
1300                 if [ ${rofscount} -ne 1 ]
1301                 then
1302                         panic "only one RO file system supported with exposedroot: ${rofslist}"
1303                 fi
1304                 exposedrootfs=${rofslist%% }
1305
1306                 mount --bind ${exposedrootfs} ${rootmnt} || \
1307                         panic "bind mount of ${exposedrootfs} failed"
1308
1309                 if [ -z "${SKIP_UNION_MOUNTS}" ]
1310                 then
1311                         cow_dirs='/var/tmp /var/lock /var/run /var/log /var/spool /home /var/lib/live'
1312                 else
1313                         cow_dirs=''
1314                 fi
1315
1316                 for dir in ${cow_dirs}; do
1317                         mkdir -p /cow${dir}
1318                         mount -t ${UNIONTYPE} \
1319                                 -o rw,noatime,dirs=/cow${dir}=rw:${exposedrootfs}${dir}=ro \
1320                                 ${UNIONTYPE} "${rootmnt}${dir}" || \
1321                                 panic "mount ${UNIONTYPE} on ${rootmnt}${dir} failed with option \
1322                                         rw,noatime,dirs=/cow${dir}=rw:${exposedrootfs}${dir}=ro"
1323                 done
1324         else
1325                 mount -t ${UNIONTYPE} -o noatime,dirs=/cow=rw:${rofsstring} \
1326                         ${UNIONTYPE} "${rootmnt}" || panic "mount ${UNIONTYPE} on \
1327                         ${rootmnt} failed with option noatime,dirs=/cow=rw:${rofsstring}"
1328         fi
1329
1330         # Correct the permissions of /:
1331         chmod 0755 "${rootmnt}"
1332
1333         # tmpfs file systems
1334         touch /etc/fstab
1335         mkdir -p "${rootmnt}/live"
1336         mount -t tmpfs tmpfs ${rootmnt}/live
1337
1338         # Adding other custom mounts
1339         if [ -n "${PERSISTENT}" ] && [ -z "${NOPERSISTENT}" ]
1340         then
1341                 # directly mount /home
1342                 # FIXME: add a custom mounts configurable system
1343
1344                 if [ -b "${homecow}" ]
1345                 then
1346                         mount -t $(get_fstype "${homecow}") -o rw,noatime "${homecow}" "${rootmnt}/home"
1347                         export HOMEMOUNTED=1 # used to proper calculate free space in do_snap_copy()
1348                 else
1349                         log_warning_msg "Unable to find the persistent home medium"
1350                 fi
1351
1352                 # Look for other snapshots to copy in
1353                 try_snap "${root_snapdata}" "${rootmnt}" "ROOT"
1354                 # This second type should be removed when snapshot grow smarter
1355                 try_snap "${home_snapdata}" "${rootmnt}/home" "HOME"
1356         fi
1357
1358         if [ -n "${SHOWMOUNTS}" ]
1359         then
1360                 for d in ${rofslist}
1361                 do
1362                         mkdir -p "${rootmnt}/live/${d##*/}"
1363
1364                         case d in
1365                                 *.dir)
1366                                         # do nothing # mount -o bind "${d}" "${rootmnt}/live/${d##*/}"
1367                                         ;;
1368
1369                                 *)
1370                                         mount -o move "${d}" "${rootmnt}/live/${d##*/}"
1371                                         ;;
1372                         esac
1373                 done
1374         fi
1375
1376         # shows cow fs on /cow for use by live-snapshot
1377         mkdir -p "${rootmnt}/live/cow"
1378         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"
1379 }
1380
1381 check_dev ()
1382 {
1383         sysdev="${1}"
1384         devname="${2}"
1385         skip_uuid_check="${3}"
1386
1387         if [ -z "${devname}" ]
1388         then
1389                 devname=$(sys2dev "${sysdev}")
1390         fi
1391
1392         if [ -d "${devname}" ]
1393         then
1394                 mount -o bind "${devname}" $mountpoint || continue
1395
1396                 if is_live_path $mountpoint
1397                 then
1398                         echo $mountpoint
1399                         return 0
1400                 else
1401                         umount $mountpoint
1402                 fi
1403         fi
1404
1405         if [ -n "${LIVE_MEDIA_OFFSET}" ]
1406         then
1407                 loopdevname=$(setup_loop "${devname}" "loop" "/sys/block/loop*" "${LIVE_MEDIA_OFFSET}" '')
1408                 devname="${loopdevname}"
1409         fi
1410
1411         fstype=$(get_fstype "${devname}")
1412
1413         if is_supported_fs ${fstype}
1414         then
1415                 mount -t ${fstype} -o ro,noatime "${devname}" ${mountpoint} || continue
1416
1417                 if is_live_path ${mountpoint} && \
1418                         ([ "${skip_uuid_check}" ] || matches_uuid ${mountpoint})
1419                 then
1420                         echo ${mountpoint}
1421                         return 0
1422                 else
1423                         umount ${mountpoint}
1424                 fi
1425         fi
1426
1427         if [ -n "${LIVE_MEDIA_OFFSET}" ]
1428         then
1429                 losetup -d "${loopdevname}"
1430         fi
1431
1432         return 1
1433 }
1434
1435 find_livefs ()
1436 {
1437         timeout="${1}"
1438
1439         # don't start autodetection before timeout has expired
1440         if [ -n "${LIVE_MEDIA_TIMEOUT}" ]
1441         then
1442                 if [ "${timeout}" -lt "${LIVE_MEDIA_TIMEOUT}" ]
1443                 then
1444                         return 1
1445                 fi
1446         fi
1447
1448         # first look at the one specified in the command line
1449         case "${LIVE_MEDIA}" in
1450                 removable-usb)
1451                         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1452                         do
1453                                 if [ "$(cat ${sysblock}/removable)" = "1" ]
1454                                 then
1455                                         if readlink ${sysblock} | grep -q usb ||
1456                                            readlink ${sysblock}/device | grep -q usb # linux < 2.6.29
1457                                         then
1458                                                 for dev in $(subdevices "${sysblock}")
1459                                                 do
1460                                                         if check_dev "${dev}"
1461                                                         then
1462                                                                 return 0
1463                                                         fi
1464                                                 done
1465                                         fi
1466                                 fi
1467                         done
1468                         ;;
1469
1470                 removable)
1471                         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1472                         do
1473                                 if [ "$(cat ${sysblock}/removable)" = "1" ]
1474                                 then
1475                                         for dev in $(subdevices "${sysblock}")
1476                                         do
1477                                                 if check_dev "${dev}"
1478                                                 then
1479                                                         return 0
1480                                                 fi
1481                                         done
1482                                 fi
1483                         done
1484                         ;;
1485
1486                 *)
1487                         if [ ! -z "${LIVE_MEDIA}" ]
1488                         then
1489                                 if check_dev "null" "${LIVE_MEDIA}" "skip_uuid_check"
1490                                 then
1491                                         return 0
1492                                 fi
1493                         fi
1494                         ;;
1495         esac
1496
1497         # or do the scan of block devices
1498         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1499         do
1500                 devname=$(sys2dev "${sysblock}")
1501                 fstype=$(get_fstype "${devname}")
1502
1503                 if /lib/udev/cdrom_id ${devname} > /dev/null
1504                 then
1505                         if check_dev "null" "${devname}"
1506                         then
1507                                 return 0
1508                         fi
1509                 elif is_nice_device "${sysblock}"
1510                 then
1511                         for dev in $(subdevices "${sysblock}")
1512                         do
1513                                 if check_dev "${dev}"
1514                                 then
1515                                         return 0
1516                                 fi
1517                         done
1518                 elif [ "${fstype}" = "squashfs" -o \
1519                         "${fstype}" = "ext2" -o \
1520                         "${fstype}" = "ext3" -o \
1521                         "${fstype}" = "ext4" -o \
1522                         "${fstype}" = "jffs2" ]
1523                 then
1524                         # This is an ugly hack situation, the block device has
1525                         # an image directly on it.  It's hopefully
1526                         # live-initramfs, so take it and run with it.
1527                         ln -s "${devname}" "${devname}.${fstype}"
1528                         echo "${devname}.${fstype}"
1529                         return 0
1530                 fi
1531         done
1532
1533         return 1
1534 }
1535
1536 set_usplash_timeout ()
1537 {
1538         if [ -x /sbin/usplash_write ]
1539         then
1540                 /sbin/usplash_write "TIMEOUT 120"
1541         else if [ -x /sbin/splashy_update ] ; then
1542                 /sbin/splashy_update "TIMEOUT 120"
1543         fi ; fi
1544 }
1545
1546 integrity_check ()
1547 {
1548         media_mountpoint="${1}"
1549
1550         log_begin_msg "Checking media integrity"
1551
1552         cd ${media_mountpoint}
1553         /bin/md5sum -c md5sum.txt < /dev/tty8 > /dev/tty8
1554         RC="${?}"
1555
1556         log_end_msg
1557
1558         if [ "${RC}" -eq 0 ]
1559         then
1560                 log_success_msg "Everything ok, will reboot in 10 seconds."
1561                 sleep 10
1562                 cd /
1563                 umount ${media_mountpoint}
1564                 sync
1565                 echo u > /proc/sysrq-trigger
1566                 echo b > /proc/sysrq-trigger
1567         else
1568                 panic "Not ok, a media defect is likely, switch to VT8 for details."
1569         fi
1570 }
1571
1572 mountroot ()
1573 {
1574         if [ -x /scripts/local-top/cryptroot ]; then
1575             /scripts/local-top/cryptroot
1576         fi
1577
1578         exec 6>&1
1579         exec 7>&2
1580         exec > live.log
1581         exec 2>&1
1582         tail -f live.log >&7 &
1583         tailpid="${!}"
1584
1585         # Ensure 'panic' function is overridden
1586         . /scripts/live-functions
1587
1588         Arguments
1589
1590         set_usplash_timeout
1591
1592         maybe_break live-premount
1593         log_begin_msg "Running /scripts/live-premount"
1594         run_scripts /scripts/live-premount
1595         log_end_msg
1596
1597         # Needed here too because some things (*cough* udev *cough*)
1598         # changes the timeout
1599
1600         set_usplash_timeout
1601
1602         if [ ! -z "${NETBOOT}" ] || [ ! -z "${FETCH}" ] || [ ! -z "${HTTPFS}" ] || [ ! -z "${FTPFS}" ]
1603         then
1604                 if do_netmount
1605                 then
1606                         livefs_root="${mountpoint}"
1607                 else
1608                         panic "Unable to find a live file system on the network"
1609                 fi
1610         else
1611                 if [ -n "${PLAIN_ROOT}" ] && [ -n "${ROOT}" ]
1612                 then
1613                         # Do a local boot from hd
1614                         livefs_root=${ROOT}
1615                 else
1616                         # Scan local devices for the image
1617                         i=0
1618                         while [ "$i" -lt 60 ]
1619                         do
1620                                 livefs_root=$(find_livefs ${i})
1621
1622                                 if [ -n "${livefs_root}" ]
1623                                 then
1624                                         break
1625                                 fi
1626
1627                                 sleep 1
1628                                 i="$(($i + 1))"
1629                         done
1630                 fi
1631         fi
1632
1633         if [ -z "${livefs_root}" ]
1634         then
1635                 panic "Unable to find a medium containing a live file system"
1636         fi
1637
1638         if [ "${INTEGRITY_CHECK}" ]
1639         then
1640                 integrity_check "${livefs_root}"
1641         fi
1642
1643         if [ "${TORAM}" ]
1644         then
1645                 live_dest="ram"
1646         elif [ "${TODISK}" ]
1647         then
1648                 live_dest="${TODISK}"
1649         fi
1650
1651         if [ "${live_dest}" ]
1652         then
1653                 log_begin_msg "Copying live media to ${live_dest}"
1654                 copy_live_to "${livefs_root}" "${live_dest}"
1655                 log_end_msg
1656         fi
1657
1658         if [ -n "${MODULETORAMFILE}" ] || [ -n "${PLAIN_ROOT}" ]
1659         then
1660                 setup_unionfs "${livefs_root}" "${rootmnt}"
1661         else
1662                 mac="$(get_mac)"
1663                 mac="$(echo ${mac} | sed 's/-//g')"
1664                 mount_images_in_directory "${livefs_root}" "${rootmnt}" "${mac}"
1665         fi
1666
1667         log_end_msg
1668
1669         maybe_break live-bottom
1670         log_begin_msg "Running /scripts/live-bottom\n"
1671
1672         run_scripts /scripts/live-bottom
1673         log_end_msg
1674
1675         exec 1>&6 6>&-
1676         exec 2>&7 7>&-
1677         kill ${tailpid}
1678         [ -w "${rootmnt}/var/log/" ] && cp live.log "${rootmnt}/var/log/" 2>/dev/null
1679 }