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 root_overlay_label="full-ov"
19 old_root_overlay_label="live-rw"
20 old_home_overlay_label="home-rw"
21 custom_overlay_label="custom-ov"
22 root_snapshot_label="live-sn"
23 old_root_snapshot_label="live-sn"
24 home_snapshot_label="home-sn"
25 persistence_list="live-persistence.conf"
32 for ARGUMENT in $(cat /proc/cmdline)
41 export NOACCESSIBILITY NOFASTBOOT NOFSTAB NONETWORKING
45 ACCESS="${ARGUMENT#access=}"
50 DEFCONSOLE="${ARGUMENT#*=}"
66 # Force dhcp even while netbooting
67 # Use for debugging in case somebody works on fixing dhclient
77 DEVICE="${ARGUMENT#ethdevice=}"
79 export DEVICE ETHDEVICE
83 ETHDEV_TIMEOUT="${ARGUMENT#ethdevice-timeout=}"
88 FETCH="${ARGUMENT#fetch=}"
93 FINDISO="${ARGUMENT#findiso=}"
98 FORCEPERSISTENCEFSCK="Yes"
99 export FORCEPERSISTENCEFSCK
103 FTPFS="${ARGUMENT#ftpfs=}"
108 HTTPFS="${ARGUMENT#httpfs=}"
113 ISCSI="${ARGUMENT#iscsi=}"
114 #ip:port - separated by ;
115 ISCSI_PORTAL="${ISCSI%;*}"
116 if echo "${ISCSI_PORTAL}" | grep -q , ; then
117 ISCSI_SERVER="${ISCSI_PORTAL%,*}"
118 ISCSI_PORT="${ISCSI_PORTAL#*,}"
121 ISCSI_TARGET="${ISCSI#*;}"
122 export ISCSI ISCSI_PORTAL ISCSI_TARGET ISCSI_SERVER ISCSI_PORT
126 FROMISO="${ARGUMENT#*=}"
136 INTEGRITY_CHECK="Yes"
137 export INTEGRITY_CHECK
141 STATICIP="${ARGUMENT#ip=}"
143 if [ -z "${STATICIP}" ]
156 live-media=*|bootfrom=*)
157 LIVE_MEDIA="${ARGUMENT#*=}"
161 live-media-encryption=*|encryption=*)
162 LIVE_MEDIA_ENCRYPTION="${ARGUMENT#*=}"
163 export LIVE_MEDIA_ENCRYPTION
167 LIVE_MEDIA_OFFSET="${ARGUMENT#live-media-offset=}"
168 export LIVE_MEDIA_OFFSET
172 LIVE_MEDIA_PATH="${ARGUMENT#live-media-path=}"
173 export LIVE_MEDIA_PATH
176 live-media-timeout=*)
177 LIVE_MEDIA_TIMEOUT="${ARGUMENT#live-media-timeout=}"
178 export LIVE_MEDIA_TIMEOUT
182 MODULE="${ARGUMENT#module=}"
187 NETBOOT="${ARGUMENT#netboot=}"
192 NFSOPTS="${ARGUMENT#nfsopts=}"
197 NFS_COW="${ARGUMENT#nfsoverlay=}"
202 NOACCESSIBILITY="Yes"
203 export NOACCESSIBILITY
222 ramdisk_size="${ARGUMENT#ramdisk-size=}"
235 persistence-encryption=*)
236 PERSISTENCE_ENCRYPTION="${ARGUMENT#*=}"
237 export PERSISTENCE_ENCRYPTION
241 PERSISTENCE_MEDIA="${ARGUMENT#*=}"
242 export PERSISTENCE_MEDIA
244 persistence-method=*)
245 PERSISTENCE_METHOD="${ARGUMENT#*=}"
246 export PERSISTENCE_METHOD
250 PERSISTENCE_PATH="${ARGUMENT#persistence-path=}"
251 export PERSISTENCE_PATH
253 persistence-read-only)
254 PERSISTENCE_READONLY="Yes"
255 export PERSISTENCE_READONLY
258 persistence-storage=*)
259 PERSISTENCE_STORAGE="${ARGUMENT#persistence-storage=}"
260 export PERSISTENCE_STORAGE
263 persistence-subtext=*)
264 root_overlay_label="${root_overlay_label}-${ARGUMENT#persistence-subtext=}"
265 old_root_overlay_label="${old_root_overlay_label}-${ARGUMENT#persistence-subtext=}"
266 old_home_overlay_label="${old_home_overlay_label}-${ARGUMENT#persistence-subtext=}"
267 custom_overlay_label="${custom_overlay_label}-${ARGUMENT#persistence-subtext=}"
268 root_snapshot_label="${root_snapshot_label}-${ARGUMENT#persistence-subtext=}"
269 old_root_snapshot_label="${root_snapshot_label}-${ARGUMENT#persistence-subtext=}"
270 home_snapshot_label="${home_snapshot_label}-${ARGUMENT#persistence-subtext=}"
284 NOPROMPT="${ARGUMENT#noprompt=}"
289 QUICKUSBMODULES="Yes"
290 export QUICKUSBMODULES
293 preseed/file=*|file=*)
294 LOCATIONS="${ARGUMENT#*=} ${LOCATIONS}"
304 question="${ARGUMENT%%=*}"
305 value="${ARGUMENT#*=}"
306 PRESEEDS="${PRESEEDS}\"${question}=${value}\" "
321 TODISK="${ARGUMENT#todisk=}"
332 MODULETORAM="${ARGUMENT#toram=}"
333 export TORAM MODULETORAM
347 SKIP_UNION_MOUNTS="Yes"
348 export SKIP_UNION_MOUNTS
352 ROOT="${ARGUMENT#root=}"
357 UNIONTYPE="${ARGUMENT#union=}"
363 # sort of compatibility with netboot.h from linux docs
364 if [ -z "${NETBOOT}" ]
366 if [ "${ROOT}" = "/dev/nfs" ]
370 elif [ "${ROOT}" = "/dev/cifs" ]
377 if [ -z "${MODULE}" ]
383 if [ -z "${UNIONTYPE}" ]
389 if [ -z "${PERSISTENCE_ENCRYPTION}" ]
391 PERSISTENCE_ENCRYPTION="none"
392 export PERSISTENCE_ENCRYPTION
393 elif echo ${PERSISTENCE_ENCRYPTION} | grep -qe "\<luks\>"
395 if ! modprobe dm-crypt
397 log_warning_msg "Unable to load module dm-crypt"
398 PERSISTENCE_ENCRYPTION=$(echo ${PERSISTENCE_ENCRYPTION} | sed -e 's/\<luks,\|,\?luks$//g')
399 export PERSISTENCE_ENCRYPTION
402 if [ ! -x /lib/cryptsetup/askpass ] || [ ! -x /sbin/cryptsetup ]
404 log_warning_msg "cryptsetup in unavailable"
405 PERSISTENCE_ENCRYPTION=$(echo ${PERSISTENCE_ENCRYPTION} | sed -e 's/\<luks,\|,\?luks$//g')
406 export PERSISTENCE_ENCRYPTION
410 if [ -z "${PERSISTENCE_METHOD}" ]
412 PERSISTENCE_METHOD="snapshot,overlay"
413 export PERSISTENCE_METHOD
416 if [ -z "${PERSISTENCE_STORAGE}" ]
418 PERSISTENCE_STORAGE="filesystem,file"
419 export PERSISTENCE_STORAGE
426 echo "/dev/$($udevinfo -q name -p ${sysdev} 2>/dev/null|| echo ${sysdev##*/})"
434 for dev in "${sysblock}"/* "${sysblock}"
436 if [ -e "${dev}/dev" ]
447 black_listed_devices="${1}"
448 white_listed_devices="${2}"
450 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "loop|ram|fd")
452 fulldevname=$(sys2dev "${sysblock}")
454 if echo "${black_listed_devices}" | grep -qe "\<${fulldevname}\>" || \
455 [ -n "${white_listed_devices}" ] && \
456 echo "${white_listed_devices}" | grep -qve "\<${fulldevname}\>"
458 # skip this device entirely
462 for dev in $(subdevices "${sysblock}")
464 devname=$(sys2dev "${dev}")
466 if echo "${black_listed_devices}" | grep -qe "\<${devname}\>"
468 # skip this subdevice
481 # Validate input first
482 if [ -z "${fstype}" ]
487 # Try to look if it is already supported by the kernel
488 if grep -q ${fstype} /proc/filesystems
492 # Then try to add support for it the gentle way using the initramfs capabilities
494 if grep -q ${fstype} /proc/filesystems
497 # Then try the hard way if /root is already reachable
499 kmodule="/root/lib/modules/`uname -r`/${fstype}/${fstype}.ko"
500 if [ -e "${kmodule}" ]
503 if grep -q ${fstype} /proc/filesystems
516 /sbin/blkid -s TYPE -o value $1 2>/dev/null
523 grep -m1 "^${device} " /proc/mounts | cut -f2 -d ' '
527 # remove all unnecessary /:s in the path, including last one (except
528 # if path is just "/")
529 echo ${1} | sed 's|//\+|/|g' | sed 's|^\(.*[^/]\)/$|\1|'
532 what_is_mounted_on ()
534 local dir="$(trim_path ${1})"
535 grep -m1 "^[^ ]\+ ${dir} " /proc/mounts | cut -d' ' -f1
540 local reference="${1}"
543 local owner=$(stat -c %u:%g "${reference}")
544 chown -h ${owner} ${targets}
549 local reference="${1}"
552 local rights=$(stat -c %a "${reference}")
553 chmod ${rights} ${targets}
569 mounts="$(awk '{print $2}' /proc/mounts)"
570 testpath="$(busybox realpath ${testpath})"
574 if echo "${mounts}" | grep -qs "^${testpath}"
576 set -- $(echo "${mounts}" | grep "^${testpath}" | lastline)
580 testpath=$(dirname $testpath)
587 # Returns used/free fs kbytes + 5% more
588 # You could pass a block device as ${1} or the mount point as ${2}
594 if [ -z "${mountp}" ]
596 mountp="$(where_is_mounted ${dev})"
598 if [ -z "${mountp}" ]
600 mountp="/mnt/tmp_fs_size"
603 mount -t $(get_fstype "${dev}") -o ro "${dev}" "${mountp}" || log_warning_msg "cannot mount -t $(get_fstype ${dev}) -o ro ${dev} ${mountp}"
609 if [ "${used}" = "used" ]
611 size=$(du -ks ${mountp} | cut -f1)
612 size=$(expr ${size} + ${size} / 20 ) # FIXME: 5% more to be sure
615 size="$(df -k | grep -s ${mountp} | awk '{print $4}')"
618 if [ -n "${doumount}" ]
620 umount "${mountp}" || log_warning_msg "cannot umount ${mountp}"
630 if [ -x /bin/loadkeys -a -r /etc/boottime.kmap.gz ]
632 loadkeys /etc/boottime.kmap.gz
642 local encryption=${5}
645 # the output of setup_loop is evaluated in other functions,
646 # modprobe leaks kernel options like "libata.dma=0"
647 # as "options libata dma=0" on stdout, causing serious
648 # problems therefor, so instead always avoid output to stdout
649 modprobe -q -b "${module}" 1>/dev/null
653 for loopdev in ${pattern}
655 if [ "$(cat ${loopdev}/size)" -eq 0 ]
657 dev=$(sys2dev "${loopdev}")
660 if [ -n "${readonly}" ]
662 if losetup --help 2>&1 | grep -q -- "-r\b"
664 options="${options} -r"
668 if [ -n "${offset}" ] && [ 0 -lt "${offset}" ]
670 options="${options} -o ${offset}"
673 if [ -z "${encryption}" ]
675 losetup ${options} "${dev}" "${fspath}"
677 # Loop AES encryption
682 echo -n "Enter passphrase for root filesystem: " >&6
684 echo "${passphrase}" > /tmp/passphrase
686 exec 9</tmp/passphrase
687 /sbin/losetup ${options} -e "${encryption}" -p 9 "${dev}" "${fspath}"
690 rm -f /tmp/passphrase
692 if [ 0 -eq ${error} ]
699 echo -n "There was an error decrypting the root filesystem ... Retry? [Y/n] " >&6
702 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
715 panic "No loop devices available"
725 old_mountp="$(where_is_mounted ${dev})"
727 if [ -n "${old_mountp}" ]
729 if [ "${opts}" != "ro" ]
731 mount -o remount,"${opts}" "${dev}" "${old_mountp}" || panic "Remounting ${dev} ${opts} on ${old_mountp} failed"
734 mount -o bind "${old_mountp}" "${mountp}" || panic "Cannot bind-mount ${old_mountp} on ${mountp}"
736 if [ -z "${fstype}" ]
738 fstype=$(get_fstype "${dev}")
740 mount -t "${fstype}" -o "${opts}" "${dev}" "${mountp}" || \
741 ( echo "SKIPPING: Cannot mount ${dev} on ${mountp}, fstype=${fstype}, options=${opts}" > live-boot.log && return 0 )
745 mount_persistence_media ()
750 # We can't mount into ${rootmnt}/live before ${rootmnt} has been
751 # mounted since that would cover our mountpoint.
752 if [ -n "${rootmnt}" ] && [ -z "$(what_is_mounted_on ${rootmnt})" ]
754 backing="/$(basename ${device})-backing"
756 backing="${rootmnt}/live/persistence/$(basename ${device})"
759 mkdir -p "${backing}"
760 local old_backing="$(where_is_mounted ${device})"
761 if [ -z "${old_backing}" ]
763 local fstype="$(get_fstype ${device})"
764 local mount_opts="rw,noatime"
765 if [ -n "${PERSISTENCE_READONLY}" ]
767 mount_opts="ro,noatime"
769 if mount -t "${fstype}" -o "${mount_opts}" "${device}" "${backing}" >/dev/null
774 log_warning_msg "Failed to mount persistence media ${device}"
777 elif [ "${backing}" != "${old_backing}" ]
779 if mount --move ${old_backing} ${backing} >/dev/null
784 log_warning_msg "Failed to move persistence media ${device}"
791 close_persistence_media () {
793 local backing="$(where_is_mounted ${device})"
795 if [ -d "${backing}" ]
797 umount "${backing}" >/dev/null 2>&1
798 rmdir "${backing}" >/dev/null 2>&1
801 if is_active_luks_mapping ${device}
803 /sbin/cryptsetup luksClose ${device}
810 name="$(basename ${dev})"
812 if [ -n "${PERSISTENCE_READONLY}" ]
814 opts="${opts} --readonly"
817 if /sbin/cryptsetup status "${name}" >/dev/null 2>&1
819 re="^[[:space:]]*device:[[:space:]]*\([^[:space:]]*\)$"
820 opened_dev=$(cryptsetup status ${name} 2>/dev/null | grep "${re}" | sed "s|${re}|\1|")
821 if [ "${opened_dev}" = "${dev}" ]
823 luks_device="/dev/mapper/${name}"
827 log_warning_msg "Cannot open luks device ${dev} since ${opened_dev} already is opened with its name"
836 /lib/cryptsetup/askpass "Enter passphrase for ${dev}: " | \
837 /sbin/cryptsetup -T 1 luksOpen ${dev} ${name} ${opts}
841 luks_device="/dev/mapper/${name}"
847 echo -n "There was an error decrypting ${dev} ... Retry? [Y/n] " >&6
850 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
860 /sbin/blkid -s PART_ENTRY_NAME -p -o value ${dev} 2>/dev/null
866 [ "$(/sbin/blkid -s PART_ENTRY_SCHEME -p -o value ${dev} 2>/dev/null)" = "gpt" ]
869 probe_for_gpt_name ()
871 local overlays="${1}"
872 local snapshots="${2}"
875 local gpt_dev="${dev}"
876 if is_active_luks_mapping ${dev}
878 # if $dev is an opened luks device, we need to check
879 # GPT stuff on the backing device
880 gpt_dev=$(get_luks_backing_device "${dev}")
883 if ! is_gpt_device ${gpt_dev}
888 local gpt_name=$(get_gpt_name ${gpt_dev})
889 for label in ${overlays} ${snapshots}
891 if [ "${gpt_name}" = "${label}" ]
893 echo "${label}=${dev}"
898 probe_for_fs_label ()
900 local overlays="${1}"
901 local snapshots="${2}"
904 for label in ${overlays} ${snapshots}
906 if [ "$(/sbin/blkid -s LABEL -o value $dev 2>/dev/null)" = "${label}" ]
908 echo "${label}=${dev}"
913 probe_for_file_name ()
915 local overlays="${1}"
916 local snapshots="${2}"
920 local backing="$(mount_persistence_media ${dev})"
921 if [ -z "${backing}" ]
926 for label in ${overlays}
928 path=${backing}/${PERSISTENCE_PATH}${label}
931 local loopdev=$(setup_loop "${path}" "loop" "/sys/block/loop*")
932 ret="${ret} ${label}=${loopdev}"
935 for label in ${snapshots}
937 for ext in squashfs cpio.gz ext2 ext3 ext4 jffs2
939 path="${PERSISTENCE_PATH}${label}.${ext}"
940 if [ -f "${backing}/${path}" ]
942 ret="${ret} ${label}=${dev}:${backing}:${path}"
951 umount ${backing} > /dev/null 2>&1 || true
955 find_persistence_media ()
957 # Scans devices for overlays and snapshots, and returns a whitespace
958 # separated list of how to use them. Only overlays with a partition
959 # label or file name in ${overlays} are returned, and ditto for
960 # snapshots with labels in ${snapshots}.
962 # When scanning a LUKS device, the user will be asked to enter the
963 # passphrase; on failure to enter it, or if no persistence partitions
964 # or files were found, the LUKS device is closed.
966 # For a snapshot file the return value is ${label}=${snapdata}", where
967 # ${snapdata} is the parameter used for try_snap().
969 # For all other cases (overlay/snapshot partition and overlay file) the
970 # return value is "${label}=${device}", where ${device} a device that
971 # can mount the content. In the case of an overlay file, the device
972 # containing the file will remain mounted as a side-effect.
974 # No devices in ${black_listed_devices} will be scanned, and if
975 # ${white_list_devices} is non-empty, only devices in it will be
978 local overlays="${1}"
979 local snapshots="${2}"
980 local white_listed_devices="${3}"
983 for dev in $(storage_devices "" "${white_listed_devices}")
988 # Check if it's a luks device; we'll have to open the device
989 # in order to probe any filesystem it contains, like we do
990 # below. activate_custom_mounts() also depends on that any luks
991 # device already has been opened.
992 if echo ${PERSISTENCE_ENCRYPTION} | grep -qe "\<luks\>" && \
993 is_luks_partition ${dev}
995 if luks_device=$(open_luks_device "${dev}")
999 # skip $dev since we failed/chose not to open it
1002 elif echo ${PERSISTENCE_ENCRYPTION} | grep -qve "\<none\>"
1004 # skip $dev since we don't allow unencrypted storage
1008 # Probe for matching GPT partition names or filesystem labels
1009 if echo ${PERSISTENCE_STORAGE} | grep -qe "\<filesystem\>"
1011 result=$(probe_for_gpt_name "${overlays}" "${snapshots}" ${dev})
1012 if [ -n "${result}" ]
1014 ret="${ret} ${result}"
1018 result=$(probe_for_fs_label "${overlays}" "${snapshots}" ${dev})
1019 if [ -n "${result}" ]
1021 ret="${ret} ${result}"
1026 # Probe for files with matching name on mounted partition
1027 if echo ${PERSISTENCE_STORAGE} | grep -qe "\<file\>"
1029 result=$(probe_for_file_name "${overlays}" "${snapshots}" ${dev})
1030 if [ -n "${result}" ]
1032 ret="${ret} ${result}"
1037 # Close luks device if it isn't used
1038 if [ -z "${result}" ] && [ -n "${luks_device}" ] && \
1039 is_active_luks_mapping "${luks_device}"
1041 /sbin/cryptsetup luksClose "${luks_device}"
1055 for adaptor in /sys/class/net/*
1057 status="$(cat ${adaptor}/iflink)"
1059 if [ "${status}" -eq 2 ]
1061 mac="$(cat ${adaptor}/address)"
1062 mac="$(echo ${mac} | sed 's/:/-/g' | tr '[a-z]' '[A-Z]')"
1069 is_luks_partition ()
1072 /sbin/cryptsetup isLuks "${device}" 1>/dev/null 2>&1
1075 is_active_luks_mapping ()
1078 /sbin/cryptsetup status "${device}" 1>/dev/null 2>&1
1081 get_luks_backing_device () {
1083 cryptsetup status ${device} 2> /dev/null | \
1084 awk '{if ($1 == "device:") print $2}'
1089 output_format="${1}"
1093 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1096 if [ "$(cat ${sysblock}/removable)" = "1" ]
1098 if [ -z "${want_usb}" ]
1102 if readlink ${sysblock} | grep -q usb
1109 if [ "${dev_ok}" = "yes" ]
1111 case "${output_format}" in
1113 ret="${ret} ${sysblock}"
1116 devname=$(sys2dev "${sysblock}")
1117 ret="${ret} ${devname}"
1126 removable_usb_dev ()
1128 output_format="${1}"
1130 removable_dev "${output_format}" "want_usb"
1133 non_removable_dev ()
1135 output_format="${1}"
1138 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1140 if [ "$(cat ${sysblock}/removable)" = "0" ]
1142 case "${output_format}" in
1144 ret="${ret} ${sysblock}"
1147 devname=$(sys2dev "${sysblock}")
1148 ret="${ret} ${devname}"
1159 # create source's directory structure in dest, and recursively
1160 # create symlinks in dest to to all files in source. if mask
1161 # is non-empty, remove mask from all source paths when
1162 # creating links (will be necessary if we change root, which
1163 # live-boot normally does (into $rootmnt)).
1165 # remove multiple /:s and ensure ending on /
1166 local src_dir="$(trim_path ${1})/"
1167 local dest_dir="$(trim_path ${2})/"
1168 local src_mask="${3}"
1170 # This check can only trigger on the inital, non-recursive call since
1171 # we create the destination before recursive calls
1172 if [ ! -d "${dest_dir}" ]
1174 log_warning_msg "Must link_files into a directory"
1178 find "${src_dir}" -mindepth 1 -maxdepth 1 | while read src; do
1179 local dest="${dest_dir}$(basename "${src}")"
1182 if [ -z "$(ls -A "${src}")" ]
1186 if [ ! -d "${dest}" ]
1189 chown_ref "${src}" "${dest}"
1190 chmod_ref "${src}" "${dest}"
1192 link_files "${src}" "${dest}" "${src_mask}"
1194 local final_src=${src}
1195 if [ -n "${src_mask}" ]
1197 final_src="$(echo ${final_src} | sed "s|^${src_mask}||")"
1199 rm -rf "${dest}" 2> /dev/null
1200 ln -s "${final_src}" "${dest}"
1201 chown_ref "${src}" "${dest}"
1208 local unionmountpoint="${1}" # directory where the union is mounted
1209 local unionrw="${2}" # branch where the union changes are stored
1210 local unionro1="${3}" # first underlying read-only branch (optional)
1211 local unionro2="${4}" # second underlying read-only branch (optional)
1213 if [ "${UNIONTYPE}" = "aufs" ]
1218 elif [ "${UNIONTYPE}" = "unionfs-fuse" ]
1227 case "${UNIONTYPE}" in
1229 unionmountopts="-o cow -o noinitgroups -o default_permissions -o allow_other -o use_ino -o suid"
1230 unionmountopts="${unionmountopts} ${unionrw}=${rw_opt}"
1231 if [ -n "${unionro1}" ]
1233 unionmountopts="${unionmountopts}:${unionro1}=${ro_opt}"
1235 if [ -n "${unionro2}" ]
1237 unionmountopts="${unionmountopts}:${unionro2}=${ro_opt}"
1239 ( sysctl -w fs.file-max=391524 ; ulimit -HSn 16384
1240 unionfs-fuse ${unionmountopts} "${unionmountpoint}" ) && \
1241 ( mkdir -p /run/sendsigs.omit.d
1242 pidof unionfs-fuse >> /run/sendsigs.omit.d/unionfs-fuse || true )
1246 # XXX: can unionro2 be used? (overlayfs only handles two dirs, but perhaps they can be chained?)
1247 # XXX: and can unionro1 be optional? i.e. can overlayfs skip lowerdir?
1248 unionmountopts="-o noatime,lowerdir=${unionro1},upperdir=${unionrw}"
1249 mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
1253 unionmountopts="-o noatime,${noxino_opt},dirs=${unionrw}=${rw_opt}"
1254 if [ -n "${unionro1}" ]
1256 unionmountopts="${unionmountopts}:${unionro1}=${ro_opt}"
1258 if [ -n "${unionro2}" ]
1260 unionmountopts="${unionmountopts}:${unionro2}=${ro_opt}"
1262 mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
1267 get_custom_mounts ()
1269 # Side-effect: leaves $devices with live-persistence.conf mounted in ${rootmnt}/live/persistence
1270 # Side-effect: prints info to file $custom_mounts
1272 local custom_mounts=${1}
1276 local bindings="/tmp/bindings.list"
1277 local links="/tmp/links.list"
1278 rm -rf ${bindings} ${links} 2> /dev/null
1280 for device in ${devices}
1282 if [ ! -b "${device}" ]
1287 local device_name="$(basename ${device})"
1288 local backing=$(mount_persistence_media ${device})
1289 if [ -z "${backing}" ]
1294 local include_list="${backing}/${persistence_list}"
1295 if [ ! -r "${include_list}" ]
1300 if [ -n "${DEBUG}" ] && [ -e "${include_list}" ]
1302 cp ${include_list} ${rootmnt}/live/persistence/${persistence_list}.${device_name}
1305 while read dir options # < ${include_list}
1307 if echo ${dir} | grep -qe "^[[:space:]]*\(#.*\)\?$"
1309 # skipping empty or commented lines
1313 if trim_path ${dir} | grep -q -e "^[^/]" -e "^/$" -e "^/live\(/.*\)\?$" -e "^/\(.*/\)\?\.\.\?\(/.*\)\?$"
1315 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) or \"/\" (for the latter, use ${root_overlay_label}-type persistence)"
1320 local opt_linkfiles=""
1321 for opt in $(echo ${options} | tr ',' ' ');
1325 opt_source=${opt#source=}
1333 log_warning_msg "Skipping custom mount with unkown option: ${opt}"
1339 local source="${dir}"
1340 if [ -n "${opt_source}" ]
1342 if echo ${opt_source} | grep -q -e "^/" -e "^\(.*/\)\?\.\.\?\(/.*\)\?$" && [ "${source}" != "." ]
1344 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"
1347 source="${opt_source}"
1351 local full_source="$(trim_path ${backing}/${source})"
1352 local full_dest="$(trim_path ${rootmnt}/${dir})"
1353 if [ -n "${opt_linkfiles}" ]
1355 echo "${device} ${full_source} ${full_dest} ${options}" >> ${links}
1357 echo "${device} ${full_source} ${full_dest} ${options}" >> ${bindings}
1359 done < ${include_list}
1362 # We sort the list according to destination so we're sure that
1363 # we won't hide a previous mount. We also ignore duplicate
1364 # destinations in a more or less arbitrary way.
1365 [ -e "${bindings}" ] && sort -k3 -sbu ${bindings} >> ${custom_mounts} && rm ${bindings}
1367 # After all mounts are considered we add symlinks so they
1368 # won't be hidden by some mount.
1369 [ -e "${links}" ] && cat ${links} >> ${custom_mounts} && rm ${links}
1371 # We need to make sure that no two custom mounts have the same sources
1372 # or are nested; if that is the case, too much weird stuff can happen.
1373 local prev_source="impossible source" # first iteration must not match
1375 # This sort will ensure that a source /a comes right before a source
1376 # /a/b so we only need to look at the previous source
1377 sort -k2 -b ${custom_mounts} |
1378 while read device source dest options
1380 if echo ${source} | grep -qe "^${prev_source}\(/.*\)\?$"
1382 panic "Two persistence mounts have the same or nested sources: ${source} on ${dest}, and ${prev_source} on ${prev_dest}"
1384 prev_source=${source}
1389 activate_custom_mounts ()
1391 local custom_mounts="${1}" # the ouput from get_custom_mounts()
1392 local used_devices=""
1394 while read device source dest options # < ${custom_mounts}
1396 local opt_bind="yes"
1397 local opt_linkfiles=""
1399 for opt in $(echo ${options} | tr ',' ' ');
1404 unset opt_linkfiles opt_union
1408 unset opt_bind opt_union
1412 unset opt_bind opt_linkfiles
1417 if [ -n "$(what_is_mounted_on "${dest}")" ]
1419 log_warning_msg "Skipping custom mount ${dest}: $(what_is_mounted_on "${dest}") is already mounted there"
1423 if [ ! -d "${dest}" ]
1425 # create the destination and delete existing files in
1426 # its path that are in the way
1428 for dir in $(echo ${dest} | sed -e 's|/\+| |g')
1430 path=$(trim_path ${path}/${dir})
1438 if echo ${path} | grep -qe "^${rootmnt}/*home/[^/]\+"
1440 # if ${dest} is in /home try fixing proper ownership by assuming that the intended user is the first, which is usually the case
1441 # FIXME: this should really be handled by live-config since we don't know for sure which uid a certain user has until then
1442 chown 1000:1000 ${path}
1448 # if ${source} doesn't exist on our persistence media
1449 # we bootstrap it with $dest from the live filesystem.
1450 # this both makes sense and is critical if we're
1451 # dealing with /etc or other system dir.
1452 if [ ! -d "${source}" ]
1454 if [ -n "${PERSISTENCE_READONLY}" ]
1457 elif [ -n "${opt_union}" ] || [ -n "${opt_linkfiles}" ]
1459 # unions and don't need to be bootstrapped
1460 # linkfiles dirs can't be bootstrapped in a sensible way
1461 mkdir -p "${source}"
1462 chown_ref "${dest}" "${source}"
1463 chmod_ref "${dest}" "${source}"
1464 elif [ -n "${opt_bind}" ]
1466 # ensure that $dest is not copied *into* $source
1467 mkdir -p "$(dirname ${source})"
1468 cp -a "${dest}" "${source}"
1472 # XXX: If CONFIG_AUFS_ROBR is added to the Debian kernel we can
1473 # ignore the loop below and set rofs_dest_backing=$dest
1474 local rofs_dest_backing=""
1475 if [ -n "${opt_linkfiles}"]
1477 for d in ${rootmnt}/live/rofs/*
1479 if [ -n "${rootmnt}" ]
1481 rofs_dest_backing="${d}/$(echo ${dest} | sed -e "s|${rootmnt}||")"
1483 rofs_dest_backing="${d}/${dest}"
1485 if [ -d "${rofs_dest_backing}" ]
1489 rofs_dest_backing=""
1494 if [ -n "${opt_linkfiles}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1496 link_files ${source} ${dest} ${rootmnt}
1497 elif [ -n "${opt_linkfiles}" ] && [ -n "${PERSISTENCE_READONLY}" ]
1499 mkdir -p ${rootmnt}/live/persistence
1500 local links_source=$(mktemp -d ${rootmnt}/live/persistence/links-source-XXXXXX)
1501 chown_ref ${source} ${links_source}
1502 chmod_ref ${source} ${links_source}
1503 # We put the cow dir in the below strange place to
1504 # make it absolutely certain that the link source
1505 # has its own directory and isn't nested with some
1506 # other custom mount (if so that mount's files would
1507 # be linked, causing breakage.
1508 if [ -n "${rootmnt}" ]
1510 local cow_dir="/overlay/live/persistence/$(basename ${links_source})"
1512 # This is happens if persistence is activated
1514 local cow_dir="/live/overlay/live/persistence/$(basename ${links_source})"
1517 chown_ref "${source}" "${cow_dir}"
1518 chmod_ref "${source}" "${cow_dir}"
1519 do_union ${links_source} ${cow_dir} ${source} ${rofs_dest_backing}
1520 link_files ${links_source} ${dest} ${rootmnt}
1521 elif [ -n "${opt_union}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1523 do_union ${dest} ${source} ${rofs_dest_backing}
1524 elif [ -n "${opt_bind}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1526 mount --bind "${source}" "${dest}"
1527 elif [ -n "${opt_bind}" -o -n "${opt_union}" ] && [ -n "${PERSISTENCE_READONLY}" ]
1529 # bind-mount and union mount are handled the same
1530 # in read-only mode, but note that rofs_dest_backing
1531 # is non-empty (and necessary) only for unions
1532 if [ -n "${rootmnt}" ]
1534 local cow_dir="$(echo ${dest} | sed -e "s|^${rootmnt}|/overlay/|")"
1536 # This is happens if persistence is activated
1538 local cow_dir="/live/overlay/${dest}"
1540 if [ -e "${cow_dir}" ] && [ -z "${opt_linkfiles}" ]
1542 # If an earlier custom mount has files here
1543 # it will "block" the current mount's files
1544 # which is undesirable
1548 chown_ref "${source}" "${cow_dir}"
1549 chmod_ref "${source}" "${cow_dir}"
1550 do_union ${dest} ${cow_dir} ${source} ${rofs_dest_backing}
1553 PERSISTENCE_IS_ON="1"
1554 export PERSISTENCE_IS_ON
1556 if echo ${used_devices} | grep -qve "^\(.* \)\?${device}\( .*\)\?$"
1558 used_devices="${used_devices} ${device}"
1560 done < ${custom_mounts}
1562 echo ${used_devices}
1565 fix_home_rw_compatibility ()
1569 if [ -n "${PERSISTENCE_READONLY}" ]
1574 local backing="$(mount_persistence_media ${device})"
1575 if [ -z "${backing}" ]
1580 local include_list="${backing}/${persistence_list}"
1581 if [ ! -r "${include_list}" ]
1583 echo "# home-rw backwards compatibility:
1584 /home source=." > "${include_list}"
1592 [ $(stat -fc%d:%D "${directory}") != $(stat -fc%d:%D "${directory}/..") ]