1 # live-boot helper functions, used by live-boot on boot and by live-snapshot
3 if [ ! -x "/bin/fstype" ]
5 # klibc not in path -> not in initramfs
6 export PATH="${PATH}:/usr/lib/klibc/bin"
9 # handle upgrade path from old udev (using udevinfo) to
10 # recent versions of udev (using udevadm info)
11 if [ -x /sbin/udevadm ]
13 udevinfo='/sbin/udevadm info'
18 old_root_overlay_label="live-rw"
19 old_home_overlay_label="home-rw"
20 custom_overlay_label="custom-ov"
21 root_snapshot_label="live-sn"
22 old_root_snapshot_label="live-sn"
23 home_snapshot_label="home-sn"
24 persistence_list="live-persistence.conf"
26 is_in_list_separator_helper () {
32 echo ${list} | grep -qe "^\(.*${sep}\)\?${element}\(${sep}.*\)\?$"
35 is_in_space_sep_list () {
38 is_in_list_separator_helper "[[:space:]]" "${element}" "${*}"
41 is_in_comma_sep_list () {
44 is_in_list_separator_helper "," "${element}" "${*}"
50 echo "/dev/$($udevinfo -q name -p ${sysdev} 2>/dev/null|| echo ${sysdev##*/})"
58 for dev in "${sysblock}"/* "${sysblock}"
60 if [ -e "${dev}/dev" ]
71 black_listed_devices="${1}"
72 white_listed_devices="${2}"
74 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "loop|ram|fd")
76 fulldevname=$(sys2dev "${sysblock}")
78 if is_in_space_sep_list ${fulldevname} ${black_listed_devices} || \
79 [ -n "${white_listed_devices}" ] && \
80 ! is_in_space_sep_list ${fulldevname} ${white_listed_devices}
82 # skip this device entirely
86 for dev in $(subdevices "${sysblock}")
88 devname=$(sys2dev "${dev}")
90 if is_in_space_sep_list ${devname} ${black_listed_devices}
105 # Validate input first
106 if [ -z "${fstype}" ]
111 # Try to look if it is already supported by the kernel
112 if grep -q ${fstype} /proc/filesystems
116 # Then try to add support for it the gentle way using the initramfs capabilities
118 if grep -q ${fstype} /proc/filesystems
121 # Then try the hard way if /root is already reachable
123 kmodule="/root/lib/modules/`uname -r`/${fstype}/${fstype}.ko"
124 if [ -e "${kmodule}" ]
127 if grep -q ${fstype} /proc/filesystems
140 /sbin/blkid -s TYPE -o value $1 2>/dev/null
147 grep -m1 "^${device} " /proc/mounts | cut -f2 -d ' '
151 # remove all unnecessary /:s in the path, including last one (except
152 # if path is just "/")
153 echo ${1} | sed 's|//\+|/|g' | sed 's|^\(.*[^/]\)/$|\1|'
156 what_is_mounted_on ()
158 local dir="$(trim_path ${1})"
159 grep -m1 "^[^ ]\+ ${dir} " /proc/mounts | cut -d' ' -f1
164 local reference="${1}"
167 local owner=$(stat -c %u:%g "${reference}")
168 chown -h ${owner} ${targets}
173 local reference="${1}"
176 local rights=$(stat -c %a "${reference}")
177 chmod ${rights} ${targets}
193 mounts="$(awk '{print $2}' /proc/mounts)"
194 testpath="$(busybox realpath ${testpath})"
198 if echo "${mounts}" | grep -qs "^${testpath}"
200 set -- $(echo "${mounts}" | grep "^${testpath}" | lastline)
204 testpath=$(dirname $testpath)
211 # Returns used/free fs kbytes + 5% more
212 # You could pass a block device as ${1} or the mount point as ${2}
218 if [ -z "${mountp}" ]
220 mountp="$(where_is_mounted ${dev})"
222 if [ -z "${mountp}" ]
224 mountp="/mnt/tmp_fs_size"
227 mount -t $(get_fstype "${dev}") -o ro "${dev}" "${mountp}" || log_warning_msg "cannot mount -t $(get_fstype ${dev}) -o ro ${dev} ${mountp}"
233 if [ "${used}" = "used" ]
235 size=$(du -ks ${mountp} | cut -f1)
236 size=$(expr ${size} + ${size} / 20 ) # FIXME: 5% more to be sure
239 size="$(df -k | grep -s ${mountp} | awk '{print $4}')"
242 if [ -n "${doumount}" ]
244 umount "${mountp}" || log_warning_msg "cannot umount ${mountp}"
254 if [ -x /bin/loadkeys -a -r /etc/boottime.kmap.gz ]
256 loadkeys /etc/boottime.kmap.gz
266 local encryption=${5}
269 # the output of setup_loop is evaluated in other functions,
270 # modprobe leaks kernel options like "libata.dma=0"
271 # as "options libata dma=0" on stdout, causing serious
272 # problems therefor, so instead always avoid output to stdout
273 modprobe -q -b "${module}" 1>/dev/null
277 for loopdev in ${pattern}
279 if [ "$(cat ${loopdev}/size)" -eq 0 ]
281 dev=$(sys2dev "${loopdev}")
284 if [ -n "${readonly}" ]
286 if losetup --help 2>&1 | grep -q -- "-r\b"
288 options="${options} -r"
292 if [ -n "${offset}" ] && [ 0 -lt "${offset}" ]
294 options="${options} -o ${offset}"
297 if [ -z "${encryption}" ]
299 losetup ${options} "${dev}" "${fspath}"
301 # Loop AES encryption
306 echo -n "Enter passphrase for root filesystem: " >&6
308 echo "${passphrase}" > /tmp/passphrase
310 exec 9</tmp/passphrase
311 /sbin/losetup ${options} -e "${encryption}" -p 9 "${dev}" "${fspath}"
314 rm -f /tmp/passphrase
316 if [ 0 -eq ${error} ]
323 echo -n "There was an error decrypting the root filesystem ... Retry? [Y/n] " >&6
326 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
339 panic "No loop devices available"
349 old_mountp="$(where_is_mounted ${dev})"
351 if [ -n "${old_mountp}" ]
353 if [ "${opts}" != "ro" ]
355 mount -o remount,"${opts}" "${dev}" "${old_mountp}" || panic "Remounting ${dev} ${opts} on ${old_mountp} failed"
358 mount -o bind "${old_mountp}" "${mountp}" || panic "Cannot bind-mount ${old_mountp} on ${mountp}"
360 if [ -z "${fstype}" ]
362 fstype=$(get_fstype "${dev}")
364 mount -t "${fstype}" -o "${opts}" "${dev}" "${mountp}" || \
365 ( echo "SKIPPING: Cannot mount ${dev} on ${mountp}, fstype=${fstype}, options=${opts}" > boot.log && return 0 )
369 mount_persistence_media ()
374 local backing="/live/persistence/$(basename ${device})"
376 mkdir -p "${backing}"
377 local old_backing="$(where_is_mounted ${device})"
378 if [ -z "${old_backing}" ]
380 local fstype="$(get_fstype ${device})"
381 local mount_opts="rw,noatime"
382 if [ -n "${PERSISTENCE_READONLY}" ]
384 mount_opts="ro,noatime"
386 if mount -t "${fstype}" -o "${mount_opts}" "${device}" "${backing}" >/dev/null
391 [ -z "${probe}" ] && log_warning_msg "Failed to mount persistence media ${device}"
395 elif [ "${backing}" != "${old_backing}" ]
397 if mount --move ${old_backing} ${backing} >/dev/null
402 [ -z "${probe}" ] && log_warning_msg "Failed to move persistence media ${device}"
410 close_persistence_media () {
412 local backing="$(where_is_mounted ${device})"
414 if [ -d "${backing}" ]
416 umount "${backing}" >/dev/null 2>&1
417 rmdir "${backing}" >/dev/null 2>&1
420 if is_active_luks_mapping ${device}
422 /sbin/cryptsetup luksClose ${device}
429 name="$(basename ${dev})"
431 if [ -n "${PERSISTENCE_READONLY}" ]
433 opts="${opts} --readonly"
436 if /sbin/cryptsetup status "${name}" >/dev/null 2>&1
438 re="^[[:space:]]*device:[[:space:]]*\([^[:space:]]*\)$"
439 opened_dev=$(cryptsetup status ${name} 2>/dev/null | grep "${re}" | sed "s|${re}|\1|")
440 if [ "${opened_dev}" = "${dev}" ]
442 luks_device="/dev/mapper/${name}"
446 log_warning_msg "Cannot open luks device ${dev} since ${opened_dev} already is opened with its name"
455 /lib/cryptsetup/askpass "Enter passphrase for ${dev}: " | \
456 /sbin/cryptsetup -T 1 luksOpen ${dev} ${name} ${opts}
460 luks_device="/dev/mapper/${name}"
466 echo -n "There was an error decrypting ${dev} ... Retry? [Y/n] " >&6
469 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
479 /sbin/blkid -s PART_ENTRY_NAME -p -o value ${dev} 2>/dev/null
485 [ "$(/sbin/blkid -s PART_ENTRY_SCHEME -p -o value ${dev} 2>/dev/null)" = "gpt" ]
488 probe_for_gpt_name ()
490 local overlays="${1}"
491 local snapshots="${2}"
494 local gpt_dev="${dev}"
495 if is_active_luks_mapping ${dev}
497 # if $dev is an opened luks device, we need to check
498 # GPT stuff on the backing device
499 gpt_dev=$(get_luks_backing_device "${dev}")
502 if ! is_gpt_device ${gpt_dev}
507 local gpt_name=$(get_gpt_name ${gpt_dev})
508 for label in ${overlays} ${snapshots}
510 if [ "${gpt_name}" = "${label}" ]
512 echo "${label}=${dev}"
517 probe_for_fs_label ()
519 local overlays="${1}"
520 local snapshots="${2}"
523 for label in ${overlays} ${snapshots}
525 if [ "$(/sbin/blkid -s LABEL -o value $dev 2>/dev/null)" = "${label}" ]
527 echo "${label}=${dev}"
532 probe_for_file_name ()
534 local overlays="${1}"
535 local snapshots="${2}"
539 local backing="$(mount_persistence_media ${dev} probe)"
540 if [ -z "${backing}" ]
545 for label in ${overlays}
547 path=${backing}/${PERSISTENCE_PATH}${label}
550 local loopdev=$(setup_loop "${path}" "loop" "/sys/block/loop*")
551 ret="${ret} ${label}=${loopdev}"
554 for label in ${snapshots}
556 for ext in squashfs cpio.gz ext2 ext3 ext4 jffs2
558 path="${PERSISTENCE_PATH}${label}.${ext}"
559 if [ -f "${backing}/${path}" ]
561 ret="${ret} ${label}=${dev}:${backing}:${path}"
570 umount ${backing} > /dev/null 2>&1 || true
574 find_persistence_media ()
576 # Scans devices for overlays and snapshots, and returns a whitespace
577 # separated list of how to use them. Only overlays with a partition
578 # label or file name in ${overlays} are returned, and ditto for
579 # snapshots with labels in ${snapshots}.
581 # When scanning a LUKS device, the user will be asked to enter the
582 # passphrase; on failure to enter it, or if no persistence partitions
583 # or files were found, the LUKS device is closed.
585 # For a snapshot file the return value is ${label}=${snapdata}", where
586 # ${snapdata} is the parameter used for try_snap().
588 # For all other cases (overlay/snapshot partition and overlay file) the
589 # return value is "${label}=${device}", where ${device} a device that
590 # can mount the content. In the case of an overlay file, the device
591 # containing the file will remain mounted as a side-effect.
593 # No devices in ${black_listed_devices} will be scanned, and if
594 # ${white_list_devices} is non-empty, only devices in it will be
597 local overlays="${1}"
598 local snapshots="${2}"
599 local white_listed_devices="${3}"
602 local black_listed_devices="$(what_is_mounted_on /live/image)"
604 for dev in $(storage_devices "${black_listed_devices}" "${white_listed_devices}")
609 # Check if it's a luks device; we'll have to open the device
610 # in order to probe any filesystem it contains, like we do
611 # below. activate_custom_mounts() also depends on that any luks
612 # device already has been opened.
613 if is_in_comma_sep_list luks ${PERSISTENCE_ENCRYPTION} && \
614 is_luks_partition ${dev}
616 if luks_device=$(open_luks_device "${dev}")
620 # skip $dev since we failed/chose not to open it
623 elif ! is_in_comma_sep_list none ${PERSISTENCE_ENCRYPTION}
625 # skip $dev since we don't allow unencrypted storage
629 # Probe for matching GPT partition names or filesystem labels
630 if is_in_comma_sep_list filesystem ${PERSISTENCE_STORAGE}
632 result=$(probe_for_gpt_name "${overlays}" "${snapshots}" ${dev})
633 if [ -n "${result}" ]
635 ret="${ret} ${result}"
639 result=$(probe_for_fs_label "${overlays}" "${snapshots}" ${dev})
640 if [ -n "${result}" ]
642 ret="${ret} ${result}"
647 # Probe for files with matching name on mounted partition
648 if is_in_comma_sep_list file ${PERSISTENCE_STORAGE}
650 result=$(probe_for_file_name "${overlays}" "${snapshots}" ${dev})
651 if [ -n "${result}" ]
653 ret="${ret} ${result}"
658 # Close luks device if it isn't used
659 if [ -z "${result}" ] && [ -n "${luks_device}" ] && \
660 is_active_luks_mapping "${luks_device}"
662 /sbin/cryptsetup luksClose "${luks_device}"
676 for adaptor in /sys/class/net/*
678 status="$(cat ${adaptor}/iflink)"
680 if [ "${status}" -eq 2 ]
682 mac="$(cat ${adaptor}/address)"
683 mac="$(echo ${mac} | sed 's/:/-/g' | tr '[a-z]' '[A-Z]')"
693 /sbin/cryptsetup isLuks "${device}" 1>/dev/null 2>&1
696 is_active_luks_mapping ()
699 /sbin/cryptsetup status "${device}" 1>/dev/null 2>&1
702 get_luks_backing_device () {
704 cryptsetup status ${device} 2> /dev/null | \
705 awk '{if ($1 == "device:") print $2}'
714 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
717 if [ "$(cat ${sysblock}/removable)" = "1" ]
719 if [ -z "${want_usb}" ]
723 if readlink ${sysblock} | grep -q usb
730 if [ "${dev_ok}" = "yes" ]
732 case "${output_format}" in
734 ret="${ret} ${sysblock}"
737 devname=$(sys2dev "${sysblock}")
738 ret="${ret} ${devname}"
751 removable_dev "${output_format}" "want_usb"
759 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
761 if [ "$(cat ${sysblock}/removable)" = "0" ]
763 case "${output_format}" in
765 ret="${ret} ${sysblock}"
768 devname=$(sys2dev "${sysblock}")
769 ret="${ret} ${devname}"
780 # create source's directory structure in dest, and recursively
781 # create symlinks in dest to to all files in source. if mask
782 # is non-empty, remove mask from all source paths when
783 # creating links (will be necessary if we change root, which
784 # live-boot normally does (into $rootmnt)).
786 # remove multiple /:s and ensure ending on /
787 local src_dir="$(trim_path ${1})/"
788 local dest_dir="$(trim_path ${2})/"
789 local src_mask="${3}"
791 # This check can only trigger on the inital, non-recursive call since
792 # we create the destination before recursive calls
793 if [ ! -d "${dest_dir}" ]
795 log_warning_msg "Must link_files into a directory"
799 find "${src_dir}" -mindepth 1 -maxdepth 1 | while read src; do
800 local dest="${dest_dir}$(basename "${src}")"
803 if [ -z "$(ls -A "${src}")" ]
807 if [ ! -d "${dest}" ]
810 chown_ref "${src}" "${dest}"
811 chmod_ref "${src}" "${dest}"
813 link_files "${src}" "${dest}" "${src_mask}"
815 local final_src=${src}
816 if [ -n "${src_mask}" ]
818 final_src="$(echo ${final_src} | sed "s|^${src_mask}||")"
820 rm -rf "${dest}" 2> /dev/null
821 ln -s "${final_src}" "${dest}"
822 chown_ref "${src}" "${dest}"
829 local unionmountpoint="${1}" # directory where the union is mounted
830 local unionrw="${2}" # branch where the union changes are stored
831 local unionro1="${3}" # first underlying read-only branch (optional)
832 local unionro2="${4}" # second underlying read-only branch (optional)
834 if [ "${UNIONTYPE}" = "aufs" ]
839 elif [ "${UNIONTYPE}" = "unionfs-fuse" ]
848 case "${UNIONTYPE}" in
850 unionmountopts="-o cow -o noinitgroups -o default_permissions -o allow_other -o use_ino -o suid"
851 unionmountopts="${unionmountopts} ${unionrw}=${rw_opt}"
852 if [ -n "${unionro1}" ]
854 unionmountopts="${unionmountopts}:${unionro1}=${ro_opt}"
856 if [ -n "${unionro2}" ]
858 unionmountopts="${unionmountopts}:${unionro2}=${ro_opt}"
860 ( sysctl -w fs.file-max=391524 ; ulimit -HSn 16384
861 unionfs-fuse ${unionmountopts} "${unionmountpoint}" ) && \
862 ( mkdir -p /run/sendsigs.omit.d
863 pidof unionfs-fuse >> /run/sendsigs.omit.d/unionfs-fuse || true )
867 # XXX: can unionro2 be used? (overlayfs only handles two dirs, but perhaps they can be chained?)
868 # XXX: and can unionro1 be optional? i.e. can overlayfs skip lowerdir?
869 unionmountopts="-o noatime,lowerdir=${unionro1},upperdir=${unionrw}"
870 mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
874 unionmountopts="-o noatime,${noxino_opt},dirs=${unionrw}=${rw_opt}"
875 if [ -n "${unionro1}" ]
877 unionmountopts="${unionmountopts}:${unionro1}=${ro_opt}"
879 if [ -n "${unionro2}" ]
881 unionmountopts="${unionmountopts}:${unionro2}=${ro_opt}"
883 mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
890 # Side-effect: leaves $devices with live-persistence.conf mounted in /live/persistence
891 # Side-effect: prints info to file $custom_mounts
893 local custom_mounts=${1}
897 local bindings="/tmp/bindings.list"
898 local links="/tmp/links.list"
899 rm -rf ${bindings} ${links} 2> /dev/null
901 for device in ${devices}
903 if [ ! -b "${device}" ]
908 local device_name="$(basename ${device})"
909 local backing=$(mount_persistence_media ${device})
910 if [ -z "${backing}" ]
915 local include_list="${backing}/${persistence_list}"
916 if [ ! -r "${include_list}" ]
921 if [ -n "${DEBUG}" ] && [ -e "${include_list}" ]
923 cp ${include_list} /live/persistence/${persistence_list}.${device_name}
926 while read dir options # < ${include_list}
928 if echo ${dir} | grep -qe "^[[:space:]]*\(#.*\)\?$"
930 # skipping empty or commented lines
934 if trim_path ${dir} | grep -q -e "^[^/]" -e "^/live\(/.*\)\?$" -e "^/\(.*/\)\?\.\.\?\(/.*\)\?$"
936 log_warning_msg "Skipping unsafe custom mount ${dir}: must be an absolute path containing neither the \".\" nor \"..\" special dirs, and cannot be \"/live\" or any sub-directory therein."
942 for opt in $(echo ${options} | tr ',' ' ');
946 opt_source=${opt#source=}
954 log_warning_msg "Skipping custom mount with unkown option: ${opt}"
960 local source="${dir}"
961 if [ -n "${opt_source}" ]
963 if echo ${opt_source} | grep -q -e "^/" -e "^\(.*/\)\?\.\.\?\(/.*\)\?$" && [ "${source}" != "." ]
965 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"
968 source="${opt_source}"
972 local full_source="$(trim_path ${backing}/${source})"
973 local full_dest="$(trim_path ${rootmnt}/${dir})"
974 if [ -n "${opt_link}" ]
976 echo "${device} ${full_source} ${full_dest} ${options}" >> ${links}
978 echo "${device} ${full_source} ${full_dest} ${options}" >> ${bindings}
980 done < ${include_list}
983 # We sort the list according to destination so we're sure that
984 # we won't hide a previous mount. We also ignore duplicate
985 # destinations in a more or less arbitrary way.
986 [ -e "${bindings}" ] && sort -k3 -sbu ${bindings} >> ${custom_mounts} && rm ${bindings}
988 # After all mounts are considered we add symlinks so they
989 # won't be hidden by some mount.
990 [ -e "${links}" ] && cat ${links} >> ${custom_mounts} && rm ${links}
992 # We need to make sure that no two custom mounts have the same sources
993 # or are nested; if that is the case, too much weird stuff can happen.
994 local prev_source="impossible source" # first iteration must not match
996 # This sort will ensure that a source /a comes right before a source
997 # /a/b so we only need to look at the previous source
998 sort -k2 -b ${custom_mounts} |
999 while read device source dest options
1001 if echo ${source} | grep -qe "^${prev_source}\(/.*\)\?$"
1003 panic "Two persistence mounts have the same or nested sources: ${source} on ${dest}, and ${prev_source} on ${prev_dest}"
1005 prev_source=${source}
1010 activate_custom_mounts ()
1012 local custom_mounts="${1}" # the ouput from get_custom_mounts()
1013 local used_devices=""
1015 while read device source dest options # < ${custom_mounts}
1017 local opt_bind="yes"
1020 for opt in $(echo ${options} | tr ',' ' ');
1025 unset opt_link opt_union
1029 unset opt_bind opt_union
1033 unset opt_bind opt_link
1038 if [ -n "$(what_is_mounted_on "${dest}")" ]
1040 if [ "${dest}" = "${rootmnt}" ]
1044 log_warning_msg "Skipping custom mount ${dest}: $(what_is_mounted_on "${dest}") is already mounted there"
1049 if [ ! -d "${dest}" ]
1051 # create the destination and delete existing files in
1052 # its path that are in the way
1054 for dir in $(echo ${dest} | sed -e 's|/\+| |g')
1056 path=$(trim_path ${path}/${dir})
1064 if echo ${path} | grep -qe "^${rootmnt}/*home/[^/]\+"
1066 # if ${dest} is in /home try fixing proper ownership by assuming that the intended user is the first, which is usually the case
1067 # FIXME: this should really be handled by live-config since we don't know for sure which uid a certain user has until then
1068 chown 1000:1000 ${path}
1074 # if ${source} doesn't exist on our persistence media
1075 # we bootstrap it with $dest from the live filesystem.
1076 # this both makes sense and is critical if we're
1077 # dealing with /etc or other system dir.
1078 if [ ! -d "${source}" ]
1080 if [ -n "${PERSISTENCE_READONLY}" ]
1083 elif [ -n "${opt_union}" ] || [ -n "${opt_link}" ]
1085 # unions and don't need to be bootstrapped
1086 # link dirs can't be bootstrapped in a sensible way
1087 mkdir -p "${source}"
1088 chown_ref "${dest}" "${source}"
1089 chmod_ref "${dest}" "${source}"
1090 elif [ -n "${opt_bind}" ]
1092 # ensure that $dest is not copied *into* $source
1093 mkdir -p "$(dirname ${source})"
1094 cp -a "${dest}" "${source}"
1098 # XXX: If CONFIG_AUFS_ROBR is added to the Debian kernel we can
1099 # ignore the loop below and set rofs_dest_backing=$dest
1100 local rofs_dest_backing=""
1101 if [ -n "${opt_link}"]
1103 for d in /live/rofs/*
1105 if [ -n "${rootmnt}" ]
1107 rofs_dest_backing="${d}/$(echo ${dest} | sed -e "s|${rootmnt}||")"
1109 rofs_dest_backing="${d}/${dest}"
1111 if [ -d "${rofs_dest_backing}" ]
1115 rofs_dest_backing=""
1120 if [ -n "${opt_link}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1122 link_files ${source} ${dest} ${rootmnt}
1123 elif [ -n "${opt_link}" ] && [ -n "${PERSISTENCE_READONLY}" ]
1125 mkdir -p /live/persistence
1126 local links_source=$(mktemp -d /live/persistence/links-source-XXXXXX)
1127 chown_ref ${source} ${links_source}
1128 chmod_ref ${source} ${links_source}
1129 # We put the cow dir in the below strange place to
1130 # make it absolutely certain that the link source
1131 # has its own directory and isn't nested with some
1132 # other custom mount (if so that mount's files would
1133 # be linked, causing breakage.
1134 local cow_dir="/live/overlay/live/persistence/$(basename ${links_source})"
1136 chown_ref "${source}" "${cow_dir}"
1137 chmod_ref "${source}" "${cow_dir}"
1138 do_union ${links_source} ${cow_dir} ${source} ${rofs_dest_backing}
1139 link_files ${links_source} ${dest} ${rootmnt}
1140 elif [ -n "${opt_union}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1142 do_union ${dest} ${source} ${rofs_dest_backing}
1143 elif [ -n "${opt_bind}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1145 mount --bind "${source}" "${dest}"
1146 elif [ -n "${opt_bind}" -o -n "${opt_union}" ] && [ -n "${PERSISTENCE_READONLY}" ]
1148 # bind-mount and union mount are handled the same
1149 # in read-only mode, but note that rofs_dest_backing
1150 # is non-empty (and necessary) only for unions
1151 if [ -n "${rootmnt}" ]
1153 local cow_dir="$(echo ${dest} | sed -e "s|^${rootmnt}|/live/overlay/|")"
1155 # This is happens if persistence is activated
1157 local cow_dir="/live/overlay/${dest}"
1159 if [ -e "${cow_dir}" ] && [ -z "${opt_link}" ]
1161 # If an earlier custom mount has files here
1162 # it will "block" the current mount's files
1163 # which is undesirable
1167 chown_ref "${source}" "${cow_dir}"
1168 chmod_ref "${source}" "${cow_dir}"
1169 do_union ${dest} ${cow_dir} ${source} ${rofs_dest_backing}
1172 PERSISTENCE_IS_ON="1"
1173 export PERSISTENCE_IS_ON
1175 if echo ${used_devices} | grep -qve "^\(.* \)\?${device}\( .*\)\?$"
1177 used_devices="${used_devices} ${device}"
1179 done < ${custom_mounts}
1181 echo ${used_devices}
1184 fix_backwards_compatibility ()
1190 if [ -n "${PERSISTENCE_READONLY}" ]
1195 local backing="$(mount_persistence_media ${device})"
1196 if [ -z "${backing}" ]
1201 local include_list="${backing}/${persistence_list}"
1202 if [ ! -r "${include_list}" ]
1204 echo "# persistence backwards compatibility:
1205 ${dir} ${opt},source=." > "${include_list}"
1213 [ $(stat -fc%d:%D "${directory}") != $(stat -fc%d:%D "${directory}/..") ]