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