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