Adding patch from Michal Suchanek <hramrach@centrum.cz> to tail live.log and show...
[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 [ -f /etc/live.conf ] && . /etc/live.conf
25 export USERNAME USERFULLNAME HOSTNAME
26
27 . /scripts/live-helpers
28
29 if [ ! -f /live.vars ]
30 then
31         touch /live.vars
32 fi
33
34 Arguments ()
35 {
36         PRESEEDS=""
37
38         for ARGUMENT in $(cat /proc/cmdline)
39         do
40                 case "${ARGUMENT}" in
41                         access=*)
42                                 ACCESS="${ARGUMENT#access=}"
43                                 export ACCESS
44                                 ;;
45
46                         console=*)
47                                 DEFCONSOLE="${ARGUMENT#*=}"
48                                 export DEFCONSOLE
49                                 ;;
50
51                         debug)
52                                 DEBUG="Yes"
53                                 export DEBUG
54
55                                 set -x
56                                 ;;
57
58                         fetch=*)
59                                 FETCH="${ARGUMENT#fetch=}"
60                                 export FETCH
61                                 ;;
62
63                         hook=*)
64                                 HOOK="${ARGUMENT#hook=}"
65                                 export HOOK
66                                 ;;
67
68                         hostname=*)
69                                 HOSTNAME="${ARGUMENT#hostname=}"
70                                 LIVECONF="changed"
71                                 export HOSTNAME LIVECONF
72                                 ;;
73
74                         username=*)
75                                 USERNAME="${ARGUMENT#username=}"
76                                 LIVECONF="changed"
77                                 export USERNAME LIVECONF
78                                 ;;
79
80                         userfullname=*)
81                                 USERFULLNAME="${ARGUMENT#userfullname=}"
82                                 LIVECONF="changed"
83                                 export USERFULLNAME LIVECONF
84                                 ;;
85
86                         ignore_uuid)
87                                 IGNORE_UUID="Yes"
88                                 export IGNORE_UUID
89                                 ;;
90
91                         ip=*)
92                                 STATICIP="${ARGUMENT#ip=}"
93
94                                 if [ -z "${STATICIP}" ]
95                                 then
96                                         STATICIP="frommedia"
97                                 fi
98
99                                 export STATICIP
100                                 ;;
101
102                         keyb=*|kbd-chooser/method=*)
103                                 KBD="${ARGUMENT#*=}"
104                                 export KBD
105                                 ;;
106
107                         klayout=*|console-setup/layoutcode=*)
108                                 KLAYOUT="${ARGUMENT#*=}"
109                                 export KLAYOUT
110                                 ;;
111
112                         kvariant=*|console-setup/variantcode=*)
113                                 KVARIANT="${ARGUMENT#*=}"
114                                 export KVARIANT
115                                 ;;
116
117                         kmodel=*|console-setup/modelcode=*)
118                                 KMODEL="${ARGUMENT#*=}"
119                                 export KMODEL
120                                 ;;
121
122                         koptions=*)
123                                 KOPTIONS="${ARGUMENT#koptions=}"
124                                 export KOPTIONS
125                                 ;;
126
127                         live-getty)
128                                 LIVE_GETTY="1"
129                                 export LIVE_GETTY
130                                 ;;
131
132                         live-media=*|bootfrom=*)
133                                 LIVE_MEDIA="${ARGUMENT#*=}"
134                                 export LIVE_MEDIA
135                                 ;;
136
137                         live-media-encryption=*|encryption=*)
138                                 LIVE_MEDIA_ENCRYPTION="${ARGUMENT#*=}"
139                                 export LIVE_MEDIA_ENCRYPTION
140                                 ;;
141
142                         live-media-offset=*)
143                                 LIVE_MEDIA_OFFSET="${ARGUMENT#live-media-offset=}"
144                                 export LIVE_MEDIA_OFFSET
145                                 ;;
146
147                         live-media-path=*)
148                                 LIVE_MEDIA_PATH="${ARGUMENT#live-media-path=}"
149                                 export LIVE_MEDIA_PATH
150                                 ;;
151
152                         live-media-timeout=*)
153                                 LIVE_MEDIA_TIMEOUT="${ARGUMENT#live-media-timeout=}"
154                                 export LIVE_MEDIA_TIMEOUT
155                                 ;;
156
157                         language=*|debian-installer/language=*)
158                                 language=${x#debian-installer/language=}
159                                 locale="$(lang2locale "$language")"
160                                 set_locale="true"
161                                 ;;
162
163                         locale=*|debian-installer/locale=*)
164                                 LOCALE="${ARGUMENT#*=}"
165                                 export LOCALE
166                                 ;;
167
168                         module=*)
169                                 MODULE="${ARGUMENT#module=}"
170                                 export MODULE
171                                 ;;
172
173                         netboot=*)
174                                 NETBOOT="${ARGUMENT#netboot=}"
175                                 export NETBOOT
176                                 ;;
177
178                         nfsopts=*)
179                                 NFSOPTS="${ARGUMENT#nfsopts=}"
180                                 export NFSOPTS
181                                 ;;
182
183                         nfscow=*)
184                                 NFS_COW="${ARGUMENT#nfscow=}"
185                                 export NFS_COW
186                                 ;;
187
188                         noaccessibility)
189                                 NOACCESSIBILITY="Yes"
190                                 export NOACCESSIBILITY
191                                 ;;
192
193                         noapparmor)
194                                 NOAPPARMOR="Yes"
195                                 export NOAPPARMOR
196                                 ;;
197
198                         noaptcdrom)
199                                 NOAPTCDROM="Yes"
200                                 export NOAPTCDROM
201                                 ;;
202
203                         noautologin)
204                                 NOAUTOLOGIN="Yes"
205                                 export NOAUTOLOGIN
206                                 ;;
207
208                         noxautologin)
209                                 NOXAUTOLOGIN="Yes"
210                                 export NOXAUTOLOGIN
211                                 ;;
212
213                         noconsolekeyboard)
214                                 NOCONSOLEKEYBOARD="Yes"
215                                 export NOCONSOLEKEYBOARD
216                                 ;;
217
218                         nofastboot)
219                                 NOFASTBOOT="Yes"
220                                 export NOFASTBOOT
221                                 ;;
222
223                         nofstab)
224                                 NOFSTAB="Yes"
225                                 export NOFSTAB
226                                 ;;
227
228                         nognomepanel)
229                                 NOGNOMEPANEL="Yes"
230                                 export NOGNOMEPANEL
231                                 ;;
232
233                         nohosts)
234                                 NOHOSTS="Yes"
235                                 export NOHOSTS
236                                 ;;
237
238                         nokpersonalizer)
239                                 NOKPERSONALIZER="Yes"
240                                 export NOKPERSONALIZER
241                                 ;;
242
243                         nolanguageselector)
244                                 NOLANGUAGESELECTOR="Yes"
245                                 export NOLANGUAGESELECTOR
246                                 ;;
247
248                         nolocales)
249                                 NOLOCALES="Yes"
250                                 export NOLOCALES
251                                 ;;
252
253                         nonetworking)
254                                 NONETWORKING="Yes"
255                                 export NONETWORKING
256                                 ;;
257
258                         nopowermanagement)
259                                 NOPOWERMANAGEMENT="Yes"
260                                 export NOPOWERMANAGEMENT
261                                 ;;
262
263                         noprogramcrashes)
264                                 NOPROGRAMCRASHES="Yes"
265                                 export NOPROGRAMCRASHES
266                                 ;;
267
268                         norestrictedmanager)
269                                 NORESTRICTEDMANAGER="Yes"
270                                 export NORESTRICTEDMANAGER
271                                 ;;
272
273                         nosudo)
274                                 NOSUDO="Yes"
275                                 export NOSUDO
276                                 ;;
277
278                         noswap)
279                                 NOSWAP="Yes"
280                                 export NOSWAP
281                                 ;;
282
283                         noupdatenotifier)
284                                 NOUPDATENOTIFIER="Yes"
285                                 export NOUPDATENOTIFIER
286                                 ;;
287
288                         nouser)
289                                 NOUSER="Yes"
290                                 export NOUSER
291                                 ;;
292
293                         noxautoconfig)
294                                 NOXAUTOCONFIG="Yes"
295                                 export NOXAUTOCONFIG
296                                 ;;
297
298                         noxscreensaver)
299                                 NOXSCREENSAVER="Yes"
300                                 export NOXSCREENSAVER
301                                 ;;
302
303                         persistent)
304                                 PERSISTENT="Yes"
305                                 export PERSISTENT
306                                 ;;
307
308                         nopersistent)
309                                 NOPERSISTENT="Yes"
310                                 export NOPERSISTENT
311                                 ;;
312
313                         preseed/file=*|file=*)
314                                 LOCATION="${ARGUMENT#*=}"
315                                 export LOCATION
316                                 ;;
317
318                         nopreseed)
319                                 NOPRESEED="Yes"
320                                 export NOPRESEED
321                                 ;;
322
323                         url=*)
324                                 location="${ARGUMENT#url=}"
325
326                                 mount -n -o bind /sys /root/sys
327                                 mount -n -o bind /proc /root/proc
328                                 mount -n -o bind /dev /root/dev
329
330                                 mkdir -p /root/var/run/network
331                                 chroot /root dhclient eth0
332                                 chroot /root wget -P /tmp "${location}"
333                                 chroot /root ifconfig eth0 down
334
335                                 umount /root/sys
336                                 umount /root/proc
337                                 umount /root/dev
338
339                                 LOCATION="/tmp/$(basename "${location}")"
340                                 ;;
341
342                         */*=*)
343                                 question="${ARGUMENT%%=*}"
344                                 value="${ARGUMENT#*=}"
345                                 PRESEEDS="${PRESEEDS}\"${question}=${value}\" "
346                                 export PRESEEDS
347                                 ;;
348
349                         showmounts)
350                                 SHOWMOUNTS="Yes"
351                                 export SHOWMOUNTS
352                                 ;;
353
354                         textonly)
355                                 TEXTONLY="Yes"
356                                 export TEXTONLY
357                                 ;;
358
359                         timezone=*)
360                                 TIMEZONE="${ARGUMENT#timezone=}"
361                                 export TIMEZONE
362                                 ;;
363
364                         notimezone)
365                                 NOTIMEZONE="Yes"
366                                 export NOTIMEZONE
367                                 ;;
368
369                         todisk=*)
370                                 TODISK="${ARGUMENT#todisk=}"
371                                 export TODISK
372                                 ;;
373
374                         toram)
375                                 TORAM="Yes"
376                                 export TORAM
377                                 ;;
378
379                         toram=*)
380                                 TORAM="Yes"
381                                 MODULETORAM="${ARGUMENT#toram=}"
382                                 export TORAM MODULETORAM
383                                 ;;
384
385                         exposedroot)
386                                 EXPOSED_ROOT="Yes"
387                                 export EXPOSED_ROOT
388                                 ;;
389
390                         plainroot)
391                                 PLAIN_ROOT="Yes"
392                                 export PLAIN_ROOT
393                                 ;;
394
395                         root=*)
396                                 ROOT="${ARGUMENT#root=}"
397                                 export ROOT
398                                 ;;
399
400                         union=*)
401                                 UNIONTYPE="${ARGUMENT#union=}"
402                                 export UNIONTYPE
403                                 ;;
404
405                         utc=*)
406                                 UTC="${ARGUMENT#utc=}"
407                                 export UTC
408                                 ;;
409
410                         xdebconf)
411                                 XDEBCONF="Yes"
412                                 export XDEBCONF
413                                 ;;
414
415                         xdriver=*)
416                                 XDRIVER="${ARGUMENT#xdriver=}"
417                                 export XDRIVER
418                                 ;;
419
420                         xvideomode=*)
421                                 XVIDEOMODE="${ARGUMENT#xvideomode=}"
422                                 export XVIDEOMODE
423                                 ;;
424                 esac
425         done
426
427         # sort of compatibility with netboot.h from linux docs
428         if [ -z "${NETBOOT}" ]
429         then
430                 if [ "${ROOT}" = "/dev/nfs" ]
431                 then
432                         NETBOOT="nfs"
433                         export NETBOOT
434                 elif [ "${ROOT}" = "/dev/cifs" ]
435                 then
436                         NETBOOT="cifs"
437                         export NETBOOT
438                 fi
439         fi
440
441         if [ -z "${MODULE}" ]
442         then
443                 MODULE="filesystem"
444                 export MODULE
445         fi
446
447         if [ -z "${UNIONTYPE}" ]
448         then
449                 UNIONTYPE="unionfs"
450                 export UNIONTYPE
451         fi
452 }
453
454 is_live_path ()
455 {
456         DIRECTORY="${1}"
457
458         if [ -d "${DIRECTORY}"/"${LIVE_MEDIA_PATH}" ]
459         then
460                 for FILESYSTEM in squashfs ext2 ext3 xfs dir jffs2
461                 do
462                         if [ "$(echo ${DIRECTORY}/${LIVE_MEDIA_PATH}/*.${FILESYSTEM})" != "${DIRECTORY}/${LIVE_MEDIA_PATH}/*.${FILESYSTEM}" ]
463                         then
464                                 return 0
465                         fi
466                 done
467         fi
468
469         return 1
470 }
471
472 matches_uuid ()
473 {
474         if [ "${IGNORE_UUID}" ] || [ ! -e /conf/uuid.conf ]
475         then
476                 return 0
477         fi
478
479         path="${1}"
480         uuid="$(cat /conf/uuid.conf)"
481
482         for try_uuid_file in "${mountpoint}/.disk/casper-uuid"*
483         do
484                 [ -e "${try_uuid_file}" ] || continue
485
486                 try_uuid="$(cat "${try_uuid_file}")"
487
488                 if [ "${uuid}" = "${try_uuid}" ]
489                 then
490                         return 0
491                 fi
492         done
493
494         return 1
495 }
496
497 get_backing_device ()
498 {
499         case "${1}" in
500                 *.squashfs|*.ext2|*.ext3|*.jffs2)
501                         echo $(setup_loop "${1}" "loop" "/sys/block/loop*" '0' "${LIVE_MEDIA_ENCRYPTION}")
502                         ;;
503
504                 *.dir)
505                         echo "directory"
506                         ;;
507
508                 *)
509                         panic "Unrecognized live filesystem: ${1}"
510                         ;;
511         esac
512 }
513
514 match_files_in_dir ()
515 {
516         # Does any files match pattern ${1} ?
517         local pattern="${1}"
518
519         if [ "$(echo ${pattern})" != "${pattern}" ]
520         then
521                 return 0
522         fi
523
524         return 1
525 }
526
527 mount_images_in_directory ()
528 {
529         directory="${1}"
530         rootmnt="${2}"
531         mac="${3}"
532
533
534         if match_files_in_dir "${directory}/${LIVE_MEDIA_PATH}/*.squashfs" ||
535                 match_files_in_dir "${directory}/${LIVE_MEDIA_PATH}/*.ext2" ||
536                 match_files_in_dir "${directory}/${LIVE_MEDIA_PATH}/*.ext3" ||
537                 match_files_in_dir "${directory}/${LIVE_MEDIA_PATH}/*.jffs2" ||
538                 match_files_in_dir "${directory}/${LIVE_MEDIA_PATH}/*.dir"
539         then
540                 [ -n "${mac}" ] && adddirectory="${directory}/${LIVE_MEDIA_PATH}/${mac}"
541                 setup_unionfs "${directory}/${LIVE_MEDIA_PATH}" "${rootmnt}" "${adddirectory}"
542         else
543                 :
544         fi
545 }
546
547 is_nice_device ()
548 {
549         sysfs_path="${1#/sys}"
550
551         if /lib/udev/path_id "${sysfs_path}" | grep -E -q "ID_PATH=(usb|pci-[^-]*-[ide|scsi|usb])"
552         then
553                 return 0
554         fi
555
556         return 1
557 }
558
559 is_supported_fs ()
560 {
561         # FIXME: do something better like the scan of supported filesystems
562         fstype="${1}"
563
564         case ${fstype} in
565                 vfat|iso9660|udf|ext2|ext3|ntfs|jffs2)
566                         return 0
567                         ;;
568         esac
569
570         return 1
571 }
572
573 copy_live_to ()
574 {
575         copyfrom="${1}"
576         copytodev="${2}"
577         copyto="${copyfrom}_swap"
578
579         if [ -z "${MODULETORAM}" ]
580         then
581                 size=$(fs_size "" ${copyfrom} "used")
582         else
583                 MODULETORAMFILE="${copyfrom}/${LIVE_MEDIA_PATH}/${MODULETORAM}"
584
585                 if [ -f "${MODULETORAMFILE}" ]
586                 then
587                         size=$( expr $(ls -la ${MODULETORAMFILE} | awk '{print $5}') / 1024 + 5000 )
588                 else
589                         log_warning_msg "Error: toram-module ${MODULETORAM} (${MODULETORAMFILE}) could not be read."
590                         return 1
591                 fi
592         fi
593
594         if [ "${copytodev}" = "ram" ]
595         then
596                 # copying to ram:
597                 freespace=$( expr $(awk '/MemFree/{print $2}' /proc/meminfo) + $( cat /proc/meminfo | grep Cached | head -n 1 | awk '/Cached/{print $2}' - ) )
598                 mount_options="-o size=${size}k"
599                 free_string="memory"
600                 fstype="tmpfs"
601                 dev="/dev/shm"
602         else
603                 # it should be a writable block device
604                 if [ -b "${copytodev}" ]
605                 then
606                         dev="${copytodev}"
607                         free_string="space"
608                         fstype=$(get_fstype "${dev}")
609                         freespace=$(fs_size "${dev}")
610                 else
611                         [ "$quiet" != "y" ] && log_warning_msg "${copytodev} is not a block device."
612                         return 1
613                 fi
614         fi
615
616         if [ "${freespace}" -lt "${size}" ]
617         then
618                 [ "${quiet}" != "y" ] && log_warning_msg "Not enough free ${free_string} (${freespace}k free, ${size}k needed) to copy live media in ${copytodev}."
619                 return 1
620         fi
621
622         # begin copying (or uncompressing)
623         mkdir "${copyto}"
624         echo "mount -t ${fstype} ${mount_options} ${dev} ${copyto}"
625         mount -t "${fstype}" ${mount_options} "${dev}" "${copyto}"
626
627         if [ "${extension}" = "tgz" ]
628         then
629                 cd "${copyto}"
630                 tar zxf "${copyfrom}/${LIVE_MEDIA_PATH}/$(basename ${FETCH})"
631                 rm -f "${copyfrom}/${LIVE_MEDIA_PATH}/$(basename ${FETCH})"
632                 mount -r --move "${copyto}" "${rootmnt}"
633                 cd "${OLDPWD}"
634         else
635                 if [ -n "${MODULETORAMFILE}" ]
636                 then
637                         cp ${MODULETORAMFILE} ${copyto} # copy only the filesystem module
638                 else
639                         cp -a ${copyfrom}/* ${copyto}   # "cp -a" from busybox also copies hidden files
640                 fi
641
642                 livefs_root
643                 umount ${copyfrom}
644                 mount -r --move ${copyto} ${copyfrom}
645         fi
646
647         rmdir ${copyto}
648         return 0
649 }
650
651 do_netmount ()
652 {
653         rc=1
654
655         modprobe -q af_packet # For DHCP
656
657         udevtrigger
658         udevsettle
659
660         ipconfig ${DEVICE} | tee /netboot.config
661
662         # source relevant ipconfig output
663         OLDHOSTNAME=${HOSTNAME}
664         . /tmp/net-${DEVICE}.conf
665         [ -z ${HOSTNAME} ] && HOSTNAME=${OLDHOSTNAME}
666         export HOSTNAME
667
668         if [ "${NFSROOT}" = "auto" ]
669         then
670                 NFSROOT=${ROOTSERVER}:${ROOTPATH}
671         fi
672
673         if [ -n "${FETCH}" ] && do_httpmount
674         then
675                 rc=0
676                 return ${rc}
677         fi
678
679         if [ "${NFSROOT#*:}" = "${NFSROOT}" ] && [ "$NETBOOT" != "cifs" ]
680         then
681                 NFSROOT=${ROOTSERVER}:${NFSROOT}
682         fi
683
684         [ "${quiet}" != "y" ] && log_begin_msg "Trying netboot from ${NFSROOT}"
685
686         if [ "${NETBOOT}" != "nfs" ] && do_cifsmount
687         then
688                 rc=0
689         elif do_nfsmount
690         then
691                 NETBOOT="nfs"
692                 export NETBOOT
693                 rc=0
694         fi
695
696         [ "${quiet}" != "y" ] && log_end_msg
697         return ${rc}
698 }
699
700 do_httpmount ()
701 {
702         rc=1
703         extension=$(echo "${FETCH}" | sed 's/\(.*\)\.\(.*\)/\2/')
704
705         case "${extension}" in
706                 squashfs|tgz|tar)
707                         [ "${quiet}" != "y" ] && log_begin_msg "Trying wget ${FETCH} -O ${mountpoint}/$(basename ${FETCH})"
708                         mkdir -p "${mountpoint}/${LIVE_MEDIA_PATH}"
709                         wget "${FETCH}" -O "${mountpoint}/${LIVE_MEDIA_PATH}/$(basename ${FETCH})"
710                         [ ${?} -eq 0 ] && rc=0
711                         [ "${extension}" = "tgz" ] && live_dest="ram"
712                         ;;
713
714                 *)
715                         [ "${quiet}" != "y" ] && log_begin_msg "Unrecognized archive extension for ${FETCH}"
716         esac
717
718         return ${rc}
719 }
720
721 do_nfsmount ()
722 {
723         rc=1
724
725         modprobe -q nfs
726
727         if [ -z "${NFSOPTS}" ]
728         then
729                 NFSOPTS=""
730         fi
731
732         [ "${quiet}" != "y" ] && log_begin_msg "Trying nfsmount -o nolock -o ro ${NFSOPTS} ${NFSROOT} ${mountpoint}"
733
734         # FIXME: This for loop is an ugly HACK round an nfs bug
735         for i in 0 1 2 3 4 5 6 7 8 9 a b c d e f 10 11 12 13
736         do
737                 nfsmount -o nolock -o ro ${NFSOPTS} "${NFSROOT}" "${mountpoint}" && rc=0 && break
738                 sleep 1
739         done
740
741         return ${rc}
742 }
743
744 do_cifsmount ()
745 {
746         rc=1
747
748         if [ -x "/sbin/mount.cifs" ]
749         then
750                 if [ -z "${NFSOPTS}" ]
751                 then
752                         CIFSOPTS="-ouser=root,password="
753                 else
754                         CIFSOPTS="${NFSOPTS}"
755                 fi
756
757                 [ "${quiet}" != "y" ] && log_begin_msg "Trying mount.cifs ${NFSROOT} ${mountpoint} ${CIFSOPTS}"
758                 modprobe -q cifs
759
760                 if mount.cifs "${NFSROOT}" "${mountpoint}" "${CIFSOPTS}"
761                 then
762                         rc=0
763                 fi
764         fi
765
766         return ${rc}
767 }
768
769 do_snap_copy ()
770 {
771         fromdev="${1}"
772         todir="${2}"
773         snap_type="${3}"
774         size=$(fs_size "${fromdev}" "" "used")
775
776         if [ -b "${fromdev}" ]
777         then
778                 # look for free mem
779                 if [ -n "${HOMEMOUNTED}" -a "${snap_type}" = "HOME" ]
780                 then
781                         todev=$(cat /proc/mounts | grep -s " $(base_path ${todir}) " | awk '{print $1}' )
782                         freespace=$(df -k  | grep -s ${todev} | awk '{print $4}')
783                 else
784                         freespace=$( expr $(awk '/MemFree/{print $2}' /proc/meminfo) + $( cat /proc/meminfo | grep Cached | head -n 1 | awk '/Cached/{print $2}' - ))
785                 fi
786
787                 tomount="/mnt/tmpsnap"
788
789                 if [ ! -d "${tomount}" ]
790                 then
791                         mkdir -p "${tomount}"
792                 fi
793
794                 fstype=$(get_fstype "${fromdev}")
795
796                 if [ -n "${fstype}" ]
797                 then
798                         # Copying stuff...
799                         mount -t "${fstype}" -o ro,noatime "${fromdev}" "${tomount}"
800                         cp -a "${tomount}"/* ${todir}
801                         umount "${tomount}"
802                 else
803                         log_warning_msg "Unrecognized fstype: ${fstype} on ${fromdev}:${snap_type}"
804                 fi
805
806                 rmdir "${tomount}"
807
808                 if echo ${fromdev} | grep -qs loop
809                 then
810                         losetup -d "${fromdev}"
811                 fi
812
813                 return 0
814         else
815                 return 1
816
817                 [ "${quiet}" != "y" ] && log_warning_msg "Unable to find the snapshot ${snap_type} medium"
818         fi
819 }
820
821 try_snap ()
822 {
823         # Look for ${snap_label}.* in block devices and copy the contents to ${snap_mount}
824         # and remember the device and filename for resync on exit in live-initramfs.init
825
826         snap_label="${1}"
827         snap_mount="${2}"
828         snap_type="${3}"
829         snapdata=$(find_files "${snap_label}.squashfs ${snap_label}.cpio.gz ${snap_label}.ext2 ${snap_label}.ext3 ${snap_label}.jffs2")
830
831         if [ ! -z "${snapdata}" ]
832         then
833                 snapdev="$(echo ${snapdata} | cut -f1 -d ' ')"
834                 snapback="$(echo ${snapdata} | cut -f2 -d ' ')"
835                 snapfile="$(echo ${snapdata} | cut -f3 -d ' ')"
836
837                 if echo "${snapfile}" | grep -qs '\(squashfs\|ext2\|ext3\|jffs2\)'
838                 then
839                         # squashfs, jffs2 or ext2/ext3 snapshot
840                         dev=$(get_backing_device "${snapback}/${snapfile}")
841
842                         if ! do_snap_copy "${dev}" "${snap_mount}" "${snap_type}"
843                         then
844                                 log_warning_msg "Impossible to include the ${snapfile} Snapshot"
845                                 return 1
846                         fi
847                 else
848                         # cpio.gz snapshot
849                         if ! (cd "${snap_mount}" && zcat "${snapback}/${snapfile}" | cpio -i -u -d 2>/dev/null)
850                         then
851                                 log_warning_msg "Impossible to include the ${snapfile} Snapshot"
852                                 return 1
853                         fi
854                 fi
855
856                 umount "${snapback}"
857         else
858                 dev=$(find_cow_device "${snap_label}")
859
860                 if [ -b ${dev} ]
861                 then
862                         if echo "${dev}" | grep -qs loop
863                         then
864                                 # strange things happens, user confused?
865                                 snaploop=$( losetup ${dev} | awk '{print $3}' | tr -d '()' )
866                                 snapfile=$(basename ${snaploop})
867                                 snapdev=$(cat /proc/mounts | awk '{print $2,$1}' | grep -es "^$( dirname ${snaploop} )" | cut -f2 -d ' ')
868                         else
869                                 snapdev="${dev}"
870                         fi
871
872                         if ! do_snap_copy "${dev}" "${snap_mount}" "${snap_type}"
873                         then
874                                 log_warning_msg "Impossible to include the ${snap_label} Snapshot"
875                                 return 1
876                         else
877                                 if [ -n "${snapfile}" ]
878                                 then
879                                         # it was a loop device, user confused
880                                         umount ${snapdev}
881                                 fi
882                         fi
883                 else
884                         log_warning_msg "Impossible to include the ${snap_label} Snapshot"
885                         return 1
886                 fi
887         fi
888
889         echo "export ${snap_type}SNAP="${snap_mount}":${snapdev}:${snapfile}" >> /etc/live.conf # for resync on reboot/halt
890         return 0
891 }
892
893 setup_unionfs ()
894 {
895         image_directory="${1}"
896         rootmnt="${2}"
897         addimage_directory="${3}"
898         modprobe -q -b ${UNIONTYPE}
899
900         # run-init can't deal with images in a subdir, but we're going to
901         # move all of these away before it runs anyway.  No, we're not,
902         # put them in / since move-mounting them into / breaks mono and
903         # some other apps.
904
905         croot="/"
906
907         # Let's just mount the read-only file systems first
908         rofsstring=""
909         rofslist=""
910         minor_kernel_version=$(uname -r|cut -c 5-|sed 's/[^0-9].*//')
911
912         if [ "${NETBOOT}" = "nfs" ] && [ "${minor_kernel_version}" -lt 22 ]
913         then
914                 # go aroung a bug in nfs-unionfs locking for unionfs <= 1.4
915                 roopt="nfsro"
916         elif [ "${UNIONTYPE}" = "aufs" ]
917         then
918                 roopt="rr"
919         else
920                 roopt="ro"
921         fi
922
923         # Read image names from ${MODULE}.module if it exists
924         if [ -e "${image_directory}/filesystem.${MODULE}.module" ]
925         then
926                 for IMAGE in $(cat ${image_directory}/filesystem.${MODULE}.module)
927                 do
928                         image_string="${image_string} ${image_directory}/${IMAGE}"
929                 done
930         elif [ -e "${image_directory}/${MODULE}.module" ]
931         then
932                 for IMAGE in $(cat ${image_directory}/${MODULE}.module)
933                 do
934                         image_string="${image_string} ${image_directory}/${IMAGE}"
935                 done
936         else
937                 # ${MODULE}.module does not exist, create a list of images
938                 for FILESYSTEM in squashfs ext2 ext3 xfs jffs2 dir
939                 do
940                         for IMAGE in "${image_directory}"/*."${FILESYSTEM}"
941                         do
942                                 if [ -e "${IMAGE}" ]
943                                 then
944                                         image_string="${image_string} ${IMAGE}"
945                                 fi
946                         done
947                 done
948
949                 if [ -n "${addimage_directory}" ] && [ -d "${addimage_directory}" ]
950                 then
951                         for FILESYSTEM in squashfs ext2 ext3 xfs jffs2 dir
952                         do
953                                 for IMAGE in "${addimage_directory}"/*."${FILESYSTEM}"
954                                 do
955                                         if [ -e "${IMAGE}" ]
956                                         then
957                                                 image_string="${image_string} ${IMAGE}"
958                                         fi
959                                 done
960                         done
961                 fi
962
963                 # Now sort the list
964                 image_string="$(echo ${image_string} | sed -e 's/ /\n/g' | sort )"
965         fi
966
967         [ -n "${MODULETORAMFILE}" ] && image_string="${image_directory}/$(basename ${MODULETORAMFILE})"
968
969         mkdir -p "${croot}"
970
971         for image in ${image_string}
972         do
973                 imagename=$(basename "${image}")
974
975                 if [ -d "${image}" ]
976                 then
977                         # it is a plain directory: do nothing
978                         rofsstring="${image}=${roopt}:${rofsstring}"
979                         rofslist="${image} ${rofslist}"
980                 elif [ -f "${image}" ]
981                 then
982                         backdev=$(get_backing_device "${image}")
983                         fstype=$(get_fstype "${backdev}")
984
985                         if [ "${fstype}" = "unknown" ]
986                         then
987                                 panic "Unknown file system type on ${backdev} (${image})"
988                         fi
989
990                         mkdir -p "${croot}/${imagename}"
991                         echo "debug: Mounting backdev \"${backdev}\" (image = ${image}) on croot/imagename \"${croot}/${imagename}\""
992                         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}"
993                 fi
994         done
995
996         rofsstring=${rofsstring%:}
997
998         mkdir -p /cow
999
1000         # Looking for "${root_persistence}" device or file
1001         if [ -n "${PERSISTENT}" ] && [ -z "${NOPERSISTENT}" ]
1002         then
1003                 # Load USB modules
1004                 num_block=$(ls -l /sys/block | wc -l)
1005                 for module in sd_mod uhci-hcd ehci-hcd ohci-hcd usb-storage
1006                 do
1007                         modprobe -q -b ${module}
1008                 done
1009
1010                 udevtrigger
1011                 udevsettle
1012
1013                 # For some reason, udevsettle does not block in this scenario,
1014                 # so we sleep for a little while.
1015                 #
1016                 # See https://bugs.launchpad.net/ubuntu/+source/casper/+bug/84591
1017                 for timeout in 5 4 3 2 1
1018                 do
1019                         sleep 1
1020
1021                         if [ $(ls -l /sys/block | wc -l) -gt ${num_block} ]
1022                         then
1023                                 break
1024                         fi
1025                 done
1026
1027                 cowprobe=$(find_cow_device "${root_persistence}")
1028
1029                 if [ -b "${cowprobe}" ]
1030                 then
1031                         cowdevice=${cowprobe}
1032                         cow_fstype=$(get_fstype "${cowprobe}")
1033                 else
1034                         [ "${quiet}" != "y" ] && log_warning_msg "Unable to find the persistent medium"
1035                         cowdevice="tmpfs"
1036                         cow_fstype="tmpfs"
1037                 fi
1038         elif [ -n "${NFS_COW}" ] && [ -z "${NOPERSISTENT}" ]
1039         then
1040                 # check if there are any nfs options
1041                 if echo ${NFS_COW}|grep -q ','
1042                 then
1043                         nfs_cow_opts="-o nolock,$(echo ${NFS_COW}|cut -d, -f2-)"
1044                         nfs_cow=$(echo ${NFS_COW}|cut -d, -f1)
1045                 else
1046                         nfs_cow_opts="-o nolock"
1047                         nfs_cow=${NFS_COW}
1048                 fi
1049                 mac="$(get_mac)"
1050                 if [ -n "${mac}" ]
1051                 then
1052                         cowdevice=$(echo ${nfs_cow}|sed "s/client_mac_address/${mac}/")
1053                         cow_fstype="nfs"
1054                 else
1055                         panic "unable to determine mac address"
1056                 fi
1057         else
1058                 cowdevice="tmpfs"
1059                 cow_fstype="tmpfs"
1060         fi
1061
1062         if [ "${cow_fstype}" = "nfs" ]
1063         then
1064                 [ "${quiet}" != "y" ] && log_begin_msg \
1065                         "Trying nfsmount ${nfs_cow_opts} ${cowdevice} /cow"
1066                 nfsmount ${nfs_cow_opts} ${cowdevice} /cow || \
1067                         panic "Can not mount ${cowdevice} (n: ${cow_fstype}) on /cow"
1068         else
1069                 mount -t ${cow_fstype} -o rw,noatime ${cowdevice} /cow || \
1070                         panic "Can not mount ${cowdevice} (o: ${cow_fstype}) on /cow"
1071         fi
1072
1073         rofscount=$(echo ${rofslist} |wc -w)
1074
1075         if [ -n "${EXPOSED_ROOT}" ]
1076         then
1077                 if [ ${rofscount} -ne 1 ]
1078                 then
1079                         panic "only one RO file system supported with exposedroot: ${rofslist}"
1080                 fi
1081                 exposedrootfs=${rofslist%% }
1082
1083                 mount --bind ${exposedrootfs} ${rootmnt} || \
1084                         panic "bind mount of ${exposedrootfs} failed"
1085
1086                 cow_dirs='/var/tmp /var/lock /var/run /var/log /var/spool
1087                         /home /var/lib/live'
1088
1089                 for dir in ${cow_dirs}; do
1090                         mkdir -p /cow${dir}
1091                         mount -t ${UNIONTYPE} \
1092                                 -o rw,noatime,dirs=/cow${dir}=rw:${exposedrootfs}${dir}=ro \
1093                                 ${UNIONTYPE} "${rootmnt}${dir}" || \
1094                                 panic "mount ${UNIONTYPE} on ${rootmnt}${dir} failed with option \
1095                                         rw,noatime,dirs=/cow${dir}=rw:${exposedrootfs}${dir}=ro"
1096                 done
1097         else
1098                 mount -t ${UNIONTYPE} -o noatime,dirs=/cow=rw:${rofsstring} \
1099                         ${UNIONTYPE} "${rootmnt}" || panic "mount ${UNIONTYPE} on \
1100                         ${rootmnt} failed with option noatime,dirs=/cow=rw:${rofsstring}"
1101         fi
1102
1103         # tmpfs file systems
1104         mkdir -p "${rootmnt}/live"
1105         mount -t tmpfs tmpfs ${rootmnt}/live
1106
1107         # Adding other custom mounts
1108         if [ -n "${PERSISTENT}" ] && [ -z "${NOPERSISTENT}" ]
1109         then
1110                 # directly mount /home
1111                 # FIXME: add a custom mounts configurable system
1112                 homecow=$(find_cow_device "${home_persistence}" )
1113
1114                 if [ -b "${homecow}" ]
1115                 then
1116                         mount -t $(get_fstype "${homecow}") -o rw,noatime "${homecow}" "${rootmnt}/home"
1117                         export HOMEMOUNTED=1 # used to proper calculate free space in do_snap_copy()
1118                 else
1119                         [ "${quiet}" != "y" ] && log_warning_msg "Unable to find the persistent home medium"
1120                 fi
1121
1122                 # Look for other snapshots to copy in
1123                 try_snap "${root_snapshot_label}" "${rootmnt}" "ROOT"
1124                 try_snap "${home_snapshot_label}" "${rootmnt}/home" "HOME"
1125         fi
1126
1127         if [ -n "${SHOWMOUNTS}" ]
1128         then
1129                 for d in ${rofslist}
1130                 do
1131                         mkdir -p "${rootmnt}/live/${d##*/}"
1132
1133                         case d in
1134                                 *.dir)
1135                                         # do nothing # mount -o bind "${d}" "${rootmnt}/live/${d##*/}"
1136                                         ;;
1137
1138                                 *)
1139                                         mount --move "${d}" "${rootmnt}/live/${d##*/}"
1140                                         ;;
1141                         esac
1142                 done
1143         fi
1144
1145         # shows cow fs on /cow for use by live-snapshot
1146         mkdir -p "${rootmnt}/live/cow"
1147         mount -o move /cow "${rootmnt}/live/cow"
1148 }
1149
1150 check_dev ()
1151 {
1152         sysdev="${1}"
1153         devname="${2}"
1154         skip_uuid_check="${3}"
1155
1156         if [ -z "${devname}" ]
1157         then
1158                 devname=$(sys2dev "${sysdev}")
1159         fi
1160
1161         if [ -n "${LIVE_MEDIA_OFFSET}" ]
1162         then
1163                 loopdevname=$(setup_loop "${devname}" "loop" "/sys/block/loop*" "${LIVE_MEDIA_OFFSET}" '')
1164                 devname="${loopdevname}"
1165         fi
1166
1167         fstype=$(get_fstype "${devname}")
1168
1169         if is_supported_fs ${fstype}
1170         then
1171                 mount -t ${fstype} -o ro,noatime "${devname}" ${mountpoint} || continue
1172
1173                 if is_live_path ${mountpoint} && \
1174                         ([ "${skip_uuid_check}" ] || matches_uuid ${mountpoint})
1175                 then
1176                         echo ${mountpoint}
1177                         return 0
1178                 else
1179                         umount ${mountpoint}
1180                 fi
1181         fi
1182
1183         if [ -n "${LIVE_MEDIA_OFFSET}" ]
1184         then
1185                 losetup -d "${loopdevname}"
1186         fi
1187
1188         return 1
1189 }
1190
1191 find_livefs ()
1192 {
1193         timeout="${1}"
1194
1195         # first look at the one specified in the command line
1196         if [ ! -z "${LIVE_MEDIA}" ]
1197         then
1198                 if check_dev "null" "${LIVE_MEDIA}" "skip_uuid_check"
1199                 then
1200                         return 0
1201                 fi
1202         fi
1203
1204         # don't start autodetection before timeout has expired
1205         if [ -n "${LIVE_MEDIA_TIMEOUT}" ]
1206         then
1207                 if [ "${timeout}" -lt "${LIVE_MEDIA_TIMEOUT}" ]
1208                 then
1209                         return 1
1210                 fi
1211         fi
1212
1213         # or do the scan of block devices
1214         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram | grep -v 'dm-')
1215         do
1216                 devname=$(sys2dev "${sysblock}")
1217                 fstype=$(get_fstype "${devname}")
1218
1219                 if /lib/udev/cdrom_id ${devname} > /dev/null
1220                 then
1221                         if check_dev "null" "${devname}"
1222                         then
1223                                 return 0
1224                         fi
1225                 elif is_nice_device "${sysblock}"
1226                 then
1227                         for dev in $(subdevices "${sysblock}")
1228                         do
1229                                 if check_dev "${dev}"
1230                                 then
1231                                         return 0
1232                                 fi
1233                         done
1234                 elif [ "${fstype}" = "squashfs" -o \
1235                         "${fstype}" = "ext2" -o \
1236                         "${fstype}" = "ext3" -o \
1237                         "${fstype}" = "jffs2" ]
1238                 then
1239                         # This is an ugly hack situation, the block device has
1240                         # an image directly on it.  It's hopefully
1241                         # live-initramfs, so take it and run with it.
1242                         ln -s "${devname}" "${devname}.${fstype}"
1243                         echo "${devname}.${fstype}"
1244                         return 0
1245                 fi
1246         done
1247
1248         return 1
1249 }
1250
1251 set_usplash_timeout ()
1252 {
1253         if [ -x /sbin/usplash_write ]
1254         then
1255                 /sbin/usplash_write "TIMEOUT 120"
1256         fi
1257 }
1258
1259 mountroot ()
1260 {
1261     
1262         if [ -x /scripts/local-top/cryptroot ]; then
1263             /scripts/local-top/cryptroot
1264         fi
1265
1266         exec 6>&1
1267         exec 7>&2
1268         exec > live.log
1269         exec 2>&1
1270         tail -f live.log >&7 &
1271         tailpid="${!}"
1272
1273         Arguments
1274
1275         set_usplash_timeout
1276         [ "${quiet}" != "y" ] && log_begin_msg "Running /scripts/live-premount"
1277         run_scripts /scripts/live-premount
1278         [ "${quiet}" != "y" ] && log_end_msg
1279
1280         # Needed here too because some things (*cough* udev *cough*)
1281         # changes the timeout
1282
1283         set_usplash_timeout
1284
1285         if [ ! -z "${NETBOOT}" ] || [ ! -z "${FETCH}" ]
1286         then
1287                 if do_netmount
1288                 then
1289                         livefs_root="${mountpoint}"
1290                 else
1291                         panic "Unable to find a live file system on the network"
1292                 fi
1293         else
1294                 if [ -n "${PLAIN_ROOT}" ] && [ -n "${ROOT}" ]
1295                 then
1296                         # Do a local boot from hd
1297                         livefs_root=${ROOT}
1298                 else
1299                         # Scan local devices for the image
1300                         for i in 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
1301                         do
1302                                 livefs_root=$(find_livefs ${i})
1303
1304                                 if [ -n "${livefs_root}" ]
1305                                 then
1306                                         break
1307                                 fi
1308
1309                                 sleep 1
1310                         done
1311                 fi
1312         fi
1313
1314         if [ -z "${livefs_root}" ]
1315         then
1316                 panic "Unable to find a medium containing a live file system"
1317         fi
1318
1319         if [ "${TORAM}" ]
1320         then
1321                 live_dest="ram"
1322         elif [ "${TODISK}" ]
1323         then
1324                 live_dest="${TODISK}"
1325         fi
1326
1327         if [ "${live_dest}" ]
1328         then
1329                 log_begin_msg "Copying live media to ${live_dest}"
1330                 copy_live_to "${livefs_root}" "${live_dest}"
1331                 log_end_msg
1332         fi
1333
1334         if [ -n "${MODULETORAMFILE}" ]
1335         then
1336                 setup_unionfs "${livefs_root}" "${rootmnt}"
1337         else
1338                 mac="$(get_mac)"
1339                 mac="$(echo ${mac} | sed 's/-//g')"
1340                 mount_images_in_directory "${livefs_root}" "${rootmnt}" "${mac}"
1341         fi
1342
1343         log_end_msg
1344
1345         maybe_break live-bottom
1346         [ "${quiet}" != "y" ] && log_begin_msg "Running /scripts/live-bottom"
1347
1348         run_scripts /scripts/live-bottom
1349         [ "${quiet}" != "y" ] && log_end_msg
1350
1351         exec 1>&6 6>&-
1352         exec 2>&7 7>&-
1353         kill ${tailpid}
1354         cp live.log "${rootmnt}/var/log/"
1355 }