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