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