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