Removed mount -n option, since mtab is there.
[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                 snapdev="$(echo ${snapdata} | cut -f1 -d ' ')"
893                 snapback="$(echo ${snapdata} | cut -f2 -d ' ')"
894                 snapfile="$(echo ${snapdata} | cut -f3 -d ' ')"
895
896                 if echo "${snapfile}" | grep -qs '\(squashfs\|ext2\|ext3\|jffs2\)'
897                 then
898                         # squashfs, jffs2 or ext2/ext3 snapshot
899                         dev=$(get_backing_device "${snapback}/${snapfile}")
900
901                         if ! do_snap_copy "${dev}" "${snap_mount}" "${snap_type}"
902                         then
903                                 log_warning_msg "Impossible to include the ${snapfile} Snapshot"
904                                 return 1
905                         fi
906                 else
907                         # cpio.gz snapshot
908                         if ! (cd "${snap_mount}" && zcat "${snapback}/${snapfile}" | cpio --extract --preserve-modification-time --no-absolute-filenames --sparse --unconditional --make-directories > /dev/null 2>&1)
909                         then
910                                 log_warning_msg "Impossible to include the ${snapfile} Snapshot"
911                                 return 1
912                         fi
913                 fi
914
915                 umount "${snapback}"
916         else
917                 dev=$(find_cow_device "${snap_label}")
918
919                 if [ -b ${dev} ]
920                 then
921                         if echo "${dev}" | grep -qs loop
922                         then
923                                 # strange things happens, user confused?
924                                 snaploop=$( losetup ${dev} | awk '{print $3}' | tr -d '()' )
925                                 snapfile=$(basename ${snaploop})
926                                 snapdev=$(cat /proc/mounts | awk '{print $2,$1}' | grep -es "^$( dirname ${snaploop} )" | cut -f2 -d ' ')
927                         else
928                                 snapdev="${dev}"
929                         fi
930
931                         if ! do_snap_copy "${dev}" "${snap_mount}" "${snap_type}"
932                         then
933                                 log_warning_msg "Impossible to include the ${snap_label} Snapshot"
934                                 return 1
935                         else
936                                 if [ -n "${snapfile}" ]
937                                 then
938                                         # it was a loop device, user confused
939                                         umount ${snapdev}
940                                 fi
941                         fi
942                 else
943                         log_warning_msg "Impossible to include the ${snap_label} Snapshot"
944                         return 1
945                 fi
946         fi
947
948         echo "export ${snap_type}SNAP="${snap_mount}":${snapdev}:${snapfile}" >> /etc/live.conf # for resync on reboot/halt
949         return 0
950 }
951
952 setup_unionfs ()
953 {
954         image_directory="${1}"
955         rootmnt="${2}"
956         addimage_directory="${3}"
957         modprobe -q -b ${UNIONTYPE}
958
959         # run-init can't deal with images in a subdir, but we're going to
960         # move all of these away before it runs anyway.  No, we're not,
961         # put them in / since move-mounting them into / breaks mono and
962         # some other apps.
963
964         croot="/"
965
966         # Let's just mount the read-only file systems first
967         rofsstring=""
968         rofslist=""
969         minor_kernel_version=$(uname -r|cut -c 5-|sed 's/[^0-9].*//')
970
971         if [ "${NETBOOT}" = "nfs" ] && [ "${minor_kernel_version}" -lt 22 ]
972         then
973                 # go aroung a bug in nfs-unionfs locking for unionfs <= 1.4
974                 roopt="nfsro"
975         elif [ "${UNIONTYPE}" = "aufs" ]
976         then
977                 roopt="rr"
978         else
979                 roopt="ro"
980         fi
981
982         # Read image names from ${MODULE}.module if it exists
983         if [ -e "${image_directory}/filesystem.${MODULE}.module" ]
984         then
985                 for IMAGE in $(cat ${image_directory}/filesystem.${MODULE}.module)
986                 do
987                         image_string="${image_string} ${image_directory}/${IMAGE}"
988                 done
989         elif [ -e "${image_directory}/${MODULE}.module" ]
990         then
991                 for IMAGE in $(cat ${image_directory}/${MODULE}.module)
992                 do
993                         image_string="${image_string} ${image_directory}/${IMAGE}"
994                 done
995         else
996                 # ${MODULE}.module does not exist, create a list of images
997                 for FILESYSTEM in squashfs ext2 ext3 xfs jffs2 dir
998                 do
999                         for IMAGE in "${image_directory}"/*."${FILESYSTEM}"
1000                         do
1001                                 if [ -e "${IMAGE}" ]
1002                                 then
1003                                         image_string="${image_string} ${IMAGE}"
1004                                 fi
1005                         done
1006                 done
1007
1008                 if [ -n "${addimage_directory}" ] && [ -d "${addimage_directory}" ]
1009                 then
1010                         for FILESYSTEM in squashfs ext2 ext3 xfs jffs2 dir
1011                         do
1012                                 for IMAGE in "${addimage_directory}"/*."${FILESYSTEM}"
1013                                 do
1014                                         if [ -e "${IMAGE}" ]
1015                                         then
1016                                                 image_string="${image_string} ${IMAGE}"
1017                                         fi
1018                                 done
1019                         done
1020                 fi
1021
1022                 # Now sort the list
1023                 image_string="$(echo ${image_string} | sed -e 's/ /\n/g' | sort )"
1024         fi
1025
1026         [ -n "${MODULETORAMFILE}" ] && image_string="${image_directory}/$(basename ${MODULETORAMFILE})"
1027
1028         mkdir -p "${croot}"
1029
1030         for image in ${image_string}
1031         do
1032                 imagename=$(basename "${image}")
1033
1034                 if [ -d "${image}" ]
1035                 then
1036                         # it is a plain directory: do nothing
1037                         rofsstring="${image}=${roopt}:${rofsstring}"
1038                         rofslist="${image} ${rofslist}"
1039                 elif [ -f "${image}" ]
1040                 then
1041                         if losetup --help 2>&1 | grep -q -- "-r\b"
1042                         then
1043                                 backdev=$(get_backing_device "${image}" "-r")
1044                         else
1045                                 backdev=$(get_backing_device "${image}")
1046                         fi
1047                         fstype=$(get_fstype "${backdev}")
1048
1049                         if [ "${fstype}" = "unknown" ]
1050                         then
1051                                 panic "Unknown file system type on ${backdev} (${image})"
1052                         fi
1053
1054                         if [ -z "${fstype}" ]
1055                         then
1056                                 fstype="${imagename##*.}"
1057                                 log_warning_msg "Unknown file system type on ${backdev} (${image}), assuming ${fstype}."
1058                         fi
1059
1060                         mkdir -p "${croot}/${imagename}"
1061                         log_begin_msg "Mounting \"${image}\" on \"${croot}${imagename}\" via \"${backdev}\""
1062                         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}"
1063                         log_end_msg
1064                 fi
1065         done
1066
1067         rofsstring=${rofsstring%:}
1068
1069         mkdir -p /cow
1070
1071         # Looking for "${root_persistence}" device or file
1072         if [ -n "${PERSISTENT}" ] && [ -z "${NOPERSISTENT}" ]
1073         then
1074                 # Load USB modules
1075                 num_block=$(ls -l /sys/block | wc -l)
1076                 for module in sd_mod uhci-hcd ehci-hcd ohci-hcd usb-storage
1077                 do
1078                         modprobe -q -b ${module}
1079                 done
1080
1081                 if [ -x /sbin/udevadm ]
1082                 then
1083                         # lenny
1084                         udevadm trigger
1085                         udevadm settle
1086                 else
1087                         # etch
1088                         udevtrigger
1089                         udevsettle
1090                 fi
1091
1092                 # For some reason, udevsettle does not block in this scenario,
1093                 # so we sleep for a little while.
1094                 #
1095                 # See https://bugs.launchpad.net/ubuntu/+source/casper/+bug/84591
1096                 for timeout in 5 4 3 2 1
1097                 do
1098                         sleep 1
1099
1100                         if [ $(ls -l /sys/block | wc -l) -gt ${num_block} ]
1101                         then
1102                                 break
1103                         fi
1104                 done
1105
1106                 cowprobe=$(find_cow_device "${root_persistence}")
1107
1108                 if [ -b "${cowprobe}" ]
1109                 then
1110                         cowdevice=${cowprobe}
1111                         cow_fstype=$(get_fstype "${cowprobe}")
1112                         cow_mountopt="rw,noatime"
1113                 else
1114                         log_warning_msg "Unable to find the persistent medium"
1115                         cowdevice="tmpfs"
1116                         cow_fstype="tmpfs"
1117                         cow_mountopt="rw,noatime,mode=755"
1118                 fi
1119         elif [ -n "${NFS_COW}" ] && [ -z "${NOPERSISTENT}" ]
1120         then
1121                 # check if there are any nfs options
1122                 if echo ${NFS_COW}|grep -q ','
1123                 then
1124                         nfs_cow_opts="-o nolock,$(echo ${NFS_COW}|cut -d, -f2-)"
1125                         nfs_cow=$(echo ${NFS_COW}|cut -d, -f1)
1126                 else
1127                         nfs_cow_opts="-o nolock"
1128                         nfs_cow=${NFS_COW}
1129                 fi
1130                 mac="$(get_mac)"
1131                 if [ -n "${mac}" ]
1132                 then
1133                         cowdevice=$(echo ${nfs_cow}|sed "s/client_mac_address/${mac}/")
1134                         cow_fstype="nfs"
1135                 else
1136                         panic "unable to determine mac address"
1137                 fi
1138         else
1139                 cowdevice="tmpfs"
1140                 cow_fstype="tmpfs"
1141                 cow_mountopt="rw,noatime,mode=755"
1142         fi
1143
1144         if [ "${cow_fstype}" = "nfs" ]
1145         then
1146                 log_begin_msg \
1147                         "Trying nfsmount ${nfs_cow_opts} ${cowdevice} /cow"
1148                 nfsmount ${nfs_cow_opts} ${cowdevice} /cow || \
1149                         panic "Can not mount ${cowdevice} (n: ${cow_fstype}) on /cow"
1150         else
1151                 mount -t ${cow_fstype} -o ${cow_mountopt} ${cowdevice} /cow || \
1152                         panic "Can not mount ${cowdevice} (o: ${cow_fstype}) on /cow"
1153         fi
1154
1155         rofscount=$(echo ${rofslist} |wc -w)
1156
1157         if [ -n "${EXPOSED_ROOT}" ]
1158         then
1159                 if [ ${rofscount} -ne 1 ]
1160                 then
1161                         panic "only one RO file system supported with exposedroot: ${rofslist}"
1162                 fi
1163                 exposedrootfs=${rofslist%% }
1164
1165                 mount --bind ${exposedrootfs} ${rootmnt} || \
1166                         panic "bind mount of ${exposedrootfs} failed"
1167
1168                 cow_dirs='/var/tmp /var/lock /var/run /var/log /var/spool
1169                         /home /var/lib/live'
1170
1171                 for dir in ${cow_dirs}; do
1172                         mkdir -p /cow${dir}
1173                         mount -t ${UNIONTYPE} \
1174                                 -o rw,noatime,dirs=/cow${dir}=rw:${exposedrootfs}${dir}=ro \
1175                                 ${UNIONTYPE} "${rootmnt}${dir}" || \
1176                                 panic "mount ${UNIONTYPE} on ${rootmnt}${dir} failed with option \
1177                                         rw,noatime,dirs=/cow${dir}=rw:${exposedrootfs}${dir}=ro"
1178                 done
1179         else
1180                 mount -t ${UNIONTYPE} -o noatime,dirs=/cow=rw:${rofsstring} \
1181                         ${UNIONTYPE} "${rootmnt}" || panic "mount ${UNIONTYPE} on \
1182                         ${rootmnt} failed with option noatime,dirs=/cow=rw:${rofsstring}"
1183         fi
1184
1185         # tmpfs file systems
1186         touch /etc/fstab
1187         mkdir -p "${rootmnt}/live"
1188         mount -t tmpfs tmpfs ${rootmnt}/live
1189
1190         # Adding other custom mounts
1191         if [ -n "${PERSISTENT}" ] && [ -z "${NOPERSISTENT}" ]
1192         then
1193                 # directly mount /home
1194                 # FIXME: add a custom mounts configurable system
1195                 homecow=$(find_cow_device "${home_persistence}" )
1196
1197                 if [ -b "${homecow}" ]
1198                 then
1199                         mount -t $(get_fstype "${homecow}") -o rw,noatime "${homecow}" "${rootmnt}/home"
1200                         export HOMEMOUNTED=1 # used to proper calculate free space in do_snap_copy()
1201                 else
1202                         log_warning_msg "Unable to find the persistent home medium"
1203                 fi
1204
1205                 # Look for other snapshots to copy in
1206                 try_snap "${root_snapshot_label}" "${rootmnt}" "ROOT"
1207                 try_snap "${home_snapshot_label}" "${rootmnt}/home" "HOME"
1208         fi
1209
1210         if [ -n "${SHOWMOUNTS}" ]
1211         then
1212                 for d in ${rofslist}
1213                 do
1214                         mkdir -p "${rootmnt}/live/${d##*/}"
1215
1216                         case d in
1217                                 *.dir)
1218                                         # do nothing # mount -o bind "${d}" "${rootmnt}/live/${d##*/}"
1219                                         ;;
1220
1221                                 *)
1222                                         mount -o move "${d}" "${rootmnt}/live/${d##*/}"
1223                                         ;;
1224                         esac
1225                 done
1226         fi
1227
1228         # shows cow fs on /cow for use by live-snapshot
1229         mkdir -p "${rootmnt}/live/cow"
1230         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"
1231 }
1232
1233 check_dev ()
1234 {
1235         sysdev="${1}"
1236         devname="${2}"
1237         skip_uuid_check="${3}"
1238
1239         if [ -z "${devname}" ]
1240         then
1241                 devname=$(sys2dev "${sysdev}")
1242         fi
1243
1244         if [ -d "${devname}" ]
1245         then
1246                 mount -o bind "${devname}" $mountpoint || continue
1247
1248                 if is_live_path $mountpoint
1249                 then
1250                         echo $mountpoint
1251                         return 0
1252                 else
1253                         umount $mountpoint
1254                 fi
1255         fi
1256
1257         if [ -n "${LIVE_MEDIA_OFFSET}" ]
1258         then
1259                 loopdevname=$(setup_loop "${devname}" "loop" "/sys/block/loop*" "${LIVE_MEDIA_OFFSET}" '')
1260                 devname="${loopdevname}"
1261         fi
1262
1263         fstype=$(get_fstype "${devname}")
1264
1265         if is_supported_fs ${fstype}
1266         then
1267                 mount -t ${fstype} -o ro,noatime "${devname}" ${mountpoint} || continue
1268
1269                 if is_live_path ${mountpoint} && \
1270                         ([ "${skip_uuid_check}" ] || matches_uuid ${mountpoint})
1271                 then
1272                         echo ${mountpoint}
1273                         return 0
1274                 else
1275                         umount ${mountpoint}
1276                 fi
1277         fi
1278
1279         if [ -n "${LIVE_MEDIA_OFFSET}" ]
1280         then
1281                 losetup -d "${loopdevname}"
1282         fi
1283
1284         return 1
1285 }
1286
1287 find_livefs ()
1288 {
1289         timeout="${1}"
1290
1291         # first look at the one specified in the command line
1292         if [ ! -z "${LIVE_MEDIA}" ]
1293         then
1294                 if check_dev "null" "${LIVE_MEDIA}" "skip_uuid_check"
1295                 then
1296                         return 0
1297                 fi
1298         fi
1299
1300         # don't start autodetection before timeout has expired
1301         if [ -n "${LIVE_MEDIA_TIMEOUT}" ]
1302         then
1303                 if [ "${timeout}" -lt "${LIVE_MEDIA_TIMEOUT}" ]
1304                 then
1305                         return 1
1306                 fi
1307         fi
1308
1309         # or do the scan of block devices
1310         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram | grep -v 'dm-' | grep -v fd )
1311         do
1312                 devname=$(sys2dev "${sysblock}")
1313                 fstype=$(get_fstype "${devname}")
1314
1315                 if /lib/udev/cdrom_id ${devname} > /dev/null
1316                 then
1317                         if check_dev "null" "${devname}"
1318                         then
1319                                 return 0
1320                         fi
1321                 elif is_nice_device "${sysblock}"
1322                 then
1323                         for dev in $(subdevices "${sysblock}")
1324                         do
1325                                 if check_dev "${dev}"
1326                                 then
1327                                         return 0
1328                                 fi
1329                         done
1330                 elif [ "${fstype}" = "squashfs" -o \
1331                         "${fstype}" = "ext2" -o \
1332                         "${fstype}" = "ext3" -o \
1333                         "${fstype}" = "jffs2" ]
1334                 then
1335                         # This is an ugly hack situation, the block device has
1336                         # an image directly on it.  It's hopefully
1337                         # live-initramfs, so take it and run with it.
1338                         ln -s "${devname}" "${devname}.${fstype}"
1339                         echo "${devname}.${fstype}"
1340                         return 0
1341                 fi
1342         done
1343
1344         return 1
1345 }
1346
1347 set_usplash_timeout ()
1348 {
1349         if [ -x /sbin/usplash_write ]
1350         then
1351                 /sbin/usplash_write "TIMEOUT 120"
1352         else if [ -x /sbin/splashy_update ] ; then
1353                 /sbin/splashy_update "TIMEOUT 120"
1354         fi ; fi
1355 }
1356
1357 mountroot ()
1358 {
1359         if [ -x /scripts/local-top/cryptroot ]; then
1360             /scripts/local-top/cryptroot
1361         fi
1362
1363         exec 6>&1
1364         exec 7>&2
1365         exec > live.log
1366         exec 2>&1
1367         tail -f live.log >&7 &
1368         tailpid="${!}"
1369
1370         # Ensure 'panic' function is overridden
1371         . /scripts/live-functions
1372
1373         Arguments
1374
1375         set_usplash_timeout
1376         log_begin_msg "Running /scripts/live-premount"
1377         run_scripts /scripts/live-premount
1378         log_end_msg
1379
1380         # Needed here too because some things (*cough* udev *cough*)
1381         # changes the timeout
1382
1383         set_usplash_timeout
1384
1385         if [ ! -z "${NETBOOT}" ] || [ ! -z "${FETCH}" ] || [ ! -z "${HTTPFS}" ] || [ ! -z "${FTPFS}" ]
1386         then
1387                 if do_netmount
1388                 then
1389                         livefs_root="${mountpoint}"
1390                 else
1391                         panic "Unable to find a live file system on the network"
1392                 fi
1393         else
1394                 if [ -n "${PLAIN_ROOT}" ] && [ -n "${ROOT}" ]
1395                 then
1396                         # Do a local boot from hd
1397                         livefs_root=${ROOT}
1398                 else
1399                         # Scan local devices for the image
1400                         for i in 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
1401                         do
1402                                 livefs_root=$(find_livefs ${i})
1403
1404                                 if [ -n "${livefs_root}" ]
1405                                 then
1406                                         break
1407                                 fi
1408
1409                                 sleep 1
1410                         done
1411                 fi
1412         fi
1413
1414         if [ -z "${livefs_root}" ]
1415         then
1416                 panic "Unable to find a medium containing a live file system"
1417         fi
1418
1419         if [ "${TORAM}" ]
1420         then
1421                 live_dest="ram"
1422         elif [ "${TODISK}" ]
1423         then
1424                 live_dest="${TODISK}"
1425         fi
1426
1427         if [ "${live_dest}" ]
1428         then
1429                 log_begin_msg "Copying live media to ${live_dest}"
1430                 copy_live_to "${livefs_root}" "${live_dest}"
1431                 log_end_msg
1432         fi
1433
1434         if [ -n "${MODULETORAMFILE}" ]
1435         then
1436                 setup_unionfs "${livefs_root}" "${rootmnt}"
1437         else
1438                 mac="$(get_mac)"
1439                 mac="$(echo ${mac} | sed 's/-//g')"
1440                 mount_images_in_directory "${livefs_root}" "${rootmnt}" "${mac}"
1441         fi
1442
1443         log_end_msg
1444
1445         maybe_break live-bottom
1446         log_begin_msg "Running /scripts/live-bottom"
1447
1448         run_scripts /scripts/live-bottom
1449         log_end_msg
1450
1451         exec 1>&6 6>&-
1452         exec 2>&7 7>&-
1453         kill ${tailpid}
1454         cp live.log "${rootmnt}/var/log/"
1455 }