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