9eaa073c5b8a093ac655410e6c98dfa790b1f588
[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 [ "${UNIONTYPE}" = "aufs" ]
1241         then
1242                 roopt="rr+wh"
1243                 noxino_opt="noxino,"
1244         elif [ "${UNIONTYPE}" = "unionfs-fuse" ]
1245         then
1246                 roopt="RO"
1247         else
1248                 roopt="ro"
1249         fi
1250
1251         if [ -z "${PLAIN_ROOT}" ]
1252         then
1253                 # Read image names from ${MODULE}.module if it exists
1254                 if [ -e "${image_directory}/filesystem.${MODULE}.module" ]
1255                 then
1256                         for IMAGE in $(cat ${image_directory}/filesystem.${MODULE}.module)
1257                         do
1258                                 image_string="${image_string} ${image_directory}/${IMAGE}"
1259                         done
1260                 elif [ -e "${image_directory}/${MODULE}.module" ]
1261                 then
1262                         for IMAGE in $(cat ${image_directory}/${MODULE}.module)
1263                         do
1264                                 image_string="${image_string} ${image_directory}/${IMAGE}"
1265                         done
1266                 else
1267                         # ${MODULE}.module does not exist, create a list of images
1268                         for FILESYSTEM in squashfs ext2 ext3 ext4 xfs jffs2 dir
1269                         do
1270                                 for IMAGE in "${image_directory}"/*."${FILESYSTEM}"
1271                                 do
1272                                         if [ -e "${IMAGE}" ]
1273                                         then
1274                                                 image_string="${image_string} ${IMAGE}"
1275                                         fi
1276                                 done
1277                         done
1278
1279                         if [ -n "${addimage_directory}" ] && [ -d "${addimage_directory}" ]
1280                         then
1281                                 for FILESYSTEM in squashfs ext2 ext3 ext4 xfs jffs2 dir
1282                                 do
1283                                         for IMAGE in "${addimage_directory}"/*."${FILESYSTEM}"
1284                                         do
1285                                                 if [ -e "${IMAGE}" ]
1286                                                 then
1287                                                         image_string="${image_string} ${IMAGE}"
1288                                                 fi
1289                                         done
1290                                 done
1291                         fi
1292
1293                         # Now sort the list
1294                         image_string="$(echo ${image_string} | sed -e 's/ /\n/g' | sort )"
1295                 fi
1296
1297                 [ -n "${MODULETORAMFILE}" ] && image_string="${image_directory}/$(basename ${MODULETORAMFILE})"
1298
1299                 mkdir -p "${croot}"
1300
1301                 for image in ${image_string}
1302                 do
1303                         imagename=$(basename "${image}")
1304
1305                         export image devname
1306                         maybe_break live-realpremount
1307                         log_begin_msg "Running /scripts/live-realpremount"
1308                         run_scripts /scripts/live-realpremount
1309                         log_end_msg
1310
1311                         if [ -d "${image}" ]
1312                         then
1313                                 # it is a plain directory: do nothing
1314                                 rofslist="${image} ${rofslist}"
1315                         elif [ -f "${image}" ]
1316                         then
1317                                 if losetup --help 2>&1 | grep -q -- "-r\b"
1318                                 then
1319                                         backdev=$(get_backing_device "${image}" "-r")
1320                                 else
1321                                         backdev=$(get_backing_device "${image}")
1322                                 fi
1323                                 fstype=$(get_fstype "${backdev}")
1324
1325                                 if [ "${fstype}" = "unknown" ]
1326                                 then
1327                                         panic "Unknown file system type on ${backdev} (${image})"
1328                                 fi
1329
1330                                 if [ -z "${fstype}" ]
1331                                 then
1332                                         fstype="${imagename##*.}"
1333                                         log_warning_msg "Unknown file system type on ${backdev} (${image}), assuming ${fstype}."
1334                                 fi
1335
1336                                 if [ "${UNIONTYPE}" != "unionmount" ]
1337                                 then
1338                                         mpoint="${croot}/${imagename}"
1339                                         rofslist="${mpoint} ${rofslist}"
1340                                 else
1341                                         mpoint="${rootmnt}"
1342                                         rofslist="${rootmnt} ${rofslist}"
1343                                 fi
1344                                 mkdir -p "${mpoint}"
1345                                 log_begin_msg "Mounting \"${image}\" on \"${mpoint}\" via \"${backdev}\""
1346                                 mount -t "${fstype}" -o ro,noatime "${backdev}" "${mpoint}" || panic "Can not mount ${backdev} (${image}) on ${mpoint}"
1347                                 log_end_msg
1348                         fi
1349                 done
1350         else
1351                 # we have a plain root system
1352                 mkdir -p "${croot}/filesystem"
1353                 log_begin_msg "Mounting \"${image_directory}\" on \"${croot}/filesystem\""
1354                 mount -t $(get_fstype "${image_directory}") -o ro,noatime "${image_directory}" "${croot}/filesystem" || \
1355                         panic "Can not mount ${image_directory} on ${croot}/filesystem" && \
1356                         rofslist="${croot}/filesystem ${rofslist}"
1357                 # probably broken:
1358                 mount -o bind ${croot}/filesystem $mountpoint
1359                 log_end_msg
1360         fi
1361
1362         mkdir -p /cow
1363
1364         # Looking for persistent devices or files
1365         if [ -n "${PERSISTENT}" ] && [ -z "${NOPERSISTENT}" ]
1366         then
1367
1368                 if [ -z "${QUICKUSBMODULES}" ]
1369                 then
1370                         # Load USB modules
1371                         num_block=$(ls -l /sys/block | wc -l)
1372                         for module in sd_mod uhci-hcd ehci-hcd ohci-hcd usb-storage
1373                         do
1374                                 modprobe -q -b ${module}
1375                         done
1376
1377                         udevadm trigger
1378                         udevadm settle
1379
1380                         # For some reason, udevsettle does not block in this scenario,
1381                         # so we sleep for a little while.
1382                         #
1383                         # See https://bugs.launchpad.net/ubuntu/+source/casper/+bug/84591
1384                         for timeout in 5 4 3 2 1
1385                         do
1386                                 sleep 1
1387
1388                                 if [ $(ls -l /sys/block | wc -l) -gt ${num_block} ]
1389                                 then
1390                                         break
1391                                 fi
1392                         done
1393                 fi
1394
1395                 case "${PERSISTENT_MEDIA}" in
1396                         removable)
1397                                 whitelistdev="$(removable_dev)"
1398                                 ;;
1399                         removable-usb)
1400                                 whitelistdev="$(removable_usb_dev)"
1401                                 ;;
1402                         *)
1403                                 whitelistdev=""
1404                                 ;;
1405                 esac
1406
1407                 if echo ${PERSISTENT_METHOD} | grep -qe "\<overlay\>"
1408                 then
1409                         overlays="${root_overlay_label} ${custom_overlay_label}"
1410                 fi
1411
1412                 if echo ${PERSISTENT_METHOD} | grep -qe "\<snapshot\>"
1413                 then
1414                         snapshots="${root_snapshot_label} ${home_snapshot_label}"
1415                 fi
1416
1417                 overlay_devices=""
1418                 for media in $(find_persistent_media "${overlays}" "${snapshots}" "${blacklistdev}" "${whitelistdev}")
1419                 do
1420                         media="$(echo ${media} | tr ":" " ")"
1421                         case ${media} in
1422                                 ${root_snapshot_label}=*)
1423                                         root_snapdata="${media#*=}"
1424                                         ;;
1425                                 ${home_snapshot_label}=*)
1426                                         # This second type should be removed when snapshot will get smarter,
1427                                         # hence when "/etc/live-snapshot*list" will be supported also by
1428                                         # ext2|ext3|ext4|jffs2 snapshot types.
1429                                         home_snapdata="${media#*=}"
1430                                         ;;
1431                                 ${root_overlay_label}=*)
1432                                         device="${media#*=}"
1433                                         root_overlay_device="${device}"
1434                                         ;;
1435                                 ${custom_overlay_label}=*)
1436                                         device="${media#*=}"
1437                                         overlay_devices="${overlay_devices} ${device}"
1438                                         ;;
1439                          esac
1440                 done
1441
1442                 if [ -b "${root_overlay_device}" ]
1443                 then
1444                         PERSISTENCE_IS_ON="1"
1445                         export PERSISTENCE_IS_ON
1446
1447                         cowdevice=${root_overlay_device}
1448                         cow_fstype=$(get_fstype "${root_overlay_device}")
1449                         if [ -z "${PERSISTENT_READONLY}" ]
1450                         then
1451                                 cow_mountopt="rw,noatime"
1452                         else
1453                                 cow_mountopt="ro,noatime"
1454                         fi
1455
1456                         if [ "${FORCEPERSISTENTFSCK}" = "Yes" ]
1457                         then
1458                                 fsck -y ${cowdevice}
1459                         fi
1460                 fi
1461         elif [ -n "${NFS_COW}" ] && [ -z "${NOPERSISTENT}" ]
1462         then
1463                 # check if there are any nfs options
1464                 if echo ${NFS_COW}|grep -q ','
1465                 then
1466                         nfs_cow_opts="-o nolock,$(echo ${NFS_COW}|cut -d, -f2-)"
1467                         nfs_cow=$(echo ${NFS_COW}|cut -d, -f1)
1468                 else
1469                         nfs_cow_opts="-o nolock"
1470                         nfs_cow=${NFS_COW}
1471                 fi
1472
1473                 if [ -n "${PERSISTENT_READONLY}" ]
1474                 then
1475                         nfs_cow_opts="${nfs_cow_opts},nocto,ro"
1476                 fi
1477
1478                 mac="$(get_mac)"
1479                 if [ -n "${mac}" ]
1480                 then
1481                         cowdevice=$(echo ${nfs_cow}|sed "s/client_mac_address/${mac}/")
1482                         cow_fstype="nfs"
1483                 else
1484                         panic "unable to determine mac address"
1485                 fi
1486         fi
1487
1488         if [ -z "${cowdevice}" ]
1489         then
1490                 cowdevice="tmpfs"
1491                 cow_fstype="tmpfs"
1492                 cow_mountopt="rw,noatime,mode=755"
1493         fi
1494
1495         if [ "${UNIONTYPE}" != "unionmount" ]
1496         then
1497                 if [ -n "${PERSISTENT_READONLY}" ]
1498                 then
1499                         persistent_root="/$(basename ${cowdevice})-backing"
1500                         mkdir -p ${persistent_root}
1501                 else
1502                         persistent_root="/cow"
1503                 fi
1504
1505                 if [ "${cow_fstype}" = "nfs" ]
1506                 then
1507                         log_begin_msg \
1508                                 "Trying nfsmount ${nfs_cow_opts} ${cowdevice} ${persistent_root}"
1509                         nfsmount ${nfs_cow_opts} ${cowdevice} ${persistent_root} || \
1510                                 panic "Can not mount ${cowdevice} (n: ${cow_fstype}) on ${persistent_root}"
1511                 else
1512                         mount -t ${cow_fstype} -o ${cow_mountopt} ${cowdevice} ${persistent_root} || \
1513                                 panic "Can not mount ${cowdevice} (o: ${cow_fstype}) on ${persistent_root}"
1514                 fi
1515         fi
1516
1517         rofscount=$(echo ${rofslist} |wc -w)
1518
1519         if [ ${rofscount} -ne 1 ]
1520         then
1521                 panic "only one RO file system supported with exposedroot: ${rofslist}"
1522         fi
1523         rofs=${rofslist%% }
1524
1525         if [ -n "${EXPOSED_ROOT}" ]
1526         then
1527                 mount --bind ${rofs} ${rootmnt} || \
1528                         panic "bind mount of ${rofs} failed"
1529
1530                 if [ -z "${SKIP_UNION_MOUNTS}" ]
1531                 then
1532                         cow_dirs='/var/tmp /var/lock /var/run /var/log /var/spool /home /var/lib/live'
1533                 else
1534                         cow_dirs=''
1535                 fi
1536         else
1537                 cow_dirs="/"
1538         fi
1539
1540         if [ "${cow_fstype}" != "tmpfs" ] && [ "${cow_dirs}" != "/" ] && [ "${UNIONTYPE}" = "unionmount" ]
1541         then
1542                 true # FIXME: Maybe it does, I don't really know.
1543                 #panic "unionmount does not support subunions (${cow_dirs})."
1544         fi
1545
1546         unionmountopts=""
1547         unionmountpoint=""
1548
1549         for dir in ${cow_dirs}; do
1550                 mkdir -p /cow${dir}
1551
1552                 unionmountpoint="${rootmnt}${dir}"
1553                 unionrw="/cow${dir}"
1554                 unionro="${rofs}${dir}"
1555                 # We don't handle spaces and other junk gracefully here, hopefully not needed.
1556                 case "${UNIONTYPE}" in
1557                         unionfs-fuse)
1558                                 unionmountopts="-o cow -o noinitgroups -o default_permissions -o allow_other -o use_ino -o suid"
1559                                 unionmountopts="${unionmountopts} ${unionrw}=RW:${unionro}=RO"
1560                                 ( sysctl -w fs.file-max=391524 ; ulimit -HSn 16384
1561                                 unionfs-fuse ${unionmountopts} "${unionmountpoint}" ) && \
1562                                 ( mkdir -p /run/sendsigs.omit.d
1563                                 pidof unionfs-fuse >> /run/sendsigs.omit.d/unionfs-fuse || true )
1564                                 ;;
1565
1566                         unionmount)
1567                                 unionmountopts="-t ${cow_fstype} -o noatime,union,${cow_mountopt} ${cowdevice}"
1568                                 mount_full $unionmountopts "${unionmountpoint}"
1569                                 ;;
1570
1571                         overlayfs)
1572                                 unionmountopts="-o noatime,${noxino_opt},lowerdir=${unionro},upperdir=${unionrw}"
1573                                 mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
1574                                 ;;
1575
1576                         *)
1577                                 if [ -n "${PERSISTENT_READONLY}" ]
1578                                 then
1579                                         mount -t tmpfs -o rw,noatime,mode=755 tmpfs "${unionrw}"
1580                                         unionmountopts="-o noatime,${noxino_opt}dirs=${unionrw}=rw:${persistent_root}=${roopt}:${unionro}=${roopt}"
1581                                 else
1582                                         unionmountopts="-o noatime,${noxino_opt}dirs=${unionrw}=rw:${unionro}=${roopt}"
1583                                 fi
1584                                 mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
1585                                 ;;
1586                 esac || \
1587                         panic "mount ${UNIONTYPE} on ${unionmountpoint} failed with option ${unionmountopts}"
1588         done
1589
1590         # Correct the permissions of /:
1591         chmod 0755 "${rootmnt}"
1592
1593         # tmpfs file systems
1594         touch /etc/fstab
1595         mkdir -p "${rootmnt}/live"
1596         mount -t tmpfs tmpfs ${rootmnt}/live
1597
1598         # Adding other custom mounts
1599         if [ -n "${PERSISTENT}" ] && [ -z "${NOPERSISTENT}" ]
1600         then
1601                 bindings="/bindings.list"
1602                 links="/links.list"
1603                 custom_mounts="/custom_mounts.list"
1604                 rm -f ${bindings} ${links} ${custom_mounts} >/dev/null 2>&1
1605                 persistent_backing="${rootmnt}/live/persistent"
1606
1607                 # First we scan all media and gather all information about custom mounts
1608                 for device in ${overlay_devices}
1609                 do
1610                         if [ ! -b "${device}" ]
1611                         then
1612                                 continue
1613                         fi
1614                         device_name="$(basename ${device})"
1615                         backing="${persistent_backing}/${device_name}"
1616                         mkdir -p "${backing}"
1617                         device_fstype="$(get_fstype ${device})"
1618                         if [ -z "${PERSISTENT_READONLY}" ]
1619                         then
1620                                 device_mount_opts="rw,noatime"
1621                         else
1622                                 device_mount_opts="ro,noatime"
1623                         fi
1624                         device_used=""
1625                         mount -t "${device_fstype}" -o "${device_mount_opts}" "${device}" "${backing}"
1626                         include_list="${backing}/${persistence_list}"
1627                         if [ ! -r "${include_list}" ]
1628                         then
1629                                 umount "${backing}"
1630                                 rmdir "${backing}"
1631                                 continue
1632                         fi
1633
1634                         [ "${DEBUG}" = "Yes" ] && cp ${include_list} ${persistent_backing}/${persistence_list}.${device_name}
1635                         while read source dest options # < ${include_list}
1636                         do
1637                                 if echo ${source} | grep -qe "^[[:space:]]*#"
1638                                 then
1639                                         # skipping commented line
1640                                         continue
1641                                 fi
1642
1643                                 if echo ${dest} | grep -qe "^[^/]"
1644                                 then
1645                                         options="${dest}"
1646                                         dest="${source}"
1647                                 elif [ -z "${dest}" ]
1648                                 then
1649                                         dest="${source}"
1650                                 fi
1651
1652                                 if echo ${dest} | grep -qe "^/\+$\|^/\+live\(/.*\)\?$"
1653                                 then
1654                                         # mounting on / or /live could cause trouble
1655                                         log_warning_msg "Skipping unsafe custom mount on ${dest}"
1656                                         continue
1657                                 fi
1658
1659                                 for opt in $(echo ${options} | tr ',' ' ');
1660                                 do
1661                                         case "${opt}" in
1662                                                 "linkfiles")
1663                                                         ;;
1664                                                 *)
1665                                                         log_warning_msg "Skipping custom mount with unkown option: ${opt}"
1666                                                         continue 2
1667                                                         ;;
1668                                         esac
1669                                 done
1670
1671                                 # 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.
1672                                 # FIXME: handle case: we have /a in $bindings from current $device, now we find /a/b, so we skip /a/b
1673
1674                                 # ensure that no multiple-/ occur in paths
1675                                 full_source="$(echo ${backing}/${source}/ | sed -e 's|/\+|/|g')"
1676                                 full_dest="$(echo ${rootmnt}/${dest}/ | sed -e 's|/\+|/|g')"
1677                                 device_used="yes"
1678                                 if echo ${options} | grep -qe "\<linkfiles\>";
1679                                 then
1680                                         echo "${full_source} ${full_dest} ${options}" >> ${links}
1681                                 else
1682                                         echo "${full_source} ${full_dest} ${options}" >> ${bindings}
1683                                 fi
1684                         done < ${include_list}
1685
1686                         if [ -z "${device_used}" ]
1687                         then
1688                                 # this device was not used for / earlier, or custom mount point now, so it's useless
1689                                 umount "${backing}"
1690                                 rmdir "${backing}"
1691                         fi
1692                 done
1693
1694                 # We sort the list according to destination so we're sure 
1695                 # that we won't hide a previous mount. We also  ignore
1696                 # duplicate destinations in a more or less arbitrary way.
1697                 [ -e "${bindings}" ] && sort -k2 -sbu ${bindings} >> ${custom_mounts}
1698                 rm -f ${bindings}
1699
1700                 # After all mounts are considered we add symlinks so they
1701                 # won't be hidden by some mount.
1702                 [ -e "${links}" ] && sort -k2 -sbu ${links} >> ${custom_mounts}
1703                 rm -f ${links}
1704
1705                 [ "${DEBUG}" = "Yes" ] && cp ${custom_mounts} ${persistent_backing}
1706
1707                 # Now we do the actual mounting (and symlinking)
1708                 while read source dest options  # < ${custom_mounts}
1709                 do
1710                         local opt_linkfiles=""
1711                         for opt in $(echo ${options} | tr ',' ' ');
1712                         do
1713                                  case "${opt}" in
1714                                         "linkfiles")
1715                                                 opt_linkfiles="yes"
1716                                                 ;;
1717                                 esac
1718                         done
1719
1720                         if mountpoint -q "${dest}";
1721                         then
1722                                 log_warning_msg "Skipping custom mount ${source} on ${dest}: destination is already a mount point"
1723                                 continue
1724                         fi
1725
1726                         # FIXME: we don't handle already existing non-directory files in the paths of both $source and $dest.
1727
1728                         if [ ! -d "${dest}" ]
1729                         then
1730                                 # if ${dest} is in /home/$user, try fixing proper ownership
1731                                 # FIXME: this should really be handled by live-config since we don't know for sure which uid a certain user has until then
1732                                 if echo ${dest} | grep -qe "^${rootmnt}/*home/\+[^/]\+"
1733                                 then
1734                                         path="/"
1735                                         for dir in $(echo ${dest} | sed -e 's|/\+| |g')
1736                                         do
1737                                                 path=${path}/${dir}
1738                                                 if [ ! -e ${path} ]
1739                                                 then
1740                                                         mkdir -p ${path}
1741                                                         # assume that the intended user is the first, which is usually the case
1742                                                         chown 1000:1000 ${path}
1743                                                 fi
1744                                         done
1745                                 else
1746                                         mkdir -p ${dest}
1747                                 fi
1748                         fi
1749
1750                         # FIXME: could we instead only save the aufs-diff in the persistent media? implications? What about when there's changes in the live image?
1751
1752                         # if ${source} doesn't exist on our persistent media we
1753                         # bootstrap it with $dest from the live filesystem.
1754                         # this both makes sense and is critical if we're
1755                         # dealing with /etc or other system dir.
1756                         if [ ! -d "${source}" ]
1757                         then
1758                                 if [ -n "${PERSISTENT_READONLY}" ] || [ -n "${opt_linkfiles}" ]
1759                                 then
1760                                         continue
1761                                 fi
1762                                 # ensure that $dest is not copied *into* $source
1763                                 mkdir -p "$(dirname ${source})"
1764                                 cp -a "${dest}" "${source}"
1765                         fi
1766
1767                         if [ -z "${PERSISTENT_READONLY}" ]
1768                         then
1769                                 if [ -n "${opt_linkfiles}" ]
1770                                 then
1771                                         links_source="${source}"
1772                                         links_dest="${dest}"
1773                                 else
1774                                         mount --bind "${source}" "${dest}"
1775                                 fi
1776                         else
1777                                 if [ -n "${opt_linkfiles}" ]
1778                                 then
1779                                         links_dest="${dest}"
1780                                         dest="$(mktemp -d ${persistent_backing}/links_source-XXXXXX)"
1781                                         links_source="${dest}"
1782                                 fi
1783                                 unionrw="$(echo ${dest} | sed -e "s|${rootmnt}|/cow/|")"
1784                                 mkdir -p ${unionrw}
1785                                 unionmountopts="noatime,${noxino_opt}dirs=${unionrw}=rw:${source}=${roopt}"
1786                                 mount -t "${UNIONTYPE}" -o "${unionmountopts}" "${UNIONTYPE}" "${dest}"
1787                         fi
1788
1789                         if [ -n "${opt_linkfiles}" ]
1790                         then
1791                                 link_files "${links_source}" "${links_dest}" "${rootmnt}"
1792                         fi
1793
1794                         PERSISTENCE_IS_ON="1"
1795                         export PERSISTENCE_IS_ON
1796                 done < ${custom_mounts}
1797                 rm -f ${bindings}
1798
1799                 # Look for other snapshots to copy in
1800                 try_snap "${root_snapdata}" "${rootmnt}" "ROOT"
1801                 # This second type should be removed when snapshot grow smarter
1802                 try_snap "${home_snapdata}" "${rootmnt}" "HOME" "/home"
1803         fi
1804
1805         if [ -n "${SHOWMOUNTS}" ]
1806         then
1807                 for d in ${rofslist}
1808                 do
1809                         mkdir -p "${rootmnt}/live/${d##*/}"
1810
1811                         case d in
1812                                 *.dir)
1813                                         # do nothing # mount -o bind "${d}" "${rootmnt}/live/${d##*/}"
1814                                         ;;
1815
1816                                 *)
1817                                         case "${UNIONTYPE}" in
1818                                                 unionfs-fuse)
1819                                                         mount -o bind "${d}" "${rootmnt}/live/${d##*/}"
1820                                                         ;;
1821
1822                                                 *)
1823                                                         mount -o move "${d}" "${rootmnt}/live/${d##*/}"
1824                                                         ;;
1825                                         esac
1826                                         ;;
1827                         esac
1828                 done
1829         fi
1830
1831         # shows cow fs on /cow for use by live-snapshot
1832         mkdir -p "${rootmnt}/live/cow"
1833         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"
1834 }
1835
1836 check_dev ()
1837 {
1838         sysdev="${1}"
1839         devname="${2}"
1840         skip_uuid_check="${3}"
1841
1842         # support for fromiso=.../isofrom=....
1843         if [ -n "$FROMISO" ]
1844         then
1845                 ISO_DEVICE=$(dirname $FROMISO)
1846                 if ! [ -b $ISO_DEVICE ]
1847                 then
1848                         # to support unusual device names like /dev/cciss/c0d0p1
1849                         # as well we have to identify the block device name, let's
1850                         # do that for up to 15 levels
1851                         i=15
1852                         while [ -n "$ISO_DEVICE" ] && [ "$i" -gt 0 ]
1853                         do
1854                                 ISO_DEVICE=$(dirname ${ISO_DEVICE})
1855                                 [ -b "$ISO_DEVICE" ] && break
1856                                 i=$(($i -1))
1857                         done
1858                 fi
1859
1860                 if [ "$ISO_DEVICE" = "/" ]
1861                 then
1862                         echo "Warning: device for bootoption isofrom= ($FROMISO) not found.">>/live-boot.log
1863                 else
1864                         fs_type=$(get_fstype "${ISO_DEVICE}")
1865                         if is_supported_fs ${fs_type}
1866                         then
1867                                 mkdir /isofrom
1868                                 mount -t $fs_type "$ISO_DEVICE" /isofrom
1869                                 ISO_NAME="$(echo $FROMISO | sed "s|$ISO_DEVICE||")"
1870                                 loopdevname=$(setup_loop "/isofrom/${ISO_NAME}" "loop" "/sys/block/loop*" "" '')
1871                                 devname="${loopdevname}"
1872                         else
1873                                 echo "Warning: unable to mount $ISO_DEVICE." >>/live-boot.log
1874                         fi
1875                 fi
1876         fi
1877
1878         if [ -z "${devname}" ]
1879         then
1880                 devname=$(sys2dev "${sysdev}")
1881         fi
1882
1883         if [ -d "${devname}" ]
1884         then
1885                 mount -o bind "${devname}" $mountpoint || continue
1886
1887                 if is_live_path $mountpoint
1888                 then
1889                         echo $mountpoint
1890                         return 0
1891                 else
1892                         umount $mountpoint
1893                 fi
1894         fi
1895
1896         IFS=","
1897         for device in ${devname}
1898         do
1899                 case "$device" in
1900                         *mapper*)
1901                                 # Adding lvm support
1902                                 if [ -x /scripts/local-top/lvm2 ]
1903                                 then
1904                                         ROOT="$device" resume="" /scripts/local-top/lvm2
1905                                 fi
1906                                 ;;
1907
1908                         /dev/md*)
1909                                 # Adding raid support
1910                                 if [ -x /scripts/local-top/mdadm ]
1911                                 then
1912                                         cp /conf/conf.d/md /conf/conf.d/md.orig
1913                                         echo "MD_DEVS=$device " >> /conf/conf.d/md
1914                                         /scripts/local-top/mdadm
1915                                         mv /conf/conf.d/md.orig /conf/conf.d/md
1916                                 fi
1917                                 ;;
1918                 esac
1919         done
1920         unset IFS
1921
1922         [ -n "$device" ] && devname="$device"
1923
1924         [ -e "$devname" ] || continue
1925
1926         if [ -n "${LIVE_MEDIA_OFFSET}" ]
1927         then
1928                 loopdevname=$(setup_loop "${devname}" "loop" "/sys/block/loop*" "${LIVE_MEDIA_OFFSET}" '')
1929                 devname="${loopdevname}"
1930         fi
1931
1932         fstype=$(get_fstype "${devname}")
1933
1934         if is_supported_fs ${fstype}
1935         then
1936                 devuid=$(blkid -o value -s UUID "$devname")
1937                 [ -n "$devuid" ] && grep -qs "\<$devuid\>" $tried && continue
1938                 mount -t ${fstype} -o ro,noatime "${devname}" ${mountpoint} || continue
1939                 [ -n "$devuid" ] && echo "$devuid" >> $tried
1940
1941                 if is_live_path ${mountpoint} && \
1942                         ([ "${skip_uuid_check}" ] || matches_uuid ${mountpoint})
1943                 then
1944                         echo ${mountpoint}
1945                         return 0
1946                 else
1947                         umount ${mountpoint} 2>/dev/null
1948                 fi
1949         fi
1950
1951         if [ -n "${LIVE_MEDIA_OFFSET}" ]
1952         then
1953                 losetup -d "${loopdevname}"
1954         fi
1955
1956         return 1
1957 }
1958
1959 find_livefs ()
1960 {
1961         timeout="${1}"
1962
1963         # don't start autodetection before timeout has expired
1964         if [ -n "${LIVE_MEDIA_TIMEOUT}" ]
1965         then
1966                 if [ "${timeout}" -lt "${LIVE_MEDIA_TIMEOUT}" ]
1967                 then
1968                         return 1
1969                 fi
1970         fi
1971
1972         # first look at the one specified in the command line
1973         case "${LIVE_MEDIA}" in
1974                 removable-usb)
1975                         for sysblock in $(removable_usb_dev "sys")
1976                         do
1977                                 for dev in $(subdevices "${sysblock}")
1978                                 do
1979                                         if check_dev "${dev}"
1980                                         then
1981                                                 return 0
1982                                         fi
1983                                 done
1984                         done
1985                         return 1
1986                         ;;
1987
1988                 removable)
1989                         for sysblock in $(removable_dev "sys")
1990                         do
1991                                 for dev in $(subdevices "${sysblock}")
1992                                 do
1993                                         if check_dev "${dev}"
1994                                         then
1995                                                 return 0
1996                                         fi
1997                                 done
1998                         done
1999                         return 1
2000                         ;;
2001
2002                 *)
2003                         if [ ! -z "${LIVE_MEDIA}" ]
2004                         then
2005                                 if check_dev "null" "${LIVE_MEDIA}" "skip_uuid_check"
2006                                 then
2007                                         return 0
2008                                 fi
2009                         fi
2010                         ;;
2011         esac
2012
2013         # or do the scan of block devices
2014         # prefer removable devices over non-removable devices, so scan them first
2015         devices_to_scan="$(removable_dev 'sys') $(non_removable_dev 'sys')"
2016
2017         for sysblock in $devices_to_scan
2018         do
2019                 devname=$(sys2dev "${sysblock}")
2020                 [ -e "$devname" ] || continue
2021                 fstype=$(get_fstype "${devname}")
2022
2023                 if /lib/udev/cdrom_id ${devname} > /dev/null
2024                 then
2025                         if check_dev "null" "${devname}"
2026                         then
2027                                 return 0
2028                         fi
2029                 elif is_nice_device "${sysblock}"
2030                 then
2031                         for dev in $(subdevices "${sysblock}")
2032                         do
2033                                 if check_dev "${dev}"
2034                                 then
2035                                         return 0
2036                                 fi
2037                         done
2038                 elif [ "${fstype}" = "squashfs" -o \
2039                         "${fstype}" = "btrfs" -o \
2040                         "${fstype}" = "ext2" -o \
2041                         "${fstype}" = "ext3" -o \
2042                         "${fstype}" = "ext4" -o \
2043                         "${fstype}" = "jffs2" ]
2044                 then
2045                         # This is an ugly hack situation, the block device has
2046                         # an image directly on it.  It's hopefully
2047                         # live-boot, so take it and run with it.
2048                         ln -s "${devname}" "${devname}.${fstype}"
2049                         echo "${devname}.${fstype}"
2050                         return 0
2051                 fi
2052         done
2053
2054         return 1
2055 }
2056
2057 integrity_check ()
2058 {
2059         media_mountpoint="${1}"
2060
2061         log_begin_msg "Checking media integrity"
2062
2063         cd ${media_mountpoint}
2064         /bin/md5sum -c md5sum.txt < /dev/tty8 > /dev/tty8
2065         RC="${?}"
2066
2067         log_end_msg
2068
2069         if [ "${RC}" -eq 0 ]
2070         then
2071                 log_success_msg "Everything ok, will reboot in 10 seconds."
2072                 sleep 10
2073                 cd /
2074                 umount ${media_mountpoint}
2075                 sync
2076                 echo u > /proc/sysrq-trigger
2077                 echo b > /proc/sysrq-trigger
2078         else
2079                 panic "Not ok, a media defect is likely, switch to VT8 for details."
2080         fi
2081 }
2082
2083 mountroot ()
2084 {
2085         if [ -x /scripts/local-top/cryptroot ]; then
2086             /scripts/local-top/cryptroot
2087         fi
2088
2089         exec 6>&1
2090         exec 7>&2
2091         exec > live-boot.log
2092         exec 2>&1
2093         tail -f live-boot.log >&7 &
2094         tailpid="${!}"
2095
2096         # Ensure 'panic' function is overridden
2097         . /scripts/live-functions
2098
2099         Arguments
2100
2101         maybe_break live-premount
2102         log_begin_msg "Running /scripts/live-premount"
2103         run_scripts /scripts/live-premount
2104         log_end_msg
2105
2106         # Needed here too because some things (*cough* udev *cough*)
2107         # changes the timeout
2108
2109         if [ ! -z "${NETBOOT}" ] || [ ! -z "${FETCH}" ] || [ ! -z "${HTTPFS}" ] || [ ! -z "${FTPFS}" ]
2110         then
2111                 if do_netmount
2112                 then
2113                         livefs_root="${mountpoint}"
2114                 else
2115                         panic "Unable to find a live file system on the network"
2116                 fi
2117         else
2118                 if [ -n "${ISCSI_PORTAL}" ]
2119                 then
2120                         do_iscsi && livefs_root="${mountpoint}"
2121                 elif [ -n "${PLAIN_ROOT}" ] && [ -n "${ROOT}" ]
2122                 then
2123                         # Do a local boot from hd
2124                         livefs_root=${ROOT}
2125                 else
2126                         if [ -x /usr/bin/memdiskfind ]
2127                         then
2128                                 MEMDISK=$(/usr/bin/memdiskfind)
2129
2130                                 if [ $? -eq 0 ]
2131                                 then
2132                                         # We found a memdisk, set up phram
2133                                         modprobe phram phram=memdisk,${MEMDISK}
2134
2135                                         # Load mtdblock, the memdisk will be /dev/mtdblock0
2136                                         modprobe mtdblock
2137                                 fi
2138                         fi
2139
2140                         # Scan local devices for the image
2141                         i=0
2142                         while [ "$i" -lt 60 ]
2143                         do
2144                                 livefs_root=$(find_livefs ${i})
2145
2146                                 if [ -n "${livefs_root}" ]
2147                                 then
2148                                         break
2149                                 fi
2150
2151                                 sleep 1
2152                                 i="$(($i + 1))"
2153                         done
2154                 fi
2155         fi
2156
2157         if [ -z "${livefs_root}" ]
2158         then
2159                 panic "Unable to find a medium containing a live file system"
2160         fi
2161
2162         if [ "${INTEGRITY_CHECK}" ]
2163         then
2164                 integrity_check "${livefs_root}"
2165         fi
2166
2167         if [ "${TORAM}" ]
2168         then
2169                 live_dest="ram"
2170         elif [ "${TODISK}" ]
2171         then
2172                 live_dest="${TODISK}"
2173         fi
2174
2175         if [ "${live_dest}" ]
2176         then
2177                 log_begin_msg "Copying live media to ${live_dest}"
2178                 copy_live_to "${livefs_root}" "${live_dest}"
2179                 log_end_msg
2180         fi
2181
2182         # if we do not unmount the ISO we can't run "fsck /dev/ice" later on
2183         # because the mountpoint is left behind in /proc/mounts, so let's get
2184         # rid of it when running from RAM
2185         if [ -n "$FROMISO" ] && [ "${TORAM}" ]
2186         then
2187           losetup -d /dev/loop0
2188           grep -q /isofrom /proc/mounts && umount /isofrom
2189         fi
2190
2191         if [ -n "${MODULETORAMFILE}" ] || [ -n "${PLAIN_ROOT}" ]
2192         then
2193                 setup_unionfs "${livefs_root}" "${rootmnt}"
2194         else
2195                 mac="$(get_mac)"
2196                 mac="$(echo ${mac} | sed 's/-//g')"
2197                 mount_images_in_directory "${livefs_root}" "${rootmnt}" "${mac}"
2198         fi
2199
2200
2201         if [ -n "${ROOT_PID}" ] ; then
2202                 echo "${ROOT_PID}" > "${rootmnt}"/live/root.pid
2203         fi
2204
2205         log_end_msg
2206
2207         # unionfs-fuse needs /dev to be bind-mounted for the duration of
2208         # live-bottom; udev's init script will take care of things after that
2209         if [ "${UNIONTYPE}" = unionfs-fuse ]
2210         then
2211                 mount -n -o bind /dev "${rootmnt}/dev"
2212         fi
2213
2214         # Move to the new root filesystem so that programs there can get at it.
2215         if [ ! -d /root/live/image ]
2216         then
2217                 mkdir -p /root/live/image
2218                 mount --move /live/image /root/live/image
2219         fi
2220
2221         # aufs2 in kernel versions around 2.6.33 has a regression:
2222         # directories can't be accessed when read for the first the time,
2223         # causing a failure for example when accessing /var/lib/fai
2224         # when booting FAI, this simple workaround solves it
2225         ls /root/* >/dev/null 2>&1
2226
2227         # copy snapshot configuration if exists
2228         if [ -f snapshot.conf ]
2229         then
2230                 log_begin_msg "Copying snapshot.conf to ${rootmnt}/etc/live/boot.d"
2231                 if [ ! -d "${rootmnt}/etc/live/boot.d" ]
2232                 then
2233                         mkdir -p "${rootmnt}/etc/live/boot.d"
2234                 fi
2235                 cp snapshot.conf "${rootmnt}/etc/live/boot.d/"
2236                 log_end_msg
2237         fi
2238
2239         if [ -f /etc/resolv.conf ] && [ ! -s ${rootmnt}/etc/resolv.conf ]
2240         then
2241                 log_begin_msg "Copying /etc/resolv.conf to ${rootmnt}/etc/resolv.conf"
2242                 cp -v /etc/resolv.conf ${rootmnt}/etc/resolv.conf
2243                 log_end_msg
2244         fi
2245
2246         maybe_break live-bottom
2247         log_begin_msg "Running /scripts/live-bottom\n"
2248
2249         run_scripts /scripts/live-bottom
2250         log_end_msg
2251
2252         if [ "${UNIONFS}" = unionfs-fuse ]
2253         then
2254                 umount "${rootmnt}/dev"
2255         fi
2256
2257         exec 1>&6 6>&-
2258         exec 2>&7 7>&-
2259         kill ${tailpid}
2260         [ -w "${rootmnt}/var/log/" ] && cp live-boot.log "${rootmnt}/var/log/" 2>/dev/null
2261 }