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