Update 03_support_for_lzma.diff
[live-initramfs-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                         xvideomode=*)
400                                 XVIDEOMODE="${ARGUMENT#xvideomode=}"
401                                 export XVIDEOMODE
402                                 ;;
403                 esac
404         done
405
406         # sort of compatibility with netboot.h from linux docs
407         if [ -z "${NETBOOT}" ]
408         then
409                 if [ "${ROOT}" = "/dev/nfs" ]
410                 then
411                         NETBOOT="nfs"
412                         export NETBOOT
413                 elif [ "${ROOT}" = "/dev/cifs" ]
414                 then
415                         NETBOOT="cifs"
416                         export NETBOOT
417                 fi
418         fi
419
420         if [ -z "${MODULE}" ]
421         then
422                 MODULE="filesystem"
423                 export MODULE
424         fi
425
426         if [ -z "${UNIONTYPE}" ]
427         then
428                 UNIONTYPE="unionfs"
429                 export UNIONTYPE
430         fi
431 }
432
433 is_live_path ()
434 {
435         DIRECTORY="${1}"
436
437         if [ -d "${DIRECTORY}"/"${LIVE_MEDIA_PATH}" ]
438         then
439                 for FILESYSTEM in squashfs ext2 ext3 xfs dir
440                 do
441                         if [ "$(echo ${DIRECTORY}/${LIVE_MEDIA_PATH}/*.${FILESYSTEM})" != "${DIRECTORY}/${LIVE_MEDIA_PATH}/*.${FILESYSTEM}" ]
442                         then
443                                 return 0
444                         fi
445                 done
446         fi
447
448         return 1
449 }
450
451 matches_uuid ()
452 {
453         if [ "${IGNORE_UUID}" ] || [ ! -e /conf/uuid.conf ]
454         then
455                 return 0
456         fi
457
458         path="${1}"
459         uuid="$(cat /conf/uuid.conf)"
460
461         for try_uuid_file in "${mountpoint}/.disk/casper-uuid"*
462         do
463                 [ -e "${try_uuid_file}" ] || continue
464
465                 try_uuid="$(cat "${try_uuid_file}")"
466
467                 if [ "${uuid}" = "${try_uuid}" ]
468                 then
469                         return 0
470                 fi
471         done
472
473         return 1
474 }
475
476 get_backing_device ()
477 {
478         case "${1}" in
479                 *.squashfs|*.ext2|*.ext3)
480                         echo $(setup_loop "${1}" "loop" "/sys/block/loop*" '0' "${LIVE_MEDIA_ENCRYPTION}")
481                         ;;
482
483                 *.dir)
484                         echo "directory"
485                         ;;
486
487                 *)
488                         panic "Unrecognized live filesystem: ${1}"
489                         ;;
490         esac
491 }
492
493 match_files_in_dir ()
494 {
495         # Does any files match pattern ${1} ?
496         local pattern="${1}"
497
498         if [ "$(echo ${pattern})" != "${pattern}" ]
499         then
500                 return 0
501         fi
502
503         return 1
504 }
505
506 mount_images_in_directory ()
507 {
508         directory="${1}"
509         rootmnt="${2}"
510         mac="${3}"
511
512
513         if match_files_in_dir "${directory}/${LIVE_MEDIA_PATH}/*.squashfs" ||
514                 match_files_in_dir "${directory}/${LIVE_MEDIA_PATH}/*.ext2" ||
515                 match_files_in_dir "${directory}/${LIVE_MEDIA_PATH}/*.ext3" ||
516                 match_files_in_dir "${directory}/${LIVE_MEDIA_PATH}/*.dir"
517         then
518                 [ -n "${mac}" ] && adddirectory="${directory}/${LIVE_MEDIA_PATH}/${mac}"
519                 setup_unionfs "${directory}/${LIVE_MEDIA_PATH}" "${rootmnt}" "${adddirectory}"
520         else
521                 :
522         fi
523 }
524
525 is_nice_device ()
526 {
527         sysfs_path="${1#/sys}"
528
529         if /lib/udev/path_id "${sysfs_path}" | grep -E -q "ID_PATH=(usb|pci-[^-]*-[ide|scsi|usb])"
530         then
531                 return 0
532         fi
533
534         return 1
535 }
536
537 is_supported_fs ()
538 {
539         # FIXME: do something better like the scan of supported filesystems
540         fstype="${1}"
541
542         case ${fstype} in
543                 vfat|iso9660|udf|ext2|ext3|ntfs)
544                         return 0
545                         ;;
546         esac
547
548         return 1
549 }
550
551 copy_live_to ()
552 {
553         copyfrom="${1}"
554         copytodev="${2}"
555         copyto="${copyfrom}_swap"
556
557         if [ -z "${MODULETORAM}" ]
558         then
559                 size=$(fs_size "" ${copyfrom} "used")
560         else
561                 MODULETORAMFILE="${copyfrom}/${LIVE_MEDIA_PATH}/${MODULETORAM}"
562
563                 if [ -f "${MODULETORAMFILE}" ]
564                 then
565                         size=$( expr $(ls -la ${MODULETORAMFILE} | awk '{print $5}') / 1024 + 5000 )
566                 else
567                         log_warning_msg "Error: toram-module ${MODULETORAM} (${MODULETORAMFILE}) could not be read."
568                         return 1
569                 fi
570         fi
571
572         if [ "${copytodev}" = "ram" ]
573         then
574                 # copying to ram:
575                 freespace=$( expr $(awk '/MemFree/{print $2}' /proc/meminfo) + $( cat /proc/meminfo | grep Cached | head -n 1 | awk '/Cached/{print $2}' - ) )
576                 mount_options="-o size=${size}k"
577                 free_string="memory"
578                 fstype="tmpfs"
579                 dev="/dev/shm"
580         else
581                 # it should be a writable block device
582                 if [ -b "${copytodev}" ]
583                 then
584                         dev="${copytodev}"
585                         free_string="space"
586                         fstype=$(get_fstype "${dev}")
587                         freespace=$(fs_size "${dev}")
588                 else
589                         [ "$quiet" != "y" ] && log_warning_msg "${copytodev} is not a block device."
590                         return 1
591                 fi
592         fi
593
594         if [ "${freespace}" -lt "${size}" ]
595         then
596                 [ "${quiet}" != "y" ] && log_warning_msg "Not enough free ${free_string} (${freespace}k free, ${size}k needed) to copy live media in ${copytodev}."
597                 return 1
598         fi
599
600         # begin copying (or uncompressing)
601         mkdir "${copyto}"
602         echo "mount -t ${fstype} ${mount_options} ${dev} ${copyto}"
603         mount -t "${fstype}" ${mount_options} "${dev}" "${copyto}"
604
605         if [ "${extension}" = "tgz" ]
606         then
607                 cd "${copyto}"
608                 tar zxf "${copyfrom}/${LIVE_MEDIA_PATH}/$(basename ${FETCH})"
609                 rm -f "${copyfrom}/${LIVE_MEDIA_PATH}/$(basename ${FETCH})"
610                 mount -r --move "${copyto}" "${rootmnt}"
611                 cd "${OLDPWD}"
612         else
613                 if [ -n "${MODULETORAMFILE}" ]
614                 then
615                         cp ${MODULETORAMFILE} ${copyto} # copy only the filesystem module
616                 else
617                         cp -a ${copyfrom}/* ${copyto}   # "cp -a" from busybox also copies hidden files
618                 fi
619
620                 livefs_root
621                 umount ${copyfrom}
622                 mount -r --move ${copyto} ${copyfrom}
623         fi
624
625         rmdir ${copyto}
626         return 0
627 }
628
629 do_netmount ()
630 {
631         rc=1
632
633         modprobe -q af_packet # For DHCP
634
635         udevtrigger
636         udevsettle
637
638         ipconfig ${DEVICE} | tee /netboot.config
639
640         # source relevant ipconfig output
641         OLDHOSTNAME=${HOSTNAME}
642         . /tmp/net-${DEVICE}.conf
643         [ -z ${HOSTNAME} ] && HOSTNAME=${OLDHOSTNAME}
644         export HOSTNAME
645
646         if [ "${NFSROOT}" = "auto" ]
647         then
648                 NFSROOT=${ROOTSERVER}:${ROOTPATH}
649         fi
650
651         if [ -n "${FETCH}" ] && do_httpmount
652         then
653                 rc=0
654                 return ${rc}
655         fi
656
657         if [ "${NFSROOT#*:}" = "${NFSROOT}" ] && [ "$NETBOOT" != "cifs" ]
658         then
659                 NFSROOT=${ROOTSERVER}:${NFSROOT}
660         fi
661
662         [ "${quiet}" != "y" ] && log_begin_msg "Trying netboot from ${NFSROOT}"
663
664         if [ "${NETBOOT}" != "nfs" ] && do_cifsmount
665         then
666                 rc=0
667         elif do_nfsmount
668         then
669                 NETBOOT="nfs"
670                 export NETBOOT
671                 rc=0
672         fi
673
674         [ "${quiet}" != "y" ] && log_end_msg
675         return ${rc}
676 }
677
678 do_httpmount ()
679 {
680         rc=1
681         extension=$(echo "${FETCH}" | sed 's/\(.*\)\.\(.*\)/\2/')
682
683         case "${extension}" in
684                 squashfs|tgz|tar)
685                         [ "${quiet}" != "y" ] && log_begin_msg "Trying wget ${FETCH} -O ${mountpoint}/$(basename ${FETCH})"
686                         mkdir -p "${mountpoint}/${LIVE_MEDIA_PATH}"
687                         wget "${FETCH}" -O "${mountpoint}/${LIVE_MEDIA_PATH}/$(basename ${FETCH})"
688                         [ ${?} -eq 0 ] && rc=0
689                         [ "${extension}" = "tgz" ] && live_dest="ram"
690                         ;;
691
692                 *)
693                         [ "${quiet}" != "y" ] && log_begin_msg "Unrecognized archive extension for ${FETCH}"
694         esac
695
696         return ${rc}
697 }
698
699 do_nfsmount ()
700 {
701         rc=1
702
703         modprobe -q nfs
704
705         if [ -z "${NFSOPTS}" ]
706         then
707                 NFSOPTS=""
708         fi
709
710         [ "${quiet}" != "y" ] && log_begin_msg "Trying nfsmount -o nolock -o ro ${NFSOPTS} ${NFSROOT} ${mountpoint}"
711
712         # FIXME: This for loop is an ugly HACK round an nfs bug
713         for i in 0 1 2 3 4 5 6 7 8 9 a b c d e f 10 11 12 13
714         do
715                 nfsmount -o nolock -o ro ${NFSOPTS} "${NFSROOT}" "${mountpoint}" && rc=0 && break
716                 sleep 1
717         done
718
719         return ${rc}
720 }
721
722 do_cifsmount ()
723 {
724         rc=1
725
726         if [ -x "/sbin/mount.cifs" ]
727         then
728                 if [ -z "${NFSOPTS}" ]
729                 then
730                         CIFSOPTS="-ouser=root,password="
731                 else
732                         CIFSOPTS="${NFSOPTS}"
733                 fi
734
735                 [ "${quiet}" != "y" ] && log_begin_msg "Trying mount.cifs ${NFSROOT} ${mountpoint} ${CIFSOPTS}"
736                 modprobe -q cifs
737
738                 if mount.cifs "${NFSROOT}" "${mountpoint}" "${CIFSOPTS}"
739                 then
740                         rc=0
741                 fi
742         fi
743
744         return ${rc}
745 }
746
747 do_snap_copy ()
748 {
749         fromdev="${1}"
750         todir="${2}"
751         snap_type="${3}"
752         size=$(fs_size "${fromdev}" "" "used")
753
754         if [ -b "${fromdev}" ]
755         then
756                 # look for free mem
757                 if [ -n "${HOMEMOUNTED}" -a "${snap_type}" = "HOME" ]
758                 then
759                         todev=$(cat /proc/mounts | grep -s " $(base_path ${todir}) " | awk '{print $1}' )
760                         freespace=$(df -k  | grep -s ${todev} | awk '{print $4}')
761                 else
762                         freespace=$( expr $(awk '/MemFree/{print $2}' /proc/meminfo) + $( cat /proc/meminfo | grep Cached | head -n 1 | awk '/Cached/{print $2}' - ))
763                 fi
764
765                 tomount="/mnt/tmpsnap"
766
767                 if [ ! -d "${tomount}" ]
768                 then
769                         mkdir -p "${tomount}"
770                 fi
771
772                 fstype=$(get_fstype "${fromdev}")
773
774                 if [ -n "${fstype}" ]
775                 then
776                         # Copying stuff...
777                         mount -t "${fstype}" -o ro,noatime "${fromdev}" "${tomount}"
778                         cp -a "${tomount}"/* ${todir}
779                         umount "${tomount}"
780                 else
781                         log_warning_msg "Unrecognized fstype: ${fstype} on ${fromdev}:${snap_type}"
782                 fi
783
784                 rmdir "${tomount}"
785
786                 if echo ${fromdev} | grep -qs loop
787                 then
788                         losetup -d "${fromdev}"
789                 fi
790
791                 return 0
792         else
793                 return 1
794
795                 [ "${quiet}" != "y" ] && log_warning_msg "Unable to find the snapshot ${snap_type} medium"
796         fi
797 }
798
799 try_snap ()
800 {
801         # Look for ${snap_label}.* in block devices and copy the contents to ${snap_mount}
802         # and remember the device and filename for resync on exit in live-initramfs.init
803
804         snap_label="${1}"
805         snap_mount="${2}"
806         snap_type="${3}"
807         snapdata=$(find_files "${snap_label}.squashfs ${snap_label}.cpio.gz ${snap_label}.ext2 ${snap_label}.ext3")
808
809         if [ ! -z "${snapdata}" ]
810         then
811                 snapdev="$(echo ${snapdata} | cut -f1 -d ' ')"
812                 snapback="$(echo ${snapdata} | cut -f2 -d ' ')"
813                 snapfile="$(echo ${snapdata} | cut -f3 -d ' ')"
814
815                 if echo "${snapfile}" | grep -qs '\(squashfs\|ext2\|ext3\)'
816                 then
817                         # squashfs or ext2/ext3 snapshot
818                         dev=$(get_backing_device "${snapback}/${snapfile}")
819
820                         if ! do_snap_copy "${dev}" "${snap_mount}" "${snap_type}"
821                         then
822                                 log_warning_msg "Impossible to include the ${snapfile} Snapshot"
823                                 return 1
824                         fi
825                 else
826                         # cpio.gz snapshot
827                         if ! (cd "${snap_mount}" && zcat "${snapback}/${snapfile}" | cpio -i -u -d 2>/dev/null)
828                         then
829                                 log_warning_msg "Impossible to include the ${snapfile} Snapshot"
830                                 return 1
831                         fi
832                 fi
833
834                 umount "${snapback}"
835         else
836                 dev=$(find_cow_device "${snap_label}")
837
838                 if [ -b ${dev} ]
839                 then
840                         if echo "${dev}" | grep -qs loop
841                         then
842                                 # strange things happens, user confused?
843                                 snaploop=$( losetup ${dev} | awk '{print $3}' | tr -d '()' )
844                                 snapfile=$(basename ${snaploop})
845                                 snapdev=$(cat /proc/mounts | awk '{print $2,$1}' | grep -es "^$( dirname ${snaploop} )" | cut -f2 -d ' ')
846                         else
847                                 snapdev="${dev}"
848                         fi
849
850                         if ! do_snap_copy "${dev}" "${snap_mount}" "${snap_type}"
851                         then
852                                 log_warning_msg "Impossible to include the ${snap_label} Snapshot"
853                                 return 1
854                         else
855                                 if [ -n "${snapfile}" ]
856                                 then
857                                         # it was a loop device, user confused
858                                         umount ${snapdev}
859                                 fi
860                         fi
861                 else
862                         log_warning_msg "Impossible to include the ${snap_label} Snapshot"
863                         return 1
864                 fi
865         fi
866
867         echo "export ${snap_type}SNAP="${snap_mount}":${snapdev}:${snapfile}" >> /etc/live.conf # for resync on reboot/halt
868         return 0
869 }
870
871 setup_unionfs ()
872 {
873         image_directory="${1}"
874         rootmnt="${2}"
875         addimage_directory="${3}"
876         modprobe -q -b ${UNIONTYPE}
877
878         # run-init can't deal with images in a subdir, but we're going to
879         # move all of these away before it runs anyway.  No, we're not,
880         # put them in / since move-mounting them into / breaks mono and
881         # some other apps.
882
883         croot="/"
884
885         # Let's just mount the read-only file systems first
886         rofsstring=""
887         rofslist=""
888         minor_kernel_version=$(uname -r|cut -c 5-|sed 's/[^0-9].*//')
889
890         if [ "${NETBOOT}" = "nfs" ] && [ "${minor_kernel_version}" -lt 22 ]
891         then
892                 # go aroung a bug in nfs-unionfs locking for unionfs <= 1.4
893                 roopt="nfsro"
894         else
895                 roopt="ro"
896         fi
897
898         # Read image names from ${MODULE}.module if it exists
899         if [ -e "${image_directory}/filesystem.${MODULE}.module" ]
900         then
901                 for IMAGE in $(cat ${image_directory}/filesystem.${MODULE}.module)
902                 do
903                         image_string="${image_string} ${image_directory}/${IMAGE}"
904                 done
905         elif [ -e "${image_directory}/${MODULE}.module" ]
906         then
907                 for IMAGE in $(cat ${image_directory}/${MODULE}.module)
908                 do
909                         image_string="${image_string} ${image_directory}/${IMAGE}"
910                 done
911         else
912                 # ${MODULE}.module does not exist, create a list of images
913                 for FILESYSTEM in squashfs ext2 ext3 xfs dir
914                 do
915                         for IMAGE in "${image_directory}"/*."${FILESYSTEM}"
916                         do
917                                 if [ -e "${IMAGE}" ]
918                                 then
919                                         image_string="${image_string} ${IMAGE}"
920                                 fi
921                         done
922                 done
923
924                 if [ -n "${addimage_directory}" ] && [ -d "${addimage_directory}" ]
925                 then
926                         for FILESYSTEM in squashfs ext2 ext3 xfs dir
927                         do
928                                 for IMAGE in "${addimage_directory}"/*."${FILESYSTEM}"
929                                 do
930                                         if [ -e "${IMAGE}" ]
931                                         then
932                                                 image_string="${image_string} ${IMAGE}"
933                                         fi
934                                 done
935                         done
936                 fi
937
938                 # Now sort the list
939                 image_string="$(echo ${image_string} | sed -e 's/ /\n/g' | sort )"
940         fi
941
942         [ -n "${MODULETORAMFILE}" ] && image_string="${image_directory}/$(basename ${MODULETORAMFILE})"
943
944         mkdir -p "${croot}"
945
946         for image in ${image_string}
947         do
948                 imagename=$(basename "${image}")
949
950                 if [ -d "${image}" ]
951                 then
952                         # it is a plain directory: do nothing
953                         rofsstring="${image}=${roopt}:${rofsstring}"
954                         rofslist="${image} ${rofslist}"
955                 elif [ -f "${image}" ]
956                 then
957                         backdev=$(get_backing_device "${image}")
958                         fstype=$(get_fstype "${backdev}")
959
960                         if [ "${fstype}" = "unknown" ]
961                         then
962                                 panic "Unknown file system type on ${backdev} (${image})"
963                         fi
964
965                         mkdir -p "${croot}/${imagename}"
966                         echo "debug: Can not mount backdev ${backdev} (image = ${image}) on croot/imagename ${croot}/${imagename}"
967                         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}"
968                 fi
969         done
970
971         rofsstring=${rofsstring%:}
972
973         mkdir -p /cow
974
975         # Looking for "${root_persistence}" device or file
976         if [ -n "${PERSISTENT}" ]
977         then
978                 cowprobe=$(find_cow_device "${root_persistence}")
979
980                 if [ -b "${cowprobe}" ]
981                 then
982                         cowdevice=${cowprobe}
983                         cow_fstype=$(get_fstype "${cowprobe}")
984                 else
985                         [ "${quiet}" != "y" ] && log_warning_msg "Unable to find the persistent medium"
986                         cowdevice="tmpfs"
987                         cow_fstype="tmpfs"
988                 fi
989         elif [ -n "${NFS_COW}" ]
990         then
991                 # check if there are any nfs options
992                 if echo ${NFS_COW}|grep -q ','
993                 then
994                         nfs_cow_opts="-o nolock,$(echo ${NFS_COW}|cut -d, -f2-)"
995                         nfs_cow=$(echo ${NFS_COW}|cut -d, -f1)
996                 else
997                         nfs_cow_opts="-o nolock"
998                         nfs_cow=${NFS_COW}
999                 fi
1000                 mac="$(get_mac)"
1001                 if [ -n "${mac}" ]
1002                 then
1003                         cowdevice=$(echo ${nfs_cow}|sed "s/client_mac_address/${mac}/")
1004                         cow_fstype="nfs"
1005                 else
1006                         panic "unable to determine mac address"
1007                 fi
1008         else
1009                 cowdevice="tmpfs"
1010                 cow_fstype="tmpfs"
1011         fi
1012
1013         if [ "${cow_fstype}" = "nfs" ]
1014         then
1015                 [ "${quiet}" != "y" ] && log_begin_msg \
1016                         "Trying nfsmount ${nfs_cow_opts} ${cowdevice} /cow"
1017                 nfsmount ${nfs_cow_opts} ${cowdevice} /cow || \
1018                         panic "Can not mount ${cowdevice} on /cow"
1019         else
1020                 mount ${cowdevice} -t ${cow_fstype} -o rw,noatime /cow || \
1021                         panic "Can not mount ${cowdevice} on /cow"
1022         fi
1023
1024         rofscount=$(echo ${rofslist} |wc -w)
1025
1026         if [ -n "${EXPOSED_ROOT}" ]
1027         then
1028                 if [ ${rofscount} -ne 1 ]
1029                 then
1030                         panic "only one RO file system supported with exposedroot: ${rofslist}"
1031                 fi
1032                 exposedrootfs=${rofslist%% }
1033
1034                 mount --bind ${exposedrootfs} ${rootmnt} || \
1035                         panic "bind mount of ${exposedrootfs} failed"
1036
1037                 cow_dirs='/var/tmp /var/lock /var/run /var/log /var/spool
1038                         /home /var/lib/live'
1039
1040                 for dir in ${cow_dirs}; do
1041                         mkdir -p /cow${dir}
1042                         mount -t ${UNIONTYPE} \
1043                                 -o rw,noatime,dirs=/cow${dir}=rw:${exposedrootfs}${dir}=ro \
1044                                 ${UNIONTYPE} "${rootmnt}${dir}" || \
1045                                 panic "mount ${UNIONTYPE} on ${rootmnt}${dir} failed with option \
1046                                         rw,noatime,dirs=/cow${dir}=rw:${exposedrootfs}${dir}=ro"
1047                 done
1048         else
1049                 mount -t ${UNIONTYPE} -o noatime,dirs=/cow=rw:${rofsstring} \
1050                         ${UNIONTYPE} "${rootmnt}" || panic "mount ${UNIONTYPE} on \
1051                         ${rootmnt} failed with option noatime,dirs=/cow=rw:${rofsstring}"
1052         fi
1053
1054         # tmpfs file systems
1055         mkdir -p "${rootmnt}/live"
1056         mount -t tmpfs tmpfs ${rootmnt}/live
1057
1058         # Adding other custom mounts
1059         if [ -n "${PERSISTENT}" ]
1060         then
1061                 # directly mount /home
1062                 # FIXME: add a custom mounts configurable system
1063                 homecow=$(find_cow_device "${home_persistence}" )
1064
1065                 if [ -b "${homecow}" ]
1066                 then
1067                         mount -t $(get_fstype "${homecow}") -o rw,noatime "${homecow}" "${rootmnt}/home"
1068                         export HOMEMOUNTED=1 # used to proper calculate free space in do_snap_copy()
1069                 else
1070                         [ "${quiet}" != "y" ] && log_warning_msg "Unable to find the persistent home medium"
1071                 fi
1072
1073                 # Look for other snapshots to copy in
1074                 try_snap "${root_snapshot_label}" "${rootmnt}" "ROOT"
1075                 try_snap "${home_snapshot_label}" "${rootmnt}/home" "HOME"
1076         fi
1077
1078         if [ -n "${SHOWMOUNTS}" ]
1079         then
1080                 for d in ${rofslist}
1081                 do
1082                         mkdir -p "${rootmnt}/live/${d##*/}"
1083
1084                         case d in
1085                                 *.dir)
1086                                         # do nothing # mount -o bind "${d}" "${rootmnt}/live/${d##*/}"
1087                                         ;;
1088
1089                                 *)
1090                                         mount --move "${d}" "${rootmnt}/live/${d##*/}"
1091                                         ;;
1092                         esac
1093                 done
1094         fi
1095
1096         # shows cow fs on /cow for use by live-snapshot
1097         mkdir -p "${rootmnt}/live/cow"
1098         mount -o move /cow "${rootmnt}/live/cow"
1099 }
1100
1101 check_dev ()
1102 {
1103         sysdev="${1}"
1104         devname="${2}"
1105         skip_uuid_check="${3}"
1106
1107         if [ -z "${devname}" ]
1108         then
1109                 devname=$(sys2dev "${sysdev}")
1110         fi
1111
1112         if [ -n "${LIVE_MEDIA_OFFSET}" ]
1113         then
1114                 loopdevname=$(setup_loop "${devname}" "loop" "/sys/block/loop*" "${LIVE_MEDIA_OFFSET}" '')
1115                 devname="${loopdevname}"
1116         fi
1117
1118         fstype=$(get_fstype "${devname}")
1119
1120         if is_supported_fs ${fstype}
1121         then
1122                 mount -t ${fstype} -o ro,noatime "${devname}" ${mountpoint} || continue
1123
1124                 if is_live_path ${mountpoint} && \
1125                         ([ "${skip_uuid_check}" ] || matches_uuid ${mountpoint})
1126                 then
1127                         echo ${mountpoint}
1128                         return 0
1129                 else
1130                         umount ${mountpoint}
1131                 fi
1132         fi
1133
1134         if [ -n "${LIVE_MEDIA_OFFSET}" ]
1135         then
1136                 losetup -d "${loopdevname}"
1137         fi
1138
1139         return 1
1140 }
1141
1142 find_livefs ()
1143 {
1144         timeout="${1}"
1145
1146         # first look at the one specified in the command line
1147         if [ ! -z "${LIVE_MEDIA}" ]
1148         then
1149                 if check_dev "null" "${LIVE_MEDIA}" "skip_uuid_check"
1150                 then
1151                         return 0
1152                 fi
1153         fi
1154
1155         # don't start autodetection before timeout has expired
1156         if [ -n "${LIVE_MEDIA_TIMEOUT}" ]
1157         then
1158                 if [ "${timeout}" -lt "${LIVE_MEDIA_TIMEOUT}" ]
1159                 then
1160                         return 1
1161                 fi
1162         fi
1163
1164         # or do the scan of block devices
1165         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram)
1166         do
1167                 devname=$(sys2dev "${sysblock}")
1168                 fstype=$(get_fstype "${devname}")
1169
1170                 if /lib/udev/cdrom_id ${devname} > /dev/null
1171                 then
1172                         if check_dev "null" "${devname}"
1173                         then
1174                                 return 0
1175                         fi
1176                 elif is_nice_device "${sysblock}"
1177                 then
1178                         for dev in $(subdevices "${sysblock}")
1179                         do
1180                                 if check_dev "${dev}"
1181                                 then
1182                                         return 0
1183                                 fi
1184                         done
1185                 elif [ "${fstype}" = "squashfs" -o \
1186                         "${fstype}" = "ext2" -o \
1187                         "${fstype}" = "ext3" ]
1188                 then
1189                         # This is an ugly hack situation, the block device has
1190                         # an image directly on it.  It's hopefully
1191                         # live-initramfs, so take it and run with it.
1192                         ln -s "${devname}" "${devname}.${fstype}"
1193                         echo "${devname}.${fstype}"
1194                         return 0
1195                 fi
1196         done
1197
1198         return 1
1199 }
1200
1201 pulsate ()
1202 {
1203         if [ -x /sbin/usplash_write ]
1204         then
1205                 /sbin/usplash_write "PULSATE"
1206         fi
1207 }
1208
1209 set_usplash_timeout ()
1210 {
1211         if [ -x /sbin/usplash_write ]
1212         then
1213                 /sbin/usplash_write "TIMEOUT 120"
1214         fi
1215 }
1216
1217 mountroot ()
1218 {
1219         exec 6>&1
1220         exec 7>&2
1221         exec > live.log
1222         exec 2>&1
1223
1224         Arguments
1225
1226         set_usplash_timeout
1227         [ "${quiet}" != "y" ] && log_begin_msg "Running /scripts/live-premount"
1228         pulsate
1229         run_scripts /scripts/live-premount
1230         [ "${quiet}" != "y" ] && log_end_msg
1231
1232         # Needed here too because some things (*cough* udev *cough*)
1233         # changes the timeout
1234
1235         set_usplash_timeout
1236
1237         if [ ! -z "${NETBOOT}" ] || [ ! -z "${FETCH}" ]
1238         then
1239                 if do_netmount
1240                 then
1241                         livefs_root="${mountpoint}"
1242                 else
1243                         panic "Unable to find a live file system on the network"
1244                 fi
1245         else
1246                 # Scan local devices for the image
1247                 for i in 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
1248                 do
1249                         livefs_root=$(find_livefs ${i})
1250
1251                         if [ -n "${livefs_root}" ]
1252                         then
1253                                 break
1254                         fi
1255
1256                         sleep 1
1257                 done
1258         fi
1259
1260         if [ -z "${livefs_root}" ]
1261         then
1262                 panic "Unable to find a medium containing a live file system"
1263         fi
1264
1265         if [ "${TORAM}" ]
1266         then
1267                 live_dest="ram"
1268         elif [ "${TODISK}" ]
1269         then
1270                 live_dest="${TODISK}"
1271         fi
1272
1273         if [ "${live_dest}" ]
1274         then
1275                 log_begin_msg "Copying live media to ${live_dest}"
1276                 copy_live_to "${livefs_root}" "${live_dest}"
1277                 log_end_msg
1278         fi
1279
1280         if [ -n "${MODULETORAMFILE}" ]
1281         then
1282                 setup_unionfs "${livefs_root}" "${rootmnt}"
1283         else
1284                 mac="$(get_mac)"
1285                 mac="$(echo ${mac} | sed 's/-//g')"
1286                 mount_images_in_directory "${livefs_root}" "${rootmnt}" "${mac}"
1287         fi
1288
1289         log_end_msg
1290
1291         maybe_break live-bottom
1292         [ "${quiet}" != "y" ] && log_begin_msg "Running /scripts/live-bottom"
1293
1294         pulsate
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 }