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