Merging casper 1.188.
[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-orion-ehci|platform-mmc|platform-mxsdhci|)"
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
1085                         # Unfortunately klibc's cpio is incompatible with the
1086                         # rest of the world; everything else requires -u -d,
1087                         # while klibc doesn't implement them. Try to detect
1088                         # whether it's in use.
1089                         cpiopath="$(which cpio)" || true
1090                         if [ "$cpiopath" ] && grep -aq /lib/klibc "$cpiopath"
1091                         then
1092                                 cpioargs=
1093                         else
1094                                 cpioargs='--unconditional --make-directories'
1095                         fi
1096
1097                         if ! (cd "${snap_mount}" && zcat "${snapback}/${snapfile}" | cpio $cpioargs --extract --preserve-modification-time --no-absolute-filenames --sparse 2>/dev/null)
1098                         then
1099                                 log_warning_msg "failure to \"zcat ${snapback}/${snapfile} | cpio $cpioargs --extract --preserve-modification-time --no-absolute-filenames --sparse\""
1100                         fi
1101                 fi
1102
1103                 umount "${snapback}" ||  log_warning_msg "failure to \"umount ${snapback}\""
1104
1105                 if [ "${RES}" != "0" ]
1106                 then
1107                         log_warning_msg "Impossible to include the ${snapfile} Snapshot file"
1108                 fi
1109
1110         elif [ -b "${snapdata}" ]
1111         then
1112                 # Try to find if it could be a snapshot partition
1113                 dev="${snapdata}"
1114                 log_success_msg "found snapshot ${snap_type} device on ${dev}"
1115                 if echo "${dev}" | grep -qs loop
1116                 then
1117                         # strange things happens, user confused?
1118                         snaploop=$( losetup ${dev} | awk '{print $3}' | tr -d '()' )
1119                         snapfile=$(basename ${snaploop})
1120                         snapdev=$(awk -v pat="$( dirname ${snaploop})" '$2 == pat { print $1 }' /proc/mounts)
1121                 else
1122                         snapdev="${dev}"
1123                 fi
1124
1125                 if ! do_snap_copy "${dev}" "${snap_mount}" "${snap_type}"
1126                 then
1127                         log_warning_msg "Impossible to include the ${snap_type} Snapshot"
1128                         return 1
1129                 else
1130                         if [ -n "${snapfile}" ]
1131                         then
1132                                 # it was a loop device, user confused
1133                                 umount ${snapdev}
1134                         fi
1135                 fi
1136         else
1137                 log_warning_msg "Impossible to include the ${snap_type} Snapshot"
1138                 return 1
1139         fi
1140
1141         echo "export ${snap_type}SNAP="/cow${snap_mount#$rootmnt}":${snapdev}:${snapfile}" >> /etc/live.conf # for resync on reboot/halt
1142         return 0
1143 }
1144
1145 setup_unionfs ()
1146 {
1147         image_directory="${1}"
1148         rootmnt="${2}"
1149         addimage_directory="${3}"
1150
1151         case ${UNIONTYPE} in
1152                 aufs|unionfs)
1153                         modprobe -q -b ${UNIONTYPE}
1154
1155                         if ! cut -f2 /proc/filesystems | grep -q "^${UNIONTYPE}\$" && [ -x /bin/unionfs-fuse ]
1156                         then
1157                                 echo "${UNIONTYPE} not available, falling back to unionfs-fuse."
1158                                 echo "This might be really slow."
1159
1160                                 UNIONTYPE="unionfs-fuse"
1161                         fi
1162                         ;;
1163         esac
1164
1165         if [ "${UNIONTYPE}" = unionfs-fuse ]
1166         then
1167                 modprobe fuse
1168         fi
1169
1170         # run-init can't deal with images in a subdir, but we're going to
1171         # move all of these away before it runs anyway.  No, we're not,
1172         # put them in / since move-mounting them into / breaks mono and
1173         # some other apps.
1174
1175         croot="/"
1176
1177         # Let's just mount the read-only file systems first
1178         rofsstring=""
1179         rofslist=""
1180
1181         if [ "${UNIONTYPE}" = "aufs" ]
1182         then
1183                 roopt="rr"
1184                 noxino_opt="noxino,"
1185         elif [ "${UNIONTYPE}" = "unionfs-fuse" ]
1186         then
1187                 roopt="RO"
1188         else
1189                 roopt="ro"
1190         fi
1191
1192         if [ -z "${PLAIN_ROOT}" ]
1193         then
1194                 # Read image names from ${MODULE}.module if it exists
1195                 if [ -e "${image_directory}/filesystem.${MODULE}.module" ]
1196                 then
1197                         for IMAGE in $(cat ${image_directory}/filesystem.${MODULE}.module)
1198                         do
1199                                 image_string="${image_string} ${image_directory}/${IMAGE}"
1200                         done
1201                 elif [ -e "${image_directory}/${MODULE}.module" ]
1202                 then
1203                         for IMAGE in $(cat ${image_directory}/${MODULE}.module)
1204                         do
1205                                 image_string="${image_string} ${image_directory}/${IMAGE}"
1206                         done
1207                 else
1208                         # ${MODULE}.module does not exist, create a list of images
1209                         for FILESYSTEM in squashfs ext2 ext3 ext4 xfs jffs2 dir
1210                         do
1211                                 for IMAGE in "${image_directory}"/*."${FILESYSTEM}"
1212                                 do
1213                                         if [ -e "${IMAGE}" ]
1214                                         then
1215                                                 image_string="${image_string} ${IMAGE}"
1216                                         fi
1217                                 done
1218                         done
1219
1220                         if [ -n "${addimage_directory}" ] && [ -d "${addimage_directory}" ]
1221                         then
1222                                 for FILESYSTEM in squashfs ext2 ext3 ext4 xfs jffs2 dir
1223                                 do
1224                                         for IMAGE in "${addimage_directory}"/*."${FILESYSTEM}"
1225                                         do
1226                                                 if [ -e "${IMAGE}" ]
1227                                                 then
1228                                                         image_string="${image_string} ${IMAGE}"
1229                                                 fi
1230                                         done
1231                                 done
1232                         fi
1233
1234                         # Now sort the list
1235                         image_string="$(echo ${image_string} | sed -e 's/ /\n/g' | sort )"
1236                 fi
1237
1238         [ -n "${MODULETORAMFILE}" ] && image_string="${image_directory}/$(basename ${MODULETORAMFILE})"
1239
1240                 mkdir -p "${croot}"
1241
1242                 for image in ${image_string}
1243                 do
1244                         imagename=$(basename "${image}")
1245
1246                         export image devname
1247                         maybe_break live-realpremount
1248                         log_begin_msg "Running /scripts/live-realpremount"
1249                         run_scripts /scripts/live-realpremount
1250                         log_end_msg
1251
1252                         if [ -d "${image}" ]
1253                         then
1254                                 # it is a plain directory: do nothing
1255                                 rofsstring="${image}=${roopt}:${rofsstring}"
1256                                 rofslist="${image} ${rofslist}"
1257                         elif [ -f "${image}" ]
1258                         then
1259                                 if losetup --help 2>&1 | grep -q -- "-r\b"
1260                                 then
1261                                         backdev=$(get_backing_device "${image}" "-r")
1262                                 else
1263                                         backdev=$(get_backing_device "${image}")
1264                                 fi
1265                                 fstype=$(get_fstype "${backdev}")
1266
1267                                 if [ "${fstype}" = "unknown" ]
1268                                 then
1269                                         panic "Unknown file system type on ${backdev} (${image})"
1270                                 fi
1271
1272                                 if [ -z "${fstype}" ]
1273                                 then
1274                                         fstype="${imagename##*.}"
1275                                         log_warning_msg "Unknown file system type on ${backdev} (${image}), assuming ${fstype}."
1276                                 fi
1277
1278                                 mkdir -p "${croot}/${imagename}"
1279                                 log_begin_msg "Mounting \"${image}\" on \"${croot}${imagename}\" via \"${backdev}\""
1280                                 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}"
1281                                 log_end_msg
1282                         fi
1283                 done
1284         else
1285                 # we have a plain root system
1286                 mkdir -p "${croot}/filesystem"
1287                 log_begin_msg "Mounting \"${image_directory}\" on \"${croot}/filesystem\""
1288                 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}"
1289                 # probably broken:
1290                 mount -o bind ${croot}/filesystem $mountpoint
1291                 log_end_msg
1292         fi
1293
1294         rofsstring=${rofsstring%:}
1295
1296         mkdir -p /cow
1297
1298         # Looking for "${root_persistence}" device or file
1299         if [ -n "${PERSISTENT}" ] && [ -z "${NOPERSISTENT}" ]
1300         then
1301                 if [ -z "${QUICKUSBMODULES}" ]
1302                 then
1303                         # Load USB modules
1304                         num_block=$(ls -l /sys/block | wc -l)
1305                         for module in sd_mod uhci-hcd ehci-hcd ohci-hcd usb-storage
1306                         do
1307                                 modprobe -q -b ${module}
1308                         done
1309
1310                         udevadm trigger
1311                         udevadm settle
1312
1313                         # For some reason, udevsettle does not block in this scenario,
1314                         # so we sleep for a little while.
1315                         #
1316                         # See https://bugs.launchpad.net/ubuntu/+source/casper/+bug/84591
1317                         for timeout in 5 4 3 2 1
1318                         do
1319                                 sleep 1
1320
1321                                 if [ $(ls -l /sys/block | wc -l) -gt ${num_block} ]
1322                                 then
1323                                         break
1324                                 fi
1325                         done
1326                 fi
1327
1328                 # search for label and files (this could be hugely optimized)
1329                 cowprobe=$(find_cow_device "${root_persistence}")
1330                 if [ -b "${cowprobe}" ]
1331                 then
1332                         # Blacklist /cow device, to avoid inconsistent setups for overlapping snapshots
1333                         # makes sense to have both persistence for /cow and /home mounted, maybe also with
1334                         # snapshots to be sure to really store some e.g key config files,
1335                         # but not on the same media
1336                         blacklistdev="${cowprobe}"
1337                         PERSISTENCE_IS_ON="1"
1338                         export PERSISTENCE_IS_ON
1339                 fi
1340                 # homecow just mount something on /home, this should be generalized some way
1341                 homecow=$(find_cow_device "${home_persistence}" "${blacklistdev}")
1342                 if [ -b "${homecow}" ]
1343                 then
1344                         PERSISTENCE_IS_ON="1"
1345                         export PERSISTENCE_IS_ON
1346                 fi
1347                 root_snapdata=$(find_snap "${root_snapshot_label}" "${blacklistdev}")
1348                 # This second type should be removed when snapshot will get smarter,
1349                 # hence when "/etc/live-snapshot*list" will be supported also by
1350                 # ext2|ext3|ext4|jffs2 snapshot types.
1351                 home_snapdata=$(find_snap "${home_snapshot_label}" "${blacklistdev}")
1352
1353                 if [ -b "${cowprobe}" ]
1354                 then
1355                         cowdevice=${cowprobe}
1356                         cow_fstype=$(get_fstype "${cowprobe}")
1357                         cow_mountopt="rw,noatime"
1358
1359                         if [ "${FORCEPERSISTENTFSCK}" = "Yes" ]
1360                         then
1361                                 fsck -y ${cowdevice}
1362                         fi
1363                 else
1364                         log_warning_msg "Unable to find the persistent medium"
1365                         cowdevice="tmpfs"
1366                         cow_fstype="tmpfs"
1367                         cow_mountopt="rw,noatime,mode=755"
1368                 fi
1369         elif [ -n "${NFS_COW}" ] && [ -z "${NOPERSISTENT}" ]
1370         then
1371                 # check if there are any nfs options
1372                 if echo ${NFS_COW}|grep -q ','
1373                 then
1374                         nfs_cow_opts="-o nolock,$(echo ${NFS_COW}|cut -d, -f2-)"
1375                         nfs_cow=$(echo ${NFS_COW}|cut -d, -f1)
1376                 else
1377                         nfs_cow_opts="-o nolock"
1378                         nfs_cow=${NFS_COW}
1379                 fi
1380                 mac="$(get_mac)"
1381                 if [ -n "${mac}" ]
1382                 then
1383                         cowdevice=$(echo ${nfs_cow}|sed "s/client_mac_address/${mac}/")
1384                         cow_fstype="nfs"
1385                 else
1386                         panic "unable to determine mac address"
1387                 fi
1388         else
1389                 cowdevice="tmpfs"
1390                 cow_fstype="tmpfs"
1391                 cow_mountopt="rw,noatime,mode=755"
1392         fi
1393
1394         if [ "${cow_fstype}" = "nfs" ]
1395         then
1396                 log_begin_msg \
1397                         "Trying nfsmount ${nfs_cow_opts} ${cowdevice} /cow"
1398                 nfsmount ${nfs_cow_opts} ${cowdevice} /cow || \
1399                         panic "Can not mount ${cowdevice} (n: ${cow_fstype}) on /cow"
1400         else
1401                 mount -t ${cow_fstype} -o ${cow_mountopt} ${cowdevice} /cow || \
1402                         panic "Can not mount ${cowdevice} (o: ${cow_fstype}) on /cow"
1403         fi
1404
1405         rofscount=$(echo ${rofslist} |wc -w)
1406
1407         if [ -n "${EXPOSED_ROOT}" ]
1408         then
1409                 if [ ${rofscount} -ne 1 ]
1410                 then
1411                         panic "only one RO file system supported with exposedroot: ${rofslist}"
1412                 fi
1413                 exposedrootfs=${rofslist%% }
1414
1415                 mount --bind ${exposedrootfs} ${rootmnt} || \
1416                         panic "bind mount of ${exposedrootfs} failed"
1417
1418                 if [ -z "${SKIP_UNION_MOUNTS}" ]
1419                 then
1420                         cow_dirs='/var/tmp /var/lock /var/run /var/log /var/spool /home /var/lib/live'
1421                 else
1422                         cow_dirs=''
1423                 fi
1424
1425                 for dir in ${cow_dirs}; do
1426                         mkdir -p /cow${dir}
1427
1428                         case "${UNIONTYPE}" in
1429                                 unionfs-fuse)
1430                                         (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}")
1431                                         mkdir -p /dev/.initramfs/varrun
1432                                         pidof unionfs-fuse >> /dev/.initramfs/varrun/sendsigs.omit || true
1433                                         ;;
1434
1435                                 *)
1436                                         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"
1437                                         ;;
1438                         esac
1439                 done
1440         else
1441                 case "${UNIONTYPE}" in
1442                         unionfs-fuse)
1443                                 (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}")
1444                                 mkdir -p /dev/.initramfs/varrun
1445                                 pidof unionfs-fuse >> /dev/.initramfs/varrun/sendsigs.omit || true
1446                                 ;;
1447
1448                         *)
1449                                 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}"
1450                                 ;;
1451                 esac
1452         fi
1453
1454         # Correct the permissions of /:
1455         chmod 0755 "${rootmnt}"
1456
1457         # tmpfs file systems
1458         touch /etc/fstab
1459         mkdir -p "${rootmnt}/live"
1460         mount -t tmpfs tmpfs ${rootmnt}/live
1461
1462         # Adding other custom mounts
1463         if [ -n "${PERSISTENT}" ] && [ -z "${NOPERSISTENT}" ]
1464         then
1465                 # directly mount /home
1466                 # FIXME: add a custom mounts configurable system
1467
1468                 if [ -b "${homecow}" ]
1469                 then
1470                         mount -t $(get_fstype "${homecow}") -o rw,noatime "${homecow}" "${rootmnt}/home"
1471                         export HOMEMOUNTED=1 # used to proper calculate free space in do_snap_copy()
1472                 else
1473                         log_warning_msg "Unable to find the persistent home medium"
1474                 fi
1475
1476                 # Look for other snapshots to copy in
1477                 try_snap "${root_snapdata}" "${rootmnt}" "ROOT"
1478                 # This second type should be removed when snapshot grow smarter
1479                 try_snap "${home_snapdata}" "${rootmnt}/home" "HOME"
1480         fi
1481
1482         if [ -n "${SHOWMOUNTS}" ]
1483         then
1484                 for d in ${rofslist}
1485                 do
1486                         mkdir -p "${rootmnt}/live/${d##*/}"
1487
1488                         case d in
1489                                 *.dir)
1490                                         # do nothing # mount -o bind "${d}" "${rootmnt}/live/${d##*/}"
1491                                         ;;
1492
1493                                 *)
1494                                         case "${UNIONTYPE}" in
1495                                                 unionfs-fuse)
1496                                                         mount -o bind "${d}" "${rootmnt}/live/${d##*/}"
1497                                                         ;;
1498
1499                                                 *)
1500                                                         mount -o move "${d}" "${rootmnt}/live/${d##*/}"
1501                                                         ;;
1502                                         esac
1503                                         ;;
1504                         esac
1505                 done
1506         fi
1507
1508         # shows cow fs on /cow for use by live-snapshot
1509         mkdir -p "${rootmnt}/live/cow"
1510         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"
1511 }
1512
1513 check_dev ()
1514 {
1515         sysdev="${1}"
1516         devname="${2}"
1517         skip_uuid_check="${3}"
1518
1519         # support for fromiso=.../isofrom=....
1520         if [ -n "$FROMISO" ]
1521         then
1522                 ISO_DEVICE=$(dirname $FROMISO)
1523                 if ! [ -b $ISO_DEVICE ]
1524                 then
1525                         # to support unusual device names like /dev/cciss/c0d0p1
1526                         # as well we have to identify the block device name, let's
1527                         # do that for up to 15 levels
1528                         i=15
1529                         while [ -n "$ISO_DEVICE" ] && [ "$i" -gt 0 ]
1530                         do
1531                                 ISO_DEVICE=$(dirname ${ISO_DEVICE})
1532                                 [ -b "$ISO_DEVICE" ] && break
1533                                 i=$(($i -1))
1534                         done
1535                 fi
1536
1537                 if [ "$ISO_DEVICE" = "/" ]
1538                 then
1539                         echo "Warning: device for bootoption isofrom= ($FROMISO) not found.">>/live.log
1540                 else
1541                         mkdir /isofrom
1542                         mount "$ISO_DEVICE" /isofrom
1543                         ISO_NAME="$(echo $FROMISO | sed "s|$ISO_DEVICE||")"
1544                         loopdevname=$(setup_loop "/isofrom/${ISO_NAME}" "loop" "/sys/block/loop*" "" '')
1545                         devname="${loopdevname}"
1546                 fi
1547         fi
1548
1549         if [ -z "${devname}" ]
1550         then
1551                 devname=$(sys2dev "${sysdev}")
1552         fi
1553
1554         if [ -d "${devname}" ]
1555         then
1556                 mount -o bind "${devname}" $mountpoint || continue
1557
1558                 if is_live_path $mountpoint
1559                 then
1560                         echo $mountpoint
1561                         return 0
1562                 else
1563                         umount $mountpoint
1564                 fi
1565         fi
1566
1567         if [ -n "${LIVE_MEDIA_OFFSET}" ]
1568         then
1569                 loopdevname=$(setup_loop "${devname}" "loop" "/sys/block/loop*" "${LIVE_MEDIA_OFFSET}" '')
1570                 devname="${loopdevname}"
1571         fi
1572
1573         fstype=$(get_fstype "${devname}")
1574
1575         if is_supported_fs ${fstype}
1576         then
1577                 mount -t ${fstype} -o ro,noatime "${devname}" ${mountpoint} || continue
1578
1579                 if is_live_path ${mountpoint} && \
1580                         ([ "${skip_uuid_check}" ] || matches_uuid ${mountpoint})
1581                 then
1582                         echo ${mountpoint}
1583                         return 0
1584                 else
1585                         umount ${mountpoint}
1586                 fi
1587         fi
1588
1589         if [ -n "${LIVE_MEDIA_OFFSET}" ]
1590         then
1591                 losetup -d "${loopdevname}"
1592         fi
1593
1594         return 1
1595 }
1596
1597 find_livefs ()
1598 {
1599         timeout="${1}"
1600
1601         # don't start autodetection before timeout has expired
1602         if [ -n "${LIVE_MEDIA_TIMEOUT}" ]
1603         then
1604                 if [ "${timeout}" -lt "${LIVE_MEDIA_TIMEOUT}" ]
1605                 then
1606                         return 1
1607                 fi
1608         fi
1609
1610         # first look at the one specified in the command line
1611         case "${LIVE_MEDIA}" in
1612                 removable-usb)
1613                         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1614                         do
1615                                 if [ "$(cat ${sysblock}/removable)" = "1" ]
1616                                 then
1617                                         if readlink ${sysblock} | grep -q usb ||
1618                                            readlink ${sysblock}/device | grep -q usb # linux < 2.6.29
1619                                         then
1620                                                 for dev in $(subdevices "${sysblock}")
1621                                                 do
1622                                                         if check_dev "${dev}"
1623                                                         then
1624                                                                 return 0
1625                                                         fi
1626                                                 done
1627                                         fi
1628                                 fi
1629                         done
1630                         return 1
1631                         ;;
1632
1633                 removable)
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                                         for dev in $(subdevices "${sysblock}")
1639                                         do
1640                                                 if check_dev "${dev}"
1641                                                 then
1642                                                         return 0
1643                                                 fi
1644                                         done
1645                                 fi
1646                         done
1647                         return 1
1648                         ;;
1649
1650                 *)
1651                         if [ ! -z "${LIVE_MEDIA}" ]
1652                         then
1653                                 if check_dev "null" "${LIVE_MEDIA}" "skip_uuid_check"
1654                                 then
1655                                         return 0
1656                                 fi
1657                         fi
1658                         ;;
1659         esac
1660
1661         # or do the scan of block devices
1662         # prefer removable devices over non-removable devices, so scan them first
1663         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1664         do
1665                 if [ "$(cat ${sysblock}/removable)" = "1" ]
1666                 then
1667                         removable_devices_to_scan="$removable_devices_to_scan $sysblock"
1668                 else
1669                         nonremovable_devices_to_scan="$nonremovable_devices_to_scan $sysblock"
1670                 fi
1671         done
1672         devices_to_scan="$removable_devices_to_scan $nonremovable_devices_to_scan"
1673
1674         for sysblock in $devices_to_scan
1675         do
1676                 devname=$(sys2dev "${sysblock}")
1677                 fstype=$(get_fstype "${devname}")
1678
1679                 if /lib/udev/cdrom_id ${devname} > /dev/null
1680                 then
1681                         if check_dev "null" "${devname}"
1682                         then
1683                                 return 0
1684                         fi
1685                 elif is_nice_device "${sysblock}"
1686                 then
1687                         for dev in $(subdevices "${sysblock}")
1688                         do
1689                                 if check_dev "${dev}"
1690                                 then
1691                                         return 0
1692                                 fi
1693                         done
1694                 elif [ "${fstype}" = "squashfs" -o \
1695                         "${fstype}" = "ext2" -o \
1696                         "${fstype}" = "ext3" -o \
1697                         "${fstype}" = "ext4" -o \
1698                         "${fstype}" = "jffs2" ]
1699                 then
1700                         # This is an ugly hack situation, the block device has
1701                         # an image directly on it.  It's hopefully
1702                         # live-initramfs, so take it and run with it.
1703                         ln -s "${devname}" "${devname}.${fstype}"
1704                         echo "${devname}.${fstype}"
1705                         return 0
1706                 fi
1707         done
1708
1709         return 1
1710 }
1711
1712 set_usplash_timeout ()
1713 {
1714         if [ -x /sbin/usplash_write ]
1715         then
1716                 /sbin/usplash_write "TIMEOUT 120"
1717         else if [ -x /sbin/splashy_update ] ; then
1718                 /sbin/splashy_update "TIMEOUT 120"
1719         fi ; fi
1720 }
1721
1722 integrity_check ()
1723 {
1724         media_mountpoint="${1}"
1725
1726         log_begin_msg "Checking media integrity"
1727
1728         cd ${media_mountpoint}
1729         /bin/md5sum -c md5sum.txt < /dev/tty8 > /dev/tty8
1730         RC="${?}"
1731
1732         log_end_msg
1733
1734         if [ "${RC}" -eq 0 ]
1735         then
1736                 log_success_msg "Everything ok, will reboot in 10 seconds."
1737                 sleep 10
1738                 cd /
1739                 umount ${media_mountpoint}
1740                 sync
1741                 echo u > /proc/sysrq-trigger
1742                 echo b > /proc/sysrq-trigger
1743         else
1744                 panic "Not ok, a media defect is likely, switch to VT8 for details."
1745         fi
1746 }
1747
1748 mountroot ()
1749 {
1750         if [ -x /scripts/local-top/cryptroot ]; then
1751             /scripts/local-top/cryptroot
1752         fi
1753
1754         exec 6>&1
1755         exec 7>&2
1756         exec > live.log
1757         exec 2>&1
1758         tail -f live.log >&7 &
1759         tailpid="${!}"
1760
1761         # Ensure 'panic' function is overridden
1762         . /scripts/live-functions
1763
1764         Arguments
1765
1766         set_usplash_timeout
1767
1768         maybe_break live-premount
1769         log_begin_msg "Running /scripts/live-premount"
1770         run_scripts /scripts/live-premount
1771         log_end_msg
1772
1773         # Needed here too because some things (*cough* udev *cough*)
1774         # changes the timeout
1775
1776         set_usplash_timeout
1777
1778         if [ ! -z "${NETBOOT}" ] || [ ! -z "${FETCH}" ] || [ ! -z "${HTTPFS}" ] || [ ! -z "${FTPFS}" ]
1779         then
1780                 if do_netmount
1781                 then
1782                         livefs_root="${mountpoint}"
1783                 else
1784                         panic "Unable to find a live file system on the network"
1785                 fi
1786         else
1787                 if [ -n "${PLAIN_ROOT}" ] && [ -n "${ROOT}" ]
1788                 then
1789                         # Do a local boot from hd
1790                         livefs_root=${ROOT}
1791                 else
1792                         # Scan local devices for the image
1793                         i=0
1794                         while [ "$i" -lt 60 ]
1795                         do
1796                                 livefs_root=$(find_livefs ${i})
1797
1798                                 if [ -n "${livefs_root}" ]
1799                                 then
1800                                         break
1801                                 fi
1802
1803                                 sleep 1
1804                                 i="$(($i + 1))"
1805                         done
1806                 fi
1807         fi
1808
1809         if [ -z "${livefs_root}" ]
1810         then
1811                 panic "Unable to find a medium containing a live file system"
1812         fi
1813
1814         if [ "${INTEGRITY_CHECK}" ]
1815         then
1816                 integrity_check "${livefs_root}"
1817         fi
1818
1819         if [ "${TORAM}" ]
1820         then
1821                 live_dest="ram"
1822         elif [ "${TODISK}" ]
1823         then
1824                 live_dest="${TODISK}"
1825         fi
1826
1827         if [ "${live_dest}" ]
1828         then
1829                 log_begin_msg "Copying live media to ${live_dest}"
1830                 copy_live_to "${livefs_root}" "${live_dest}"
1831                 log_end_msg
1832         fi
1833
1834         # if we do not unmount the ISO we can't run "fsck /dev/ice" later on
1835         # because the mountpoint is left behind in /proc/mounts, so let's get
1836         # rid of it when running from RAM
1837         if [ -n "$FROMISO" ] && [ "${TORAM}" ]
1838         then
1839           losetup -d /dev/loop0
1840           grep -q /isofrom /proc/mounts && umount /isofrom
1841         fi
1842
1843         if [ -n "${MODULETORAMFILE}" ] || [ -n "${PLAIN_ROOT}" ]
1844         then
1845                 setup_unionfs "${livefs_root}" "${rootmnt}"
1846         else
1847                 mac="$(get_mac)"
1848                 mac="$(echo ${mac} | sed 's/-//g')"
1849                 mount_images_in_directory "${livefs_root}" "${rootmnt}" "${mac}"
1850         fi
1851
1852         log_end_msg
1853
1854         # unionfs-fuse needs /dev to be bind-mounted for the duration of
1855         # live-bottom; udev's init script will take care of things after that
1856         if [ "${UNIONTYPE}" = unionfs-fuse ]
1857         then
1858                 mount -n -o bind /dev "${rootmnt}/dev"
1859         fi
1860
1861         maybe_break live-bottom
1862         log_begin_msg "Running /scripts/live-bottom\n"
1863
1864         run_scripts /scripts/live-bottom
1865         log_end_msg
1866
1867         if [ "${UNIONFS}" = unionfs-fuse ]
1868         then
1869                 umount "${rootmnt}/dev"
1870         fi
1871
1872         exec 1>&6 6>&-
1873         exec 2>&7 7>&-
1874         kill ${tailpid}
1875         [ -w "${rootmnt}/var/log/" ] && cp live.log "${rootmnt}/var/log/" 2>/dev/null
1876         if [ -f /etc/live.conf ]
1877         then
1878                 cp /etc/live.conf "${rootmnt}/etc/"
1879         fi
1880 }