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