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