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