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