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