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