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