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