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