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