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