d7742847a6acc32bc81420172562a707dcffa780
[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_overlay_label="full-ov"
15 custom_overlay_label="custom-ov"
16 root_snapshot_label="live-sn"
17 home_snapshot_label="home-sn"
18 persistence_list="live.persist"
19
20 USERNAME="user"
21 USERFULLNAME="Live user"
22 HOSTNAME="host"
23
24 mkdir -p "${mountpoint}"
25 tried="/tmp/tried"
26
27 # Create /etc/mtab for debug purpose and future syncs
28 if [ ! -d /etc ]
29 then
30         mkdir /etc/
31 fi
32
33 if [ ! -f /etc/mtab ]
34 then
35         touch /etc/mtab
36 fi
37
38 . /scripts/live-helpers
39
40 if [ ! -f /live.vars ]
41 then
42         touch /live.vars
43 fi
44
45 Arguments ()
46 {
47         PRESEEDS=""
48         LOCATIONS=""
49
50         for ARGUMENT in $(cat /proc/cmdline)
51         do
52                 case "${ARGUMENT}" in
53                         skipconfig)
54                                 NOACCESSIBILITY="Yes"
55                                 NOFASTBOOT="Yes"
56                                 NOFSTAB="Yes"
57                                 NONETWORKING="Yes"
58
59                                 export NOACCESSIBILITY NOFASTBOOT NOFSTAB NONETWORKING
60                                 ;;
61
62                         access=*)
63                                 ACCESS="${ARGUMENT#access=}"
64                                 export ACCESS
65                                 ;;
66
67                         console=*)
68                                 DEFCONSOLE="${ARGUMENT#*=}"
69                                 export DEFCONSOLE
70                                 ;;
71
72                         BOOTIF=*)
73                                 BOOTIF="${x#BOOTIF=}"
74                                 ;;
75
76                         debug)
77                                 DEBUG="Yes"
78                                 export DEBUG
79
80                                 set -x
81                                 ;;
82
83                         dhcp)
84                                 # Force dhcp even while netbooting
85                                 # Use for debugging in case somebody works on fixing dhclient
86                                 DHCP="Force";
87                                 export DHCP
88                                 ;;
89
90                         nodhcp)
91                                 unset DHCP
92                                 ;;
93
94                         ethdevice=*)
95                                 DEVICE="${ARGUMENT#ethdevice=}"
96                                 ETHDEVICE="${DEVICE}"
97                                 export DEVICE ETHDEVICE
98                                 ;;
99
100                         ethdevice-timeout=*)
101                                 ETHDEV_TIMEOUT="${ARGUMENT#ethdevice-timeout=}"
102                                 export ETHDEV_TIMEOUT
103                                 ;;
104
105                         fetch=*)
106                                 FETCH="${ARGUMENT#fetch=}"
107                                 export FETCH
108                                 ;;
109
110                         forcepersistentfsck)
111                                 FORCEPERSISTENTFSCK="Yes"
112                                 export FORCEPERSISTENTFSCK
113                                 ;;
114
115                         ftpfs=*)
116                                 FTPFS="${ARGUMENT#ftpfs=}"
117                                 export FTPFS
118                                 ;;
119
120                         httpfs=*)
121                                 HTTPFS="${ARGUMENT#httpfs=}"
122                                 export HTTPFS
123                                 ;;
124
125                         iscsi=*)
126                                 ISCSI="${ARGUMENT#iscsi=}"
127                                 #ip:port - separated by ;
128                                 ISCSI_PORTAL="${ISCSI%;*}"
129                                 if echo "${ISCSI_PORTAL}" | grep -q , ; then
130                                         ISCSI_SERVER="${ISCSI_PORTAL%,*}"
131                                         ISCSI_PORT="${ISCSI_PORTAL#*,}"
132                                 fi
133                                 #target name
134                                 ISCSI_TARGET="${ISCSI#*;}"
135                                 export ISCSI ISCSI_PORTAL ISCSI_TARGET ISCSI_SERVER ISCSI_PORT
136                                 ;;
137
138                         isofrom=*|fromiso=*)
139                                 FROMISO="${ARGUMENT#*=}"
140                                 export FROMISO
141                                 ;;
142
143                         ignore_uuid)
144                                 IGNORE_UUID="Yes"
145                                 export IGNORE_UUID
146                                 ;;
147
148                         integrity-check)
149                                 INTEGRITY_CHECK="Yes"
150                                 export INTEGRITY_CHECK
151                                 ;;
152
153                         ip=*)
154                                 STATICIP="${ARGUMENT#ip=}"
155
156                                 if [ -z "${STATICIP}" ]
157                                 then
158                                         STATICIP="frommedia"
159                                 fi
160
161                                 export STATICIP
162                                 ;;
163
164                         live-getty)
165                                 LIVE_GETTY="1"
166                                 export LIVE_GETTY
167                                 ;;
168
169                         live-media=*|bootfrom=*)
170                                 LIVE_MEDIA="${ARGUMENT#*=}"
171                                 export LIVE_MEDIA
172                                 ;;
173
174                         live-media-encryption=*|encryption=*)
175                                 LIVE_MEDIA_ENCRYPTION="${ARGUMENT#*=}"
176                                 export LIVE_MEDIA_ENCRYPTION
177                                 ;;
178
179                         live-media-offset=*)
180                                 LIVE_MEDIA_OFFSET="${ARGUMENT#live-media-offset=}"
181                                 export LIVE_MEDIA_OFFSET
182                                 ;;
183
184                         live-media-path=*)
185                                 LIVE_MEDIA_PATH="${ARGUMENT#live-media-path=}"
186                                 export LIVE_MEDIA_PATH
187                                 ;;
188
189                         live-media-timeout=*)
190                                 LIVE_MEDIA_TIMEOUT="${ARGUMENT#live-media-timeout=}"
191                                 export LIVE_MEDIA_TIMEOUT
192                                 ;;
193
194                         module=*)
195                                 MODULE="${ARGUMENT#module=}"
196                                 export MODULE
197                                 ;;
198
199                         netboot=*)
200                                 NETBOOT="${ARGUMENT#netboot=}"
201                                 export NETBOOT
202                                 ;;
203
204                         nfsopts=*)
205                                 NFSOPTS="${ARGUMENT#nfsopts=}"
206                                 export NFSOPTS
207                                 ;;
208
209                         nfscow=*)
210                                 NFS_COW="${ARGUMENT#nfscow=}"
211                                 export NFS_COW
212                                 ;;
213
214                         noaccessibility)
215                                 NOACCESSIBILITY="Yes"
216                                 export NOACCESSIBILITY
217                                 ;;
218
219                         nofastboot)
220                                 NOFASTBOOT="Yes"
221                                 export NOFASTBOOT
222                                 ;;
223
224                         nofstab)
225                                 NOFSTAB="Yes"
226                                 export NOFSTAB
227                                 ;;
228
229                         nonetworking)
230                                 NONETWORKING="Yes"
231                                 export NONETWORKING
232                                 ;;
233
234                         ramdisk-size=*)
235                                 ramdisk_size="${ARGUMENT#ramdisk-size=}"
236                                 ;;
237
238                         swapon)
239                                 SWAPON="Yes"
240                                 export SWAPON
241                                 ;;
242
243                         persistent)
244                                 PERSISTENT="Yes"
245                                 export PERSISTENT
246                                 ;;
247
248                         persistent-encryption=*)
249                                 PERSISTENT_ENCRYPTION="${ARGUMENT#*=}"
250                                 export PERSISTENT_ENCRYPTION
251                                 ;;
252
253                         persistent-media=*)
254                                 PERSISTENT_MEDIA="${ARGUMENT#*=}"
255                                 export PERSISTENT_MEDIA
256                                 ;;
257                         persistent-method=*)
258                                 PERSISTENT_METHOD="${ARGUMENT#*=}"
259                                 export PERSISTENT_METHOD
260                                 ;;
261
262                         persistent-path=*)
263                                 PERSISTENT_PATH="${ARGUMENT#persistent-path=}"
264                                 export PERSISTENT_PATH
265                                 ;;
266                         persistent-read-only)
267                                 PERSISTENT_READONLY="Yes"
268                                 export PERSISTENT_READONLY
269                                 ;;
270
271                         persistent-storage=*)
272                                 PERSISTENT_STORAGE="${ARGUMENT#persistent-storage=}"
273                                 export PERSISTENT_STORAGE
274                                 ;;
275
276                         persistent-subtext=*)
277                                 root_overlay_label="${root_overlay_label}-${ARGUMENT#persistent-subtext=}"
278                                 custom_overlay_label="${custom_overlay_label}-${ARGUMENT#persistent-subtext=}"
279                                 root_snapshot_label="${root_snapshot_label}-${ARGUMENT#persistent-subtext=}"
280                                 home_snapshot_label="${home_snapshot_label}-${ARGUMENT#persistent-subtext=}"
281                                 ;;
282
283                         nopersistent)
284                                 NOPERSISTENT="Yes"
285                                 export NOPERSISTENT
286                                 ;;
287
288                         noprompt)
289                                 NOPROMPT="Yes"
290                                 export NOPROMPT
291                                 ;;
292
293                         noprompt=*)
294                                 NOPROMPT="${ARGUMENT#noprompt=}"
295                                 export NOPROMPT
296                                 ;;
297
298                         quickusbmodules)
299                                 QUICKUSBMODULES="Yes"
300                                 export QUICKUSBMODULES
301                                 ;;
302
303                         preseed/file=*|file=*)
304                                 LOCATIONS="${ARGUMENT#*=} ${LOCATIONS}"
305                                 export LOCATIONS
306                                 ;;
307
308                         nopreseed)
309                                 NOPRESEED="Yes"
310                                 export NOPRESEED
311                                 ;;
312
313                         */*=*)
314                                 question="${ARGUMENT%%=*}"
315                                 value="${ARGUMENT#*=}"
316                                 PRESEEDS="${PRESEEDS}\"${question}=${value}\" "
317                                 export PRESEEDS
318                                 ;;
319
320                         showmounts)
321                                 SHOWMOUNTS="Yes"
322                                 export SHOWMOUNTS
323                                 ;;
324
325                         silent)
326                                 SILENT="Yes"
327                                 export SILENT
328                                 ;;
329
330                         todisk=*)
331                                 TODISK="${ARGUMENT#todisk=}"
332                                 export TODISK
333                                 ;;
334
335                         toram)
336                                 TORAM="Yes"
337                                 export TORAM
338                                 ;;
339
340                         toram=*)
341                                 TORAM="Yes"
342                                 MODULETORAM="${ARGUMENT#toram=}"
343                                 export TORAM MODULETORAM
344                                 ;;
345
346                         exposedroot)
347                                 EXPOSED_ROOT="Yes"
348                                 export EXPOSED_ROOT
349                                 ;;
350
351                         plainroot)
352                                 PLAIN_ROOT="Yes"
353                                 export PLAIN_ROOT
354                                 ;;
355
356                         skipunion)
357                                 SKIP_UNION_MOUNTS="Yes"
358                                 export SKIP_UNION_MOUNTS
359                                 ;;
360
361                         root=*)
362                                 ROOT="${ARGUMENT#root=}"
363                                 export ROOT
364                                 ;;
365
366                         union=*)
367                                 UNIONTYPE="${ARGUMENT#union=}"
368                                 export UNIONTYPE
369                                 ;;
370                 esac
371         done
372
373         # sort of compatibility with netboot.h from linux docs
374         if [ -z "${NETBOOT}" ]
375         then
376                 if [ "${ROOT}" = "/dev/nfs" ]
377                 then
378                         NETBOOT="nfs"
379                         export NETBOOT
380                 elif [ "${ROOT}" = "/dev/cifs" ]
381                 then
382                         NETBOOT="cifs"
383                         export NETBOOT
384                 fi
385         fi
386
387         if [ -z "${MODULE}" ]
388         then
389                 MODULE="filesystem"
390                 export MODULE
391         fi
392
393         if [ -z "${UNIONTYPE}" ]
394         then
395                 UNIONTYPE="aufs"
396                 export UNIONTYPE
397         fi
398
399         if [ -z "${PERSISTENT_ENCRYPTION}" ]
400         then
401                 PERSISTENT_ENCRYPTION="none"
402                 export PERSISTENT_ENCRYPTION
403         elif echo ${PERSISTENT_ENCRYPTION} | grep -qe "\<luks\>"
404         then
405                 if ! modprobe dm-crypt
406                 then
407                         log_warning_msg "Unable to load module dm-crypt"
408                         PERSISTENT_ENCRYPTION=$(echo ${PERSISTENT_ENCRYPTION} | sed -e 's/\<luks,\|,\?luks$//g')
409                         export PERSISTENT_ENCRYPTION
410                 fi
411
412                 if [ ! -x /lib/cryptsetup/askpass ] || [ ! -x /sbin/cryptsetup ]
413                 then
414                         log_warning_msg "cryptsetup in unavailable"
415                         PERSISTENT_ENCRYPTION=$(echo ${PERSISTENT_ENCRYPTION} | sed -e 's/\<luks,\|,\?luks$//g')
416                         export PERSISTENT_ENCRYPTION
417                 fi
418         fi
419
420         if [ -n "${PERSISTENT}" ] && [ -z "${PERSISTENT_METHOD}" ]
421         then
422                 PERSISTENT_METHOD="snapshot,overlay"
423                 export PERSISTENT_METHOD
424         fi
425
426         if [ -n "${PERSISTENT}" ] && [ -z "${PERSISTENT_STORAGE}" ]
427         then
428                 PERSISTENT_STORAGE="filesystem,file"
429                 export PERSISTENT_STORAGE
430         fi
431 }
432
433 is_live_path ()
434 {
435         DIRECTORY="${1}"
436
437         if [ -d "${DIRECTORY}"/"${LIVE_MEDIA_PATH}" ]
438         then
439                 for FILESYSTEM in squashfs ext2 ext3 ext4 xfs dir jffs2
440                 do
441                         if [ "$(echo ${DIRECTORY}/${LIVE_MEDIA_PATH}/*.${FILESYSTEM})" != "${DIRECTORY}/${LIVE_MEDIA_PATH}/*.${FILESYSTEM}" ]
442                         then
443                                 return 0
444                         fi
445                 done
446         fi
447
448         return 1
449 }
450
451 matches_uuid ()
452 {
453         if [ "${IGNORE_UUID}" ] || [ ! -e /conf/uuid.conf ]
454         then
455                 return 0
456         fi
457
458         path="${1}"
459         uuid="$(cat /conf/uuid.conf)"
460
461         for try_uuid_file in "${path}/.disk/live-uuid"*
462         do
463                 [ -e "${try_uuid_file}" ] || continue
464
465                 try_uuid="$(cat "${try_uuid_file}")"
466
467                 if [ "${uuid}" = "${try_uuid}" ]
468                 then
469                         return 0
470                 fi
471         done
472
473         return 1
474 }
475
476 get_backing_device ()
477 {
478         case "${1}" in
479                 *.squashfs|*.ext2|*.ext3|*.ext4|*.jffs2)
480                         echo $(setup_loop "${1}" "loop" "/sys/block/loop*" '0' "${LIVE_MEDIA_ENCRYPTION}" "${2}")
481                         ;;
482
483                 *.dir)
484                         echo "directory"
485                         ;;
486
487                 *)
488                         panic "Unrecognized live filesystem: ${1}"
489                         ;;
490         esac
491 }
492
493 match_files_in_dir ()
494 {
495         # Does any files match pattern ${1} ?
496         local pattern="${1}"
497
498         if [ "$(echo ${pattern})" != "${pattern}" ]
499         then
500                 return 0
501         fi
502
503         return 1
504 }
505
506 mount_images_in_directory ()
507 {
508         directory="${1}"
509         rootmnt="${2}"
510         mac="${3}"
511
512
513         if match_files_in_dir "${directory}/${LIVE_MEDIA_PATH}/*.squashfs" ||
514                 match_files_in_dir "${directory}/${LIVE_MEDIA_PATH}/*.ext2" ||
515                 match_files_in_dir "${directory}/${LIVE_MEDIA_PATH}/*.ext3" ||
516                 match_files_in_dir "${directory}/${LIVE_MEDIA_PATH}/*.ext4" ||
517                 match_files_in_dir "${directory}/${LIVE_MEDIA_PATH}/*.jffs2" ||
518                 match_files_in_dir "${directory}/${LIVE_MEDIA_PATH}/*.dir"
519         then
520                 [ -n "${mac}" ] && adddirectory="${directory}/${LIVE_MEDIA_PATH}/${mac}"
521                 setup_unionfs "${directory}/${LIVE_MEDIA_PATH}" "${rootmnt}" "${adddirectory}"
522         else
523                 panic "No supported filesystem images found at /${LIVE_MEDIA_PATH}."
524         fi
525 }
526
527 is_nice_device ()
528 {
529         sysfs_path="${1#/sys}"
530
531         if [ -e /lib/udev/path_id ]
532         then
533                 # squeeze
534                 PATH_ID="/lib/udev/path_id"
535         else
536                 # wheezy/sid (udev >= 174)
537                 PATH_ID="/sbin/udevadm test-builtin path_id"
538         fi
539
540         if ${PATH_ID} "${sysfs_path}" | egrep -q "ID_PATH=(usb|pci-[^-]*-(ide|sas|scsi|usb|virtio)|platform-sata_mv|platform-orion-ehci|platform-mmc|platform-mxsdhci)"
541         then
542                 return 0
543         elif echo "${sysfs_path}" | grep -q '^/block/vd[a-z]$'
544         then
545                 return 0
546         elif echo ${sysfs_path} | grep -q "^/block/dm-"
547         then
548                 return 0
549         elif echo ${sysfs_path} | grep -q "^/block/mtdblock"
550         then
551                 return 0
552         fi
553
554         return 1
555 }
556
557 copy_live_to ()
558 {
559         copyfrom="${1}"
560         copytodev="${2}"
561         copyto="${copyfrom}_swap"
562
563         if [ -z "${MODULETORAM}" ]
564         then
565                 size=$(fs_size "" ${copyfrom}/${LIVE_MEDIA_PATH} "used")
566         else
567                 MODULETORAMFILE="${copyfrom}/${LIVE_MEDIA_PATH}/${MODULETORAM}"
568
569                 if [ -f "${MODULETORAMFILE}" ]
570                 then
571                         size=$( expr $(ls -la ${MODULETORAMFILE} | awk '{print $5}') / 1024 + 5000 )
572                 else
573                         log_warning_msg "Error: toram-module ${MODULETORAM} (${MODULETORAMFILE}) could not be read."
574                         return 1
575                 fi
576         fi
577
578         if [ "${copytodev}" = "ram" ]
579         then
580                 # copying to ram:
581                 freespace=$(awk '/^MemFree:/{f=$2} /^Cached:/{c=$2} END{print f+c}' /proc/meminfo)
582                 mount_options="-o size=${size}k"
583                 free_string="memory"
584                 fstype="tmpfs"
585                 dev="/dev/shm"
586         else
587                 # it should be a writable block device
588                 if [ -b "${copytodev}" ]
589                 then
590                         dev="${copytodev}"
591                         free_string="space"
592                         fstype=$(get_fstype "${dev}")
593                         freespace=$(fs_size "${dev}")
594                 else
595                         log_warning_msg "${copytodev} is not a block device."
596                         return 1
597                 fi
598         fi
599
600         if [ "${freespace}" -lt "${size}" ]
601         then
602                 log_warning_msg "Not enough free ${free_string} (${freespace}k free, ${size}k needed) to copy live media in ${copytodev}."
603                 return 1
604         fi
605
606         # Custom ramdisk size
607         if [ -z "${mount_options}" ] && [ -n "${ramdisk_size}" ]
608         then
609                 # FIXME: should check for wrong values
610                 mount_options="-o size=${ramdisk_size}"
611         fi
612
613         # begin copying (or uncompressing)
614         mkdir "${copyto}"
615         log_begin_msg "mount -t ${fstype} ${mount_options} ${dev} ${copyto}"
616         mount -t "${fstype}" ${mount_options} "${dev}" "${copyto}"
617
618         if [ "${extension}" = "tgz" ]
619         then
620                 cd "${copyto}"
621                 tar zxf "${copyfrom}/${LIVE_MEDIA_PATH}/$(basename ${FETCH})"
622                 rm -f "${copyfrom}/${LIVE_MEDIA_PATH}/$(basename ${FETCH})"
623                 mount -r -o move "${copyto}" "${rootmnt}"
624                 cd "${OLDPWD}"
625         else
626                 if [ -n "${MODULETORAMFILE}" ]
627                 then
628                         if [ -x /bin/rsync ]
629                         then
630                                 echo " * Copying $MODULETORAMFILE to RAM" 1>/dev/console
631                                 rsync -a --progress ${MODULETORAMFILE} ${copyto} 1>/dev/console # copy only the filesystem module
632                         else
633                                 cp ${MODULETORAMFILE} ${copyto} # copy only the filesystem module
634                         fi
635                 else
636                         if [ -x /bin/rsync ]
637                         then
638                                 echo " * Copying whole medium to RAM" 1>/dev/console
639                                 rsync -a --progress ${copyfrom}/* ${copyto} 1>/dev/console  # "cp -a" from busybox also copies hidden files
640                         else
641                                 mkdir -p ${copyto}/${LIVE_MEDIA_PATH}
642                                 cp -a ${copyfrom}/${LIVE_MEDIA_PATH}/* ${copyto}/${LIVE_MEDIA_PATH}
643                                 if [ -e ${copyfrom}/${LIVE_MEDIA_PATH}/.disk ]
644                                 then
645                                         cp -a ${copyfrom}/${LIVE_MEDIA_PATH}/.disk ${copyto}
646                                 fi
647                         fi
648                 fi
649
650                 umount ${copyfrom}
651                 mount -r -o move ${copyto} ${copyfrom}
652         fi
653
654         rmdir ${copyto}
655         return 0
656 }
657
658 do_netsetup ()
659 {
660         modprobe -q af_packet # For DHCP
661
662         udevadm trigger
663         udevadm settle
664
665         [ -n "$ETHDEV_TIMEOUT" ] || ETHDEV_TIMEOUT=15
666         echo "Using timeout of $ETHDEV_TIMEOUT seconds for network configuration."
667
668         if [ -z "${NETBOOT}" ] && [ -z "${FETCH}" ] && \
669            [ -z "${HTTPFS}" ] && [ -z "${FTPFS}" ]
670         then
671
672
673         # support for Syslinux IPAPPEND parameter
674         # it sets the BOOTIF variable on the kernel parameter
675
676         if [ -n "${BOOTIF}" ]
677         then
678                 # pxelinux sets BOOTIF to a value based on the mac address of the
679                 # network card used to PXE boot, so use this value for DEVICE rather
680                 # than a hard-coded device name from initramfs.conf. this facilitates
681                 # network booting when machines may have multiple network cards.
682                 # pxelinux sets BOOTIF to 01-$mac_address
683
684                 # strip off the leading "01-", which isn't part of the mac
685                 # address
686                 temp_mac=${BOOTIF#*-}
687
688                 # convert to typical mac address format by replacing "-" with ":"
689                 bootif_mac=""
690                 IFS='-'
691                 for x in $temp_mac
692                 do
693                         if [ -z "$bootif_mac" ]
694                         then
695                                 bootif_mac="$x"
696                         else
697                                 bootif_mac="$bootif_mac:$x"
698                         fi
699                 done
700                 unset IFS
701
702                 # look for devices with matching mac address, and set DEVICE to
703                 # appropriate value if match is found.
704
705                 for device in /sys/class/net/*
706                 do
707                         if [ -f "$device/address" ]
708                         then
709                                 current_mac=$(cat "$device/address")
710
711                                 if [ "$bootif_mac" = "$current_mac" ]
712                                 then
713                                         DEVICE=${device##*/}
714                                         break
715                                 fi
716                         fi
717                 done
718         fi
719
720         # if ethdevice was not specified on the kernel command line
721         # make sure we try to get a working network configuration
722         # for *every* present network device (except for loopback of course)
723         if [ -z "$ETHDEVICE" ] ; then
724                 echo "If you want to boot from a specific device use bootoption ethdevice=..."
725                 for device in /sys/class/net/*; do
726                         dev=${device##*/} ;
727                         if [ "$dev" != "lo" ] ; then
728                                 ETHDEVICE="$ETHDEVICE $dev"
729                         fi
730                 done
731         fi
732
733         # split args of ethdevice=eth0,eth1 into "eth0 eth1"
734         for device in $(echo $ETHDEVICE | sed 's/,/ /g') ; do
735                 devlist="$devlist $device"
736         done
737
738         # this is tricky (and ugly) because ipconfig sometimes just hangs/runs into
739         # an endless loop; iff execution fails give it two further tries, that's
740         # why we use '$devlist $devlist $devlist' for the other for loop
741         for dev in $devlist $devlist $devlist ; do
742                 echo "Executing ipconfig -t $ETHDEV_TIMEOUT $dev"
743                 ipconfig -t "$ETHDEV_TIMEOUT" $dev | tee -a /netboot.config &
744                 jobid=$!
745                 sleep "$ETHDEV_TIMEOUT" ; sleep 1
746                 if [ -r /proc/"$jobid"/status ] ; then
747                         echo "Killing job $jobid for device $dev as ipconfig ran into recursion..."
748                         kill -9 $jobid
749                 fi
750
751                 # if configuration of device worked we should have an assigned
752                 # IP address, iff so let's use the according as $DEVICE for later usage
753                 # simple and primitive approach which seems to work fine
754                 if ifconfig $dev | grep -q 'inet.*addr:' ; then
755                         export DEVICE="$dev"
756                         break
757                 fi
758         done
759
760         else
761                 for interface in ${DEVICE}; do
762                         ipconfig -t "$ETHDEV_TIMEOUT" ${interface} | tee /netboot-${interface}.config
763                         [ -e /tmp/net-${interface}.conf ] && . /tmp/net-${interface}.conf
764                         if [ "$IPV4ADDR" != "0.0.0.0" ]
765                         then
766                                 break
767                         fi
768                 done
769         fi
770
771         for interface in ${DEVICE}; do
772                 # source relevant ipconfig output
773                 OLDHOSTNAME=${HOSTNAME}
774                 [ -e /tmp/net-${interface}.conf ] && . /tmp/net-${interface}.conf
775                 [ -z ${HOSTNAME} ] && HOSTNAME=${OLDHOSTNAME}
776                 export HOSTNAME
777
778                 if [ -n "${interface}" ]
779                 then
780                         HWADDR="$(cat /sys/class/net/${interface}/address)"
781                 fi
782
783                 if [ ! -e "/etc/resolv.conf" ]
784                 then
785                         echo "Creating /etc/resolv.conf"
786
787                         if [ -n "${DNSDOMAIN}" ]
788                         then
789                                 echo "domain ${DNSDOMAIN}" > /etc/resolv.conf
790                                 echo "search ${DNSDOMAIN}" >> /etc/resolv.conf
791                         fi
792
793                         for i in ${IPV4DNS0} ${IPV4DNS1} ${IPV4DNS1}
794                         do
795                                 if [ -n "$i" ] && [ "$i" != 0.0.0.0 ]
796                                 then
797                                         echo "nameserver $i" >> /etc/resolv.conf
798                                 fi
799                         done
800                 fi
801
802                 # Check if we have a network device at all
803                 if ! ls /sys/class/net/"$interface" > /dev/null 2>&1 && \
804                    ! ls /sys/class/net/eth0 > /dev/null 2>&1 && \
805                    ! ls /sys/class/net/wlan0 > /dev/null 2>&1 && \
806                    ! ls /sys/class/net/ath0 > /dev/null 2>&1 && \
807                    ! ls /sys/class/net/ra0 > /dev/null 2>&1
808                 then
809                         panic "No supported network device found, maybe a non-mainline driver is required."
810                 fi
811         done
812 }
813
814 do_netmount()
815 {
816         do_netsetup
817
818         if [ "${NFSROOT}" = "auto" ]
819         then
820                 NFSROOT=${ROOTSERVER}:${ROOTPATH}
821         fi
822
823         rc=1
824
825         if ( [ -n "${FETCH}" ] || [ -n "${HTTPFS}" ] || [ -n "${FTPFS}" ] )
826         then
827                 do_httpmount
828                 return $?
829         fi
830
831         if [ "${NFSROOT#*:}" = "${NFSROOT}" ] && [ "$NETBOOT" != "cifs" ]
832         then
833                 NFSROOT=${ROOTSERVER}:${NFSROOT}
834         fi
835
836         log_begin_msg "Trying netboot from ${NFSROOT}"
837
838         if [ "${NETBOOT}" != "nfs" ] && do_cifsmount
839         then
840                 rc=0
841         elif do_nfsmount
842         then
843                 NETBOOT="nfs"
844                 export NETBOOT
845                 rc=0
846         fi
847
848         log_end_msg
849         return ${rc}
850 }
851
852 do_iscsi()
853 {
854         do_netsetup
855         #modprobe ib_iser
856         modprobe iscsi_tcp
857         local debugopt=""
858         [ "${DEBUG}" = "Yes" ] && debugopt="-d 8"
859         #FIXME this name is supposed to be unique - some date + ifconfig hash?
860         ISCSI_INITIATORNAME="iqn.1993-08.org.debian.live:01:$(echo "${HWADDR}" | sed -e s/://g)"
861         export ISCSI_INITIATORNAME
862         if [ -n "${ISCSI_SERVER}" ] ; then
863                 iscsistart $debugopt -i "${ISCSI_INITIATORNAME}" -t "${ISCSI_TARGET}" -g 1 -a "${ISCSI_SERVER}" -p "${ISCSI_PORT}"
864         else
865                 iscsistart $debugopt -i "${ISCSI_INITIATORNAME}" -t "${ISCSI_TARGET}" -g 1 -a "${ISCSI_PORTAL}" -p 3260
866         fi
867         if [ $? != 0 ]
868         then
869                 panic "Failed to log into iscsi target"
870         fi
871         local host="$(ls -d /sys/class/scsi_host/host*/device/iscsi_host:host* \
872                             /sys/class/scsi_host/host*/device/iscsi_host/host* | sed -e 's:/device.*::' -e 's:.*host::')"
873         if [ -n "${host}" ]
874         then
875                 local devices=""
876                 local i=0
877                 while [ -z "${devices}" -a $i -lt 60 ]
878                 do
879                         sleep 1
880                         devices="$(ls -d /sys/class/scsi_device/${host}*/device/block:* \
881                                          /sys/class/scsi_device/${host}*/device/block/* | sed -e 's!.*[:/]!!')"
882                         i=$(expr $i + 1)
883                         echo -ne $i\\r
884                 done
885                 for dev in $devices
886                 do
887                         if check_dev "null" "/dev/$dev"
888                         then
889                                 NETBOOT="iscsi"
890                                 export NETBOOT
891                                 return 0;
892                         fi
893                 done
894                 panic "Failed to locate a live device on iSCSI devices (tried: $devices)."
895         else
896                 panic "Failed to locate iSCSI host in /sys"
897         fi
898 }
899
900 do_httpmount ()
901 {
902         rc=1
903
904         for webfile in HTTPFS FTPFS FETCH
905         do
906                 local url="$(eval echo \"\$\{${webfile}\}\")"
907                 local extension="$(echo "${url}" | sed 's/\(.*\)\.\(.*\)/\2/')"
908
909                 if [ -n "$url" ]
910                 then
911                         case "${extension}" in
912                                 iso|squashfs|tgz|tar)
913                                         if [ "${extension}" = "iso" ]
914                                         then
915                                                 mkdir -p "${alt_mountpoint}"
916                                                 dest="${alt_mountpoint}"
917                                         else
918                                                 local dest="${mountpoint}/${LIVE_MEDIA_PATH}"
919                                                 mount -t ramfs ram "${mountpoint}"
920                                                 mkdir -p "${dest}"
921                                         fi
922                                         if [ "${webfile}" = "FETCH" ]
923                                         then
924                                                 case "$url" in
925                                                         tftp*)
926                                                                 ip="$(dirname $url | sed -e 's|tftp://||g' -e 's|/.*$||g')"
927                                                                 rfile="$(echo $url | sed -e "s|tftp://$ip||g")"
928                                                                 lfile="$(basename $url)"
929                                                                 log_begin_msg "Trying tftp -g -b 10240 -r $rfile -l ${dest}/$lfile $ip"
930                                                                 tftp -g -b 10240 -r $rfile -l ${dest}/$lfile $ip
931                                                         ;;
932
933                                                         *)
934                                                                 log_begin_msg "Trying wget ${url} -O ${dest}/$(basename ${url})"
935                                                                 wget "${url}" -O "${dest}/$(basename ${url})"
936                                                                 ;;
937                                                 esac
938                                         else
939                                                 log_begin_msg "Trying to mount ${url} on ${dest}/$(basename ${url})"
940                                                 if [ "${webfile}" = "FTPFS" ]
941                                                 then
942                                                         FUSE_MOUNT="curlftpfs"
943                                                         url="$(dirname ${url})"
944                                                 else
945                                                         FUSE_MOUNT="httpfs"
946                                                 fi
947                                                 modprobe fuse
948                                                 $FUSE_MOUNT "${url}" "${dest}"
949                                                 ROOT_PID="$(minips h -C "$FUSE_MOUNT" | { read x y ; echo "$x" ; } )"
950                                         fi
951                                         [ ${?} -eq 0 ] && rc=0
952                                         [ "${extension}" = "tgz" ] && live_dest="ram"
953                                         if [ "${extension}" = "iso" ]
954                                         then
955                                                 isoloop=$(setup_loop "${dest}/$(basename "${url}")" "loop" "/sys/block/loop*" "" '')
956                                                 mount -t iso9660 "${isoloop}" "${mountpoint}"
957                                                 rc=${?}
958                                         fi
959                                         break
960                                         ;;
961
962                                 *)
963                                         log_begin_msg "Unrecognized archive extension for ${url}"
964                                         ;;
965                         esac
966                 fi
967         done
968
969         if [ ${rc} != 0 ]
970         then
971                 if [ -d "${alt_mountpoint}" ]
972                 then
973                         umount "${alt_mountpoint}"
974                         rmdir "${alt_mountpoint}"
975                 fi
976                 umount "${mountpoint}"
977         elif [ "${webfile}"  != "FETCH" ] ; then
978                 NETBOOT="${webfile}"
979                 export NETBOOT
980         fi
981
982         return ${rc}
983 }
984
985 do_nfsmount ()
986 {
987         rc=1
988
989         modprobe -q nfs
990
991         if [ -z "${NFSOPTS}" ]
992         then
993                 NFSOPTS=""
994         fi
995
996         log_begin_msg "Trying nfsmount -o nolock -o ro ${NFSOPTS} ${NFSROOT} ${mountpoint}"
997
998         # FIXME: This while loop is an ugly HACK round an nfs bug
999         i=0
1000         while [ "$i" -lt 60 ]
1001         do
1002                 nfsmount -o nolock -o ro ${NFSOPTS} "${NFSROOT}" "${mountpoint}" && rc=0 && break
1003                 sleep 1
1004                 i="$(($i + 1))"
1005         done
1006
1007         return ${rc}
1008 }
1009
1010 do_cifsmount ()
1011 {
1012         rc=1
1013
1014         if [ -x "/sbin/mount.cifs" ]
1015         then
1016                 if [ -z "${NFSOPTS}" ]
1017                 then
1018                         CIFSOPTS="-ouser=root,password="
1019                 else
1020                         CIFSOPTS="${NFSOPTS}"
1021                 fi
1022
1023                 log_begin_msg "Trying mount.cifs ${NFSROOT} ${mountpoint} ${CIFSOPTS}"
1024                 modprobe -q cifs
1025
1026                 if mount.cifs "${NFSROOT}" "${mountpoint}" "${CIFSOPTS}"
1027                 then
1028                         rc=0
1029                 fi
1030         fi
1031
1032         return ${rc}
1033 }
1034
1035 do_snap_copy ()
1036 {
1037         fromdev="${1}"
1038         todir="${2}"
1039         snap_type="${3}"
1040         size=$(fs_size "${fromdev}" "" "used")
1041
1042         if [ -b "${fromdev}" ]
1043         then
1044                 log_success_msg "Copying snapshot ${fromdev} to ${todir}..."
1045
1046                 # look for free mem
1047                 if [ -n "${HOMEMOUNTED}" -a "${snap_type}" = "HOME" ]
1048                 then
1049                         todev=$(awk -v pat="$(base_path ${todir})" '$2 == pat { print $1 }' /proc/mounts)
1050                         freespace=$(df -k | awk '/'${todev}'/{print $4}')
1051                 else
1052                         freespace=$(awk '/^MemFree:/{f=$2} /^Cached:/{c=$2} END{print f+c}' /proc/meminfo)
1053                 fi
1054
1055                 tomount="/mnt/tmpsnap"
1056
1057                 if [ ! -d "${tomount}" ]
1058                 then
1059                         mkdir -p "${tomount}"
1060                 fi
1061
1062                 fstype=$(get_fstype "${fromdev}")
1063
1064                 if [ -n "${fstype}" ]
1065                 then
1066                         # Copying stuff...
1067                         mount -o ro -t "${fstype}" "${fromdev}" "${tomount}" || log_warning_msg "Error in mount -t ${fstype} -o ro ${fromdev} ${tomount}"
1068                         cp -a "${tomount}"/* ${todir}
1069                         umount "${tomount}"
1070                 else
1071                         log_warning_msg "Unrecognized fstype: ${fstype} on ${fromdev}:${snap_type}"
1072                 fi
1073
1074                 rmdir "${tomount}"
1075
1076                 if echo ${fromdev} | grep -qs loop
1077                 then
1078                         losetup -d "${fromdev}"
1079                 fi
1080
1081                 return 0
1082         else
1083                 log_warning_msg "Unable to find the snapshot ${snap_type} medium"
1084                 return 1
1085         fi
1086 }
1087
1088 try_snap ()
1089 {
1090         # copy the contents of previously found snapshot to ${snap_mount}
1091         # and remember the device and filename for resync on exit in live-boot.init
1092
1093         snapdata="${1}"
1094         snap_mount="${2}"
1095         snap_type="${3}"
1096         snap_relpath="${4}"
1097
1098         if [ -z "${snap_relpath}" ]
1099         then
1100                 # root snapshot, default usage
1101                 snap_relpath="/"
1102         else
1103                 # relative snapshot (actually used just for "/home" snapshots)
1104                 snap_mount="${2}${snap_relpath}"
1105         fi
1106
1107         if [ -n "${snapdata}" ] && [ ! -b "${snapdata}" ]
1108         then
1109                 log_success_msg "found snapshot: ${snapdata}"
1110                 snapdev="$(echo ${snapdata} | cut -f1 -d ' ')"
1111                 snapback="$(echo ${snapdata} | cut -f2 -d ' ')"
1112                 snapfile="$(echo ${snapdata} | cut -f3 -d ' ')"
1113
1114                 if ! try_mount "${snapdev}" "${snapback}" "ro"
1115                 then
1116                         break
1117                 fi
1118
1119                 RES="0"
1120
1121                 if echo "${snapfile}" | grep -qs '\(squashfs\|ext2\|ext3\|ext4\|jffs2\)'
1122                 then
1123                         # squashfs, jffs2 or ext2/ext3/ext4 snapshot
1124                         dev=$(get_backing_device "${snapback}/${snapfile}")
1125
1126                         do_snap_copy "${dev}" "${snap_mount}" "${snap_type}"
1127                         RES="$?"
1128                 else
1129                         # cpio.gz snapshot
1130
1131                         # Unfortunately klibc's cpio is incompatible with the
1132                         # rest of the world; everything else requires -u -d,
1133                         # while klibc doesn't implement them. Try to detect
1134                         # whether it's in use.
1135                         cpiopath="$(which cpio)" || true
1136                         if [ "$cpiopath" ] && grep -aq /lib/klibc "$cpiopath"
1137                         then
1138                                 cpioargs=
1139                         else
1140                                 cpioargs='--unconditional --make-directories'
1141                         fi
1142
1143                         if [ -s "${snapback}/${snapfile}" ]
1144                         then
1145                                 BEFOREDIR="$(pwd)"
1146                                 cd "${snap_mount}" && zcat "${snapback}/${snapfile}" | $cpiopath $cpioargs --extract --preserve-modification-time --no-absolute-filenames --sparse 2>/dev/null
1147                                 RES="$?"
1148                                 cd "${BEFOREDIR}"
1149                         else
1150                                 log_warning_msg "${snapback}/${snapfile} is empty, adding it for sync on reboot."
1151                                 RES="0"
1152                         fi
1153
1154                         if [ "${RES}" != "0" ]
1155                         then
1156                                 log_warning_msg "failure to \"zcat ${snapback}/${snapfile} | $cpiopath $cpioargs --extract --preserve-modification-time --no-absolute-filenames --sparse\""
1157                         fi
1158                 fi
1159
1160                 umount "${snapback}" ||  log_warning_msg "failure to \"umount ${snapback}\""
1161
1162                 if [ "${RES}" != "0" ]
1163                 then
1164                         log_warning_msg "Impossible to include the ${snapfile} Snapshot file"
1165                 fi
1166
1167         elif [ -b "${snapdata}" ]
1168         then
1169                 # Try to find if it could be a snapshot partition
1170                 dev="${snapdata}"
1171                 log_success_msg "found snapshot ${snap_type} device on ${dev}"
1172                 if echo "${dev}" | grep -qs loop
1173                 then
1174                         # strange things happens, user confused?
1175                         snaploop=$( losetup ${dev} | awk '{print $3}' | tr -d '()' )
1176                         snapfile=$(basename ${snaploop})
1177                         snapdev=$(awk -v pat="$( dirname ${snaploop})" '$2 == pat { print $1 }' /proc/mounts)
1178                 else
1179                         snapdev="${dev}"
1180                 fi
1181
1182                 if ! do_snap_copy "${dev}" "${snap_mount}" "${snap_type}"
1183                 then
1184                         log_warning_msg "Impossible to include the ${snap_type} Snapshot (i)"
1185                         return 1
1186                 else
1187                         if [ -n "${snapfile}" ]
1188                         then
1189                                 # it was a loop device, user confused
1190                                 umount ${snapdev}
1191                         fi
1192                 fi
1193         else
1194                 log_warning_msg "Impossible to include the ${snap_type} Snapshot (o)"
1195                 return 1
1196         fi
1197
1198         if [ -z ${PERSISTENT_READONLY} ]
1199         then
1200                 echo "export ${snap_type}SNAP=${snap_relpath}:${snapdev}:${snapfile}" >> snapshot.conf # for resync on reboot/halt
1201         fi
1202         return 0
1203 }
1204
1205 setup_unionfs ()
1206 {
1207         image_directory="${1}"
1208         rootmnt="${2}"
1209         addimage_directory="${3}"
1210
1211         case ${UNIONTYPE} in
1212                 aufs|unionfs|overlayfs)
1213                         modprobe -q -b ${UNIONTYPE}
1214
1215                         if ! cut -f2 /proc/filesystems | grep -q "^${UNIONTYPE}\$" && [ -x /bin/unionfs-fuse ]
1216                         then
1217                                 echo "${UNIONTYPE} not available, falling back to unionfs-fuse."
1218                                 echo "This might be really slow."
1219
1220                                 UNIONTYPE="unionfs-fuse"
1221                         fi
1222                         ;;
1223         esac
1224
1225         if [ "${UNIONTYPE}" = unionfs-fuse ]
1226         then
1227                 modprobe fuse
1228         fi
1229
1230         # run-init can't deal with images in a subdir, but we're going to
1231         # move all of these away before it runs anyway.  No, we're not,
1232         # put them in / since move-mounting them into / breaks mono and
1233         # some other apps.
1234
1235         croot="/"
1236
1237         # Let's just mount the read-only file systems first
1238         rofslist=""
1239
1240         if [ -z "${PLAIN_ROOT}" ]
1241         then
1242                 # Read image names from ${MODULE}.module if it exists
1243                 if [ -e "${image_directory}/filesystem.${MODULE}.module" ]
1244                 then
1245                         for IMAGE in $(cat ${image_directory}/filesystem.${MODULE}.module)
1246                         do
1247                                 image_string="${image_string} ${image_directory}/${IMAGE}"
1248                         done
1249                 elif [ -e "${image_directory}/${MODULE}.module" ]
1250                 then
1251                         for IMAGE in $(cat ${image_directory}/${MODULE}.module)
1252                         do
1253                                 image_string="${image_string} ${image_directory}/${IMAGE}"
1254                         done
1255                 else
1256                         # ${MODULE}.module does not exist, create a list of images
1257                         for FILESYSTEM in squashfs ext2 ext3 ext4 xfs jffs2 dir
1258                         do
1259                                 for IMAGE in "${image_directory}"/*."${FILESYSTEM}"
1260                                 do
1261                                         if [ -e "${IMAGE}" ]
1262                                         then
1263                                                 image_string="${image_string} ${IMAGE}"
1264                                         fi
1265                                 done
1266                         done
1267
1268                         if [ -n "${addimage_directory}" ] && [ -d "${addimage_directory}" ]
1269                         then
1270                                 for FILESYSTEM in squashfs ext2 ext3 ext4 xfs jffs2 dir
1271                                 do
1272                                         for IMAGE in "${addimage_directory}"/*."${FILESYSTEM}"
1273                                         do
1274                                                 if [ -e "${IMAGE}" ]
1275                                                 then
1276                                                         image_string="${image_string} ${IMAGE}"
1277                                                 fi
1278                                         done
1279                                 done
1280                         fi
1281
1282                         # Now sort the list
1283                         image_string="$(echo ${image_string} | sed -e 's/ /\n/g' | sort )"
1284                 fi
1285
1286                 [ -n "${MODULETORAMFILE}" ] && image_string="${image_directory}/$(basename ${MODULETORAMFILE})"
1287
1288                 mkdir -p "${croot}"
1289
1290                 for image in ${image_string}
1291                 do
1292                         imagename=$(basename "${image}")
1293
1294                         export image devname
1295                         maybe_break live-realpremount
1296                         log_begin_msg "Running /scripts/live-realpremount"
1297                         run_scripts /scripts/live-realpremount
1298                         log_end_msg
1299
1300                         if [ -d "${image}" ]
1301                         then
1302                                 # it is a plain directory: do nothing
1303                                 rofslist="${image} ${rofslist}"
1304                         elif [ -f "${image}" ]
1305                         then
1306                                 if losetup --help 2>&1 | grep -q -- "-r\b"
1307                                 then
1308                                         backdev=$(get_backing_device "${image}" "-r")
1309                                 else
1310                                         backdev=$(get_backing_device "${image}")
1311                                 fi
1312                                 fstype=$(get_fstype "${backdev}")
1313
1314                                 if [ "${fstype}" = "unknown" ]
1315                                 then
1316                                         panic "Unknown file system type on ${backdev} (${image})"
1317                                 fi
1318
1319                                 if [ -z "${fstype}" ]
1320                                 then
1321                                         fstype="${imagename##*.}"
1322                                         log_warning_msg "Unknown file system type on ${backdev} (${image}), assuming ${fstype}."
1323                                 fi
1324
1325                                 if [ "${UNIONTYPE}" != "unionmount" ]
1326                                 then
1327                                         mpoint="${croot}/${imagename}"
1328                                         rofslist="${mpoint} ${rofslist}"
1329                                 else
1330                                         mpoint="${rootmnt}"
1331                                         rofslist="${rootmnt} ${rofslist}"
1332                                 fi
1333                                 mkdir -p "${mpoint}"
1334                                 log_begin_msg "Mounting \"${image}\" on \"${mpoint}\" via \"${backdev}\""
1335                                 mount -t "${fstype}" -o ro,noatime "${backdev}" "${mpoint}" || panic "Can not mount ${backdev} (${image}) on ${mpoint}"
1336                                 log_end_msg
1337                         fi
1338                 done
1339         else
1340                 # we have a plain root system
1341                 mkdir -p "${croot}/filesystem"
1342                 log_begin_msg "Mounting \"${image_directory}\" on \"${croot}/filesystem\""
1343                 mount -t $(get_fstype "${image_directory}") -o ro,noatime "${image_directory}" "${croot}/filesystem" || \
1344                         panic "Can not mount ${image_directory} on ${croot}/filesystem" && \
1345                         rofslist="${croot}/filesystem ${rofslist}"
1346                 # probably broken:
1347                 mount -o bind ${croot}/filesystem $mountpoint
1348                 log_end_msg
1349         fi
1350
1351         mkdir -p /cow
1352
1353         # Looking for persistent devices or files
1354         if [ -n "${PERSISTENT}" ] && [ -z "${NOPERSISTENT}" ]
1355         then
1356
1357                 if [ -z "${QUICKUSBMODULES}" ]
1358                 then
1359                         # Load USB modules
1360                         num_block=$(ls -l /sys/block | wc -l)
1361                         for module in sd_mod uhci-hcd ehci-hcd ohci-hcd usb-storage
1362                         do
1363                                 modprobe -q -b ${module}
1364                         done
1365
1366                         udevadm trigger
1367                         udevadm settle
1368
1369                         # For some reason, udevsettle does not block in this scenario,
1370                         # so we sleep for a little while.
1371                         #
1372                         # See https://bugs.launchpad.net/ubuntu/+source/casper/+bug/84591
1373                         for timeout in 5 4 3 2 1
1374                         do
1375                                 sleep 1
1376
1377                                 if [ $(ls -l /sys/block | wc -l) -gt ${num_block} ]
1378                                 then
1379                                         break
1380                                 fi
1381                         done
1382                 fi
1383
1384                 case "${PERSISTENT_MEDIA}" in
1385                         removable)
1386                                 whitelistdev="$(removable_dev)"
1387                                 ;;
1388                         removable-usb)
1389                                 whitelistdev="$(removable_usb_dev)"
1390                                 ;;
1391                         *)
1392                                 whitelistdev=""
1393                                 ;;
1394                 esac
1395
1396                 if echo ${PERSISTENT_METHOD} | grep -qe "\<overlay\>"
1397                 then
1398                         overlays="${root_overlay_label} ${custom_overlay_label}"
1399                 fi
1400
1401                 if echo ${PERSISTENT_METHOD} | grep -qe "\<snapshot\>"
1402                 then
1403                         snapshots="${root_snapshot_label} ${home_snapshot_label}"
1404                 fi
1405
1406                 overlay_devices=""
1407                 for media in $(find_persistent_media "${overlays}" "${snapshots}" "${blacklistdev}" "${whitelistdev}")
1408                 do
1409                         media="$(echo ${media} | tr ":" " ")"
1410                         case ${media} in
1411                                 ${root_snapshot_label}=*)
1412                                         root_snapdata="${media#*=}"
1413                                         ;;
1414                                 ${home_snapshot_label}=*)
1415                                         # This second type should be removed when snapshot will get smarter,
1416                                         # hence when "/etc/live-snapshot*list" will be supported also by
1417                                         # ext2|ext3|ext4|jffs2 snapshot types.
1418                                         home_snapdata="${media#*=}"
1419                                         ;;
1420                                 ${root_overlay_label}=*)
1421                                         device="${media#*=}"
1422                                         root_overlay_device="${device}"
1423                                         ;;
1424                                 ${custom_overlay_label}=*)
1425                                         device="${media#*=}"
1426                                         overlay_devices="${overlay_devices} ${device}"
1427                                         ;;
1428                          esac
1429                 done
1430
1431                 if [ -b "${root_overlay_device}" ]
1432                 then
1433                         PERSISTENCE_IS_ON="1"
1434                         export PERSISTENCE_IS_ON
1435
1436                         cowdevice=${root_overlay_device}
1437                         cow_fstype=$(get_fstype "${root_overlay_device}")
1438                         if [ -z "${PERSISTENT_READONLY}" ]
1439                         then
1440                                 cow_mountopt="rw,noatime"
1441                         else
1442                                 cow_mountopt="ro,noatime"
1443                         fi
1444
1445                         if [ "${FORCEPERSISTENTFSCK}" = "Yes" ]
1446                         then
1447                                 fsck -y ${cowdevice}
1448                         fi
1449                 fi
1450         elif [ -n "${NFS_COW}" ] && [ -z "${NOPERSISTENT}" ]
1451         then
1452                 # check if there are any nfs options
1453                 if echo ${NFS_COW}|grep -q ','
1454                 then
1455                         nfs_cow_opts="-o nolock,$(echo ${NFS_COW}|cut -d, -f2-)"
1456                         nfs_cow=$(echo ${NFS_COW}|cut -d, -f1)
1457                 else
1458                         nfs_cow_opts="-o nolock"
1459                         nfs_cow=${NFS_COW}
1460                 fi
1461
1462                 if [ -n "${PERSISTENT_READONLY}" ]
1463                 then
1464                         nfs_cow_opts="${nfs_cow_opts},nocto,ro"
1465                 fi
1466
1467                 mac="$(get_mac)"
1468                 if [ -n "${mac}" ]
1469                 then
1470                         cowdevice=$(echo ${nfs_cow}|sed "s/client_mac_address/${mac}/")
1471                         cow_fstype="nfs"
1472                 else
1473                         panic "unable to determine mac address"
1474                 fi
1475         fi
1476
1477         if [ -z "${cowdevice}" ]
1478         then
1479                 cowdevice="tmpfs"
1480                 cow_fstype="tmpfs"
1481                 cow_mountopt="rw,noatime,mode=755"
1482         fi
1483
1484         if [ "${UNIONTYPE}" != "unionmount" ]
1485         then
1486                 if [ -n "${PERSISTENT_READONLY}" ]
1487                 then
1488                         mount -t tmpfs -o rw,noatime,mode=755 tmpfs "/cow"
1489                         root_backing="${rootmnt}/live/persistent/$(basename ${cowdevice})-root"
1490                         mkdir -p ${root_backing}
1491                 else
1492                         root_backing="/cow"
1493                 fi
1494
1495                 if [ "${cow_fstype}" = "nfs" ]
1496                 then
1497                         log_begin_msg \
1498                                 "Trying nfsmount ${nfs_cow_opts} ${cowdevice} ${root_backing}"
1499                         nfsmount ${nfs_cow_opts} ${cowdevice} ${root_backing} || \
1500                                 panic "Can not mount ${cowdevice} (n: ${cow_fstype}) on ${root_backing}"
1501                 else
1502                         mount -t ${cow_fstype} -o ${cow_mountopt} ${cowdevice} ${root_backing} || \
1503                                 panic "Can not mount ${cowdevice} (o: ${cow_fstype}) on ${root_backing}"
1504                 fi
1505         fi
1506
1507         rofscount=$(echo ${rofslist} |wc -w)
1508
1509         # XXX: we now ensure that there can only be one read-only filesystem. Should this be inside the EXPOSED_ROOT if?
1510         if [ ${rofscount} -ne 1 ]
1511         then
1512                 panic "only one RO file system supported with exposedroot: ${rofslist}"
1513         fi
1514         rofs=${rofslist%% }
1515
1516         if [ -n "${EXPOSED_ROOT}" ]
1517         then
1518                 mount --bind ${rofs} ${rootmnt} || \
1519                         panic "bind mount of ${rofs} failed"
1520
1521                 if [ -z "${SKIP_UNION_MOUNTS}" ]
1522                 then
1523                         cow_dirs='/var/tmp /var/lock /var/run /var/log /var/spool /home /var/lib/live'
1524                 else
1525                         cow_dirs=''
1526                 fi
1527         else
1528                 cow_dirs="/"
1529         fi
1530
1531         if [ "${cow_fstype}" != "tmpfs" ] && [ "${cow_dirs}" != "/" ] && [ "${UNIONTYPE}" = "unionmount" ]
1532         then
1533                 true # FIXME: Maybe it does, I don't really know.
1534                 #panic "unionmount does not support subunions (${cow_dirs})."
1535         fi
1536
1537         for dir in ${cow_dirs}; do
1538                 unionmountpoint="${rootmnt}${dir}"
1539                 mkdir -p ${unionmountpoint}
1540                 if [ "${UNIONTYPE}" = "unionmount" ]
1541                 then
1542                         # FIXME: handle PERSISTENT_READONLY
1543                         unionmountopts="-t ${cow_fstype} -o noatime,union,${cow_mountopt} ${cowdevice}"
1544                         mount_full $unionmountopts "${unionmountpoint}"
1545                 else
1546                         cow_dir="/cow${dir}"
1547                         rofs_dir="${rofs}${dir}"
1548                         mkdir -p ${cow_dir}
1549                         if [ -n "${PERSISTENT_READONLY}" ] && [ "${cowdevice}" != "tmpfs" ]
1550                         then
1551                                 #mount -t tmpfs -o rw,noatime,mode=755 tmpfs "${cow_dir}"
1552                                 do_union ${unionmountpoint} ${cow_dir} ${root_backing} ${rofs_dir}
1553                         else
1554                                 do_union ${unionmountpoint} ${cow_dir} ${rofs_dir}
1555                         fi
1556                 fi || panic "mount ${UNIONTYPE} on ${unionmountpoint} failed with option ${unionmountopts}"
1557         done
1558
1559         # Correct the permissions of /:
1560         chmod 0755 "${rootmnt}"
1561
1562         # tmpfs file systems
1563         touch /etc/fstab
1564         mkdir -p "${rootmnt}/live"
1565         mount -t tmpfs tmpfs ${rootmnt}/live
1566
1567         # Adding other custom mounts
1568         if [ -n "${PERSISTENT}" ] && [ -z "${NOPERSISTENT}" ]
1569         then
1570                 bindings="/bindings.list"
1571                 links="/links.list"
1572                 custom_mounts="/custom_mounts.list"
1573                 rm -f ${bindings} ${links} ${custom_mounts} >/dev/null 2>&1
1574                 persistent_backing="${rootmnt}/live/persistent"
1575
1576                 # First we scan all media and gather all information about custom mounts
1577                 for device in ${overlay_devices}
1578                 do
1579                         if [ ! -b "${device}" ]
1580                         then
1581                                 continue
1582                         fi
1583                         device_name="$(basename ${device})"
1584                         backing="${persistent_backing}/${device_name}"
1585                         mkdir -p "${backing}"
1586                         device_fstype="$(get_fstype ${device})"
1587                         if [ -z "${PERSISTENT_READONLY}" ]
1588                         then
1589                                 device_mount_opts="rw,noatime"
1590                         else
1591                                 device_mount_opts="ro,noatime"
1592                         fi
1593                         device_used=""
1594                         mount -t "${device_fstype}" -o "${device_mount_opts}" "${device}" "${backing}"
1595                         include_list="${backing}/${persistence_list}"
1596                         if [ ! -r "${include_list}" ]
1597                         then
1598                                 umount "${backing}"
1599                                 rmdir "${backing}"
1600                                 continue
1601                         fi
1602
1603                         [ "${DEBUG}" = "Yes" ] && cp ${include_list} ${persistent_backing}/${persistence_list}.${device_name}
1604                         while read source dest options # < ${include_list}
1605                         do
1606                                 if echo ${source} | grep -qe "^[[:space:]]*#"
1607                                 then
1608                                         # skipping commented line
1609                                         continue
1610                                 fi
1611
1612                                 if echo ${dest} | grep -qe "^[^/]"
1613                                 then
1614                                         options="${dest}"
1615                                         dest="${source}"
1616                                 elif [ -z "${dest}" ]
1617                                 then
1618                                         dest="${source}"
1619                                 fi
1620
1621                                 if echo ${dest} | grep -qe "^/\+$\|^/\+live\(/.*\)\?$"
1622                                 then
1623                                         # mounting on / or /live could cause trouble
1624                                         log_warning_msg "Skipping unsafe custom mount on ${dest}"
1625                                         continue
1626                                 fi
1627
1628                                 for opt in $(echo ${options} | tr ',' ' ');
1629                                 do
1630                                         case "${opt}" in
1631                                                 "linkfiles")
1632                                                         ;;
1633                                                 *)
1634                                                         log_warning_msg "Skipping custom mount with unkown option: ${opt}"
1635                                                         continue 2
1636                                                         ;;
1637                                         esac
1638                                 done
1639
1640                                 # FIXME: handle case: we already have /a/b in $bindings added from current $device, but now we find /a -- /a should replace /a/b in $bindings.
1641                                 # FIXME: handle case: we have /a in $bindings from current $device, now we find /a/b, so we skip /a/b
1642
1643                                 # ensure that no multiple-/ occur in paths
1644                                 full_source="$(echo ${backing}/${source}/ | sed -e 's|/\+|/|g')"
1645                                 full_dest="$(echo ${rootmnt}/${dest}/ | sed -e 's|/\+|/|g')"
1646                                 device_used="yes"
1647                                 if echo ${options} | grep -qe "\<linkfiles\>";
1648                                 then
1649                                         echo "${full_source} ${full_dest} ${options}" >> ${links}
1650                                 else
1651                                         echo "${full_source} ${full_dest} ${options}" >> ${bindings}
1652                                 fi
1653                         done < ${include_list}
1654
1655                         if [ -z "${device_used}" ]
1656                         then
1657                                 # this device was not used for / earlier, or custom mount point now, so it's useless
1658                                 umount "${backing}"
1659                                 rmdir "${backing}"
1660                         fi
1661                 done
1662
1663                 # We sort the list according to destination so we're sure 
1664                 # that we won't hide a previous mount. We also  ignore
1665                 # duplicate destinations in a more or less arbitrary way.
1666                 [ -e "${bindings}" ] && sort -k2 -sbu ${bindings} >> ${custom_mounts}
1667                 rm -f ${bindings}
1668
1669                 # After all mounts are considered we add symlinks so they
1670                 # won't be hidden by some mount.
1671                 [ -e "${links}" ] && sort -k2 -sbu ${links} >> ${custom_mounts}
1672                 rm -f ${links}
1673
1674                 [ "${DEBUG}" = "Yes" ] && cp ${custom_mounts} ${persistent_backing}
1675
1676                 # Now we do the actual mounting (and symlinking)
1677                 while read source dest options  # < ${custom_mounts}
1678                 do
1679                         local opt_linkfiles=""
1680                         for opt in $(echo ${options} | tr ',' ' ');
1681                         do
1682                                  case "${opt}" in
1683                                         "linkfiles")
1684                                                 opt_linkfiles="yes"
1685                                                 ;;
1686                                 esac
1687                         done
1688
1689                         if mountpoint -q "${dest}";
1690                         then
1691                                 log_warning_msg "Skipping custom mount ${source} on ${dest}: destination is already a mount point"
1692                                 continue
1693                         fi
1694
1695                         # FIXME: we don't handle already existing non-directory files in the paths of both $source and $dest.
1696
1697                         if [ ! -d "${dest}" ]
1698                         then
1699                                 # if ${dest} is in /home/$user, try fixing proper ownership
1700                                 # FIXME: this should really be handled by live-config since we don't know for sure which uid a certain user has until then
1701                                 if echo ${dest} | grep -qe "^${rootmnt}/*home/\+[^/]\+"
1702                                 then
1703                                         path="/"
1704                                         for dir in $(echo ${dest} | sed -e 's|/\+| |g')
1705                                         do
1706                                                 path=${path}/${dir}
1707                                                 if [ ! -e ${path} ]
1708                                                 then
1709                                                         mkdir -p ${path}
1710                                                         # assume that the intended user is the first, which is usually the case
1711                                                         chown 1000:1000 ${path}
1712                                                 fi
1713                                         done
1714                                 else
1715                                         mkdir -p ${dest}
1716                                 fi
1717                         fi
1718
1719                         # FIXME: could we instead only save the aufs-diff in the persistent media? implications? What about when there's changes in the live image?
1720
1721                         # if ${source} doesn't exist on our persistent media we
1722                         # bootstrap it with $dest from the live filesystem.
1723                         # this both makes sense and is critical if we're
1724                         # dealing with /etc or other system dir.
1725                         if [ ! -d "${source}" ]
1726                         then
1727                                 if [ -n "${PERSISTENT_READONLY}" ] || [ -n "${opt_linkfiles}" ]
1728                                 then
1729                                         continue
1730                                 fi
1731                                 # ensure that $dest is not copied *into* $source
1732                                 mkdir -p "$(dirname ${source})"
1733                                 cp -a "${dest}" "${source}"
1734                         fi
1735
1736                         if [ -z "${PERSISTENT_READONLY}" ]
1737                         then
1738                                 if [ -n "${opt_linkfiles}" ]
1739                                 then
1740                                         links_source="${source}"
1741                                         links_dest="${dest}"
1742                                 else
1743                                         mount --bind "${source}" "${dest}"
1744                                 fi
1745                         else
1746                                 if [ -n "${opt_linkfiles}" ]
1747                                 then
1748                                         links_dest="${dest}"
1749                                         dest="$(mktemp -d ${persistent_backing}/links_source-XXXXXX)"
1750                                         links_source="${dest}"
1751                                 fi
1752                                 cow_dir="$(echo ${dest} | sed -e "s|${rootmnt}|/cow/|")"
1753                                 mkdir -p ${cow_dir}
1754                                 # XXX: broken! will be fixed in the next few commits...
1755                                 do_union ${dest} ${cow_dir} ${source}
1756                         fi
1757
1758                         if [ -n "${opt_linkfiles}" ]
1759                         then
1760                                 link_files "${links_source}" "${links_dest}" "${rootmnt}"
1761                         fi
1762
1763                         PERSISTENCE_IS_ON="1"
1764                         export PERSISTENCE_IS_ON
1765                 done < ${custom_mounts}
1766                 rm -f ${bindings}
1767
1768                 # Look for other snapshots to copy in
1769                 try_snap "${root_snapdata}" "${rootmnt}" "ROOT"
1770                 # This second type should be removed when snapshot grow smarter
1771                 try_snap "${home_snapdata}" "${rootmnt}" "HOME" "/home"
1772         fi
1773
1774         if [ -n "${SHOWMOUNTS}" ]
1775         then
1776                 for d in ${rofslist}
1777                 do
1778                         mkdir -p "${rootmnt}/live/${d##*/}"
1779
1780                         case d in
1781                                 *.dir)
1782                                         # do nothing # mount -o bind "${d}" "${rootmnt}/live/${d##*/}"
1783                                         ;;
1784
1785                                 *)
1786                                         case "${UNIONTYPE}" in
1787                                                 unionfs-fuse)
1788                                                         mount -o bind "${d}" "${rootmnt}/live/${d##*/}"
1789                                                         ;;
1790
1791                                                 *)
1792                                                         mount -o move "${d}" "${rootmnt}/live/${d##*/}"
1793                                                         ;;
1794                                         esac
1795                                         ;;
1796                         esac
1797                 done
1798         fi
1799
1800         # shows cow fs on /cow for use by live-snapshot
1801         mkdir -p "${rootmnt}/live/cow"
1802         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"
1803 }
1804
1805 check_dev ()
1806 {
1807         sysdev="${1}"
1808         devname="${2}"
1809         skip_uuid_check="${3}"
1810
1811         # support for fromiso=.../isofrom=....
1812         if [ -n "$FROMISO" ]
1813         then
1814                 ISO_DEVICE=$(dirname $FROMISO)
1815                 if ! [ -b $ISO_DEVICE ]
1816                 then
1817                         # to support unusual device names like /dev/cciss/c0d0p1
1818                         # as well we have to identify the block device name, let's
1819                         # do that for up to 15 levels
1820                         i=15
1821                         while [ -n "$ISO_DEVICE" ] && [ "$i" -gt 0 ]
1822                         do
1823                                 ISO_DEVICE=$(dirname ${ISO_DEVICE})
1824                                 [ -b "$ISO_DEVICE" ] && break
1825                                 i=$(($i -1))
1826                         done
1827                 fi
1828
1829                 if [ "$ISO_DEVICE" = "/" ]
1830                 then
1831                         echo "Warning: device for bootoption isofrom= ($FROMISO) not found.">>/live-boot.log
1832                 else
1833                         fs_type=$(get_fstype "${ISO_DEVICE}")
1834                         if is_supported_fs ${fs_type}
1835                         then
1836                                 mkdir /isofrom
1837                                 mount -t $fs_type "$ISO_DEVICE" /isofrom
1838                                 ISO_NAME="$(echo $FROMISO | sed "s|$ISO_DEVICE||")"
1839                                 loopdevname=$(setup_loop "/isofrom/${ISO_NAME}" "loop" "/sys/block/loop*" "" '')
1840                                 devname="${loopdevname}"
1841                         else
1842                                 echo "Warning: unable to mount $ISO_DEVICE." >>/live-boot.log
1843                         fi
1844                 fi
1845         fi
1846
1847         if [ -z "${devname}" ]
1848         then
1849                 devname=$(sys2dev "${sysdev}")
1850         fi
1851
1852         if [ -d "${devname}" ]
1853         then
1854                 mount -o bind "${devname}" $mountpoint || continue
1855
1856                 if is_live_path $mountpoint
1857                 then
1858                         echo $mountpoint
1859                         return 0
1860                 else
1861                         umount $mountpoint
1862                 fi
1863         fi
1864
1865         IFS=","
1866         for device in ${devname}
1867         do
1868                 case "$device" in
1869                         *mapper*)
1870                                 # Adding lvm support
1871                                 if [ -x /scripts/local-top/lvm2 ]
1872                                 then
1873                                         ROOT="$device" resume="" /scripts/local-top/lvm2
1874                                 fi
1875                                 ;;
1876
1877                         /dev/md*)
1878                                 # Adding raid support
1879                                 if [ -x /scripts/local-top/mdadm ]
1880                                 then
1881                                         cp /conf/conf.d/md /conf/conf.d/md.orig
1882                                         echo "MD_DEVS=$device " >> /conf/conf.d/md
1883                                         /scripts/local-top/mdadm
1884                                         mv /conf/conf.d/md.orig /conf/conf.d/md
1885                                 fi
1886                                 ;;
1887                 esac
1888         done
1889         unset IFS
1890
1891         [ -n "$device" ] && devname="$device"
1892
1893         [ -e "$devname" ] || continue
1894
1895         if [ -n "${LIVE_MEDIA_OFFSET}" ]
1896         then
1897                 loopdevname=$(setup_loop "${devname}" "loop" "/sys/block/loop*" "${LIVE_MEDIA_OFFSET}" '')
1898                 devname="${loopdevname}"
1899         fi
1900
1901         fstype=$(get_fstype "${devname}")
1902
1903         if is_supported_fs ${fstype}
1904         then
1905                 devuid=$(blkid -o value -s UUID "$devname")
1906                 [ -n "$devuid" ] && grep -qs "\<$devuid\>" $tried && continue
1907                 mount -t ${fstype} -o ro,noatime "${devname}" ${mountpoint} || continue
1908                 [ -n "$devuid" ] && echo "$devuid" >> $tried
1909
1910                 if is_live_path ${mountpoint} && \
1911                         ([ "${skip_uuid_check}" ] || matches_uuid ${mountpoint})
1912                 then
1913                         echo ${mountpoint}
1914                         return 0
1915                 else
1916                         umount ${mountpoint} 2>/dev/null
1917                 fi
1918         fi
1919
1920         if [ -n "${LIVE_MEDIA_OFFSET}" ]
1921         then
1922                 losetup -d "${loopdevname}"
1923         fi
1924
1925         return 1
1926 }
1927
1928 find_livefs ()
1929 {
1930         timeout="${1}"
1931
1932         # don't start autodetection before timeout has expired
1933         if [ -n "${LIVE_MEDIA_TIMEOUT}" ]
1934         then
1935                 if [ "${timeout}" -lt "${LIVE_MEDIA_TIMEOUT}" ]
1936                 then
1937                         return 1
1938                 fi
1939         fi
1940
1941         # first look at the one specified in the command line
1942         case "${LIVE_MEDIA}" in
1943                 removable-usb)
1944                         for sysblock in $(removable_usb_dev "sys")
1945                         do
1946                                 for dev in $(subdevices "${sysblock}")
1947                                 do
1948                                         if check_dev "${dev}"
1949                                         then
1950                                                 return 0
1951                                         fi
1952                                 done
1953                         done
1954                         return 1
1955                         ;;
1956
1957                 removable)
1958                         for sysblock in $(removable_dev "sys")
1959                         do
1960                                 for dev in $(subdevices "${sysblock}")
1961                                 do
1962                                         if check_dev "${dev}"
1963                                         then
1964                                                 return 0
1965                                         fi
1966                                 done
1967                         done
1968                         return 1
1969                         ;;
1970
1971                 *)
1972                         if [ ! -z "${LIVE_MEDIA}" ]
1973                         then
1974                                 if check_dev "null" "${LIVE_MEDIA}" "skip_uuid_check"
1975                                 then
1976                                         return 0
1977                                 fi
1978                         fi
1979                         ;;
1980         esac
1981
1982         # or do the scan of block devices
1983         # prefer removable devices over non-removable devices, so scan them first
1984         devices_to_scan="$(removable_dev 'sys') $(non_removable_dev 'sys')"
1985
1986         for sysblock in $devices_to_scan
1987         do
1988                 devname=$(sys2dev "${sysblock}")
1989                 [ -e "$devname" ] || continue
1990                 fstype=$(get_fstype "${devname}")
1991
1992                 if /lib/udev/cdrom_id ${devname} > /dev/null
1993                 then
1994                         if check_dev "null" "${devname}"
1995                         then
1996                                 return 0
1997                         fi
1998                 elif is_nice_device "${sysblock}"
1999                 then
2000                         for dev in $(subdevices "${sysblock}")
2001                         do
2002                                 if check_dev "${dev}"
2003                                 then
2004                                         return 0
2005                                 fi
2006                         done
2007                 elif [ "${fstype}" = "squashfs" -o \
2008                         "${fstype}" = "btrfs" -o \
2009                         "${fstype}" = "ext2" -o \
2010                         "${fstype}" = "ext3" -o \
2011                         "${fstype}" = "ext4" -o \
2012                         "${fstype}" = "jffs2" ]
2013                 then
2014                         # This is an ugly hack situation, the block device has
2015                         # an image directly on it.  It's hopefully
2016                         # live-boot, so take it and run with it.
2017                         ln -s "${devname}" "${devname}.${fstype}"
2018                         echo "${devname}.${fstype}"
2019                         return 0
2020                 fi
2021         done
2022
2023         return 1
2024 }
2025
2026 integrity_check ()
2027 {
2028         media_mountpoint="${1}"
2029
2030         log_begin_msg "Checking media integrity"
2031
2032         cd ${media_mountpoint}
2033         /bin/md5sum -c md5sum.txt < /dev/tty8 > /dev/tty8
2034         RC="${?}"
2035
2036         log_end_msg
2037
2038         if [ "${RC}" -eq 0 ]
2039         then
2040                 log_success_msg "Everything ok, will reboot in 10 seconds."
2041                 sleep 10
2042                 cd /
2043                 umount ${media_mountpoint}
2044                 sync
2045                 echo u > /proc/sysrq-trigger
2046                 echo b > /proc/sysrq-trigger
2047         else
2048                 panic "Not ok, a media defect is likely, switch to VT8 for details."
2049         fi
2050 }
2051
2052 mountroot ()
2053 {
2054         if [ -x /scripts/local-top/cryptroot ]; then
2055             /scripts/local-top/cryptroot
2056         fi
2057
2058         exec 6>&1
2059         exec 7>&2
2060         exec > live-boot.log
2061         exec 2>&1
2062         tail -f live-boot.log >&7 &
2063         tailpid="${!}"
2064
2065         # Ensure 'panic' function is overridden
2066         . /scripts/live-functions
2067
2068         Arguments
2069
2070         maybe_break live-premount
2071         log_begin_msg "Running /scripts/live-premount"
2072         run_scripts /scripts/live-premount
2073         log_end_msg
2074
2075         # Needed here too because some things (*cough* udev *cough*)
2076         # changes the timeout
2077
2078         if [ ! -z "${NETBOOT}" ] || [ ! -z "${FETCH}" ] || [ ! -z "${HTTPFS}" ] || [ ! -z "${FTPFS}" ]
2079         then
2080                 if do_netmount
2081                 then
2082                         livefs_root="${mountpoint}"
2083                 else
2084                         panic "Unable to find a live file system on the network"
2085                 fi
2086         else
2087                 if [ -n "${ISCSI_PORTAL}" ]
2088                 then
2089                         do_iscsi && livefs_root="${mountpoint}"
2090                 elif [ -n "${PLAIN_ROOT}" ] && [ -n "${ROOT}" ]
2091                 then
2092                         # Do a local boot from hd
2093                         livefs_root=${ROOT}
2094                 else
2095                         if [ -x /usr/bin/memdiskfind ]
2096                         then
2097                                 MEMDISK=$(/usr/bin/memdiskfind)
2098
2099                                 if [ $? -eq 0 ]
2100                                 then
2101                                         # We found a memdisk, set up phram
2102                                         modprobe phram phram=memdisk,${MEMDISK}
2103
2104                                         # Load mtdblock, the memdisk will be /dev/mtdblock0
2105                                         modprobe mtdblock
2106                                 fi
2107                         fi
2108
2109                         # Scan local devices for the image
2110                         i=0
2111                         while [ "$i" -lt 60 ]
2112                         do
2113                                 livefs_root=$(find_livefs ${i})
2114
2115                                 if [ -n "${livefs_root}" ]
2116                                 then
2117                                         break
2118                                 fi
2119
2120                                 sleep 1
2121                                 i="$(($i + 1))"
2122                         done
2123                 fi
2124         fi
2125
2126         if [ -z "${livefs_root}" ]
2127         then
2128                 panic "Unable to find a medium containing a live file system"
2129         fi
2130
2131         if [ "${INTEGRITY_CHECK}" ]
2132         then
2133                 integrity_check "${livefs_root}"
2134         fi
2135
2136         if [ "${TORAM}" ]
2137         then
2138                 live_dest="ram"
2139         elif [ "${TODISK}" ]
2140         then
2141                 live_dest="${TODISK}"
2142         fi
2143
2144         if [ "${live_dest}" ]
2145         then
2146                 log_begin_msg "Copying live media to ${live_dest}"
2147                 copy_live_to "${livefs_root}" "${live_dest}"
2148                 log_end_msg
2149         fi
2150
2151         # if we do not unmount the ISO we can't run "fsck /dev/ice" later on
2152         # because the mountpoint is left behind in /proc/mounts, so let's get
2153         # rid of it when running from RAM
2154         if [ -n "$FROMISO" ] && [ "${TORAM}" ]
2155         then
2156           losetup -d /dev/loop0
2157           grep -q /isofrom /proc/mounts && umount /isofrom
2158         fi
2159
2160         if [ -n "${MODULETORAMFILE}" ] || [ -n "${PLAIN_ROOT}" ]
2161         then
2162                 setup_unionfs "${livefs_root}" "${rootmnt}"
2163         else
2164                 mac="$(get_mac)"
2165                 mac="$(echo ${mac} | sed 's/-//g')"
2166                 mount_images_in_directory "${livefs_root}" "${rootmnt}" "${mac}"
2167         fi
2168
2169
2170         if [ -n "${ROOT_PID}" ] ; then
2171                 echo "${ROOT_PID}" > "${rootmnt}"/live/root.pid
2172         fi
2173
2174         log_end_msg
2175
2176         # unionfs-fuse needs /dev to be bind-mounted for the duration of
2177         # live-bottom; udev's init script will take care of things after that
2178         if [ "${UNIONTYPE}" = unionfs-fuse ]
2179         then
2180                 mount -n -o bind /dev "${rootmnt}/dev"
2181         fi
2182
2183         # Move to the new root filesystem so that programs there can get at it.
2184         if [ ! -d /root/live/image ]
2185         then
2186                 mkdir -p /root/live/image
2187                 mount --move /live/image /root/live/image
2188         fi
2189
2190         # aufs2 in kernel versions around 2.6.33 has a regression:
2191         # directories can't be accessed when read for the first the time,
2192         # causing a failure for example when accessing /var/lib/fai
2193         # when booting FAI, this simple workaround solves it
2194         ls /root/* >/dev/null 2>&1
2195
2196         # copy snapshot configuration if exists
2197         if [ -f snapshot.conf ]
2198         then
2199                 log_begin_msg "Copying snapshot.conf to ${rootmnt}/etc/live/boot.d"
2200                 if [ ! -d "${rootmnt}/etc/live/boot.d" ]
2201                 then
2202                         mkdir -p "${rootmnt}/etc/live/boot.d"
2203                 fi
2204                 cp snapshot.conf "${rootmnt}/etc/live/boot.d/"
2205                 log_end_msg
2206         fi
2207
2208         if [ -f /etc/resolv.conf ] && [ ! -s ${rootmnt}/etc/resolv.conf ]
2209         then
2210                 log_begin_msg "Copying /etc/resolv.conf to ${rootmnt}/etc/resolv.conf"
2211                 cp -v /etc/resolv.conf ${rootmnt}/etc/resolv.conf
2212                 log_end_msg
2213         fi
2214
2215         maybe_break live-bottom
2216         log_begin_msg "Running /scripts/live-bottom\n"
2217
2218         run_scripts /scripts/live-bottom
2219         log_end_msg
2220
2221         if [ "${UNIONFS}" = unionfs-fuse ]
2222         then
2223                 umount "${rootmnt}/dev"
2224         fi
2225
2226         exec 1>&6 6>&-
2227         exec 2>&7 7>&-
2228         kill ${tailpid}
2229         [ -w "${rootmnt}/var/log/" ] && cp live-boot.log "${rootmnt}/var/log/" 2>/dev/null
2230 }