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