Adding message to notify users when live-initramfs is falling back to unionfs-fuse.
[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                         quickusbmodules)
405                                 QUICKUSBMODULES="Yes"
406                                 export QUICKUSBMODULES
407                                 ;;
408
409                         preseed/file=*|file=*)
410                                 LOCATION="${ARGUMENT#*=}"
411                                 export LOCATION
412                                 ;;
413
414                         nopreseed)
415                                 NOPRESEED="Yes"
416                                 export NOPRESEED
417                                 ;;
418
419                         url=*)
420                                 location="${ARGUMENT#url=}"
421
422                                 mount -o bind /sys /root/sys
423                                 mount -o bind /proc /root/proc
424                                 mount -o bind /dev /root/dev
425
426                                 mkdir -p /root/var/run/network
427                                 [ "${NETBOOT}" ] || chroot /root dhclient eth0
428                                 chroot /root wget -P /tmp "${location}"
429                                 [ "${NETBOOT}" ] || chroot /root ifconfig eth0 down
430
431                                 umount /root/sys
432                                 umount /root/proc
433                                 umount /root/dev
434
435                                 LOCATION="/tmp/$(basename "${location}")"
436                                 ;;
437
438                         */*=*)
439                                 question="${ARGUMENT%%=*}"
440                                 value="${ARGUMENT#*=}"
441                                 PRESEEDS="${PRESEEDS}\"${question}=${value}\" "
442                                 export PRESEEDS
443                                 ;;
444
445                         showmounts)
446                                 SHOWMOUNTS="Yes"
447                                 export SHOWMOUNTS
448                                 ;;
449
450                         silent)
451                                 SILENT="Yes"
452                                 export SILENT
453                                 ;;
454
455                         textonly)
456                                 TEXTONLY="Yes"
457                                 export TEXTONLY
458                                 ;;
459
460                         timezone=*)
461                                 TIMEZONE="${ARGUMENT#timezone=}"
462                                 export TIMEZONE
463                                 ;;
464
465                         notimezone)
466                                 NOTIMEZONE="Yes"
467                                 export NOTIMEZONE
468                                 ;;
469
470                         todisk=*)
471                                 TODISK="${ARGUMENT#todisk=}"
472                                 export TODISK
473                                 ;;
474
475                         toram)
476                                 TORAM="Yes"
477                                 export TORAM
478                                 ;;
479
480                         toram=*)
481                                 TORAM="Yes"
482                                 MODULETORAM="${ARGUMENT#toram=}"
483                                 export TORAM MODULETORAM
484                                 ;;
485
486                         exposedroot)
487                                 EXPOSED_ROOT="Yes"
488                                 export EXPOSED_ROOT
489                                 ;;
490
491                         plainroot)
492                                 PLAIN_ROOT="Yes"
493                                 export PLAIN_ROOT
494                                 ;;
495
496                         skipunion)
497                                 SKIP_UNION_MOUNTS="Yes"
498                                 export SKIP_UNION_MOUNTS
499                                 ;;
500
501                         root=*)
502                                 ROOT="${ARGUMENT#root=}"
503                                 export ROOT
504                                 ;;
505
506                         union=*)
507                                 UNIONTYPE="${ARGUMENT#union=}"
508                                 export UNIONTYPE
509                                 ;;
510
511                         utc=*)
512                                 UTC="${ARGUMENT#utc=}"
513                                 export UTC
514                                 ;;
515
516                         xdebconf)
517                                 XDEBCONF="Yes"
518                                 export XDEBCONF
519                                 ;;
520
521                         xdriver=*)
522                                 XDRIVER="${ARGUMENT#xdriver=}"
523                                 export XDRIVER
524                                 ;;
525
526                         xvideomode=*)
527                                 XVIDEOMODE="${ARGUMENT#xvideomode=}"
528                                 export XVIDEOMODE
529                                 ;;
530                 esac
531         done
532
533         # sort of compatibility with netboot.h from linux docs
534         if [ -z "${NETBOOT}" ]
535         then
536                 if [ "${ROOT}" = "/dev/nfs" ]
537                 then
538                         NETBOOT="nfs"
539                         export NETBOOT
540                 elif [ "${ROOT}" = "/dev/cifs" ]
541                 then
542                         NETBOOT="cifs"
543                         export NETBOOT
544                 fi
545         fi
546
547         if [ -z "${MODULE}" ]
548         then
549                 MODULE="filesystem"
550                 export MODULE
551         fi
552
553         if [ -z "${UNIONTYPE}" ]
554         then
555                 UNIONTYPE="aufs"
556                 export UNIONTYPE
557         fi
558 }
559
560 is_live_path ()
561 {
562         DIRECTORY="${1}"
563
564         if [ -d "${DIRECTORY}"/"${LIVE_MEDIA_PATH}" ]
565         then
566                 for FILESYSTEM in squashfs ext2 ext3 ext4 xfs dir jffs2
567                 do
568                         if [ "$(echo ${DIRECTORY}/${LIVE_MEDIA_PATH}/*.${FILESYSTEM})" != "${DIRECTORY}/${LIVE_MEDIA_PATH}/*.${FILESYSTEM}" ]
569                         then
570                                 return 0
571                         fi
572                 done
573         fi
574
575         return 1
576 }
577
578 matches_uuid ()
579 {
580         if [ "${IGNORE_UUID}" ] || [ ! -e /conf/uuid.conf ]
581         then
582                 return 0
583         fi
584
585         path="${1}"
586         uuid="$(cat /conf/uuid.conf)"
587
588         for try_uuid_file in "${path}/.disk/live-uuid"*
589         do
590                 [ -e "${try_uuid_file}" ] || continue
591
592                 try_uuid="$(cat "${try_uuid_file}")"
593
594                 if [ "${uuid}" = "${try_uuid}" ]
595                 then
596                         return 0
597                 fi
598         done
599
600         return 1
601 }
602
603 get_backing_device ()
604 {
605         case "${1}" in
606                 *.squashfs|*.ext2|*.ext3|*.ext4|*.jffs2)
607                         echo $(setup_loop "${1}" "loop" "/sys/block/loop*" '0' "${LIVE_MEDIA_ENCRYPTION}" "${2}")
608                         ;;
609
610                 *.dir)
611                         echo "directory"
612                         ;;
613
614                 *)
615                         panic "Unrecognized live filesystem: ${1}"
616                         ;;
617         esac
618 }
619
620 match_files_in_dir ()
621 {
622         # Does any files match pattern ${1} ?
623         local pattern="${1}"
624
625         if [ "$(echo ${pattern})" != "${pattern}" ]
626         then
627                 return 0
628         fi
629
630         return 1
631 }
632
633 mount_images_in_directory ()
634 {
635         directory="${1}"
636         rootmnt="${2}"
637         mac="${3}"
638
639
640         if match_files_in_dir "${directory}/${LIVE_MEDIA_PATH}/*.squashfs" ||
641                 match_files_in_dir "${directory}/${LIVE_MEDIA_PATH}/*.ext2" ||
642                 match_files_in_dir "${directory}/${LIVE_MEDIA_PATH}/*.ext3" ||
643                 match_files_in_dir "${directory}/${LIVE_MEDIA_PATH}/*.ext4" ||
644                 match_files_in_dir "${directory}/${LIVE_MEDIA_PATH}/*.jffs2" ||
645                 match_files_in_dir "${directory}/${LIVE_MEDIA_PATH}/*.dir"
646         then
647                 [ -n "${mac}" ] && adddirectory="${directory}/${LIVE_MEDIA_PATH}/${mac}"
648                 setup_unionfs "${directory}/${LIVE_MEDIA_PATH}" "${rootmnt}" "${adddirectory}"
649         else
650                 panic "No supported filesystem images found at /${LIVE_MEDIA_PATH}."
651         fi
652 }
653
654 is_nice_device ()
655 {
656         sysfs_path="${1#/sys}"
657
658         if /lib/udev/path_id "${sysfs_path}" | grep -E -q "ID_PATH=(usb|pci-|platform-mmc)"
659         then
660                 return 0
661         elif echo "${sysfs_path}" | grep -q '^/block/vd[a-z]$'
662         then
663                 return 0
664         fi
665
666         return 1
667 }
668
669 copy_live_to ()
670 {
671         copyfrom="${1}"
672         copytodev="${2}"
673         copyto="${copyfrom}_swap"
674
675         if [ -z "${MODULETORAM}" ]
676         then
677                 size=$(fs_size "" ${copyfrom}/${LIVE_MEDIA_PATH} "used")
678         else
679                 MODULETORAMFILE="${copyfrom}/${LIVE_MEDIA_PATH}/${MODULETORAM}"
680
681                 if [ -f "${MODULETORAMFILE}" ]
682                 then
683                         size=$( expr $(ls -la ${MODULETORAMFILE} | awk '{print $5}') / 1024 + 5000 )
684                 else
685                         log_warning_msg "Error: toram-module ${MODULETORAM} (${MODULETORAMFILE}) could not be read."
686                         return 1
687                 fi
688         fi
689
690         if [ "${copytodev}" = "ram" ]
691         then
692                 # copying to ram:
693                 freespace=$( expr $(awk '/MemFree/{print $2}' /proc/meminfo) + $( awk '/\<Cached/{print $2}' /proc/meminfo ) )
694                 mount_options="-o size=${size}k"
695                 free_string="memory"
696                 fstype="tmpfs"
697                 dev="/dev/shm"
698         else
699                 # it should be a writable block device
700                 if [ -b "${copytodev}" ]
701                 then
702                         dev="${copytodev}"
703                         free_string="space"
704                         fstype=$(get_fstype "${dev}")
705                         freespace=$(fs_size "${dev}")
706                 else
707                         log_warning_msg "${copytodev} is not a block device."
708                         return 1
709                 fi
710         fi
711
712         if [ "${freespace}" -lt "${size}" ]
713         then
714                 log_warning_msg "Not enough free ${free_string} (${freespace}k free, ${size}k needed) to copy live media in ${copytodev}."
715                 return 1
716         fi
717
718         # begin copying (or uncompressing)
719         mkdir "${copyto}"
720         log_begin_msg "mount -t ${fstype} ${mount_options} ${dev} ${copyto}"
721         mount -t "${fstype}" ${mount_options} "${dev}" "${copyto}"
722
723         if [ "${extension}" = "tgz" ]
724         then
725                 cd "${copyto}"
726                 tar zxf "${copyfrom}/${LIVE_MEDIA_PATH}/$(basename ${FETCH})"
727                 rm -f "${copyfrom}/${LIVE_MEDIA_PATH}/$(basename ${FETCH})"
728                 mount -r -o move "${copyto}" "${rootmnt}"
729                 cd "${OLDPWD}"
730         else
731                 if [ -n "${MODULETORAMFILE}" ]
732                 then
733                         if [ -x /bin/rsync ]
734                         then
735                                 echo " * Copying $MODULETORAMFILE to RAM" 1>/dev/console
736                                 rsync -a --progress ${MODULETORAMFILE} ${copyto} 1>/dev/console # copy only the filesystem module
737                         else
738                                 cp ${MODULETORAMFILE} ${copyto} # copy only the filesystem module
739                         fi
740                 else
741                         if [ -x /bin/rsync ]
742                         then
743                                 echo " * Copying whole medium to RAM" 1>/dev/console
744                                 rsync -a --progress ${copyfrom}/* ${copyto} 1>/dev/console  # "cp -a" from busybox also copies hidden files
745                         else
746                                 mkdir -p ${copyto}/${LIVE_MEDIA_PATH}
747                                 cp -a ${copyfrom}/${LIVE_MEDIA_PATH}/* ${copyto}/${LIVE_MEDIA_PATH}   # "cp -a" from busybox also copies hidden files
748                         fi
749                 fi
750
751                 umount ${copyfrom}
752                 mount -r -o move ${copyto} ${copyfrom}
753         fi
754
755         rmdir ${copyto}
756         return 0
757 }
758
759 do_netmount ()
760 {
761         rc=1
762
763         modprobe -q af_packet # For DHCP
764
765         udevadm trigger
766         udevadm settle
767
768         ipconfig ${DEVICE} | tee /netboot.config
769
770         # source relevant ipconfig output
771         OLDHOSTNAME=${HOSTNAME}
772         . /tmp/net-${DEVICE}.conf
773         [ -z ${HOSTNAME} ] && HOSTNAME=${OLDHOSTNAME}
774         export HOSTNAME
775
776         # Check if we have a network device at all
777         if ! ls /sys/class/net/eth0 > /dev/null 2>&1 && \
778            ! ls /sys/class/net/wlan0 > /dev/null 2>&1 && \
779            ! ls /sys/class/net/ath0 > /dev/null 2>&1 && \
780            ! ls /sys/class/net/ra0 > /dev/null 2>&1
781         then
782                 panic "No supported network device found, maybe a non-mainline driver is required."
783         fi
784
785         if [ "${NFSROOT}" = "auto" ]
786         then
787                 NFSROOT=${ROOTSERVER}:${ROOTPATH}
788         fi
789
790         if ( [ -n "${FETCH}" ] || [ -n "${HTTPFS}" ] || [ -n "${FTPFS}" ] ) && do_httpmount
791         then
792                 rc=0
793                 return ${rc}
794         fi
795
796         if [ "${NFSROOT#*:}" = "${NFSROOT}" ] && [ "$NETBOOT" != "cifs" ]
797         then
798                 NFSROOT=${ROOTSERVER}:${NFSROOT}
799         fi
800
801         log_begin_msg "Trying netboot from ${NFSROOT}"
802
803         if [ "${NETBOOT}" != "nfs" ] && do_cifsmount
804         then
805                 rc=0
806         elif do_nfsmount
807         then
808                 NETBOOT="nfs"
809                 export NETBOOT
810                 rc=0
811         fi
812
813         log_end_msg
814         return ${rc}
815 }
816
817 do_httpmount ()
818 {
819         rc=1
820         dest="${mountpoint}/${LIVE_MEDIA_PATH}"
821         mount -t ramfs ram "${mountpoint}"
822         mkdir -p "${dest}"
823
824         for webfile in HTTPFS FTPFS FETCH
825         do
826                 url="$(eval echo \"\$\{${webfile}\}\")"
827                 extension="$(echo "${url}" | sed 's/\(.*\)\.\(.*\)/\2/')"
828
829                 if [ -n "$url" ]
830                 then
831                         case "${extension}" in
832                                 squashfs|tgz|tar)
833                                         if [ "${webfile}" = "FETCH" ]
834                                         then
835                                                 case "$url" in
836                                                         tftp*)
837                                                                 ip="$(dirname $url | sed -e 's|tftp://||g' -e 's|/.*$||g')"
838                                                                 rfile="$(echo $url | sed -e "s|tftp://$ip||g")"
839                                                                 lfile="$(basename $url)"
840                                                                 log_begin_msg "Trying tftp -g -b 10240 -r $rfile -l ${dest}/$lfile $ip"
841                                                                 tftp -g -b 10240 -r $rfile -l ${dest}/$lfile $ip
842                                                         ;;
843
844                                                         *)
845                                                                 log_begin_msg "Trying wget ${url} -O ${dest}/$(basename ${url})"
846                                                                 wget "${url}" -O "${dest}/$(basename ${url})"
847                                                                 ;;
848                                                 esac
849                                         else
850                                                 log_begin_msg "Trying to mount ${url} on ${dest}/$(basename ${url})"
851                                                 if [ "${webfile}" = "FTPFS" ]
852                                                 then
853                                                         FUSE_MOUNT="curlftpfs"
854                                                         url="$(dirname ${url})"
855                                                 else
856                                                         FUSE_MOUNT="httpfs"
857                                                 fi
858                                                 modprobe fuse
859                                                 $FUSE_MOUNT "${url}" "${dest}"
860                                         fi
861                                         [ ${?} -eq 0 ] && rc=0
862                                         [ "${extension}" = "tgz" ] && live_dest="ram"
863                                         break
864                                         ;;
865
866                                 *)
867                                         log_begin_msg "Unrecognized archive extension for ${url}"
868                                         ;;
869                         esac
870                 fi
871         done
872
873         if [ ${rc} != 0 ]
874         then
875             umount "${mountpoint}"
876         fi
877
878         return ${rc}
879 }
880
881 do_nfsmount ()
882 {
883         rc=1
884
885         modprobe -q nfs
886
887         if [ -z "${NFSOPTS}" ]
888         then
889                 NFSOPTS=""
890         fi
891
892         log_begin_msg "Trying nfsmount -o nolock -o ro ${NFSOPTS} ${NFSROOT} ${mountpoint}"
893
894         # FIXME: This while loop is an ugly HACK round an nfs bug
895         i=0
896         while [ "$i" -lt 60 ]
897         do
898                 nfsmount -o nolock -o ro ${NFSOPTS} "${NFSROOT}" "${mountpoint}" && rc=0 && break
899                 sleep 1
900                 i="$(($i + 1))"
901         done
902
903         return ${rc}
904 }
905
906 do_cifsmount ()
907 {
908         rc=1
909
910         if [ -x "/sbin/mount.cifs" ]
911         then
912                 if [ -z "${NFSOPTS}" ]
913                 then
914                         CIFSOPTS="-ouser=root,password="
915                 else
916                         CIFSOPTS="${NFSOPTS}"
917                 fi
918
919                 log_begin_msg "Trying mount.cifs ${NFSROOT} ${mountpoint} ${CIFSOPTS}"
920                 modprobe -q cifs
921
922                 if mount.cifs "${NFSROOT}" "${mountpoint}" "${CIFSOPTS}"
923                 then
924                         rc=0
925                 fi
926         fi
927
928         return ${rc}
929 }
930
931 do_snap_copy ()
932 {
933         fromdev="${1}"
934         todir="${2}"
935         snap_type="${3}"
936         size=$(fs_size "${fromdev}" "" "used")
937
938         if [ -b "${fromdev}" ]
939         then
940                 # look for free mem
941                 if [ -n "${HOMEMOUNTED}" -a "${snap_type}" = "HOME" ]
942                 then
943                         todev=$(awk -v pat="$(base_path ${todir})" '$2 == pat { print $1 }' /proc/mounts)
944                         freespace=$(df -k | awk '/'${todev}'/{print $4}')
945                 else
946                         freespace=$( expr $(awk '/MemFree/{print $2}' /proc/meminfo) + $( awk '/\<Cached/{print $2}' /proc/meminfo))
947                 fi
948
949                 tomount="/mnt/tmpsnap"
950
951                 if [ ! -d "${tomount}" ]
952                 then
953                         mkdir -p "${tomount}"
954                 fi
955
956                 fstype=$(get_fstype "${fromdev}")
957
958                 if [ -n "${fstype}" ]
959                 then
960                         # Copying stuff...
961                         mount -o ro -t "${fstype}" "${fromdev}" "${tomount}" || log_warning_msg "Error in mount -t ${fstype} -o ro ${fromdev} ${tomount}"
962                         cp -a "${tomount}"/* ${todir}
963                         umount "${tomount}"
964                 else
965                         log_warning_msg "Unrecognized fstype: ${fstype} on ${fromdev}:${snap_type}"
966                 fi
967
968                 rmdir "${tomount}"
969
970                 if echo ${fromdev} | grep -qs loop
971                 then
972                         losetup -d "${fromdev}"
973                 fi
974
975                 return 0
976         else
977                 return 1
978
979                 log_warning_msg "Unable to find the snapshot ${snap_type} medium"
980         fi
981 }
982
983 find_snap ()
984 {
985         # Look for ${snap_label}.* in block devices
986         snap_label="${1}"
987
988         if [ "${PERSISTENT}" != "nofiles" ]
989         then
990                 # search for image files
991                 snapdata=$(find_files "${PERSISTENT_PATH}${snap_label}.squashfs ${PERSISTENT_PATH}${snap_label}.cpio.gz ${PERSISTENT_PATH}${snap_label}.ext2 ${PERSISTENT_PATH}${snap_label}.ext3 ${PERSISTENT_PATH}${snap_label}.ext4 ${PERSISTENT_PATH}${snap_label}.jffs2")
992         fi
993
994         if [ -z "${snapdata}" ]
995         then
996                 snapdata=$(find_cow_device "${snap_label}")
997         fi
998         echo "${snapdata}"
999 }
1000
1001 try_snap ()
1002 {
1003         # copy the contents of previously found snapshot to ${snap_mount}
1004         # and remember the device and filename for resync on exit in live-initramfs.init
1005
1006         snapdata="${1}"
1007         snap_mount="${2}"
1008         snap_type="${3}"
1009
1010         if [ -n "${snapdata}" ] && [ ! -b "${snapdata}" ]
1011         then
1012                 log_success_msg "found snapshot: ${snapdata}"
1013                 snapdev="$(echo ${snapdata} | cut -f1 -d ' ')"
1014                 snapback="$(echo ${snapdata} | cut -f2 -d ' ')"
1015                 snapfile="$(echo ${snapdata} | cut -f3 -d ' ')"
1016
1017                 RES=""
1018                 if ! try_mount "${snapdev}" "${snapback}" "ro"
1019                 then
1020                         break
1021                 fi
1022
1023                 if echo "${snapfile}" | grep -qs '\(squashfs\|ext2\|ext3\|ext4\|jffs2\)'
1024                 then
1025                         # squashfs, jffs2 or ext2/ext3/ext4 snapshot
1026                         dev=$(get_backing_device "${snapback}/${snapfile}")
1027
1028                         do_snap_copy "${dev}" "${snap_mount}" "${snap_type}"
1029                         RES=$?
1030                 else
1031                         # cpio.gz snapshot
1032                         cd "${snap_mount}"
1033                         zcat "${snapback}/${snapfile}" | /bin/cpio --extract --preserve-modification-time --no-absolute-filenames --sparse --unconditional --make-directories > /dev/null 2>&1
1034                         RES=$?
1035                         if [ "${RES}" != "0" ]
1036                         then
1037                                 log_warning_msg "failure to \"zcat ${snapback}/${snapfile} | /bin/cpio --extract --preserve-modification-time --no-absolute-filenames --sparse --unconditional --make-directories\""
1038                         fi
1039                         cd "${OLDPWD}"
1040                 fi
1041
1042                 umount "${snapback}" ||  log_warning_msg "failure to \"umount ${snapback}\""
1043
1044                 if [ "${RES}" != "0" ]
1045                 then
1046                         log_warning_msg "Impossible to include the ${snapfile} Snapshot file"
1047                 fi
1048
1049         elif [ -b "${snapdata}" ]
1050         then
1051                 # Try to find if it could be a snapshot partition
1052                 dev="${snapdata}"
1053                 log_success_msg "found snapshot ${snap_type} device on ${dev}"
1054                 if echo "${dev}" | grep -qs loop
1055                 then
1056                         # strange things happens, user confused?
1057                         snaploop=$( losetup ${dev} | awk '{print $3}' | tr -d '()' )
1058                         snapfile=$(basename ${snaploop})
1059                         snapdev=$(awk -v pat="$( dirname ${snaploop})" '$2 == pat { print $1 }' /proc/mounts)
1060                 else
1061                         snapdev="${dev}"
1062                 fi
1063
1064                 if ! do_snap_copy "${dev}" "${snap_mount}" "${snap_type}"
1065                 then
1066                         log_warning_msg "Impossible to include the ${snap_type} Snapshot"
1067                         return 1
1068                 else
1069                         if [ -n "${snapfile}" ]
1070                         then
1071                                 # it was a loop device, user confused
1072                                 umount ${snapdev}
1073                         fi
1074                 fi
1075         else
1076                 log_warning_msg "Impossible to include the ${snap_type} Snapshot"
1077                 return 1
1078         fi
1079
1080         echo "export ${snap_type}SNAP="${snap_mount}":${snapdev}:${snapfile}" >> /etc/live.conf # for resync on reboot/halt
1081         return 0
1082 }
1083
1084 setup_unionfs ()
1085 {
1086         image_directory="${1}"
1087         rootmnt="${2}"
1088         addimage_directory="${3}"
1089
1090         case ${UNIONTYPE} in
1091                 aufs|unionfs)
1092                         modprobe -q -b ${UNIONTYPE}
1093
1094                         if ! cut -f2 /proc/filesystems | grep -q "^${UNIONTYPE}\$" && [ -x /bin/unionfs-fuse ]
1095                         then
1096                                 echo "${UNIONTYPE} not available, falling back to unionfs-fuse."
1097                                 echo "This might be really slow."
1098
1099                                 UNIONTYPE="unionfs-fuse"
1100                         fi
1101                         ;;
1102         esac
1103
1104         # run-init can't deal with images in a subdir, but we're going to
1105         # move all of these away before it runs anyway.  No, we're not,
1106         # put them in / since move-mounting them into / breaks mono and
1107         # some other apps.
1108
1109         croot="/"
1110
1111         # Let's just mount the read-only file systems first
1112         rofsstring=""
1113         rofslist=""
1114
1115         if [ "${UNIONTYPE}" = "aufs" ]
1116         then
1117                 roopt="rr,noxino"
1118         elif [ "${UNIONTYPE}" = "unionfs-fuse" ]
1119         then
1120                 roopt="RO"
1121         else
1122                 roopt="ro"
1123         fi
1124
1125         if [ -z "${PLAIN_ROOT}" ]
1126         then
1127                 # Read image names from ${MODULE}.module if it exists
1128                 if [ -e "${image_directory}/filesystem.${MODULE}.module" ]
1129                 then
1130                         for IMAGE in $(cat ${image_directory}/filesystem.${MODULE}.module)
1131                         do
1132                                 image_string="${image_string} ${image_directory}/${IMAGE}"
1133                         done
1134                 elif [ -e "${image_directory}/${MODULE}.module" ]
1135                 then
1136                         for IMAGE in $(cat ${image_directory}/${MODULE}.module)
1137                         do
1138                                 image_string="${image_string} ${image_directory}/${IMAGE}"
1139                         done
1140                 else
1141                         # ${MODULE}.module does not exist, create a list of images
1142                         for FILESYSTEM in squashfs ext2 ext3 ext4 xfs jffs2 dir
1143                         do
1144                                 for IMAGE in "${image_directory}"/*."${FILESYSTEM}"
1145                                 do
1146                                         if [ -e "${IMAGE}" ]
1147                                         then
1148                                                 image_string="${image_string} ${IMAGE}"
1149                                         fi
1150                                 done
1151                         done
1152
1153                         if [ -n "${addimage_directory}" ] && [ -d "${addimage_directory}" ]
1154                         then
1155                                 for FILESYSTEM in squashfs ext2 ext3 ext4 xfs jffs2 dir
1156                                 do
1157                                         for IMAGE in "${addimage_directory}"/*."${FILESYSTEM}"
1158                                         do
1159                                                 if [ -e "${IMAGE}" ]
1160                                                 then
1161                                                         image_string="${image_string} ${IMAGE}"
1162                                                 fi
1163                                         done
1164                                 done
1165                         fi
1166
1167                         # Now sort the list
1168                         image_string="$(echo ${image_string} | sed -e 's/ /\n/g' | sort )"
1169                 fi
1170
1171         [ -n "${MODULETORAMFILE}" ] && image_string="${image_directory}/$(basename ${MODULETORAMFILE})"
1172
1173                 mkdir -p "${croot}"
1174
1175                 for image in ${image_string}
1176                 do
1177                         imagename=$(basename "${image}")
1178
1179                         export image devname
1180                         maybe_break live-realpremount
1181                         log_begin_msg "Running /scripts/live-realpremount"
1182                         run_scripts /scripts/live-realpremount
1183                         log_end_msg
1184
1185                         if [ -d "${image}" ]
1186                         then
1187                                 # it is a plain directory: do nothing
1188                                 rofsstring="${image}=${roopt}:${rofsstring}"
1189                                 rofslist="${image} ${rofslist}"
1190                         elif [ -f "${image}" ]
1191                         then
1192                                 if losetup --help 2>&1 | grep -q -- "-r\b"
1193                                 then
1194                                         backdev=$(get_backing_device "${image}" "-r")
1195                                 else
1196                                         backdev=$(get_backing_device "${image}")
1197                                 fi
1198                                 fstype=$(get_fstype "${backdev}")
1199
1200                                 if [ "${fstype}" = "unknown" ]
1201                                 then
1202                                         panic "Unknown file system type on ${backdev} (${image})"
1203                                 fi
1204
1205                                 if [ -z "${fstype}" ]
1206                                 then
1207                                         fstype="${imagename##*.}"
1208                                         log_warning_msg "Unknown file system type on ${backdev} (${image}), assuming ${fstype}."
1209                                 fi
1210
1211                                 mkdir -p "${croot}/${imagename}"
1212                                 log_begin_msg "Mounting \"${image}\" on \"${croot}${imagename}\" via \"${backdev}\""
1213                                 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}"
1214                                 log_end_msg
1215                         fi
1216                 done
1217         else
1218                 # we have a plain root system
1219                 mkdir -p "${croot}/filesystem"
1220                 log_begin_msg "Mounting \"${image_directory}\" on \"${croot}/filesystem\""
1221                 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}"
1222                 # probably broken:
1223                 mount -o bind ${croot}/filesystem $mountpoint
1224                 log_end_msg
1225         fi
1226
1227         rofsstring=${rofsstring%:}
1228
1229         mkdir -p /cow
1230
1231         # Looking for "${root_persistence}" device or file
1232         if [ -n "${PERSISTENT}" ] && [ -z "${NOPERSISTENT}" ]
1233         then
1234                 if [ -z "${QUICKUSBMODULES}" ]
1235                 then
1236                         # Load USB modules
1237                         num_block=$(ls -l /sys/block | wc -l)
1238                         for module in sd_mod uhci-hcd ehci-hcd ohci-hcd usb-storage
1239                         do
1240                                 modprobe -q -b ${module}
1241                         done
1242
1243                         udevadm trigger
1244                         udevadm settle
1245
1246                         # For some reason, udevsettle does not block in this scenario,
1247                         # so we sleep for a little while.
1248                         #
1249                         # See https://bugs.launchpad.net/ubuntu/+source/casper/+bug/84591
1250                         for timeout in 5 4 3 2 1
1251                         do
1252                                 sleep 1
1253
1254                                 if [ $(ls -l /sys/block | wc -l) -gt ${num_block} ]
1255                                 then
1256                                         break
1257                                 fi
1258                         done
1259                 fi
1260
1261                 # search for label and files (this could be hugely optimized)
1262                 cowprobe=$(find_cow_device "${root_persistence}")
1263                 if [ -b "${cowprobe}" ]
1264                 then
1265                         # Blacklist /cow device, to avoid inconsistent setups for overlapping snapshots
1266                         # makes sense to have both persistence for /cow and /home mounted, maybe also with
1267                         # snapshots to be sure to really store some e.g key config files,
1268                         # but not on the same media
1269                         blacklistdev="${cowprobe}"
1270                         PERSISTENCE_IS_ON="1"
1271                         export PERSISTENCE_IS_ON
1272                 fi
1273                 # homecow just mount something on /home, this should be generalized some way
1274                 homecow=$(find_cow_device "${home_persistence}" "${blacklistdev}")
1275                 if [ -b "${homecow}" ]
1276                 then
1277                         PERSISTENCE_IS_ON="1"
1278                         export PERSISTENCE_IS_ON
1279                 fi
1280                 root_snapdata=$(find_snap "${root_snapshot_label}" "${blacklistdev}")
1281                 # This second type should be removed when snapshot will get smarter,
1282                 # hence when "/etc/live-snapshot*list" will be supported also by
1283                 # ext2|ext3|ext4|jffs2 snapshot types.
1284                 home_snapdata=$(find_snap "${home_snapshot_label}" "${blacklistdev}")
1285
1286                 if [ -b "${cowprobe}" ]
1287                 then
1288                         cowdevice=${cowprobe}
1289                         cow_fstype=$(get_fstype "${cowprobe}")
1290                         cow_mountopt="rw,noatime"
1291
1292                         if [ "${FORCEPERSISTENTFSCK}" = "Yes" ]
1293                         then
1294                                 fsck -y ${cowdevice}
1295                         fi
1296                 else
1297                         log_warning_msg "Unable to find the persistent medium"
1298                         cowdevice="tmpfs"
1299                         cow_fstype="tmpfs"
1300                         cow_mountopt="rw,noatime,mode=755"
1301                 fi
1302         elif [ -n "${NFS_COW}" ] && [ -z "${NOPERSISTENT}" ]
1303         then
1304                 # check if there are any nfs options
1305                 if echo ${NFS_COW}|grep -q ','
1306                 then
1307                         nfs_cow_opts="-o nolock,$(echo ${NFS_COW}|cut -d, -f2-)"
1308                         nfs_cow=$(echo ${NFS_COW}|cut -d, -f1)
1309                 else
1310                         nfs_cow_opts="-o nolock"
1311                         nfs_cow=${NFS_COW}
1312                 fi
1313                 mac="$(get_mac)"
1314                 if [ -n "${mac}" ]
1315                 then
1316                         cowdevice=$(echo ${nfs_cow}|sed "s/client_mac_address/${mac}/")
1317                         cow_fstype="nfs"
1318                 else
1319                         panic "unable to determine mac address"
1320                 fi
1321         else
1322                 cowdevice="tmpfs"
1323                 cow_fstype="tmpfs"
1324                 cow_mountopt="rw,noatime,mode=755"
1325         fi
1326
1327         if [ "${cow_fstype}" = "nfs" ]
1328         then
1329                 log_begin_msg \
1330                         "Trying nfsmount ${nfs_cow_opts} ${cowdevice} /cow"
1331                 nfsmount ${nfs_cow_opts} ${cowdevice} /cow || \
1332                         panic "Can not mount ${cowdevice} (n: ${cow_fstype}) on /cow"
1333         else
1334                 mount -t ${cow_fstype} -o ${cow_mountopt} ${cowdevice} /cow || \
1335                         panic "Can not mount ${cowdevice} (o: ${cow_fstype}) on /cow"
1336         fi
1337
1338         rofscount=$(echo ${rofslist} |wc -w)
1339
1340         if [ -n "${EXPOSED_ROOT}" ]
1341         then
1342                 if [ ${rofscount} -ne 1 ]
1343                 then
1344                         panic "only one RO file system supported with exposedroot: ${rofslist}"
1345                 fi
1346                 exposedrootfs=${rofslist%% }
1347
1348                 mount --bind ${exposedrootfs} ${rootmnt} || \
1349                         panic "bind mount of ${exposedrootfs} failed"
1350
1351                 if [ -z "${SKIP_UNION_MOUNTS}" ]
1352                 then
1353                         cow_dirs='/var/tmp /var/lock /var/run /var/log /var/spool /home /var/lib/live'
1354                 else
1355                         cow_dirs=''
1356                 fi
1357
1358                 for dir in ${cow_dirs}; do
1359                         mkdir -p /cow${dir}
1360
1361                         case "${UNIONTYPE}" in
1362                                 unionfs-fuse)
1363                                         (ulimit -n 16384; unionfs-fuse -o cow -o noinitgroups -o default_permissions -o allow_other -o use_ino -o suid /cow=RW:${exposedrootfs}${dir} "${rootmnt}${dir}" || panic "mount ${UNIONTYPE} on ${rootmnt}${dir} failed with option cow,noinitgroups,default_permissions,allow_other,use_ino,suid=/cow=RW:${exposedrootfs}${dir}")
1364                                         ;;
1365
1366                                 *)
1367                                         mount -t ${UNIONTYPE} -o rw,noatime,dirs=/cow${dir}=rw:${exposedrootfs}${dir}=ro ${UNIONTYPE} "${rootmnt}${dir}" || panic "mount ${UNIONTYPE} on ${rootmnt}${dir} failed with option rw,noatime,dirs=/cow${dir}=rw:${exposedrootfs}${dir}=ro"
1368                                         ;;
1369                         esac
1370                 done
1371         else
1372                 case "${UNIONTYPE}" in
1373                         unionfs-fuse)
1374                                 (ulimit -n 16384; unionfs-fuse -o cow -o noinitgroups -o default_permissions -o allow_other -o use_ino -o suid /cow=RW:${rofsstring} "${rootmnt}" || panic "mount ${UNIONTYPE} on ${rootmnt} failed with option cow,noinitgroups,default_permissions,allow_other,use_ino,suid=/cow:RW:${rofsstring}")
1375                                 ;;
1376
1377                         *)
1378                                 mount -t ${UNIONTYPE} -o noatime,dirs=/cow=rw:${rofsstring} ${UNIONTYPE} "${rootmnt}" || panic "mount ${UNIONTYPE} on ${rootmnt} failed with option noatime,dirs=/cow=rw:${rofsstring}"
1379                                 ;;
1380                 esac
1381         fi
1382
1383         # Correct the permissions of /:
1384         chmod 0755 "${rootmnt}"
1385
1386         # tmpfs file systems
1387         touch /etc/fstab
1388         mkdir -p "${rootmnt}/live"
1389         mount -t tmpfs tmpfs ${rootmnt}/live
1390
1391         # Adding other custom mounts
1392         if [ -n "${PERSISTENT}" ] && [ -z "${NOPERSISTENT}" ]
1393         then
1394                 # directly mount /home
1395                 # FIXME: add a custom mounts configurable system
1396
1397                 if [ -b "${homecow}" ]
1398                 then
1399                         mount -t $(get_fstype "${homecow}") -o rw,noatime "${homecow}" "${rootmnt}/home"
1400                         export HOMEMOUNTED=1 # used to proper calculate free space in do_snap_copy()
1401                 else
1402                         log_warning_msg "Unable to find the persistent home medium"
1403                 fi
1404
1405                 # Look for other snapshots to copy in
1406                 try_snap "${root_snapdata}" "${rootmnt}" "ROOT"
1407                 # This second type should be removed when snapshot grow smarter
1408                 try_snap "${home_snapdata}" "${rootmnt}/home" "HOME"
1409         fi
1410
1411         if [ -n "${SHOWMOUNTS}" ]
1412         then
1413                 for d in ${rofslist}
1414                 do
1415                         mkdir -p "${rootmnt}/live/${d##*/}"
1416
1417                         case d in
1418                                 *.dir)
1419                                         # do nothing # mount -o bind "${d}" "${rootmnt}/live/${d##*/}"
1420                                         ;;
1421
1422                                 *)
1423                                         case "${UNIONTYPE}" in
1424                                                 unionfs-fuse)
1425                                                         mount -o bind "${d}" "${rootmnt}/live/${d##*/}"
1426                                                         ;;
1427
1428                                                 *)
1429                                                         mount -o move "${d}" "${rootmnt}/live/${d##*/}"
1430                                                         ;;
1431                                         esac
1432                                         ;;
1433                         esac
1434                 done
1435         fi
1436
1437         # shows cow fs on /cow for use by live-snapshot
1438         mkdir -p "${rootmnt}/live/cow"
1439         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"
1440 }
1441
1442 check_dev ()
1443 {
1444         sysdev="${1}"
1445         devname="${2}"
1446         skip_uuid_check="${3}"
1447
1448         # support for fromiso=.../isofrom=....
1449         if [ -n "$FROMISO" ]
1450         then
1451                 mkdir /isofrom
1452                 ISO_DEVICE="$(echo $FROMISO | sed 's|\(/dev/[a-z]*[0-9]*\).*|\1|')"
1453                 mount "$ISO_DEVICE" /isofrom
1454                 ISO_NAME="$(echo $FROMISO | sed 's|/dev/[a-z]*[0-9]*/||')"
1455                 loopdevname=$(setup_loop "/isofrom/${ISO_NAME}" "loop" "/sys/block/loop*" "" '')
1456                 devname="${loopdevname}"
1457         fi
1458
1459         if [ -z "${devname}" ]
1460         then
1461                 devname=$(sys2dev "${sysdev}")
1462         fi
1463
1464         if [ -d "${devname}" ]
1465         then
1466                 mount -o bind "${devname}" $mountpoint || continue
1467
1468                 if is_live_path $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                 loopdevname=$(setup_loop "${devname}" "loop" "/sys/block/loop*" "${LIVE_MEDIA_OFFSET}" '')
1480                 devname="${loopdevname}"
1481         fi
1482
1483         fstype=$(get_fstype "${devname}")
1484
1485         if is_supported_fs ${fstype}
1486         then
1487                 mount -t ${fstype} -o ro,noatime "${devname}" ${mountpoint} || continue
1488
1489                 if is_live_path ${mountpoint} && \
1490                         ([ "${skip_uuid_check}" ] || matches_uuid ${mountpoint})
1491                 then
1492                         echo ${mountpoint}
1493                         return 0
1494                 else
1495                         umount ${mountpoint}
1496                 fi
1497         fi
1498
1499         if [ -n "${LIVE_MEDIA_OFFSET}" ]
1500         then
1501                 losetup -d "${loopdevname}"
1502         fi
1503
1504         return 1
1505 }
1506
1507 find_livefs ()
1508 {
1509         timeout="${1}"
1510
1511         # don't start autodetection before timeout has expired
1512         if [ -n "${LIVE_MEDIA_TIMEOUT}" ]
1513         then
1514                 if [ "${timeout}" -lt "${LIVE_MEDIA_TIMEOUT}" ]
1515                 then
1516                         return 1
1517                 fi
1518         fi
1519
1520         # first look at the one specified in the command line
1521         case "${LIVE_MEDIA}" in
1522                 removable-usb)
1523                         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1524                         do
1525                                 if [ "$(cat ${sysblock}/removable)" = "1" ]
1526                                 then
1527                                         if readlink ${sysblock} | grep -q usb ||
1528                                            readlink ${sysblock}/device | grep -q usb # linux < 2.6.29
1529                                         then
1530                                                 for dev in $(subdevices "${sysblock}")
1531                                                 do
1532                                                         if check_dev "${dev}"
1533                                                         then
1534                                                                 return 0
1535                                                         fi
1536                                                 done
1537                                         fi
1538                                 fi
1539                         done
1540                         return 1
1541                         ;;
1542
1543                 removable)
1544                         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1545                         do
1546                                 if [ "$(cat ${sysblock}/removable)" = "1" ]
1547                                 then
1548                                         for dev in $(subdevices "${sysblock}")
1549                                         do
1550                                                 if check_dev "${dev}"
1551                                                 then
1552                                                         return 0
1553                                                 fi
1554                                         done
1555                                 fi
1556                         done
1557                         return 1
1558                         ;;
1559
1560                 *)
1561                         if [ ! -z "${LIVE_MEDIA}" ]
1562                         then
1563                                 if check_dev "null" "${LIVE_MEDIA}" "skip_uuid_check"
1564                                 then
1565                                         return 0
1566                                 fi
1567                         fi
1568                         ;;
1569         esac
1570
1571         # or do the scan of block devices
1572         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1573         do
1574                 devname=$(sys2dev "${sysblock}")
1575                 fstype=$(get_fstype "${devname}")
1576
1577                 if /lib/udev/cdrom_id ${devname} > /dev/null
1578                 then
1579                         if check_dev "null" "${devname}"
1580                         then
1581                                 return 0
1582                         fi
1583                 elif is_nice_device "${sysblock}"
1584                 then
1585                         for dev in $(subdevices "${sysblock}")
1586                         do
1587                                 if check_dev "${dev}"
1588                                 then
1589                                         return 0
1590                                 fi
1591                         done
1592                 elif [ "${fstype}" = "squashfs" -o \
1593                         "${fstype}" = "ext2" -o \
1594                         "${fstype}" = "ext3" -o \
1595                         "${fstype}" = "ext4" -o \
1596                         "${fstype}" = "jffs2" ]
1597                 then
1598                         # This is an ugly hack situation, the block device has
1599                         # an image directly on it.  It's hopefully
1600                         # live-initramfs, so take it and run with it.
1601                         ln -s "${devname}" "${devname}.${fstype}"
1602                         echo "${devname}.${fstype}"
1603                         return 0
1604                 fi
1605         done
1606
1607         return 1
1608 }
1609
1610 set_usplash_timeout ()
1611 {
1612         if [ -x /sbin/usplash_write ]
1613         then
1614                 /sbin/usplash_write "TIMEOUT 120"
1615         else if [ -x /sbin/splashy_update ] ; then
1616                 /sbin/splashy_update "TIMEOUT 120"
1617         fi ; fi
1618 }
1619
1620 integrity_check ()
1621 {
1622         media_mountpoint="${1}"
1623
1624         log_begin_msg "Checking media integrity"
1625
1626         cd ${media_mountpoint}
1627         /bin/md5sum -c md5sum.txt < /dev/tty8 > /dev/tty8
1628         RC="${?}"
1629
1630         log_end_msg
1631
1632         if [ "${RC}" -eq 0 ]
1633         then
1634                 log_success_msg "Everything ok, will reboot in 10 seconds."
1635                 sleep 10
1636                 cd /
1637                 umount ${media_mountpoint}
1638                 sync
1639                 echo u > /proc/sysrq-trigger
1640                 echo b > /proc/sysrq-trigger
1641         else
1642                 panic "Not ok, a media defect is likely, switch to VT8 for details."
1643         fi
1644 }
1645
1646 mountroot ()
1647 {
1648         if [ -x /scripts/local-top/cryptroot ]; then
1649             /scripts/local-top/cryptroot
1650         fi
1651
1652         exec 6>&1
1653         exec 7>&2
1654         exec > live.log
1655         exec 2>&1
1656         tail -f live.log >&7 &
1657         tailpid="${!}"
1658
1659         # Ensure 'panic' function is overridden
1660         . /scripts/live-functions
1661
1662         Arguments
1663
1664         set_usplash_timeout
1665
1666         maybe_break live-premount
1667         log_begin_msg "Running /scripts/live-premount"
1668         run_scripts /scripts/live-premount
1669         log_end_msg
1670
1671         # Needed here too because some things (*cough* udev *cough*)
1672         # changes the timeout
1673
1674         set_usplash_timeout
1675
1676         if [ ! -z "${NETBOOT}" ] || [ ! -z "${FETCH}" ] || [ ! -z "${HTTPFS}" ] || [ ! -z "${FTPFS}" ]
1677         then
1678                 if do_netmount
1679                 then
1680                         livefs_root="${mountpoint}"
1681                 else
1682                         panic "Unable to find a live file system on the network"
1683                 fi
1684         else
1685                 if [ -n "${PLAIN_ROOT}" ] && [ -n "${ROOT}" ]
1686                 then
1687                         # Do a local boot from hd
1688                         livefs_root=${ROOT}
1689                 else
1690                         # Scan local devices for the image
1691                         i=0
1692                         while [ "$i" -lt 60 ]
1693                         do
1694                                 livefs_root=$(find_livefs ${i})
1695
1696                                 if [ -n "${livefs_root}" ]
1697                                 then
1698                                         break
1699                                 fi
1700
1701                                 sleep 1
1702                                 i="$(($i + 1))"
1703                         done
1704                 fi
1705         fi
1706
1707         if [ -z "${livefs_root}" ]
1708         then
1709                 panic "Unable to find a medium containing a live file system"
1710         fi
1711
1712         if [ "${INTEGRITY_CHECK}" ]
1713         then
1714                 integrity_check "${livefs_root}"
1715         fi
1716
1717         if [ "${TORAM}" ]
1718         then
1719                 live_dest="ram"
1720         elif [ "${TODISK}" ]
1721         then
1722                 live_dest="${TODISK}"
1723         fi
1724
1725         if [ "${live_dest}" ]
1726         then
1727                 log_begin_msg "Copying live media to ${live_dest}"
1728                 copy_live_to "${livefs_root}" "${live_dest}"
1729                 log_end_msg
1730         fi
1731
1732         # if we do not unmount the ISO we can't run "fsck /dev/ice" later on
1733         # because the mountpoint is left behind in /proc/mounts, so let's get
1734         # rid of it when running from RAM
1735         if [ -n "$FROMISO" ] && [ "${TORAM}" ]
1736         then
1737           losetup -d /dev/loop0
1738           grep -q /isofrom /proc/mounts && umount /isofrom
1739         fi
1740
1741         if [ -n "${MODULETORAMFILE}" ] || [ -n "${PLAIN_ROOT}" ]
1742         then
1743                 setup_unionfs "${livefs_root}" "${rootmnt}"
1744         else
1745                 mac="$(get_mac)"
1746                 mac="$(echo ${mac} | sed 's/-//g')"
1747                 mount_images_in_directory "${livefs_root}" "${rootmnt}" "${mac}"
1748         fi
1749
1750         log_end_msg
1751
1752         # unionfs-fuse needs /dev to be bind-mounted for the duration of
1753         # live-bottom; udev's init script will take care of things after that
1754         if [ "${UNIONTYPE}" = unionfs-fuse ]
1755         then
1756                 mount -n -o bind /dev "${rootmnt}/dev"
1757         fi
1758
1759         maybe_break live-bottom
1760         log_begin_msg "Running /scripts/live-bottom\n"
1761
1762         run_scripts /scripts/live-bottom
1763         log_end_msg
1764
1765         if [ "${UNIONFS}" = unionfs-fuse ]
1766         then
1767                 umount "${rootmnt}/dev"
1768         fi
1769
1770         exec 1>&6 6>&-
1771         exec 2>&7 7>&-
1772         kill ${tailpid}
1773         [ -w "${rootmnt}/var/log/" ] && cp live.log "${rootmnt}/var/log/" 2>/dev/null
1774 }