Adding live-initramfs 1.87.4-1.
[live-boot-grml.git] / scripts / live
1 #!/bin/sh
2
3 # set -e
4
5 export PATH=/root/usr/bin:/root/usr/sbin:/root/bin:/root/sbin:/usr/bin:/usr/sbin:/bin:/sbin
6
7 echo "/root/lib" >> /etc/ld.so.conf
8 echo "/root/usr/lib" >> /etc/ld.so.conf
9
10 mountpoint=/live_media
11
12 root_persistence="live-rw"
13 home_persistence="home-rw"
14 root_snapshot_label="live-sn"
15 home_snapshot_label="home-sn"
16
17 USERNAME=user
18 USERFULLNAME="Live user"
19 HOSTNAME=host
20 BUILD_SYSTEM=Custom
21
22 mkdir -p $mountpoint
23
24 [ -f /etc/live.conf ] && . /etc/live.conf
25 export USERNAME USERFULLNAME HOSTNAME BUILD_SYSTEM
26
27 . /scripts/live-helpers
28
29 if [ ! -f /live.vars ]; then
30     touch /live.vars
31 fi
32
33 parse_cmdline ()
34 {
35     PRESEEDS=
36     # looking for live-initramfs specifics options as kernel parameters
37     for x in $(cat /proc/cmdline); do
38         case $x in
39             userfullname=*)
40                 export USERFULLNAME=${x#userfullname=}
41                 export LIVECONF="changed"
42                 ;;
43             hostname=*)
44                 export HOSTNAME=${x#hostname=}
45                 export LIVECONF="changed"
46                 ;;
47             username=*)
48                 export USERNAME=${x#username=}
49                 export LIVECONF="changed"
50                 ;;
51             netboot*)
52                 export NETBOOT=${x#netboot=} ;;
53             access=*)
54                 export ACCESS=${x#access=} ;;
55             xdebconf)
56                 export XDEBCONF="Yes" ;;
57             xvideomode=*)
58                 export XVIDEOMODE="${x#xvideomode=}" ;;
59             toram)
60                 export TORAM="Yes" ;;
61             todisk=*)
62                 export TODISK=${x#todisk=} ;;
63             noswap)
64                 export NOSWAP="Yes" ;;
65             noautologin)
66                 export NOAUTOLOGIN="Yes" ;;
67             noxautologin)
68                 export NOXAUTOLOGIN="Yes" ;;
69             nosudo)
70                 export NOSUDO="Yes" ;;
71             showmounts)
72                 export SHOWMOUNTS="Yes" ;;
73             persistent)
74                 export PERSISTENT="Yes" ;;
75             nopersistent)
76                 export PERSISTENT="" ;;
77             ip*)
78                 STATICIP=${x#ip=}
79                 if [ "${STATICIP}" == "" ]; then
80                     STATICIP="frommedia"
81                 fi
82                 export STATICIP ;;
83             live-getty)
84                 export LIVE_GETTY=1 ;;
85             bootfrom=*|live-media=*)
86                 export LIVE_MEDIA=${x#*=} ;;
87             live-media-encryption=*|encryption=*)
88                 export LIVE_MEDIA_ENCRYPTION=${x#*=} ;;
89             live-media-timeout=*)
90                 export LIVE_MEDIA_TIMEOUT=${x#live-media-timeout=} ;;
91             live-media-offset=*)
92                 export LIVE_MEDIA_OFFSET=${x#live-media-offset=} ;;
93             locale=*|debian-installer/locale=*)
94                 export LOCALE=${x#*=} ;;
95             keyb=*|kbd-chooser/method=*)
96                 export KBD=${x#*=} ;;
97             klayout=*|console-setup/layoutcode=*)
98                 export KLAYOUT=${x#*=} ;;
99             koptions=*)
100                 export KOPTIONS=${x#koptions=} ;;
101             kvariant=*|console-setup/variantcode=*)
102                 export KVARIANT=${x#*=} ;;
103             kmodel=*|console-setup/modelcode=*)
104                 export KMODEL=${x#*=} ;;
105             module=*)
106                 export MODULE=${x#module=} ;;
107             preseed/file=*|file=*)
108                 export LOCATION="${x#*=}" ;;
109             */*=*)
110                 question="${x%%=*}"
111                 value="${x#*=}"
112                 PRESEEDS="${PRESEEDS}\"${question}=${value}\" "
113                 ;;
114             console=*)
115                 export DEFCONSOLE="${x#*=}" ;;
116         esac
117     done
118
119     # sort of compatibility with netboot.h from linux docs
120     if [ -z "${NETBOOT}" ]; then
121         if [ "${ROOT}" == "/dev/nfs" ]; then
122             NETBOOT="nfs"
123             export NETBOOT
124         elif [ "${ROOT}" == "/dev/cifs" ]; then
125             NETBOOT="cifs"
126             export NETBOOT
127         fi
128     fi
129
130     if [ -z "${MODULE}" ]; then
131         MODULE=order
132     fi
133 }
134
135 is_live_path() {
136     path=$1
137     if [ -d "$path/live" ]; then
138         if [ "$(echo $path/live/*.squashfs)" != "$path/live/*.squashfs" ] ||
139             [ "$(echo $path/live/*.ext2)" != "$path/live/*.ext2" ] ||
140             [ "$(echo $path/live/*.ext3)" != "$path/live/*.ext3" ] ||
141             [ "$(echo $path/live/*.dir)" != "$path/live/*.dir" ]; then
142             return 0
143         fi
144     fi
145     return 1
146 }
147
148 get_backing_device() {
149     case "$1" in
150         *.squashfs|*.ext2|*.ext3)
151             echo $(setup_loop "$1" "loop" "/sys/block/loop*" '0' "${LIVE_MEDIA_ENCRYPTION}")
152             ;;
153         *.dir)
154             echo "directory"
155             ;;
156         *)
157             panic "Unrecognized live filesystem: $1"
158             ;;
159     esac
160 }
161
162 match_files_in_dir() {
163     # Does any files match pattern $1 ?
164
165     local pattern="$1"
166     if [ "$(echo $pattern)" != "$pattern" ]; then
167         return 0
168     fi
169     return 1
170 }
171
172 mount_images_in_directory() {
173     directory="$1"
174     rootmnt="$2"
175     if match_files_in_dir "$directory/live/*.squashfs" ||
176         match_files_in_dir "$directory/live/*.ext2" ||
177         match_files_in_dir "$directory/live/*.ext3" ||
178         match_files_in_dir "$directory/live/*.dir"; then
179         setup_unionfs "$directory/live" "$rootmnt"
180     else
181         :
182     fi
183 }
184
185 is_nice_device() {
186     sysfs_path="${1#/sys}"
187     if /lib/udev/path_id "${sysfs_path}" | grep -E -q "ID_PATH=(usb|pci-[^-]*-[ide|scsi|usb])"; then
188         return 0
189     fi
190     return 1
191 }
192
193 is_supported_fs () {
194     # FIXME: do something better like the scan of supported filesystems
195     fstype="${1}"
196     case ${fstype} in
197         vfat|iso9660|udf|ext2|ext3|ntfs)
198             return 0
199             ;;
200     esac
201     return 1
202 }
203
204 copy_live_to() {
205     copyfrom="${1}"
206     copytodev="${2}"
207     copyto="${copyfrom}_swap"
208
209     size=$(fs_size "" ${copyfrom} "used")
210
211     if [ "${copytodev}" = "ram" ]; then
212         # copying to ram:
213         freespace=$( expr $(awk '/MemFree/{print $2}' /proc/meminfo) + $( cat /proc/meminfo | grep Cached | head -n 1 | awk '/Cached/{print $2}' - ) )
214         mount_options="-o size=${size}k"
215         free_string="memory"
216         fstype="tmpfs"
217         dev="/dev/shm"
218     else
219         # it should be a writable block device
220         if [ -b "${copytodev}" ]; then
221             dev="${copytodev}"
222             free_string="space"
223             fstype=$(get_fstype "${dev}")
224             freespace=$(fs_size "${dev}")
225         else
226             [ "$quiet" != "y" ] && log_warning_msg "${copytodev} is not a block device."
227             return 1
228         fi
229     fi
230     if [ "${freespace}" -lt "${size}" ] ; then
231         [ "$quiet" != "y" ] && log_warning_msg "Not enough free ${free_string} (${freespace}k > ${size}k) to copy live media in ${copytodev}."
232         return 1
233     fi
234
235     # begin copying..
236     mkdir "${copyto}"
237     echo "mount -t ${fstype} ${mount_options} ${dev} ${copyto}"
238     mount -t "${fstype}" ${mount_options} "${dev}" "${copyto}"
239     cp -a ${copyfrom}/* ${copyto} # "cp -a" from busybox also copies hidden files
240     umount ${copyfrom}
241     mount -r --move ${copyto} ${copyfrom}
242     rmdir ${copyto}
243     return 0
244 }
245
246 do_netmount() {
247     rc=1
248
249     modprobe "${MP_QUIET}" af_packet # For DHCP
250
251     udevtrigger
252     udevsettle
253
254     ipconfig ${DEVICE} /tmp/net-${DEVICE}.conf | tee /netboot.config
255
256     if [ "${NFSROOT}" = "auto" ]; then
257         NFSROOT=${ROOTSERVER}:${ROOTPATH}
258     fi
259
260     [ "$quiet" != "y" ] && log_begin_msg "Trying netboot from ${NFSROOT}"
261
262     if [ "${NETBOOT}" != "nfs" ] && do_cifsmount ; then
263         rc=0
264     elif do_nfsmount ; then
265         NETBOOT="nfs"
266         export NETBOOT
267         rc=0
268     fi
269
270     [ "$quiet" != "y" ] && log_end_msg
271     return ${rc}
272 }
273
274 do_nfsmount() {
275     rc=1
276     modprobe "${MP_QUIET}" nfs
277     if [ -z "${NFSOPTS}" ]; then
278         NFSOPTS=""
279     fi
280
281     [ "$quiet" != "y" ] && log_begin_msg "Trying nfsmount -o nolock -o ro ${NFSOPTS} ${NFSROOT} ${mountpoint}"
282     # FIXME: This for loop is an ugly HACK round an nfs bug
283     for i in 0 1 2 3 4 5 6 7 8 9 a b c d e f 10 11 12 13; do
284         nfsmount -o nolock -o ro ${NFSOPTS} "${NFSROOT}" "${mountpoint}" && rc=0 && break
285         sleep 1
286     done
287     return ${rc}
288 }
289
290 do_cifsmount() {
291     rc=1
292     if [ -x "/sbin/mount.cifs" ]; then
293         if [ -z "${NFSOPTS}" ]; then
294             CIFSOPTS="-ouser=root,password="
295         else
296             CIFSOPTS="${NFSOPTS}"
297         fi
298
299         [ "$quiet" != "y" ] && log_begin_msg "Trying mount.cifs ${NFSROOT} ${mountpoint} ${CIFSOPTS}"
300         modprobe "${MP_QUIET}" cifs
301
302         if mount.cifs "${NFSROOT}" "${mountpoint}" "${CIFSOPTS}" ; then
303             rc=0
304         fi
305     fi
306     return ${rc}
307 }
308
309 do_snap_copy ()
310 {
311     fromdev="${1}"
312     todir="${2}"
313     snap_type="${3}"
314
315     size=$(fs_size "${fromdev}" "" "used")
316
317     if [ -b "${fromdev}" ]; then
318         # look for free mem
319         if [ -n "${HOMEMOUNTED}" -a "${snap_type}" = "HOME" ]; then
320             todev=$(cat /proc/mounts | grep -s " $(base_path ${todir}) " | awk '{print $1}' )
321             freespace=$(df -k  | grep -s ${todev} | awk '{print $4}')
322         else
323             freespace=$( expr $(awk '/MemFree/{print $2}' /proc/meminfo) + $( cat /proc/meminfo | grep Cached | head -n 1 | awk '/Cached/{print $2}' - ))
324         fi
325
326         tomount="/mnt/tmpsnap"
327         if [ ! -d "${tomount}" ] ; then
328             mkdir -p "${tomount}"
329         fi
330
331         fstype=$(get_fstype "${fromdev}")
332         if [ -n "${fstype}" ]; then
333             # Copying stuff...
334             mount -t "${fstype}" -o ro "${fromdev}" "${tomount}"
335             cp -a "${tomount}"/* ${todir}
336             umount "${tomount}"
337         else
338             log_warning_msg "Unrecognized fstype: ${fstype} on ${fromdev}:${snap_type}"
339         fi
340
341         rmdir "${tomount}"
342         if echo ${fromdev} | grep -qs loop; then
343            losetup -d "${fromdev}"
344         fi
345         return 0
346     else
347         return 1
348         [ "$quiet" != "y" ] && log_warning_msg "Unable to find the snapshot ${snap_type} medium"
349     fi
350 }
351
352 try_snap ()
353 {
354     # Look for $snap_label.* in block devices and copy the contents to $snap_mount
355     #   and remember the device and filename for resync on exit in live-initramfs.init
356
357     snap_label="${1}"
358     snap_mount="${2}"
359     snap_type="${3}"
360
361     snapdata=$(find_files "${snap_label}.squashfs ${snap_label}.cpio.gz ${snap_label}.ext2 ${snap_label}.ext3")
362     if [ ! -z "${snapdata}" ]; then
363         snapdev="$(echo ${snapdata} | cut -f1 -d ' ')"
364         snapback="$(echo ${snapdata} | cut -f2 -d ' ')"
365         snapfile="$(echo ${snapdata} | cut -f3 -d ' ')"
366         if echo "${snapfile}" | grep -qs '\(squashfs\|ext2\|ext3\)'; then
367             # squashfs or ext2/ext3 snapshot
368             dev=$(get_backing_device "${snapback}/${snapfile}")
369             if ! do_snap_copy "${dev}" "${snap_mount}" "${snap_type}"; then
370                  log_warning_msg "Impossible to include the ${snapfile} Snapshot"
371                  return 1
372             fi
373         else
374             # cpio.gz snapshot
375             if ! (cd "${snap_mount}" && zcat "${snapback}/${snapfile}" | cpio -i -u -d 2>/dev/null) ; then
376                 log_warning_msg "Impossible to include the ${snapfile} Snapshot"
377                 return 1
378             fi
379         fi
380         umount "${snapback}"
381     else
382         dev=$(find_cow_device "${snap_label}")
383         if [ -b ${dev} ]; then
384             if echo "${dev}" | grep -qs loop; then
385                 # strange things happens, user confused?
386                 snaploop=$( losetup ${dev} | awk '{print $3}' | tr -d '()' )
387                 snapfile=$(basename ${snaploop})
388                 snapdev=$(cat /proc/mounts | awk '{print $2,$1}' | grep -es "^$( dirname ${snaploop} )" | cut -f2 -d ' ')
389             else
390                 snapdev="${dev}"
391             fi
392             if ! do_snap_copy "${dev}" "${snap_mount}" "${snap_type}" ; then
393                 log_warning_msg "Impossible to include the ${snap_label} Snapshot"
394                 return 1
395             else
396                 if [ -n "${snapfile}" ]; then
397                      # it was a loop device, user confused
398                      umount ${snapdev}
399                 fi
400             fi
401         else
402             log_warning_msg "Impossible to include the ${snap_label} Snapshot"
403             return 1
404         fi
405     fi
406     echo "export ${snap_type}SNAP="${snap_mount}":${snapdev}:${snapfile}" >> /etc/live.conf # for resync on reboot/halt
407     return 0
408 }
409
410 setup_unionfs() {
411     image_directory="$1"
412     rootmnt="$2"
413
414     modprobe "${MP_QUIET}" -b unionfs
415
416     # run-init can't deal with images in a subdir, but we're going to
417     # move all of these away before it runs anyway.  No, we're not,
418     # put them in / since move-mounting them into / breaks mono and
419     # some other apps.
420
421     croot="/"
422
423     # Let's just mount the read-only file systems first
424     rofsstring=""
425     rofslist=""
426     if [ "${NETBOOT}" = "nfs" ] ; then
427         roopt="nfsro" # go aroung a bug in nfs-unionfs locking
428     else
429         roopt="ro"
430     fi
431
432     # Read image names from ${MODULE}.lst if it exists
433     if [ -e "${image_directory}/${MODULE}.lst" ]; then
434         for image in $(cat "${image_directory}/${MODULE}.lst"); do
435             image_string="${image_string} ${image_directory}/${image}";
436         done
437     else
438         # If ${MODULE}.lst does not exist, create a list of images
439         for image_type in "ext2" "ext3" "squashfs" "dir"; do
440             for image in "${image_directory}"/*."${image_type}"; do
441                 if [ -e "${image}" ]; then
442                     image_string="${image_string} ${image}";
443                 fi
444             done
445         done
446         # Now sort the list
447         image_string=$(echo ${image_string} | sed -e 's/ /\n/g' | sort )
448     fi
449
450     mkdir -p "${croot}"
451     for image in ${image_string}; do
452         imagename=$(basename "${image}")
453         if [ -d "${image}" ]; then
454             # it is a plain directory: do nothing
455             rofsstring="${image}=${roopt}:${rofsstring}"
456             rofslist="${image} ${rofslist}"
457         elif [ -f "${image}" ]; then
458             backdev=$(get_backing_device "$image")
459             fstype=$(get_fstype "${backdev}")
460             if [ "${fstype}" = "unknown" ]; then
461                 panic "Unknown file system type on ${backdev} (${image})"
462              fi
463             mkdir -p "${croot}/${imagename}"
464             mount -t "${fstype}" -o ro "${backdev}" "${croot}/${imagename}" || panic "Can not mount $backdev ($image) on ${croot}/${imagename}" && rofsstring="${croot}/${imagename}=${roopt}:${rofsstring}" && rofslist="${croot}/${imagename} ${rofslist}"
465         fi
466     done
467     rofsstring=${rofsstring%:}
468
469     mkdir -p /cow
470     cowdevice="tmpfs"
471     cow_fstype="tmpfs"
472
473     # Looking for "${root_persistence}" device or file
474     if [ -n "${PERSISTENT}" ]; then
475         cowprobe=$(find_cow_device "${root_persistence}")
476         if [ -b "${cowprobe}" ]; then
477             cowdevice=${cowprobe}
478             cow_fstype=$(get_fstype "${cowprobe}")
479         else
480             [ "$quiet" != "y" ] && log_warning_msg "Unable to find the persistent medium"
481         fi
482     fi
483
484     mount ${cowdevice} -t ${cow_fstype} -o rw /cow || panic "Can not mount $cowdevice on /cow"
485
486     mount -t unionfs -o dirs=/cow=rw:$rofsstring unionfs "$rootmnt" || panic "Unionfs mount failed"
487
488     # Adding other custom mounts
489     if [ -n "${PERSISTENT}" ]; then
490         # directly mount /home
491         # FIXME: add a custom mounts configurable system
492         homecow=$(find_cow_device "${home_persistence}" )
493         if [ -b "${homecow}" ]; then
494             mount -t $(get_fstype "${homecow}") -o rw "${homecow}" "${rootmnt}/home"
495             export HOMEMOUNTED=1 # used to proper calculate free space in do_snap_copy()
496         else
497             [ "$quiet" != "y" ] && log_warning_msg "Unable to find the persistent home medium"
498         fi
499         # Look for other snapshots to copy in
500         try_snap "${root_snapshot_label}" "${rootmnt}" "ROOT"
501         try_snap "${home_snapshot_label}" "${rootmnt}/home" "HOME"
502     fi
503
504     if [ -n "${SHOWMOUNTS}" ]; then
505         for d in ${rofslist}; do
506             mkdir -p "${rootmnt}/live/${d##*/}"
507             case d in
508                 *.dir) # do nothing # mount -o bind "${d}" "${rootmnt}/live/${d##*/}"
509                     ;;
510                 *) mount --move "${d}" "${rootmnt}/live/${d##*/}"
511                     ;;
512             esac
513         done
514     fi
515
516     # shows cow fs on /cow for use by live-snapshot
517     mkdir -p "${rootmnt}/cow"
518     mount -o bind /cow "${rootmnt}/cow"
519 }
520
521 check_dev ()
522 {
523     sysdev="${1}"
524     devname="${2}"
525     if [ -z "${devname}" ]; then
526         devname=$(sys2dev "${sysdev}")
527     fi
528
529     if [ -n "${LIVE_MEDIA_OFFSET}" ]; then
530         loopdevname=$(setup_loop "${devname}" "loop" "/sys/block/loop*" "${LIVE_MEDIA_OFFSET}" '')
531         devname="${loopdevname}"
532     fi
533
534     fstype=$(get_fstype "${devname}")
535     if is_supported_fs ${fstype}; then
536         mount -t ${fstype} -o ro "${devname}" $mountpoint || continue
537         if is_live_path $mountpoint; then
538             echo $mountpoint
539             return 0
540         else
541             umount $mountpoint
542         fi
543     fi
544
545     if [ -n "${LIVE_MEDIA_OFFSET}" ]; then
546         losetup -d "${loopdevname}"
547     fi
548     return 1
549 }
550
551 find_livefs() {
552     timeout="${1}"
553     # first look at the one specified in the command line
554     if [ ! -z "${LIVE_MEDIA}" ]; then
555         if check_dev "null" "${LIVE_MEDIA}"; then
556             return 0
557         fi
558     fi
559     # don't start autodetection before timeout has expired
560     if [ -n "${LIVE_MEDIA_TIMEOUT}" ]; then
561         if [ "${timeout}" -lt "${LIVE_MEDIA_TIMEOUT}" ]; then
562             return 1
563         fi
564     fi
565     # or do the scan of block devices
566     for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram); do
567         devname=$(sys2dev "${sysblock}")
568         fstype=$(get_fstype "${devname}")
569         if /lib/udev/cdrom_id ${devname} > /dev/null; then
570             if check_dev "null" "${devname}" ; then
571                 return 0
572             fi
573         elif is_nice_device "${sysblock}" ; then
574             for dev in $(subdevices "${sysblock}"); do
575                 if check_dev "${dev}" ; then
576                     return 0
577                 fi
578             done
579         elif [ "${fstype}" = "squashfs" -o \
580                 "${fstype}" = "ext2" -o \
581                 "${fstype}" = "ext3" ]; then
582             # This is an ugly hack situation, the block device has
583             # an image directly on it.  It's hopefully
584             # live-initramfs, so take it and run with it.
585             ln -s "${devname}" "${devname}.${fstype}"
586             echo "${devname}.${fstype}"
587             return 0
588         fi
589     done
590     return 1
591 }
592
593 pulsate() {
594     if [ -x /sbin/usplash_write ]; then
595         /sbin/usplash_write "PULSATE"
596     fi
597 }
598
599 set_usplash_timeout() {
600     if [ -x /sbin/usplash_write ]; then
601         /sbin/usplash_write "TIMEOUT 120"
602     fi
603 }
604
605 mountroot() {
606     exec 6>&1
607     exec 7>&2
608     exec > live.log
609     exec 2>&1
610
611     parse_cmdline
612
613     set_usplash_timeout
614     [ "$quiet" != "y" ] && log_begin_msg "Running /scripts/live-premount"
615     pulsate
616     run_scripts /scripts/live-premount
617     [ "$quiet" != "y" ] && log_end_msg
618
619     # Needed here too because some things (*cough* udev *cough*)
620     # changes the timeout
621
622     set_usplash_timeout
623
624     if [ ! -z "${NETBOOT}" ]; then
625         if do_netmount ; then
626             livefs_root="${mountpoint}"
627         else
628             panic "Unable to find a live file system on the network"
629         fi
630     else
631         # Scan local devices for the image
632         for i in 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19; do
633             livefs_root=$(find_livefs $i)
634             if [ -n "${livefs_root}" ]; then
635                 break
636             fi
637             sleep 1
638         done
639     fi
640
641     if [ -z "${livefs_root}" ]; then
642         panic "Unable to find a medium containing a live file system"
643     fi
644
645     if [ "${TORAM}" ]; then
646         live_dest="ram"
647     elif [ "${TODISK}" ]; then
648         live_dest="${TODISK}"
649     fi
650     if [ "${live_dest}" ]; then
651         log_begin_msg "Copying live_media to ${live_dest}"
652         copy_live_to "${livefs_root}" "${live_dest}"
653         log_end_msg
654     fi
655
656     mount_images_in_directory "${livefs_root}" "${rootmnt}"
657
658     log_end_msg
659
660     maybe_break live-bottom
661     [ "$quiet" != "y" ] && log_begin_msg "Running /scripts/live-bottom"
662
663     pulsate
664     run_scripts /scripts/live-bottom
665     [ "$quiet" != "y" ] && log_end_msg
666
667     exec 1>&6 6>&-
668     exec 2>&7 7>&-
669     cp live.log "${rootmnt}/var/log/"
670 }