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