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