Adding upstream version 4.0~a14.
[debian-live-boot.git] / components / 9990-misc-helpers.sh
blob2a3e6ad2b3f2a68458d098e163d8efc54cc63818
1 #!/bin/sh
3 #set -e
5 is_live_path()
7 DIRECTORY="${1}/${LIVE_MEDIA_PATH}"
8 for FILESYSTEM in squashfs ext2 ext3 ext4 xfs dir jffs
9 do
10 if ls "${DIRECTORY}/"*.${FILESYSTEM} > /dev/null 2>&1
11 then
12 return 0
14 done
15 return 1
18 matches_uuid ()
20 if [ "${IGNORE_UUID}" ] || [ ! -e /conf/uuid.conf ]
21 then
22 return 0
25 path="${1}"
26 uuid="$(cat /conf/uuid.conf)"
28 for try_uuid_file in "${path}/.disk/live-uuid"*
30 [ -e "${try_uuid_file}" ] || continue
32 try_uuid="$(cat "${try_uuid_file}")"
34 if [ "${uuid}" = "${try_uuid}" ]
35 then
36 return 0
38 done
40 return 1
43 get_backing_device ()
45 case "${1}" in
46 *.squashfs|*.ext2|*.ext3|*.ext4|*.jffs2)
47 echo $(setup_loop "${1}" "loop" "/sys/block/loop*" '0' "${LIVE_MEDIA_ENCRYPTION}" "${2}")
50 *.dir)
51 echo "directory"
55 panic "Unrecognized live filesystem: ${1}"
57 esac
60 mount_images_in_directory ()
62 directory="${1}"
63 rootmnt="${2}"
64 mac="${3}"
66 if is_live_path "${directory}"
67 then
68 [ -n "${mac}" ] && adddirectory="${directory}/${LIVE_MEDIA_PATH}/${mac}"
69 setup_unionfs "${directory}/${LIVE_MEDIA_PATH}" "${rootmnt}" "${adddirectory}"
70 else
71 panic "No supported filesystem images found at /${LIVE_MEDIA_PATH}."
75 is_nice_device ()
77 sysfs_path="${1#/sys}"
79 if udevadm test-builtin path_id "${sysfs_path}" | egrep -q "ID_PATH=(usb|pci-[^-]*-(ide|sas|scsi|usb|virtio)|platform-sata_mv|platform-orion-ehci|platform-mmc|platform-mxsdhci)"
80 then
81 return 0
82 elif echo "${sysfs_path}" | grep -q '^/block/vd[a-z]$'
83 then
84 return 0
85 elif echo ${sysfs_path} | grep -q "^/block/dm-"
86 then
87 return 0
88 elif echo ${sysfs_path} | grep -q "^/block/mtdblock"
89 then
90 return 0
93 return 1
96 check_dev ()
98 sysdev="${1}"
99 devname="${2}"
100 skip_uuid_check="${3}"
102 # support for fromiso=.../isofrom=....
103 if [ -n "$FROMISO" ]
104 then
105 ISO_DEVICE=$(dirname $FROMISO)
106 if ! [ -b $ISO_DEVICE ]
107 then
108 # to support unusual device names like /dev/cciss/c0d0p1
109 # as well we have to identify the block device name, let's
110 # do that for up to 15 levels
111 i=15
112 while [ -n "$ISO_DEVICE" ] && [ "$i" -gt 0 ]
114 ISO_DEVICE=$(dirname ${ISO_DEVICE})
115 [ -b "$ISO_DEVICE" ] && break
116 i=$(($i -1))
117 done
120 if [ "$ISO_DEVICE" = "/" ]
121 then
122 echo "Warning: device for bootoption fromiso= ($FROMISO) not found.">>/boot.log
123 else
124 fs_type=$(get_fstype "${ISO_DEVICE}")
125 if is_supported_fs ${fs_type}
126 then
127 mkdir /live/fromiso
128 mount -t $fs_type "$ISO_DEVICE" /live/fromiso
129 ISO_NAME="$(echo $FROMISO | sed "s|$ISO_DEVICE||")"
130 loopdevname=$(setup_loop "/live/fromiso/${ISO_NAME}" "loop" "/sys/block/loop*" "" '')
131 devname="${loopdevname}"
132 else
133 echo "Warning: unable to mount $ISO_DEVICE." >>/boot.log
138 if [ -z "${devname}" ]
139 then
140 devname=$(sys2dev "${sysdev}")
143 if [ -d "${devname}" ]
144 then
145 mount -o bind "${devname}" $mountpoint || continue
147 if is_live_path $mountpoint
148 then
149 echo $mountpoint
150 return 0
151 else
152 umount $mountpoint
156 IFS=","
157 for device in ${devname}
159 case "$device" in
160 *mapper*)
161 # Adding lvm support
162 if [ -x /scripts/local-top/lvm2 ]
163 then
164 ROOT="$device" resume="" /scripts/local-top/lvm2
168 /dev/md*)
169 # Adding raid support
170 if [ -x /scripts/local-top/mdadm ]
171 then
172 cp /conf/conf.d/md /conf/conf.d/md.orig
173 echo "MD_DEVS=$device " >> /conf/conf.d/md
174 /scripts/local-top/mdadm
175 mv /conf/conf.d/md.orig /conf/conf.d/md
178 esac
179 done
180 unset IFS
182 [ -n "$device" ] && devname="$device"
184 [ -e "$devname" ] || continue
186 if [ -n "${LIVE_MEDIA_OFFSET}" ]
187 then
188 loopdevname=$(setup_loop "${devname}" "loop" "/sys/block/loop*" "${LIVE_MEDIA_OFFSET}" '')
189 devname="${loopdevname}"
192 fstype=$(get_fstype "${devname}")
194 if is_supported_fs ${fstype}
195 then
196 devuid=$(blkid -o value -s UUID "$devname")
197 [ -n "$devuid" ] && grep -qs "\<$devuid\>" /var/lib/live/boot/devices-already-tried-to-mount && continue
198 mount -t ${fstype} -o ro,noatime "${devname}" ${mountpoint} || continue
199 [ -n "$devuid" ] && echo "$devuid" >> /var/lib/live/boot/devices-already-tried-to-mount
201 if [ -n "${FINDISO}" ]
202 then
203 if [ -f ${mountpoint}/${FINDISO} ]
204 then
205 umount ${mountpoint}
206 mkdir -p /live/findiso
207 mount -t ${fstype} -o ro,noatime "${devname}" /live/findiso
208 loopdevname=$(setup_loop "/live/findiso/${FINDISO}" "loop" "/sys/block/loop*" 0 "")
209 devname="${loopdevname}"
210 mount -t iso9660 -o ro,noatime "${devname}" ${mountpoint}
211 else
212 umount ${mountpoint}
216 if is_live_path ${mountpoint} && \
217 ([ "${skip_uuid_check}" ] || matches_uuid ${mountpoint})
218 then
219 echo ${mountpoint}
220 return 0
221 else
222 umount ${mountpoint} 2>/dev/null
226 if [ -n "${LIVE_MEDIA_OFFSET}" ]
227 then
228 losetup -d "${loopdevname}"
231 return 1
234 find_livefs ()
236 timeout="${1}"
238 # don't start autodetection before timeout has expired
239 if [ -n "${LIVE_MEDIA_TIMEOUT}" ]
240 then
241 if [ "${timeout}" -lt "${LIVE_MEDIA_TIMEOUT}" ]
242 then
243 return 1
247 # first look at the one specified in the command line
248 case "${LIVE_MEDIA}" in
249 removable-usb)
250 for sysblock in $(removable_usb_dev "sys")
252 for dev in $(subdevices "${sysblock}")
254 if check_dev "${dev}"
255 then
256 return 0
258 done
259 done
260 return 1
263 removable)
264 for sysblock in $(removable_dev "sys")
266 for dev in $(subdevices "${sysblock}")
268 if check_dev "${dev}"
269 then
270 return 0
272 done
273 done
274 return 1
278 if [ ! -z "${LIVE_MEDIA}" ]
279 then
280 if check_dev "null" "${LIVE_MEDIA}" "skip_uuid_check"
281 then
282 return 0
286 esac
288 # or do the scan of block devices
289 # prefer removable devices over non-removable devices, so scan them first
290 devices_to_scan="$(removable_dev 'sys') $(non_removable_dev 'sys')"
292 for sysblock in $devices_to_scan
294 devname=$(sys2dev "${sysblock}")
295 [ -e "$devname" ] || continue
296 fstype=$(get_fstype "${devname}")
298 if /lib/udev/cdrom_id ${devname} > /dev/null
299 then
300 if check_dev "null" "${devname}"
301 then
302 return 0
304 elif is_nice_device "${sysblock}"
305 then
306 for dev in $(subdevices "${sysblock}")
308 if check_dev "${dev}"
309 then
310 return 0
312 done
313 elif [ "${fstype}" = "squashfs" -o \
314 "${fstype}" = "btrfs" -o \
315 "${fstype}" = "ext2" -o \
316 "${fstype}" = "ext3" -o \
317 "${fstype}" = "ext4" -o \
318 "${fstype}" = "jffs2" ]
319 then
320 # This is an ugly hack situation, the block device has
321 # an image directly on it. It's hopefully
322 # live-boot, so take it and run with it.
323 ln -s "${devname}" "${devname}.${fstype}"
324 echo "${devname}.${fstype}"
325 return 0
327 done
329 return 1
332 is_in_list_separator_helper ()
334 local sep element list
335 sep=${1}
336 shift
337 element=${1}
338 shift
339 list=${*}
340 echo ${list} | grep -qe "^\(.*${sep}\)\?${element}\(${sep}.*\)\?$"
343 is_in_space_sep_list ()
345 local element
346 element=${1}
347 shift
348 is_in_list_separator_helper "[[:space:]]" "${element}" "${*}"
351 is_in_comma_sep_list ()
353 local element
354 element=${1}
355 shift
356 is_in_list_separator_helper "," "${element}" "${*}"
359 sys2dev ()
361 sysdev=${1#/sys}
362 echo "/dev/$(udevadm info -q name -p ${sysdev} 2>/dev/null|| echo ${sysdev##*/})"
365 subdevices ()
367 sysblock=${1}
368 r=""
370 for dev in "${sysblock}"/* "${sysblock}"
372 if [ -e "${dev}/dev" ]
373 then
374 r="${r} ${dev}"
376 done
378 echo ${r}
381 storage_devices()
383 black_listed_devices="${1}"
384 white_listed_devices="${2}"
386 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "loop|ram|fd")
388 fulldevname=$(sys2dev "${sysblock}")
390 if is_in_space_sep_list ${fulldevname} ${black_listed_devices} || \
391 [ -n "${white_listed_devices}" ] && \
392 ! is_in_space_sep_list ${fulldevname} ${white_listed_devices}
393 then
394 # skip this device entirely
395 continue
398 for dev in $(subdevices "${sysblock}")
400 devname=$(sys2dev "${dev}")
402 if is_in_space_sep_list ${devname} ${black_listed_devices}
403 then
404 # skip this subdevice
405 continue
406 else
407 echo "${devname}"
409 done
410 done
413 is_supported_fs ()
415 fstype="${1}"
417 # Validate input first
418 if [ -z "${fstype}" ]
419 then
420 return 1
423 # Try to look if it is already supported by the kernel
424 if grep -q ${fstype} /proc/filesystems
425 then
426 return 0
427 else
428 # Then try to add support for it the gentle way using the initramfs capabilities
429 modprobe ${fstype}
430 if grep -q ${fstype} /proc/filesystems
431 then
432 return 0
433 # Then try the hard way if /root is already reachable
434 else
435 kmodule="/root/lib/modules/`uname -r`/${fstype}/${fstype}.ko"
436 if [ -e "${kmodule}" ]
437 then
438 insmod "${kmodule}"
439 if grep -q ${fstype} /proc/filesystems
440 then
441 return 0
447 return 1
450 get_fstype ()
452 /sbin/blkid -s TYPE -o value $1 2>/dev/null
455 where_is_mounted ()
457 device=${1}
458 # return first found
459 grep -m1 "^${device} " /proc/mounts | cut -f2 -d ' '
462 trim_path ()
464 # remove all unnecessary /:s in the path, including last one (except
465 # if path is just "/")
466 echo ${1} | sed 's|//\+|/|g' | sed 's|^\(.*[^/]\)/$|\1|'
469 what_is_mounted_on ()
471 local dir
472 dir="$(trim_path ${1})"
473 grep -m1 "^[^ ]\+ ${dir} " /proc/mounts | cut -d' ' -f1
476 chown_ref ()
478 local reference targets owner
479 reference="${1}"
480 shift
481 targets=${@}
482 owner=$(stat -c %u:%g "${reference}")
483 chown -h ${owner} ${targets}
486 chmod_ref ()
488 local reference targets rights
489 reference="${1}"
490 shift
491 targets=${@}
492 rights=$(stat -c %a "${reference}")
493 chmod ${rights} ${targets}
496 lastline ()
498 while read lines
500 line=${lines}
501 done
503 echo "${line}"
506 base_path ()
508 testpath="${1}"
509 mounts="$(awk '{print $2}' /proc/mounts)"
510 testpath="$(busybox realpath ${testpath})"
512 while true
514 if echo "${mounts}" | grep -qs "^${testpath}"
515 then
516 set -- $(echo "${mounts}" | grep "^${testpath}" | lastline)
517 echo ${1}
518 break
519 else
520 testpath=$(dirname $testpath)
522 done
525 fs_size ()
527 # Returns used/free fs kbytes + 5% more
528 # You could pass a block device as ${1} or the mount point as ${2}
530 dev="${1}"
531 mountp="${2}"
532 used="${3}"
534 if [ -z "${mountp}" ]
535 then
536 mountp="$(where_is_mounted ${dev})"
538 if [ -z "${mountp}" ]
539 then
540 mountp="/mnt/tmp_fs_size"
542 mkdir -p "${mountp}"
543 mount -t $(get_fstype "${dev}") -o ro "${dev}" "${mountp}" || log_warning_msg "cannot mount -t $(get_fstype ${dev}) -o ro ${dev} ${mountp}"
545 doumount=1
549 if [ "${used}" = "used" ]
550 then
551 size=$(du -ks ${mountp} | cut -f1)
552 size=$(expr ${size} + ${size} / 20 ) # FIXME: 5% more to be sure
553 else
554 # free space
555 size="$(df -kP | grep -s ${mountp} | awk '{print $4}')"
558 if [ -n "${doumount}" ]
559 then
560 umount "${mountp}" || log_warning_msg "cannot umount ${mountp}"
561 rmdir "${mountp}"
564 echo "${size}"
567 load_keymap ()
569 # Load custom keymap
570 if [ -x /bin/loadkeys -a -r /etc/boottime.kmap.gz ]
571 then
572 loadkeys --quiet /etc/boottime.kmap.gz
576 setup_loop ()
578 local fspath module pattern offset encryption readonly
579 fspath=${1}
580 module=${2}
581 pattern=${3}
582 offset=${4}
583 encryption=${5}
584 readonly=${6}
586 # the output of setup_loop is evaluated in other functions,
587 # modprobe leaks kernel options like "libata.dma=0"
588 # as "options libata dma=0" on stdout, causing serious
589 # problems therefor, so instead always avoid output to stdout
590 modprobe -q -b "${module}" 1>/dev/null
592 udevadm settle
594 for loopdev in ${pattern}
596 if [ "$(cat ${loopdev}/size)" -eq 0 ]
597 then
598 dev=$(sys2dev "${loopdev}")
599 options=''
601 if [ -n "${readonly}" ]
602 then
603 if losetup --help 2>&1 | grep -q -- "-r\b"
604 then
605 options="${options} -r"
609 if [ -n "${offset}" ] && [ 0 -lt "${offset}" ]
610 then
611 options="${options} -o ${offset}"
614 if [ -z "${encryption}" ]
615 then
616 losetup ${options} "${dev}" "${fspath}"
617 else
618 # Loop AES encryption
619 while true
621 load_keymap
623 echo -n "Enter passphrase for root filesystem: " >&6
624 read -s passphrase
625 echo "${passphrase}" > /tmp/passphrase
626 unset passphrase
627 exec 9</tmp/passphrase
628 /sbin/losetup ${options} -e "${encryption}" -p 9 "${dev}" "${fspath}"
629 error=${?}
630 exec 9<&-
631 rm -f /tmp/passphrase
633 if [ 0 -eq ${error} ]
634 then
635 unset error
636 break
639 echo
640 echo -n "There was an error decrypting the root filesystem ... Retry? [Y/n] " >&6
641 read answer
643 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
644 then
645 unset answer
646 break
648 done
651 echo "${dev}"
652 return 0
654 done
656 panic "No loop devices available"
659 try_mount ()
661 dev="${1}"
662 mountp="${2}"
663 opts="${3}"
664 fstype="${4}"
666 old_mountp="$(where_is_mounted ${dev})"
668 if [ -n "${old_mountp}" ]
669 then
670 if [ "${opts}" != "ro" ]
671 then
672 mount -o remount,"${opts}" "${dev}" "${old_mountp}" || panic "Remounting ${dev} ${opts} on ${old_mountp} failed"
675 mount -o bind "${old_mountp}" "${mountp}" || panic "Cannot bind-mount ${old_mountp} on ${mountp}"
676 else
677 if [ -z "${fstype}" ]
678 then
679 fstype=$(get_fstype "${dev}")
681 mount -t "${fstype}" -o "${opts}" "${dev}" "${mountp}" || \
682 ( echo "SKIPPING: Cannot mount ${dev} on ${mountp}, fstype=${fstype}, options=${opts}" > boot.log && return 0 )
686 # Try to mount $device to the place expected by live-boot. If $device
687 # is already mounted somewhere, move it to the expected place. If $device
688 # ends with a "/" this is a directory path.
689 # If we're only probing $device (to check if it has custom persistence)
690 # $probe should be set, which suppresses warnings upon failure. On
691 # success, print the mount point for $device.
692 mount_persistence_media ()
694 local device probe backing old_backing fstype mount_opts
695 device=${1}
696 probe=${2}
698 # get_custom_mounts() might call this with a directory path instead
699 # of a block device path. This means we have found sub-directory path
700 # underneath /lib/live/mounts/persistence, so we're done
701 if [ -d "${device}" ]
702 then
703 echo "${device}"
704 return 0
707 if [ ! -b "${device}" ]
708 then
709 return 1
712 backing="/live/persistence/$(basename ${device})"
714 mkdir -p "${backing}"
715 old_backing="$(where_is_mounted ${device})"
716 if [ -z "${old_backing}" ]
717 then
718 fstype="$(get_fstype ${device})"
719 mount_opts="rw,noatime"
720 if [ -n "${PERSISTENCE_READONLY}" ]
721 then
722 mount_opts="ro,noatime"
724 if mount -t "${fstype}" -o "${mount_opts}" "${device}" "${backing}" >/dev/null
725 then
726 echo ${backing}
727 return 0
728 else
729 [ -z "${probe}" ] && log_warning_msg "Failed to mount persistence media ${device}"
730 rmdir "${backing}"
731 return 1
733 elif [ "${backing}" != "${old_backing}" ]
734 then
735 if ! mount --move ${old_backing} ${backing} >/dev/null
736 then
737 [ -z "${probe}" ] && log_warning_msg "Failed to move persistence media ${device}"
738 rmdir "${backing}"
739 return 1
741 mount_opts="rw,noatime"
742 if [ -n "${PERSISTENCE_READONLY}" ]
743 then
744 mount_opts="ro,noatime"
746 if ! mount -o "remount,${mount_opts}" "${backing}" >/dev/null
747 then
748 log_warning_msg "Failed to remount persistence media ${device} writable"
749 # Don't unmount or rmdir the new mountpoint in this case
751 echo ${backing}
752 return 0
753 else
754 # This means that $device has already been mounted on
755 # the place expected by live-boot, so we're done.
756 echo ${backing}
757 return 0
761 close_persistence_media ()
763 local device backing
764 device=${1}
765 backing="$(where_is_mounted ${device})"
767 if [ -d "${backing}" ]
768 then
769 umount "${backing}" >/dev/null 2>&1
770 rmdir "${backing}" >/dev/null 2>&1
773 if is_active_luks_mapping ${device}
774 then
775 /sbin/cryptsetup luksClose ${device}
779 open_luks_device ()
781 dev="${1}"
782 name="$(basename ${dev})"
783 opts="--key-file=-"
784 if [ -n "${PERSISTENCE_READONLY}" ]
785 then
786 opts="${opts} --readonly"
789 if /sbin/cryptsetup status "${name}" >/dev/null 2>&1
790 then
791 re="^[[:space:]]*device:[[:space:]]*\([^[:space:]]*\)$"
792 opened_dev=$(cryptsetup status ${name} 2>/dev/null | grep "${re}" | sed "s|${re}|\1|")
793 if [ "${opened_dev}" = "${dev}" ]
794 then
795 luks_device="/dev/mapper/${name}"
796 echo ${luks_device}
797 return 0
798 else
799 log_warning_msg "Cannot open luks device ${dev} since ${opened_dev} already is opened with its name"
800 return 1
804 load_keymap
806 while true
808 /lib/cryptsetup/askpass "Enter passphrase for ${dev}: " | \
809 /sbin/cryptsetup -T 1 luksOpen ${dev} ${name} ${opts}
811 if [ 0 -eq ${?} ]
812 then
813 luks_device="/dev/mapper/${name}"
814 echo ${luks_device}
815 return 0
818 echo >&6
819 echo -n "There was an error decrypting ${dev} ... Retry? [Y/n] " >&6
820 read answer
822 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
823 then
824 return 2
826 done
829 get_gpt_name ()
831 local dev
832 dev="${1}"
833 /sbin/blkid -s PART_ENTRY_NAME -p -o value ${dev} 2>/dev/null
836 is_gpt_device ()
838 local dev
839 dev="${1}"
840 [ "$(/sbin/blkid -s PART_ENTRY_SCHEME -p -o value ${dev} 2>/dev/null)" = "gpt" ]
843 probe_for_gpt_name ()
845 local overlays dev gpt_dev gpt_name
846 overlays="${1}"
847 dev="${2}"
849 gpt_dev="${dev}"
850 if is_active_luks_mapping ${dev}
851 then
852 # if $dev is an opened luks device, we need to check
853 # GPT stuff on the backing device
854 gpt_dev=$(get_luks_backing_device "${dev}")
857 if ! is_gpt_device ${gpt_dev}
858 then
859 return
862 gpt_name=$(get_gpt_name ${gpt_dev})
863 for label in ${overlays}
865 if [ "${gpt_name}" = "${label}" ]
866 then
867 echo "${label}=${dev}"
869 done
872 probe_for_fs_label ()
874 local overlays dev
875 overlays="${1}"
876 dev="${2}"
878 for label in ${overlays}
880 if [ "$(/sbin/blkid -s LABEL -o value $dev 2>/dev/null)" = "${label}" ]
881 then
882 echo "${label}=${dev}"
884 done
887 probe_for_file_name ()
889 local overlays dev ret backing
890 overlays="${1}"
891 dev="${2}"
893 ret=""
894 backing="$(mount_persistence_media ${dev} probe)"
895 if [ -z "${backing}" ]
896 then
897 return
900 for label in ${overlays}
902 path=${backing}/${PERSISTENCE_PATH}/${label}
903 if [ -f "${path}" ]
904 then
905 local loopdev
906 loopdev=$(setup_loop "${path}" "loop" "/sys/block/loop*")
907 ret="${ret} ${label}=${loopdev}"
909 done
911 if [ -n "${ret}" ]
912 then
913 echo ${ret}
914 else
915 # unmount and remove mountpoint
916 umount ${backing} > /dev/null 2>&1 || true
917 rmdir ${backing} > /dev/null 2>&1 || true
921 probe_for_directory_name ()
923 local overlays dev ret backing
924 overlays="${1}"
925 dev="${2}"
927 ret=""
928 backing="$(mount_persistence_media ${dev} probe)"
929 if [ -z "${backing}" ]
930 then
931 return
934 for label in ${overlays}
936 path=${backing}/${PERSISTENCE_PATH}/${label}
937 if [ -d "${path}" ]
938 then
939 # in this case the "device" ends with a "/"
940 ret="${ret} ${label}=${backing}/${PERSISTENCE_PATH}/${label%%/}/"
942 done
944 if [ -n "${ret}" ]
945 then
946 echo ${ret}
947 else
948 # unmount and remove mountpoint
949 umount ${backing} > /dev/null 2>&1 || true
950 rmdir ${backing} > /dev/null 2>&1 || true
954 find_persistence_media ()
956 # Scans devices for overlays, and returns a whitespace
957 # separated list of how to use them. Only overlays with a partition
958 # label or file name in ${overlays} are returned.
960 # When scanning a LUKS device, the user will be asked to enter the
961 # passphrase; on failure to enter it, or if no persistence partitions
962 # or files were found, the LUKS device is closed.
964 # For all other cases (overlay partition and overlay file) the
965 # return value is "${label}=${device}", where ${device} a device that
966 # can mount the content. In the case of an overlay file, the device
967 # containing the file will remain mounted as a side-effect.
969 # No devices in ${black_listed_devices} will be scanned, and if
970 # ${white_list_devices} is non-empty, only devices in it will be
971 # scanned.
973 local overlays white_listed_devices ret black_listed_devices
974 overlays="${1}"
975 white_listed_devices="${2}"
976 ret=""
979 # The devices that are hosting the actual live rootfs should not be
980 # used for persistence storage since otherwise you might mount a
981 # parent directory on top of a sub-directory of the same filesystem
982 # in one union together.
984 black_listed_devices=""
985 for d in /live/rootfs/* /live/findiso /live/fromiso
987 black_listed_devices="${black_listed_devices} $(what_is_mounted_on d)"
988 done
990 for dev in $(storage_devices "${black_listed_devices}" "${white_listed_devices}")
992 local result luks_device
993 result=""
995 luks_device=""
996 # Check if it's a luks device; we'll have to open the device
997 # in order to probe any filesystem it contains, like we do
998 # below. activate_custom_mounts() also depends on that any luks
999 # device already has been opened.
1000 if is_in_comma_sep_list luks ${PERSISTENCE_ENCRYPTION} && is_luks_partition ${dev}
1001 then
1002 if luks_device=$(open_luks_device "${dev}")
1003 then
1004 dev="${luks_device}"
1005 else
1006 # skip $dev since we failed/chose not to open it
1007 continue
1009 elif ! is_in_comma_sep_list none ${PERSISTENCE_ENCRYPTION}
1010 then
1011 # skip $dev since we don't allow unencrypted storage
1012 continue
1015 # Probe for matching GPT partition names or filesystem labels
1016 if is_in_comma_sep_list filesystem ${PERSISTENCE_STORAGE}
1017 then
1018 result=$(probe_for_gpt_name "${overlays}" ${dev})
1019 if [ -n "${result}" ]
1020 then
1021 ret="${ret} ${result}"
1022 continue
1025 result=$(probe_for_fs_label "${overlays}" ${dev})
1026 if [ -n "${result}" ]
1027 then
1028 ret="${ret} ${result}"
1029 continue
1033 # Probe for files with matching name on mounted partition
1034 if is_in_comma_sep_list file ${PERSISTENCE_STORAGE}
1035 then
1036 result=$(probe_for_file_name "${overlays}" ${dev})
1037 if [ -n "${result}" ]
1038 then
1039 local loopdevice
1040 loopdevice=${result##*=}
1041 if is_in_comma_sep_list luks ${PERSISTENCE_ENCRYPTION} && is_luks_partition ${loopdevice}
1042 then
1043 local luksfile
1044 luksfile=""
1045 if luksfile=$(open_luks_device "${loopdevice}")
1046 then
1047 result=${result%%=*}
1048 result="${result}=${luksfile}"
1049 else
1050 losetup -d $loopdevice
1051 result=""
1054 ret="${ret} ${result}"
1055 continue
1059 # Probe for directory with matching name on mounted partition
1060 if is_in_comma_sep_list directory ${PERSISTENCE_STORAGE}
1061 then
1062 result=$(probe_for_directory_name "${overlays}" ${dev})
1063 if [ -n "${result}" ]
1064 then
1065 ret="${ret} ${result}"
1066 continue
1070 # Close luks device if it isn't used
1071 if [ -z "${result}" ] && [ -n "${luks_device}" ] && is_active_luks_mapping "${luks_device}"
1072 then
1073 /sbin/cryptsetup luksClose "${luks_device}"
1075 done
1077 if [ -n "${ret}" ]
1078 then
1079 echo ${ret}
1083 get_mac ()
1085 mac=""
1087 for adaptor in /sys/class/net/*
1089 status="$(cat ${adaptor}/iflink)"
1091 if [ "${status}" -eq 2 ]
1092 then
1093 mac="$(cat ${adaptor}/address)"
1094 mac="$(echo ${mac} | sed 's/:/-/g' | tr '[a-z]' '[A-Z]')"
1096 done
1098 echo ${mac}
1101 is_luks_partition ()
1103 device="${1}"
1104 /sbin/cryptsetup isLuks "${device}" 1>/dev/null 2>&1
1107 is_active_luks_mapping ()
1109 device="${1}"
1110 /sbin/cryptsetup status "${device}" 1>/dev/null 2>&1
1113 get_luks_backing_device ()
1115 device=${1}
1116 cryptsetup status ${device} 2> /dev/null | \
1117 awk '{if ($1 == "device:") print $2}'
1120 removable_dev ()
1122 output_format="${1}"
1123 want_usb="${2}"
1124 ret=
1126 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1128 dev_ok=
1129 if [ "$(cat ${sysblock}/removable)" = "1" ]
1130 then
1131 if [ -z "${want_usb}" ]
1132 then
1133 dev_ok="true"
1134 else
1135 if readlink ${sysblock} | grep -q usb
1136 then
1137 dev_ok="true"
1142 if [ "${dev_ok}" = "true" ]
1143 then
1144 case "${output_format}" in
1145 sys)
1146 ret="${ret} ${sysblock}"
1149 devname=$(sys2dev "${sysblock}")
1150 ret="${ret} ${devname}"
1152 esac
1154 done
1156 echo "${ret}"
1159 removable_usb_dev ()
1161 output_format="${1}"
1163 removable_dev "${output_format}" "want_usb"
1166 non_removable_dev ()
1168 output_format="${1}"
1169 ret=
1171 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1173 if [ "$(cat ${sysblock}/removable)" = "0" ]
1174 then
1175 case "${output_format}" in
1176 sys)
1177 ret="${ret} ${sysblock}"
1180 devname=$(sys2dev "${sysblock}")
1181 ret="${ret} ${devname}"
1183 esac
1185 done
1187 echo "${ret}"
1190 link_files ()
1192 # create source's directory structure in dest, and recursively
1193 # create symlinks in dest to to all files in source. if mask
1194 # is non-empty, remove mask from all source paths when
1195 # creating links (will be necessary if we change root, which
1196 # live-boot normally does (into $rootmnt)).
1197 local src_dir dest_dir src_transform
1199 # remove multiple /:s and ensure ending on /
1200 src_dir="$(trim_path ${1})/"
1201 dest_dir="$(trim_path ${2})/"
1202 src_transform="${3}"
1204 # This check can only trigger on the inital, non-recursive call since
1205 # we create the destination before recursive calls
1206 if [ ! -d "${dest_dir}" ]
1207 then
1208 log_warning_msg "Must link_files into a directory"
1209 return
1212 find "${src_dir}" -mindepth 1 -maxdepth 1 | \
1213 while read src
1215 local dest final_src
1216 dest="${dest_dir}$(basename "${src}")"
1217 if [ -d "${src}" ]
1218 then
1219 if [ -z "$(ls -A "${src}")" ]
1220 then
1221 continue
1223 if [ ! -d "${dest}" ]
1224 then
1225 mkdir -p "${dest}"
1226 chown_ref "${src}" "${dest}"
1227 chmod_ref "${src}" "${dest}"
1229 link_files "${src}" "${dest}" "${src_transform}"
1230 else
1231 final_src=${src}
1232 if [ -n "${src_transform}" ]
1233 then
1234 final_src="$(echo ${final_src} | sed "${src_transform}")"
1236 rm -rf "${dest}" 2> /dev/null
1237 ln -s "${final_src}" "${dest}"
1238 chown_ref "${src}" "${dest}"
1240 done
1243 do_union ()
1245 local unionmountpoint unionrw unionro
1246 unionmountpoint="${1}" # directory where the union is mounted
1247 shift
1248 unionrw="${1}" # branch where the union changes are stored
1249 shift
1250 unionro="${*}" # space separated list of read-only branches (optional)
1252 case "${UNIONTYPE}" in
1253 aufs)
1254 rw_opt="rw"
1255 ro_opt="rr+wh"
1256 noxino_opt="noxino"
1259 unionfs-fuse)
1260 rw_opt="RW"
1261 ro_opt="RO"
1265 rw_opt="rw"
1266 ro_opt="ro"
1268 esac
1270 case "${UNIONTYPE}" in
1271 unionfs-fuse)
1272 unionmountopts="-o cow -o noinitgroups -o default_permissions -o allow_other -o use_ino -o suid"
1273 unionmountopts="${unionmountopts} ${unionrw}=${rw_opt}"
1274 if [ -n "${unionro}" ]
1275 then
1276 for rofs in ${unionro}
1278 unionmountopts="${unionmountopts}:${rofs}=${ro_opt}"
1279 done
1281 ( sysctl -w fs.file-max=391524 ; ulimit -HSn 16384
1282 unionfs-fuse ${unionmountopts} "${unionmountpoint}" ) && \
1283 ( mkdir -p /run/sendsigs.omit.d
1284 pidof unionfs-fuse >> /run/sendsigs.omit.d/unionfs-fuse || true )
1287 overlayfs)
1288 # XXX: can multiple unionro be used? (overlayfs only handles two dirs, but perhaps they can be chained?)
1289 # XXX: and can unionro be optional? i.e. can overlayfs skip lowerdir?
1290 if echo ${unionro} | grep -q " "
1291 then
1292 panic "Multiple lower filesystems are currently not supported with overlayfs (unionro = ${unionro})."
1293 elif [ -z "${unionro}" ]
1294 then
1295 panic "Overlayfs needs at least one lower filesystem (read-only branch)."
1297 unionmountopts="-o noatime,lowerdir=${unionro},upperdir=${unionrw}"
1298 mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
1302 unionmountopts="-o noatime,${noxino_opt},dirs=${unionrw}=${rw_opt}"
1303 if [ -n "${unionro}" ]
1304 then
1305 for rofs in ${unionro}
1307 unionmountopts="${unionmountopts}:${rofs}=${ro_opt}"
1308 done
1310 mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
1312 esac
1315 get_custom_mounts ()
1317 # Side-effect: leaves $devices with persistence.conf mounted in /live/persistence
1318 # Side-effect: prints info to file $custom_mounts
1320 local custom_mounts devices bindings links
1321 custom_mounts=${1}
1322 shift
1323 devices=${@}
1325 bindings="/tmp/bindings.list"
1326 links="/tmp/links.list"
1327 rm -rf ${bindings} ${links} 2> /dev/null
1329 for device in ${devices}
1331 local device_name backing include_list
1332 device_name="$(basename ${device})"
1333 backing=$(mount_persistence_media ${device})
1334 if [ -z "${backing}" ]
1335 then
1336 continue
1339 if [ -r "${backing}/${persistence_list}" ]
1340 then
1341 include_list="${backing}/${persistence_list}"
1342 else
1343 continue
1346 if [ -n "${LIVE_BOOT_DEBUG}" ] && [ -e "${include_list}" ]
1347 then
1348 cp ${include_list} /live/persistence/${persistence_list}.${device_name}
1351 while read dir options # < ${include_list}
1353 if echo ${dir} | grep -qe "^[[:space:]]*\(#.*\)\?$"
1354 then
1355 # skipping empty or commented lines
1356 continue
1359 if trim_path ${dir} | grep -q -e "^[^/]" -e "^/lib" -e "^/lib/live\(/.*\)\?$" -e "^/\(.*/\)\?\.\.\?\(/.*\)\?$"
1360 then
1361 log_warning_msg "Skipping unsafe custom mount ${dir}: must be an absolute path containing neither the \".\" nor \"..\" special dirs, and cannot be \"/lib\", or \"/lib/live\" or any of its sub-directories."
1362 continue
1365 local opt_source opt_link source full_source full_dest
1366 opt_source=""
1367 opt_link=""
1368 for opt in $(echo ${options} | tr ',' ' ');
1370 case "${opt}" in
1371 source=*)
1372 opt_source=${opt#source=}
1374 link)
1375 opt_link="true"
1377 union|bind)
1380 log_warning_msg "Skipping custom mount with unkown option: ${opt}"
1381 continue 2
1383 esac
1384 done
1386 source="${dir}"
1387 if [ -n "${opt_source}" ]
1388 then
1389 if echo ${opt_source} | grep -q -e "^/" -e "^\(.*/\)\?\.\.\?\(/.*\)\?$" && [ "${opt_source}" != "." ]
1390 then
1391 log_warning_msg "Skipping unsafe custom mount with option source=${opt_source}: must be either \".\" (the media root) or a relative path w.r.t. the media root that contains neither comas, nor the special \".\" and \"..\" path components"
1392 continue
1393 else
1394 source="${opt_source}"
1398 full_source="$(trim_path ${backing}/${source})"
1399 full_dest="$(trim_path ${rootmnt}/${dir})"
1400 if [ -n "${opt_link}" ]
1401 then
1402 echo "${device} ${full_source} ${full_dest} ${options}" >> ${links}
1403 else
1404 echo "${device} ${full_source} ${full_dest} ${options}" >> ${bindings}
1406 done < ${include_list}
1407 done
1409 # We sort the list according to destination so we're sure that
1410 # we won't hide a previous mount. We also ignore duplicate
1411 # destinations in a more or less arbitrary way.
1412 [ -e "${bindings}" ] && sort -k3 -sbu ${bindings} >> ${custom_mounts} && rm ${bindings}
1414 # After all mounts are considered we add symlinks so they
1415 # won't be hidden by some mount.
1416 [ -e "${links}" ] && cat ${links} >> ${custom_mounts} && rm ${links}
1418 # We need to make sure that no two custom mounts have the same sources
1419 # or are nested; if that is the case, too much weird stuff can happen.
1420 local prev_source prev_dest
1421 prev_source="impossible source" # first iteration must not match
1422 prev_dest=""
1423 # This sort will ensure that a source /a comes right before a source
1424 # /a/b so we only need to look at the previous source
1425 sort -k2 -b ${custom_mounts} |
1426 while read device source dest options
1428 if echo ${source} | grep -qe "^${prev_source}\(/.*\)\?$"
1429 then
1430 panic "Two persistence mounts have the same or nested sources: ${source} on ${dest}, and ${prev_source} on ${prev_dest}"
1432 prev_source=${source}
1433 prev_dest=${dest}
1434 done
1437 activate_custom_mounts ()
1439 local custom_mounts used_devices
1440 custom_mounts="${1}" # the ouput from get_custom_mounts()
1441 used_devices=""
1443 while read device source dest options # < ${custom_mounts}
1445 local opt_bind opt_link opt_union
1446 opt_bind="true"
1447 opt_link=""
1448 opt_union=""
1449 for opt in $(echo ${options} | tr ',' ' ');
1451 case "${opt}" in
1452 bind)
1453 opt_bind="true"
1454 unset opt_link opt_union
1456 link)
1457 opt_link="true"
1458 unset opt_bind opt_union
1460 union)
1461 opt_union="true"
1462 unset opt_bind opt_link
1464 esac
1465 done
1467 if [ -n "$(what_is_mounted_on "${dest}")" ]
1468 then
1469 if [ "${dest}" = "${rootmnt}" ]
1470 then
1471 umount "${dest}"
1472 else
1473 log_warning_msg "Skipping custom mount ${dest}: $(what_is_mounted_on "${dest}") is already mounted there"
1474 continue
1478 if [ ! -d "${dest}" ]
1479 then
1480 # create the destination and delete existing files in
1481 # its path that are in the way
1482 path="/"
1483 for dir in $(echo ${dest} | sed -e 's|/\+| |g')
1485 path=$(trim_path ${path}/${dir})
1486 if [ -f ${path} ]
1487 then
1488 rm -f ${path}
1490 if [ ! -e ${path} ]
1491 then
1492 mkdir -p ${path}
1493 if echo ${path} | grep -qe "^${rootmnt}/*home/[^/]\+"
1494 then
1495 # if ${dest} is in /home try fixing proper ownership by assuming that the intended user is the first, which is usually the case
1496 # FIXME: this should really be handled by live-config since we don't know for sure which uid a certain user has until then
1497 chown 1000:1000 ${path}
1500 done
1503 # if ${source} doesn't exist on our persistence media
1504 # we bootstrap it with $dest from the live filesystem.
1505 # this both makes sense and is critical if we're
1506 # dealing with /etc or other system dir.
1507 if [ ! -d "${source}" ]
1508 then
1509 if [ -n "${PERSISTENCE_READONLY}" ]
1510 then
1511 continue
1512 elif [ -n "${opt_union}" ] || [ -n "${opt_link}" ]
1513 then
1514 # unions and don't need to be bootstrapped
1515 # link dirs can't be bootstrapped in a sensible way
1516 mkdir -p "${source}"
1517 chown_ref "${dest}" "${source}"
1518 chmod_ref "${dest}" "${source}"
1519 elif [ -n "${opt_bind}" ]
1520 then
1521 # ensure that $dest is not copied *into* $source
1522 mkdir -p "$(dirname ${source})"
1523 cp -a "${dest}" "${source}"
1527 # XXX: If CONFIG_AUFS_ROBR is added to the Debian kernel we can
1528 # ignore the loop below and set rootfs_dest_backing=$dest
1529 local rootfs_dest_backing
1530 rootfs_dest_backing=""
1531 if [ -n "${opt_link}" ] || [ -n "${opt_union}" ]
1532 then
1533 for d in /live/rootfs/*
1535 if [ -n "${rootmnt}" ]
1536 then
1537 fs="${d}/$(echo ${dest} | sed -e "s|${rootmnt}||")"
1538 else
1539 fs="${d}/${dest}"
1541 if [ -d "${fs}" ]
1542 then
1543 rootfs_dest_backing="${rootfs_dest_backing} ${fs}"
1545 done
1548 local cow_dir links_source
1549 if [ -n "${opt_link}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1550 then
1551 link_files ${source} ${dest} "s|^/live/|/lib/live/mount/|"
1552 elif [ -n "${opt_link}" ] && [ -n "${PERSISTENCE_READONLY}" ]
1553 then
1554 mkdir -p ${rootmnt}/lib/live/mount/persistence
1555 links_source=$(mktemp -d ${rootmnt}/lib/live/mount/persistence/links-source-XXXXXX)
1556 chown_ref ${source} ${links_source}
1557 chmod_ref ${source} ${links_source}
1558 # We put the cow dir in the below strange place to
1559 # make it absolutely certain that the link source
1560 # has its own directory and isn't nested with some
1561 # other custom mount (if so that mount's files would
1562 # be linked, causing breakage.
1563 cow_dir="/live/overlay/lib/live/mount/persistence/$(basename ${links_source})"
1564 mkdir -p ${cow_dir}
1565 chown_ref "${source}" "${cow_dir}"
1566 chmod_ref "${source}" "${cow_dir}"
1567 do_union ${links_source} ${cow_dir} ${source} ${rootfs_dest_backing}
1568 link_files ${links_source} ${dest} "s|^${rootmnt}||"
1569 elif [ -n "${opt_union}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1570 then
1571 do_union ${dest} ${source} ${rootfs_dest_backing}
1572 elif [ -n "${opt_bind}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1573 then
1574 mount --bind "${source}" "${dest}"
1575 elif [ -n "${opt_bind}" -o -n "${opt_union}" ] && [ -n "${PERSISTENCE_READONLY}" ]
1576 then
1577 # bind-mount and union mount are handled the same
1578 # in read-only mode, but note that rootfs_dest_backing
1579 # is non-empty (and necessary) only for unions
1580 cow_dir="/live/overlay/${dest}"
1581 if [ -e "${cow_dir}" ] && [ -z "${opt_link}" ]
1582 then
1583 # If an earlier custom mount has files here
1584 # it will "block" the current mount's files
1585 # which is undesirable
1586 rm -rf "${cow_dir}"
1588 mkdir -p ${cow_dir}
1589 chown_ref "${source}" "${cow_dir}"
1590 chmod_ref "${source}" "${cow_dir}"
1591 do_union ${dest} ${cow_dir} ${source} ${rootfs_dest_backing}
1594 PERSISTENCE_IS_ON="1"
1595 export PERSISTENCE_IS_ON
1597 if echo ${used_devices} | grep -qve "^\(.* \)\?${device}\( .*\)\?$"
1598 then
1599 used_devices="${used_devices} ${device}"
1601 done < ${custom_mounts}
1603 echo ${used_devices}
1606 is_mountpoint ()
1608 directory="$1"
1610 [ $(stat -fc%d:%D "${directory}") != $(stat -fc%d:%D "${directory}/..") ]