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