Adding debian version 2.0~a1-1.
[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 tried="/tmp/tried"
24
25 # Create /etc/mtab for debug purpose and future syncs
26 if [ ! -d /etc ]
27 then
28         mkdir /etc/
29 fi
30
31 if [ ! -f /etc/mtab ]
32 then
33         touch /etc/mtab
34 fi
35
36 [ -f /etc/live.conf ] && . /etc/live.conf
37 export USERNAME USERFULLNAME HOSTNAME
38
39 . /scripts/live-helpers
40
41 if [ ! -f /live.vars ]
42 then
43         touch /live.vars
44 fi
45
46 Arguments ()
47 {
48         PRESEEDS=""
49         LOCATIONS=""
50
51         for ARGUMENT in $(cat /proc/cmdline)
52         do
53                 case "${ARGUMENT}" in
54                         skipconfig)
55                                 NOACCESSIBILITY="Yes"
56                                 NOAPTCDROM="Yes"
57                                 NOAUTOLOGIN="Yes"
58                                 NOCONSOLEKEYBOARD="Yes"
59                                 NOFASTBOOT="Yes"
60                                 NOFSTAB="Yes"
61                                 NOHOSTS="Yes"
62                                 NOLOCALES="Yes"
63                                 NONETWORKING="Yes"
64                                 NOTIMEZONE="Yes"
65                                 NOXAUTOCONFIG="Yes"
66                                 NOXAUTOLOGIN="Yes"
67
68                                 export NOACCESSIBILITY NOAPTCDROM NOAUTOLOGIN NOCONSOLEKEYBOARD NOFASTBOOT NOFSTAB NOHOSTS NOLOCALES NONETWORKING NOTIMEZONE NOXAUTOCONFIG NOXAUTOLOGIN
69                                 ;;
70
71                         access=*)
72                                 ACCESS="${ARGUMENT#access=}"
73                                 export ACCESS
74                                 ;;
75
76                         console=*)
77                                 DEFCONSOLE="${ARGUMENT#*=}"
78                                 export DEFCONSOLE
79                                 ;;
80
81                         debug)
82                                 DEBUG="Yes"
83                                 export DEBUG
84
85                                 set -x
86                                 ;;
87
88                         ethdevice=*)
89                                 DEVICE="${ARGUMENT#ethdevice=}"
90                                 export DEVICE
91                                 ;;
92
93                         ethdevice=*)
94                                 ETHDEVICE="${ARGUMENT#ethdevice=}"
95                                 export ETHDEVICE
96                                 ;;
97
98                         ethdevice-timeout=*)
99                                 ETHDEV_TIMEOUT="${ARGUMENT#ethdevice-timeout=}"
100                                 export ETHDEV_TIMEOUT
101                                 ;;
102
103                         fetch=*)
104                                 FETCH="${ARGUMENT#fetch=}"
105                                 export FETCH
106                                 ;;
107
108                         forcepersistentfsck)
109                                 FORCEPERSISTENTFSCK="Yes"
110                                 export FORCEPERSISTENTFSCK
111                                 ;;
112
113                         ftpfs=*)
114                                 FTPFS="${ARGUMENT#ftpfs=}"
115                                 export FTPFS
116                                 ;;
117
118                         httpfs=*)
119                                 HTTPFS="${ARGUMENT#httpfs=}"
120                                 export HTTPFS
121                                 ;;
122
123                         isofrom=*|fromiso=*)
124                                 FROMISO="${ARGUMENT#*=}"
125                                 export FROMISO
126                                 ;;
127
128                         username=*)
129                                 USERNAME="${ARGUMENT#username=}"
130                                 LIVECONF="changed"
131                                 export USERNAME LIVECONF
132                                 ;;
133
134                         userfullname=*)
135                                 USERFULLNAME="${ARGUMENT#userfullname=}"
136                                 LIVECONF="changed"
137                                 export USERFULLNAME LIVECONF
138                                 ;;
139
140                         ignore_uuid)
141                                 IGNORE_UUID="Yes"
142                                 export IGNORE_UUID
143                                 ;;
144
145                         integrity-check)
146                                 INTEGRITY_CHECK="Yes"
147                                 export INTEGRITY_CHECK
148                                 ;;
149
150                         ip=*)
151                                 STATICIP="${ARGUMENT#ip=}"
152
153                                 if [ -z "${STATICIP}" ]
154                                 then
155                                         STATICIP="frommedia"
156                                 fi
157
158                                 export STATICIP
159                                 ;;
160
161                         keyb=*|kbd-chooser/method=*)
162                                 KBD="${ARGUMENT#*=}"
163                                 export KBD
164                                 ;;
165
166                         klayout=*|console-setup/layoutcode=*)
167                                 KLAYOUT="${ARGUMENT#*=}"
168                                 export KLAYOUT
169                                 ;;
170
171                         kvariant=*|console-setup/variantcode=*)
172                                 KVARIANT="${ARGUMENT#*=}"
173                                 export KVARIANT
174                                 ;;
175
176                         kmodel=*|console-setup/modelcode=*)
177                                 KMODEL="${ARGUMENT#*=}"
178                                 export KMODEL
179                                 ;;
180
181                         koptions=*)
182                                 KOPTIONS="${ARGUMENT#koptions=}"
183                                 export KOPTIONS
184                                 ;;
185
186                         live-getty)
187                                 LIVE_GETTY="1"
188                                 export LIVE_GETTY
189                                 ;;
190
191                         live-media=*|bootfrom=*)
192                                 LIVE_MEDIA="${ARGUMENT#*=}"
193                                 export LIVE_MEDIA
194                                 ;;
195
196                         live-media-encryption=*|encryption=*)
197                                 LIVE_MEDIA_ENCRYPTION="${ARGUMENT#*=}"
198                                 export LIVE_MEDIA_ENCRYPTION
199                                 ;;
200
201                         live-media-offset=*)
202                                 LIVE_MEDIA_OFFSET="${ARGUMENT#live-media-offset=}"
203                                 export LIVE_MEDIA_OFFSET
204                                 ;;
205
206                         live-media-path=*)
207                                 LIVE_MEDIA_PATH="${ARGUMENT#live-media-path=}"
208                                 export LIVE_MEDIA_PATH
209                                 ;;
210
211                         live-media-timeout=*)
212                                 LIVE_MEDIA_TIMEOUT="${ARGUMENT#live-media-timeout=}"
213                                 export LIVE_MEDIA_TIMEOUT
214                                 ;;
215
216                         language=*|debian-installer/language=*)
217                                 language=${x#debian-installer/language=}
218                                 locale="$(lang2locale "$language")"
219                                 set_locale="true"
220                                 ;;
221
222                         locale=*|debian-installer/locale=*)
223                                 LOCALE="${ARGUMENT#*=}"
224                                 export LOCALE
225                                 ;;
226
227                         module=*)
228                                 MODULE="${ARGUMENT#module=}"
229                                 export MODULE
230                                 ;;
231
232                         netboot=*)
233                                 NETBOOT="${ARGUMENT#netboot=}"
234                                 export NETBOOT
235                                 ;;
236
237                         nfsopts=*)
238                                 NFSOPTS="${ARGUMENT#nfsopts=}"
239                                 export NFSOPTS
240                                 ;;
241
242                         nfscow=*)
243                                 NFS_COW="${ARGUMENT#nfscow=}"
244                                 export NFS_COW
245                                 ;;
246
247                         noaccessibility)
248                                 NOACCESSIBILITY="Yes"
249                                 export NOACCESSIBILITY
250                                 ;;
251
252                         noaptcdrom)
253                                 NOAPTCDROM="Yes"
254                                 export NOAPTCDROM
255                                 ;;
256
257                         noautologin)
258                                 NOAUTOLOGIN="Yes"
259                                 export NOAUTOLOGIN
260                                 ;;
261
262                         noxautologin)
263                                 NOXAUTOLOGIN="Yes"
264                                 export NOXAUTOLOGIN
265                                 ;;
266
267                         noconsolekeyboard)
268                                 NOCONSOLEKEYBOARD="Yes"
269                                 export NOCONSOLEKEYBOARD
270                                 ;;
271
272                         nofastboot)
273                                 NOFASTBOOT="Yes"
274                                 export NOFASTBOOT
275                                 ;;
276
277                         nofstab)
278                                 NOFSTAB="Yes"
279                                 export NOFSTAB
280                                 ;;
281
282                         nohosts)
283                                 NOHOSTS="Yes"
284                                 export NOHOSTS
285                                 ;;
286
287                         nolocales)
288                                 NOLOCALES="Yes"
289                                 export NOLOCALES
290                                 ;;
291
292                         nonetworking)
293                                 NONETWORKING="Yes"
294                                 export NONETWORKING
295                                 ;;
296
297                         swapon)
298                                 SWAPON="Yes"
299                                 export SWAPON
300                                 ;;
301
302                         noxautoconfig)
303                                 NOXAUTOCONFIG="Yes"
304                                 export NOXAUTOCONFIG
305                                 ;;
306
307                         noxscreensaver)
308                                 NOXSCREENSAVER="Yes"
309                                 export NOXSCREENSAVER
310                                 ;;
311
312                         persistent)
313                                 PERSISTENT="Yes"
314                                 export PERSISTENT
315                                 ;;
316
317                         persistent=*)
318                                 PERSISTENT="${ARGUMENT#persistent=}"
319                                 if [ -z "${PERSISTENT}" ]
320                                 then
321                                         PERSISTENT="Yes"
322                                 fi
323                                 export PERSISTENT
324                                 ;;
325
326                         persistent-path=*)
327                                 PERSISTENT_PATH="${ARGUMENT#persistent-path=}"
328                                 export PERSISTENT_PATH
329                                 ;;
330
331                         persistent-subtext=*)
332                                 root_persistence="${root_persistence}-${ARGUMENT#persistent-subtext=}"
333                                 home_persistence="${home_persistence}-${ARGUMENT#persistent-subtext=}"
334                                 root_snapshot_label="${root_snapshot_label}-${ARGUMENT#persistent-subtext=}"
335                                 home_snapshot_label="${home_snapshot_label}-${ARGUMENT#persistent-subtext=}"
336                                 ;;
337
338                         nopersistent)
339                                 NOPERSISTENT="Yes"
340                                 export NOPERSISTENT
341                                 ;;
342
343                         quickusbmodules)
344                                 QUICKUSBMODULES="Yes"
345                                 export QUICKUSBMODULES
346                                 ;;
347
348                         preseed/file=*|file=*)
349                                 LOCATIONS="${ARGUMENT#*=} ${LOCATIONS}"
350                                 export LOCATIONS
351                                 ;;
352
353                         nopreseed)
354                                 NOPRESEED="Yes"
355                                 export NOPRESEED
356                                 ;;
357
358                         url=*)
359                                 URL_LOCATION="${ARGUMENT#url=}"
360
361                                 mount -o bind /sys /root/sys
362                                 mount -o bind /proc /root/proc
363                                 mount -o bind /dev /root/dev
364
365                                 mkdir -p /root/var/run/network
366                                 [ "${NETBOOT}" ] || chroot /root dhclient eth0
367                                 chroot /root wget -P /tmp "${URL_LOCATION}"
368                                 [ "${NETBOOT}" ] || chroot /root ifconfig eth0 down
369
370                                 umount /root/sys
371                                 umount /root/proc
372                                 umount /root/dev
373
374                                 LOCATIONS="/tmp/$(basename ${URL_LOCATION}) ${LOCATIONS}"
375                                 ;;
376
377                         */*=*)
378                                 question="${ARGUMENT%%=*}"
379                                 value="${ARGUMENT#*=}"
380                                 PRESEEDS="${PRESEEDS}\"${question}=${value}\" "
381                                 export PRESEEDS
382                                 ;;
383
384                         showmounts)
385                                 SHOWMOUNTS="Yes"
386                                 export SHOWMOUNTS
387                                 ;;
388
389                         silent)
390                                 SILENT="Yes"
391                                 export SILENT
392                                 ;;
393
394                         textonly)
395                                 TEXTONLY="Yes"
396                                 export TEXTONLY
397                                 ;;
398
399                         timezone=*)
400                                 TIMEZONE="${ARGUMENT#timezone=}"
401                                 export TIMEZONE
402                                 ;;
403
404                         notimezone)
405                                 NOTIMEZONE="Yes"
406                                 export NOTIMEZONE
407                                 ;;
408
409                         todisk=*)
410                                 TODISK="${ARGUMENT#todisk=}"
411                                 export TODISK
412                                 ;;
413
414                         toram)
415                                 TORAM="Yes"
416                                 export TORAM
417                                 ;;
418
419                         toram=*)
420                                 TORAM="Yes"
421                                 MODULETORAM="${ARGUMENT#toram=}"
422                                 export TORAM MODULETORAM
423                                 ;;
424
425                         exposedroot)
426                                 EXPOSED_ROOT="Yes"
427                                 export EXPOSED_ROOT
428                                 ;;
429
430                         plainroot)
431                                 PLAIN_ROOT="Yes"
432                                 export PLAIN_ROOT
433                                 ;;
434
435                         skipunion)
436                                 SKIP_UNION_MOUNTS="Yes"
437                                 export SKIP_UNION_MOUNTS
438                                 ;;
439
440                         root=*)
441                                 ROOT="${ARGUMENT#root=}"
442                                 export ROOT
443                                 ;;
444
445                         union=*)
446                                 UNIONTYPE="${ARGUMENT#union=}"
447                                 export UNIONTYPE
448                                 ;;
449
450                         xdebconf)
451                                 XDEBCONF="Yes"
452                                 export XDEBCONF
453                                 ;;
454
455                         xdriver=*)
456                                 XDRIVER="${ARGUMENT#xdriver=}"
457                                 export XDRIVER
458                                 ;;
459
460                         xvideomode=*)
461                                 XVIDEOMODE="${ARGUMENT#xvideomode=}"
462                                 export XVIDEOMODE
463                                 ;;
464                 esac
465         done
466
467         # sort of compatibility with netboot.h from linux docs
468         if [ -z "${NETBOOT}" ]
469         then
470                 if [ "${ROOT}" = "/dev/nfs" ]
471                 then
472                         NETBOOT="nfs"
473                         export NETBOOT
474                 elif [ "${ROOT}" = "/dev/cifs" ]
475                 then
476                         NETBOOT="cifs"
477                         export NETBOOT
478                 fi
479         fi
480
481         if [ -z "${MODULE}" ]
482         then
483                 MODULE="filesystem"
484                 export MODULE
485         fi
486
487         if [ -z "${UNIONTYPE}" ]
488         then
489                 UNIONTYPE="aufs"
490                 export UNIONTYPE
491         fi
492 }
493
494 is_live_path ()
495 {
496         DIRECTORY="${1}"
497
498         if [ -d "${DIRECTORY}"/"${LIVE_MEDIA_PATH}" ]
499         then
500                 for FILESYSTEM in squashfs ext2 ext3 ext4 xfs dir jffs2
501                 do
502                         if [ "$(echo ${DIRECTORY}/${LIVE_MEDIA_PATH}/*.${FILESYSTEM})" != "${DIRECTORY}/${LIVE_MEDIA_PATH}/*.${FILESYSTEM}" ]
503                         then
504                                 return 0
505                         fi
506                 done
507         fi
508
509         return 1
510 }
511
512 matches_uuid ()
513 {
514         if [ "${IGNORE_UUID}" ] || [ ! -e /conf/uuid.conf ]
515         then
516                 return 0
517         fi
518
519         path="${1}"
520         uuid="$(cat /conf/uuid.conf)"
521
522         for try_uuid_file in "${path}/.disk/live-uuid"*
523         do
524                 [ -e "${try_uuid_file}" ] || continue
525
526                 try_uuid="$(cat "${try_uuid_file}")"
527
528                 if [ "${uuid}" = "${try_uuid}" ]
529                 then
530                         return 0
531                 fi
532         done
533
534         return 1
535 }
536
537 get_backing_device ()
538 {
539         case "${1}" in
540                 *.squashfs|*.ext2|*.ext3|*.ext4|*.jffs2)
541                         echo $(setup_loop "${1}" "loop" "/sys/block/loop*" '0' "${LIVE_MEDIA_ENCRYPTION}" "${2}")
542                         ;;
543
544                 *.dir)
545                         echo "directory"
546                         ;;
547
548                 *)
549                         panic "Unrecognized live filesystem: ${1}"
550                         ;;
551         esac
552 }
553
554 match_files_in_dir ()
555 {
556         # Does any files match pattern ${1} ?
557         local pattern="${1}"
558
559         if [ "$(echo ${pattern})" != "${pattern}" ]
560         then
561                 return 0
562         fi
563
564         return 1
565 }
566
567 mount_images_in_directory ()
568 {
569         directory="${1}"
570         rootmnt="${2}"
571         mac="${3}"
572
573
574         if match_files_in_dir "${directory}/${LIVE_MEDIA_PATH}/*.squashfs" ||
575                 match_files_in_dir "${directory}/${LIVE_MEDIA_PATH}/*.ext2" ||
576                 match_files_in_dir "${directory}/${LIVE_MEDIA_PATH}/*.ext3" ||
577                 match_files_in_dir "${directory}/${LIVE_MEDIA_PATH}/*.ext4" ||
578                 match_files_in_dir "${directory}/${LIVE_MEDIA_PATH}/*.jffs2" ||
579                 match_files_in_dir "${directory}/${LIVE_MEDIA_PATH}/*.dir"
580         then
581                 [ -n "${mac}" ] && adddirectory="${directory}/${LIVE_MEDIA_PATH}/${mac}"
582                 setup_unionfs "${directory}/${LIVE_MEDIA_PATH}" "${rootmnt}" "${adddirectory}"
583         else
584                 panic "No supported filesystem images found at /${LIVE_MEDIA_PATH}."
585         fi
586 }
587
588 is_nice_device ()
589 {
590         sysfs_path="${1#/sys}"
591
592         if /lib/udev/path_id "${sysfs_path}" | egrep -q "ID_PATH=(usb|pci-|platform-orion-ehci|platform-mmc|platform-mxsdhci|)"
593         then
594                 return 0
595         elif echo "${sysfs_path}" | grep -q '^/block/vd[a-z]$'
596         then
597                 return 0
598         elif echo ${sysfs_path} | grep -q "^/block/dm-"
599         then
600                 return 0
601         fi
602
603         return 1
604 }
605
606 copy_live_to ()
607 {
608         copyfrom="${1}"
609         copytodev="${2}"
610         copyto="${copyfrom}_swap"
611
612         if [ -z "${MODULETORAM}" ]
613         then
614                 size=$(fs_size "" ${copyfrom}/${LIVE_MEDIA_PATH} "used")
615         else
616                 MODULETORAMFILE="${copyfrom}/${LIVE_MEDIA_PATH}/${MODULETORAM}"
617
618                 if [ -f "${MODULETORAMFILE}" ]
619                 then
620                         size=$( expr $(ls -la ${MODULETORAMFILE} | awk '{print $5}') / 1024 + 5000 )
621                 else
622                         log_warning_msg "Error: toram-module ${MODULETORAM} (${MODULETORAMFILE}) could not be read."
623                         return 1
624                 fi
625         fi
626
627         if [ "${copytodev}" = "ram" ]
628         then
629                 # copying to ram:
630                 freespace=$(awk '/^MemFree:/{f=$2} /^Cached:/{c=$2} END{print f+c}' /proc/meminfo)
631                 mount_options="-o size=${size}k"
632                 free_string="memory"
633                 fstype="tmpfs"
634                 dev="/dev/shm"
635         else
636                 # it should be a writable block device
637                 if [ -b "${copytodev}" ]
638                 then
639                         dev="${copytodev}"
640                         free_string="space"
641                         fstype=$(get_fstype "${dev}")
642                         freespace=$(fs_size "${dev}")
643                 else
644                         log_warning_msg "${copytodev} is not a block device."
645                         return 1
646                 fi
647         fi
648
649         if [ "${freespace}" -lt "${size}" ]
650         then
651                 log_warning_msg "Not enough free ${free_string} (${freespace}k free, ${size}k needed) to copy live media in ${copytodev}."
652                 return 1
653         fi
654
655         # begin copying (or uncompressing)
656         mkdir "${copyto}"
657         log_begin_msg "mount -t ${fstype} ${mount_options} ${dev} ${copyto}"
658         mount -t "${fstype}" ${mount_options} "${dev}" "${copyto}"
659
660         if [ "${extension}" = "tgz" ]
661         then
662                 cd "${copyto}"
663                 tar zxf "${copyfrom}/${LIVE_MEDIA_PATH}/$(basename ${FETCH})"
664                 rm -f "${copyfrom}/${LIVE_MEDIA_PATH}/$(basename ${FETCH})"
665                 mount -r -o move "${copyto}" "${rootmnt}"
666                 cd "${OLDPWD}"
667         else
668                 if [ -n "${MODULETORAMFILE}" ]
669                 then
670                         if [ -x /bin/rsync ]
671                         then
672                                 echo " * Copying $MODULETORAMFILE to RAM" 1>/dev/console
673                                 rsync -a --progress ${MODULETORAMFILE} ${copyto} 1>/dev/console # copy only the filesystem module
674                         else
675                                 cp ${MODULETORAMFILE} ${copyto} # copy only the filesystem module
676                         fi
677                 else
678                         if [ -x /bin/rsync ]
679                         then
680                                 echo " * Copying whole medium to RAM" 1>/dev/console
681                                 rsync -a --progress ${copyfrom}/* ${copyto} 1>/dev/console  # "cp -a" from busybox also copies hidden files
682                         else
683                                 mkdir -p ${copyto}/${LIVE_MEDIA_PATH}
684                                 cp -a ${copyfrom}/${LIVE_MEDIA_PATH}/* ${copyto}/${LIVE_MEDIA_PATH}
685                                 if [ -e ${copyfrom}/${LIVE_MEDIA_PATH}/.disk ]
686                                 then
687                                         cp -a ${copyfrom}/${LIVE_MEDIA_PATH}/.disk ${copyto}
688                                 fi
689                         fi
690                 fi
691
692                 umount ${copyfrom}
693                 mount -r -o move ${copyto} ${copyfrom}
694         fi
695
696         rmdir ${copyto}
697         return 0
698 }
699
700 do_netmount ()
701 {
702         rc=1
703
704         modprobe -q af_packet # For DHCP
705
706         udevadm trigger
707         udevadm settle
708
709         if [ -z "${NETBOOT}" ] && [ -z "${FETCH}" ] && \
710            [ -z "${HTTPFS}" ] && [ -z "${FTPFS}" ]
711         then
712
713         # if ethdevice was not specified on the kernel command line
714         # make sure we try to get a working network configuration
715         # for *every* present network device (except for loopback of course)
716         if [ -z "$ETHDEVICE" ] ; then
717                 echo "If you want to boot from a specific device use bootoption ethdevice=..."
718                 for device in /sys/class/net/*; do
719                         dev=${device##*/} ;
720                         if [ "$dev" != "lo" ] ; then
721                                 ETHDEVICE="$ETHDEVICE $dev"
722                         fi
723                 done
724         fi
725
726         # split args of ethdevice=eth0,eth1 into "eth0 eth1"
727         for device in $(echo $ETHDEVICE | sed 's/,/ /g') ; do
728                 devlist="$devlist $device"
729         done
730
731         [ -n "$ETHDEV_TIMEOUT" ] || ETHDEV_TIMEOUT=15
732         echo "Using timeout of $ETHDEV_TIMEOUT seconds for network configuration."
733
734         # this is tricky (and ugly) because ipconfig sometimes just hangs/runs into
735         # an endless loop; iff execution fails give it two further tries, that's
736         # why we use '$devlist $devlist $devlist' for the other for loop
737         for dev in $devlist $devlist $devlist ; do
738                 echo "Executing ipconfig -t $ETHDEV_TIMEOUT $dev"
739                 ipconfig -t "$ETHDEV_TIMEOUT" $dev | tee -a /netboot.config &
740                 jobid=$!
741                 sleep "$ETHDEV_TIMEOUT" ; sleep 1
742                 if [ -r /proc/"$jobid"/status ] ; then
743                         echo "Killing job $jobid for device $dev as ipconfig ran into recursion..."
744                         kill -9 $jobid
745                 fi
746
747                 # if configuration of device worked we should have an assigned
748                 # IP address, iff so let's use the according as $DEVICE for later usage
749                 # simple and primitive approach which seems to work fine
750                 if ifconfig $dev | grep -q 'inet.*addr:' ; then
751                         export DEVICE="$dev"
752                         break
753                 fi
754         done
755
756         else
757         ipconfig ${DEVICE} | tee /netboot.config
758         fi
759
760         # source relevant ipconfig output
761         OLDHOSTNAME=${HOSTNAME}
762         . /tmp/net-${DEVICE}.conf
763         [ -z ${HOSTNAME} ] && HOSTNAME=${OLDHOSTNAME}
764         export HOSTNAME
765
766         # Check if we have a network device at all
767         if ! ls /sys/class/net/"$DEVICE" > /dev/null 2>&1 && \
768            ! ls /sys/class/net/eth0 > /dev/null 2>&1 && \
769            ! ls /sys/class/net/wlan0 > /dev/null 2>&1 && \
770            ! ls /sys/class/net/ath0 > /dev/null 2>&1 && \
771            ! ls /sys/class/net/ra0 > /dev/null 2>&1
772         then
773                 panic "No supported network device found, maybe a non-mainline driver is required."
774         fi
775
776         if [ "${NFSROOT}" = "auto" ]
777         then
778                 NFSROOT=${ROOTSERVER}:${ROOTPATH}
779         fi
780
781         if ( [ -n "${FETCH}" ] || [ -n "${HTTPFS}" ] || [ -n "${FTPFS}" ] ) && do_httpmount
782         then
783                 rc=0
784                 return ${rc}
785         fi
786
787         if [ "${NFSROOT#*:}" = "${NFSROOT}" ] && [ "$NETBOOT" != "cifs" ]
788         then
789                 NFSROOT=${ROOTSERVER}:${NFSROOT}
790         fi
791
792         log_begin_msg "Trying netboot from ${NFSROOT}"
793
794         if [ "${NETBOOT}" != "nfs" ] && do_cifsmount
795         then
796                 rc=0
797         elif do_nfsmount
798         then
799                 NETBOOT="nfs"
800                 export NETBOOT
801                 rc=0
802         fi
803
804         log_end_msg
805         return ${rc}
806 }
807
808 do_httpmount ()
809 {
810         rc=1
811         dest="${mountpoint}/${LIVE_MEDIA_PATH}"
812         mount -t ramfs ram "${mountpoint}"
813         mkdir -p "${dest}"
814
815         for webfile in HTTPFS FTPFS FETCH
816         do
817                 url="$(eval echo \"\$\{${webfile}\}\")"
818                 extension="$(echo "${url}" | sed 's/\(.*\)\.\(.*\)/\2/')"
819
820                 if [ -n "$url" ]
821                 then
822                         case "${extension}" in
823                                 squashfs|tgz|tar)
824                                         if [ "${webfile}" = "FETCH" ]
825                                         then
826                                                 case "$url" in
827                                                         tftp*)
828                                                                 ip="$(dirname $url | sed -e 's|tftp://||g' -e 's|/.*$||g')"
829                                                                 rfile="$(echo $url | sed -e "s|tftp://$ip||g")"
830                                                                 lfile="$(basename $url)"
831                                                                 log_begin_msg "Trying tftp -g -b 10240 -r $rfile -l ${dest}/$lfile $ip"
832                                                                 tftp -g -b 10240 -r $rfile -l ${dest}/$lfile $ip
833                                                         ;;
834
835                                                         *)
836                                                                 log_begin_msg "Trying wget ${url} -O ${dest}/$(basename ${url})"
837                                                                 wget "${url}" -O "${dest}/$(basename ${url})"
838                                                                 ;;
839                                                 esac
840                                         else
841                                                 log_begin_msg "Trying to mount ${url} on ${dest}/$(basename ${url})"
842                                                 if [ "${webfile}" = "FTPFS" ]
843                                                 then
844                                                         FUSE_MOUNT="curlftpfs"
845                                                         url="$(dirname ${url})"
846                                                 else
847                                                         FUSE_MOUNT="httpfs"
848                                                 fi
849                                                 modprobe fuse
850                                                 $FUSE_MOUNT "${url}" "${dest}"
851                                         fi
852                                         [ ${?} -eq 0 ] && rc=0
853                                         [ "${extension}" = "tgz" ] && live_dest="ram"
854                                         break
855                                         ;;
856
857                                 *)
858                                         log_begin_msg "Unrecognized archive extension for ${url}"
859                                         ;;
860                         esac
861                 fi
862         done
863
864         if [ ${rc} != 0 ]
865         then
866             umount "${mountpoint}"
867         fi
868
869         return ${rc}
870 }
871
872 do_nfsmount ()
873 {
874         rc=1
875
876         modprobe -q nfs
877
878         if [ -z "${NFSOPTS}" ]
879         then
880                 NFSOPTS=""
881         fi
882
883         log_begin_msg "Trying nfsmount -o nolock -o ro ${NFSOPTS} ${NFSROOT} ${mountpoint}"
884
885         # FIXME: This while loop is an ugly HACK round an nfs bug
886         i=0
887         while [ "$i" -lt 60 ]
888         do
889                 nfsmount -o nolock -o ro ${NFSOPTS} "${NFSROOT}" "${mountpoint}" && rc=0 && break
890                 sleep 1
891                 i="$(($i + 1))"
892         done
893
894         return ${rc}
895 }
896
897 do_cifsmount ()
898 {
899         rc=1
900
901         if [ -x "/sbin/mount.cifs" ]
902         then
903                 if [ -z "${NFSOPTS}" ]
904                 then
905                         CIFSOPTS="-ouser=root,password="
906                 else
907                         CIFSOPTS="${NFSOPTS}"
908                 fi
909
910                 log_begin_msg "Trying mount.cifs ${NFSROOT} ${mountpoint} ${CIFSOPTS}"
911                 modprobe -q cifs
912
913                 if mount.cifs "${NFSROOT}" "${mountpoint}" "${CIFSOPTS}"
914                 then
915                         rc=0
916                 fi
917         fi
918
919         return ${rc}
920 }
921
922 do_snap_copy ()
923 {
924         fromdev="${1}"
925         todir="${2}"
926         snap_type="${3}"
927         size=$(fs_size "${fromdev}" "" "used")
928
929         if [ -b "${fromdev}" ]
930         then
931                 # look for free mem
932                 if [ -n "${HOMEMOUNTED}" -a "${snap_type}" = "HOME" ]
933                 then
934                         todev=$(awk -v pat="$(base_path ${todir})" '$2 == pat { print $1 }' /proc/mounts)
935                         freespace=$(df -k | awk '/'${todev}'/{print $4}')
936                 else
937                         freespace=$(awk '/^MemFree:/{f=$2} /^Cached:/{c=$2} END{print f+c}' /proc/meminfo)
938                 fi
939
940                 tomount="/mnt/tmpsnap"
941
942                 if [ ! -d "${tomount}" ]
943                 then
944                         mkdir -p "${tomount}"
945                 fi
946
947                 fstype=$(get_fstype "${fromdev}")
948
949                 if [ -n "${fstype}" ]
950                 then
951                         # Copying stuff...
952                         mount -o ro -t "${fstype}" "${fromdev}" "${tomount}" || log_warning_msg "Error in mount -t ${fstype} -o ro ${fromdev} ${tomount}"
953                         cp -a "${tomount}"/* ${todir}
954                         umount "${tomount}"
955                 else
956                         log_warning_msg "Unrecognized fstype: ${fstype} on ${fromdev}:${snap_type}"
957                 fi
958
959                 rmdir "${tomount}"
960
961                 if echo ${fromdev} | grep -qs loop
962                 then
963                         losetup -d "${fromdev}"
964                 fi
965
966                 return 0
967         else
968                 return 1
969
970                 log_warning_msg "Unable to find the snapshot ${snap_type} medium"
971         fi
972 }
973
974 find_snap ()
975 {
976         # Look for ${snap_label}.* in block devices
977         snap_label="${1}"
978
979         if [ "${PERSISTENT}" != "nofiles" ]
980         then
981                 # search for image files
982                 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")
983         fi
984
985         if [ -z "${snapdata}" ]
986         then
987                 snapdata=$(find_cow_device "${snap_label}")
988         fi
989         echo "${snapdata}"
990 }
991
992 try_snap ()
993 {
994         # copy the contents of previously found snapshot to ${snap_mount}
995         # and remember the device and filename for resync on exit in live-boot.init
996
997         snapdata="${1}"
998         snap_mount="${2}"
999         snap_type="${3}"
1000
1001         if [ -n "${snapdata}" ] && [ ! -b "${snapdata}" ]
1002         then
1003                 log_success_msg "found snapshot: ${snapdata}"
1004                 snapdev="$(echo ${snapdata} | cut -f1 -d ' ')"
1005                 snapback="$(echo ${snapdata} | cut -f2 -d ' ')"
1006                 snapfile="$(echo ${snapdata} | cut -f3 -d ' ')"
1007
1008                 RES=""
1009                 if ! try_mount "${snapdev}" "${snapback}" "ro"
1010                 then
1011                         break
1012                 fi
1013
1014                 if echo "${snapfile}" | grep -qs '\(squashfs\|ext2\|ext3\|ext4\|jffs2\)'
1015                 then
1016                         # squashfs, jffs2 or ext2/ext3/ext4 snapshot
1017                         dev=$(get_backing_device "${snapback}/${snapfile}")
1018
1019                         do_snap_copy "${dev}" "${snap_mount}" "${snap_type}"
1020                         RES=$?
1021                 else
1022                         # cpio.gz snapshot
1023
1024                         # Unfortunately klibc's cpio is incompatible with the
1025                         # rest of the world; everything else requires -u -d,
1026                         # while klibc doesn't implement them. Try to detect
1027                         # whether it's in use.
1028                         cpiopath="$(which cpio)" || true
1029                         if [ "$cpiopath" ] && grep -aq /lib/klibc "$cpiopath"
1030                         then
1031                                 cpioargs=
1032                         else
1033                                 cpioargs='--unconditional --make-directories'
1034                         fi
1035
1036                         if ! (cd "${snap_mount}" && zcat "${snapback}/${snapfile}" | cpio $cpioargs --extract --preserve-modification-time --no-absolute-filenames --sparse 2>/dev/null)
1037                         then
1038                                 log_warning_msg "failure to \"zcat ${snapback}/${snapfile} | cpio $cpioargs --extract --preserve-modification-time --no-absolute-filenames --sparse\""
1039                         fi
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="/cow${snap_mount#$rootmnt}":${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                                         mkdir -p /dev/.initramfs/varrun
1371                                         pidof unionfs-fuse >> /dev/.initramfs/varrun/sendsigs.omit || true
1372                                         ;;
1373
1374                                 *)
1375                                         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"
1376                                         ;;
1377                         esac
1378                 done
1379         else
1380                 case "${UNIONTYPE}" in
1381                         unionfs-fuse)
1382                                 (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}")
1383                                 mkdir -p /dev/.initramfs/varrun
1384                                 pidof unionfs-fuse >> /dev/.initramfs/varrun/sendsigs.omit || true
1385                                 ;;
1386
1387                         *)
1388                                 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}"
1389                                 ;;
1390                 esac
1391         fi
1392
1393         # Correct the permissions of /:
1394         chmod 0755 "${rootmnt}"
1395
1396         # tmpfs file systems
1397         touch /etc/fstab
1398         mkdir -p "${rootmnt}/live"
1399         mount -t tmpfs tmpfs ${rootmnt}/live
1400
1401         # Adding other custom mounts
1402         if [ -n "${PERSISTENT}" ] && [ -z "${NOPERSISTENT}" ]
1403         then
1404                 # directly mount /home
1405                 # FIXME: add a custom mounts configurable system
1406
1407                 if [ -b "${homecow}" ]
1408                 then
1409                         mount -t $(get_fstype "${homecow}") -o rw,noatime "${homecow}" "${rootmnt}/home"
1410                         export HOMEMOUNTED=1 # used to proper calculate free space in do_snap_copy()
1411                 else
1412                         log_warning_msg "Unable to find the persistent home medium"
1413                 fi
1414
1415                 # Look for other snapshots to copy in
1416                 try_snap "${root_snapdata}" "${rootmnt}" "ROOT"
1417                 # This second type should be removed when snapshot grow smarter
1418                 try_snap "${home_snapdata}" "${rootmnt}/home" "HOME"
1419         fi
1420
1421         if [ -n "${SHOWMOUNTS}" ]
1422         then
1423                 for d in ${rofslist}
1424                 do
1425                         mkdir -p "${rootmnt}/live/${d##*/}"
1426
1427                         case d in
1428                                 *.dir)
1429                                         # do nothing # mount -o bind "${d}" "${rootmnt}/live/${d##*/}"
1430                                         ;;
1431
1432                                 *)
1433                                         case "${UNIONTYPE}" in
1434                                                 unionfs-fuse)
1435                                                         mount -o bind "${d}" "${rootmnt}/live/${d##*/}"
1436                                                         ;;
1437
1438                                                 *)
1439                                                         mount -o move "${d}" "${rootmnt}/live/${d##*/}"
1440                                                         ;;
1441                                         esac
1442                                         ;;
1443                         esac
1444                 done
1445         fi
1446
1447         # shows cow fs on /cow for use by live-snapshot
1448         mkdir -p "${rootmnt}/live/cow"
1449         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"
1450 }
1451
1452 check_dev ()
1453 {
1454         sysdev="${1}"
1455         devname="${2}"
1456         skip_uuid_check="${3}"
1457
1458         # support for fromiso=.../isofrom=....
1459         if [ -n "$FROMISO" ]
1460         then
1461                 ISO_DEVICE=$(dirname $FROMISO)
1462                 if ! [ -b $ISO_DEVICE ]
1463                 then
1464                         # to support unusual device names like /dev/cciss/c0d0p1
1465                         # as well we have to identify the block device name, let's
1466                         # do that for up to 15 levels
1467                         i=15
1468                         while [ -n "$ISO_DEVICE" ] && [ "$i" -gt 0 ]
1469                         do
1470                                 ISO_DEVICE=$(dirname ${ISO_DEVICE})
1471                                 [ -b "$ISO_DEVICE" ] && break
1472                                 i=$(($i -1))
1473                         done
1474                 fi
1475
1476                 if [ "$ISO_DEVICE" = "/" ]
1477                 then
1478                         echo "Warning: device for bootoption isofrom= ($FROMISO) not found.">>/live.log
1479                 else
1480                         mkdir /isofrom
1481                         mount "$ISO_DEVICE" /isofrom
1482                         ISO_NAME="$(echo $FROMISO | sed "s|$ISO_DEVICE||")"
1483                         loopdevname=$(setup_loop "/isofrom/${ISO_NAME}" "loop" "/sys/block/loop*" "" '')
1484                         devname="${loopdevname}"
1485                 fi
1486         fi
1487
1488         if [ -z "${devname}" ]
1489         then
1490                 devname=$(sys2dev "${sysdev}")
1491         fi
1492
1493         if [ -d "${devname}" ]
1494         then
1495                 mount -o bind "${devname}" $mountpoint || continue
1496
1497                 if is_live_path $mountpoint
1498                 then
1499                         echo $mountpoint
1500                         return 0
1501                 else
1502                         umount $mountpoint
1503                 fi
1504         fi
1505         [ -e "$devname" ] || continue
1506
1507         if [ -n "${LIVE_MEDIA_OFFSET}" ]
1508         then
1509                 loopdevname=$(setup_loop "${devname}" "loop" "/sys/block/loop*" "${LIVE_MEDIA_OFFSET}" '')
1510                 devname="${loopdevname}"
1511         fi
1512
1513         fstype=$(get_fstype "${devname}")
1514
1515         if is_supported_fs ${fstype}
1516         then
1517                 devuid=$(blkid -o value -s UUID "$devname")
1518                 [ -n "$devuid" ] && grep -qs "\<$devuid\>" $tried && continue
1519                 mount -t ${fstype} -o ro,noatime "${devname}" ${mountpoint} || continue
1520                 [ -n "$devuid" ] && echo "$devuid" >> $tried
1521
1522                 if is_live_path ${mountpoint} && \
1523                         ([ "${skip_uuid_check}" ] || matches_uuid ${mountpoint})
1524                 then
1525                         echo ${mountpoint}
1526                         return 0
1527                 else
1528                         umount ${mountpoint}
1529                 fi
1530         fi
1531
1532         if [ -n "${LIVE_MEDIA_OFFSET}" ]
1533         then
1534                 losetup -d "${loopdevname}"
1535         fi
1536
1537         return 1
1538 }
1539
1540 find_livefs ()
1541 {
1542         timeout="${1}"
1543
1544         # don't start autodetection before timeout has expired
1545         if [ -n "${LIVE_MEDIA_TIMEOUT}" ]
1546         then
1547                 if [ "${timeout}" -lt "${LIVE_MEDIA_TIMEOUT}" ]
1548                 then
1549                         return 1
1550                 fi
1551         fi
1552
1553         # first look at the one specified in the command line
1554         case "${LIVE_MEDIA}" in
1555                 removable-usb)
1556                         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1557                         do
1558                                 if [ "$(cat ${sysblock}/removable)" = "1" ]
1559                                 then
1560                                         if readlink ${sysblock} | grep -q usb ||
1561                                            readlink ${sysblock}/device | grep -q usb # linux < 2.6.29
1562                                         then
1563                                                 for dev in $(subdevices "${sysblock}")
1564                                                 do
1565                                                         if check_dev "${dev}"
1566                                                         then
1567                                                                 return 0
1568                                                         fi
1569                                                 done
1570                                         fi
1571                                 fi
1572                         done
1573                         return 1
1574                         ;;
1575
1576                 removable)
1577                         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1578                         do
1579                                 if [ "$(cat ${sysblock}/removable)" = "1" ]
1580                                 then
1581                                         for dev in $(subdevices "${sysblock}")
1582                                         do
1583                                                 if check_dev "${dev}"
1584                                                 then
1585                                                         return 0
1586                                                 fi
1587                                         done
1588                                 fi
1589                         done
1590                         return 1
1591                         ;;
1592
1593                 *)
1594                         if [ ! -z "${LIVE_MEDIA}" ]
1595                         then
1596                                 if check_dev "null" "${LIVE_MEDIA}" "skip_uuid_check"
1597                                 then
1598                                         return 0
1599                                 fi
1600                         fi
1601                         ;;
1602         esac
1603
1604         # or do the scan of block devices
1605         # prefer removable devices over non-removable devices, so scan them first
1606         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | egrep -v "/(loop|ram|dm-|fd)")
1607         do
1608                 if [ "$(cat ${sysblock}/removable)" = "1" ]
1609                 then
1610                         removable_devices_to_scan="$removable_devices_to_scan $sysblock"
1611                 else
1612                         nonremovable_devices_to_scan="$nonremovable_devices_to_scan $sysblock"
1613                 fi
1614         done
1615         devices_to_scan="$removable_devices_to_scan $nonremovable_devices_to_scan"
1616
1617         for sysblock in $devices_to_scan
1618         do
1619                 devname=$(sys2dev "${sysblock}")
1620                 [ -e "$devname" ] || continue
1621                 fstype=$(get_fstype "${devname}")
1622
1623                 if /lib/udev/cdrom_id ${devname} > /dev/null
1624                 then
1625                         if check_dev "null" "${devname}"
1626                         then
1627                                 return 0
1628                         fi
1629                 elif is_nice_device "${sysblock}"
1630                 then
1631                         for dev in $(subdevices "${sysblock}")
1632                         do
1633                                 if check_dev "${dev}"
1634                                 then
1635                                         return 0
1636                                 fi
1637                         done
1638                 elif [ "${fstype}" = "squashfs" -o \
1639                         "${fstype}" = "ext2" -o \
1640                         "${fstype}" = "ext3" -o \
1641                         "${fstype}" = "ext4" -o \
1642                         "${fstype}" = "jffs2" ]
1643                 then
1644                         # This is an ugly hack situation, the block device has
1645                         # an image directly on it.  It's hopefully
1646                         # live-boot, so take it and run with it.
1647                         ln -s "${devname}" "${devname}.${fstype}"
1648                         echo "${devname}.${fstype}"
1649                         return 0
1650                 fi
1651         done
1652
1653         return 1
1654 }
1655
1656 set_usplash_timeout ()
1657 {
1658         if [ -x /sbin/usplash_write ]
1659         then
1660                 /sbin/usplash_write "TIMEOUT 120"
1661         else if [ -x /sbin/splashy_update ] ; then
1662                 /sbin/splashy_update "TIMEOUT 120"
1663         fi ; fi
1664 }
1665
1666 integrity_check ()
1667 {
1668         media_mountpoint="${1}"
1669
1670         log_begin_msg "Checking media integrity"
1671
1672         cd ${media_mountpoint}
1673         /bin/md5sum -c md5sum.txt < /dev/tty8 > /dev/tty8
1674         RC="${?}"
1675
1676         log_end_msg
1677
1678         if [ "${RC}" -eq 0 ]
1679         then
1680                 log_success_msg "Everything ok, will reboot in 10 seconds."
1681                 sleep 10
1682                 cd /
1683                 umount ${media_mountpoint}
1684                 sync
1685                 echo u > /proc/sysrq-trigger
1686                 echo b > /proc/sysrq-trigger
1687         else
1688                 panic "Not ok, a media defect is likely, switch to VT8 for details."
1689         fi
1690 }
1691
1692 start_usplash_pulse ()
1693 {
1694         if [ -x /sbin/usplash_write ]
1695         then
1696                 /sbin/usplash_write "PULSELOGO"
1697         fi
1698 }
1699
1700 mountroot ()
1701 {
1702         if [ -x /scripts/local-top/cryptroot ]; then
1703             /scripts/local-top/cryptroot
1704         fi
1705
1706         exec 6>&1
1707         exec 7>&2
1708         exec > live.log
1709         exec 2>&1
1710         tail -f live.log >&7 &
1711         tailpid="${!}"
1712
1713         # Ensure 'panic' function is overridden
1714         . /scripts/live-functions
1715
1716         Arguments
1717
1718         set_usplash_timeout
1719         start_usplash_pulse
1720
1721         maybe_break live-premount
1722         log_begin_msg "Running /scripts/live-premount"
1723         run_scripts /scripts/live-premount
1724         log_end_msg
1725
1726         # Needed here too because some things (*cough* udev *cough*)
1727         # changes the timeout
1728
1729         set_usplash_timeout
1730
1731         if [ ! -z "${NETBOOT}" ] || [ ! -z "${FETCH}" ] || [ ! -z "${HTTPFS}" ] || [ ! -z "${FTPFS}" ]
1732         then
1733                 if do_netmount
1734                 then
1735                         livefs_root="${mountpoint}"
1736                 else
1737                         panic "Unable to find a live file system on the network"
1738                 fi
1739         else
1740                 if [ -n "${PLAIN_ROOT}" ] && [ -n "${ROOT}" ]
1741                 then
1742                         # Do a local boot from hd
1743                         livefs_root=${ROOT}
1744                 else
1745                         # Scan local devices for the image
1746                         i=0
1747                         while [ "$i" -lt 60 ]
1748                         do
1749                                 livefs_root=$(find_livefs ${i})
1750
1751                                 if [ -n "${livefs_root}" ]
1752                                 then
1753                                         break
1754                                 fi
1755
1756                                 sleep 1
1757                                 i="$(($i + 1))"
1758                         done
1759                 fi
1760         fi
1761
1762         if [ -z "${livefs_root}" ]
1763         then
1764                 panic "Unable to find a medium containing a live file system"
1765         fi
1766
1767         if [ "${INTEGRITY_CHECK}" ]
1768         then
1769                 integrity_check "${livefs_root}"
1770         fi
1771
1772         if [ "${TORAM}" ]
1773         then
1774                 live_dest="ram"
1775         elif [ "${TODISK}" ]
1776         then
1777                 live_dest="${TODISK}"
1778         fi
1779
1780         if [ "${live_dest}" ]
1781         then
1782                 log_begin_msg "Copying live media to ${live_dest}"
1783                 copy_live_to "${livefs_root}" "${live_dest}"
1784                 log_end_msg
1785         fi
1786
1787         # if we do not unmount the ISO we can't run "fsck /dev/ice" later on
1788         # because the mountpoint is left behind in /proc/mounts, so let's get
1789         # rid of it when running from RAM
1790         if [ -n "$FROMISO" ] && [ "${TORAM}" ]
1791         then
1792           losetup -d /dev/loop0
1793           grep -q /isofrom /proc/mounts && umount /isofrom
1794         fi
1795
1796         if [ -n "${MODULETORAMFILE}" ] || [ -n "${PLAIN_ROOT}" ]
1797         then
1798                 setup_unionfs "${livefs_root}" "${rootmnt}"
1799         else
1800                 mac="$(get_mac)"
1801                 mac="$(echo ${mac} | sed 's/-//g')"
1802                 mount_images_in_directory "${livefs_root}" "${rootmnt}" "${mac}"
1803         fi
1804
1805         log_end_msg
1806
1807         # unionfs-fuse needs /dev to be bind-mounted for the duration of
1808         # live-bottom; udev's init script will take care of things after that
1809         if [ "${UNIONTYPE}" = unionfs-fuse ]
1810         then
1811                 mount -n -o bind /dev "${rootmnt}/dev"
1812         fi
1813
1814         # Move to the new root filesystem so that programs there can get at it.
1815         if [ ! -d /root/live/image ]
1816         then
1817                 mkdir -p /root/live/image
1818                 mount --move /live/image /root/live/image
1819         fi
1820
1821         maybe_break live-bottom
1822         log_begin_msg "Running /scripts/live-bottom\n"
1823
1824         run_scripts /scripts/live-bottom
1825         log_end_msg
1826
1827         if [ "${UNIONFS}" = unionfs-fuse ]
1828         then
1829                 umount "${rootmnt}/dev"
1830         fi
1831
1832         exec 1>&6 6>&-
1833         exec 2>&7 7>&-
1834         kill ${tailpid}
1835         [ -w "${rootmnt}/var/log/" ] && cp live.log "${rootmnt}/var/log/" 2>/dev/null
1836         if [ -f /etc/live.conf ]
1837         then
1838                 cp /etc/live.conf "${rootmnt}/etc/"
1839         fi
1840 }