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