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