ef87d3c4f2a0c8232e43adfa8851e56cbecce192
[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         rofsstring=""
1195         rofslist=""
1196
1197         if [ "${UNIONTYPE}" = "aufs" ]
1198         then
1199                 roopt="rr"
1200                 noxino_opt="noxino,"
1201         elif [ "${UNIONTYPE}" = "unionfs-fuse" ]
1202         then
1203                 roopt="RO"
1204         else
1205                 roopt="ro"
1206         fi
1207
1208         if [ -z "${PLAIN_ROOT}" ]
1209         then
1210                 # Read image names from ${MODULE}.module if it exists
1211                 if [ -e "${image_directory}/filesystem.${MODULE}.module" ]
1212                 then
1213                         for IMAGE in $(cat ${image_directory}/filesystem.${MODULE}.module)
1214                         do
1215                                 image_string="${image_string} ${image_directory}/${IMAGE}"
1216                         done
1217                 elif [ -e "${image_directory}/${MODULE}.module" ]
1218                 then
1219                         for IMAGE in $(cat ${image_directory}/${MODULE}.module)
1220                         do
1221                                 image_string="${image_string} ${image_directory}/${IMAGE}"
1222                         done
1223                 else
1224                         # ${MODULE}.module does not exist, create a list of images
1225                         for FILESYSTEM in squashfs ext2 ext3 ext4 xfs jffs2 dir
1226                         do
1227                                 for IMAGE in "${image_directory}"/*."${FILESYSTEM}"
1228                                 do
1229                                         if [ -e "${IMAGE}" ]
1230                                         then
1231                                                 image_string="${image_string} ${IMAGE}"
1232                                         fi
1233                                 done
1234                         done
1235
1236                         if [ -n "${addimage_directory}" ] && [ -d "${addimage_directory}" ]
1237                         then
1238                                 for FILESYSTEM in squashfs ext2 ext3 ext4 xfs jffs2 dir
1239                                 do
1240                                         for IMAGE in "${addimage_directory}"/*."${FILESYSTEM}"
1241                                         do
1242                                                 if [ -e "${IMAGE}" ]
1243                                                 then
1244                                                         image_string="${image_string} ${IMAGE}"
1245                                                 fi
1246                                         done
1247                                 done
1248                         fi
1249
1250                         # Now sort the list
1251                         image_string="$(echo ${image_string} | sed -e 's/ /\n/g' | sort )"
1252                 fi
1253
1254         [ -n "${MODULETORAMFILE}" ] && image_string="${image_directory}/$(basename ${MODULETORAMFILE})"
1255
1256                 mkdir -p "${croot}"
1257
1258                 for image in ${image_string}
1259                 do
1260                         imagename=$(basename "${image}")
1261
1262                         export image devname
1263                         maybe_break live-realpremount
1264                         log_begin_msg "Running /scripts/live-realpremount"
1265                         run_scripts /scripts/live-realpremount
1266                         log_end_msg
1267
1268                         if [ -d "${image}" ]
1269                         then
1270                                 # it is a plain directory: do nothing
1271                                 rofsstring="${image}=${roopt}:${rofsstring}"
1272                                 rofslist="${image} ${rofslist}"
1273                         elif [ -f "${image}" ]
1274                         then
1275                                 if losetup --help 2>&1 | grep -q -- "-r\b"
1276                                 then
1277                                         backdev=$(get_backing_device "${image}" "-r")
1278                                 else
1279                                         backdev=$(get_backing_device "${image}")
1280                                 fi
1281                                 fstype=$(get_fstype "${backdev}")
1282
1283                                 if [ "${fstype}" = "unknown" ]
1284                                 then
1285                                         panic "Unknown file system type on ${backdev} (${image})"
1286                                 fi
1287
1288                                 if [ -z "${fstype}" ]
1289                                 then
1290                                         fstype="${imagename##*.}"
1291                                         log_warning_msg "Unknown file system type on ${backdev} (${image}), assuming ${fstype}."
1292                                 fi
1293
1294                                 if [ "${UNIONTYPE}" != "unionmount" ]
1295                                 then
1296                                         mpoint="${croot}/${imagename}"
1297                                         rofsstring="${mpoint}=${roopt}:${rofsstring}" && rofslist="${mpoint} ${rofslist}"
1298                                 else
1299                                         mpoint="${rootmnt}"
1300                                 fi
1301                                 mkdir -p "${mpoint}"
1302                                 log_begin_msg "Mounting \"${image}\" on \"${mpoint}\" via \"${backdev}\""
1303                                 mount -t "${fstype}" -o ro,noatime "${backdev}" "${mpoint}" || panic "Can not mount ${backdev} (${image}) on ${mpoint}"
1304                                 log_end_msg
1305                         fi
1306                 done
1307         else
1308                 # we have a plain root system
1309                 mkdir -p "${croot}/filesystem"
1310                 log_begin_msg "Mounting \"${image_directory}\" on \"${croot}/filesystem\""
1311                 mount -t $(get_fstype "${image_directory}") -o ro,noatime "${image_directory}" "${croot}/filesystem" || panic "Can not mount ${image_directory} on ${croot}/filesystem" && rofsstring="${croot}/filesystem=${roopt}:${rofsstring}" && rofslist="${croot}/filesystem ${rofslist}"
1312                 # probably broken:
1313                 mount -o bind ${croot}/filesystem $mountpoint
1314                 log_end_msg
1315         fi
1316
1317         rofsstring=${rofsstring%:}
1318
1319         mkdir -p /cow
1320
1321         # Looking for "${root_persistence}" device or file
1322         if [ -n "${PERSISTENT}" ] && [ -z "${NOPERSISTENT}" ]
1323         then
1324                 if [ -z "${QUICKUSBMODULES}" ]
1325                 then
1326                         # Load USB modules
1327                         num_block=$(ls -l /sys/block | wc -l)
1328                         for module in sd_mod uhci-hcd ehci-hcd ohci-hcd usb-storage
1329                         do
1330                                 modprobe -q -b ${module}
1331                         done
1332
1333                         udevadm trigger
1334                         udevadm settle
1335
1336                         # For some reason, udevsettle does not block in this scenario,
1337                         # so we sleep for a little while.
1338                         #
1339                         # See https://bugs.launchpad.net/ubuntu/+source/casper/+bug/84591
1340                         for timeout in 5 4 3 2 1
1341                         do
1342                                 sleep 1
1343
1344                                 if [ $(ls -l /sys/block | wc -l) -gt ${num_block} ]
1345                                 then
1346                                         break
1347                                 fi
1348                         done
1349                 fi
1350
1351                 # search for label and files (this could be hugely optimized)
1352                 cowprobe=$(find_cow_device "${root_persistence}")
1353                 if [ -b "${cowprobe}" ]
1354                 then
1355                         # Blacklist /cow device, to avoid inconsistent setups for overlapping snapshots
1356                         # makes sense to have both persistence for /cow and /home mounted, maybe also with
1357                         # snapshots to be sure to really store some e.g key config files,
1358                         # but not on the same media
1359                         blacklistdev="${cowprobe}"
1360                         PERSISTENCE_IS_ON="1"
1361                         export PERSISTENCE_IS_ON
1362                 fi
1363                 # homecow just mount something on /home, this should be generalized some way
1364                 homecow=$(find_cow_device "${home_persistence}" "${blacklistdev}")
1365                 if [ -b "${homecow}" ]
1366                 then
1367                         PERSISTENCE_IS_ON="1"
1368                         export PERSISTENCE_IS_ON
1369                 fi
1370                 root_snapdata=$(find_snap "${root_snapshot_label}" "${blacklistdev}")
1371                 # This second type should be removed when snapshot will get smarter,
1372                 # hence when "/etc/live-snapshot*list" will be supported also by
1373                 # ext2|ext3|ext4|jffs2 snapshot types.
1374                 home_snapdata=$(find_snap "${home_snapshot_label}" "${blacklistdev}")
1375
1376                 if [ -b "${cowprobe}" ]
1377                 then
1378                         cowdevice=${cowprobe}
1379                         cow_fstype=$(get_fstype "${cowprobe}")
1380                         cow_mountopt="rw,noatime"
1381
1382                         if [ "${FORCEPERSISTENTFSCK}" = "Yes" ]
1383                         then
1384                                 fsck -y ${cowdevice}
1385                         fi
1386                 else
1387                         log_warning_msg "Unable to find the persistent medium"
1388                         cowdevice="tmpfs"
1389                         cow_fstype="tmpfs"
1390                         cow_mountopt="rw,noatime,mode=755"
1391                 fi
1392         elif [ -n "${NFS_COW}" ] && [ -z "${NOPERSISTENT}" ]
1393         then
1394                 # check if there are any nfs options
1395                 if echo ${NFS_COW}|grep -q ','
1396                 then
1397                         nfs_cow_opts="-o nolock,$(echo ${NFS_COW}|cut -d, -f2-)"
1398                         nfs_cow=$(echo ${NFS_COW}|cut -d, -f1)
1399                 else
1400                         nfs_cow_opts="-o nolock"
1401                         nfs_cow=${NFS_COW}
1402                 fi
1403                 mac="$(get_mac)"
1404                 if [ -n "${mac}" ]
1405                 then
1406                         cowdevice=$(echo ${nfs_cow}|sed "s/client_mac_address/${mac}/")
1407                         cow_fstype="nfs"
1408                 else
1409                         panic "unable to determine mac address"
1410                 fi
1411         else
1412                 cowdevice="tmpfs"
1413                 cow_fstype="tmpfs"
1414                 cow_mountopt="rw,noatime,mode=755"
1415         fi
1416
1417         if [ "${UNIONTYPE}" != "unionmount" ]
1418         then
1419
1420         if [ "${cow_fstype}" = "nfs" ]
1421         then
1422                 log_begin_msg \
1423                         "Trying nfsmount ${nfs_cow_opts} ${cowdevice} /cow"
1424                 nfsmount ${nfs_cow_opts} ${cowdevice} /cow || \
1425                         panic "Can not mount ${cowdevice} (n: ${cow_fstype}) on /cow"
1426         else
1427                 mount -t ${cow_fstype} -o ${cow_mountopt} ${cowdevice} /cow || \
1428                         panic "Can not mount ${cowdevice} (o: ${cow_fstype}) on /cow"
1429         fi
1430         fi
1431
1432         rofscount=$(echo ${rofslist} |wc -w)
1433
1434         if [ -n "${EXPOSED_ROOT}" ]
1435         then
1436                 if [ ${rofscount} -ne 1 ]
1437                 then
1438                         panic "only one RO file system supported with exposedroot: ${rofslist}"
1439                 fi
1440                 exposedrootfs=${rofslist%% }
1441
1442                 mount --bind ${exposedrootfs} ${rootmnt} || \
1443                         panic "bind mount of ${exposedrootfs} failed"
1444
1445                 if [ -z "${SKIP_UNION_MOUNTS}" ]
1446                 then
1447                         cow_dirs='/var/tmp /var/lock /var/run /var/log /var/spool /home /var/lib/live'
1448                 else
1449                         cow_dirs=''
1450                 fi
1451
1452                 for dir in ${cow_dirs}; do
1453                         mkdir -p /cow${dir}
1454
1455                         case "${UNIONTYPE}" in
1456                                 unionfs-fuse)
1457                                         (ulimit -n 16384; unionfs-fuse -o cow -o noinitgroups -o default_permissions -o allow_other -o use_ino -o suid /cow=RW:${exposedrootfs}${dir} "${rootmnt}${dir}" || panic "mount ${UNIONTYPE} on ${rootmnt}${dir} failed with option cow,noinitgroups,default_permissions,allow_other,use_ino,suid=/cow=RW:${exposedrootfs}${dir}")
1458                                         mkdir -p /dev/.initramfs/varrun
1459                                         pidof unionfs-fuse >> /dev/.initramfs/varrun/sendsigs.omit || true
1460                                         ;;
1461
1462                                 *)
1463                                         mount -t ${UNIONTYPE} -o rw,noatime,dirs=/cow${dir}=rw:${exposedrootfs}${dir}=ro ${UNIONTYPE} "${rootmnt}${dir}" || panic "mount ${UNIONTYPE} on ${rootmnt}${dir} failed with option rw,noatime,dirs=/cow${dir}=rw:${exposedrootfs}${dir}=ro"
1464                                         ;;
1465                         esac
1466                 done
1467         else
1468                 case "${UNIONTYPE}" in
1469                         unionfs-fuse)
1470                                 (ulimit -n 16384; unionfs-fuse -o cow -o noinitgroups -o default_permissions -o allow_other -o use_ino -o suid /cow=RW:${rofsstring} "${rootmnt}" || panic "mount ${UNIONTYPE} on ${rootmnt} failed with option cow,noinitgroups,default_permissions,allow_other,use_ino,suid=/cow:RW:${rofsstring}")
1471                                 mkdir -p /dev/.initramfs/varrun
1472                                 pidof unionfs-fuse >> /dev/.initramfs/varrun/sendsigs.omit || true
1473                                 ;;
1474
1475                         unionmount)
1476                                 mount_full -t ${cow_fstype} -o noatime,union,${cow_mountopt} ${cowdevice} "${rootmnt}" || panic "${UNIONTYPE} ${cowdevice} on ${rootmnt} failed with option noatime,union,${cow_mountopt}"
1477                                 ;;
1478
1479                         *)
1480                                 mount -t ${UNIONTYPE} -o noatime,${noxino_opt}dirs=/cow=rw:${rofsstring} ${UNIONTYPE} "${rootmnt}" || panic "mount ${UNIONTYPE} on ${rootmnt} failed with option noatime,${noxino_opt}dirs=/cow=rw:${rofsstring}"
1481                                 ;;
1482                 esac
1483         fi
1484
1485         # Correct the permissions of /:
1486         chmod 0755 "${rootmnt}"
1487
1488         # tmpfs file systems
1489         touch /etc/fstab
1490         mkdir -p "${rootmnt}/live"
1491         mount -t tmpfs tmpfs ${rootmnt}/live
1492
1493         # Adding other custom mounts
1494         if [ -n "${PERSISTENT}" ] && [ -z "${NOPERSISTENT}" ]
1495         then
1496                 # directly mount /home
1497                 # FIXME: add a custom mounts configurable system
1498
1499                 if [ -b "${homecow}" ]
1500                 then
1501                         mount -t $(get_fstype "${homecow}") -o rw,noatime "${homecow}" "${rootmnt}/home"
1502                         export HOMEMOUNTED=1 # used to proper calculate free space in do_snap_copy()
1503                 else
1504                         log_warning_msg "Unable to find the persistent home medium"
1505                 fi
1506
1507                 # Look for other snapshots to copy in
1508                 try_snap "${root_snapdata}" "${rootmnt}" "ROOT"
1509                 # This second type should be removed when snapshot grow smarter
1510                 try_snap "${home_snapdata}" "${rootmnt}" "HOME" "/home"
1511         fi
1512
1513         if [ -n "${SHOWMOUNTS}" ]
1514         then
1515                 for d in ${rofslist}
1516                 do
1517                         mkdir -p "${rootmnt}/live/${d##*/}"
1518
1519                         case d in
1520                                 *.dir)
1521                                         # do nothing # mount -o bind "${d}" "${rootmnt}/live/${d##*/}"
1522                                         ;;
1523
1524                                 *)
1525                                         case "${UNIONTYPE}" in
1526                                                 unionfs-fuse)
1527                                                         mount -o bind "${d}" "${rootmnt}/live/${d##*/}"
1528                                                         ;;
1529
1530                                                 *)
1531                                                         mount -o move "${d}" "${rootmnt}/live/${d##*/}"
1532                                                         ;;
1533                                         esac
1534                                         ;;
1535                         esac
1536                 done
1537         fi
1538
1539         # shows cow fs on /cow for use by live-snapshot
1540         mkdir -p "${rootmnt}/live/cow"
1541         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"
1542 }
1543
1544 check_dev ()
1545 {
1546         sysdev="${1}"
1547         devname="${2}"
1548         skip_uuid_check="${3}"
1549
1550         # support for fromiso=.../isofrom=....
1551         if [ -n "$FROMISO" ]
1552         then
1553                 ISO_DEVICE=$(dirname $FROMISO)
1554                 if ! [ -b $ISO_DEVICE ]
1555                 then
1556                         # to support unusual device names like /dev/cciss/c0d0p1
1557                         # as well we have to identify the block device name, let's
1558                         # do that for up to 15 levels
1559                         i=15
1560                         while [ -n "$ISO_DEVICE" ] && [ "$i" -gt 0 ]
1561                         do
1562                                 ISO_DEVICE=$(dirname ${ISO_DEVICE})
1563                                 [ -b "$ISO_DEVICE" ] && break
1564                                 i=$(($i -1))
1565                         done
1566                 fi
1567
1568                 if [ "$ISO_DEVICE" = "/" ]
1569                 then
1570                         echo "Warning: device for bootoption isofrom= ($FROMISO) not found.">>/live-boot.log
1571                 else
1572                         mkdir /isofrom
1573                         mount -t auto "$ISO_DEVICE" /isofrom
1574                         ISO_NAME="$(echo $FROMISO | sed "s|$ISO_DEVICE||")"
1575                         loopdevname=$(setup_loop "/isofrom/${ISO_NAME}" "loop" "/sys/block/loop*" "" '')
1576                         devname="${loopdevname}"
1577                 fi
1578         fi
1579
1580         if [ -z "${devname}" ]
1581         then
1582                 devname=$(sys2dev "${sysdev}")
1583         fi
1584
1585         if [ -d "${devname}" ]
1586         then
1587                 mount -o bind "${devname}" $mountpoint || continue
1588
1589                 if is_live_path $mountpoint
1590                 then
1591                         echo $mountpoint
1592                         return 0
1593                 else
1594                         umount $mountpoint
1595                 fi
1596         fi
1597         [ -e "$devname" ] || continue
1598
1599         if [ -n "${LIVE_MEDIA_OFFSET}" ]
1600         then
1601                 loopdevname=$(setup_loop "${devname}" "loop" "/sys/block/loop*" "${LIVE_MEDIA_OFFSET}" '')
1602                 devname="${loopdevname}"
1603         fi
1604
1605         fstype=$(get_fstype "${devname}")
1606
1607         if is_supported_fs ${fstype}
1608         then
1609                 devuid=$(blkid -o value -s UUID "$devname")
1610                 [ -n "$devuid" ] && grep -qs "\<$devuid\>" $tried && continue
1611                 mount -t ${fstype} -o ro,noatime "${devname}" ${mountpoint} || continue
1612                 [ -n "$devuid" ] && echo "$devuid" >> $tried
1613
1614                 if is_live_path ${mountpoint} && \
1615                         ([ "${skip_uuid_check}" ] || matches_uuid ${mountpoint})
1616                 then
1617                         echo ${mountpoint}
1618                         return 0
1619                 else
1620                         umount ${mountpoint}
1621                 fi
1622         fi
1623
1624         if [ -n "${LIVE_MEDIA_OFFSET}" ]
1625         then
1626                 losetup -d "${loopdevname}"
1627         fi
1628
1629         return 1
1630 }
1631
1632 find_livefs ()
1633 {
1634         timeout="${1}"
1635
1636         # don't start autodetection before timeout has expired
1637         if [ -n "${LIVE_MEDIA_TIMEOUT}" ]
1638         then
1639                 if [ "${timeout}" -lt "${LIVE_MEDIA_TIMEOUT}" ]
1640                 then
1641                         return 1
1642                 fi
1643         fi
1644
1645         # first look at the one specified in the command line
1646         case "${LIVE_MEDIA}" in
1647                 removable-usb)
1648                         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1649                         do
1650                                 if [ "$(cat ${sysblock}/removable)" = "1" ]
1651                                 then
1652                                         if readlink ${sysblock} | grep -q usb
1653                                         then
1654                                                 for dev in $(subdevices "${sysblock}")
1655                                                 do
1656                                                         if check_dev "${dev}"
1657                                                         then
1658                                                                 return 0
1659                                                         fi
1660                                                 done
1661                                         fi
1662                                 fi
1663                         done
1664                         return 1
1665                         ;;
1666
1667                 removable)
1668                         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1669                         do
1670                                 if [ "$(cat ${sysblock}/removable)" = "1" ]
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                         done
1681                         return 1
1682                         ;;
1683
1684                 *)
1685                         if [ ! -z "${LIVE_MEDIA}" ]
1686                         then
1687                                 if check_dev "null" "${LIVE_MEDIA}" "skip_uuid_check"
1688                                 then
1689                                         return 0
1690                                 fi
1691                         fi
1692                         ;;
1693         esac
1694
1695         # or do the scan of block devices
1696         # prefer removable devices over non-removable devices, so scan them first
1697         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | egrep -v "/(loop|ram|dm-|fd)")
1698         do
1699                 if [ "$(cat ${sysblock}/removable)" = "1" ]
1700                 then
1701                         removable_devices_to_scan="$removable_devices_to_scan $sysblock"
1702                 else
1703                         nonremovable_devices_to_scan="$nonremovable_devices_to_scan $sysblock"
1704                 fi
1705         done
1706         devices_to_scan="$removable_devices_to_scan $nonremovable_devices_to_scan"
1707
1708         for sysblock in $devices_to_scan
1709         do
1710                 devname=$(sys2dev "${sysblock}")
1711                 [ -e "$devname" ] || continue
1712                 fstype=$(get_fstype "${devname}")
1713
1714                 if /lib/udev/cdrom_id ${devname} > /dev/null
1715                 then
1716                         if check_dev "null" "${devname}"
1717                         then
1718                                 return 0
1719                         fi
1720                 elif is_nice_device "${sysblock}"
1721                 then
1722                         for dev in $(subdevices "${sysblock}")
1723                         do
1724                                 if check_dev "${dev}"
1725                                 then
1726                                         return 0
1727                                 fi
1728                         done
1729                 elif [ "${fstype}" = "squashfs" -o \
1730                         "${fstype}" = "btrfs" -o \
1731                         "${fstype}" = "ext2" -o \
1732                         "${fstype}" = "ext3" -o \
1733                         "${fstype}" = "ext4" -o \
1734                         "${fstype}" = "jffs2" ]
1735                 then
1736                         # This is an ugly hack situation, the block device has
1737                         # an image directly on it.  It's hopefully
1738                         # live-boot, so take it and run with it.
1739                         ln -s "${devname}" "${devname}.${fstype}"
1740                         echo "${devname}.${fstype}"
1741                         return 0
1742                 fi
1743         done
1744
1745         return 1
1746 }
1747
1748 integrity_check ()
1749 {
1750         media_mountpoint="${1}"
1751
1752         log_begin_msg "Checking media integrity"
1753
1754         cd ${media_mountpoint}
1755         /bin/md5sum -c md5sum.txt < /dev/tty8 > /dev/tty8
1756         RC="${?}"
1757
1758         log_end_msg
1759
1760         if [ "${RC}" -eq 0 ]
1761         then
1762                 log_success_msg "Everything ok, will reboot in 10 seconds."
1763                 sleep 10
1764                 cd /
1765                 umount ${media_mountpoint}
1766                 sync
1767                 echo u > /proc/sysrq-trigger
1768                 echo b > /proc/sysrq-trigger
1769         else
1770                 panic "Not ok, a media defect is likely, switch to VT8 for details."
1771         fi
1772 }
1773
1774 mountroot ()
1775 {
1776         if [ -x /scripts/local-top/cryptroot ]; then
1777             /scripts/local-top/cryptroot
1778         fi
1779
1780         exec 6>&1
1781         exec 7>&2
1782         exec > live-boot.log
1783         exec 2>&1
1784         tail -f live-boot.log >&7 &
1785         tailpid="${!}"
1786
1787         # Ensure 'panic' function is overridden
1788         . /scripts/live-functions
1789
1790         Arguments
1791
1792         maybe_break live-premount
1793         log_begin_msg "Running /scripts/live-premount"
1794         run_scripts /scripts/live-premount
1795         log_end_msg
1796
1797         # Needed here too because some things (*cough* udev *cough*)
1798         # changes the timeout
1799
1800         if [ ! -z "${NETBOOT}" ] || [ ! -z "${FETCH}" ] || [ ! -z "${HTTPFS}" ] || [ ! -z "${FTPFS}" ]
1801         then
1802                 if do_netmount
1803                 then
1804                         livefs_root="${mountpoint}"
1805                 else
1806                         panic "Unable to find a live file system on the network"
1807                 fi
1808         else
1809                 if [ -n "${ISCSI_PORTAL}" ]
1810                 then
1811                         do_iscsi && livefs_root="${mountpoint}"
1812                 elif [ -n "${PLAIN_ROOT}" ] && [ -n "${ROOT}" ]
1813                 then
1814                         # Do a local boot from hd
1815                         livefs_root=${ROOT}
1816                 else
1817                         if [ -x /usr/bin/memdiskfind ]
1818                         then
1819                                 MEMDISK=$(/usr/bin/memdiskfind)
1820
1821                                 if [ $? -eq 0 ]
1822                                 then
1823                                         # We found a memdisk, set up phram
1824                                         modprobe phram phram=memdisk,${MEMDISK}
1825
1826                                         # Load mtdblock, the memdisk will be /dev/mtdblock0
1827                                         modprobe mtdblock
1828                                 fi
1829                         fi
1830
1831                         # Scan local devices for the image
1832                         i=0
1833                         while [ "$i" -lt 60 ]
1834                         do
1835                                 livefs_root=$(find_livefs ${i})
1836
1837                                 if [ -n "${livefs_root}" ]
1838                                 then
1839                                         break
1840                                 fi
1841
1842                                 sleep 1
1843                                 i="$(($i + 1))"
1844                         done
1845                 fi
1846         fi
1847
1848         if [ -z "${livefs_root}" ]
1849         then
1850                 panic "Unable to find a medium containing a live file system"
1851         fi
1852
1853         if [ "${INTEGRITY_CHECK}" ]
1854         then
1855                 integrity_check "${livefs_root}"
1856         fi
1857
1858         if [ "${TORAM}" ]
1859         then
1860                 live_dest="ram"
1861         elif [ "${TODISK}" ]
1862         then
1863                 live_dest="${TODISK}"
1864         fi
1865
1866         if [ "${live_dest}" ]
1867         then
1868                 log_begin_msg "Copying live media to ${live_dest}"
1869                 copy_live_to "${livefs_root}" "${live_dest}"
1870                 log_end_msg
1871         fi
1872
1873         # if we do not unmount the ISO we can't run "fsck /dev/ice" later on
1874         # because the mountpoint is left behind in /proc/mounts, so let's get
1875         # rid of it when running from RAM
1876         if [ -n "$FROMISO" ] && [ "${TORAM}" ]
1877         then
1878           losetup -d /dev/loop0
1879           grep -q /isofrom /proc/mounts && umount /isofrom
1880         fi
1881
1882         if [ -n "${MODULETORAMFILE}" ] || [ -n "${PLAIN_ROOT}" ]
1883         then
1884                 setup_unionfs "${livefs_root}" "${rootmnt}"
1885         else
1886                 mac="$(get_mac)"
1887                 mac="$(echo ${mac} | sed 's/-//g')"
1888                 mount_images_in_directory "${livefs_root}" "${rootmnt}" "${mac}"
1889         fi
1890
1891
1892         if [ -n "${ROOT_PID}" ] ; then
1893                 echo "${ROOT_PID}" > "${rootmnt}"/live/root.pid
1894         fi
1895
1896         log_end_msg
1897
1898         # unionfs-fuse needs /dev to be bind-mounted for the duration of
1899         # live-bottom; udev's init script will take care of things after that
1900         if [ "${UNIONTYPE}" = unionfs-fuse ]
1901         then
1902                 mount -n -o bind /dev "${rootmnt}/dev"
1903         fi
1904
1905         # Move to the new root filesystem so that programs there can get at it.
1906         if [ ! -d /root/live/image ]
1907         then
1908                 mkdir -p /root/live/image
1909                 mount --move /live/image /root/live/image
1910         fi
1911
1912         # aufs2 in kernel versions around 2.6.33 has a regression:
1913         # directories can't be accessed when read for the first the time,
1914         # causing a failure for example when accessing /var/lib/fai
1915         # when booting FAI, this simple workaround solves it
1916         ls /root/* >/dev/null 2>&1
1917
1918         # copy snapshot configuration if exists
1919         if [ -f snapshot.conf ]
1920         then
1921                 log_begin_msg "Copying snapshot.conf to ${rootmnt}/etc/live/boot.d"
1922                 if [ ! -d "${rootmnt}/etc/live/boot.d" ]
1923                 then
1924                         mkdir -p "${rootmnt}/etc/live/boot.d"
1925                 fi
1926                 cp snapshot.conf "${rootmnt}/etc/live/boot.d/"
1927                 log_end_msg
1928         fi
1929
1930         maybe_break live-bottom
1931         log_begin_msg "Running /scripts/live-bottom\n"
1932
1933         run_scripts /scripts/live-bottom
1934         log_end_msg
1935
1936         if [ "${UNIONFS}" = unionfs-fuse ]
1937         then
1938                 umount "${rootmnt}/dev"
1939         fi
1940
1941         exec 1>&6 6>&-
1942         exec 2>&7 7>&-
1943         kill ${tailpid}
1944         [ -w "${rootmnt}/var/log/" ] && cp live-boot.log "${rootmnt}/var/log/" 2>/dev/null
1945 }