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