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