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