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