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