Fixed some snapshot related debug messages.
[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-sata_mv|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         [ -n "$ETHDEV_TIMEOUT" ] || ETHDEV_TIMEOUT=15
585         echo "Using timeout of $ETHDEV_TIMEOUT seconds for network configuration."
586
587         if [ -z "${NETBOOT}" ] && [ -z "${FETCH}" ] && \
588            [ -z "${HTTPFS}" ] && [ -z "${FTPFS}" ]
589         then
590
591
592         # support for Syslinux IPAPPEND parameter
593         # it sets the BOOTIF variable on the kernel parameter
594
595         if [ -n "${BOOTIF}" ]
596         then
597                 # pxelinux sets BOOTIF to a value based on the mac address of the
598                 # network card used to PXE boot, so use this value for DEVICE rather
599                 # than a hard-coded device name from initramfs.conf. this facilitates
600                 # network booting when machines may have multiple network cards.
601                 # pxelinux sets BOOTIF to 01-$mac_address
602
603                 # strip off the leading "01-", which isn't part of the mac
604                 # address
605                 temp_mac=${BOOTIF#*-}
606
607                 # convert to typical mac address format by replacing "-" with ":"
608                 bootif_mac=""
609                 IFS='-'
610                 for x in $temp_mac
611                 do
612                         if [ -z "$bootif_mac" ]
613                         then
614                                 bootif_mac="$x"
615                         else
616                                 bootif_mac="$bootif_mac:$x"
617                         fi
618                 done
619                 unset IFS
620
621                 # look for devices with matching mac address, and set DEVICE to
622                 # appropriate value if match is found.
623
624                 for device in /sys/class/net/*
625                 do
626                         if [ -f "$device/address" ]
627                         then
628                                 current_mac=$(cat "$device/address")
629
630                                 if [ "$bootif_mac" = "$current_mac" ]
631                                 then
632                                         DEVICE=${device##*/}
633                                         break
634                                 fi
635                         fi
636                 done
637         fi
638
639         # if ethdevice was not specified on the kernel command line
640         # make sure we try to get a working network configuration
641         # for *every* present network device (except for loopback of course)
642         if [ -z "$ETHDEVICE" ] ; then
643                 echo "If you want to boot from a specific device use bootoption ethdevice=..."
644                 for device in /sys/class/net/*; do
645                         dev=${device##*/} ;
646                         if [ "$dev" != "lo" ] ; then
647                                 ETHDEVICE="$ETHDEVICE $dev"
648                         fi
649                 done
650         fi
651
652         # split args of ethdevice=eth0,eth1 into "eth0 eth1"
653         for device in $(echo $ETHDEVICE | sed 's/,/ /g') ; do
654                 devlist="$devlist $device"
655         done
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                 for interface in ${DEVICE}; do
681                         ipconfig -t "$ETHDEV_TIMEOUT" ${interface} | tee /netboot-${interface}.config
682                         [ -e /tmp/net-${interface}.conf ] && . /tmp/net-${interface}.conf
683                         if [ "$IPV4ADDR" != "0.0.0.0" ]
684                         then
685                                 break
686                         fi
687                 done
688         fi
689
690         for interface in ${DEVICE}; do
691                 # source relevant ipconfig output
692                 OLDHOSTNAME=${HOSTNAME}
693                 [ -e /tmp/net-${interface}.conf ] && . /tmp/net-${interface}.conf
694                 [ -z ${HOSTNAME} ] && HOSTNAME=${OLDHOSTNAME}
695                 export HOSTNAME
696
697                 if [ -n "${interface}" ]
698                 then
699                         HWADDR="$(cat /sys/class/net/${interface}/address)"
700                 fi
701
702                 if [ ! -e "/etc/resolv.conf" ]
703                 then
704                         echo "Creating /etc/resolv.conf"
705
706                         if [ -n "${DNSDOMAIN}" ]
707                         then
708                                 echo "domain ${DNSDOMAIN}" > /etc/resolv.conf
709                                 echo "search ${DNSDOMAIN}" > /etc/resolv.conf
710                         fi
711
712                         for i in ${IPV4DNS0} ${IPV4DNS1} ${IPV4DNS1}
713                         do
714                                 echo "nameserver $i" >> /etc/resolv.conf
715                         done
716                 fi
717
718                 # Check if we have a network device at all
719                 if ! ls /sys/class/net/"$interface" > /dev/null 2>&1 && \
720                    ! ls /sys/class/net/eth0 > /dev/null 2>&1 && \
721                    ! ls /sys/class/net/wlan0 > /dev/null 2>&1 && \
722                    ! ls /sys/class/net/ath0 > /dev/null 2>&1 && \
723                    ! ls /sys/class/net/ra0 > /dev/null 2>&1
724                 then
725                         panic "No supported network device found, maybe a non-mainline driver is required."
726                 fi
727         done
728 }
729
730 do_netmount()
731 {
732         do_netsetup
733
734         if [ "${NFSROOT}" = "auto" ]
735         then
736                 NFSROOT=${ROOTSERVER}:${ROOTPATH}
737         fi
738
739         rc=1
740
741         if ( [ -n "${FETCH}" ] || [ -n "${HTTPFS}" ] || [ -n "${FTPFS}" ] )
742         then
743                 do_httpmount
744                 return $?
745         fi
746
747         if [ "${NFSROOT#*:}" = "${NFSROOT}" ] && [ "$NETBOOT" != "cifs" ]
748         then
749                 NFSROOT=${ROOTSERVER}:${NFSROOT}
750         fi
751
752         log_begin_msg "Trying netboot from ${NFSROOT}"
753
754         if [ "${NETBOOT}" != "nfs" ] && do_cifsmount
755         then
756                 rc=0
757         elif do_nfsmount
758         then
759                 NETBOOT="nfs"
760                 export NETBOOT
761                 rc=0
762         fi
763
764         log_end_msg
765         return ${rc}
766 }
767
768 do_iscsi()
769 {
770         do_netsetup
771         #modprobe ib_iser
772         modprobe iscsi_tcp
773         local debugopt=""
774         [ "${DEBUG}" == "Yes" ] && debugopt="-d 8"
775         #FIXME this name is supposed to be unique - some date + ifconfig hash?
776         ISCSI_INITIATORNAME="iqn.1993-08.org.debian.live:01:$(echo "${HWADDR}" | sed -e s/://g)"
777         export ISCSI_INITIATORNAME
778         if [ -n "${ISCSI_SERVER}" ] ; then
779                 iscsistart $debugopt -i "${ISCSI_INITIATORNAME}" -t "${ISCSI_TARGET}" -g 1 -a "${ISCSI_SERVER}" -p "${ISCSI_PORT}"
780         else
781                 iscsistart $debugopt -i "${ISCSI_INITIATORNAME}" -t "${ISCSI_TARGET}" -g 1 -a "${ISCSI_PORTAL}" -p 3260
782         fi
783         if [ $? != 0 ]
784         then
785                 panic "Failed to log into iscsi target"
786         fi
787         local host="$(ls -d /sys/class/scsi_host/host*/device/iscsi_host:host* \
788                             /sys/class/scsi_host/host*/device/iscsi_host/host* | sed -e 's:/device.*::' -e 's:.*host::')"
789         if [ -n "${host}" ]
790         then
791                 local devices=""
792                 local i=0
793                 while [ -z "${devices}" -a $i -lt 60 ]
794                 do
795                         sleep 1
796                         devices="$(ls -d /sys/class/scsi_device/${host}*/device/block:* \
797                                          /sys/class/scsi_device/${host}*/device/block/* | sed -e 's!.*[:/]!!')"
798                         i=$(expr $i + 1)
799                         echo -ne $i\\r
800                 done
801                 for dev in $devices
802                 do
803                         if check_dev "null" "/dev/$dev"
804                         then
805                                 NETBOOT="iscsi"
806                                 export NETBOOT
807                                 return 0;
808                         fi
809                 done
810                 panic "Failed to locate a live device on iSCSI devices (tried: $devices)."
811         else
812                 panic "Failed to locate iSCSI host in /sys"
813         fi
814 }
815
816 do_httpmount ()
817 {
818         rc=1
819
820         for webfile in HTTPFS FTPFS FETCH
821         do
822                 local url="$(eval echo \"\$\{${webfile}\}\")"
823                 local extension="$(echo "${url}" | sed 's/\(.*\)\.\(.*\)/\2/')"
824
825                 if [ -n "$url" ]
826                 then
827                         case "${extension}" in
828                                 iso|squashfs|tgz|tar)
829                                         if [ "${extension}" = "iso" ]
830                                         then
831                                                 mkdir -p "${alt_mountpoint}"
832                                                 dest="${alt_mountpoint}"
833                                         else
834                                                 local dest="${mountpoint}/${LIVE_MEDIA_PATH}"
835                                                 mount -t ramfs ram "${mountpoint}"
836                                                 mkdir -p "${dest}"
837                                         fi
838                                         if [ "${webfile}" = "FETCH" ]
839                                         then
840                                                 case "$url" in
841                                                         tftp*)
842                                                                 ip="$(dirname $url | sed -e 's|tftp://||g' -e 's|/.*$||g')"
843                                                                 rfile="$(echo $url | sed -e "s|tftp://$ip||g")"
844                                                                 lfile="$(basename $url)"
845                                                                 log_begin_msg "Trying tftp -g -b 10240 -r $rfile -l ${dest}/$lfile $ip"
846                                                                 tftp -g -b 10240 -r $rfile -l ${dest}/$lfile $ip
847                                                         ;;
848
849                                                         *)
850                                                                 log_begin_msg "Trying wget ${url} -O ${dest}/$(basename ${url})"
851                                                                 wget "${url}" -O "${dest}/$(basename ${url})"
852                                                                 ;;
853                                                 esac
854                                         else
855                                                 log_begin_msg "Trying to mount ${url} on ${dest}/$(basename ${url})"
856                                                 if [ "${webfile}" = "FTPFS" ]
857                                                 then
858                                                         FUSE_MOUNT="curlftpfs"
859                                                         url="$(dirname ${url})"
860                                                 else
861                                                         FUSE_MOUNT="httpfs"
862                                                 fi
863                                                 modprobe fuse
864                                                 $FUSE_MOUNT "${url}" "${dest}"
865                                                 ROOT_PID="$(minips h -C "$FUSE_MOUNT" | { read x y ; echo "$x" ; } )"
866                                         fi
867                                         [ ${?} -eq 0 ] && rc=0
868                                         [ "${extension}" = "tgz" ] && live_dest="ram"
869                                         if [ "${extension}" = "iso" ]
870                                         then
871                                                 isoloop=$(setup_loop "${dest}/$(basename "${url}")" "loop" "/sys/block/loop*" "" '')
872                                                 mount -t iso9660 "${isoloop}" "${mountpoint}"
873                                                 rc=${?}
874                                         fi
875                                         break
876                                         ;;
877
878                                 *)
879                                         log_begin_msg "Unrecognized archive extension for ${url}"
880                                         ;;
881                         esac
882                 fi
883         done
884
885         if [ ${rc} != 0 ]
886         then
887                 if [ -d "${alt_mountpoint}" ]
888                 then
889                         umount "${alt_mountpoint}"
890                         rmdir "${alt_mountpoint}"
891                 fi
892                 umount "${mountpoint}"
893         elif [ "${webfile}"  != "FETCH" ] ; then
894                 NETBOOT="${webfile}"
895                 export NETBOOT
896         fi
897
898         return ${rc}
899 }
900
901 do_nfsmount ()
902 {
903         rc=1
904
905         modprobe -q nfs
906
907         if [ -z "${NFSOPTS}" ]
908         then
909                 NFSOPTS=""
910         fi
911
912         log_begin_msg "Trying nfsmount -o nolock -o ro ${NFSOPTS} ${NFSROOT} ${mountpoint}"
913
914         # FIXME: This while loop is an ugly HACK round an nfs bug
915         i=0
916         while [ "$i" -lt 60 ]
917         do
918                 nfsmount -o nolock -o ro ${NFSOPTS} "${NFSROOT}" "${mountpoint}" && rc=0 && break
919                 sleep 1
920                 i="$(($i + 1))"
921         done
922
923         return ${rc}
924 }
925
926 do_cifsmount ()
927 {
928         rc=1
929
930         if [ -x "/sbin/mount.cifs" ]
931         then
932                 if [ -z "${NFSOPTS}" ]
933                 then
934                         CIFSOPTS="-ouser=root,password="
935                 else
936                         CIFSOPTS="${NFSOPTS}"
937                 fi
938
939                 log_begin_msg "Trying mount.cifs ${NFSROOT} ${mountpoint} ${CIFSOPTS}"
940                 modprobe -q cifs
941
942                 if mount.cifs "${NFSROOT}" "${mountpoint}" "${CIFSOPTS}"
943                 then
944                         rc=0
945                 fi
946         fi
947
948         return ${rc}
949 }
950
951 do_snap_copy ()
952 {
953         fromdev="${1}"
954         todir="${2}"
955         snap_type="${3}"
956         size=$(fs_size "${fromdev}" "" "used")
957
958         if [ -b "${fromdev}" ]
959         then
960                 log_success_msg "Copying snapshot ${fromdev} to ${todir}..."
961
962                 # look for free mem
963                 if [ -n "${HOMEMOUNTED}" -a "${snap_type}" = "HOME" ]
964                 then
965                         todev=$(awk -v pat="$(base_path ${todir})" '$2 == pat { print $1 }' /proc/mounts)
966                         freespace=$(df -k | awk '/'${todev}'/{print $4}')
967                 else
968                         freespace=$(awk '/^MemFree:/{f=$2} /^Cached:/{c=$2} END{print f+c}' /proc/meminfo)
969                 fi
970
971                 tomount="/mnt/tmpsnap"
972
973                 if [ ! -d "${tomount}" ]
974                 then
975                         mkdir -p "${tomount}"
976                 fi
977
978                 fstype=$(get_fstype "${fromdev}")
979
980                 if [ -n "${fstype}" ]
981                 then
982                         # Copying stuff...
983                         mount -o ro -t "${fstype}" "${fromdev}" "${tomount}" || log_warning_msg "Error in mount -t ${fstype} -o ro ${fromdev} ${tomount}"
984                         cp -a "${tomount}"/* ${todir}
985                         umount "${tomount}"
986                 else
987                         log_warning_msg "Unrecognized fstype: ${fstype} on ${fromdev}:${snap_type}"
988                 fi
989
990                 rmdir "${tomount}"
991
992                 if echo ${fromdev} | grep -qs loop
993                 then
994                         losetup -d "${fromdev}"
995                 fi
996
997                 return 0
998         else
999                 log_warning_msg "Unable to find the snapshot ${snap_type} medium"
1000                 return 1
1001         fi
1002 }
1003
1004 find_snap ()
1005 {
1006         # Look for ${snap_label}.* in block devices
1007         snap_label="${1}"
1008
1009         if [ "${PERSISTENT}" != "nofiles" ]
1010         then
1011                 # search for image files
1012                 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")
1013         fi
1014
1015         if [ -z "${snapdata}" ]
1016         then
1017                 snapdata=$(find_cow_device "${snap_label}")
1018         fi
1019         echo "${snapdata}"
1020 }
1021
1022 try_snap ()
1023 {
1024         # copy the contents of previously found snapshot to ${snap_mount}
1025         # and remember the device and filename for resync on exit in live-boot.init
1026
1027         snapdata="${1}"
1028         snap_mount="${2}"
1029         snap_type="${3}"
1030         snap_relpath="${4}"
1031
1032         if [ -z "${snap_relpath}" ]
1033         then
1034                 # root snapshot, default usage
1035                 snap_relpath="/"
1036         else
1037                 # relative snapshot (actually used just for "/home" snapshots)
1038                 snap_mount="${2}${snap_relpath}"
1039         fi
1040
1041         if [ -n "${snapdata}" ] && [ ! -b "${snapdata}" ]
1042         then
1043                 log_success_msg "found snapshot: ${snapdata}"
1044                 snapdev="$(echo ${snapdata} | cut -f1 -d ' ')"
1045                 snapback="$(echo ${snapdata} | cut -f2 -d ' ')"
1046                 snapfile="$(echo ${snapdata} | cut -f3 -d ' ')"
1047
1048                 if ! try_mount "${snapdev}" "${snapback}" "ro"
1049                 then
1050                         break
1051                 fi
1052
1053                 RES="0"
1054
1055                 if echo "${snapfile}" | grep -qs '\(squashfs\|ext2\|ext3\|ext4\|jffs2\)'
1056                 then
1057                         # squashfs, jffs2 or ext2/ext3/ext4 snapshot
1058                         dev=$(get_backing_device "${snapback}/${snapfile}")
1059
1060                         do_snap_copy "${dev}" "${snap_mount}" "${snap_type}"
1061                         RES="$?"
1062                 else
1063                         # cpio.gz snapshot
1064
1065                         # Unfortunately klibc's cpio is incompatible with the
1066                         # rest of the world; everything else requires -u -d,
1067                         # while klibc doesn't implement them. Try to detect
1068                         # whether it's in use.
1069                         cpiopath="$(which cpio)" || true
1070                         if [ "$cpiopath" ] && grep -aq /lib/klibc "$cpiopath"
1071                         then
1072                                 cpioargs=
1073                         else
1074                                 cpioargs='--unconditional --make-directories'
1075                         fi
1076
1077                         cd "${snap_mount}" && zcat "${snapback}/${snapfile}" | $cpiopath $cpioargs --extract --preserve-modification-time --no-absolute-filenames --sparse 2>/dev/null
1078                         RES="$?"
1079
1080                         if [ "${RES}" != "0" ]
1081                         then
1082                                 log_warning_msg "failure to \"zcat ${snapback}/${snapfile} | $cpiopath $cpioargs --extract --preserve-modification-time --no-absolute-filenames --sparse\""
1083                         fi
1084                 fi
1085
1086                 umount "${snapback}" ||  log_warning_msg "failure to \"umount ${snapback}\""
1087
1088                 if [ "${RES}" != "0" ]
1089                 then
1090                         log_warning_msg "Impossible to include the ${snapfile} Snapshot file"
1091                 fi
1092
1093         elif [ -b "${snapdata}" ]
1094         then
1095                 # Try to find if it could be a snapshot partition
1096                 dev="${snapdata}"
1097                 log_success_msg "found snapshot ${snap_type} device on ${dev}"
1098                 if echo "${dev}" | grep -qs loop
1099                 then
1100                         # strange things happens, user confused?
1101                         snaploop=$( losetup ${dev} | awk '{print $3}' | tr -d '()' )
1102                         snapfile=$(basename ${snaploop})
1103                         snapdev=$(awk -v pat="$( dirname ${snaploop})" '$2 == pat { print $1 }' /proc/mounts)
1104                 else
1105                         snapdev="${dev}"
1106                 fi
1107
1108                 if ! do_snap_copy "${dev}" "${snap_mount}" "${snap_type}"
1109                 then
1110                         log_warning_msg "Impossible to include the ${snap_type} Snapshot (i)"
1111                         return 1
1112                 else
1113                         if [ -n "${snapfile}" ]
1114                         then
1115                                 # it was a loop device, user confused
1116                                 umount ${snapdev}
1117                         fi
1118                 fi
1119         else
1120                 log_warning_msg "Impossible to include the ${snap_type} Snapshot (o)"
1121                 return 1
1122         fi
1123
1124         echo "export ${snap_type}SNAP=${snap_relpath}:${snapdev}:${snapfile}" >> snapshot.conf # for resync on reboot/halt
1125         return 0
1126 }
1127
1128 setup_unionfs ()
1129 {
1130         image_directory="${1}"
1131         rootmnt="${2}"
1132         addimage_directory="${3}"
1133
1134         case ${UNIONTYPE} in
1135                 aufs|unionfs)
1136                         modprobe -q -b ${UNIONTYPE}
1137
1138                         if ! cut -f2 /proc/filesystems | grep -q "^${UNIONTYPE}\$" && [ -x /bin/unionfs-fuse ]
1139                         then
1140                                 echo "${UNIONTYPE} not available, falling back to unionfs-fuse."
1141                                 echo "This might be really slow."
1142
1143                                 UNIONTYPE="unionfs-fuse"
1144                         fi
1145                         ;;
1146         esac
1147
1148         if [ "${UNIONTYPE}" = unionfs-fuse ]
1149         then
1150                 modprobe fuse
1151         fi
1152
1153         # run-init can't deal with images in a subdir, but we're going to
1154         # move all of these away before it runs anyway.  No, we're not,
1155         # put them in / since move-mounting them into / breaks mono and
1156         # some other apps.
1157
1158         croot="/"
1159
1160         # Let's just mount the read-only file systems first
1161         rofsstring=""
1162         rofslist=""
1163
1164         if [ "${UNIONTYPE}" = "aufs" ]
1165         then
1166                 roopt="rr"
1167                 noxino_opt="noxino,"
1168         elif [ "${UNIONTYPE}" = "unionfs-fuse" ]
1169         then
1170                 roopt="RO"
1171         else
1172                 roopt="ro"
1173         fi
1174
1175         if [ -z "${PLAIN_ROOT}" ]
1176         then
1177                 # Read image names from ${MODULE}.module if it exists
1178                 if [ -e "${image_directory}/filesystem.${MODULE}.module" ]
1179                 then
1180                         for IMAGE in $(cat ${image_directory}/filesystem.${MODULE}.module)
1181                         do
1182                                 image_string="${image_string} ${image_directory}/${IMAGE}"
1183                         done
1184                 elif [ -e "${image_directory}/${MODULE}.module" ]
1185                 then
1186                         for IMAGE in $(cat ${image_directory}/${MODULE}.module)
1187                         do
1188                                 image_string="${image_string} ${image_directory}/${IMAGE}"
1189                         done
1190                 else
1191                         # ${MODULE}.module does not exist, create a list of images
1192                         for FILESYSTEM in squashfs ext2 ext3 ext4 xfs jffs2 dir
1193                         do
1194                                 for IMAGE in "${image_directory}"/*."${FILESYSTEM}"
1195                                 do
1196                                         if [ -e "${IMAGE}" ]
1197                                         then
1198                                                 image_string="${image_string} ${IMAGE}"
1199                                         fi
1200                                 done
1201                         done
1202
1203                         if [ -n "${addimage_directory}" ] && [ -d "${addimage_directory}" ]
1204                         then
1205                                 for FILESYSTEM in squashfs ext2 ext3 ext4 xfs jffs2 dir
1206                                 do
1207                                         for IMAGE in "${addimage_directory}"/*."${FILESYSTEM}"
1208                                         do
1209                                                 if [ -e "${IMAGE}" ]
1210                                                 then
1211                                                         image_string="${image_string} ${IMAGE}"
1212                                                 fi
1213                                         done
1214                                 done
1215                         fi
1216
1217                         # Now sort the list
1218                         image_string="$(echo ${image_string} | sed -e 's/ /\n/g' | sort )"
1219                 fi
1220
1221         [ -n "${MODULETORAMFILE}" ] && image_string="${image_directory}/$(basename ${MODULETORAMFILE})"
1222
1223                 mkdir -p "${croot}"
1224
1225                 for image in ${image_string}
1226                 do
1227                         imagename=$(basename "${image}")
1228
1229                         export image devname
1230                         maybe_break live-realpremount
1231                         log_begin_msg "Running /scripts/live-realpremount"
1232                         run_scripts /scripts/live-realpremount
1233                         log_end_msg
1234
1235                         if [ -d "${image}" ]
1236                         then
1237                                 # it is a plain directory: do nothing
1238                                 rofsstring="${image}=${roopt}:${rofsstring}"
1239                                 rofslist="${image} ${rofslist}"
1240                         elif [ -f "${image}" ]
1241                         then
1242                                 if losetup --help 2>&1 | grep -q -- "-r\b"
1243                                 then
1244                                         backdev=$(get_backing_device "${image}" "-r")
1245                                 else
1246                                         backdev=$(get_backing_device "${image}")
1247                                 fi
1248                                 fstype=$(get_fstype "${backdev}")
1249
1250                                 if [ "${fstype}" = "unknown" ]
1251                                 then
1252                                         panic "Unknown file system type on ${backdev} (${image})"
1253                                 fi
1254
1255                                 if [ -z "${fstype}" ]
1256                                 then
1257                                         fstype="${imagename##*.}"
1258                                         log_warning_msg "Unknown file system type on ${backdev} (${image}), assuming ${fstype}."
1259                                 fi
1260
1261                                 mkdir -p "${croot}/${imagename}"
1262                                 log_begin_msg "Mounting \"${image}\" on \"${croot}${imagename}\" via \"${backdev}\""
1263                                 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}"
1264                                 log_end_msg
1265                         fi
1266                 done
1267         else
1268                 # we have a plain root system
1269                 mkdir -p "${croot}/filesystem"
1270                 log_begin_msg "Mounting \"${image_directory}\" on \"${croot}/filesystem\""
1271                 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}"
1272                 # probably broken:
1273                 mount -o bind ${croot}/filesystem $mountpoint
1274                 log_end_msg
1275         fi
1276
1277         rofsstring=${rofsstring%:}
1278
1279         mkdir -p /cow
1280
1281         # Looking for "${root_persistence}" device or file
1282         if [ -n "${PERSISTENT}" ] && [ -z "${NOPERSISTENT}" ]
1283         then
1284                 if [ -z "${QUICKUSBMODULES}" ]
1285                 then
1286                         # Load USB modules
1287                         num_block=$(ls -l /sys/block | wc -l)
1288                         for module in sd_mod uhci-hcd ehci-hcd ohci-hcd usb-storage
1289                         do
1290                                 modprobe -q -b ${module}
1291                         done
1292
1293                         udevadm trigger
1294                         udevadm settle
1295
1296                         # For some reason, udevsettle does not block in this scenario,
1297                         # so we sleep for a little while.
1298                         #
1299                         # See https://bugs.launchpad.net/ubuntu/+source/casper/+bug/84591
1300                         for timeout in 5 4 3 2 1
1301                         do
1302                                 sleep 1
1303
1304                                 if [ $(ls -l /sys/block | wc -l) -gt ${num_block} ]
1305                                 then
1306                                         break
1307                                 fi
1308                         done
1309                 fi
1310
1311                 # search for label and files (this could be hugely optimized)
1312                 cowprobe=$(find_cow_device "${root_persistence}")
1313                 if [ -b "${cowprobe}" ]
1314                 then
1315                         # Blacklist /cow device, to avoid inconsistent setups for overlapping snapshots
1316                         # makes sense to have both persistence for /cow and /home mounted, maybe also with
1317                         # snapshots to be sure to really store some e.g key config files,
1318                         # but not on the same media
1319                         blacklistdev="${cowprobe}"
1320                         PERSISTENCE_IS_ON="1"
1321                         export PERSISTENCE_IS_ON
1322                 fi
1323                 # homecow just mount something on /home, this should be generalized some way
1324                 homecow=$(find_cow_device "${home_persistence}" "${blacklistdev}")
1325                 if [ -b "${homecow}" ]
1326                 then
1327                         PERSISTENCE_IS_ON="1"
1328                         export PERSISTENCE_IS_ON
1329                 fi
1330                 root_snapdata=$(find_snap "${root_snapshot_label}" "${blacklistdev}")
1331                 # This second type should be removed when snapshot will get smarter,
1332                 # hence when "/etc/live-snapshot*list" will be supported also by
1333                 # ext2|ext3|ext4|jffs2 snapshot types.
1334                 home_snapdata=$(find_snap "${home_snapshot_label}" "${blacklistdev}")
1335
1336                 if [ -b "${cowprobe}" ]
1337                 then
1338                         cowdevice=${cowprobe}
1339                         cow_fstype=$(get_fstype "${cowprobe}")
1340                         cow_mountopt="rw,noatime"
1341
1342                         if [ "${FORCEPERSISTENTFSCK}" = "Yes" ]
1343                         then
1344                                 fsck -y ${cowdevice}
1345                         fi
1346                 else
1347                         log_warning_msg "Unable to find the persistent medium"
1348                         cowdevice="tmpfs"
1349                         cow_fstype="tmpfs"
1350                         cow_mountopt="rw,noatime,mode=755"
1351                 fi
1352         elif [ -n "${NFS_COW}" ] && [ -z "${NOPERSISTENT}" ]
1353         then
1354                 # check if there are any nfs options
1355                 if echo ${NFS_COW}|grep -q ','
1356                 then
1357                         nfs_cow_opts="-o nolock,$(echo ${NFS_COW}|cut -d, -f2-)"
1358                         nfs_cow=$(echo ${NFS_COW}|cut -d, -f1)
1359                 else
1360                         nfs_cow_opts="-o nolock"
1361                         nfs_cow=${NFS_COW}
1362                 fi
1363                 mac="$(get_mac)"
1364                 if [ -n "${mac}" ]
1365                 then
1366                         cowdevice=$(echo ${nfs_cow}|sed "s/client_mac_address/${mac}/")
1367                         cow_fstype="nfs"
1368                 else
1369                         panic "unable to determine mac address"
1370                 fi
1371         else
1372                 cowdevice="tmpfs"
1373                 cow_fstype="tmpfs"
1374                 cow_mountopt="rw,noatime,mode=755"
1375         fi
1376
1377         if [ "${cow_fstype}" = "nfs" ]
1378         then
1379                 log_begin_msg \
1380                         "Trying nfsmount ${nfs_cow_opts} ${cowdevice} /cow"
1381                 nfsmount ${nfs_cow_opts} ${cowdevice} /cow || \
1382                         panic "Can not mount ${cowdevice} (n: ${cow_fstype}) on /cow"
1383         else
1384                 mount -t ${cow_fstype} -o ${cow_mountopt} ${cowdevice} /cow || \
1385                         panic "Can not mount ${cowdevice} (o: ${cow_fstype}) on /cow"
1386         fi
1387
1388         rofscount=$(echo ${rofslist} |wc -w)
1389
1390         if [ -n "${EXPOSED_ROOT}" ]
1391         then
1392                 if [ ${rofscount} -ne 1 ]
1393                 then
1394                         panic "only one RO file system supported with exposedroot: ${rofslist}"
1395                 fi
1396                 exposedrootfs=${rofslist%% }
1397
1398                 mount --bind ${exposedrootfs} ${rootmnt} || \
1399                         panic "bind mount of ${exposedrootfs} failed"
1400
1401                 if [ -z "${SKIP_UNION_MOUNTS}" ]
1402                 then
1403                         cow_dirs='/var/tmp /var/lock /var/run /var/log /var/spool /home /var/lib/live'
1404                 else
1405                         cow_dirs=''
1406                 fi
1407
1408                 for dir in ${cow_dirs}; do
1409                         mkdir -p /cow${dir}
1410
1411                         case "${UNIONTYPE}" in
1412                                 unionfs-fuse)
1413                                         (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}")
1414                                         mkdir -p /dev/.initramfs/varrun
1415                                         pidof unionfs-fuse >> /dev/.initramfs/varrun/sendsigs.omit || true
1416                                         ;;
1417
1418                                 *)
1419                                         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"
1420                                         ;;
1421                         esac
1422                 done
1423         else
1424                 case "${UNIONTYPE}" in
1425                         unionfs-fuse)
1426                                 (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}")
1427                                 mkdir -p /dev/.initramfs/varrun
1428                                 pidof unionfs-fuse >> /dev/.initramfs/varrun/sendsigs.omit || true
1429                                 ;;
1430
1431                         *)
1432                                 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}"
1433                                 ;;
1434                 esac
1435         fi
1436
1437         # Correct the permissions of /:
1438         chmod 0755 "${rootmnt}"
1439
1440         # tmpfs file systems
1441         touch /etc/fstab
1442         mkdir -p "${rootmnt}/live"
1443         mount -t tmpfs tmpfs ${rootmnt}/live
1444
1445         # Adding other custom mounts
1446         if [ -n "${PERSISTENT}" ] && [ -z "${NOPERSISTENT}" ]
1447         then
1448                 # directly mount /home
1449                 # FIXME: add a custom mounts configurable system
1450
1451                 if [ -b "${homecow}" ]
1452                 then
1453                         mount -t $(get_fstype "${homecow}") -o rw,noatime "${homecow}" "${rootmnt}/home"
1454                         export HOMEMOUNTED=1 # used to proper calculate free space in do_snap_copy()
1455                 else
1456                         log_warning_msg "Unable to find the persistent home medium"
1457                 fi
1458
1459                 # Look for other snapshots to copy in
1460                 try_snap "${root_snapdata}" "${rootmnt}" "ROOT"
1461                 # This second type should be removed when snapshot grow smarter
1462                 try_snap "${home_snapdata}" "${rootmnt}" "HOME" "/home"
1463         fi
1464
1465         if [ -n "${SHOWMOUNTS}" ]
1466         then
1467                 for d in ${rofslist}
1468                 do
1469                         mkdir -p "${rootmnt}/live/${d##*/}"
1470
1471                         case d in
1472                                 *.dir)
1473                                         # do nothing # mount -o bind "${d}" "${rootmnt}/live/${d##*/}"
1474                                         ;;
1475
1476                                 *)
1477                                         case "${UNIONTYPE}" in
1478                                                 unionfs-fuse)
1479                                                         mount -o bind "${d}" "${rootmnt}/live/${d##*/}"
1480                                                         ;;
1481
1482                                                 *)
1483                                                         mount -o move "${d}" "${rootmnt}/live/${d##*/}"
1484                                                         ;;
1485                                         esac
1486                                         ;;
1487                         esac
1488                 done
1489         fi
1490
1491         # shows cow fs on /cow for use by live-snapshot
1492         mkdir -p "${rootmnt}/live/cow"
1493         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"
1494 }
1495
1496 check_dev ()
1497 {
1498         sysdev="${1}"
1499         devname="${2}"
1500         skip_uuid_check="${3}"
1501
1502         # support for fromiso=.../isofrom=....
1503         if [ -n "$FROMISO" ]
1504         then
1505                 ISO_DEVICE=$(dirname $FROMISO)
1506                 if ! [ -b $ISO_DEVICE ]
1507                 then
1508                         # to support unusual device names like /dev/cciss/c0d0p1
1509                         # as well we have to identify the block device name, let's
1510                         # do that for up to 15 levels
1511                         i=15
1512                         while [ -n "$ISO_DEVICE" ] && [ "$i" -gt 0 ]
1513                         do
1514                                 ISO_DEVICE=$(dirname ${ISO_DEVICE})
1515                                 [ -b "$ISO_DEVICE" ] && break
1516                                 i=$(($i -1))
1517                         done
1518                 fi
1519
1520                 if [ "$ISO_DEVICE" = "/" ]
1521                 then
1522                         echo "Warning: device for bootoption isofrom= ($FROMISO) not found.">>/live.log
1523                 else
1524                         mkdir /isofrom
1525                         mount "$ISO_DEVICE" /isofrom
1526                         ISO_NAME="$(echo $FROMISO | sed "s|$ISO_DEVICE||")"
1527                         loopdevname=$(setup_loop "/isofrom/${ISO_NAME}" "loop" "/sys/block/loop*" "" '')
1528                         devname="${loopdevname}"
1529                 fi
1530         fi
1531
1532         if [ -z "${devname}" ]
1533         then
1534                 devname=$(sys2dev "${sysdev}")
1535         fi
1536
1537         if [ -d "${devname}" ]
1538         then
1539                 mount -o bind "${devname}" $mountpoint || continue
1540
1541                 if is_live_path $mountpoint
1542                 then
1543                         echo $mountpoint
1544                         return 0
1545                 else
1546                         umount $mountpoint
1547                 fi
1548         fi
1549         [ -e "$devname" ] || continue
1550
1551         if [ -n "${LIVE_MEDIA_OFFSET}" ]
1552         then
1553                 loopdevname=$(setup_loop "${devname}" "loop" "/sys/block/loop*" "${LIVE_MEDIA_OFFSET}" '')
1554                 devname="${loopdevname}"
1555         fi
1556
1557         fstype=$(get_fstype "${devname}")
1558
1559         if is_supported_fs ${fstype}
1560         then
1561                 devuid=$(blkid -o value -s UUID "$devname")
1562                 [ -n "$devuid" ] && grep -qs "\<$devuid\>" $tried && continue
1563                 mount -t ${fstype} -o ro,noatime "${devname}" ${mountpoint} || continue
1564                 [ -n "$devuid" ] && echo "$devuid" >> $tried
1565
1566                 if is_live_path ${mountpoint} && \
1567                         ([ "${skip_uuid_check}" ] || matches_uuid ${mountpoint})
1568                 then
1569                         echo ${mountpoint}
1570                         return 0
1571                 else
1572                         umount ${mountpoint}
1573                 fi
1574         fi
1575
1576         if [ -n "${LIVE_MEDIA_OFFSET}" ]
1577         then
1578                 losetup -d "${loopdevname}"
1579         fi
1580
1581         return 1
1582 }
1583
1584 find_livefs ()
1585 {
1586         timeout="${1}"
1587
1588         # don't start autodetection before timeout has expired
1589         if [ -n "${LIVE_MEDIA_TIMEOUT}" ]
1590         then
1591                 if [ "${timeout}" -lt "${LIVE_MEDIA_TIMEOUT}" ]
1592                 then
1593                         return 1
1594                 fi
1595         fi
1596
1597         # first look at the one specified in the command line
1598         case "${LIVE_MEDIA}" in
1599                 removable-usb)
1600                         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1601                         do
1602                                 if [ "$(cat ${sysblock}/removable)" = "1" ]
1603                                 then
1604                                         if readlink ${sysblock} | grep -q usb ||
1605                                            readlink ${sysblock}/device | grep -q usb # linux < 2.6.29
1606                                         then
1607                                                 for dev in $(subdevices "${sysblock}")
1608                                                 do
1609                                                         if check_dev "${dev}"
1610                                                         then
1611                                                                 return 0
1612                                                         fi
1613                                                 done
1614                                         fi
1615                                 fi
1616                         done
1617                         return 1
1618                         ;;
1619
1620                 removable)
1621                         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1622                         do
1623                                 if [ "$(cat ${sysblock}/removable)" = "1" ]
1624                                 then
1625                                         for dev in $(subdevices "${sysblock}")
1626                                         do
1627                                                 if check_dev "${dev}"
1628                                                 then
1629                                                         return 0
1630                                                 fi
1631                                         done
1632                                 fi
1633                         done
1634                         return 1
1635                         ;;
1636
1637                 *)
1638                         if [ ! -z "${LIVE_MEDIA}" ]
1639                         then
1640                                 if check_dev "null" "${LIVE_MEDIA}" "skip_uuid_check"
1641                                 then
1642                                         return 0
1643                                 fi
1644                         fi
1645                         ;;
1646         esac
1647
1648         # or do the scan of block devices
1649         # prefer removable devices over non-removable devices, so scan them first
1650         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | egrep -v "/(loop|ram|dm-|fd)")
1651         do
1652                 if [ "$(cat ${sysblock}/removable)" = "1" ]
1653                 then
1654                         removable_devices_to_scan="$removable_devices_to_scan $sysblock"
1655                 else
1656                         nonremovable_devices_to_scan="$nonremovable_devices_to_scan $sysblock"
1657                 fi
1658         done
1659         devices_to_scan="$removable_devices_to_scan $nonremovable_devices_to_scan"
1660
1661         for sysblock in $devices_to_scan
1662         do
1663                 devname=$(sys2dev "${sysblock}")
1664                 [ -e "$devname" ] || continue
1665                 fstype=$(get_fstype "${devname}")
1666
1667                 if /lib/udev/cdrom_id ${devname} > /dev/null
1668                 then
1669                         if check_dev "null" "${devname}"
1670                         then
1671                                 return 0
1672                         fi
1673                 elif is_nice_device "${sysblock}"
1674                 then
1675                         for dev in $(subdevices "${sysblock}")
1676                         do
1677                                 if check_dev "${dev}"
1678                                 then
1679                                         return 0
1680                                 fi
1681                         done
1682                 elif [ "${fstype}" = "squashfs" -o \
1683                         "${fstype}" = "btrfs" -o \
1684                         "${fstype}" = "ext2" -o \
1685                         "${fstype}" = "ext3" -o \
1686                         "${fstype}" = "ext4" -o \
1687                         "${fstype}" = "jffs2" ]
1688                 then
1689                         # This is an ugly hack situation, the block device has
1690                         # an image directly on it.  It's hopefully
1691                         # live-boot, so take it and run with it.
1692                         ln -s "${devname}" "${devname}.${fstype}"
1693                         echo "${devname}.${fstype}"
1694                         return 0
1695                 fi
1696         done
1697
1698         return 1
1699 }
1700
1701 integrity_check ()
1702 {
1703         media_mountpoint="${1}"
1704
1705         log_begin_msg "Checking media integrity"
1706
1707         cd ${media_mountpoint}
1708         /bin/md5sum -c md5sum.txt < /dev/tty8 > /dev/tty8
1709         RC="${?}"
1710
1711         log_end_msg
1712
1713         if [ "${RC}" -eq 0 ]
1714         then
1715                 log_success_msg "Everything ok, will reboot in 10 seconds."
1716                 sleep 10
1717                 cd /
1718                 umount ${media_mountpoint}
1719                 sync
1720                 echo u > /proc/sysrq-trigger
1721                 echo b > /proc/sysrq-trigger
1722         else
1723                 panic "Not ok, a media defect is likely, switch to VT8 for details."
1724         fi
1725 }
1726
1727 mountroot ()
1728 {
1729         if [ -x /scripts/local-top/cryptroot ]; then
1730             /scripts/local-top/cryptroot
1731         fi
1732
1733         exec 6>&1
1734         exec 7>&2
1735         exec > live.log
1736         exec 2>&1
1737         tail -f live.log >&7 &
1738         tailpid="${!}"
1739
1740         # Ensure 'panic' function is overridden
1741         . /scripts/live-functions
1742
1743         Arguments
1744
1745         maybe_break live-premount
1746         log_begin_msg "Running /scripts/live-premount"
1747         run_scripts /scripts/live-premount
1748         log_end_msg
1749
1750         # Needed here too because some things (*cough* udev *cough*)
1751         # changes the timeout
1752
1753         if [ ! -z "${NETBOOT}" ] || [ ! -z "${FETCH}" ] || [ ! -z "${HTTPFS}" ] || [ ! -z "${FTPFS}" ]
1754         then
1755                 if do_netmount
1756                 then
1757                         livefs_root="${mountpoint}"
1758                 else
1759                         panic "Unable to find a live file system on the network"
1760                 fi
1761         else
1762                 if [ -n "${ISCSI_PORTAL}" ]
1763                 then
1764                         do_iscsi && livefs_root="${mountpoint}"
1765                 elif [ -n "${PLAIN_ROOT}" ] && [ -n "${ROOT}" ]
1766                 then
1767                         # Do a local boot from hd
1768                         livefs_root=${ROOT}
1769                 else
1770                         if [ -x /usr/bin/memdiskfind ]
1771                         then
1772                                 MEMDISK=$(/usr/bin/memdiskfind)
1773
1774                                 if [ $? -eq 0 ]
1775                                 then
1776                                         # We found a memdisk, set up phram
1777                                         modprobe phram phram=memdisk,${MEMDISK}
1778
1779                                         # Load mtdblock, the memdisk will be /dev/mtdblock0
1780                                         modprobe mtdblock
1781                                 fi
1782                         fi
1783
1784                         # Scan local devices for the image
1785                         i=0
1786                         while [ "$i" -lt 60 ]
1787                         do
1788                                 livefs_root=$(find_livefs ${i})
1789
1790                                 if [ -n "${livefs_root}" ]
1791                                 then
1792                                         break
1793                                 fi
1794
1795                                 sleep 1
1796                                 i="$(($i + 1))"
1797                         done
1798                 fi
1799         fi
1800
1801         if [ -z "${livefs_root}" ]
1802         then
1803                 panic "Unable to find a medium containing a live file system"
1804         fi
1805
1806         if [ "${INTEGRITY_CHECK}" ]
1807         then
1808                 integrity_check "${livefs_root}"
1809         fi
1810
1811         if [ "${TORAM}" ]
1812         then
1813                 live_dest="ram"
1814         elif [ "${TODISK}" ]
1815         then
1816                 live_dest="${TODISK}"
1817         fi
1818
1819         if [ "${live_dest}" ]
1820         then
1821                 log_begin_msg "Copying live media to ${live_dest}"
1822                 copy_live_to "${livefs_root}" "${live_dest}"
1823                 log_end_msg
1824         fi
1825
1826         # if we do not unmount the ISO we can't run "fsck /dev/ice" later on
1827         # because the mountpoint is left behind in /proc/mounts, so let's get
1828         # rid of it when running from RAM
1829         if [ -n "$FROMISO" ] && [ "${TORAM}" ]
1830         then
1831           losetup -d /dev/loop0
1832           grep -q /isofrom /proc/mounts && umount /isofrom
1833         fi
1834
1835         if [ -n "${MODULETORAMFILE}" ] || [ -n "${PLAIN_ROOT}" ]
1836         then
1837                 setup_unionfs "${livefs_root}" "${rootmnt}"
1838         else
1839                 mac="$(get_mac)"
1840                 mac="$(echo ${mac} | sed 's/-//g')"
1841                 mount_images_in_directory "${livefs_root}" "${rootmnt}" "${mac}"
1842         fi
1843
1844
1845         if [ -n "${ROOT_PID}" ] ; then
1846                 echo "${ROOT_PID}" > "${rootmnt}"/live/root.pid
1847         fi
1848
1849         log_end_msg
1850
1851         # unionfs-fuse needs /dev to be bind-mounted for the duration of
1852         # live-bottom; udev's init script will take care of things after that
1853         if [ "${UNIONTYPE}" = unionfs-fuse ]
1854         then
1855                 mount -n -o bind /dev "${rootmnt}/dev"
1856         fi
1857
1858         # Move to the new root filesystem so that programs there can get at it.
1859         if [ ! -d /root/live/image ]
1860         then
1861                 mkdir -p /root/live/image
1862                 mount --move /live/image /root/live/image
1863         fi
1864
1865         # aufs2 in kernel versions around 2.6.33 has a regression:
1866         # directories can't be accessed when read for the first the time,
1867         # causing a failure for example when accessing /var/lib/fai
1868         # when booting FAI, this simple workaround solves it
1869         ls /root/* >/dev/null 2>&1
1870
1871         # copy snapshot configuration if exists
1872         if [ -f snapshot.conf ]
1873         then
1874                 log_begin_msg "Copying snapshot.conf to ${rootmnt}/etc/live/boot.d"
1875                 if [ ! -d "${rootmnt}/etc/live/boot.d" ]
1876                 then
1877                         mkdir -p "${rootmnt}/etc/live/boot.d"
1878                 fi
1879                 cp snapshot.conf "${rootmnt}/etc/live/boot.d/"
1880                 log_end_msg
1881         fi
1882
1883         maybe_break live-bottom
1884         log_begin_msg "Running /scripts/live-bottom\n"
1885
1886         run_scripts /scripts/live-bottom
1887         log_end_msg
1888
1889         if [ "${UNIONFS}" = unionfs-fuse ]
1890         then
1891                 umount "${rootmnt}/dev"
1892         fi
1893
1894         exec 1>&6 6>&-
1895         exec 2>&7 7>&-
1896         kill ${tailpid}
1897         [ -w "${rootmnt}/var/log/" ] && cp live.log "${rootmnt}/var/log/" 2>/dev/null
1898 }