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