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