Using noxino instead of xino, as the latter is not supported by aufs2 as we need...
[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         if [ -x /sbin/udevadm ]
766         then
767                 # lenny
768                 udevadm trigger
769                 udevadm settle
770         else
771                 # etch
772                 udevtrigger
773                 udevsettle
774         fi
775
776         ipconfig ${DEVICE} | tee /netboot.config
777
778         # source relevant ipconfig output
779         OLDHOSTNAME=${HOSTNAME}
780         . /tmp/net-${DEVICE}.conf
781         [ -z ${HOSTNAME} ] && HOSTNAME=${OLDHOSTNAME}
782         export HOSTNAME
783
784         # Check if we have a network device at all
785         if ! ls /sys/class/net/eth0 > /dev/null 2>&1 && \
786            ! ls /sys/class/net/wlan0 > /dev/null 2>&1 && \
787            ! ls /sys/class/net/ath0 > /dev/null 2>&1 && \
788            ! ls /sys/class/net/ra0 > /dev/null 2>&1
789         then
790                 panic "No supported network device found, maybe a non-mainline driver is required."
791         fi
792
793         if [ "${NFSROOT}" = "auto" ]
794         then
795                 NFSROOT=${ROOTSERVER}:${ROOTPATH}
796         fi
797
798         if ( [ -n "${FETCH}" ] || [ -n "${HTTPFS}" ] || [ -n "${FTPFS}" ] ) && do_httpmount
799         then
800                 rc=0
801                 return ${rc}
802         fi
803
804         if [ "${NFSROOT#*:}" = "${NFSROOT}" ] && [ "$NETBOOT" != "cifs" ]
805         then
806                 NFSROOT=${ROOTSERVER}:${NFSROOT}
807         fi
808
809         log_begin_msg "Trying netboot from ${NFSROOT}"
810
811         if [ "${NETBOOT}" != "nfs" ] && do_cifsmount
812         then
813                 rc=0
814         elif do_nfsmount
815         then
816                 NETBOOT="nfs"
817                 export NETBOOT
818                 rc=0
819         fi
820
821         log_end_msg
822         return ${rc}
823 }
824
825 do_httpmount ()
826 {
827         rc=1
828         dest="${mountpoint}/${LIVE_MEDIA_PATH}"
829         mount -t ramfs ram "${mountpoint}"
830         mkdir -p "${dest}"
831
832         for webfile in HTTPFS FTPFS FETCH
833         do
834                 url="$(eval echo \"\$\{${webfile}\}\")"
835                 extension="$(echo "${url}" | sed 's/\(.*\)\.\(.*\)/\2/')"
836
837                 if [ -n "$url" ]
838                 then
839                         case "${extension}" in
840                                 squashfs|tgz|tar)
841                                         if [ "${webfile}" = "FETCH" ]
842                                         then
843                                                 case "$url" in
844                                                         tftp*)
845                                                                 ip="$(dirname $url | sed -e 's|tftp://||g' -e 's|/.*$||g')"
846                                                                 rfile="$(echo $url | sed -e "s|tftp://$ip||g")"
847                                                                 lfile="$(basename $url)"
848                                                                 log_begin_msg "Trying tftp -g -b 10240 -r $rfile -l ${dest}/$lfile $ip"
849                                                                 tftp -g -b 10240 -r $rfile -l ${dest}/$lfile $ip
850                                                         ;;
851
852                                                         *)
853                                                                 log_begin_msg "Trying wget ${url} -O ${dest}/$(basename ${url})"
854                                                                 wget "${url}" -O "${dest}/$(basename ${url})"
855                                                                 ;;
856                                                 esac
857                                         else
858                                                 log_begin_msg "Trying to mount ${url} on ${dest}/$(basename ${url})"
859                                                 if [ "${webfile}" = "FTPFS" ]
860                                                 then
861                                                         FUSE_MOUNT="curlftpfs"
862                                                         url="$(dirname ${url})"
863                                                 else
864                                                         FUSE_MOUNT="httpfs"
865                                                 fi
866                                                 modprobe fuse
867                                                 $FUSE_MOUNT "${url}" "${dest}"
868                                         fi
869                                         [ ${?} -eq 0 ] && rc=0
870                                         [ "${extension}" = "tgz" ] && live_dest="ram"
871                                         break
872                                         ;;
873
874                                 *)
875                                         log_begin_msg "Unrecognized archive extension for ${url}"
876                                         ;;
877                         esac
878                 fi
879         done
880
881         if [ ${rc} != 0 ]
882         then
883             umount "${mountpoint}"
884         fi
885
886         return ${rc}
887 }
888
889 do_nfsmount ()
890 {
891         rc=1
892
893         modprobe -q nfs
894
895         if [ -z "${NFSOPTS}" ]
896         then
897                 NFSOPTS=""
898         fi
899
900         log_begin_msg "Trying nfsmount -o nolock -o ro ${NFSOPTS} ${NFSROOT} ${mountpoint}"
901
902         # FIXME: This while loop is an ugly HACK round an nfs bug
903         i=0
904         while [ "$i" -lt 60 ]
905         do
906                 nfsmount -o nolock -o ro ${NFSOPTS} "${NFSROOT}" "${mountpoint}" && rc=0 && break
907                 sleep 1
908                 i="$(($i + 1))"
909         done
910
911         return ${rc}
912 }
913
914 do_cifsmount ()
915 {
916         rc=1
917
918         if [ -x "/sbin/mount.cifs" ]
919         then
920                 if [ -z "${NFSOPTS}" ]
921                 then
922                         CIFSOPTS="-ouser=root,password="
923                 else
924                         CIFSOPTS="${NFSOPTS}"
925                 fi
926
927                 log_begin_msg "Trying mount.cifs ${NFSROOT} ${mountpoint} ${CIFSOPTS}"
928                 modprobe -q cifs
929
930                 if mount.cifs "${NFSROOT}" "${mountpoint}" "${CIFSOPTS}"
931                 then
932                         rc=0
933                 fi
934         fi
935
936         return ${rc}
937 }
938
939 do_snap_copy ()
940 {
941         fromdev="${1}"
942         todir="${2}"
943         snap_type="${3}"
944         size=$(fs_size "${fromdev}" "" "used")
945
946         if [ -b "${fromdev}" ]
947         then
948                 # look for free mem
949                 if [ -n "${HOMEMOUNTED}" -a "${snap_type}" = "HOME" ]
950                 then
951                         todev=$(awk -v pat="$(base_path ${todir})" '$2 == pat { print $1 }' /proc/mounts)
952                         freespace=$(df -k | awk '/'${todev}'/{print $4}')
953                 else
954                         freespace=$( expr $(awk '/MemFree/{print $2}' /proc/meminfo) + $( awk '/\<Cached/{print $2}' /proc/meminfo))
955                 fi
956
957                 tomount="/mnt/tmpsnap"
958
959                 if [ ! -d "${tomount}" ]
960                 then
961                         mkdir -p "${tomount}"
962                 fi
963
964                 fstype=$(get_fstype "${fromdev}")
965
966                 if [ -n "${fstype}" ]
967                 then
968                         # Copying stuff...
969                         mount -o ro -t "${fstype}" "${fromdev}" "${tomount}" || log_warning_msg "Error in mount -t ${fstype} -o ro ${fromdev} ${tomount}"
970                         cp -a "${tomount}"/* ${todir}
971                         umount "${tomount}"
972                 else
973                         log_warning_msg "Unrecognized fstype: ${fstype} on ${fromdev}:${snap_type}"
974                 fi
975
976                 rmdir "${tomount}"
977
978                 if echo ${fromdev} | grep -qs loop
979                 then
980                         losetup -d "${fromdev}"
981                 fi
982
983                 return 0
984         else
985                 return 1
986
987                 log_warning_msg "Unable to find the snapshot ${snap_type} medium"
988         fi
989 }
990
991 find_snap ()
992 {
993         # Look for ${snap_label}.* in block devices
994         snap_label="${1}"
995
996         if [ "${PERSISTENT}" != "nofiles" ]
997         then
998                 # search for image files
999                 snapdata=$(find_files "${snap_label}.squashfs ${snap_label}.cpio.gz ${snap_label}.ext2 ${snap_label}.ext3 ${snap_label}.ext4 ${snap_label}.jffs2")
1000         fi
1001
1002         if [ -z "${snapdata}" ]
1003         then
1004                 snapdata=$(find_cow_device "${snap_label}")
1005         fi
1006         echo "${snapdata}"
1007 }
1008
1009 try_snap ()
1010 {
1011         # copy the contents of previously found snapshot to ${snap_mount}
1012         # and remember the device and filename for resync on exit in live-initramfs.init
1013
1014         snapdata="${1}"
1015         snap_mount="${2}"
1016         snap_type="${3}"
1017
1018         if [ -n "${snapdata}" ] && [ ! -b "${snapdata}" ]
1019         then
1020                 log_success_msg "found snapshot: ${snapdata}"
1021                 snapdev="$(echo ${snapdata} | cut -f1 -d ' ')"
1022                 snapback="$(echo ${snapdata} | cut -f2 -d ' ')"
1023                 snapfile="$(echo ${snapdata} | cut -f3 -d ' ')"
1024
1025                 RES=""
1026                 if ! try_mount "${snapdev}" "${snapback}" "ro"
1027                 then
1028                         break
1029                 fi
1030
1031                 if echo "${snapfile}" | grep -qs '\(squashfs\|ext2\|ext3\|ext4\|jffs2\)'
1032                 then
1033                         # squashfs, jffs2 or ext2/ext3/ext4 snapshot
1034                         dev=$(get_backing_device "${snapback}/${snapfile}")
1035
1036                         do_snap_copy "${dev}" "${snap_mount}" "${snap_type}"
1037                         RES=$?
1038                 else
1039                         # cpio.gz snapshot
1040                         cd "${snap_mount}"
1041                         zcat "${snapback}/${snapfile}" | /bin/cpio --extract --preserve-modification-time --no-absolute-filenames --sparse --unconditional --make-directories > /dev/null 2>&1
1042                         RES=$?
1043                         if [ "${RES}" != "0" ]
1044                         then
1045                                 log_warning_msg "failure to \"zcat ${snapback}/${snapfile} | /bin/cpio --extract --preserve-modification-time --no-absolute-filenames --sparse --unconditional --make-directories\""
1046                         fi
1047                         cd "${OLDPWD}"
1048                 fi
1049
1050                 umount "${snapback}" ||  log_warning_msg "failure to \"umount ${snapback}\""
1051
1052                 if [ "${RES}" != "0" ]
1053                 then
1054                         log_warning_msg "Impossible to include the ${snapfile} Snapshot file"
1055                 fi
1056
1057         elif [ -b "${snapdata}" ]
1058         then
1059                 # Try to find if it could be a snapshot partition
1060                 dev="${snapdata}"
1061                 log_success_msg "found snapshot ${snap_type} device on ${dev}"
1062                 if echo "${dev}" | grep -qs loop
1063                 then
1064                         # strange things happens, user confused?
1065                         snaploop=$( losetup ${dev} | awk '{print $3}' | tr -d '()' )
1066                         snapfile=$(basename ${snaploop})
1067                         snapdev=$(awk -v pat="$( dirname ${snaploop})" '$2 == pat { print $1 }' /proc/mounts)
1068                 else
1069                         snapdev="${dev}"
1070                 fi
1071
1072                 if ! do_snap_copy "${dev}" "${snap_mount}" "${snap_type}"
1073                 then
1074                         log_warning_msg "Impossible to include the ${snap_type} Snapshot"
1075                         return 1
1076                 else
1077                         if [ -n "${snapfile}" ]
1078                         then
1079                                 # it was a loop device, user confused
1080                                 umount ${snapdev}
1081                         fi
1082                 fi
1083         else
1084                 log_warning_msg "Impossible to include the ${snap_type} Snapshot"
1085                 return 1
1086         fi
1087
1088         echo "export ${snap_type}SNAP="${snap_mount}":${snapdev}:${snapfile}" >> /etc/live.conf # for resync on reboot/halt
1089         return 0
1090 }
1091
1092 setup_unionfs ()
1093 {
1094         image_directory="${1}"
1095         rootmnt="${2}"
1096         addimage_directory="${3}"
1097         modprobe -q -b ${UNIONTYPE}
1098
1099         # run-init can't deal with images in a subdir, but we're going to
1100         # move all of these away before it runs anyway.  No, we're not,
1101         # put them in / since move-mounting them into / breaks mono and
1102         # some other apps.
1103
1104         croot="/"
1105
1106         # Let's just mount the read-only file systems first
1107         rofsstring=""
1108         rofslist=""
1109
1110         if [ "${UNIONTYPE}" = "aufs" ]
1111         then
1112                 roopt="rr,noxino"
1113         else
1114                 roopt="ro"
1115         fi
1116
1117         if [ -z "${PLAIN_ROOT}" ]
1118         then
1119                 # Read image names from ${MODULE}.module if it exists
1120                 if [ -e "${image_directory}/filesystem.${MODULE}.module" ]
1121                 then
1122                         for IMAGE in $(cat ${image_directory}/filesystem.${MODULE}.module)
1123                         do
1124                                 image_string="${image_string} ${image_directory}/${IMAGE}"
1125                         done
1126                 elif [ -e "${image_directory}/${MODULE}.module" ]
1127                 then
1128                         for IMAGE in $(cat ${image_directory}/${MODULE}.module)
1129                         do
1130                                 image_string="${image_string} ${image_directory}/${IMAGE}"
1131                         done
1132                 else
1133                         # ${MODULE}.module does not exist, create a list of images
1134                         for FILESYSTEM in squashfs ext2 ext3 ext4 xfs jffs2 dir
1135                         do
1136                                 for IMAGE in "${image_directory}"/*."${FILESYSTEM}"
1137                                 do
1138                                         if [ -e "${IMAGE}" ]
1139                                         then
1140                                                 image_string="${image_string} ${IMAGE}"
1141                                         fi
1142                                 done
1143                         done
1144
1145                         if [ -n "${addimage_directory}" ] && [ -d "${addimage_directory}" ]
1146                         then
1147                                 for FILESYSTEM in squashfs ext2 ext3 ext4 xfs jffs2 dir
1148                                 do
1149                                         for IMAGE in "${addimage_directory}"/*."${FILESYSTEM}"
1150                                         do
1151                                                 if [ -e "${IMAGE}" ]
1152                                                 then
1153                                                         image_string="${image_string} ${IMAGE}"
1154                                                 fi
1155                                         done
1156                                 done
1157                         fi
1158
1159                         # Now sort the list
1160                         image_string="$(echo ${image_string} | sed -e 's/ /\n/g' | sort )"
1161                 fi
1162
1163         [ -n "${MODULETORAMFILE}" ] && image_string="${image_directory}/$(basename ${MODULETORAMFILE})"
1164
1165                 mkdir -p "${croot}"
1166
1167                 for image in ${image_string}
1168                 do
1169                         imagename=$(basename "${image}")
1170
1171                         export image devname
1172                         maybe_break live-realpremount
1173                         log_begin_msg "Running /scripts/live-realpremount"
1174                         run_scripts /scripts/live-realpremount
1175                         log_end_msg
1176
1177                         if [ -d "${image}" ]
1178                         then
1179                                 # it is a plain directory: do nothing
1180                                 rofsstring="${image}=${roopt}:${rofsstring}"
1181                                 rofslist="${image} ${rofslist}"
1182                         elif [ -f "${image}" ]
1183                         then
1184                                 if losetup --help 2>&1 | grep -q -- "-r\b"
1185                                 then
1186                                         backdev=$(get_backing_device "${image}" "-r")
1187                                 else
1188                                         backdev=$(get_backing_device "${image}")
1189                                 fi
1190                                 fstype=$(get_fstype "${backdev}")
1191
1192                                 if [ "${fstype}" = "unknown" ]
1193                                 then
1194                                         panic "Unknown file system type on ${backdev} (${image})"
1195                                 fi
1196
1197                                 if [ -z "${fstype}" ]
1198                                 then
1199                                         fstype="${imagename##*.}"
1200                                         log_warning_msg "Unknown file system type on ${backdev} (${image}), assuming ${fstype}."
1201                                 fi
1202
1203                                 mkdir -p "${croot}/${imagename}"
1204                                 log_begin_msg "Mounting \"${image}\" on \"${croot}${imagename}\" via \"${backdev}\""
1205                                 mount -t "${fstype}" -o ro,noatime "${backdev}" "${croot}/${imagename}" || panic "Can not mount ${backdev} (${image}) on ${croot}/${imagename}" && rofsstring="${croot}/${imagename}=${roopt}:${rofsstring}" && rofslist="${croot}/${imagename} ${rofslist}"
1206                                 log_end_msg
1207                         fi
1208                 done
1209         else
1210                 # we have a plain root system
1211                 mkdir -p "${croot}/filesystem"
1212                 log_begin_msg "Mounting \"${image_directory}\" on \"${croot}/filesystem\""
1213                 mount -t $(get_fstype "${image_directory}") -o ro,noatime "${image_directory}" "${croot}/filesystem" || panic "Can not mount ${image_directory} on ${croot}/filesystem" && rofsstring="${croot}/filesystem=${roopt}:${rofsstring}" && rofslist="${croot}/filesystem ${rofslist}"
1214                 # probably broken:
1215                 mount -o bind ${croot}/filesystem $mountpoint
1216                 log_end_msg
1217         fi
1218
1219         rofsstring=${rofsstring%:}
1220
1221         mkdir -p /cow
1222
1223         # Looking for "${root_persistence}" device or file
1224         if [ -n "${PERSISTENT}" ] && [ -z "${NOPERSISTENT}" ]
1225         then
1226                 if [ -z "${QUICKUSBMODULES}" ]
1227                 then
1228                         # Load USB modules
1229                         num_block=$(ls -l /sys/block | wc -l)
1230                         for module in sd_mod uhci-hcd ehci-hcd ohci-hcd usb-storage
1231                         do
1232                                 modprobe -q -b ${module}
1233                         done
1234
1235                         if [ -x /sbin/udevadm ]
1236                         then
1237                                 # lenny
1238                                 udevadm trigger
1239                                 udevadm settle
1240                         else
1241                                 # etch
1242                                 udevtrigger
1243                                 udevsettle
1244                         fi
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                         mount -t ${UNIONTYPE} \
1361                                 -o rw,noatime,dirs=/cow${dir}=rw:${exposedrootfs}${dir}=ro \
1362                                 ${UNIONTYPE} "${rootmnt}${dir}" || \
1363                                 panic "mount ${UNIONTYPE} on ${rootmnt}${dir} failed with option \
1364                                         rw,noatime,dirs=/cow${dir}=rw:${exposedrootfs}${dir}=ro"
1365                 done
1366         else
1367                 mount -t ${UNIONTYPE} -o noatime,dirs=/cow=rw:${rofsstring} \
1368                         ${UNIONTYPE} "${rootmnt}" || panic "mount ${UNIONTYPE} on \
1369                         ${rootmnt} failed with option noatime,dirs=/cow=rw:${rofsstring}"
1370         fi
1371
1372         # Correct the permissions of /:
1373         chmod 0755 "${rootmnt}"
1374
1375         # tmpfs file systems
1376         touch /etc/fstab
1377         mkdir -p "${rootmnt}/live"
1378         mount -t tmpfs tmpfs ${rootmnt}/live
1379
1380         # Adding other custom mounts
1381         if [ -n "${PERSISTENT}" ] && [ -z "${NOPERSISTENT}" ]
1382         then
1383                 # directly mount /home
1384                 # FIXME: add a custom mounts configurable system
1385
1386                 if [ -b "${homecow}" ]
1387                 then
1388                         mount -t $(get_fstype "${homecow}") -o rw,noatime "${homecow}" "${rootmnt}/home"
1389                         export HOMEMOUNTED=1 # used to proper calculate free space in do_snap_copy()
1390                 else
1391                         log_warning_msg "Unable to find the persistent home medium"
1392                 fi
1393
1394                 # Look for other snapshots to copy in
1395                 try_snap "${root_snapdata}" "${rootmnt}" "ROOT"
1396                 # This second type should be removed when snapshot grow smarter
1397                 try_snap "${home_snapdata}" "${rootmnt}/home" "HOME"
1398         fi
1399
1400         if [ -n "${SHOWMOUNTS}" ]
1401         then
1402                 for d in ${rofslist}
1403                 do
1404                         mkdir -p "${rootmnt}/live/${d##*/}"
1405
1406                         case d in
1407                                 *.dir)
1408                                         # do nothing # mount -o bind "${d}" "${rootmnt}/live/${d##*/}"
1409                                         ;;
1410
1411                                 *)
1412                                         mount -o move "${d}" "${rootmnt}/live/${d##*/}"
1413                                         ;;
1414                         esac
1415                 done
1416         fi
1417
1418         # shows cow fs on /cow for use by live-snapshot
1419         mkdir -p "${rootmnt}/live/cow"
1420         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"
1421 }
1422
1423 check_dev ()
1424 {
1425         sysdev="${1}"
1426         devname="${2}"
1427         skip_uuid_check="${3}"
1428
1429         # support for fromiso=.../isofrom=....
1430         if [ -n "$FROMISO" ]
1431         then
1432                 mkdir /isofrom
1433                 ISO_DEVICE="$(echo $FROMISO | sed 's|\(/dev/[a-z]*[0-9]*\).*|\1|')"
1434                 mount "$ISO_DEVICE" /isofrom
1435                 ISO_NAME="$(echo $FROMISO | sed 's|/dev/[a-z]*[0-9]*/||')"
1436                 loopdevname=$(setup_loop "/isofrom/${ISO_NAME}" "loop" "/sys/block/loop*" "" '')
1437                 devname="${loopdevname}"
1438         fi
1439
1440         if [ -z "${devname}" ]
1441         then
1442                 devname=$(sys2dev "${sysdev}")
1443         fi
1444
1445         if [ -d "${devname}" ]
1446         then
1447                 mount -o bind "${devname}" $mountpoint || continue
1448
1449                 if is_live_path $mountpoint
1450                 then
1451                         echo $mountpoint
1452                         return 0
1453                 else
1454                         umount $mountpoint
1455                 fi
1456         fi
1457
1458         if [ -n "${LIVE_MEDIA_OFFSET}" ]
1459         then
1460                 loopdevname=$(setup_loop "${devname}" "loop" "/sys/block/loop*" "${LIVE_MEDIA_OFFSET}" '')
1461                 devname="${loopdevname}"
1462         fi
1463
1464         fstype=$(get_fstype "${devname}")
1465
1466         if is_supported_fs ${fstype}
1467         then
1468                 mount -t ${fstype} -o ro,noatime "${devname}" ${mountpoint} || continue
1469
1470                 if is_live_path ${mountpoint} && \
1471                         ([ "${skip_uuid_check}" ] || matches_uuid ${mountpoint})
1472                 then
1473                         echo ${mountpoint}
1474                         return 0
1475                 else
1476                         umount ${mountpoint}
1477                 fi
1478         fi
1479
1480         if [ -n "${LIVE_MEDIA_OFFSET}" ]
1481         then
1482                 losetup -d "${loopdevname}"
1483         fi
1484
1485         return 1
1486 }
1487
1488 find_livefs ()
1489 {
1490         timeout="${1}"
1491
1492         # don't start autodetection before timeout has expired
1493         if [ -n "${LIVE_MEDIA_TIMEOUT}" ]
1494         then
1495                 if [ "${timeout}" -lt "${LIVE_MEDIA_TIMEOUT}" ]
1496                 then
1497                         return 1
1498                 fi
1499         fi
1500
1501         # first look at the one specified in the command line
1502         case "${LIVE_MEDIA}" in
1503                 removable-usb)
1504                         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1505                         do
1506                                 if [ "$(cat ${sysblock}/removable)" = "1" ]
1507                                 then
1508                                         if readlink ${sysblock} | grep -q usb ||
1509                                            readlink ${sysblock}/device | grep -q usb # linux < 2.6.29
1510                                         then
1511                                                 for dev in $(subdevices "${sysblock}")
1512                                                 do
1513                                                         if check_dev "${dev}"
1514                                                         then
1515                                                                 return 0
1516                                                         fi
1517                                                 done
1518                                         fi
1519                                 fi
1520                         done
1521                         return 1
1522                         ;;
1523
1524                 removable)
1525                         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1526                         do
1527                                 if [ "$(cat ${sysblock}/removable)" = "1" ]
1528                                 then
1529                                         for dev in $(subdevices "${sysblock}")
1530                                         do
1531                                                 if check_dev "${dev}"
1532                                                 then
1533                                                         return 0
1534                                                 fi
1535                                         done
1536                                 fi
1537                         done
1538                         return 1
1539                         ;;
1540
1541                 *)
1542                         if [ ! -z "${LIVE_MEDIA}" ]
1543                         then
1544                                 if check_dev "null" "${LIVE_MEDIA}" "skip_uuid_check"
1545                                 then
1546                                         return 0
1547                                 fi
1548                         fi
1549                         ;;
1550         esac
1551
1552         # or do the scan of block devices
1553         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1554         do
1555                 devname=$(sys2dev "${sysblock}")
1556                 fstype=$(get_fstype "${devname}")
1557
1558                 if /lib/udev/cdrom_id ${devname} > /dev/null
1559                 then
1560                         if check_dev "null" "${devname}"
1561                         then
1562                                 return 0
1563                         fi
1564                 elif is_nice_device "${sysblock}"
1565                 then
1566                         for dev in $(subdevices "${sysblock}")
1567                         do
1568                                 if check_dev "${dev}"
1569                                 then
1570                                         return 0
1571                                 fi
1572                         done
1573                 elif [ "${fstype}" = "squashfs" -o \
1574                         "${fstype}" = "ext2" -o \
1575                         "${fstype}" = "ext3" -o \
1576                         "${fstype}" = "ext4" -o \
1577                         "${fstype}" = "jffs2" ]
1578                 then
1579                         # This is an ugly hack situation, the block device has
1580                         # an image directly on it.  It's hopefully
1581                         # live-initramfs, so take it and run with it.
1582                         ln -s "${devname}" "${devname}.${fstype}"
1583                         echo "${devname}.${fstype}"
1584                         return 0
1585                 fi
1586         done
1587
1588         return 1
1589 }
1590
1591 set_usplash_timeout ()
1592 {
1593         if [ -x /sbin/usplash_write ]
1594         then
1595                 /sbin/usplash_write "TIMEOUT 120"
1596         else if [ -x /sbin/splashy_update ] ; then
1597                 /sbin/splashy_update "TIMEOUT 120"
1598         fi ; fi
1599 }
1600
1601 integrity_check ()
1602 {
1603         media_mountpoint="${1}"
1604
1605         log_begin_msg "Checking media integrity"
1606
1607         cd ${media_mountpoint}
1608         /bin/md5sum -c md5sum.txt < /dev/tty8 > /dev/tty8
1609         RC="${?}"
1610
1611         log_end_msg
1612
1613         if [ "${RC}" -eq 0 ]
1614         then
1615                 log_success_msg "Everything ok, will reboot in 10 seconds."
1616                 sleep 10
1617                 cd /
1618                 umount ${media_mountpoint}
1619                 sync
1620                 echo u > /proc/sysrq-trigger
1621                 echo b > /proc/sysrq-trigger
1622         else
1623                 panic "Not ok, a media defect is likely, switch to VT8 for details."
1624         fi
1625 }
1626
1627 mountroot ()
1628 {
1629         if [ -x /scripts/local-top/cryptroot ]; then
1630             /scripts/local-top/cryptroot
1631         fi
1632
1633         exec 6>&1
1634         exec 7>&2
1635         exec > live.log
1636         exec 2>&1
1637         tail -f live.log >&7 &
1638         tailpid="${!}"
1639
1640         # Ensure 'panic' function is overridden
1641         . /scripts/live-functions
1642
1643         Arguments
1644
1645         set_usplash_timeout
1646
1647         maybe_break live-premount
1648         log_begin_msg "Running /scripts/live-premount"
1649         run_scripts /scripts/live-premount
1650         log_end_msg
1651
1652         # Needed here too because some things (*cough* udev *cough*)
1653         # changes the timeout
1654
1655         set_usplash_timeout
1656
1657         if [ ! -z "${NETBOOT}" ] || [ ! -z "${FETCH}" ] || [ ! -z "${HTTPFS}" ] || [ ! -z "${FTPFS}" ]
1658         then
1659                 if do_netmount
1660                 then
1661                         livefs_root="${mountpoint}"
1662                 else
1663                         panic "Unable to find a live file system on the network"
1664                 fi
1665         else
1666                 if [ -n "${PLAIN_ROOT}" ] && [ -n "${ROOT}" ]
1667                 then
1668                         # Do a local boot from hd
1669                         livefs_root=${ROOT}
1670                 else
1671                         # Scan local devices for the image
1672                         i=0
1673                         while [ "$i" -lt 60 ]
1674                         do
1675                                 livefs_root=$(find_livefs ${i})
1676
1677                                 if [ -n "${livefs_root}" ]
1678                                 then
1679                                         break
1680                                 fi
1681
1682                                 sleep 1
1683                                 i="$(($i + 1))"
1684                         done
1685                 fi
1686         fi
1687
1688         if [ -z "${livefs_root}" ]
1689         then
1690                 panic "Unable to find a medium containing a live file system"
1691         fi
1692
1693         if [ "${INTEGRITY_CHECK}" ]
1694         then
1695                 integrity_check "${livefs_root}"
1696         fi
1697
1698         if [ "${TORAM}" ]
1699         then
1700                 live_dest="ram"
1701         elif [ "${TODISK}" ]
1702         then
1703                 live_dest="${TODISK}"
1704         fi
1705
1706         if [ "${live_dest}" ]
1707         then
1708                 log_begin_msg "Copying live media to ${live_dest}"
1709                 copy_live_to "${livefs_root}" "${live_dest}"
1710                 log_end_msg
1711         fi
1712
1713         # if we do not unmount the ISO we can't run "fsck /dev/ice" later on
1714         # because the mountpoint is left behind in /proc/mounts, so let's get
1715         # rid of it when running from RAM
1716         if [ -n "$FROMISO" ] && [ "${TORAM}" ]
1717         then
1718           losetup -d /dev/loop0
1719           grep -q /isofrom /proc/mounts && umount /isofrom
1720         fi
1721
1722         if [ -n "${MODULETORAMFILE}" ] || [ -n "${PLAIN_ROOT}" ]
1723         then
1724                 setup_unionfs "${livefs_root}" "${rootmnt}"
1725         else
1726                 mac="$(get_mac)"
1727                 mac="$(echo ${mac} | sed 's/-//g')"
1728                 mount_images_in_directory "${livefs_root}" "${rootmnt}" "${mac}"
1729         fi
1730
1731         log_end_msg
1732
1733         maybe_break live-bottom
1734         log_begin_msg "Running /scripts/live-bottom\n"
1735
1736         run_scripts /scripts/live-bottom
1737         log_end_msg
1738
1739         exec 1>&6 6>&-
1740         exec 2>&7 7>&-
1741         kill ${tailpid}
1742         [ -w "${rootmnt}/var/log/" ] && cp live.log "${rootmnt}/var/log/" 2>/dev/null
1743 }