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