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