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