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