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