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