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