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