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