Merging casper 1.118.
[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 jffs2
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|*.jffs2)
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         mac="${3}"
511
512
513         if match_files_in_dir "${directory}/${LIVE_MEDIA_PATH}/*.squashfs" ||
514                 match_files_in_dir "${directory}/${LIVE_MEDIA_PATH}/*.ext2" ||
515                 match_files_in_dir "${directory}/${LIVE_MEDIA_PATH}/*.ext3" ||
516                 match_files_in_dir "${directory}/${LIVE_MEDIA_PATH}/*.jffs2" ||
517                 match_files_in_dir "${directory}/${LIVE_MEDIA_PATH}/*.dir"
518         then
519                 [ -n "${mac}" ] && adddirectory="${directory}/${LIVE_MEDIA_PATH}/${mac}"
520                 setup_unionfs "${directory}/${LIVE_MEDIA_PATH}" "${rootmnt}" "${adddirectory}"
521         else
522                 :
523         fi
524 }
525
526 is_nice_device ()
527 {
528         sysfs_path="${1#/sys}"
529
530         if /lib/udev/path_id "${sysfs_path}" | grep -E -q "ID_PATH=(usb|pci-[^-]*-[ide|scsi|usb])"
531         then
532                 return 0
533         fi
534
535         return 1
536 }
537
538 is_supported_fs ()
539 {
540         # FIXME: do something better like the scan of supported filesystems
541         fstype="${1}"
542
543         case ${fstype} in
544                 vfat|iso9660|udf|ext2|ext3|ntfs|jffs2)
545                         return 0
546                         ;;
547         esac
548
549         return 1
550 }
551
552 copy_live_to ()
553 {
554         copyfrom="${1}"
555         copytodev="${2}"
556         copyto="${copyfrom}_swap"
557
558         if [ -z "${MODULETORAM}" ]
559         then
560                 size=$(fs_size "" ${copyfrom} "used")
561         else
562                 MODULETORAMFILE="${copyfrom}/${LIVE_MEDIA_PATH}/${MODULETORAM}"
563
564                 if [ -f "${MODULETORAMFILE}" ]
565                 then
566                         size=$( expr $(ls -la ${MODULETORAMFILE} | awk '{print $5}') / 1024 + 5000 )
567                 else
568                         log_warning_msg "Error: toram-module ${MODULETORAM} (${MODULETORAMFILE}) could not be read."
569                         return 1
570                 fi
571         fi
572
573         if [ "${copytodev}" = "ram" ]
574         then
575                 # copying to ram:
576                 freespace=$( expr $(awk '/MemFree/{print $2}' /proc/meminfo) + $( cat /proc/meminfo | grep Cached | head -n 1 | awk '/Cached/{print $2}' - ) )
577                 mount_options="-o size=${size}k"
578                 free_string="memory"
579                 fstype="tmpfs"
580                 dev="/dev/shm"
581         else
582                 # it should be a writable block device
583                 if [ -b "${copytodev}" ]
584                 then
585                         dev="${copytodev}"
586                         free_string="space"
587                         fstype=$(get_fstype "${dev}")
588                         freespace=$(fs_size "${dev}")
589                 else
590                         [ "$quiet" != "y" ] && log_warning_msg "${copytodev} is not a block device."
591                         return 1
592                 fi
593         fi
594
595         if [ "${freespace}" -lt "${size}" ]
596         then
597                 [ "${quiet}" != "y" ] && log_warning_msg "Not enough free ${free_string} (${freespace}k free, ${size}k needed) to copy live media in ${copytodev}."
598                 return 1
599         fi
600
601         # begin copying (or uncompressing)
602         mkdir "${copyto}"
603         echo "mount -t ${fstype} ${mount_options} ${dev} ${copyto}"
604         mount -t "${fstype}" ${mount_options} "${dev}" "${copyto}"
605
606         if [ "${extension}" = "tgz" ]
607         then
608                 cd "${copyto}"
609                 tar zxf "${copyfrom}/${LIVE_MEDIA_PATH}/$(basename ${FETCH})"
610                 rm -f "${copyfrom}/${LIVE_MEDIA_PATH}/$(basename ${FETCH})"
611                 mount -r --move "${copyto}" "${rootmnt}"
612                 cd "${OLDPWD}"
613         else
614                 if [ -n "${MODULETORAMFILE}" ]
615                 then
616                         cp ${MODULETORAMFILE} ${copyto} # copy only the filesystem module
617                 else
618                         cp -a ${copyfrom}/* ${copyto}   # "cp -a" from busybox also copies hidden files
619                 fi
620
621                 livefs_root
622                 umount ${copyfrom}
623                 mount -r --move ${copyto} ${copyfrom}
624         fi
625
626         rmdir ${copyto}
627         return 0
628 }
629
630 do_netmount ()
631 {
632         rc=1
633
634         modprobe -q af_packet # For DHCP
635
636         udevtrigger
637         udevsettle
638
639         ipconfig ${DEVICE} | tee /netboot.config
640
641         # source relevant ipconfig output
642         OLDHOSTNAME=${HOSTNAME}
643         . /tmp/net-${DEVICE}.conf
644         [ -z ${HOSTNAME} ] && HOSTNAME=${OLDHOSTNAME}
645         export HOSTNAME
646
647         if [ "${NFSROOT}" = "auto" ]
648         then
649                 NFSROOT=${ROOTSERVER}:${ROOTPATH}
650         fi
651
652         if [ -n "${FETCH}" ] && do_httpmount
653         then
654                 rc=0
655                 return ${rc}
656         fi
657
658         if [ "${NFSROOT#*:}" = "${NFSROOT}" ] && [ "$NETBOOT" != "cifs" ]
659         then
660                 NFSROOT=${ROOTSERVER}:${NFSROOT}
661         fi
662
663         [ "${quiet}" != "y" ] && log_begin_msg "Trying netboot from ${NFSROOT}"
664
665         if [ "${NETBOOT}" != "nfs" ] && do_cifsmount
666         then
667                 rc=0
668         elif do_nfsmount
669         then
670                 NETBOOT="nfs"
671                 export NETBOOT
672                 rc=0
673         fi
674
675         [ "${quiet}" != "y" ] && log_end_msg
676         return ${rc}
677 }
678
679 do_httpmount ()
680 {
681         rc=1
682         extension=$(echo "${FETCH}" | sed 's/\(.*\)\.\(.*\)/\2/')
683
684         case "${extension}" in
685                 squashfs|tgz|tar)
686                         [ "${quiet}" != "y" ] && log_begin_msg "Trying wget ${FETCH} -O ${mountpoint}/$(basename ${FETCH})"
687                         mkdir -p "${mountpoint}/${LIVE_MEDIA_PATH}"
688                         wget "${FETCH}" -O "${mountpoint}/${LIVE_MEDIA_PATH}/$(basename ${FETCH})"
689                         [ ${?} -eq 0 ] && rc=0
690                         [ "${extension}" = "tgz" ] && live_dest="ram"
691                         ;;
692
693                 *)
694                         [ "${quiet}" != "y" ] && log_begin_msg "Unrecognized archive extension for ${FETCH}"
695         esac
696
697         return ${rc}
698 }
699
700 do_nfsmount ()
701 {
702         rc=1
703
704         modprobe -q nfs
705
706         if [ -z "${NFSOPTS}" ]
707         then
708                 NFSOPTS=""
709         fi
710
711         [ "${quiet}" != "y" ] && log_begin_msg "Trying nfsmount -o nolock -o ro ${NFSOPTS} ${NFSROOT} ${mountpoint}"
712
713         # FIXME: This for loop is an ugly HACK round an nfs bug
714         for i in 0 1 2 3 4 5 6 7 8 9 a b c d e f 10 11 12 13
715         do
716                 nfsmount -o nolock -o ro ${NFSOPTS} "${NFSROOT}" "${mountpoint}" && rc=0 && break
717                 sleep 1
718         done
719
720         return ${rc}
721 }
722
723 do_cifsmount ()
724 {
725         rc=1
726
727         if [ -x "/sbin/mount.cifs" ]
728         then
729                 if [ -z "${NFSOPTS}" ]
730                 then
731                         CIFSOPTS="-ouser=root,password="
732                 else
733                         CIFSOPTS="${NFSOPTS}"
734                 fi
735
736                 [ "${quiet}" != "y" ] && log_begin_msg "Trying mount.cifs ${NFSROOT} ${mountpoint} ${CIFSOPTS}"
737                 modprobe -q cifs
738
739                 if mount.cifs "${NFSROOT}" "${mountpoint}" "${CIFSOPTS}"
740                 then
741                         rc=0
742                 fi
743         fi
744
745         return ${rc}
746 }
747
748 do_snap_copy ()
749 {
750         fromdev="${1}"
751         todir="${2}"
752         snap_type="${3}"
753         size=$(fs_size "${fromdev}" "" "used")
754
755         if [ -b "${fromdev}" ]
756         then
757                 # look for free mem
758                 if [ -n "${HOMEMOUNTED}" -a "${snap_type}" = "HOME" ]
759                 then
760                         todev=$(cat /proc/mounts | grep -s " $(base_path ${todir}) " | awk '{print $1}' )
761                         freespace=$(df -k  | grep -s ${todev} | awk '{print $4}')
762                 else
763                         freespace=$( expr $(awk '/MemFree/{print $2}' /proc/meminfo) + $( cat /proc/meminfo | grep Cached | head -n 1 | awk '/Cached/{print $2}' - ))
764                 fi
765
766                 tomount="/mnt/tmpsnap"
767
768                 if [ ! -d "${tomount}" ]
769                 then
770                         mkdir -p "${tomount}"
771                 fi
772
773                 fstype=$(get_fstype "${fromdev}")
774
775                 if [ -n "${fstype}" ]
776                 then
777                         # Copying stuff...
778                         mount -t "${fstype}" -o ro,noatime "${fromdev}" "${tomount}"
779                         cp -a "${tomount}"/* ${todir}
780                         umount "${tomount}"
781                 else
782                         log_warning_msg "Unrecognized fstype: ${fstype} on ${fromdev}:${snap_type}"
783                 fi
784
785                 rmdir "${tomount}"
786
787                 if echo ${fromdev} | grep -qs loop
788                 then
789                         losetup -d "${fromdev}"
790                 fi
791
792                 return 0
793         else
794                 return 1
795
796                 [ "${quiet}" != "y" ] && log_warning_msg "Unable to find the snapshot ${snap_type} medium"
797         fi
798 }
799
800 try_snap ()
801 {
802         # Look for ${snap_label}.* in block devices and copy the contents to ${snap_mount}
803         # and remember the device and filename for resync on exit in live-initramfs.init
804
805         snap_label="${1}"
806         snap_mount="${2}"
807         snap_type="${3}"
808         snapdata=$(find_files "${snap_label}.squashfs ${snap_label}.cpio.gz ${snap_label}.ext2 ${snap_label}.ext3 ${snap_label}.jffs2")
809
810         if [ ! -z "${snapdata}" ]
811         then
812                 snapdev="$(echo ${snapdata} | cut -f1 -d ' ')"
813                 snapback="$(echo ${snapdata} | cut -f2 -d ' ')"
814                 snapfile="$(echo ${snapdata} | cut -f3 -d ' ')"
815
816                 if echo "${snapfile}" | grep -qs '\(squashfs\|ext2\|ext3\|jffs2\)'
817                 then
818                         # squashfs, jffs2 or ext2/ext3 snapshot
819                         dev=$(get_backing_device "${snapback}/${snapfile}")
820
821                         if ! do_snap_copy "${dev}" "${snap_mount}" "${snap_type}"
822                         then
823                                 log_warning_msg "Impossible to include the ${snapfile} Snapshot"
824                                 return 1
825                         fi
826                 else
827                         # cpio.gz snapshot
828                         if ! (cd "${snap_mount}" && zcat "${snapback}/${snapfile}" | cpio -i -u -d 2>/dev/null)
829                         then
830                                 log_warning_msg "Impossible to include the ${snapfile} Snapshot"
831                                 return 1
832                         fi
833                 fi
834
835                 umount "${snapback}"
836         else
837                 dev=$(find_cow_device "${snap_label}")
838
839                 if [ -b ${dev} ]
840                 then
841                         if echo "${dev}" | grep -qs loop
842                         then
843                                 # strange things happens, user confused?
844                                 snaploop=$( losetup ${dev} | awk '{print $3}' | tr -d '()' )
845                                 snapfile=$(basename ${snaploop})
846                                 snapdev=$(cat /proc/mounts | awk '{print $2,$1}' | grep -es "^$( dirname ${snaploop} )" | cut -f2 -d ' ')
847                         else
848                                 snapdev="${dev}"
849                         fi
850
851                         if ! do_snap_copy "${dev}" "${snap_mount}" "${snap_type}"
852                         then
853                                 log_warning_msg "Impossible to include the ${snap_label} Snapshot"
854                                 return 1
855                         else
856                                 if [ -n "${snapfile}" ]
857                                 then
858                                         # it was a loop device, user confused
859                                         umount ${snapdev}
860                                 fi
861                         fi
862                 else
863                         log_warning_msg "Impossible to include the ${snap_label} Snapshot"
864                         return 1
865                 fi
866         fi
867
868         echo "export ${snap_type}SNAP="${snap_mount}":${snapdev}:${snapfile}" >> /etc/live.conf # for resync on reboot/halt
869         return 0
870 }
871
872 setup_unionfs ()
873 {
874         image_directory="${1}"
875         rootmnt="${2}"
876         addimage_directory="${3}"
877         modprobe -q -b ${UNIONTYPE}
878
879         # run-init can't deal with images in a subdir, but we're going to
880         # move all of these away before it runs anyway.  No, we're not,
881         # put them in / since move-mounting them into / breaks mono and
882         # some other apps.
883
884         croot="/"
885
886         # Let's just mount the read-only file systems first
887         rofsstring=""
888         rofslist=""
889         minor_kernel_version=$(uname -r|cut -c 5-|sed 's/[^0-9].*//')
890
891         if [ "${NETBOOT}" = "nfs" ] && [ "${minor_kernel_version}" -lt 22 ]
892         then
893                 # go aroung a bug in nfs-unionfs locking for unionfs <= 1.4
894                 roopt="nfsro"
895         elif [ "${UNIONTYPE}" = "aufs" ]
896         then
897                 roopt="rr"
898         else
899                 roopt="ro"
900         fi
901
902         # Read image names from ${MODULE}.module if it exists
903         if [ -e "${image_directory}/filesystem.${MODULE}.module" ]
904         then
905                 for IMAGE in $(cat ${image_directory}/filesystem.${MODULE}.module)
906                 do
907                         image_string="${image_string} ${image_directory}/${IMAGE}"
908                 done
909         elif [ -e "${image_directory}/${MODULE}.module" ]
910         then
911                 for IMAGE in $(cat ${image_directory}/${MODULE}.module)
912                 do
913                         image_string="${image_string} ${image_directory}/${IMAGE}"
914                 done
915         else
916                 # ${MODULE}.module does not exist, create a list of images
917                 for FILESYSTEM in squashfs ext2 ext3 xfs jffs2 dir
918                 do
919                         for IMAGE in "${image_directory}"/*."${FILESYSTEM}"
920                         do
921                                 if [ -e "${IMAGE}" ]
922                                 then
923                                         image_string="${image_string} ${IMAGE}"
924                                 fi
925                         done
926                 done
927
928                 if [ -n "${addimage_directory}" ] && [ -d "${addimage_directory}" ]
929                 then
930                         for FILESYSTEM in squashfs ext2 ext3 xfs jffs2 dir
931                         do
932                                 for IMAGE in "${addimage_directory}"/*."${FILESYSTEM}"
933                                 do
934                                         if [ -e "${IMAGE}" ]
935                                         then
936                                                 image_string="${image_string} ${IMAGE}"
937                                         fi
938                                 done
939                         done
940                 fi
941
942                 # Now sort the list
943                 image_string="$(echo ${image_string} | sed -e 's/ /\n/g' | sort )"
944         fi
945
946         [ -n "${MODULETORAMFILE}" ] && image_string="${image_directory}/$(basename ${MODULETORAMFILE})"
947
948         mkdir -p "${croot}"
949
950         for image in ${image_string}
951         do
952                 imagename=$(basename "${image}")
953
954                 if [ -d "${image}" ]
955                 then
956                         # it is a plain directory: do nothing
957                         rofsstring="${image}=${roopt}:${rofsstring}"
958                         rofslist="${image} ${rofslist}"
959                 elif [ -f "${image}" ]
960                 then
961                         backdev=$(get_backing_device "${image}")
962                         fstype=$(get_fstype "${backdev}")
963
964                         if [ "${fstype}" = "unknown" ]
965                         then
966                                 panic "Unknown file system type on ${backdev} (${image})"
967                         fi
968
969                         mkdir -p "${croot}/${imagename}"
970                         echo "debug: Can not mount backdev ${backdev} (image = ${image}) on croot/imagename ${croot}/${imagename}"
971                         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}"
972                 fi
973         done
974
975         rofsstring=${rofsstring%:}
976
977         mkdir -p /cow
978
979         # Looking for "${root_persistence}" device or file
980         if [ -n "${PERSISTENT}" ]
981         then
982                 cowprobe=$(find_cow_device "${root_persistence}")
983
984                 if [ -b "${cowprobe}" ]
985                 then
986                         cowdevice=${cowprobe}
987                         cow_fstype=$(get_fstype "${cowprobe}")
988                 else
989                         [ "${quiet}" != "y" ] && log_warning_msg "Unable to find the persistent medium"
990                         cowdevice="tmpfs"
991                         cow_fstype="tmpfs"
992                 fi
993         elif [ -n "${NFS_COW}" ]
994         then
995                 # check if there are any nfs options
996                 if echo ${NFS_COW}|grep -q ','
997                 then
998                         nfs_cow_opts="-o nolock,$(echo ${NFS_COW}|cut -d, -f2-)"
999                         nfs_cow=$(echo ${NFS_COW}|cut -d, -f1)
1000                 else
1001                         nfs_cow_opts="-o nolock"
1002                         nfs_cow=${NFS_COW}
1003                 fi
1004                 mac="$(get_mac)"
1005                 if [ -n "${mac}" ]
1006                 then
1007                         cowdevice=$(echo ${nfs_cow}|sed "s/client_mac_address/${mac}/")
1008                         cow_fstype="nfs"
1009                 else
1010                         panic "unable to determine mac address"
1011                 fi
1012         else
1013                 cowdevice="tmpfs"
1014                 cow_fstype="tmpfs"
1015         fi
1016
1017         if [ "${cow_fstype}" = "nfs" ]
1018         then
1019                 [ "${quiet}" != "y" ] && log_begin_msg \
1020                         "Trying nfsmount ${nfs_cow_opts} ${cowdevice} /cow"
1021                 nfsmount ${nfs_cow_opts} ${cowdevice} /cow || \
1022                         panic "Can not mount ${cowdevice} on /cow"
1023         else
1024                 mount ${cowdevice} -t ${cow_fstype} -o rw,noatime /cow || \
1025                         panic "Can not mount ${cowdevice} on /cow"
1026         fi
1027
1028         rofscount=$(echo ${rofslist} |wc -w)
1029
1030         if [ -n "${EXPOSED_ROOT}" ]
1031         then
1032                 if [ ${rofscount} -ne 1 ]
1033                 then
1034                         panic "only one RO file system supported with exposedroot: ${rofslist}"
1035                 fi
1036                 exposedrootfs=${rofslist%% }
1037
1038                 mount --bind ${exposedrootfs} ${rootmnt} || \
1039                         panic "bind mount of ${exposedrootfs} failed"
1040
1041                 cow_dirs='/var/tmp /var/lock /var/run /var/log /var/spool
1042                         /home /var/lib/live'
1043
1044                 for dir in ${cow_dirs}; do
1045                         mkdir -p /cow${dir}
1046                         mount -t ${UNIONTYPE} \
1047                                 -o rw,noatime,dirs=/cow${dir}=rw:${exposedrootfs}${dir}=ro \
1048                                 ${UNIONTYPE} "${rootmnt}${dir}" || \
1049                                 panic "mount ${UNIONTYPE} on ${rootmnt}${dir} failed with option \
1050                                         rw,noatime,dirs=/cow${dir}=rw:${exposedrootfs}${dir}=ro"
1051                 done
1052         else
1053                 mount -t ${UNIONTYPE} -o noatime,dirs=/cow=rw:${rofsstring} \
1054                         ${UNIONTYPE} "${rootmnt}" || panic "mount ${UNIONTYPE} on \
1055                         ${rootmnt} failed with option noatime,dirs=/cow=rw:${rofsstring}"
1056         fi
1057
1058         # tmpfs file systems
1059         mkdir -p "${rootmnt}/live"
1060         mount -t tmpfs tmpfs ${rootmnt}/live
1061
1062         # Adding other custom mounts
1063         if [ -n "${PERSISTENT}" ]
1064         then
1065                 # directly mount /home
1066                 # FIXME: add a custom mounts configurable system
1067                 homecow=$(find_cow_device "${home_persistence}" )
1068
1069                 if [ -b "${homecow}" ]
1070                 then
1071                         mount -t $(get_fstype "${homecow}") -o rw,noatime "${homecow}" "${rootmnt}/home"
1072                         export HOMEMOUNTED=1 # used to proper calculate free space in do_snap_copy()
1073                 else
1074                         [ "${quiet}" != "y" ] && log_warning_msg "Unable to find the persistent home medium"
1075                 fi
1076
1077                 # Look for other snapshots to copy in
1078                 try_snap "${root_snapshot_label}" "${rootmnt}" "ROOT"
1079                 try_snap "${home_snapshot_label}" "${rootmnt}/home" "HOME"
1080         fi
1081
1082         if [ -n "${SHOWMOUNTS}" ]
1083         then
1084                 for d in ${rofslist}
1085                 do
1086                         mkdir -p "${rootmnt}/live/${d##*/}"
1087
1088                         case d in
1089                                 *.dir)
1090                                         # do nothing # mount -o bind "${d}" "${rootmnt}/live/${d##*/}"
1091                                         ;;
1092
1093                                 *)
1094                                         mount --move "${d}" "${rootmnt}/live/${d##*/}"
1095                                         ;;
1096                         esac
1097                 done
1098         fi
1099
1100         # shows cow fs on /cow for use by live-snapshot
1101         mkdir -p "${rootmnt}/live/cow"
1102         mount -o move /cow "${rootmnt}/live/cow"
1103 }
1104
1105 check_dev ()
1106 {
1107         sysdev="${1}"
1108         devname="${2}"
1109         skip_uuid_check="${3}"
1110
1111         if [ -z "${devname}" ]
1112         then
1113                 devname=$(sys2dev "${sysdev}")
1114         fi
1115
1116         if [ -n "${LIVE_MEDIA_OFFSET}" ]
1117         then
1118                 loopdevname=$(setup_loop "${devname}" "loop" "/sys/block/loop*" "${LIVE_MEDIA_OFFSET}" '')
1119                 devname="${loopdevname}"
1120         fi
1121
1122         fstype=$(get_fstype "${devname}")
1123
1124         if is_supported_fs ${fstype}
1125         then
1126                 mount -t ${fstype} -o ro,noatime "${devname}" ${mountpoint} || continue
1127
1128                 if is_live_path ${mountpoint} && \
1129                         ([ "${skip_uuid_check}" ] || matches_uuid ${mountpoint})
1130                 then
1131                         echo ${mountpoint}
1132                         return 0
1133                 else
1134                         umount ${mountpoint}
1135                 fi
1136         fi
1137
1138         if [ -n "${LIVE_MEDIA_OFFSET}" ]
1139         then
1140                 losetup -d "${loopdevname}"
1141         fi
1142
1143         return 1
1144 }
1145
1146 find_livefs ()
1147 {
1148         timeout="${1}"
1149
1150         # first look at the one specified in the command line
1151         if [ ! -z "${LIVE_MEDIA}" ]
1152         then
1153                 if check_dev "null" "${LIVE_MEDIA}" "skip_uuid_check"
1154                 then
1155                         return 0
1156                 fi
1157         fi
1158
1159         # don't start autodetection before timeout has expired
1160         if [ -n "${LIVE_MEDIA_TIMEOUT}" ]
1161         then
1162                 if [ "${timeout}" -lt "${LIVE_MEDIA_TIMEOUT}" ]
1163                 then
1164                         return 1
1165                 fi
1166         fi
1167
1168         # or do the scan of block devices
1169         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram)
1170         do
1171                 devname=$(sys2dev "${sysblock}")
1172                 fstype=$(get_fstype "${devname}")
1173
1174                 if /lib/udev/cdrom_id ${devname} > /dev/null
1175                 then
1176                         if check_dev "null" "${devname}"
1177                         then
1178                                 return 0
1179                         fi
1180                 elif is_nice_device "${sysblock}"
1181                 then
1182                         for dev in $(subdevices "${sysblock}")
1183                         do
1184                                 if check_dev "${dev}"
1185                                 then
1186                                         return 0
1187                                 fi
1188                         done
1189                 elif [ "${fstype}" = "squashfs" -o \
1190                         "${fstype}" = "ext2" -o \
1191                         "${fstype}" = "ext3" -o \
1192                         "${fstype}" = "jffs2" ]
1193                 then
1194                         # This is an ugly hack situation, the block device has
1195                         # an image directly on it.  It's hopefully
1196                         # live-initramfs, so take it and run with it.
1197                         ln -s "${devname}" "${devname}.${fstype}"
1198                         echo "${devname}.${fstype}"
1199                         return 0
1200                 fi
1201         done
1202
1203         return 1
1204 }
1205
1206 set_usplash_timeout ()
1207 {
1208         if [ -x /sbin/usplash_write ]
1209         then
1210                 /sbin/usplash_write "TIMEOUT 120"
1211         fi
1212 }
1213
1214 mountroot ()
1215 {
1216         exec 6>&1
1217         exec 7>&2
1218         exec > live.log
1219         exec 2>&1
1220
1221         Arguments
1222
1223         set_usplash_timeout
1224         [ "${quiet}" != "y" ] && log_begin_msg "Running /scripts/live-premount"
1225         run_scripts /scripts/live-premount
1226         [ "${quiet}" != "y" ] && log_end_msg
1227
1228         # Needed here too because some things (*cough* udev *cough*)
1229         # changes the timeout
1230
1231         set_usplash_timeout
1232
1233         if [ ! -z "${NETBOOT}" ] || [ ! -z "${FETCH}" ]
1234         then
1235                 if do_netmount
1236                 then
1237                         livefs_root="${mountpoint}"
1238                 else
1239                         panic "Unable to find a live file system on the network"
1240                 fi
1241         else
1242                 # Scan local devices for the image
1243                 for i in 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
1244                 do
1245                         livefs_root=$(find_livefs ${i})
1246
1247                         if [ -n "${livefs_root}" ]
1248                         then
1249                                 break
1250                         fi
1251
1252                         sleep 1
1253                 done
1254         fi
1255
1256         if [ -z "${livefs_root}" ]
1257         then
1258                 panic "Unable to find a medium containing a live file system"
1259         fi
1260
1261         if [ "${TORAM}" ]
1262         then
1263                 live_dest="ram"
1264         elif [ "${TODISK}" ]
1265         then
1266                 live_dest="${TODISK}"
1267         fi
1268
1269         if [ "${live_dest}" ]
1270         then
1271                 log_begin_msg "Copying live media to ${live_dest}"
1272                 copy_live_to "${livefs_root}" "${live_dest}"
1273                 log_end_msg
1274         fi
1275
1276         if [ -n "${MODULETORAMFILE}" ]
1277         then
1278                 setup_unionfs "${livefs_root}" "${rootmnt}"
1279         else
1280                 mac="$(get_mac)"
1281                 mac="$(echo ${mac} | sed 's/-//g')"
1282                 mount_images_in_directory "${livefs_root}" "${rootmnt}" "${mac}"
1283         fi
1284
1285         log_end_msg
1286
1287         maybe_break live-bottom
1288         [ "${quiet}" != "y" ] && log_begin_msg "Running /scripts/live-bottom"
1289
1290         run_scripts /scripts/live-bottom
1291         [ "${quiet}" != "y" ] && log_end_msg
1292
1293         exec 1>&6 6>&-
1294         exec 2>&7 7>&-
1295         cp live.log "${rootmnt}/var/log/"
1296 }