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