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