Calling rm without -f when removing some temporary custom mount related files to...
[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
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                         if mountpoint -q "${dest}";
1711                         then
1712                                 log_warning_msg "Skipping custom mount ${source} on ${dest}: destination is already a mount point"
1713                                 continue
1714                         fi
1715
1716                         # FIXME: we don't handle already existing non-directory files in the paths of both $source and $dest.
1717
1718                         if [ ! -d "${dest}" ]
1719                         then
1720                                 # if ${dest} is in /home/$user, try fixing proper ownership
1721                                 # FIXME: this should really be handled by live-config since we don't know for sure which uid a certain user has until then
1722                                 if echo ${dest} | grep -qe "^${rootmnt}/*home/\+[^/]\+"
1723                                 then
1724                                         path="/"
1725                                         for dir in $(echo ${dest} | sed -e 's|/\+| |g')
1726                                         do
1727                                                 path=${path}/${dir}
1728                                                 if [ ! -e ${path} ]
1729                                                 then
1730                                                         mkdir -p ${path}
1731                                                         # assume that the intended user is the first, which is usually the case
1732                                                         chown 1000:1000 ${path}
1733                                                 fi
1734                                         done
1735                                 else
1736                                         mkdir -p ${dest}
1737                                 fi
1738                         fi
1739
1740                         # FIXME: could we instead only save the aufs-diff in the persistent media? implications? What about when there's changes in the live image?
1741
1742                         # if ${source} doesn't exist on our persistent media we
1743                         # bootstrap it with $dest from the live filesystem.
1744                         # this both makes sense and is critical if we're
1745                         # dealing with /etc or other system dir.
1746                         if [ ! -d "${source}" ]
1747                         then
1748                                 if [ -n "${PERSISTENT_READONLY}" ] || echo ${options} | grep -qe "\<linkfiles\>"
1749                                 then
1750                                         continue
1751                                 fi
1752                                 # ensure that $dest is not copied *into* $source
1753                                 mkdir -p "$(dirname ${source})"
1754                                 cp -a "${dest}" "${source}"
1755                         fi
1756
1757                         if [ -z "${PERSISTENT_READONLY}" ]
1758                         then
1759                                 if echo ${options} | grep -qe "\<linkfiles\>";
1760                                 then
1761                                         links_source="${source}"
1762                                         links_dest="${dest}"
1763                                 else
1764                                         mount --bind "${source}" "${dest}"
1765                                 fi
1766                         else
1767                                 if echo ${options} | grep -qe "\<linkfiles\>";
1768                                 then
1769                                         links_dest="${dest}"
1770                                         dest="$(mktemp -d ${persistent_backing}/links_source-XXXXXX)"
1771                                         links_source="${dest}"
1772                                 fi
1773                                 unionrw="$(echo ${dest} | sed -e "s|${rootmnt}|/cow/|")"
1774                                 mkdir -p ${unionrw}
1775                                 unionmountopts="noatime,${noxino_opt}dirs=${unionrw}=rw:${source}=${roopt}"
1776                                 mount -t "${UNIONTYPE}" -o "${unionmountopts}" "${UNIONTYPE}" "${dest}"
1777                         fi
1778
1779                         if echo $options | grep -qe "\<linkfiles\>";
1780                         then
1781                                 link_files "${links_source}" "${links_dest}" "${rootmnt}"
1782                         fi
1783
1784                         PERSISTENCE_IS_ON="1"
1785                         export PERSISTENCE_IS_ON
1786                 done < ${custom_mounts}
1787                 rm -f ${bindings}
1788
1789                 # Look for other snapshots to copy in
1790                 try_snap "${root_snapdata}" "${rootmnt}" "ROOT"
1791                 # This second type should be removed when snapshot grow smarter
1792                 try_snap "${home_snapdata}" "${rootmnt}" "HOME" "/home"
1793         fi
1794
1795         if [ -n "${SHOWMOUNTS}" ]
1796         then
1797                 for d in ${rofslist}
1798                 do
1799                         mkdir -p "${rootmnt}/live/${d##*/}"
1800
1801                         case d in
1802                                 *.dir)
1803                                         # do nothing # mount -o bind "${d}" "${rootmnt}/live/${d##*/}"
1804                                         ;;
1805
1806                                 *)
1807                                         case "${UNIONTYPE}" in
1808                                                 unionfs-fuse)
1809                                                         mount -o bind "${d}" "${rootmnt}/live/${d##*/}"
1810                                                         ;;
1811
1812                                                 *)
1813                                                         mount -o move "${d}" "${rootmnt}/live/${d##*/}"
1814                                                         ;;
1815                                         esac
1816                                         ;;
1817                         esac
1818                 done
1819         fi
1820
1821         # shows cow fs on /cow for use by live-snapshot
1822         mkdir -p "${rootmnt}/live/cow"
1823         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"
1824 }
1825
1826 check_dev ()
1827 {
1828         sysdev="${1}"
1829         devname="${2}"
1830         skip_uuid_check="${3}"
1831
1832         # support for fromiso=.../isofrom=....
1833         if [ -n "$FROMISO" ]
1834         then
1835                 ISO_DEVICE=$(dirname $FROMISO)
1836                 if ! [ -b $ISO_DEVICE ]
1837                 then
1838                         # to support unusual device names like /dev/cciss/c0d0p1
1839                         # as well we have to identify the block device name, let's
1840                         # do that for up to 15 levels
1841                         i=15
1842                         while [ -n "$ISO_DEVICE" ] && [ "$i" -gt 0 ]
1843                         do
1844                                 ISO_DEVICE=$(dirname ${ISO_DEVICE})
1845                                 [ -b "$ISO_DEVICE" ] && break
1846                                 i=$(($i -1))
1847                         done
1848                 fi
1849
1850                 if [ "$ISO_DEVICE" = "/" ]
1851                 then
1852                         echo "Warning: device for bootoption isofrom= ($FROMISO) not found.">>/live-boot.log
1853                 else
1854                         fs_type=$(get_fstype "${ISO_DEVICE}")
1855                         if is_supported_fs ${fs_type}
1856                         then
1857                                 mkdir /isofrom
1858                                 mount -t $fs_type "$ISO_DEVICE" /isofrom
1859                                 ISO_NAME="$(echo $FROMISO | sed "s|$ISO_DEVICE||")"
1860                                 loopdevname=$(setup_loop "/isofrom/${ISO_NAME}" "loop" "/sys/block/loop*" "" '')
1861                                 devname="${loopdevname}"
1862                         else
1863                                 echo "Warning: unable to mount $ISO_DEVICE." >>/live-boot.log
1864                         fi
1865                 fi
1866         fi
1867
1868         if [ -z "${devname}" ]
1869         then
1870                 devname=$(sys2dev "${sysdev}")
1871         fi
1872
1873         if [ -d "${devname}" ]
1874         then
1875                 mount -o bind "${devname}" $mountpoint || continue
1876
1877                 if is_live_path $mountpoint
1878                 then
1879                         echo $mountpoint
1880                         return 0
1881                 else
1882                         umount $mountpoint
1883                 fi
1884         fi
1885
1886         IFS=","
1887         for device in ${devname}
1888         do
1889                 case "$device" in
1890                         *mapper*)
1891                                 # Adding lvm support
1892                                 if [ -x /scripts/local-top/lvm2 ]
1893                                 then
1894                                         ROOT="$device" resume="" /scripts/local-top/lvm2
1895                                 fi
1896                                 ;;
1897
1898                         /dev/md*)
1899                                 # Adding raid support
1900                                 if [ -x /scripts/local-top/mdadm ]
1901                                 then
1902                                         cp /conf/conf.d/md /conf/conf.d/md.orig
1903                                         echo "MD_DEVS=$device " >> /conf/conf.d/md
1904                                         /scripts/local-top/mdadm
1905                                         mv /conf/conf.d/md.orig /conf/conf.d/md
1906                                 fi
1907                                 ;;
1908                 esac
1909         done
1910         unset IFS
1911
1912         [ -n "$device" ] && devname="$device"
1913
1914         [ -e "$devname" ] || continue
1915
1916         if [ -n "${LIVE_MEDIA_OFFSET}" ]
1917         then
1918                 loopdevname=$(setup_loop "${devname}" "loop" "/sys/block/loop*" "${LIVE_MEDIA_OFFSET}" '')
1919                 devname="${loopdevname}"
1920         fi
1921
1922         fstype=$(get_fstype "${devname}")
1923
1924         if is_supported_fs ${fstype}
1925         then
1926                 devuid=$(blkid -o value -s UUID "$devname")
1927                 [ -n "$devuid" ] && grep -qs "\<$devuid\>" $tried && continue
1928                 mount -t ${fstype} -o ro,noatime "${devname}" ${mountpoint} || continue
1929                 [ -n "$devuid" ] && echo "$devuid" >> $tried
1930
1931                 if is_live_path ${mountpoint} && \
1932                         ([ "${skip_uuid_check}" ] || matches_uuid ${mountpoint})
1933                 then
1934                         echo ${mountpoint}
1935                         return 0
1936                 else
1937                         umount ${mountpoint} 2>/dev/null
1938                 fi
1939         fi
1940
1941         if [ -n "${LIVE_MEDIA_OFFSET}" ]
1942         then
1943                 losetup -d "${loopdevname}"
1944         fi
1945
1946         return 1
1947 }
1948
1949 find_livefs ()
1950 {
1951         timeout="${1}"
1952
1953         # don't start autodetection before timeout has expired
1954         if [ -n "${LIVE_MEDIA_TIMEOUT}" ]
1955         then
1956                 if [ "${timeout}" -lt "${LIVE_MEDIA_TIMEOUT}" ]
1957                 then
1958                         return 1
1959                 fi
1960         fi
1961
1962         # first look at the one specified in the command line
1963         case "${LIVE_MEDIA}" in
1964                 removable-usb)
1965                         for sysblock in $(removable_usb_dev "sys")
1966                         do
1967                                 for dev in $(subdevices "${sysblock}")
1968                                 do
1969                                         if check_dev "${dev}"
1970                                         then
1971                                                 return 0
1972                                         fi
1973                                 done
1974                         done
1975                         return 1
1976                         ;;
1977
1978                 removable)
1979                         for sysblock in $(removable_dev "sys")
1980                         do
1981                                 for dev in $(subdevices "${sysblock}")
1982                                 do
1983                                         if check_dev "${dev}"
1984                                         then
1985                                                 return 0
1986                                         fi
1987                                 done
1988                         done
1989                         return 1
1990                         ;;
1991
1992                 *)
1993                         if [ ! -z "${LIVE_MEDIA}" ]
1994                         then
1995                                 if check_dev "null" "${LIVE_MEDIA}" "skip_uuid_check"
1996                                 then
1997                                         return 0
1998                                 fi
1999                         fi
2000                         ;;
2001         esac
2002
2003         # or do the scan of block devices
2004         # prefer removable devices over non-removable devices, so scan them first
2005         devices_to_scan="$(removable_dev 'sys') $(non_removable_dev 'sys')"
2006
2007         for sysblock in $devices_to_scan
2008         do
2009                 devname=$(sys2dev "${sysblock}")
2010                 [ -e "$devname" ] || continue
2011                 fstype=$(get_fstype "${devname}")
2012
2013                 if /lib/udev/cdrom_id ${devname} > /dev/null
2014                 then
2015                         if check_dev "null" "${devname}"
2016                         then
2017                                 return 0
2018                         fi
2019                 elif is_nice_device "${sysblock}"
2020                 then
2021                         for dev in $(subdevices "${sysblock}")
2022                         do
2023                                 if check_dev "${dev}"
2024                                 then
2025                                         return 0
2026                                 fi
2027                         done
2028                 elif [ "${fstype}" = "squashfs" -o \
2029                         "${fstype}" = "btrfs" -o \
2030                         "${fstype}" = "ext2" -o \
2031                         "${fstype}" = "ext3" -o \
2032                         "${fstype}" = "ext4" -o \
2033                         "${fstype}" = "jffs2" ]
2034                 then
2035                         # This is an ugly hack situation, the block device has
2036                         # an image directly on it.  It's hopefully
2037                         # live-boot, so take it and run with it.
2038                         ln -s "${devname}" "${devname}.${fstype}"
2039                         echo "${devname}.${fstype}"
2040                         return 0
2041                 fi
2042         done
2043
2044         return 1
2045 }
2046
2047 integrity_check ()
2048 {
2049         media_mountpoint="${1}"
2050
2051         log_begin_msg "Checking media integrity"
2052
2053         cd ${media_mountpoint}
2054         /bin/md5sum -c md5sum.txt < /dev/tty8 > /dev/tty8
2055         RC="${?}"
2056
2057         log_end_msg
2058
2059         if [ "${RC}" -eq 0 ]
2060         then
2061                 log_success_msg "Everything ok, will reboot in 10 seconds."
2062                 sleep 10
2063                 cd /
2064                 umount ${media_mountpoint}
2065                 sync
2066                 echo u > /proc/sysrq-trigger
2067                 echo b > /proc/sysrq-trigger
2068         else
2069                 panic "Not ok, a media defect is likely, switch to VT8 for details."
2070         fi
2071 }
2072
2073 mountroot ()
2074 {
2075         if [ -x /scripts/local-top/cryptroot ]; then
2076             /scripts/local-top/cryptroot
2077         fi
2078
2079         exec 6>&1
2080         exec 7>&2
2081         exec > live-boot.log
2082         exec 2>&1
2083         tail -f live-boot.log >&7 &
2084         tailpid="${!}"
2085
2086         # Ensure 'panic' function is overridden
2087         . /scripts/live-functions
2088
2089         Arguments
2090
2091         maybe_break live-premount
2092         log_begin_msg "Running /scripts/live-premount"
2093         run_scripts /scripts/live-premount
2094         log_end_msg
2095
2096         # Needed here too because some things (*cough* udev *cough*)
2097         # changes the timeout
2098
2099         if [ ! -z "${NETBOOT}" ] || [ ! -z "${FETCH}" ] || [ ! -z "${HTTPFS}" ] || [ ! -z "${FTPFS}" ]
2100         then
2101                 if do_netmount
2102                 then
2103                         livefs_root="${mountpoint}"
2104                 else
2105                         panic "Unable to find a live file system on the network"
2106                 fi
2107         else
2108                 if [ -n "${ISCSI_PORTAL}" ]
2109                 then
2110                         do_iscsi && livefs_root="${mountpoint}"
2111                 elif [ -n "${PLAIN_ROOT}" ] && [ -n "${ROOT}" ]
2112                 then
2113                         # Do a local boot from hd
2114                         livefs_root=${ROOT}
2115                 else
2116                         if [ -x /usr/bin/memdiskfind ]
2117                         then
2118                                 MEMDISK=$(/usr/bin/memdiskfind)
2119
2120                                 if [ $? -eq 0 ]
2121                                 then
2122                                         # We found a memdisk, set up phram
2123                                         modprobe phram phram=memdisk,${MEMDISK}
2124
2125                                         # Load mtdblock, the memdisk will be /dev/mtdblock0
2126                                         modprobe mtdblock
2127                                 fi
2128                         fi
2129
2130                         # Scan local devices for the image
2131                         i=0
2132                         while [ "$i" -lt 60 ]
2133                         do
2134                                 livefs_root=$(find_livefs ${i})
2135
2136                                 if [ -n "${livefs_root}" ]
2137                                 then
2138                                         break
2139                                 fi
2140
2141                                 sleep 1
2142                                 i="$(($i + 1))"
2143                         done
2144                 fi
2145         fi
2146
2147         if [ -z "${livefs_root}" ]
2148         then
2149                 panic "Unable to find a medium containing a live file system"
2150         fi
2151
2152         if [ "${INTEGRITY_CHECK}" ]
2153         then
2154                 integrity_check "${livefs_root}"
2155         fi
2156
2157         if [ "${TORAM}" ]
2158         then
2159                 live_dest="ram"
2160         elif [ "${TODISK}" ]
2161         then
2162                 live_dest="${TODISK}"
2163         fi
2164
2165         if [ "${live_dest}" ]
2166         then
2167                 log_begin_msg "Copying live media to ${live_dest}"
2168                 copy_live_to "${livefs_root}" "${live_dest}"
2169                 log_end_msg
2170         fi
2171
2172         # if we do not unmount the ISO we can't run "fsck /dev/ice" later on
2173         # because the mountpoint is left behind in /proc/mounts, so let's get
2174         # rid of it when running from RAM
2175         if [ -n "$FROMISO" ] && [ "${TORAM}" ]
2176         then
2177           losetup -d /dev/loop0
2178           grep -q /isofrom /proc/mounts && umount /isofrom
2179         fi
2180
2181         if [ -n "${MODULETORAMFILE}" ] || [ -n "${PLAIN_ROOT}" ]
2182         then
2183                 setup_unionfs "${livefs_root}" "${rootmnt}"
2184         else
2185                 mac="$(get_mac)"
2186                 mac="$(echo ${mac} | sed 's/-//g')"
2187                 mount_images_in_directory "${livefs_root}" "${rootmnt}" "${mac}"
2188         fi
2189
2190
2191         if [ -n "${ROOT_PID}" ] ; then
2192                 echo "${ROOT_PID}" > "${rootmnt}"/live/root.pid
2193         fi
2194
2195         log_end_msg
2196
2197         # unionfs-fuse needs /dev to be bind-mounted for the duration of
2198         # live-bottom; udev's init script will take care of things after that
2199         if [ "${UNIONTYPE}" = unionfs-fuse ]
2200         then
2201                 mount -n -o bind /dev "${rootmnt}/dev"
2202         fi
2203
2204         # Move to the new root filesystem so that programs there can get at it.
2205         if [ ! -d /root/live/image ]
2206         then
2207                 mkdir -p /root/live/image
2208                 mount --move /live/image /root/live/image
2209         fi
2210
2211         # aufs2 in kernel versions around 2.6.33 has a regression:
2212         # directories can't be accessed when read for the first the time,
2213         # causing a failure for example when accessing /var/lib/fai
2214         # when booting FAI, this simple workaround solves it
2215         ls /root/* >/dev/null 2>&1
2216
2217         # copy snapshot configuration if exists
2218         if [ -f snapshot.conf ]
2219         then
2220                 log_begin_msg "Copying snapshot.conf to ${rootmnt}/etc/live/boot.d"
2221                 if [ ! -d "${rootmnt}/etc/live/boot.d" ]
2222                 then
2223                         mkdir -p "${rootmnt}/etc/live/boot.d"
2224                 fi
2225                 cp snapshot.conf "${rootmnt}/etc/live/boot.d/"
2226                 log_end_msg
2227         fi
2228
2229         if [ -f /etc/resolv.conf ] && [ ! -s ${rootmnt}/etc/resolv.conf ]
2230         then
2231                 log_begin_msg "Copying /etc/resolv.conf to ${rootmnt}/etc/resolv.conf"
2232                 cp -v /etc/resolv.conf ${rootmnt}/etc/resolv.conf
2233                 log_end_msg
2234         fi
2235
2236         maybe_break live-bottom
2237         log_begin_msg "Running /scripts/live-bottom\n"
2238
2239         run_scripts /scripts/live-bottom
2240         log_end_msg
2241
2242         if [ "${UNIONFS}" = unionfs-fuse ]
2243         then
2244                 umount "${rootmnt}/dev"
2245         fi
2246
2247         exec 1>&6 6>&-
2248         exec 2>&7 7>&-
2249         kill ${tailpid}
2250         [ -w "${rootmnt}/var/log/" ] && cp live-boot.log "${rootmnt}/var/log/" 2>/dev/null
2251 }