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