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