Removing locales bottom script, now handled by live-config.
[live-boot-grml.git] / scripts / live
1 #!/bin/sh
2
3 # set -e
4
5 export PATH="/root/usr/bin:/root/usr/sbin:/root/bin:/root/sbin:/usr/bin:/usr/sbin:/bin:/sbin"
6
7 echo "/root/lib" >> /etc/ld.so.conf
8 echo "/root/usr/lib" >> /etc/ld.so.conf
9
10 mountpoint="/live/image"
11 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         rc=1
686
687         modprobe -q af_packet # For DHCP
688
689         udevadm trigger
690         udevadm settle
691
692         if [ -z "${NETBOOT}" ] && [ -z "${FETCH}" ] && \
693            [ -z "${HTTPFS}" ] && [ -z "${FTPFS}" ]
694         then
695
696         # if ethdevice was not specified on the kernel command line
697         # make sure we try to get a working network configuration
698         # for *every* present network device (except for loopback of course)
699         if [ -z "$ETHDEVICE" ] ; then
700                 echo "If you want to boot from a specific device use bootoption ethdevice=..."
701                 for device in /sys/class/net/*; do
702                         dev=${device##*/} ;
703                         if [ "$dev" != "lo" ] ; then
704                                 ETHDEVICE="$ETHDEVICE $dev"
705                         fi
706                 done
707         fi
708
709         # split args of ethdevice=eth0,eth1 into "eth0 eth1"
710         for device in $(echo $ETHDEVICE | sed 's/,/ /g') ; do
711                 devlist="$devlist $device"
712         done
713
714         [ -n "$ETHDEV_TIMEOUT" ] || ETHDEV_TIMEOUT=15
715         echo "Using timeout of $ETHDEV_TIMEOUT seconds for network configuration."
716
717         # this is tricky (and ugly) because ipconfig sometimes just hangs/runs into
718         # an endless loop; iff execution fails give it two further tries, that's
719         # why we use '$devlist $devlist $devlist' for the other for loop
720         for dev in $devlist $devlist $devlist ; do
721                 echo "Executing ipconfig -t $ETHDEV_TIMEOUT $dev"
722                 ipconfig -t "$ETHDEV_TIMEOUT" $dev | tee -a /netboot.config &
723                 jobid=$!
724                 sleep "$ETHDEV_TIMEOUT" ; sleep 1
725                 if [ -r /proc/"$jobid"/status ] ; then
726                         echo "Killing job $jobid for device $dev as ipconfig ran into recursion..."
727                         kill -9 $jobid
728                 fi
729
730                 # if configuration of device worked we should have an assigned
731                 # IP address, iff so let's use the according as $DEVICE for later usage
732                 # simple and primitive approach which seems to work fine
733                 if ifconfig $dev | grep -q 'inet.*addr:' ; then
734                         export DEVICE="$dev"
735                         break
736                 fi
737         done
738
739         else
740         ipconfig ${DEVICE} | tee /netboot.config
741         fi
742
743         # source relevant ipconfig output
744         OLDHOSTNAME=${HOSTNAME}
745         . /tmp/net-${DEVICE}.conf
746         [ -z ${HOSTNAME} ] && HOSTNAME=${OLDHOSTNAME}
747         export HOSTNAME
748
749         # Check if we have a network device at all
750         if ! ls /sys/class/net/"$DEVICE" > /dev/null 2>&1 && \
751            ! ls /sys/class/net/eth0 > /dev/null 2>&1 && \
752            ! ls /sys/class/net/wlan0 > /dev/null 2>&1 && \
753            ! ls /sys/class/net/ath0 > /dev/null 2>&1 && \
754            ! ls /sys/class/net/ra0 > /dev/null 2>&1
755         then
756                 panic "No supported network device found, maybe a non-mainline driver is required."
757         fi
758
759         if [ "${NFSROOT}" = "auto" ]
760         then
761                 NFSROOT=${ROOTSERVER}:${ROOTPATH}
762         fi
763
764         if ( [ -n "${FETCH}" ] || [ -n "${HTTPFS}" ] || [ -n "${FTPFS}" ] ) && do_httpmount
765         then
766                 rc=0
767                 return ${rc}
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         fi
851
852         return ${rc}
853 }
854
855 do_nfsmount ()
856 {
857         rc=1
858
859         modprobe -q nfs
860
861         if [ -z "${NFSOPTS}" ]
862         then
863                 NFSOPTS=""
864         fi
865
866         log_begin_msg "Trying nfsmount -o nolock -o ro ${NFSOPTS} ${NFSROOT} ${mountpoint}"
867
868         # FIXME: This while loop is an ugly HACK round an nfs bug
869         i=0
870         while [ "$i" -lt 60 ]
871         do
872                 nfsmount -o nolock -o ro ${NFSOPTS} "${NFSROOT}" "${mountpoint}" && rc=0 && break
873                 sleep 1
874                 i="$(($i + 1))"
875         done
876
877         return ${rc}
878 }
879
880 do_cifsmount ()
881 {
882         rc=1
883
884         if [ -x "/sbin/mount.cifs" ]
885         then
886                 if [ -z "${NFSOPTS}" ]
887                 then
888                         CIFSOPTS="-ouser=root,password="
889                 else
890                         CIFSOPTS="${NFSOPTS}"
891                 fi
892
893                 log_begin_msg "Trying mount.cifs ${NFSROOT} ${mountpoint} ${CIFSOPTS}"
894                 modprobe -q cifs
895
896                 if mount.cifs "${NFSROOT}" "${mountpoint}" "${CIFSOPTS}"
897                 then
898                         rc=0
899                 fi
900         fi
901
902         return ${rc}
903 }
904
905 do_snap_copy ()
906 {
907         fromdev="${1}"
908         todir="${2}"
909         snap_type="${3}"
910         size=$(fs_size "${fromdev}" "" "used")
911
912         if [ -b "${fromdev}" ]
913         then
914                 # look for free mem
915                 if [ -n "${HOMEMOUNTED}" -a "${snap_type}" = "HOME" ]
916                 then
917                         todev=$(awk -v pat="$(base_path ${todir})" '$2 == pat { print $1 }' /proc/mounts)
918                         freespace=$(df -k | awk '/'${todev}'/{print $4}')
919                 else
920                         freespace=$(awk '/^MemFree:/{f=$2} /^Cached:/{c=$2} END{print f+c}' /proc/meminfo)
921                 fi
922
923                 tomount="/mnt/tmpsnap"
924
925                 if [ ! -d "${tomount}" ]
926                 then
927                         mkdir -p "${tomount}"
928                 fi
929
930                 fstype=$(get_fstype "${fromdev}")
931
932                 if [ -n "${fstype}" ]
933                 then
934                         # Copying stuff...
935                         mount -o ro -t "${fstype}" "${fromdev}" "${tomount}" || log_warning_msg "Error in mount -t ${fstype} -o ro ${fromdev} ${tomount}"
936                         cp -a "${tomount}"/* ${todir}
937                         umount "${tomount}"
938                 else
939                         log_warning_msg "Unrecognized fstype: ${fstype} on ${fromdev}:${snap_type}"
940                 fi
941
942                 rmdir "${tomount}"
943
944                 if echo ${fromdev} | grep -qs loop
945                 then
946                         losetup -d "${fromdev}"
947                 fi
948
949                 return 0
950         else
951                 return 1
952
953                 log_warning_msg "Unable to find the snapshot ${snap_type} medium"
954         fi
955 }
956
957 find_snap ()
958 {
959         # Look for ${snap_label}.* in block devices
960         snap_label="${1}"
961
962         if [ "${PERSISTENT}" != "nofiles" ]
963         then
964                 # search for image files
965                 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")
966         fi
967
968         if [ -z "${snapdata}" ]
969         then
970                 snapdata=$(find_cow_device "${snap_label}")
971         fi
972         echo "${snapdata}"
973 }
974
975 try_snap ()
976 {
977         # copy the contents of previously found snapshot to ${snap_mount}
978         # and remember the device and filename for resync on exit in live-boot.init
979
980         snapdata="${1}"
981         snap_mount="${2}"
982         snap_type="${3}"
983
984         if [ -n "${snapdata}" ] && [ ! -b "${snapdata}" ]
985         then
986                 log_success_msg "found snapshot: ${snapdata}"
987                 snapdev="$(echo ${snapdata} | cut -f1 -d ' ')"
988                 snapback="$(echo ${snapdata} | cut -f2 -d ' ')"
989                 snapfile="$(echo ${snapdata} | cut -f3 -d ' ')"
990
991                 RES=""
992                 if ! try_mount "${snapdev}" "${snapback}" "ro"
993                 then
994                         break
995                 fi
996
997                 if echo "${snapfile}" | grep -qs '\(squashfs\|ext2\|ext3\|ext4\|jffs2\)'
998                 then
999                         # squashfs, jffs2 or ext2/ext3/ext4 snapshot
1000                         dev=$(get_backing_device "${snapback}/${snapfile}")
1001
1002                         do_snap_copy "${dev}" "${snap_mount}" "${snap_type}"
1003                         RES=$?
1004                 else
1005                         # cpio.gz snapshot
1006
1007                         # Unfortunately klibc's cpio is incompatible with the
1008                         # rest of the world; everything else requires -u -d,
1009                         # while klibc doesn't implement them. Try to detect
1010                         # whether it's in use.
1011                         cpiopath="$(which cpio)" || true
1012                         if [ "$cpiopath" ] && grep -aq /lib/klibc "$cpiopath"
1013                         then
1014                                 cpioargs=
1015                         else
1016                                 cpioargs='--unconditional --make-directories'
1017                         fi
1018
1019                         if ! (cd "${snap_mount}" && zcat "${snapback}/${snapfile}" | cpio $cpioargs --extract --preserve-modification-time --no-absolute-filenames --sparse 2>/dev/null)
1020                         then
1021                                 log_warning_msg "failure to \"zcat ${snapback}/${snapfile} | cpio $cpioargs --extract --preserve-modification-time --no-absolute-filenames --sparse\""
1022                         fi
1023                 fi
1024
1025                 umount "${snapback}" ||  log_warning_msg "failure to \"umount ${snapback}\""
1026
1027                 if [ "${RES}" != "0" ]
1028                 then
1029                         log_warning_msg "Impossible to include the ${snapfile} Snapshot file"
1030                 fi
1031
1032         elif [ -b "${snapdata}" ]
1033         then
1034                 # Try to find if it could be a snapshot partition
1035                 dev="${snapdata}"
1036                 log_success_msg "found snapshot ${snap_type} device on ${dev}"
1037                 if echo "${dev}" | grep -qs loop
1038                 then
1039                         # strange things happens, user confused?
1040                         snaploop=$( losetup ${dev} | awk '{print $3}' | tr -d '()' )
1041                         snapfile=$(basename ${snaploop})
1042                         snapdev=$(awk -v pat="$( dirname ${snaploop})" '$2 == pat { print $1 }' /proc/mounts)
1043                 else
1044                         snapdev="${dev}"
1045                 fi
1046
1047                 if ! do_snap_copy "${dev}" "${snap_mount}" "${snap_type}"
1048                 then
1049                         log_warning_msg "Impossible to include the ${snap_type} Snapshot"
1050                         return 1
1051                 else
1052                         if [ -n "${snapfile}" ]
1053                         then
1054                                 # it was a loop device, user confused
1055                                 umount ${snapdev}
1056                         fi
1057                 fi
1058         else
1059                 log_warning_msg "Impossible to include the ${snap_type} Snapshot"
1060                 return 1
1061         fi
1062
1063         echo "export ${snap_type}SNAP="/cow${snap_mount#$rootmnt}":${snapdev}:${snapfile}" >> /etc/live.conf # for resync on reboot/halt
1064         return 0
1065 }
1066
1067 setup_unionfs ()
1068 {
1069         image_directory="${1}"
1070         rootmnt="${2}"
1071         addimage_directory="${3}"
1072
1073         case ${UNIONTYPE} in
1074                 aufs|unionfs)
1075                         modprobe -q -b ${UNIONTYPE}
1076
1077                         if ! cut -f2 /proc/filesystems | grep -q "^${UNIONTYPE}\$" && [ -x /bin/unionfs-fuse ]
1078                         then
1079                                 echo "${UNIONTYPE} not available, falling back to unionfs-fuse."
1080                                 echo "This might be really slow."
1081
1082                                 UNIONTYPE="unionfs-fuse"
1083                         fi
1084                         ;;
1085         esac
1086
1087         if [ "${UNIONTYPE}" = unionfs-fuse ]
1088         then
1089                 modprobe fuse
1090         fi
1091
1092         # run-init can't deal with images in a subdir, but we're going to
1093         # move all of these away before it runs anyway.  No, we're not,
1094         # put them in / since move-mounting them into / breaks mono and
1095         # some other apps.
1096
1097         croot="/"
1098
1099         # Let's just mount the read-only file systems first
1100         rofsstring=""
1101         rofslist=""
1102
1103         if [ "${UNIONTYPE}" = "aufs" ]
1104         then
1105                 roopt="rr"
1106                 noxino_opt="noxino,"
1107         elif [ "${UNIONTYPE}" = "unionfs-fuse" ]
1108         then
1109                 roopt="RO"
1110         else
1111                 roopt="ro"
1112         fi
1113
1114         if [ -z "${PLAIN_ROOT}" ]
1115         then
1116                 # Read image names from ${MODULE}.module if it exists
1117                 if [ -e "${image_directory}/filesystem.${MODULE}.module" ]
1118                 then
1119                         for IMAGE in $(cat ${image_directory}/filesystem.${MODULE}.module)
1120                         do
1121                                 image_string="${image_string} ${image_directory}/${IMAGE}"
1122                         done
1123                 elif [ -e "${image_directory}/${MODULE}.module" ]
1124                 then
1125                         for IMAGE in $(cat ${image_directory}/${MODULE}.module)
1126                         do
1127                                 image_string="${image_string} ${image_directory}/${IMAGE}"
1128                         done
1129                 else
1130                         # ${MODULE}.module does not exist, create a list of images
1131                         for FILESYSTEM in squashfs ext2 ext3 ext4 xfs jffs2 dir
1132                         do
1133                                 for IMAGE in "${image_directory}"/*."${FILESYSTEM}"
1134                                 do
1135                                         if [ -e "${IMAGE}" ]
1136                                         then
1137                                                 image_string="${image_string} ${IMAGE}"
1138                                         fi
1139                                 done
1140                         done
1141
1142                         if [ -n "${addimage_directory}" ] && [ -d "${addimage_directory}" ]
1143                         then
1144                                 for FILESYSTEM in squashfs ext2 ext3 ext4 xfs jffs2 dir
1145                                 do
1146                                         for IMAGE in "${addimage_directory}"/*."${FILESYSTEM}"
1147                                         do
1148                                                 if [ -e "${IMAGE}" ]
1149                                                 then
1150                                                         image_string="${image_string} ${IMAGE}"
1151                                                 fi
1152                                         done
1153                                 done
1154                         fi
1155
1156                         # Now sort the list
1157                         image_string="$(echo ${image_string} | sed -e 's/ /\n/g' | sort )"
1158                 fi
1159
1160         [ -n "${MODULETORAMFILE}" ] && image_string="${image_directory}/$(basename ${MODULETORAMFILE})"
1161
1162                 mkdir -p "${croot}"
1163
1164                 for image in ${image_string}
1165                 do
1166                         imagename=$(basename "${image}")
1167
1168                         export image devname
1169                         maybe_break live-realpremount
1170                         log_begin_msg "Running /scripts/live-realpremount"
1171                         run_scripts /scripts/live-realpremount
1172                         log_end_msg
1173
1174                         if [ -d "${image}" ]
1175                         then
1176                                 # it is a plain directory: do nothing
1177                                 rofsstring="${image}=${roopt}:${rofsstring}"
1178                                 rofslist="${image} ${rofslist}"
1179                         elif [ -f "${image}" ]
1180                         then
1181                                 if losetup --help 2>&1 | grep -q -- "-r\b"
1182                                 then
1183                                         backdev=$(get_backing_device "${image}" "-r")
1184                                 else
1185                                         backdev=$(get_backing_device "${image}")
1186                                 fi
1187                                 fstype=$(get_fstype "${backdev}")
1188
1189                                 if [ "${fstype}" = "unknown" ]
1190                                 then
1191                                         panic "Unknown file system type on ${backdev} (${image})"
1192                                 fi
1193
1194                                 if [ -z "${fstype}" ]
1195                                 then
1196                                         fstype="${imagename##*.}"
1197                                         log_warning_msg "Unknown file system type on ${backdev} (${image}), assuming ${fstype}."
1198                                 fi
1199
1200                                 mkdir -p "${croot}/${imagename}"
1201                                 log_begin_msg "Mounting \"${image}\" on \"${croot}${imagename}\" via \"${backdev}\""
1202                                 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}"
1203                                 log_end_msg
1204                         fi
1205                 done
1206         else
1207                 # we have a plain root system
1208                 mkdir -p "${croot}/filesystem"
1209                 log_begin_msg "Mounting \"${image_directory}\" on \"${croot}/filesystem\""
1210                 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}"
1211                 # probably broken:
1212                 mount -o bind ${croot}/filesystem $mountpoint
1213                 log_end_msg
1214         fi
1215
1216         rofsstring=${rofsstring%:}
1217
1218         mkdir -p /cow
1219
1220         # Looking for "${root_persistence}" device or file
1221         if [ -n "${PERSISTENT}" ] && [ -z "${NOPERSISTENT}" ]
1222         then
1223                 if [ -z "${QUICKUSBMODULES}" ]
1224                 then
1225                         # Load USB modules
1226                         num_block=$(ls -l /sys/block | wc -l)
1227                         for module in sd_mod uhci-hcd ehci-hcd ohci-hcd usb-storage
1228                         do
1229                                 modprobe -q -b ${module}
1230                         done
1231
1232                         udevadm trigger
1233                         udevadm settle
1234
1235                         # For some reason, udevsettle does not block in this scenario,
1236                         # so we sleep for a little while.
1237                         #
1238                         # See https://bugs.launchpad.net/ubuntu/+source/casper/+bug/84591
1239                         for timeout in 5 4 3 2 1
1240                         do
1241                                 sleep 1
1242
1243                                 if [ $(ls -l /sys/block | wc -l) -gt ${num_block} ]
1244                                 then
1245                                         break
1246                                 fi
1247                         done
1248                 fi
1249
1250                 # search for label and files (this could be hugely optimized)
1251                 cowprobe=$(find_cow_device "${root_persistence}")
1252                 if [ -b "${cowprobe}" ]
1253                 then
1254                         # Blacklist /cow device, to avoid inconsistent setups for overlapping snapshots
1255                         # makes sense to have both persistence for /cow and /home mounted, maybe also with
1256                         # snapshots to be sure to really store some e.g key config files,
1257                         # but not on the same media
1258                         blacklistdev="${cowprobe}"
1259                         PERSISTENCE_IS_ON="1"
1260                         export PERSISTENCE_IS_ON
1261                 fi
1262                 # homecow just mount something on /home, this should be generalized some way
1263                 homecow=$(find_cow_device "${home_persistence}" "${blacklistdev}")
1264                 if [ -b "${homecow}" ]
1265                 then
1266                         PERSISTENCE_IS_ON="1"
1267                         export PERSISTENCE_IS_ON
1268                 fi
1269                 root_snapdata=$(find_snap "${root_snapshot_label}" "${blacklistdev}")
1270                 # This second type should be removed when snapshot will get smarter,
1271                 # hence when "/etc/live-snapshot*list" will be supported also by
1272                 # ext2|ext3|ext4|jffs2 snapshot types.
1273                 home_snapdata=$(find_snap "${home_snapshot_label}" "${blacklistdev}")
1274
1275                 if [ -b "${cowprobe}" ]
1276                 then
1277                         cowdevice=${cowprobe}
1278                         cow_fstype=$(get_fstype "${cowprobe}")
1279                         cow_mountopt="rw,noatime"
1280
1281                         if [ "${FORCEPERSISTENTFSCK}" = "Yes" ]
1282                         then
1283                                 fsck -y ${cowdevice}
1284                         fi
1285                 else
1286                         log_warning_msg "Unable to find the persistent medium"
1287                         cowdevice="tmpfs"
1288                         cow_fstype="tmpfs"
1289                         cow_mountopt="rw,noatime,mode=755"
1290                 fi
1291         elif [ -n "${NFS_COW}" ] && [ -z "${NOPERSISTENT}" ]
1292         then
1293                 # check if there are any nfs options
1294                 if echo ${NFS_COW}|grep -q ','
1295                 then
1296                         nfs_cow_opts="-o nolock,$(echo ${NFS_COW}|cut -d, -f2-)"
1297                         nfs_cow=$(echo ${NFS_COW}|cut -d, -f1)
1298                 else
1299                         nfs_cow_opts="-o nolock"
1300                         nfs_cow=${NFS_COW}
1301                 fi
1302                 mac="$(get_mac)"
1303                 if [ -n "${mac}" ]
1304                 then
1305                         cowdevice=$(echo ${nfs_cow}|sed "s/client_mac_address/${mac}/")
1306                         cow_fstype="nfs"
1307                 else
1308                         panic "unable to determine mac address"
1309                 fi
1310         else
1311                 cowdevice="tmpfs"
1312                 cow_fstype="tmpfs"
1313                 cow_mountopt="rw,noatime,mode=755"
1314         fi
1315
1316         if [ "${cow_fstype}" = "nfs" ]
1317         then
1318                 log_begin_msg \
1319                         "Trying nfsmount ${nfs_cow_opts} ${cowdevice} /cow"
1320                 nfsmount ${nfs_cow_opts} ${cowdevice} /cow || \
1321                         panic "Can not mount ${cowdevice} (n: ${cow_fstype}) on /cow"
1322         else
1323                 mount -t ${cow_fstype} -o ${cow_mountopt} ${cowdevice} /cow || \
1324                         panic "Can not mount ${cowdevice} (o: ${cow_fstype}) on /cow"
1325         fi
1326
1327         rofscount=$(echo ${rofslist} |wc -w)
1328
1329         if [ -n "${EXPOSED_ROOT}" ]
1330         then
1331                 if [ ${rofscount} -ne 1 ]
1332                 then
1333                         panic "only one RO file system supported with exposedroot: ${rofslist}"
1334                 fi
1335                 exposedrootfs=${rofslist%% }
1336
1337                 mount --bind ${exposedrootfs} ${rootmnt} || \
1338                         panic "bind mount of ${exposedrootfs} failed"
1339
1340                 if [ -z "${SKIP_UNION_MOUNTS}" ]
1341                 then
1342                         cow_dirs='/var/tmp /var/lock /var/run /var/log /var/spool /home /var/lib/live'
1343                 else
1344                         cow_dirs=''
1345                 fi
1346
1347                 for dir in ${cow_dirs}; do
1348                         mkdir -p /cow${dir}
1349
1350                         case "${UNIONTYPE}" in
1351                                 unionfs-fuse)
1352                                         (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}")
1353                                         mkdir -p /dev/.initramfs/varrun
1354                                         pidof unionfs-fuse >> /dev/.initramfs/varrun/sendsigs.omit || true
1355                                         ;;
1356
1357                                 *)
1358                                         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"
1359                                         ;;
1360                         esac
1361                 done
1362         else
1363                 case "${UNIONTYPE}" in
1364                         unionfs-fuse)
1365                                 (ulimit -n 16384; unionfs-fuse -o cow -o noinitgroups -o default_permissions -o allow_other -o use_ino -o suid /cow=RW:${rofsstring} "${rootmnt}" || panic "mount ${UNIONTYPE} on ${rootmnt} failed with option cow,noinitgroups,default_permissions,allow_other,use_ino,suid=/cow:RW:${rofsstring}")
1366                                 mkdir -p /dev/.initramfs/varrun
1367                                 pidof unionfs-fuse >> /dev/.initramfs/varrun/sendsigs.omit || true
1368                                 ;;
1369
1370                         *)
1371                                 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}"
1372                                 ;;
1373                 esac
1374         fi
1375
1376         # Correct the permissions of /:
1377         chmod 0755 "${rootmnt}"
1378
1379         # tmpfs file systems
1380         touch /etc/fstab
1381         mkdir -p "${rootmnt}/live"
1382         mount -t tmpfs tmpfs ${rootmnt}/live
1383
1384         # Adding other custom mounts
1385         if [ -n "${PERSISTENT}" ] && [ -z "${NOPERSISTENT}" ]
1386         then
1387                 # directly mount /home
1388                 # FIXME: add a custom mounts configurable system
1389
1390                 if [ -b "${homecow}" ]
1391                 then
1392                         mount -t $(get_fstype "${homecow}") -o rw,noatime "${homecow}" "${rootmnt}/home"
1393                         export HOMEMOUNTED=1 # used to proper calculate free space in do_snap_copy()
1394                 else
1395                         log_warning_msg "Unable to find the persistent home medium"
1396                 fi
1397
1398                 # Look for other snapshots to copy in
1399                 try_snap "${root_snapdata}" "${rootmnt}" "ROOT"
1400                 # This second type should be removed when snapshot grow smarter
1401                 try_snap "${home_snapdata}" "${rootmnt}/home" "HOME"
1402         fi
1403
1404         if [ -n "${SHOWMOUNTS}" ]
1405         then
1406                 for d in ${rofslist}
1407                 do
1408                         mkdir -p "${rootmnt}/live/${d##*/}"
1409
1410                         case d in
1411                                 *.dir)
1412                                         # do nothing # mount -o bind "${d}" "${rootmnt}/live/${d##*/}"
1413                                         ;;
1414
1415                                 *)
1416                                         case "${UNIONTYPE}" in
1417                                                 unionfs-fuse)
1418                                                         mount -o bind "${d}" "${rootmnt}/live/${d##*/}"
1419                                                         ;;
1420
1421                                                 *)
1422                                                         mount -o move "${d}" "${rootmnt}/live/${d##*/}"
1423                                                         ;;
1424                                         esac
1425                                         ;;
1426                         esac
1427                 done
1428         fi
1429
1430         # shows cow fs on /cow for use by live-snapshot
1431         mkdir -p "${rootmnt}/live/cow"
1432         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"
1433 }
1434
1435 check_dev ()
1436 {
1437         sysdev="${1}"
1438         devname="${2}"
1439         skip_uuid_check="${3}"
1440
1441         # support for fromiso=.../isofrom=....
1442         if [ -n "$FROMISO" ]
1443         then
1444                 ISO_DEVICE=$(dirname $FROMISO)
1445                 if ! [ -b $ISO_DEVICE ]
1446                 then
1447                         # to support unusual device names like /dev/cciss/c0d0p1
1448                         # as well we have to identify the block device name, let's
1449                         # do that for up to 15 levels
1450                         i=15
1451                         while [ -n "$ISO_DEVICE" ] && [ "$i" -gt 0 ]
1452                         do
1453                                 ISO_DEVICE=$(dirname ${ISO_DEVICE})
1454                                 [ -b "$ISO_DEVICE" ] && break
1455                                 i=$(($i -1))
1456                         done
1457                 fi
1458
1459                 if [ "$ISO_DEVICE" = "/" ]
1460                 then
1461                         echo "Warning: device for bootoption isofrom= ($FROMISO) not found.">>/live.log
1462                 else
1463                         mkdir /isofrom
1464                         mount "$ISO_DEVICE" /isofrom
1465                         ISO_NAME="$(echo $FROMISO | sed "s|$ISO_DEVICE||")"
1466                         loopdevname=$(setup_loop "/isofrom/${ISO_NAME}" "loop" "/sys/block/loop*" "" '')
1467                         devname="${loopdevname}"
1468                 fi
1469         fi
1470
1471         if [ -z "${devname}" ]
1472         then
1473                 devname=$(sys2dev "${sysdev}")
1474         fi
1475
1476         if [ -d "${devname}" ]
1477         then
1478                 mount -o bind "${devname}" $mountpoint || continue
1479
1480                 if is_live_path $mountpoint
1481                 then
1482                         echo $mountpoint
1483                         return 0
1484                 else
1485                         umount $mountpoint
1486                 fi
1487         fi
1488         [ -e "$devname" ] || continue
1489
1490         if [ -n "${LIVE_MEDIA_OFFSET}" ]
1491         then
1492                 loopdevname=$(setup_loop "${devname}" "loop" "/sys/block/loop*" "${LIVE_MEDIA_OFFSET}" '')
1493                 devname="${loopdevname}"
1494         fi
1495
1496         fstype=$(get_fstype "${devname}")
1497
1498         if is_supported_fs ${fstype}
1499         then
1500                 devuid=$(blkid -o value -s UUID "$devname")
1501                 [ -n "$devuid" ] && grep -qs "\<$devuid\>" $tried && continue
1502                 mount -t ${fstype} -o ro,noatime "${devname}" ${mountpoint} || continue
1503                 [ -n "$devuid" ] && echo "$devuid" >> $tried
1504
1505                 if is_live_path ${mountpoint} && \
1506                         ([ "${skip_uuid_check}" ] || matches_uuid ${mountpoint})
1507                 then
1508                         echo ${mountpoint}
1509                         return 0
1510                 else
1511                         umount ${mountpoint}
1512                 fi
1513         fi
1514
1515         if [ -n "${LIVE_MEDIA_OFFSET}" ]
1516         then
1517                 losetup -d "${loopdevname}"
1518         fi
1519
1520         return 1
1521 }
1522
1523 find_livefs ()
1524 {
1525         timeout="${1}"
1526
1527         # don't start autodetection before timeout has expired
1528         if [ -n "${LIVE_MEDIA_TIMEOUT}" ]
1529         then
1530                 if [ "${timeout}" -lt "${LIVE_MEDIA_TIMEOUT}" ]
1531                 then
1532                         return 1
1533                 fi
1534         fi
1535
1536         # first look at the one specified in the command line
1537         case "${LIVE_MEDIA}" in
1538                 removable-usb)
1539                         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1540                         do
1541                                 if [ "$(cat ${sysblock}/removable)" = "1" ]
1542                                 then
1543                                         if readlink ${sysblock} | grep -q usb ||
1544                                            readlink ${sysblock}/device | grep -q usb # linux < 2.6.29
1545                                         then
1546                                                 for dev in $(subdevices "${sysblock}")
1547                                                 do
1548                                                         if check_dev "${dev}"
1549                                                         then
1550                                                                 return 0
1551                                                         fi
1552                                                 done
1553                                         fi
1554                                 fi
1555                         done
1556                         return 1
1557                         ;;
1558
1559                 removable)
1560                         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1561                         do
1562                                 if [ "$(cat ${sysblock}/removable)" = "1" ]
1563                                 then
1564                                         for dev in $(subdevices "${sysblock}")
1565                                         do
1566                                                 if check_dev "${dev}"
1567                                                 then
1568                                                         return 0
1569                                                 fi
1570                                         done
1571                                 fi
1572                         done
1573                         return 1
1574                         ;;
1575
1576                 *)
1577                         if [ ! -z "${LIVE_MEDIA}" ]
1578                         then
1579                                 if check_dev "null" "${LIVE_MEDIA}" "skip_uuid_check"
1580                                 then
1581                                         return 0
1582                                 fi
1583                         fi
1584                         ;;
1585         esac
1586
1587         # or do the scan of block devices
1588         # prefer removable devices over non-removable devices, so scan them first
1589         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | egrep -v "/(loop|ram|dm-|fd)")
1590         do
1591                 if [ "$(cat ${sysblock}/removable)" = "1" ]
1592                 then
1593                         removable_devices_to_scan="$removable_devices_to_scan $sysblock"
1594                 else
1595                         nonremovable_devices_to_scan="$nonremovable_devices_to_scan $sysblock"
1596                 fi
1597         done
1598         devices_to_scan="$removable_devices_to_scan $nonremovable_devices_to_scan"
1599
1600         for sysblock in $devices_to_scan
1601         do
1602                 devname=$(sys2dev "${sysblock}")
1603                 [ -e "$devname" ] || continue
1604                 fstype=$(get_fstype "${devname}")
1605
1606                 if /lib/udev/cdrom_id ${devname} > /dev/null
1607                 then
1608                         if check_dev "null" "${devname}"
1609                         then
1610                                 return 0
1611                         fi
1612                 elif is_nice_device "${sysblock}"
1613                 then
1614                         for dev in $(subdevices "${sysblock}")
1615                         do
1616                                 if check_dev "${dev}"
1617                                 then
1618                                         return 0
1619                                 fi
1620                         done
1621                 elif [ "${fstype}" = "squashfs" -o \
1622                         "${fstype}" = "ext2" -o \
1623                         "${fstype}" = "ext3" -o \
1624                         "${fstype}" = "ext4" -o \
1625                         "${fstype}" = "jffs2" ]
1626                 then
1627                         # This is an ugly hack situation, the block device has
1628                         # an image directly on it.  It's hopefully
1629                         # live-boot, so take it and run with it.
1630                         ln -s "${devname}" "${devname}.${fstype}"
1631                         echo "${devname}.${fstype}"
1632                         return 0
1633                 fi
1634         done
1635
1636         return 1
1637 }
1638
1639 set_usplash_timeout ()
1640 {
1641         if [ -x /sbin/usplash_write ]
1642         then
1643                 /sbin/usplash_write "TIMEOUT 120"
1644         else if [ -x /sbin/splashy_update ] ; then
1645                 /sbin/splashy_update "TIMEOUT 120"
1646         fi ; fi
1647 }
1648
1649 integrity_check ()
1650 {
1651         media_mountpoint="${1}"
1652
1653         log_begin_msg "Checking media integrity"
1654
1655         cd ${media_mountpoint}
1656         /bin/md5sum -c md5sum.txt < /dev/tty8 > /dev/tty8
1657         RC="${?}"
1658
1659         log_end_msg
1660
1661         if [ "${RC}" -eq 0 ]
1662         then
1663                 log_success_msg "Everything ok, will reboot in 10 seconds."
1664                 sleep 10
1665                 cd /
1666                 umount ${media_mountpoint}
1667                 sync
1668                 echo u > /proc/sysrq-trigger
1669                 echo b > /proc/sysrq-trigger
1670         else
1671                 panic "Not ok, a media defect is likely, switch to VT8 for details."
1672         fi
1673 }
1674
1675 start_usplash_pulse ()
1676 {
1677         if [ -x /sbin/usplash_write ]
1678         then
1679                 /sbin/usplash_write "PULSELOGO"
1680         fi
1681 }
1682
1683 mountroot ()
1684 {
1685         if [ -x /scripts/local-top/cryptroot ]; then
1686             /scripts/local-top/cryptroot
1687         fi
1688
1689         exec 6>&1
1690         exec 7>&2
1691         exec > live.log
1692         exec 2>&1
1693         tail -f live.log >&7 &
1694         tailpid="${!}"
1695
1696         # Ensure 'panic' function is overridden
1697         . /scripts/live-functions
1698
1699         Arguments
1700
1701         set_usplash_timeout
1702         start_usplash_pulse
1703
1704         maybe_break live-premount
1705         log_begin_msg "Running /scripts/live-premount"
1706         run_scripts /scripts/live-premount
1707         log_end_msg
1708
1709         # Needed here too because some things (*cough* udev *cough*)
1710         # changes the timeout
1711
1712         set_usplash_timeout
1713
1714         if [ ! -z "${NETBOOT}" ] || [ ! -z "${FETCH}" ] || [ ! -z "${HTTPFS}" ] || [ ! -z "${FTPFS}" ]
1715         then
1716                 if do_netmount
1717                 then
1718                         livefs_root="${mountpoint}"
1719                 else
1720                         panic "Unable to find a live file system on the network"
1721                 fi
1722         else
1723                 if [ -n "${PLAIN_ROOT}" ] && [ -n "${ROOT}" ]
1724                 then
1725                         # Do a local boot from hd
1726                         livefs_root=${ROOT}
1727                 else
1728                         # Scan local devices for the image
1729                         i=0
1730                         while [ "$i" -lt 60 ]
1731                         do
1732                                 livefs_root=$(find_livefs ${i})
1733
1734                                 if [ -n "${livefs_root}" ]
1735                                 then
1736                                         break
1737                                 fi
1738
1739                                 sleep 1
1740                                 i="$(($i + 1))"
1741                         done
1742                 fi
1743         fi
1744
1745         if [ -z "${livefs_root}" ]
1746         then
1747                 panic "Unable to find a medium containing a live file system"
1748         fi
1749
1750         if [ "${INTEGRITY_CHECK}" ]
1751         then
1752                 integrity_check "${livefs_root}"
1753         fi
1754
1755         if [ "${TORAM}" ]
1756         then
1757                 live_dest="ram"
1758         elif [ "${TODISK}" ]
1759         then
1760                 live_dest="${TODISK}"
1761         fi
1762
1763         if [ "${live_dest}" ]
1764         then
1765                 log_begin_msg "Copying live media to ${live_dest}"
1766                 copy_live_to "${livefs_root}" "${live_dest}"
1767                 log_end_msg
1768         fi
1769
1770         # if we do not unmount the ISO we can't run "fsck /dev/ice" later on
1771         # because the mountpoint is left behind in /proc/mounts, so let's get
1772         # rid of it when running from RAM
1773         if [ -n "$FROMISO" ] && [ "${TORAM}" ]
1774         then
1775           losetup -d /dev/loop0
1776           grep -q /isofrom /proc/mounts && umount /isofrom
1777         fi
1778
1779         if [ -n "${MODULETORAMFILE}" ] || [ -n "${PLAIN_ROOT}" ]
1780         then
1781                 setup_unionfs "${livefs_root}" "${rootmnt}"
1782         else
1783                 mac="$(get_mac)"
1784                 mac="$(echo ${mac} | sed 's/-//g')"
1785                 mount_images_in_directory "${livefs_root}" "${rootmnt}" "${mac}"
1786         fi
1787
1788         log_end_msg
1789
1790         # unionfs-fuse needs /dev to be bind-mounted for the duration of
1791         # live-bottom; udev's init script will take care of things after that
1792         if [ "${UNIONTYPE}" = unionfs-fuse ]
1793         then
1794                 mount -n -o bind /dev "${rootmnt}/dev"
1795         fi
1796
1797         # Move to the new root filesystem so that programs there can get at it.
1798         if [ ! -d /root/live/image ]
1799         then
1800                 mkdir -p /root/live/image
1801                 mount --move /live/image /root/live/image
1802         fi
1803
1804         maybe_break live-bottom
1805         log_begin_msg "Running /scripts/live-bottom\n"
1806
1807         run_scripts /scripts/live-bottom
1808         log_end_msg
1809
1810         if [ "${UNIONFS}" = unionfs-fuse ]
1811         then
1812                 umount "${rootmnt}/dev"
1813         fi
1814
1815         exec 1>&6 6>&-
1816         exec 2>&7 7>&-
1817         kill ${tailpid}
1818         [ -w "${rootmnt}/var/log/" ] && cp live.log "${rootmnt}/var/log/" 2>/dev/null
1819         if [ -f /etc/live.conf ]
1820         then
1821                 cp /etc/live.conf "${rootmnt}/etc/"
1822         fi
1823 }