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