Synchronize with Debian.
[live-initramfs-grml.git] / scripts / live
1 #!/bin/sh
2
3 # set -e
4
5 export PATH="/root/usr/bin:/root/usr/sbin:/root/bin:/root/sbin:/usr/bin:/usr/sbin:/bin:/sbin"
6
7 echo "/root/lib" >> /etc/ld.so.conf
8 echo "/root/usr/lib" >> /etc/ld.so.conf
9
10 mountpoint="/live/image"
11 LIVE_MEDIA_PATH="live"
12
13 root_persistence="live-rw"
14 home_persistence="home-rw"
15 root_snapshot_label="live-sn"
16 home_snapshot_label="home-sn"
17
18 USERNAME="user"
19 USERFULLNAME="Live user"
20 HOSTNAME="host"
21
22 mkdir -p "${mountpoint}"
23
24 # Create /etc/mtab for debug purpose and future syncs
25 if [ ! -d /etc ]
26 then
27         mkdir /etc/
28 fi
29
30 if [ ! -f /etc/mtab ]
31 then
32         touch /etc/mtab
33 fi
34
35 [ -f /etc/live.conf ] && . /etc/live.conf
36 export USERNAME USERFULLNAME HOSTNAME
37
38 . /scripts/live-helpers
39
40 if [ ! -f /live.vars ]
41 then
42         touch /live.vars
43 fi
44
45 Arguments ()
46 {
47         PRESEEDS=""
48
49         for ARGUMENT in $(cat /proc/cmdline)
50         do
51                 case "${ARGUMENT}" in
52                         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         if [ "${UNIONTYPE}" = unionfs-fuse ]
1105         then
1106                 modprobe fuse
1107         fi
1108
1109         # run-init can't deal with images in a subdir, but we're going to
1110         # move all of these away before it runs anyway.  No, we're not,
1111         # put them in / since move-mounting them into / breaks mono and
1112         # some other apps.
1113
1114         croot="/"
1115
1116         # Let's just mount the read-only file systems first
1117         rofsstring=""
1118         rofslist=""
1119
1120         if [ "${UNIONTYPE}" = "aufs" ]
1121         then
1122                 roopt="rr"
1123                 noxino_opt="noxino,"
1124         elif [ "${UNIONTYPE}" = "unionfs-fuse" ]
1125         then
1126                 roopt="RO"
1127         else
1128                 roopt="ro"
1129         fi
1130
1131         if [ -z "${PLAIN_ROOT}" ]
1132         then
1133                 # Read image names from ${MODULE}.module if it exists
1134                 if [ -e "${image_directory}/filesystem.${MODULE}.module" ]
1135                 then
1136                         for IMAGE in $(cat ${image_directory}/filesystem.${MODULE}.module)
1137                         do
1138                                 image_string="${image_string} ${image_directory}/${IMAGE}"
1139                         done
1140                 elif [ -e "${image_directory}/${MODULE}.module" ]
1141                 then
1142                         for IMAGE in $(cat ${image_directory}/${MODULE}.module)
1143                         do
1144                                 image_string="${image_string} ${image_directory}/${IMAGE}"
1145                         done
1146                 else
1147                         # ${MODULE}.module does not exist, create a list of images
1148                         for FILESYSTEM in squashfs ext2 ext3 ext4 xfs jffs2 dir
1149                         do
1150                                 for IMAGE in "${image_directory}"/*."${FILESYSTEM}"
1151                                 do
1152                                         if [ -e "${IMAGE}" ]
1153                                         then
1154                                                 image_string="${image_string} ${IMAGE}"
1155                                         fi
1156                                 done
1157                         done
1158
1159                         if [ -n "${addimage_directory}" ] && [ -d "${addimage_directory}" ]
1160                         then
1161                                 for FILESYSTEM in squashfs ext2 ext3 ext4 xfs jffs2 dir
1162                                 do
1163                                         for IMAGE in "${addimage_directory}"/*."${FILESYSTEM}"
1164                                         do
1165                                                 if [ -e "${IMAGE}" ]
1166                                                 then
1167                                                         image_string="${image_string} ${IMAGE}"
1168                                                 fi
1169                                         done
1170                                 done
1171                         fi
1172
1173                         # Now sort the list
1174                         image_string="$(echo ${image_string} | sed -e 's/ /\n/g' | sort )"
1175                 fi
1176
1177         [ -n "${MODULETORAMFILE}" ] && image_string="${image_directory}/$(basename ${MODULETORAMFILE})"
1178
1179                 mkdir -p "${croot}"
1180
1181                 for image in ${image_string}
1182                 do
1183                         imagename=$(basename "${image}")
1184
1185                         export image devname
1186                         maybe_break live-realpremount
1187                         log_begin_msg "Running /scripts/live-realpremount"
1188                         run_scripts /scripts/live-realpremount
1189                         log_end_msg
1190
1191                         if [ -d "${image}" ]
1192                         then
1193                                 # it is a plain directory: do nothing
1194                                 rofsstring="${image}=${roopt}:${rofsstring}"
1195                                 rofslist="${image} ${rofslist}"
1196                         elif [ -f "${image}" ]
1197                         then
1198                                 if losetup --help 2>&1 | grep -q -- "-r\b"
1199                                 then
1200                                         backdev=$(get_backing_device "${image}" "-r")
1201                                 else
1202                                         backdev=$(get_backing_device "${image}")
1203                                 fi
1204                                 fstype=$(get_fstype "${backdev}")
1205
1206                                 if [ "${fstype}" = "unknown" ]
1207                                 then
1208                                         panic "Unknown file system type on ${backdev} (${image})"
1209                                 fi
1210
1211                                 if [ -z "${fstype}" ]
1212                                 then
1213                                         fstype="${imagename##*.}"
1214                                         log_warning_msg "Unknown file system type on ${backdev} (${image}), assuming ${fstype}."
1215                                 fi
1216
1217                                 mkdir -p "${croot}/${imagename}"
1218                                 log_begin_msg "Mounting \"${image}\" on \"${croot}${imagename}\" via \"${backdev}\""
1219                                 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}"
1220                                 log_end_msg
1221                         fi
1222                 done
1223         else
1224                 # we have a plain root system
1225                 mkdir -p "${croot}/filesystem"
1226                 log_begin_msg "Mounting \"${image_directory}\" on \"${croot}/filesystem\""
1227                 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}"
1228                 # probably broken:
1229                 mount -o bind ${croot}/filesystem $mountpoint
1230                 log_end_msg
1231         fi
1232
1233         rofsstring=${rofsstring%:}
1234
1235         mkdir -p /cow
1236
1237         # Looking for "${root_persistence}" device or file
1238         if [ -n "${PERSISTENT}" ] && [ -z "${NOPERSISTENT}" ]
1239         then
1240                 if [ -z "${QUICKUSBMODULES}" ]
1241                 then
1242                         # Load USB modules
1243                         num_block=$(ls -l /sys/block | wc -l)
1244                         for module in sd_mod uhci-hcd ehci-hcd ohci-hcd usb-storage
1245                         do
1246                                 modprobe -q -b ${module}
1247                         done
1248
1249                         udevadm trigger
1250                         udevadm settle
1251
1252                         # For some reason, udevsettle does not block in this scenario,
1253                         # so we sleep for a little while.
1254                         #
1255                         # See https://bugs.launchpad.net/ubuntu/+source/casper/+bug/84591
1256                         for timeout in 5 4 3 2 1
1257                         do
1258                                 sleep 1
1259
1260                                 if [ $(ls -l /sys/block | wc -l) -gt ${num_block} ]
1261                                 then
1262                                         break
1263                                 fi
1264                         done
1265                 fi
1266
1267                 # search for label and files (this could be hugely optimized)
1268                 cowprobe=$(find_cow_device "${root_persistence}")
1269                 if [ -b "${cowprobe}" ]
1270                 then
1271                         # Blacklist /cow device, to avoid inconsistent setups for overlapping snapshots
1272                         # makes sense to have both persistence for /cow and /home mounted, maybe also with
1273                         # snapshots to be sure to really store some e.g key config files,
1274                         # but not on the same media
1275                         blacklistdev="${cowprobe}"
1276                         PERSISTENCE_IS_ON="1"
1277                         export PERSISTENCE_IS_ON
1278                 fi
1279                 # homecow just mount something on /home, this should be generalized some way
1280                 homecow=$(find_cow_device "${home_persistence}" "${blacklistdev}")
1281                 if [ -b "${homecow}" ]
1282                 then
1283                         PERSISTENCE_IS_ON="1"
1284                         export PERSISTENCE_IS_ON
1285                 fi
1286                 root_snapdata=$(find_snap "${root_snapshot_label}" "${blacklistdev}")
1287                 # This second type should be removed when snapshot will get smarter,
1288                 # hence when "/etc/live-snapshot*list" will be supported also by
1289                 # ext2|ext3|ext4|jffs2 snapshot types.
1290                 home_snapdata=$(find_snap "${home_snapshot_label}" "${blacklistdev}")
1291
1292                 if [ -b "${cowprobe}" ]
1293                 then
1294                         cowdevice=${cowprobe}
1295                         cow_fstype=$(get_fstype "${cowprobe}")
1296                         cow_mountopt="rw,noatime"
1297
1298                         if [ "${FORCEPERSISTENTFSCK}" = "Yes" ]
1299                         then
1300                                 fsck -y ${cowdevice}
1301                         fi
1302                 else
1303                         log_warning_msg "Unable to find the persistent medium"
1304                         cowdevice="tmpfs"
1305                         cow_fstype="tmpfs"
1306                         cow_mountopt="rw,noatime,mode=755"
1307                 fi
1308         elif [ -n "${NFS_COW}" ] && [ -z "${NOPERSISTENT}" ]
1309         then
1310                 # check if there are any nfs options
1311                 if echo ${NFS_COW}|grep -q ','
1312                 then
1313                         nfs_cow_opts="-o nolock,$(echo ${NFS_COW}|cut -d, -f2-)"
1314                         nfs_cow=$(echo ${NFS_COW}|cut -d, -f1)
1315                 else
1316                         nfs_cow_opts="-o nolock"
1317                         nfs_cow=${NFS_COW}
1318                 fi
1319                 mac="$(get_mac)"
1320                 if [ -n "${mac}" ]
1321                 then
1322                         cowdevice=$(echo ${nfs_cow}|sed "s/client_mac_address/${mac}/")
1323                         cow_fstype="nfs"
1324                 else
1325                         panic "unable to determine mac address"
1326                 fi
1327         else
1328                 cowdevice="tmpfs"
1329                 cow_fstype="tmpfs"
1330                 cow_mountopt="rw,noatime,mode=755"
1331         fi
1332
1333         if [ "${cow_fstype}" = "nfs" ]
1334         then
1335                 log_begin_msg \
1336                         "Trying nfsmount ${nfs_cow_opts} ${cowdevice} /cow"
1337                 nfsmount ${nfs_cow_opts} ${cowdevice} /cow || \
1338                         panic "Can not mount ${cowdevice} (n: ${cow_fstype}) on /cow"
1339         else
1340                 mount -t ${cow_fstype} -o ${cow_mountopt} ${cowdevice} /cow || \
1341                         panic "Can not mount ${cowdevice} (o: ${cow_fstype}) on /cow"
1342         fi
1343
1344         rofscount=$(echo ${rofslist} |wc -w)
1345
1346         if [ -n "${EXPOSED_ROOT}" ]
1347         then
1348                 if [ ${rofscount} -ne 1 ]
1349                 then
1350                         panic "only one RO file system supported with exposedroot: ${rofslist}"
1351                 fi
1352                 exposedrootfs=${rofslist%% }
1353
1354                 mount --bind ${exposedrootfs} ${rootmnt} || \
1355                         panic "bind mount of ${exposedrootfs} failed"
1356
1357                 if [ -z "${SKIP_UNION_MOUNTS}" ]
1358                 then
1359                         cow_dirs='/var/tmp /var/lock /var/run /var/log /var/spool /home /var/lib/live'
1360                 else
1361                         cow_dirs=''
1362                 fi
1363
1364                 for dir in ${cow_dirs}; do
1365                         mkdir -p /cow${dir}
1366
1367                         case "${UNIONTYPE}" in
1368                                 unionfs-fuse)
1369                                         (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}")
1370                                         ;;
1371
1372                                 *)
1373                                         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"
1374                                         ;;
1375                         esac
1376                 done
1377         else
1378                 case "${UNIONTYPE}" in
1379                         unionfs-fuse)
1380                                 (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}")
1381                                 ;;
1382
1383                         *)
1384                                 mount -t ${UNIONTYPE} -o noatime,${noxino_opt}dirs=/cow=rw:${rofsstring} ${UNIONTYPE} "${rootmnt}" || panic "mount ${UNIONTYPE} on ${rootmnt} failed with option noatime,${noxino_opt}dirs=/cow=rw:${rofsstring}"
1385                                 ;;
1386                 esac
1387         fi
1388
1389         # Correct the permissions of /:
1390         chmod 0755 "${rootmnt}"
1391
1392         # tmpfs file systems
1393         touch /etc/fstab
1394         mkdir -p "${rootmnt}/live"
1395         mount -t tmpfs tmpfs ${rootmnt}/live
1396
1397         # Adding other custom mounts
1398         if [ -n "${PERSISTENT}" ] && [ -z "${NOPERSISTENT}" ]
1399         then
1400                 # directly mount /home
1401                 # FIXME: add a custom mounts configurable system
1402
1403                 if [ -b "${homecow}" ]
1404                 then
1405                         mount -t $(get_fstype "${homecow}") -o rw,noatime "${homecow}" "${rootmnt}/home"
1406                         export HOMEMOUNTED=1 # used to proper calculate free space in do_snap_copy()
1407                 else
1408                         log_warning_msg "Unable to find the persistent home medium"
1409                 fi
1410
1411                 # Look for other snapshots to copy in
1412                 try_snap "${root_snapdata}" "${rootmnt}" "ROOT"
1413                 # This second type should be removed when snapshot grow smarter
1414                 try_snap "${home_snapdata}" "${rootmnt}/home" "HOME"
1415         fi
1416
1417         if [ -n "${SHOWMOUNTS}" ]
1418         then
1419                 for d in ${rofslist}
1420                 do
1421                         mkdir -p "${rootmnt}/live/${d##*/}"
1422
1423                         case d in
1424                                 *.dir)
1425                                         # do nothing # mount -o bind "${d}" "${rootmnt}/live/${d##*/}"
1426                                         ;;
1427
1428                                 *)
1429                                         case "${UNIONTYPE}" in
1430                                                 unionfs-fuse)
1431                                                         mount -o bind "${d}" "${rootmnt}/live/${d##*/}"
1432                                                         ;;
1433
1434                                                 *)
1435                                                         mount -o move "${d}" "${rootmnt}/live/${d##*/}"
1436                                                         ;;
1437                                         esac
1438                                         ;;
1439                         esac
1440                 done
1441         fi
1442
1443         # shows cow fs on /cow for use by live-snapshot
1444         mkdir -p "${rootmnt}/live/cow"
1445         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"
1446 }
1447
1448 check_dev ()
1449 {
1450         sysdev="${1}"
1451         devname="${2}"
1452         skip_uuid_check="${3}"
1453
1454         # support for fromiso=.../isofrom=....
1455         if [ -n "$FROMISO" ]
1456         then
1457                 mkdir /isofrom
1458                 ISO_DEVICE="$(echo $FROMISO | sed 's|\(/dev/[a-z]*[0-9]*\).*|\1|')"
1459                 mount "$ISO_DEVICE" /isofrom
1460                 ISO_NAME="$(echo $FROMISO | sed 's|/dev/[a-z]*[0-9]*/||')"
1461                 loopdevname=$(setup_loop "/isofrom/${ISO_NAME}" "loop" "/sys/block/loop*" "" '')
1462                 devname="${loopdevname}"
1463         fi
1464
1465         if [ -z "${devname}" ]
1466         then
1467                 devname=$(sys2dev "${sysdev}")
1468         fi
1469
1470         if [ -d "${devname}" ]
1471         then
1472                 mount -o bind "${devname}" $mountpoint || continue
1473
1474                 if is_live_path $mountpoint
1475                 then
1476                         echo $mountpoint
1477                         return 0
1478                 else
1479                         umount $mountpoint
1480                 fi
1481         fi
1482
1483         if [ -n "${LIVE_MEDIA_OFFSET}" ]
1484         then
1485                 loopdevname=$(setup_loop "${devname}" "loop" "/sys/block/loop*" "${LIVE_MEDIA_OFFSET}" '')
1486                 devname="${loopdevname}"
1487         fi
1488
1489         fstype=$(get_fstype "${devname}")
1490
1491         if is_supported_fs ${fstype}
1492         then
1493                 mount -t ${fstype} -o ro,noatime "${devname}" ${mountpoint} || continue
1494
1495                 if is_live_path ${mountpoint} && \
1496                         ([ "${skip_uuid_check}" ] || matches_uuid ${mountpoint})
1497                 then
1498                         echo ${mountpoint}
1499                         return 0
1500                 else
1501                         umount ${mountpoint}
1502                 fi
1503         fi
1504
1505         if [ -n "${LIVE_MEDIA_OFFSET}" ]
1506         then
1507                 losetup -d "${loopdevname}"
1508         fi
1509
1510         return 1
1511 }
1512
1513 find_livefs ()
1514 {
1515         timeout="${1}"
1516
1517         # don't start autodetection before timeout has expired
1518         if [ -n "${LIVE_MEDIA_TIMEOUT}" ]
1519         then
1520                 if [ "${timeout}" -lt "${LIVE_MEDIA_TIMEOUT}" ]
1521                 then
1522                         return 1
1523                 fi
1524         fi
1525
1526         # first look at the one specified in the command line
1527         case "${LIVE_MEDIA}" in
1528                 removable-usb)
1529                         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1530                         do
1531                                 if [ "$(cat ${sysblock}/removable)" = "1" ]
1532                                 then
1533                                         if readlink ${sysblock} | grep -q usb ||
1534                                            readlink ${sysblock}/device | grep -q usb # linux < 2.6.29
1535                                         then
1536                                                 for dev in $(subdevices "${sysblock}")
1537                                                 do
1538                                                         if check_dev "${dev}"
1539                                                         then
1540                                                                 return 0
1541                                                         fi
1542                                                 done
1543                                         fi
1544                                 fi
1545                         done
1546                         return 1
1547                         ;;
1548
1549                 removable)
1550                         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1551                         do
1552                                 if [ "$(cat ${sysblock}/removable)" = "1" ]
1553                                 then
1554                                         for dev in $(subdevices "${sysblock}")
1555                                         do
1556                                                 if check_dev "${dev}"
1557                                                 then
1558                                                         return 0
1559                                                 fi
1560                                         done
1561                                 fi
1562                         done
1563                         return 1
1564                         ;;
1565
1566                 *)
1567                         if [ ! -z "${LIVE_MEDIA}" ]
1568                         then
1569                                 if check_dev "null" "${LIVE_MEDIA}" "skip_uuid_check"
1570                                 then
1571                                         return 0
1572                                 fi
1573                         fi
1574                         ;;
1575         esac
1576
1577         # or do the scan of block devices
1578         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1579         do
1580                 devname=$(sys2dev "${sysblock}")
1581                 fstype=$(get_fstype "${devname}")
1582
1583                 if /lib/udev/cdrom_id ${devname} > /dev/null
1584                 then
1585                         if check_dev "null" "${devname}"
1586                         then
1587                                 return 0
1588                         fi
1589                 elif is_nice_device "${sysblock}"
1590                 then
1591                         for dev in $(subdevices "${sysblock}")
1592                         do
1593                                 if check_dev "${dev}"
1594                                 then
1595                                         return 0
1596                                 fi
1597                         done
1598                 elif [ "${fstype}" = "squashfs" -o \
1599                         "${fstype}" = "ext2" -o \
1600                         "${fstype}" = "ext3" -o \
1601                         "${fstype}" = "ext4" -o \
1602                         "${fstype}" = "jffs2" ]
1603                 then
1604                         # This is an ugly hack situation, the block device has
1605                         # an image directly on it.  It's hopefully
1606                         # live-initramfs, so take it and run with it.
1607                         ln -s "${devname}" "${devname}.${fstype}"
1608                         echo "${devname}.${fstype}"
1609                         return 0
1610                 fi
1611         done
1612
1613         return 1
1614 }
1615
1616 set_usplash_timeout ()
1617 {
1618         if [ -x /sbin/usplash_write ]
1619         then
1620                 /sbin/usplash_write "TIMEOUT 120"
1621         else if [ -x /sbin/splashy_update ] ; then
1622                 /sbin/splashy_update "TIMEOUT 120"
1623         fi ; fi
1624 }
1625
1626 integrity_check ()
1627 {
1628         media_mountpoint="${1}"
1629
1630         log_begin_msg "Checking media integrity"
1631
1632         cd ${media_mountpoint}
1633         /bin/md5sum -c md5sum.txt < /dev/tty8 > /dev/tty8
1634         RC="${?}"
1635
1636         log_end_msg
1637
1638         if [ "${RC}" -eq 0 ]
1639         then
1640                 log_success_msg "Everything ok, will reboot in 10 seconds."
1641                 sleep 10
1642                 cd /
1643                 umount ${media_mountpoint}
1644                 sync
1645                 echo u > /proc/sysrq-trigger
1646                 echo b > /proc/sysrq-trigger
1647         else
1648                 panic "Not ok, a media defect is likely, switch to VT8 for details."
1649         fi
1650 }
1651
1652 mountroot ()
1653 {
1654         if [ -x /scripts/local-top/cryptroot ]; then
1655             /scripts/local-top/cryptroot
1656         fi
1657
1658         exec 6>&1
1659         exec 7>&2
1660         exec > live.log
1661         exec 2>&1
1662         tail -f live.log >&7 &
1663         tailpid="${!}"
1664
1665         # Ensure 'panic' function is overridden
1666         . /scripts/live-functions
1667
1668         Arguments
1669
1670         set_usplash_timeout
1671
1672         maybe_break live-premount
1673         log_begin_msg "Running /scripts/live-premount"
1674         run_scripts /scripts/live-premount
1675         log_end_msg
1676
1677         # Needed here too because some things (*cough* udev *cough*)
1678         # changes the timeout
1679
1680         set_usplash_timeout
1681
1682         if [ ! -z "${NETBOOT}" ] || [ ! -z "${FETCH}" ] || [ ! -z "${HTTPFS}" ] || [ ! -z "${FTPFS}" ]
1683         then
1684                 if do_netmount
1685                 then
1686                         livefs_root="${mountpoint}"
1687                 else
1688                         panic "Unable to find a live file system on the network"
1689                 fi
1690         else
1691                 if [ -n "${PLAIN_ROOT}" ] && [ -n "${ROOT}" ]
1692                 then
1693                         # Do a local boot from hd
1694                         livefs_root=${ROOT}
1695                 else
1696                         # Scan local devices for the image
1697                         i=0
1698                         while [ "$i" -lt 60 ]
1699                         do
1700                                 livefs_root=$(find_livefs ${i})
1701
1702                                 if [ -n "${livefs_root}" ]
1703                                 then
1704                                         break
1705                                 fi
1706
1707                                 sleep 1
1708                                 i="$(($i + 1))"
1709                         done
1710                 fi
1711         fi
1712
1713         if [ -z "${livefs_root}" ]
1714         then
1715                 panic "Unable to find a medium containing a live file system"
1716         fi
1717
1718         if [ "${INTEGRITY_CHECK}" ]
1719         then
1720                 integrity_check "${livefs_root}"
1721         fi
1722
1723         if [ "${TORAM}" ]
1724         then
1725                 live_dest="ram"
1726         elif [ "${TODISK}" ]
1727         then
1728                 live_dest="${TODISK}"
1729         fi
1730
1731         if [ "${live_dest}" ]
1732         then
1733                 log_begin_msg "Copying live media to ${live_dest}"
1734                 copy_live_to "${livefs_root}" "${live_dest}"
1735                 log_end_msg
1736         fi
1737
1738         # if we do not unmount the ISO we can't run "fsck /dev/ice" later on
1739         # because the mountpoint is left behind in /proc/mounts, so let's get
1740         # rid of it when running from RAM
1741         if [ -n "$FROMISO" ] && [ "${TORAM}" ]
1742         then
1743           losetup -d /dev/loop0
1744           grep -q /isofrom /proc/mounts && umount /isofrom
1745         fi
1746
1747         if [ -n "${MODULETORAMFILE}" ] || [ -n "${PLAIN_ROOT}" ]
1748         then
1749                 setup_unionfs "${livefs_root}" "${rootmnt}"
1750         else
1751                 mac="$(get_mac)"
1752                 mac="$(echo ${mac} | sed 's/-//g')"
1753                 mount_images_in_directory "${livefs_root}" "${rootmnt}" "${mac}"
1754         fi
1755
1756         log_end_msg
1757
1758         # unionfs-fuse needs /dev to be bind-mounted for the duration of
1759         # live-bottom; udev's init script will take care of things after that
1760         if [ "${UNIONTYPE}" = unionfs-fuse ]
1761         then
1762                 mount -n -o bind /dev "${rootmnt}/dev"
1763         fi
1764
1765         maybe_break live-bottom
1766         log_begin_msg "Running /scripts/live-bottom\n"
1767
1768         run_scripts /scripts/live-bottom
1769         log_end_msg
1770
1771         if [ "${UNIONFS}" = unionfs-fuse ]
1772         then
1773                 umount "${rootmnt}/dev"
1774         fi
1775
1776         exec 1>&6 6>&-
1777         exec 2>&7 7>&-
1778         kill ${tailpid}
1779         [ -w "${rootmnt}/var/log/" ] && cp live.log "${rootmnt}/var/log/" 2>/dev/null
1780 }