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