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