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