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