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