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