Adding live-initramfs 1.99.2-1.
[live-boot-grml.git] / scripts / live
1 #!/bin/sh
2
3 # set -e
4
5 export PATH="/root/usr/bin:/root/usr/sbin:/root/bin:/root/sbin:/usr/bin:/usr/sbin:/bin:/sbin"
6
7 echo "/root/lib" >> /etc/ld.so.conf
8 echo "/root/usr/lib" >> /etc/ld.so.conf
9
10 mountpoint="/live/image"
11 LIVE_MEDIA_PATH="live"
12
13 root_persistence="live-rw"
14 home_persistence="home-rw"
15 root_snapshot_label="live-sn"
16 home_snapshot_label="home-sn"
17
18 USERNAME="user"
19 USERFULLNAME="Live user"
20 HOSTNAME="host"
21 BUILD_SYSTEM="Custom"
22
23 mkdir -p "${mountpoint}"
24
25 [ -f /etc/live.conf ] && . /etc/live.conf
26 export USERNAME USERFULLNAME HOSTNAME BUILD_SYSTEM
27
28 . /scripts/live-helpers
29
30 if [ ! -f /live.vars ]
31 then
32         touch /live.vars
33 fi
34
35 Arguments ()
36 {
37         PRESEEDS=""
38
39         for ARGUMENT in `cat /proc/cmdline`
40         do
41                 case "${ARGUMENT}" in
42                         access=*)
43                                 ACCESS="${ARGUMENT#access=}"
44                                 export ACCESS
45                                 ;;
46
47                         console=*)
48                                 DEFCONSOLE="${ARGUMENT#*=}"
49                                 export DEFCONFSOLE
50                                 ;;
51
52                         debug)
53                                 DEBUG="Yes"
54                                 export DEBUG
55
56                                 set -x
57                                 ;;
58
59                         fetch=*)
60                                 FETCH="${ARGUMENT#fetch=}"
61                                 export FETCH
62                                 ;;
63
64                         hostname=*)
65                                 HOSTNAME="${ARGUMENT#hostname=}"
66                                 LIVECONF="changed"
67                                 export HOSTNAME LIVECONF
68                                 ;;
69
70                         username=*)
71                                 USERNAME="${ARGUMENT#username=}"
72                                 LIVECONF="changed"
73                                 export USERNAME LIVECONF
74                                 ;;
75
76                         userfullname=*)
77                                 USERFULLNAME="${ARGUMENT#userfullname=}"
78                                 LIVECONF="changed"
79                                 export USERFULLNAME LIVECONF
80                                 ;;
81
82                         ip=*)
83                                 STATICIP="${ARGUMENT#ip=}"
84
85                                 if [ -z "${STATICIP}" ]
86                                 then
87                                         STATICIP="frommedia"
88                                 fi
89
90                                 export STATICIP
91                                 ;;
92
93                         keyb=*|kbd-chooser/method=*)
94                                 KBD="${ARGUMENT#*=}"
95                                 export KBD
96                                 ;;
97
98                         klayout=*|console-setup/layoutcode=*)
99                                 KLAYOUT="${ARGUMENT#*=}"
100                                 export KLAYOUT
101                                 ;;
102
103                         kvariant=*|console-setup/variantcode=*)
104                                 KVARIANT="${ARGUMENT#*=}"
105                                 export KVARIANT
106                                 ;;
107
108                         kmodel=*|console-setup/modelcode=*)
109                                 KMODEL="${ARGUMENT#*=}"
110                                 export KMODEL
111                                 ;;
112
113                         koptions=*)
114                                 KOPTIONS="${ARGUMENT#koptions=}"
115                                 export KOPTIONS
116                                 ;;
117
118                         live-getty)
119                                 LIVE_GETTY="1"
120                                 export LIVE_GETTY
121                                 ;;
122
123                         live-media=*|bootfrom=*)
124                                 LIVE_MEDIA="${ARGUMENT#*=}"
125                                 export LIVE_MEDIA
126                                 ;;
127
128                         live-media-encryption=*|encryption=*)
129                                 LIVE_MEDIA_ENCRYPTION="${ARGUMENT#*=}"
130                                 export LIVE_MEDIA_ENCRYPTION
131                                 ;;
132
133                         live-media-offset=*)
134                                 LIVE_MEDIA_OFFSET="${ARGUMENT#live-media-offset=}"
135                                 export LIVE_MEDIA_OFFSET
136                                 ;;
137
138                         live-media-path=*)
139                                 LIVE_MEDIA_PATH="${ARGUMENT#live-media-path=}"
140                                 export LIVE_MEDIA_PATH
141                                 ;;
142
143                         live-media-timeout=*)
144                                 LIVE_MEDIA_TIMEOUT="${ARGUMENT#live-media-timeout=}"
145                                 export LIVE_MEDIA_TIMEOUT
146                                 ;;
147
148                         locale=*|debian-installer/locale=*)
149                                 LOCALE="${ARGUMENT#*=}"
150                                 export LOCALE
151                                 ;;
152
153                         module=*)
154                                 MODULE="${ARGUMENT#module=}"
155                                 export MODULE
156                                 ;;
157
158                         netboot=*)
159                                 NETBOOT="${ARGUMENT#netboot=}"
160                                 export NETBOOT
161                                 ;;
162
163                         nfsopts=*)
164                                 NFSOPTS="${ARGUMENT#nfsopts=}"
165                                 export NFSOPTS
166                                 ;;
167
168                         noaccessibility)
169                                 NOACCESSIBILITY="Yes"
170                                 export NOACCESSIBILITY
171                                 ;;
172
173                         noaptcdrom)
174                                 NOAPTCDROM="Yes"
175                                 export NOAPTCDROM
176                                 ;;
177
178                         noautologin)
179                                 NOAUTOLOGIN="Yes"
180                                 export NOAUTOLOGIN
181                                 ;;
182
183                         noxautologin)
184                                 NOXAUTOLOGIN="Yes"
185                                 export NOXAUTOLOGIN
186                                 ;;
187
188                         noconsolekeyboard)
189                                 NOCONSOLEKEYBOARD="Yes"
190                                 export NOCONSOLEKEYBOARD
191                                 ;;
192
193                         nofastboot)
194                                 NOFASTBOOT="Yes"
195                                 export NOFASTBOOT
196                                 ;;
197
198                         nofstab)
199                                 NOFSTAB="Yes"
200                                 export NOFSTAB
201                                 ;;
202
203                         nognomepanel)
204                                 NOGNOMEPANEL="Yes"
205                                 export NOGNOMEPANEL
206                                 ;;
207
208                         nohosts)
209                                 NOHOSTS="Yes"
210                                 export NOHOSTS
211                                 ;;
212
213                         nokpersonalizer)
214                                 NOKPERSONALIZER="Yes"
215                                 export NOKPERSONALIZER
216                                 ;;
217
218                         nokwallet)
219                                 NOKWALLET="Yes"
220                                 export NOKWALLET
221                                 ;;
222
223                         nolanguageselector)
224                                 NOLANGUAGESELECTOR="Yes"
225                                 export NOLANGUAGESELECTOR
226                                 ;;
227
228                         nolocales)
229                                 NOLOCALES="Yes"
230                                 export NOLOCALES
231                                 ;;
232
233                         nonetworking)
234                                 NONETWORKING="Yes"
235                                 export NONETWORKING
236                                 ;;
237
238                         nopowermanagement)
239                                 NOPOWERMANAGEMENT="Yes"
240                                 export NOPOWERMANAGEMENT
241                                 ;;
242
243                         noprogramcrashes)
244                                 NOPROGRAMCRASHES="Yes"
245                                 export NOPROGRAMCRASHES
246                                 ;;
247
248                         norestrictedmanager)
249                                 NORESTRICTEDMANAGER="Yes"
250                                 export NORESTRICTEDMANAGER
251                                 ;;
252
253                         nosudo)
254                                 NOSUDO="Yes"
255                                 export NOSUDO
256                                 ;;
257
258                         noswap)
259                                 NOSWAP="Yes"
260                                 export NOSWAP
261                                 ;;
262
263                         noupdatenotifier)
264                                 NOUPDATENOTIFIER="Yes"
265                                 export NOUPDATENOTIFIER
266                                 ;;
267
268                         nouser)
269                                 NOUSER="Yes"
270                                 export NOUSER
271                                 ;;
272
273                         noxautoconfig)
274                                 NOXAUTOCONFIG="Yes"
275                                 export NOXAUTOCONFIG
276                                 ;;
277
278                         noxscreensaver)
279                                 NOXSCREENSAVER="Yes"
280                                 export NOXSCREENSAVER
281                                 ;;
282
283                         persistent)
284                                 PERSISTENT="Yes"
285                                 export PERSISTENT
286                                 ;;
287
288                         nopersistent)
289                                 NOPERSISTENT="Yes"
290                                 export NOPERSISTENT
291                                 ;;
292
293                         preseed/file=*|file=*)
294                                 LOCATION="${ARGUMENT#*=}"
295                                 export LOCATION
296                                 ;;
297
298                         nopreseed)
299                                 NOPRESEED="Yes"
300                                 export NOPRESEED
301                                 ;;
302
303                         url=*)
304                                 location="${ARGUMENT#url=}"
305
306                                 mount -n -o bind /sys /root/sys
307                                 mount -n -o bind /proc /root/proc
308                                 mount -n -o bind /dev /root/dev
309
310                                 mkdir -p /root/var/run/network
311                                 chroot /root ifup -a
312                                 chroot /root wget -P /tmp "$location"
313                                 chroot /root ifdown -a
314
315                                 umount /root/sys
316                                 umount /root/proc
317                                 umount /root/dev
318
319                                 LOCATION="/tmp/$(basename "$location")"
320                                 ;;
321
322                         */*=*)
323                                 question="${ARGUMENT%%=*}"
324                                 value="${ARGUMENT#*=}"
325                                 PRESEEDS="${PRESEEDS}\"${question}=${value}\" "
326                                 export PRESEEDS
327                                 ;;
328
329                         showmounts)
330                                 SHOWMOUNTS="Yes"
331                                 export SHOWMOUNTS
332                                 ;;
333
334                         timezone=*)
335                                 TIMEZONE="${ARGUMENT#timezone=}"
336                                 export TIMEZONE
337                                 ;;
338
339                         notimezone)
340                                 NOTIMEZONE="Yes"
341                                 export NOTIMEZONE
342                                 ;;
343
344                         todisk=*)
345                                 TODISK="${ARGUMENT#todisk=}"
346                                 export TODISK
347                                 ;;
348
349                         toram)
350                                 TORAM="Yes"
351                                 export TORAM
352                                 ;;
353
354                         toram=*)
355                                 TORAM="Yes"
356                                 MODULETORAM="${ARGUMENT#toram=}"
357                                 export TORAM MODULETORAM
358                                 ;;
359
360                         union=*)
361                                 UNIONTYPE="${ARGUMENT#union=}"
362                                 export UNIONTYPE
363                                 ;;
364
365                         utc=*)
366                                 UTC="${ARGUMENT#utc=}"
367                                 export UTC
368                                 ;;
369
370                         xdebconf)
371                                 XDEBCONF="Yes"
372                                 export XDEBCONF
373                                 ;;
374
375                         xvideomode=*)
376                                 XVIDEOMODE="${ARGUMENT#xvideomode=}"
377                                 export XVIDEOMODE
378                                 ;;
379                 esac
380         done
381
382         # sort of compatibility with netboot.h from linux docs
383         if [ -z "${NETBOOT}" ]
384         then
385                 if [ "${ROOT}" = "/dev/nfs" ]
386                 then
387                         NETBOOT="nfs"
388                         export NETBOOT
389                 elif [ "${ROOT}" = "/dev/cifs" ]
390                 then
391                         NETBOOT="cifs"
392                         export NETBOOT
393                 fi
394         fi
395
396         if [ -z "${MODULE}" ]
397         then
398                 MODULE="filesystem"
399                 export MODULE
400         fi
401
402         if [ -z "${UNIONTYPE}" ]
403         then
404                 UNIONTYPE="unionfs"
405                 export UNIONTYPE
406         fi
407 }
408
409 is_live_path ()
410 {
411         DIRECTORY="${1}"
412
413         if [ -d "${DIRECTORY}"/"${LIVE_MEDIA_PATH}" ]
414         then
415                 for FILESYSTEM in squashfs ext2 ext3 xfs dir
416                 do
417                         if [ "`echo ${DIRECTORY}/${LIVE_MEDIA_PATH}/*.${FILESYSTEM}`" != "${DIRECTORY}/${LIVE_MEDIA_PATH}/*.${FILESYSTEM}" ]
418                         then
419                                 return 0
420                         fi
421                 done
422         fi
423
424         return 1
425 }
426
427 get_backing_device ()
428 {
429         case "$1" in
430                 *.squashfs|*.ext2|*.ext3)
431                         echo $(setup_loop "$1" "loop" "/sys/block/loop*" '0' "${LIVE_MEDIA_ENCRYPTION}")
432                         ;;
433
434                 *.dir)
435                         echo "directory"
436                         ;;
437
438                 *)
439                         panic "Unrecognized live filesystem: $1"
440                         ;;
441         esac
442 }
443
444 match_files_in_dir ()
445 {
446         # Does any files match pattern $1 ?
447         local pattern="$1"
448
449         if [ "$(echo $pattern)" != "$pattern" ]
450         then
451                 return 0
452         fi
453
454         return 1
455 }
456
457 mount_images_in_directory ()
458 {
459         directory="$1"
460         rootmnt="$2"
461
462         if match_files_in_dir "$directory/${LIVE_MEDIA_PATH}/*.squashfs" ||
463                 match_files_in_dir "$directory/${LIVE_MEDIA_PATH}/*.ext2" ||
464                 match_files_in_dir "$directory/${LIVE_MEDIA_PATH}/*.ext3" ||
465                 match_files_in_dir "$directory/${LIVE_MEDIA_PATH}/*.dir"
466         then
467                 setup_unionfs "$directory/${LIVE_MEDIA_PATH}" "$rootmnt"
468         else
469                 :
470         fi
471 }
472
473 is_nice_device ()
474 {
475         sysfs_path="${1#/sys}"
476
477         if /lib/udev/path_id "${sysfs_path}" | grep -E -q "ID_PATH=(usb|pci-[^-]*-[ide|scsi|usb])"
478         then
479                 return 0
480         fi
481
482         return 1
483 }
484
485 is_supported_fs ()
486 {
487         # FIXME: do something better like the scan of supported filesystems
488         fstype="${1}"
489
490         case ${fstype} in
491                 vfat|iso9660|udf|ext2|ext3|ntfs)
492                         return 0
493                         ;;
494         esac
495
496         return 1
497 }
498
499 copy_live_to ()
500 {
501         copyfrom="${1}"
502         copytodev="${2}"
503         copyto="${copyfrom}_swap"
504
505         if [ -z "${MODULETORAM}" ]
506         then
507                 size=$(fs_size "" ${copyfrom} "used")
508         else
509                 MODULETORAMFILE="${copyfrom}/${LIVE_MEDIA_PATH}/${MODULETORAM}"
510
511                 if [ -f "${MODULETORAMFILE}" ]
512                 then
513                         size=$( expr $(ls -la ${MODULETORAMFILE} | awk '{print $5}') / 1024 + 5000 )
514                 else
515                         log_warning_msg "Error: toram-module $MODULETORAM ($MODULETORAMFILE) could not be read."
516                         return 1
517                 fi
518         fi
519
520         if [ "${copytodev}" = "ram" ]
521         then
522                 # copying to ram:
523                 freespace=$( expr $(awk '/MemFree/{print $2}' /proc/meminfo) + $( cat /proc/meminfo | grep Cached | head -n 1 | awk '/Cached/{print $2}' - ) )
524                 mount_options="-o size=${size}k"
525                 free_string="memory"
526                 fstype="tmpfs"
527                 dev="/dev/shm"
528         else
529                 # it should be a writable block device
530                 if [ -b "${copytodev}" ]
531                 then
532                         dev="${copytodev}"
533                         free_string="space"
534                         fstype=$(get_fstype "${dev}")
535                         freespace=$(fs_size "${dev}")
536                 else
537                         [ "$quiet" != "y" ] && log_warning_msg "${copytodev} is not a block device."
538                         return 1
539                 fi
540         fi
541
542         if [ "${freespace}" -lt "${size}" ]
543         then
544                 [ "$quiet" != "y" ] && log_warning_msg "Not enough free ${free_string} (${freespace}k free, ${size}k needed) to copy live media in ${copytodev}."
545                 return 1
546         fi
547
548         # begin copying (or uncompressing)
549         mkdir "${copyto}"
550         echo "mount -t ${fstype} ${mount_options} ${dev} ${copyto}"
551         mount -t "${fstype}" ${mount_options} "${dev}" "${copyto}"
552
553         if [ "$extension" = "tgz" ]
554         then
555                 cd "${copyto}"
556                 tar zxf "${copyfrom}/${LIVE_MEDIA_PATH}/$(basename ${FETCH})"
557                 rm -f "${copyfrom}/${LIVE_MEDIA_PATH}/$(basename ${FETCH})"
558                 mount -r --move "${copyto}" "${rootmnt}"
559                 cd "${OLDPWD}"
560         else
561                 if [ -n "${MODULETORAMFILE}" ]
562                 then
563                         cp ${MODULETORAMFILE} ${copyto} # copy only the filesystem module
564                 else
565                         cp -a ${copyfrom}/* ${copyto}   # "cp -a" from busybox also copies hidden files
566                 fi
567
568                 livefs_root
569                 umount ${copyfrom}
570                 mount -r --move ${copyto} ${copyfrom}
571         fi
572
573         rmdir ${copyto}
574         return 0
575 }
576
577 do_netmount ()
578 {
579         rc=1
580
581         modprobe "${MP_QUIET}" af_packet # For DHCP
582
583         udevtrigger
584         udevsettle
585
586         ipconfig ${DEVICE} | tee /netboot.config
587
588         if [ "${NFSROOT}" = "auto" ]
589         then
590                 NFSROOT=${ROOTSERVER}:${ROOTPATH}
591         fi
592
593         # source relevant ipconfig output
594         OLDHOSTNAME=$HOSTNAME
595         . /tmp/net-${DEVICE}.conf
596         [ -z $HOSTNAME ] && HOSTNAME=$OLDHOSTNAME
597         export HOSTNAME
598
599         if [ -n "${FETCH}" ] && do_httpmount
600         then
601                 rc=0
602                 return ${rc}
603         fi
604
605         if [ "${NFSROOT#*:}" = "$NFSROOT" ] && [ "$NETBOOT" != "cifs" ]
606         then
607                 NFSROOT=${ROOTSERVER}:${NFSROOT}
608         fi
609
610         [ "$quiet" != "y" ] && log_begin_msg "Trying netboot from ${NFSROOT}"
611
612         if [ "${NETBOOT}" != "nfs" ] && do_cifsmount
613         then
614                 rc=0
615         elif do_nfsmount
616         then
617                 NETBOOT="nfs"
618                 export NETBOOT
619                 rc=0
620         fi
621
622         [ "$quiet" != "y" ] && log_end_msg
623         return ${rc}
624 }
625
626 do_httpmount ()
627 {
628         rc=1
629         extension=`echo "${FETCH}" | sed 's/\(.*\)\.\(.*\)/\2/'`
630
631         case "${extension}" in
632                 squashfs|tgz|tar)
633                         [ "$quiet" != "y" ] && log_begin_msg "Trying wget ${FETCH} -O ${mountpoint}/$(basename ${FETCH})"
634                         mkdir -p "${mountpoint}/${LIVE_MEDIA_PATH}"
635                         wget "${FETCH}" -O "${mountpoint}/${LIVE_MEDIA_PATH}/$(basename ${FETCH})"
636                         [ $? -eq 0 ] && rc=0
637                         [ "${extension}" == "tgz" ] && live_dest="ram"
638                         ;;
639
640                 *)
641                         [ "$quiet" != "y" ] && log_begin_msg "Unrecognized archive extension for ${FETCH}"
642         esac
643
644         return ${rc}
645 }
646
647 do_nfsmount ()
648 {
649         rc=1
650
651         modprobe "${MP_QUIET}" nfs
652
653         if [ -z "${NFSOPTS}" ]
654         then
655                 NFSOPTS=""
656         fi
657
658         [ "$quiet" != "y" ] && log_begin_msg "Trying nfsmount -o nolock -o ro ${NFSOPTS} ${NFSROOT} ${mountpoint}"
659
660         # FIXME: This for loop is an ugly HACK round an nfs bug
661         for i in 0 1 2 3 4 5 6 7 8 9 a b c d e f 10 11 12 13
662         do
663                 nfsmount -o nolock -o ro ${NFSOPTS} "${NFSROOT}" "${mountpoint}" && rc=0 && break
664                 sleep 1
665         done
666
667         return ${rc}
668 }
669
670 do_cifsmount ()
671 {
672         rc=1
673
674         if [ -x "/sbin/mount.cifs" ]
675         then
676                 if [ -z "${NFSOPTS}" ]
677                 then
678                         CIFSOPTS="-ouser=root,password="
679                 else
680                         CIFSOPTS="${NFSOPTS}"
681                 fi
682
683                 [ "$quiet" != "y" ] && log_begin_msg "Trying mount.cifs ${NFSROOT} ${mountpoint} ${CIFSOPTS}"
684                 modprobe "${MP_QUIET}" cifs
685
686                 if mount.cifs "${NFSROOT}" "${mountpoint}" "${CIFSOPTS}"
687                 then
688                         rc=0
689                 fi
690         fi
691
692         return ${rc}
693 }
694
695 do_snap_copy ()
696 {
697         fromdev="${1}"
698         todir="${2}"
699         snap_type="${3}"
700         size=$(fs_size "${fromdev}" "" "used")
701
702         if [ -b "${fromdev}" ]
703         then
704                 # look for free mem
705                 if [ -n "${HOMEMOUNTED}" -a "${snap_type}" = "HOME" ]
706                 then
707                         todev=$(cat /proc/mounts | grep -s " $(base_path ${todir}) " | awk '{print $1}' )
708                         freespace=$(df -k  | grep -s ${todev} | awk '{print $4}')
709                 else
710                         freespace=$( expr $(awk '/MemFree/{print $2}' /proc/meminfo) + $( cat /proc/meminfo | grep Cached | head -n 1 | awk '/Cached/{print $2}' - ))
711                 fi
712
713                 tomount="/mnt/tmpsnap"
714
715                 if [ ! -d "${tomount}" ]
716                 then
717                         mkdir -p "${tomount}"
718                 fi
719
720                 fstype=$(get_fstype "${fromdev}")
721
722                 if [ -n "${fstype}" ]
723                 then
724                         # Copying stuff...
725                         mount -t "${fstype}" -o ro "${fromdev}" "${tomount}"
726                         cp -a "${tomount}"/* ${todir}
727                         umount "${tomount}"
728                 else
729                         log_warning_msg "Unrecognized fstype: ${fstype} on ${fromdev}:${snap_type}"
730                 fi
731
732                 rmdir "${tomount}"
733
734                 if echo ${fromdev} | grep -qs loop
735                 then
736                         losetup -d "${fromdev}"
737                 fi
738
739                 return 0
740         else
741                 return 1
742
743                 [ "$quiet" != "y" ] && log_warning_msg "Unable to find the snapshot ${snap_type} medium"
744         fi
745 }
746
747 try_snap ()
748 {
749         # Look for $snap_label.* in block devices and copy the contents to $snap_mount
750         # and remember the device and filename for resync on exit in live-initramfs.init
751
752         snap_label="${1}"
753         snap_mount="${2}"
754         snap_type="${3}"
755         snapdata=$(find_files "${snap_label}.squashfs ${snap_label}.cpio.gz ${snap_label}.ext2 ${snap_label}.ext3")
756
757         if [ ! -z "${snapdata}" ]
758         then
759                 snapdev="$(echo ${snapdata} | cut -f1 -d ' ')"
760                 snapback="$(echo ${snapdata} | cut -f2 -d ' ')"
761                 snapfile="$(echo ${snapdata} | cut -f3 -d ' ')"
762
763                 if echo "${snapfile}" | grep -qs '\(squashfs\|ext2\|ext3\)'
764                 then
765                         # squashfs or ext2/ext3 snapshot
766                         dev=$(get_backing_device "${snapback}/${snapfile}")
767
768                         if ! do_snap_copy "${dev}" "${snap_mount}" "${snap_type}"
769                         then
770                                 log_warning_msg "Impossible to include the ${snapfile} Snapshot"
771                                 return 1
772                         fi
773                 else
774                         # cpio.gz snapshot
775                         if ! (cd "${snap_mount}" && zcat "${snapback}/${snapfile}" | cpio -i -u -d 2>/dev/null)
776                         then
777                                 log_warning_msg "Impossible to include the ${snapfile} Snapshot"
778                                 return 1
779                         fi
780                 fi
781
782                 umount "${snapback}"
783         else
784                 dev=$(find_cow_device "${snap_label}")
785
786                 if [ -b ${dev} ]
787                 then
788                         if echo "${dev}" | grep -qs loop
789                         then
790                                 # strange things happens, user confused?
791                                 snaploop=$( losetup ${dev} | awk '{print $3}' | tr -d '()' )
792                                 snapfile=$(basename ${snaploop})
793                                 snapdev=$(cat /proc/mounts | awk '{print $2,$1}' | grep -es "^$( dirname ${snaploop} )" | cut -f2 -d ' ')
794                         else
795                                 snapdev="${dev}"
796                         fi
797
798                         if ! do_snap_copy "${dev}" "${snap_mount}" "${snap_type}"
799                         then
800                                 log_warning_msg "Impossible to include the ${snap_label} Snapshot"
801                                 return 1
802                         else
803                                 if [ -n "${snapfile}" ]
804                                 then
805                                         # it was a loop device, user confused
806                                         umount ${snapdev}
807                                 fi
808                         fi
809                 else
810                         log_warning_msg "Impossible to include the ${snap_label} Snapshot"
811                         return 1
812                 fi
813         fi
814
815         echo "export ${snap_type}SNAP="${snap_mount}":${snapdev}:${snapfile}" >> /etc/live.conf # for resync on reboot/halt
816         return 0
817 }
818
819 setup_unionfs ()
820 {
821         image_directory="$1"
822         rootmnt="$2"
823
824         modprobe "${MP_QUIET}" -b ${UNIONTYPE}
825
826         # run-init can't deal with images in a subdir, but we're going to
827         # move all of these away before it runs anyway.  No, we're not,
828         # put them in / since move-mounting them into / breaks mono and
829         # some other apps.
830
831         croot="/"
832
833         # Let's just mount the read-only file systems first
834         rofsstring=""
835         rofslist=""
836         minor_kernel_version=`uname -r|cut -c 5-|sed 's/[^0-9].*//'`
837
838         if [ "${NETBOOT}" = "nfs" ] && [ "${minor_kernel_version}" -lt 22 ]
839         then
840                 # go aroung a bug in nfs-unionfs locking for unionfs <= 1.4
841                 roopt="nfsro"
842         else
843                 roopt="ro"
844         fi
845
846         # Read image names from ${MODULE}.module if it exists
847         if [ -e "${image_directory}/filesystem.${MODULE}.module" ]
848         then
849                 for IMAGE in `cat ${image_directory}/filesystem.${MODULE}.module`
850                 do
851                         image_string="${image_string} ${image_directory}/${image}"
852                 done
853         elif [ -e "${image_directory}/${MODULE}.module" ]
854         then
855                 for IMAGE in `cat ${image_directory}/${MODULE}.module`
856                 do
857                         image_string="${image_string} ${image_directory}/${image}"
858                 done
859         else
860                 # ${MODULE}.module does not exist, create a list of images
861                 for FILESYSTEM in squashfs ext2 ext3 xfs dir
862                 do
863                         for IMAGE in "${image_directory}"/*."${FILESYSTEM}"
864                         do
865                                 if [ -e "${IMAGE}" ]
866                                 then
867                                         image_string="${image_string} ${IMAGE}"
868                                 fi
869                         done
870                 done
871
872                 # Now sort the list
873                 image_string="`echo ${image_string} | sed -e 's/ /\n/g' | sort `"
874         fi
875
876         [ -n "${MODULETORAMFILE}" ] && image_string="${image_directory}/$(basename ${MODULETORAMFILE})"
877
878         mkdir -p "${croot}"
879
880         for image in ${image_string}
881         do
882                 imagename=$(basename "${image}")
883
884                 if [ -d "${image}" ]
885                 then
886                         # it is a plain directory: do nothing
887                         rofsstring="${image}=${roopt}:${rofsstring}"
888                         rofslist="${image} ${rofslist}"
889                 elif [ -f "${image}" ]
890                 then
891                         backdev=$(get_backing_device "$image")
892                         fstype=$(get_fstype "${backdev}")
893
894                         if [ "${fstype}" = "unknown" ]
895                         then
896                                 panic "Unknown file system type on ${backdev} (${image})"
897                         fi
898
899                         mkdir -p "${croot}/${imagename}"
900                         echo "debug: Can not mount backdev $backdev (image = $image) on croot/imagename ${croot}/${imagename}"
901                         mount -t "${fstype}" -o ro "${backdev}" "${croot}/${imagename}" || panic "Can not mount $backdev ($image) on ${croot}/${imagename}" && rofsstring="${croot}/${imagename}=${roopt}:${rofsstring}" && rofslist="${croot}/${imagename} ${rofslist}"
902                 fi
903         done
904
905         rofsstring=${rofsstring%:}
906
907         mkdir -p /cow
908         cowdevice="tmpfs"
909         cow_fstype="tmpfs"
910
911         # Looking for "${root_persistence}" device or file
912         if [ -n "${PERSISTENT}" ]
913         then
914                 cowprobe=$(find_cow_device "${root_persistence}")
915
916                 if [ -b "${cowprobe}" ]
917                 then
918                         cowdevice=${cowprobe}
919                         cow_fstype=$(get_fstype "${cowprobe}")
920                 else
921                         [ "$quiet" != "y" ] && log_warning_msg "Unable to find the persistent medium"
922                 fi
923         fi
924
925         mount ${cowdevice} -t ${cow_fstype} -o rw /cow || panic "Can not mount $cowdevice on /cow"
926
927         mount -t ${UNIONTYPE} -o dirs=/cow=rw:$rofsstring ${UNIONTYPE} "$rootmnt" || panic "${UNIONTYPE} mount failed"
928
929         # Adding other custom mounts
930         if [ -n "${PERSISTENT}" ]
931         then
932                 # directly mount /home
933                 # FIXME: add a custom mounts configurable system
934                 homecow=$(find_cow_device "${home_persistence}" )
935
936                 if [ -b "${homecow}" ]
937                 then
938                         mount -t $(get_fstype "${homecow}") -o rw "${homecow}" "${rootmnt}/home"
939                         export HOMEMOUNTED=1 # used to proper calculate free space in do_snap_copy()
940                 else
941                         [ "$quiet" != "y" ] && log_warning_msg "Unable to find the persistent home medium"
942                 fi
943
944                 # Look for other snapshots to copy in
945                 try_snap "${root_snapshot_label}" "${rootmnt}" "ROOT"
946                 try_snap "${home_snapshot_label}" "${rootmnt}/home" "HOME"
947         fi
948
949         if [ -n "${SHOWMOUNTS}" ]
950         then
951                 for d in ${rofslist}
952                 do
953                         mkdir -p "${rootmnt}/live/${d##*/}"
954
955                         case d in
956                                 *.dir)
957                                         # do nothing # mount -o bind "${d}" "${rootmnt}/live/${d##*/}"
958                                         ;;
959
960                                 *)
961                                         mount --move "${d}" "${rootmnt}/live/${d##*/}"
962                                         ;;
963                         esac
964                 done
965         fi
966
967         # shows cow fs on /cow for use by live-snapshot
968         mkdir -p "${rootmnt}/live/cow"
969         mount -o bind /cow "${rootmnt}/live/cow"
970 }
971
972 check_dev ()
973 {
974         sysdev="${1}"
975         devname="${2}"
976
977         if [ -z "${devname}" ]
978         then
979                 devname=$(sys2dev "${sysdev}")
980         fi
981
982         if [ -n "${LIVE_MEDIA_OFFSET}" ]
983         then
984                 loopdevname=$(setup_loop "${devname}" "loop" "/sys/block/loop*" "${LIVE_MEDIA_OFFSET}" '')
985                 devname="${loopdevname}"
986         fi
987
988         fstype=$(get_fstype "${devname}")
989
990         if is_supported_fs ${fstype}
991         then
992                 mount -t ${fstype} -o ro "${devname}" $mountpoint || continue
993
994                 if is_live_path $mountpoint
995                 then
996                         echo $mountpoint
997                         return 0
998                 else
999                         umount $mountpoint
1000                 fi
1001         fi
1002
1003         if [ -n "${LIVE_MEDIA_OFFSET}" ]
1004         then
1005                 losetup -d "${loopdevname}"
1006         fi
1007
1008         return 1
1009 }
1010
1011 find_livefs ()
1012 {
1013         timeout="${1}"
1014
1015         # first look at the one specified in the command line
1016         if [ ! -z "${LIVE_MEDIA}" ]
1017         then
1018                 if check_dev "null" "${LIVE_MEDIA}"
1019                 then
1020                         return 0
1021                 fi
1022         fi
1023
1024         # don't start autodetection before timeout has expired
1025         if [ -n "${LIVE_MEDIA_TIMEOUT}" ]
1026         then
1027                 if [ "${timeout}" -lt "${LIVE_MEDIA_TIMEOUT}" ]
1028                 then
1029                         return 1
1030                 fi
1031         fi
1032
1033         # or do the scan of block devices
1034         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram)
1035         do
1036                 devname=$(sys2dev "${sysblock}")
1037                 fstype=$(get_fstype "${devname}")
1038
1039                 if /lib/udev/cdrom_id ${devname} > /dev/null
1040                 then
1041                         if check_dev "null" "${devname}"
1042                         then
1043                                 return 0
1044                         fi
1045                 elif is_nice_device "${sysblock}"
1046                 then
1047                         for dev in $(subdevices "${sysblock}")
1048                         do
1049                                 if check_dev "${dev}"
1050                                 then
1051                                         return 0
1052                                 fi
1053                         done
1054                 elif [ "${fstype}" = "squashfs" -o \
1055                         "${fstype}" = "ext2" -o \
1056                         "${fstype}" = "ext3" ]
1057                 then
1058                         # This is an ugly hack situation, the block device has
1059                         # an image directly on it.  It's hopefully
1060                         # live-initramfs, so take it and run with it.
1061                         ln -s "${devname}" "${devname}.${fstype}"
1062                         echo "${devname}.${fstype}"
1063                         return 0
1064                 fi
1065         done
1066
1067         return 1
1068 }
1069
1070 pulsate ()
1071 {
1072         if [ -x /sbin/usplash_write ]
1073         then
1074                 /sbin/usplash_write "PULSATE"
1075         fi
1076 }
1077
1078 set_usplash_timeout ()
1079 {
1080         if [ -x /sbin/usplash_write ]
1081         then
1082                 /sbin/usplash_write "TIMEOUT 120"
1083         fi
1084 }
1085
1086 mountroot ()
1087 {
1088         exec 6>&1
1089         exec 7>&2
1090         exec > live.log
1091         exec 2>&1
1092
1093         Arguments
1094
1095         set_usplash_timeout
1096         [ "$quiet" != "y" ] && log_begin_msg "Running /scripts/live-premount"
1097         pulsate
1098         run_scripts /scripts/live-premount
1099         [ "$quiet" != "y" ] && log_end_msg
1100
1101         # Needed here too because some things (*cough* udev *cough*)
1102         # changes the timeout
1103
1104         set_usplash_timeout
1105
1106         if [ ! -z "${NETBOOT}" ] || [ ! -z "${FETCH}" ]
1107         then
1108                 if do_netmount
1109                 then
1110                         livefs_root="${mountpoint}"
1111                 else
1112                         panic "Unable to find a live file system on the network"
1113                 fi
1114         else
1115                 # Scan local devices for the image
1116                 for i in 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
1117                 do
1118                         livefs_root=$(find_livefs $i)
1119
1120                         if [ -n "${livefs_root}" ]
1121                         then
1122                                 break
1123                         fi
1124
1125                         sleep 1
1126                 done
1127         fi
1128
1129         if [ -z "${livefs_root}" ]
1130         then
1131                 panic "Unable to find a medium containing a live file system"
1132         fi
1133
1134         if [ "${TORAM}" ]
1135         then
1136                 live_dest="ram"
1137         elif [ "${TODISK}" ]
1138         then
1139                 live_dest="${TODISK}"
1140         fi
1141
1142         if [ "${live_dest}" ]
1143         then
1144                 log_begin_msg "Copying live media to ${live_dest}"
1145                 copy_live_to "${livefs_root}" "${live_dest}"
1146                 log_end_msg
1147         fi
1148
1149         if [ -n "${MODULETORAMFILE}" ]
1150         then
1151                 setup_unionfs "${livefs_root}" "$rootmnt"
1152         else
1153                 mount_images_in_directory "${livefs_root}" "${rootmnt}"
1154         fi
1155
1156         log_end_msg
1157
1158         maybe_break live-bottom
1159         [ "$quiet" != "y" ] && log_begin_msg "Running /scripts/live-bottom"
1160
1161         pulsate
1162         run_scripts /scripts/live-bottom
1163         [ "$quiet" != "y" ] && log_end_msg
1164
1165         exec 1>&6 6>&-
1166         exec 2>&7 7>&-
1167         cp live.log "${rootmnt}/var/log/"
1168 }