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