Really set limit on open files for unionfs-fuse
[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                                 echo "nameserver $i" >> /etc/resolv.conf
739                         done
740                 fi
741
742                 # Check if we have a network device at all
743                 if ! ls /sys/class/net/"$interface" > /dev/null 2>&1 && \
744                    ! ls /sys/class/net/eth0 > /dev/null 2>&1 && \
745                    ! ls /sys/class/net/wlan0 > /dev/null 2>&1 && \
746                    ! ls /sys/class/net/ath0 > /dev/null 2>&1 && \
747                    ! ls /sys/class/net/ra0 > /dev/null 2>&1
748                 then
749                         panic "No supported network device found, maybe a non-mainline driver is required."
750                 fi
751         done
752 }
753
754 do_netmount()
755 {
756         do_netsetup
757
758         if [ "${NFSROOT}" = "auto" ]
759         then
760                 NFSROOT=${ROOTSERVER}:${ROOTPATH}
761         fi
762
763         rc=1
764
765         if ( [ -n "${FETCH}" ] || [ -n "${HTTPFS}" ] || [ -n "${FTPFS}" ] )
766         then
767                 do_httpmount
768                 return $?
769         fi
770
771         if [ "${NFSROOT#*:}" = "${NFSROOT}" ] && [ "$NETBOOT" != "cifs" ]
772         then
773                 NFSROOT=${ROOTSERVER}:${NFSROOT}
774         fi
775
776         log_begin_msg "Trying netboot from ${NFSROOT}"
777
778         if [ "${NETBOOT}" != "nfs" ] && do_cifsmount
779         then
780                 rc=0
781         elif do_nfsmount
782         then
783                 NETBOOT="nfs"
784                 export NETBOOT
785                 rc=0
786         fi
787
788         log_end_msg
789         return ${rc}
790 }
791
792 do_iscsi()
793 {
794         do_netsetup
795         #modprobe ib_iser
796         modprobe iscsi_tcp
797         local debugopt=""
798         [ "${DEBUG}" == "Yes" ] && debugopt="-d 8"
799         #FIXME this name is supposed to be unique - some date + ifconfig hash?
800         ISCSI_INITIATORNAME="iqn.1993-08.org.debian.live:01:$(echo "${HWADDR}" | sed -e s/://g)"
801         export ISCSI_INITIATORNAME
802         if [ -n "${ISCSI_SERVER}" ] ; then
803                 iscsistart $debugopt -i "${ISCSI_INITIATORNAME}" -t "${ISCSI_TARGET}" -g 1 -a "${ISCSI_SERVER}" -p "${ISCSI_PORT}"
804         else
805                 iscsistart $debugopt -i "${ISCSI_INITIATORNAME}" -t "${ISCSI_TARGET}" -g 1 -a "${ISCSI_PORTAL}" -p 3260
806         fi
807         if [ $? != 0 ]
808         then
809                 panic "Failed to log into iscsi target"
810         fi
811         local host="$(ls -d /sys/class/scsi_host/host*/device/iscsi_host:host* \
812                             /sys/class/scsi_host/host*/device/iscsi_host/host* | sed -e 's:/device.*::' -e 's:.*host::')"
813         if [ -n "${host}" ]
814         then
815                 local devices=""
816                 local i=0
817                 while [ -z "${devices}" -a $i -lt 60 ]
818                 do
819                         sleep 1
820                         devices="$(ls -d /sys/class/scsi_device/${host}*/device/block:* \
821                                          /sys/class/scsi_device/${host}*/device/block/* | sed -e 's!.*[:/]!!')"
822                         i=$(expr $i + 1)
823                         echo -ne $i\\r
824                 done
825                 for dev in $devices
826                 do
827                         if check_dev "null" "/dev/$dev"
828                         then
829                                 NETBOOT="iscsi"
830                                 export NETBOOT
831                                 return 0;
832                         fi
833                 done
834                 panic "Failed to locate a live device on iSCSI devices (tried: $devices)."
835         else
836                 panic "Failed to locate iSCSI host in /sys"
837         fi
838 }
839
840 do_httpmount ()
841 {
842         rc=1
843
844         for webfile in HTTPFS FTPFS FETCH
845         do
846                 local url="$(eval echo \"\$\{${webfile}\}\")"
847                 local extension="$(echo "${url}" | sed 's/\(.*\)\.\(.*\)/\2/')"
848
849                 if [ -n "$url" ]
850                 then
851                         case "${extension}" in
852                                 iso|squashfs|tgz|tar)
853                                         if [ "${extension}" = "iso" ]
854                                         then
855                                                 mkdir -p "${alt_mountpoint}"
856                                                 dest="${alt_mountpoint}"
857                                         else
858                                                 local dest="${mountpoint}/${LIVE_MEDIA_PATH}"
859                                                 mount -t ramfs ram "${mountpoint}"
860                                                 mkdir -p "${dest}"
861                                         fi
862                                         if [ "${webfile}" = "FETCH" ]
863                                         then
864                                                 case "$url" in
865                                                         tftp*)
866                                                                 ip="$(dirname $url | sed -e 's|tftp://||g' -e 's|/.*$||g')"
867                                                                 rfile="$(echo $url | sed -e "s|tftp://$ip||g")"
868                                                                 lfile="$(basename $url)"
869                                                                 log_begin_msg "Trying tftp -g -b 10240 -r $rfile -l ${dest}/$lfile $ip"
870                                                                 tftp -g -b 10240 -r $rfile -l ${dest}/$lfile $ip
871                                                         ;;
872
873                                                         *)
874                                                                 log_begin_msg "Trying wget ${url} -O ${dest}/$(basename ${url})"
875                                                                 wget "${url}" -O "${dest}/$(basename ${url})"
876                                                                 ;;
877                                                 esac
878                                         else
879                                                 log_begin_msg "Trying to mount ${url} on ${dest}/$(basename ${url})"
880                                                 if [ "${webfile}" = "FTPFS" ]
881                                                 then
882                                                         FUSE_MOUNT="curlftpfs"
883                                                         url="$(dirname ${url})"
884                                                 else
885                                                         FUSE_MOUNT="httpfs"
886                                                 fi
887                                                 modprobe fuse
888                                                 $FUSE_MOUNT "${url}" "${dest}"
889                                                 ROOT_PID="$(minips h -C "$FUSE_MOUNT" | { read x y ; echo "$x" ; } )"
890                                         fi
891                                         [ ${?} -eq 0 ] && rc=0
892                                         [ "${extension}" = "tgz" ] && live_dest="ram"
893                                         if [ "${extension}" = "iso" ]
894                                         then
895                                                 isoloop=$(setup_loop "${dest}/$(basename "${url}")" "loop" "/sys/block/loop*" "" '')
896                                                 mount -t iso9660 "${isoloop}" "${mountpoint}"
897                                                 rc=${?}
898                                         fi
899                                         break
900                                         ;;
901
902                                 *)
903                                         log_begin_msg "Unrecognized archive extension for ${url}"
904                                         ;;
905                         esac
906                 fi
907         done
908
909         if [ ${rc} != 0 ]
910         then
911                 if [ -d "${alt_mountpoint}" ]
912                 then
913                         umount "${alt_mountpoint}"
914                         rmdir "${alt_mountpoint}"
915                 fi
916                 umount "${mountpoint}"
917         elif [ "${webfile}"  != "FETCH" ] ; then
918                 NETBOOT="${webfile}"
919                 export NETBOOT
920         fi
921
922         return ${rc}
923 }
924
925 do_nfsmount ()
926 {
927         rc=1
928
929         modprobe -q nfs
930
931         if [ -z "${NFSOPTS}" ]
932         then
933                 NFSOPTS=""
934         fi
935
936         log_begin_msg "Trying nfsmount -o nolock -o ro ${NFSOPTS} ${NFSROOT} ${mountpoint}"
937
938         # FIXME: This while loop is an ugly HACK round an nfs bug
939         i=0
940         while [ "$i" -lt 60 ]
941         do
942                 nfsmount -o nolock -o ro ${NFSOPTS} "${NFSROOT}" "${mountpoint}" && rc=0 && break
943                 sleep 1
944                 i="$(($i + 1))"
945         done
946
947         return ${rc}
948 }
949
950 do_cifsmount ()
951 {
952         rc=1
953
954         if [ -x "/sbin/mount.cifs" ]
955         then
956                 if [ -z "${NFSOPTS}" ]
957                 then
958                         CIFSOPTS="-ouser=root,password="
959                 else
960                         CIFSOPTS="${NFSOPTS}"
961                 fi
962
963                 log_begin_msg "Trying mount.cifs ${NFSROOT} ${mountpoint} ${CIFSOPTS}"
964                 modprobe -q cifs
965
966                 if mount.cifs "${NFSROOT}" "${mountpoint}" "${CIFSOPTS}"
967                 then
968                         rc=0
969                 fi
970         fi
971
972         return ${rc}
973 }
974
975 do_snap_copy ()
976 {
977         fromdev="${1}"
978         todir="${2}"
979         snap_type="${3}"
980         size=$(fs_size "${fromdev}" "" "used")
981
982         if [ -b "${fromdev}" ]
983         then
984                 log_success_msg "Copying snapshot ${fromdev} to ${todir}..."
985
986                 # look for free mem
987                 if [ -n "${HOMEMOUNTED}" -a "${snap_type}" = "HOME" ]
988                 then
989                         todev=$(awk -v pat="$(base_path ${todir})" '$2 == pat { print $1 }' /proc/mounts)
990                         freespace=$(df -k | awk '/'${todev}'/{print $4}')
991                 else
992                         freespace=$(awk '/^MemFree:/{f=$2} /^Cached:/{c=$2} END{print f+c}' /proc/meminfo)
993                 fi
994
995                 tomount="/mnt/tmpsnap"
996
997                 if [ ! -d "${tomount}" ]
998                 then
999                         mkdir -p "${tomount}"
1000                 fi
1001
1002                 fstype=$(get_fstype "${fromdev}")
1003
1004                 if [ -n "${fstype}" ]
1005                 then
1006                         # Copying stuff...
1007                         mount -o ro -t "${fstype}" "${fromdev}" "${tomount}" || log_warning_msg "Error in mount -t ${fstype} -o ro ${fromdev} ${tomount}"
1008                         cp -a "${tomount}"/* ${todir}
1009                         umount "${tomount}"
1010                 else
1011                         log_warning_msg "Unrecognized fstype: ${fstype} on ${fromdev}:${snap_type}"
1012                 fi
1013
1014                 rmdir "${tomount}"
1015
1016                 if echo ${fromdev} | grep -qs loop
1017                 then
1018                         losetup -d "${fromdev}"
1019                 fi
1020
1021                 return 0
1022         else
1023                 log_warning_msg "Unable to find the snapshot ${snap_type} medium"
1024                 return 1
1025         fi
1026 }
1027
1028 find_snap ()
1029 {
1030         # Look for ${snap_label}.* in block devices
1031         snap_label="${1}"
1032         black_listed_devices="${2}"
1033
1034         if [ "${PERSISTENT}" != "nofiles" ]
1035         then
1036                 # search for image files
1037                 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}")
1038         fi
1039
1040         if [ -z "${snapdata}" ]
1041         then
1042                 snapdata=$(find_cow_device "${snap_label}" "${black_listed_devices}")
1043         fi
1044         echo "${snapdata}"
1045 }
1046
1047 try_snap ()
1048 {
1049         # copy the contents of previously found snapshot to ${snap_mount}
1050         # and remember the device and filename for resync on exit in live-boot.init
1051
1052         snapdata="${1}"
1053         snap_mount="${2}"
1054         snap_type="${3}"
1055         snap_relpath="${4}"
1056
1057         if [ -z "${snap_relpath}" ]
1058         then
1059                 # root snapshot, default usage
1060                 snap_relpath="/"
1061         else
1062                 # relative snapshot (actually used just for "/home" snapshots)
1063                 snap_mount="${2}${snap_relpath}"
1064         fi
1065
1066         if [ -n "${snapdata}" ] && [ ! -b "${snapdata}" ]
1067         then
1068                 log_success_msg "found snapshot: ${snapdata}"
1069                 snapdev="$(echo ${snapdata} | cut -f1 -d ' ')"
1070                 snapback="$(echo ${snapdata} | cut -f2 -d ' ')"
1071                 snapfile="$(echo ${snapdata} | cut -f3 -d ' ')"
1072
1073                 if ! try_mount "${snapdev}" "${snapback}" "ro"
1074                 then
1075                         break
1076                 fi
1077
1078                 RES="0"
1079
1080                 if echo "${snapfile}" | grep -qs '\(squashfs\|ext2\|ext3\|ext4\|jffs2\)'
1081                 then
1082                         # squashfs, jffs2 or ext2/ext3/ext4 snapshot
1083                         dev=$(get_backing_device "${snapback}/${snapfile}")
1084
1085                         do_snap_copy "${dev}" "${snap_mount}" "${snap_type}"
1086                         RES="$?"
1087                 else
1088                         # cpio.gz snapshot
1089
1090                         # Unfortunately klibc's cpio is incompatible with the
1091                         # rest of the world; everything else requires -u -d,
1092                         # while klibc doesn't implement them. Try to detect
1093                         # whether it's in use.
1094                         cpiopath="$(which cpio)" || true
1095                         if [ "$cpiopath" ] && grep -aq /lib/klibc "$cpiopath"
1096                         then
1097                                 cpioargs=
1098                         else
1099                                 cpioargs='--unconditional --make-directories'
1100                         fi
1101
1102                         if [ -s "${snapback}/${snapfile}" ]
1103                         then
1104                                 BEFOREDIR="$(pwd)"
1105                                 cd "${snap_mount}" && zcat "${snapback}/${snapfile}" | $cpiopath $cpioargs --extract --preserve-modification-time --no-absolute-filenames --sparse 2>/dev/null
1106                                 RES="$?"
1107                                 cd "${BEFOREDIR}"
1108                         else
1109                                 log_warning_msg "${snapback}/${snapfile} is empty, adding it for sync on reboot."
1110                                 RES="0"
1111                         fi
1112
1113                         if [ "${RES}" != "0" ]
1114                         then
1115                                 log_warning_msg "failure to \"zcat ${snapback}/${snapfile} | $cpiopath $cpioargs --extract --preserve-modification-time --no-absolute-filenames --sparse\""
1116                         fi
1117                 fi
1118
1119                 umount "${snapback}" ||  log_warning_msg "failure to \"umount ${snapback}\""
1120
1121                 if [ "${RES}" != "0" ]
1122                 then
1123                         log_warning_msg "Impossible to include the ${snapfile} Snapshot file"
1124                 fi
1125
1126         elif [ -b "${snapdata}" ]
1127         then
1128                 # Try to find if it could be a snapshot partition
1129                 dev="${snapdata}"
1130                 log_success_msg "found snapshot ${snap_type} device on ${dev}"
1131                 if echo "${dev}" | grep -qs loop
1132                 then
1133                         # strange things happens, user confused?
1134                         snaploop=$( losetup ${dev} | awk '{print $3}' | tr -d '()' )
1135                         snapfile=$(basename ${snaploop})
1136                         snapdev=$(awk -v pat="$( dirname ${snaploop})" '$2 == pat { print $1 }' /proc/mounts)
1137                 else
1138                         snapdev="${dev}"
1139                 fi
1140
1141                 if ! do_snap_copy "${dev}" "${snap_mount}" "${snap_type}"
1142                 then
1143                         log_warning_msg "Impossible to include the ${snap_type} Snapshot (i)"
1144                         return 1
1145                 else
1146                         if [ -n "${snapfile}" ]
1147                         then
1148                                 # it was a loop device, user confused
1149                                 umount ${snapdev}
1150                         fi
1151                 fi
1152         else
1153                 log_warning_msg "Impossible to include the ${snap_type} Snapshot (o)"
1154                 return 1
1155         fi
1156
1157         echo "export ${snap_type}SNAP=${snap_relpath}:${snapdev}:${snapfile}" >> snapshot.conf # for resync on reboot/halt
1158         return 0
1159 }
1160
1161 setup_unionfs ()
1162 {
1163         image_directory="${1}"
1164         rootmnt="${2}"
1165         addimage_directory="${3}"
1166
1167         case ${UNIONTYPE} in
1168                 aufs|unionfs)
1169                         modprobe -q -b ${UNIONTYPE}
1170
1171                         if ! cut -f2 /proc/filesystems | grep -q "^${UNIONTYPE}\$" && [ -x /bin/unionfs-fuse ]
1172                         then
1173                                 echo "${UNIONTYPE} not available, falling back to unionfs-fuse."
1174                                 echo "This might be really slow."
1175
1176                                 UNIONTYPE="unionfs-fuse"
1177                         fi
1178                         ;;
1179         esac
1180
1181         if [ "${UNIONTYPE}" = unionfs-fuse ]
1182         then
1183                 modprobe fuse
1184         fi
1185
1186         # run-init can't deal with images in a subdir, but we're going to
1187         # move all of these away before it runs anyway.  No, we're not,
1188         # put them in / since move-mounting them into / breaks mono and
1189         # some other apps.
1190
1191         croot="/"
1192
1193         # Let's just mount the read-only file systems first
1194         rofslist=""
1195
1196         if [ "${UNIONTYPE}" = "aufs" ]
1197         then
1198                 roopt="rr"
1199                 noxino_opt="noxino,"
1200         elif [ "${UNIONTYPE}" = "unionfs-fuse" ]
1201         then
1202                 roopt="RO"
1203         else
1204                 roopt="ro"
1205         fi
1206
1207         if [ -z "${PLAIN_ROOT}" ]
1208         then
1209                 # Read image names from ${MODULE}.module if it exists
1210                 if [ -e "${image_directory}/filesystem.${MODULE}.module" ]
1211                 then
1212                         for IMAGE in $(cat ${image_directory}/filesystem.${MODULE}.module)
1213                         do
1214                                 image_string="${image_string} ${image_directory}/${IMAGE}"
1215                         done
1216                 elif [ -e "${image_directory}/${MODULE}.module" ]
1217                 then
1218                         for IMAGE in $(cat ${image_directory}/${MODULE}.module)
1219                         do
1220                                 image_string="${image_string} ${image_directory}/${IMAGE}"
1221                         done
1222                 else
1223                         # ${MODULE}.module does not exist, create a list of images
1224                         for FILESYSTEM in squashfs ext2 ext3 ext4 xfs jffs2 dir
1225                         do
1226                                 for IMAGE in "${image_directory}"/*."${FILESYSTEM}"
1227                                 do
1228                                         if [ -e "${IMAGE}" ]
1229                                         then
1230                                                 image_string="${image_string} ${IMAGE}"
1231                                         fi
1232                                 done
1233                         done
1234
1235                         if [ -n "${addimage_directory}" ] && [ -d "${addimage_directory}" ]
1236                         then
1237                                 for FILESYSTEM in squashfs ext2 ext3 ext4 xfs jffs2 dir
1238                                 do
1239                                         for IMAGE in "${addimage_directory}"/*."${FILESYSTEM}"
1240                                         do
1241                                                 if [ -e "${IMAGE}" ]
1242                                                 then
1243                                                         image_string="${image_string} ${IMAGE}"
1244                                                 fi
1245                                         done
1246                                 done
1247                         fi
1248
1249                         # Now sort the list
1250                         image_string="$(echo ${image_string} | sed -e 's/ /\n/g' | sort )"
1251                 fi
1252
1253                 [ -n "${MODULETORAMFILE}" ] && image_string="${image_directory}/$(basename ${MODULETORAMFILE})"
1254
1255                 mkdir -p "${croot}"
1256
1257                 for image in ${image_string}
1258                 do
1259                         imagename=$(basename "${image}")
1260
1261                         export image devname
1262                         maybe_break live-realpremount
1263                         log_begin_msg "Running /scripts/live-realpremount"
1264                         run_scripts /scripts/live-realpremount
1265                         log_end_msg
1266
1267                         if [ -d "${image}" ]
1268                         then
1269                                 # it is a plain directory: do nothing
1270                                 rofslist="${image} ${rofslist}"
1271                         elif [ -f "${image}" ]
1272                         then
1273                                 if losetup --help 2>&1 | grep -q -- "-r\b"
1274                                 then
1275                                         backdev=$(get_backing_device "${image}" "-r")
1276                                 else
1277                                         backdev=$(get_backing_device "${image}")
1278                                 fi
1279                                 fstype=$(get_fstype "${backdev}")
1280
1281                                 if [ "${fstype}" = "unknown" ]
1282                                 then
1283                                         panic "Unknown file system type on ${backdev} (${image})"
1284                                 fi
1285
1286                                 if [ -z "${fstype}" ]
1287                                 then
1288                                         fstype="${imagename##*.}"
1289                                         log_warning_msg "Unknown file system type on ${backdev} (${image}), assuming ${fstype}."
1290                                 fi
1291
1292                                 if [ "${UNIONTYPE}" != "unionmount" ]
1293                                 then
1294                                         mpoint="${croot}/${imagename}"
1295                                         rofslist="${mpoint} ${rofslist}"
1296                                 else
1297                                         mpoint="${rootmnt}"
1298                                         rofslist="${rootmnt} ${rofslist}"
1299                                 fi
1300                                 mkdir -p "${mpoint}"
1301                                 log_begin_msg "Mounting \"${image}\" on \"${mpoint}\" via \"${backdev}\""
1302                                 mount -t "${fstype}" -o ro,noatime "${backdev}" "${mpoint}" || panic "Can not mount ${backdev} (${image}) on ${mpoint}"
1303                                 log_end_msg
1304                         fi
1305                 done
1306         else
1307                 # we have a plain root system
1308                 mkdir -p "${croot}/filesystem"
1309                 log_begin_msg "Mounting \"${image_directory}\" on \"${croot}/filesystem\""
1310                 mount -t $(get_fstype "${image_directory}") -o ro,noatime "${image_directory}" "${croot}/filesystem" || \
1311                         panic "Can not mount ${image_directory} on ${croot}/filesystem" && \
1312                         rofslist="${croot}/filesystem ${rofslist}"
1313                 # probably broken:
1314                 mount -o bind ${croot}/filesystem $mountpoint
1315                 log_end_msg
1316         fi
1317
1318         mkdir -p /cow
1319
1320         # Looking for "${root_persistence}" device or file
1321         if [ -n "${PERSISTENT}" ] && [ -z "${NOPERSISTENT}" ]
1322         then
1323                 if [ -z "${QUICKUSBMODULES}" ]
1324                 then
1325                         # Load USB modules
1326                         num_block=$(ls -l /sys/block | wc -l)
1327                         for module in sd_mod uhci-hcd ehci-hcd ohci-hcd usb-storage
1328                         do
1329                                 modprobe -q -b ${module}
1330                         done
1331
1332                         udevadm trigger
1333                         udevadm settle
1334
1335                         # For some reason, udevsettle does not block in this scenario,
1336                         # so we sleep for a little while.
1337                         #
1338                         # See https://bugs.launchpad.net/ubuntu/+source/casper/+bug/84591
1339                         for timeout in 5 4 3 2 1
1340                         do
1341                                 sleep 1
1342
1343                                 if [ $(ls -l /sys/block | wc -l) -gt ${num_block} ]
1344                                 then
1345                                         break
1346                                 fi
1347                         done
1348                 fi
1349
1350                 # search for label and files (this could be hugely optimized)
1351                 cowprobe=$(find_cow_device "${root_persistence}")
1352                 if [ -b "${cowprobe}" ]
1353                 then
1354                         # Blacklist /cow device, to avoid inconsistent setups for overlapping snapshots
1355                         # makes sense to have both persistence for /cow and /home mounted, maybe also with
1356                         # snapshots to be sure to really store some e.g key config files,
1357                         # but not on the same media
1358                         blacklistdev="${cowprobe}"
1359                         PERSISTENCE_IS_ON="1"
1360                         export PERSISTENCE_IS_ON
1361                 fi
1362                 # homecow just mount something on /home, this should be generalized some way
1363                 homecow=$(find_cow_device "${home_persistence}" "${blacklistdev}")
1364                 if [ -b "${homecow}" ]
1365                 then
1366                         PERSISTENCE_IS_ON="1"
1367                         export PERSISTENCE_IS_ON
1368                 fi
1369                 root_snapdata=$(find_snap "${root_snapshot_label}" "${blacklistdev}")
1370                 # This second type should be removed when snapshot will get smarter,
1371                 # hence when "/etc/live-snapshot*list" will be supported also by
1372                 # ext2|ext3|ext4|jffs2 snapshot types.
1373                 home_snapdata=$(find_snap "${home_snapshot_label}" "${blacklistdev}")
1374
1375                 if [ -b "${cowprobe}" ]
1376                 then
1377                         cowdevice=${cowprobe}
1378                         cow_fstype=$(get_fstype "${cowprobe}")
1379                         cow_mountopt="rw,noatime"
1380
1381                         if [ "${FORCEPERSISTENTFSCK}" = "Yes" ]
1382                         then
1383                                 fsck -y ${cowdevice}
1384                         fi
1385                 else
1386                         log_warning_msg "Unable to find the persistent medium"
1387                         cowdevice="tmpfs"
1388                         cow_fstype="tmpfs"
1389                         cow_mountopt="rw,noatime,mode=755"
1390                 fi
1391         elif [ -n "${NFS_COW}" ] && [ -z "${NOPERSISTENT}" ]
1392         then
1393                 # check if there are any nfs options
1394                 if echo ${NFS_COW}|grep -q ','
1395                 then
1396                         nfs_cow_opts="-o nolock,$(echo ${NFS_COW}|cut -d, -f2-)"
1397                         nfs_cow=$(echo ${NFS_COW}|cut -d, -f1)
1398                 else
1399                         nfs_cow_opts="-o nolock"
1400                         nfs_cow=${NFS_COW}
1401                 fi
1402                 mac="$(get_mac)"
1403                 if [ -n "${mac}" ]
1404                 then
1405                         cowdevice=$(echo ${nfs_cow}|sed "s/client_mac_address/${mac}/")
1406                         cow_fstype="nfs"
1407                 else
1408                         panic "unable to determine mac address"
1409                 fi
1410         else
1411                 cowdevice="tmpfs"
1412                 cow_fstype="tmpfs"
1413                 cow_mountopt="rw,noatime,mode=755"
1414         fi
1415
1416         if [ "${UNIONTYPE}" != "unionmount" ]
1417         then
1418
1419                 if [ "${cow_fstype}" = "nfs" ]
1420                 then
1421                         log_begin_msg \
1422                                 "Trying nfsmount ${nfs_cow_opts} ${cowdevice} /cow"
1423                         nfsmount ${nfs_cow_opts} ${cowdevice} /cow || \
1424                                 panic "Can not mount ${cowdevice} (n: ${cow_fstype}) on /cow"
1425                 else
1426                         mount -t ${cow_fstype} -o ${cow_mountopt} ${cowdevice} /cow || \
1427                                 panic "Can not mount ${cowdevice} (o: ${cow_fstype}) on /cow"
1428                 fi
1429         fi
1430
1431         rofscount=$(echo ${rofslist} |wc -w)
1432
1433         if [ ${rofscount} -ne 1 ]
1434         then
1435                 panic "only one RO file system supported with exposedroot: ${rofslist}"
1436         fi
1437         rofs=${rofslist%% }
1438
1439         if [ -n "${EXPOSED_ROOT}" ]
1440         then
1441                 mount --bind ${rofs} ${rootmnt} || \
1442                         panic "bind mount of ${rofs} failed"
1443
1444                 if [ -z "${SKIP_UNION_MOUNTS}" ]
1445                 then
1446                         cow_dirs='/var/tmp /var/lock /var/run /var/log /var/spool /home /var/lib/live'
1447                 else
1448                         cow_dirs=''
1449                 fi
1450         else
1451                 cow_dirs="/"
1452         fi
1453
1454         if [ "${cow_fstype}" != "tmpfs" ] && [ "${cow_dirs}" != "/" ] && [ "${UNIONTYPE}" = "unionmount" ]
1455         then
1456                 true # FIXME: Maybe it does, I don't really know.
1457                 #panic "unionmount does not support subunions (${cow_dirs})."
1458         fi
1459
1460         unionmountopts=""
1461         unionmountpoint=""
1462
1463         for dir in ${cow_dirs}; do
1464                 mkdir -p /cow${dir}
1465
1466                 unionmountpoint="${rootmnt}${dir}"
1467                 unionrw="/cow${dir}"
1468                 unionro="${rofs}${dir}"
1469                 # We don't handle spaces and other junk gracefully here, hopefully not needed.
1470                 case "${UNIONTYPE}" in
1471                         unionfs-fuse)
1472                                 unionmountopts="-o cow -o noinitgroups -o default_permissions -o allow_other -o use_ino -o suid"
1473                                 unionmountopts="${unionmountopts} ${unionrw}=RW:${unionro}=RO"
1474                                 ( sysctl -w fs.file-max=391524 ; ulimit -HSn 16384
1475                                 unionfs-fuse ${unionmountopts} "${unionmountpoint}" ) && \
1476                                         ( mkdir -p /dev/.initramfs/varrun
1477                                 pidof unionfs-fuse >> /dev/.initramfs/varrun/sendsigs.omit || true )
1478                                 ;;
1479
1480                         unionmount)
1481                                 unionmountopts="-t ${cow_fstype} -o noatime,union,${cow_mountopt} ${cowdevice}"
1482                                 mount_full $unionmountopts "${unionmountpoint}"
1483                                 ;;
1484
1485
1486                         *)
1487                                 unionmountopts="-o noatime,${noxino_opt}dirs=${unionrw}=rw:${unionro}=${roopt}"
1488                                 mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
1489                                 ;;
1490                 esac || \
1491                         panic "mount ${UNIONTYPE} on ${unionmountpoint} failed with option ${unionmountopts}"
1492         done
1493
1494         # Correct the permissions of /:
1495         chmod 0755 "${rootmnt}"
1496
1497         # tmpfs file systems
1498         touch /etc/fstab
1499         mkdir -p "${rootmnt}/live"
1500         mount -t tmpfs tmpfs ${rootmnt}/live
1501
1502         # Adding other custom mounts
1503         if [ -n "${PERSISTENT}" ] && [ -z "${NOPERSISTENT}" ]
1504         then
1505                 # directly mount /home
1506                 # FIXME: add a custom mounts configurable system
1507
1508                 if [ -b "${homecow}" ]
1509                 then
1510                         mount -t $(get_fstype "${homecow}") -o rw,noatime "${homecow}" "${rootmnt}/home"
1511                         export HOMEMOUNTED=1 # used to proper calculate free space in do_snap_copy()
1512                 else
1513                         log_warning_msg "Unable to find the persistent home medium"
1514                 fi
1515
1516                 # Look for other snapshots to copy in
1517                 try_snap "${root_snapdata}" "${rootmnt}" "ROOT"
1518                 # This second type should be removed when snapshot grow smarter
1519                 try_snap "${home_snapdata}" "${rootmnt}" "HOME" "/home"
1520         fi
1521
1522         if [ -n "${SHOWMOUNTS}" ]
1523         then
1524                 for d in ${rofslist}
1525                 do
1526                         mkdir -p "${rootmnt}/live/${d##*/}"
1527
1528                         case d in
1529                                 *.dir)
1530                                         # do nothing # mount -o bind "${d}" "${rootmnt}/live/${d##*/}"
1531                                         ;;
1532
1533                                 *)
1534                                         case "${UNIONTYPE}" in
1535                                                 unionfs-fuse)
1536                                                         mount -o bind "${d}" "${rootmnt}/live/${d##*/}"
1537                                                         ;;
1538
1539                                                 *)
1540                                                         mount -o move "${d}" "${rootmnt}/live/${d##*/}"
1541                                                         ;;
1542                                         esac
1543                                         ;;
1544                         esac
1545                 done
1546         fi
1547
1548         # shows cow fs on /cow for use by live-snapshot
1549         mkdir -p "${rootmnt}/live/cow"
1550         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"
1551 }
1552
1553 check_dev ()
1554 {
1555         sysdev="${1}"
1556         devname="${2}"
1557         skip_uuid_check="${3}"
1558
1559         # support for fromiso=.../isofrom=....
1560         if [ -n "$FROMISO" ]
1561         then
1562                 ISO_DEVICE=$(dirname $FROMISO)
1563                 if ! [ -b $ISO_DEVICE ]
1564                 then
1565                         # to support unusual device names like /dev/cciss/c0d0p1
1566                         # as well we have to identify the block device name, let's
1567                         # do that for up to 15 levels
1568                         i=15
1569                         while [ -n "$ISO_DEVICE" ] && [ "$i" -gt 0 ]
1570                         do
1571                                 ISO_DEVICE=$(dirname ${ISO_DEVICE})
1572                                 [ -b "$ISO_DEVICE" ] && break
1573                                 i=$(($i -1))
1574                         done
1575                 fi
1576
1577                 if [ "$ISO_DEVICE" = "/" ]
1578                 then
1579                         echo "Warning: device for bootoption isofrom= ($FROMISO) not found.">>/live-boot.log
1580                 else
1581                         mkdir /isofrom
1582                         mount -t auto "$ISO_DEVICE" /isofrom
1583                         ISO_NAME="$(echo $FROMISO | sed "s|$ISO_DEVICE||")"
1584                         loopdevname=$(setup_loop "/isofrom/${ISO_NAME}" "loop" "/sys/block/loop*" "" '')
1585                         devname="${loopdevname}"
1586                 fi
1587         fi
1588
1589         if [ -z "${devname}" ]
1590         then
1591                 devname=$(sys2dev "${sysdev}")
1592         fi
1593
1594         if [ -d "${devname}" ]
1595         then
1596                 mount -o bind "${devname}" $mountpoint || continue
1597
1598                 if is_live_path $mountpoint
1599                 then
1600                         echo $mountpoint
1601                         return 0
1602                 else
1603                         umount $mountpoint
1604                 fi
1605         fi
1606         [ -e "$devname" ] || continue
1607
1608         if [ -n "${LIVE_MEDIA_OFFSET}" ]
1609         then
1610                 loopdevname=$(setup_loop "${devname}" "loop" "/sys/block/loop*" "${LIVE_MEDIA_OFFSET}" '')
1611                 devname="${loopdevname}"
1612         fi
1613
1614         fstype=$(get_fstype "${devname}")
1615
1616         if is_supported_fs ${fstype}
1617         then
1618                 devuid=$(blkid -o value -s UUID "$devname")
1619                 [ -n "$devuid" ] && grep -qs "\<$devuid\>" $tried && continue
1620                 mount -t ${fstype} -o ro,noatime "${devname}" ${mountpoint} || continue
1621                 [ -n "$devuid" ] && echo "$devuid" >> $tried
1622
1623                 if is_live_path ${mountpoint} && \
1624                         ([ "${skip_uuid_check}" ] || matches_uuid ${mountpoint})
1625                 then
1626                         echo ${mountpoint}
1627                         return 0
1628                 else
1629                         umount ${mountpoint}
1630                 fi
1631         fi
1632
1633         if [ -n "${LIVE_MEDIA_OFFSET}" ]
1634         then
1635                 losetup -d "${loopdevname}"
1636         fi
1637
1638         return 1
1639 }
1640
1641 find_livefs ()
1642 {
1643         timeout="${1}"
1644
1645         # don't start autodetection before timeout has expired
1646         if [ -n "${LIVE_MEDIA_TIMEOUT}" ]
1647         then
1648                 if [ "${timeout}" -lt "${LIVE_MEDIA_TIMEOUT}" ]
1649                 then
1650                         return 1
1651                 fi
1652         fi
1653
1654         # first look at the one specified in the command line
1655         case "${LIVE_MEDIA}" in
1656                 removable-usb)
1657                         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1658                         do
1659                                 if [ "$(cat ${sysblock}/removable)" = "1" ]
1660                                 then
1661                                         if readlink ${sysblock} | grep -q usb
1662                                         then
1663                                                 for dev in $(subdevices "${sysblock}")
1664                                                 do
1665                                                         if check_dev "${dev}"
1666                                                         then
1667                                                                 return 0
1668                                                         fi
1669                                                 done
1670                                         fi
1671                                 fi
1672                         done
1673                         return 1
1674                         ;;
1675
1676                 removable)
1677                         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1678                         do
1679                                 if [ "$(cat ${sysblock}/removable)" = "1" ]
1680                                 then
1681                                         for dev in $(subdevices "${sysblock}")
1682                                         do
1683                                                 if check_dev "${dev}"
1684                                                 then
1685                                                         return 0
1686                                                 fi
1687                                         done
1688                                 fi
1689                         done
1690                         return 1
1691                         ;;
1692
1693                 *)
1694                         if [ ! -z "${LIVE_MEDIA}" ]
1695                         then
1696                                 if check_dev "null" "${LIVE_MEDIA}" "skip_uuid_check"
1697                                 then
1698                                         return 0
1699                                 fi
1700                         fi
1701                         ;;
1702         esac
1703
1704         # or do the scan of block devices
1705         # prefer removable devices over non-removable devices, so scan them first
1706         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | egrep -v "/(loop|ram|dm-|fd)")
1707         do
1708                 if [ "$(cat ${sysblock}/removable)" = "1" ]
1709                 then
1710                         removable_devices_to_scan="$removable_devices_to_scan $sysblock"
1711                 else
1712                         nonremovable_devices_to_scan="$nonremovable_devices_to_scan $sysblock"
1713                 fi
1714         done
1715         devices_to_scan="$removable_devices_to_scan $nonremovable_devices_to_scan"
1716
1717         for sysblock in $devices_to_scan
1718         do
1719                 devname=$(sys2dev "${sysblock}")
1720                 [ -e "$devname" ] || continue
1721                 fstype=$(get_fstype "${devname}")
1722
1723                 if /lib/udev/cdrom_id ${devname} > /dev/null
1724                 then
1725                         if check_dev "null" "${devname}"
1726                         then
1727                                 return 0
1728                         fi
1729                 elif is_nice_device "${sysblock}"
1730                 then
1731                         for dev in $(subdevices "${sysblock}")
1732                         do
1733                                 if check_dev "${dev}"
1734                                 then
1735                                         return 0
1736                                 fi
1737                         done
1738                 elif [ "${fstype}" = "squashfs" -o \
1739                         "${fstype}" = "btrfs" -o \
1740                         "${fstype}" = "ext2" -o \
1741                         "${fstype}" = "ext3" -o \
1742                         "${fstype}" = "ext4" -o \
1743                         "${fstype}" = "jffs2" ]
1744                 then
1745                         # This is an ugly hack situation, the block device has
1746                         # an image directly on it.  It's hopefully
1747                         # live-boot, so take it and run with it.
1748                         ln -s "${devname}" "${devname}.${fstype}"
1749                         echo "${devname}.${fstype}"
1750                         return 0
1751                 fi
1752         done
1753
1754         return 1
1755 }
1756
1757 integrity_check ()
1758 {
1759         media_mountpoint="${1}"
1760
1761         log_begin_msg "Checking media integrity"
1762
1763         cd ${media_mountpoint}
1764         /bin/md5sum -c md5sum.txt < /dev/tty8 > /dev/tty8
1765         RC="${?}"
1766
1767         log_end_msg
1768
1769         if [ "${RC}" -eq 0 ]
1770         then
1771                 log_success_msg "Everything ok, will reboot in 10 seconds."
1772                 sleep 10
1773                 cd /
1774                 umount ${media_mountpoint}
1775                 sync
1776                 echo u > /proc/sysrq-trigger
1777                 echo b > /proc/sysrq-trigger
1778         else
1779                 panic "Not ok, a media defect is likely, switch to VT8 for details."
1780         fi
1781 }
1782
1783 mountroot ()
1784 {
1785         if [ -x /scripts/local-top/cryptroot ]; then
1786             /scripts/local-top/cryptroot
1787         fi
1788
1789         exec 6>&1
1790         exec 7>&2
1791         exec > live-boot.log
1792         exec 2>&1
1793         tail -f live-boot.log >&7 &
1794         tailpid="${!}"
1795
1796         # Ensure 'panic' function is overridden
1797         . /scripts/live-functions
1798
1799         Arguments
1800
1801         maybe_break live-premount
1802         log_begin_msg "Running /scripts/live-premount"
1803         run_scripts /scripts/live-premount
1804         log_end_msg
1805
1806         # Needed here too because some things (*cough* udev *cough*)
1807         # changes the timeout
1808
1809         if [ ! -z "${NETBOOT}" ] || [ ! -z "${FETCH}" ] || [ ! -z "${HTTPFS}" ] || [ ! -z "${FTPFS}" ]
1810         then
1811                 if do_netmount
1812                 then
1813                         livefs_root="${mountpoint}"
1814                 else
1815                         panic "Unable to find a live file system on the network"
1816                 fi
1817         else
1818                 if [ -n "${ISCSI_PORTAL}" ]
1819                 then
1820                         do_iscsi && livefs_root="${mountpoint}"
1821                 elif [ -n "${PLAIN_ROOT}" ] && [ -n "${ROOT}" ]
1822                 then
1823                         # Do a local boot from hd
1824                         livefs_root=${ROOT}
1825                 else
1826                         if [ -x /usr/bin/memdiskfind ]
1827                         then
1828                                 MEMDISK=$(/usr/bin/memdiskfind)
1829
1830                                 if [ $? -eq 0 ]
1831                                 then
1832                                         # We found a memdisk, set up phram
1833                                         modprobe phram phram=memdisk,${MEMDISK}
1834
1835                                         # Load mtdblock, the memdisk will be /dev/mtdblock0
1836                                         modprobe mtdblock
1837                                 fi
1838                         fi
1839
1840                         # Scan local devices for the image
1841                         i=0
1842                         while [ "$i" -lt 60 ]
1843                         do
1844                                 livefs_root=$(find_livefs ${i})
1845
1846                                 if [ -n "${livefs_root}" ]
1847                                 then
1848                                         break
1849                                 fi
1850
1851                                 sleep 1
1852                                 i="$(($i + 1))"
1853                         done
1854                 fi
1855         fi
1856
1857         if [ -z "${livefs_root}" ]
1858         then
1859                 panic "Unable to find a medium containing a live file system"
1860         fi
1861
1862         if [ "${INTEGRITY_CHECK}" ]
1863         then
1864                 integrity_check "${livefs_root}"
1865         fi
1866
1867         if [ "${TORAM}" ]
1868         then
1869                 live_dest="ram"
1870         elif [ "${TODISK}" ]
1871         then
1872                 live_dest="${TODISK}"
1873         fi
1874
1875         if [ "${live_dest}" ]
1876         then
1877                 log_begin_msg "Copying live media to ${live_dest}"
1878                 copy_live_to "${livefs_root}" "${live_dest}"
1879                 log_end_msg
1880         fi
1881
1882         # if we do not unmount the ISO we can't run "fsck /dev/ice" later on
1883         # because the mountpoint is left behind in /proc/mounts, so let's get
1884         # rid of it when running from RAM
1885         if [ -n "$FROMISO" ] && [ "${TORAM}" ]
1886         then
1887           losetup -d /dev/loop0
1888           grep -q /isofrom /proc/mounts && umount /isofrom
1889         fi
1890
1891         if [ -n "${MODULETORAMFILE}" ] || [ -n "${PLAIN_ROOT}" ]
1892         then
1893                 setup_unionfs "${livefs_root}" "${rootmnt}"
1894         else
1895                 mac="$(get_mac)"
1896                 mac="$(echo ${mac} | sed 's/-//g')"
1897                 mount_images_in_directory "${livefs_root}" "${rootmnt}" "${mac}"
1898         fi
1899
1900
1901         if [ -n "${ROOT_PID}" ] ; then
1902                 echo "${ROOT_PID}" > "${rootmnt}"/live/root.pid
1903         fi
1904
1905         log_end_msg
1906
1907         # unionfs-fuse needs /dev to be bind-mounted for the duration of
1908         # live-bottom; udev's init script will take care of things after that
1909         if [ "${UNIONTYPE}" = unionfs-fuse ]
1910         then
1911                 mount -n -o bind /dev "${rootmnt}/dev"
1912         fi
1913
1914         # Move to the new root filesystem so that programs there can get at it.
1915         if [ ! -d /root/live/image ]
1916         then
1917                 mkdir -p /root/live/image
1918                 mount --move /live/image /root/live/image
1919         fi
1920
1921         # aufs2 in kernel versions around 2.6.33 has a regression:
1922         # directories can't be accessed when read for the first the time,
1923         # causing a failure for example when accessing /var/lib/fai
1924         # when booting FAI, this simple workaround solves it
1925         ls /root/* >/dev/null 2>&1
1926
1927         # copy snapshot configuration if exists
1928         if [ -f snapshot.conf ]
1929         then
1930                 log_begin_msg "Copying snapshot.conf to ${rootmnt}/etc/live/boot.d"
1931                 if [ ! -d "${rootmnt}/etc/live/boot.d" ]
1932                 then
1933                         mkdir -p "${rootmnt}/etc/live/boot.d"
1934                 fi
1935                 cp snapshot.conf "${rootmnt}/etc/live/boot.d/"
1936                 log_end_msg
1937         fi
1938
1939         maybe_break live-bottom
1940         log_begin_msg "Running /scripts/live-bottom\n"
1941
1942         run_scripts /scripts/live-bottom
1943         log_end_msg
1944
1945         if [ "${UNIONFS}" = unionfs-fuse ]
1946         then
1947                 umount "${rootmnt}/dev"
1948         fi
1949
1950         exec 1>&6 6>&-
1951         exec 2>&7 7>&-
1952         kill ${tailpid}
1953         [ -w "${rootmnt}/var/log/" ] && cp live-boot.log "${rootmnt}/var/log/" 2>/dev/null
1954 }