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.persist"
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 FORCEPERSISTENTFSCK="Yes"
94 export FORCEPERSISTENTFSCK
98 FTPFS="${ARGUMENT#ftpfs=}"
103 HTTPFS="${ARGUMENT#httpfs=}"
108 ISCSI="${ARGUMENT#iscsi=}"
109 #ip:port - separated by ;
110 ISCSI_PORTAL="${ISCSI%;*}"
111 if echo "${ISCSI_PORTAL}" | grep -q , ; then
112 ISCSI_SERVER="${ISCSI_PORTAL%,*}"
113 ISCSI_PORT="${ISCSI_PORTAL#*,}"
116 ISCSI_TARGET="${ISCSI#*;}"
117 export ISCSI ISCSI_PORTAL ISCSI_TARGET ISCSI_SERVER ISCSI_PORT
121 FROMISO="${ARGUMENT#*=}"
131 INTEGRITY_CHECK="Yes"
132 export INTEGRITY_CHECK
136 STATICIP="${ARGUMENT#ip=}"
138 if [ -z "${STATICIP}" ]
151 live-media=*|bootfrom=*)
152 LIVE_MEDIA="${ARGUMENT#*=}"
156 live-media-encryption=*|encryption=*)
157 LIVE_MEDIA_ENCRYPTION="${ARGUMENT#*=}"
158 export LIVE_MEDIA_ENCRYPTION
162 LIVE_MEDIA_OFFSET="${ARGUMENT#live-media-offset=}"
163 export LIVE_MEDIA_OFFSET
167 LIVE_MEDIA_PATH="${ARGUMENT#live-media-path=}"
168 export LIVE_MEDIA_PATH
171 live-media-timeout=*)
172 LIVE_MEDIA_TIMEOUT="${ARGUMENT#live-media-timeout=}"
173 export LIVE_MEDIA_TIMEOUT
177 MODULE="${ARGUMENT#module=}"
182 NETBOOT="${ARGUMENT#netboot=}"
187 NFSOPTS="${ARGUMENT#nfsopts=}"
192 NFS_COW="${ARGUMENT#nfscow=}"
197 NOACCESSIBILITY="Yes"
198 export NOACCESSIBILITY
217 ramdisk_size="${ARGUMENT#ramdisk-size=}"
230 persistent-encryption=*)
231 PERSISTENT_ENCRYPTION="${ARGUMENT#*=}"
232 export PERSISTENT_ENCRYPTION
236 PERSISTENT_MEDIA="${ARGUMENT#*=}"
237 export PERSISTENT_MEDIA
240 PERSISTENT_METHOD="${ARGUMENT#*=}"
241 export PERSISTENT_METHOD
245 PERSISTENT_PATH="${ARGUMENT#persistent-path=}"
246 export PERSISTENT_PATH
248 persistent-read-only)
249 PERSISTENT_READONLY="Yes"
250 export PERSISTENT_READONLY
253 persistent-storage=*)
254 PERSISTENT_STORAGE="${ARGUMENT#persistent-storage=}"
255 export PERSISTENT_STORAGE
258 persistent-subtext=*)
259 root_overlay_label="${root_overlay_label}-${ARGUMENT#persistent-subtext=}"
260 old_root_overlay_label="${old_root_overlay_label}-${ARGUMENT#persistent-subtext=}"
261 old_home_overlay_label="${old_home_overlay_label}-${ARGUMENT#persistent-subtext=}"
262 custom_overlay_label="${custom_overlay_label}-${ARGUMENT#persistent-subtext=}"
263 root_snapshot_label="${root_snapshot_label}-${ARGUMENT#persistent-subtext=}"
264 old_root_snapshot_label="${root_snapshot_label}-${ARGUMENT#persistent-subtext=}"
265 home_snapshot_label="${home_snapshot_label}-${ARGUMENT#persistent-subtext=}"
279 NOPROMPT="${ARGUMENT#noprompt=}"
284 QUICKUSBMODULES="Yes"
285 export QUICKUSBMODULES
288 preseed/file=*|file=*)
289 LOCATIONS="${ARGUMENT#*=} ${LOCATIONS}"
299 question="${ARGUMENT%%=*}"
300 value="${ARGUMENT#*=}"
301 PRESEEDS="${PRESEEDS}\"${question}=${value}\" "
316 TODISK="${ARGUMENT#todisk=}"
327 MODULETORAM="${ARGUMENT#toram=}"
328 export TORAM MODULETORAM
342 SKIP_UNION_MOUNTS="Yes"
343 export SKIP_UNION_MOUNTS
347 ROOT="${ARGUMENT#root=}"
352 UNIONTYPE="${ARGUMENT#union=}"
358 # sort of compatibility with netboot.h from linux docs
359 if [ -z "${NETBOOT}" ]
361 if [ "${ROOT}" = "/dev/nfs" ]
365 elif [ "${ROOT}" = "/dev/cifs" ]
372 if [ -z "${MODULE}" ]
378 if [ -z "${UNIONTYPE}" ]
384 if [ -z "${PERSISTENT_ENCRYPTION}" ]
386 PERSISTENT_ENCRYPTION="none"
387 export PERSISTENT_ENCRYPTION
388 elif echo ${PERSISTENT_ENCRYPTION} | grep -qe "\<luks\>"
390 if ! modprobe dm-crypt
392 log_warning_msg "Unable to load module dm-crypt"
393 PERSISTENT_ENCRYPTION=$(echo ${PERSISTENT_ENCRYPTION} | sed -e 's/\<luks,\|,\?luks$//g')
394 export PERSISTENT_ENCRYPTION
397 if [ ! -x /lib/cryptsetup/askpass ] || [ ! -x /sbin/cryptsetup ]
399 log_warning_msg "cryptsetup in unavailable"
400 PERSISTENT_ENCRYPTION=$(echo ${PERSISTENT_ENCRYPTION} | sed -e 's/\<luks,\|,\?luks$//g')
401 export PERSISTENT_ENCRYPTION
405 if [ -z "${PERSISTENT_METHOD}" ]
407 PERSISTENT_METHOD="snapshot,overlay"
408 export PERSISTENT_METHOD
411 if [ -z "${PERSISTENT_STORAGE}" ]
413 PERSISTENT_STORAGE="filesystem,file"
414 export PERSISTENT_STORAGE
421 echo "/dev/$($udevinfo -q name -p ${sysdev} 2>/dev/null|| echo ${sysdev##*/})"
429 for dev in "${sysblock}"/* "${sysblock}"
431 if [ -e "${dev}/dev" ]
442 black_listed_devices="${1}"
443 white_listed_devices="${2}"
445 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "loop|ram|fd")
447 fulldevname=$(sys2dev "${sysblock}")
449 if echo "${black_listed_devices}" | grep -qe "\<${fulldevname}\>" || \
450 [ -n "${white_listed_devices}" ] && \
451 echo "${white_listed_devices}" | grep -qve "\<${fulldevname}\>"
453 # skip this device entirely
457 for dev in $(subdevices "${sysblock}")
459 devname=$(sys2dev "${dev}")
461 if echo "${black_listed_devices}" | grep -qe "\<${devname}\>"
463 # skip this subdevice
476 # Validate input first
477 if [ -z "${fstype}" ]
482 # Try to look if it is already supported by the kernel
483 if grep -q ${fstype} /proc/filesystems
487 # Then try to add support for it the gentle way using the initramfs capabilities
489 if grep -q ${fstype} /proc/filesystems
492 # Then try the hard way if /root is already reachable
494 kmodule="/root/lib/modules/`uname -r`/${fstype}/${fstype}.ko"
495 if [ -e "${kmodule}" ]
498 if grep -q ${fstype} /proc/filesystems
511 /sbin/blkid -s TYPE -o value $1 2>/dev/null
518 grep -m1 "^${device} " /proc/mounts | cut -f2 -d ' '
522 # remove all unnecessary /:s in the path, including last one (except
523 # if path is just "/")
524 echo ${1} | sed 's|//\+|/|g' | sed 's|^\(.*[^/]\)/$|\1|'
527 what_is_mounted_on ()
529 local dir="$(trim_path ${1})"
530 grep -m1 "^[^ ]\+ ${dir} " /proc/mounts | cut -d' ' -f1
535 local reference="${1}"
538 local owner=$(stat -c %u:%g "${reference}")
539 chown -h ${owner} ${targets}
544 local reference="${1}"
547 local rights=$(stat -c %a "${reference}")
548 chmod ${rights} ${targets}
564 mounts="$(awk '{print $2}' /proc/mounts)"
565 testpath="$(busybox realpath ${testpath})"
569 if echo "${mounts}" | grep -qs "^${testpath}"
571 set -- $(echo "${mounts}" | grep "^${testpath}" | lastline)
575 testpath=$(dirname $testpath)
582 # Returns used/free fs kbytes + 5% more
583 # You could pass a block device as ${1} or the mount point as ${2}
589 if [ -z "${mountp}" ]
591 mountp="$(where_is_mounted ${dev})"
593 if [ -z "${mountp}" ]
595 mountp="/mnt/tmp_fs_size"
598 mount -t $(get_fstype "${dev}") -o ro "${dev}" "${mountp}" || log_warning_msg "cannot mount -t $(get_fstype ${dev}) -o ro ${dev} ${mountp}"
604 if [ "${used}" = "used" ]
606 size=$(du -ks ${mountp} | cut -f1)
607 size=$(expr ${size} + ${size} / 20 ) # FIXME: 5% more to be sure
610 size="$(df -k | grep -s ${mountp} | awk '{print $4}')"
613 if [ -n "${doumount}" ]
615 umount "${mountp}" || log_warning_msg "cannot umount ${mountp}"
625 if [ -x /bin/loadkeys -a -r /etc/boottime.kmap.gz ]
627 loadkeys /etc/boottime.kmap.gz
637 local encryption=${5}
640 # the output of setup_loop is evaluated in other functions,
641 # modprobe leaks kernel options like "libata.dma=0"
642 # as "options libata dma=0" on stdout, causing serious
643 # problems therefor, so instead always avoid output to stdout
644 modprobe -q -b "${module}" 1>/dev/null
648 for loopdev in ${pattern}
650 if [ "$(cat ${loopdev}/size)" -eq 0 ]
652 dev=$(sys2dev "${loopdev}")
655 if [ -n "${readonly}" ]
657 if losetup --help 2>&1 | grep -q -- "-r\b"
659 options="${options} -r"
663 if [ -n "${offset}" ] && [ 0 -lt "${offset}" ]
665 options="${options} -o ${offset}"
668 if [ -z "${encryption}" ]
670 losetup ${options} "${dev}" "${fspath}"
672 # Loop AES encryption
677 echo -n "Enter passphrase for root filesystem: " >&6
679 echo "${passphrase}" > /tmp/passphrase
681 exec 9</tmp/passphrase
682 /sbin/losetup ${options} -e "${encryption}" -p 9 "${dev}" "${fspath}"
685 rm -f /tmp/passphrase
687 if [ 0 -eq ${error} ]
694 echo -n "There was an error decrypting the root filesystem ... Retry? [Y/n] " >&6
697 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
710 panic "No loop devices available"
720 old_mountp="$(where_is_mounted ${dev})"
722 if [ -n "${old_mountp}" ]
724 if [ "${opts}" != "ro" ]
726 mount -o remount,"${opts}" "${dev}" "${old_mountp}" || panic "Remounting ${dev} ${opts} on ${old_mountp} failed"
729 mount -o bind "${old_mountp}" "${mountp}" || panic "Cannot bind-mount ${old_mountp} on ${mountp}"
731 if [ -z "${fstype}" ]
733 fstype=$(get_fstype "${dev}")
735 mount -t "${fstype}" -o "${opts}" "${dev}" "${mountp}" || \
736 ( echo "SKIPPING: Cannot mount ${dev} on ${mountp}, fstype=${fstype}, options=${opts}" > live-boot.log && return 0 )
740 mount_persistent_media ()
745 # We can't mount into ${rootmnt}/live before ${rootmnt} has been
746 # mounted since that would cover our mountpoint.
747 if [ -n "${rootmnt}" ] && [ -z "$(what_is_mounted_on ${rootmnt})" ]
749 backing="/$(basename ${device})-backing"
751 backing="${rootmnt}/live/persistent/$(basename ${device})"
754 mkdir -p "${backing}"
755 local old_backing="$(where_is_mounted ${device})"
756 if [ -z "${old_backing}" ]
758 local fstype="$(get_fstype ${device})"
759 local mount_opts="rw,noatime"
760 if [ -n "${PERSISTENT_READONLY}" ]
762 mount_opts="ro,noatime"
764 if mount -t "${fstype}" -o "${mount_opts}" "${device}" "${backing}" >/dev/null
769 log_warning_msg "Failed to mount persistent media ${device}"
772 elif [ "${backing}" != "${old_backing}" ]
774 if mount --move ${old_backing} ${backing} >/dev/null
779 log_warning_msg "Failed to move persistent media ${device}"
786 close_persistent_media () {
788 local backing="$(where_is_mounted ${device})"
790 if [ -d "${backing}" ]
792 umount "${backing}" >/dev/null 2>&1
793 rmdir "${backing}" >/dev/null 2>&1
796 if is_active_luks_mapping ${device}
798 /sbin/cryptsetup luksClose ${device}
805 name="$(basename ${dev})"
807 if [ -n "${PERSISTENT_READONLY}" ]
809 opts="${opts} --readonly"
812 if /sbin/cryptsetup status "${name}" >/dev/null 2>&1
814 re="^[[:space:]]*device:[[:space:]]*\([^[:space:]]*\)$"
815 opened_dev=$(cryptsetup status ${name} 2>/dev/null | grep "${re}" | sed "s|${re}|\1|")
816 if [ "${opened_dev}" = "${dev}" ]
818 luks_device="/dev/mapper/${name}"
822 log_warning_msg "Cannot open luks device ${dev} since ${opened_dev} already is opened with its name"
831 /lib/cryptsetup/askpass "Enter passphrase for ${dev}: " | \
832 /sbin/cryptsetup -T 1 luksOpen ${dev} ${name} ${opts}
836 luks_device="/dev/mapper/${name}"
842 echo -n "There was an error decrypting ${dev} ... Retry? [Y/n] " >&6
845 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
855 /sbin/blkid -s PART_ENTRY_NAME -p -o value ${dev} 2>/dev/null
861 [ "$(/sbin/blkid -s PART_ENTRY_SCHEME -p -o value ${dev} 2>/dev/null)" = "gpt" ]
864 probe_for_gpt_name ()
866 local overlays="${1}"
867 local snapshots="${2}"
870 local gpt_dev="${dev}"
871 if is_active_luks_mapping ${dev}
873 # if $dev is an opened luks device, we need to check
874 # GPT stuff on the backing device
875 gpt_dev=$(get_luks_backing_device "${dev}")
878 if ! is_gpt_device ${gpt_dev}
883 local gpt_name=$(get_gpt_name ${gpt_dev})
884 for label in ${overlays} ${snapshots}
886 if [ "${gpt_name}" = "${label}" ]
888 echo "${label}=${dev}"
893 probe_for_fs_label ()
895 local overlays="${1}"
896 local snapshots="${2}"
899 for label in ${overlays} ${snapshots}
901 if [ "$(/sbin/blkid -s LABEL -o value $dev 2>/dev/null)" = "${label}" ]
903 echo "${label}=${dev}"
908 probe_for_file_name ()
910 local overlays="${1}"
911 local snapshots="${2}"
915 local backing="$(mount_persistent_media ${dev})"
916 if [ -z "${backing}" ]
921 for label in ${overlays}
923 path=${backing}/${PERSISTENT_PATH}${label}
926 local loopdev=$(setup_loop "${path}" "loop" "/sys/block/loop*")
927 ret="${ret} ${label}=${loopdev}"
930 for label in ${snapshots}
932 for ext in squashfs cpio.gz ext2 ext3 ext4 jffs2
934 path="${PERSISTENT_PATH}${label}.${ext}"
935 if [ -f "${backing}/${path}" ]
937 ret="${ret} ${label}=${dev}:${backing}:${path}"
946 umount ${backing} > /dev/null 2>&1 || true
950 find_persistent_media ()
952 # Scans devices for overlays and snapshots, and returns a whitespace
953 # separated list of how to use them. Only overlays with a partition
954 # label or file name in ${overlays} are returned, and ditto for
955 # snapshots with labels in ${snapshots}.
957 # When scanning a LUKS device, the user will be asked to enter the
958 # passphrase; on failure to enter it, or if no persistent partitions
959 # or files were found, the LUKS device is closed.
961 # For a snapshot file the return value is ${label}=${snapdata}", where
962 # ${snapdata} is the parameter used for try_snap().
964 # For all other cases (overlay/snapshot 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
973 local overlays="${1}"
974 local snapshots="${2}"
975 local white_listed_devices="${3}"
978 for dev in $(storage_devices "" "${white_listed_devices}")
983 # Check if it's a luks device; we'll have to open the device
984 # in order to probe any filesystem it contains, like we do
985 # below. activate_custom_mounts() also depends on that any luks
986 # device already has been opened.
987 if echo ${PERSISTENT_ENCRYPTION} | grep -qe "\<luks\>" && \
988 is_luks_partition ${dev}
990 if luks_device=$(open_luks_device "${dev}")
994 # skip $dev since we failed/chose not to open it
997 elif echo ${PERSISTENT_ENCRYPTION} | grep -qve "\<none\>"
999 # skip $dev since we don't allow unencrypted storage
1003 # Probe for matching GPT partition names or filesystem labels
1004 if echo ${PERSISTENT_STORAGE} | grep -qe "\<filesystem\>"
1006 result=$(probe_for_gpt_name "${overlays}" "${snapshots}" ${dev})
1007 if [ -n "${result}" ]
1009 ret="${ret} ${result}"
1013 result=$(probe_for_fs_label "${overlays}" "${snapshots}" ${dev})
1014 if [ -n "${result}" ]
1016 ret="${ret} ${result}"
1021 # Probe for files with matching name on mounted partition
1022 if echo ${PERSISTENT_STORAGE} | grep -qe "\<file\>"
1024 result=$(probe_for_file_name "${overlays}" "${snapshots}" ${dev})
1025 if [ -n "${result}" ]
1027 ret="${ret} ${result}"
1032 # Close luks device if it isn't used
1033 if [ -z "${result}" ] && [ -n "${luks_device}" ] && \
1034 is_active_luks_mapping "${luks_device}"
1036 /sbin/cryptsetup luksClose "${luks_device}"
1050 for adaptor in /sys/class/net/*
1052 status="$(cat ${adaptor}/iflink)"
1054 if [ "${status}" -eq 2 ]
1056 mac="$(cat ${adaptor}/address)"
1057 mac="$(echo ${mac} | sed 's/:/-/g' | tr '[a-z]' '[A-Z]')"
1064 is_luks_partition ()
1067 /sbin/cryptsetup isLuks "${device}" 1>/dev/null 2>&1
1070 is_active_luks_mapping ()
1073 /sbin/cryptsetup status "${device}" 1>/dev/null 2>&1
1076 get_luks_backing_device () {
1078 cryptsetup status ${device} 2> /dev/null | \
1079 awk '{if ($1 == "device:") print $2}'
1084 output_format="${1}"
1088 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1091 if [ "$(cat ${sysblock}/removable)" = "1" ]
1093 if [ -z "${want_usb}" ]
1097 if readlink ${sysblock} | grep -q usb
1104 if [ "${dev_ok}" = "yes" ]
1106 case "${output_format}" in
1108 ret="${ret} ${sysblock}"
1111 devname=$(sys2dev "${sysblock}")
1112 ret="${ret} ${devname}"
1121 removable_usb_dev ()
1123 output_format="${1}"
1125 removable_dev "${output_format}" "want_usb"
1128 non_removable_dev ()
1130 output_format="${1}"
1133 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1135 if [ "$(cat ${sysblock}/removable)" = "0" ]
1137 case "${output_format}" in
1139 ret="${ret} ${sysblock}"
1142 devname=$(sys2dev "${sysblock}")
1143 ret="${ret} ${devname}"
1154 # create source's directory structure in dest, and recursively
1155 # create symlinks in dest to to all files in source. if mask
1156 # is non-empty, remove mask from all source paths when
1157 # creating links (will be necessary if we change root, which
1158 # live-boot normally does (into $rootmnt)).
1160 # remove multiple /:s and ensure ending on /
1161 local src_dir="$(trim_path ${1})/"
1162 local dest_dir="$(trim_path ${2})/"
1163 local src_mask="${3}"
1165 # This check can only trigger on the inital, non-recursive call since
1166 # we create the destination before recursive calls
1167 if [ ! -d "${dest_dir}" ]
1169 log_warning_msg "Must link_files into a directory"
1173 find "${src_dir}" -mindepth 1 -maxdepth 1 | while read src; do
1174 local dest="${dest_dir}$(basename "${src}")"
1177 if [ -z "$(ls -A "${src}")" ]
1181 if [ ! -d "${dest}" ]
1184 chown_ref "${src}" "${dest}"
1185 chmod_ref "${src}" "${dest}"
1187 link_files "${src}" "${dest}" "${src_mask}"
1189 local final_src=${src}
1190 if [ -n "${src_mask}" ]
1192 final_src="$(echo ${final_src} | sed "s|^${src_mask}||")"
1194 rm -rf "${dest}" 2> /dev/null
1195 ln -s "${final_src}" "${dest}"
1196 chown_ref "${src}" "${dest}"
1203 local unionmountpoint="${1}" # directory where the union is mounted
1204 local unionrw="${2}" # branch where the union changes are stored
1205 local unionro1="${3}" # first underlying read-only branch (optional)
1206 local unionro2="${4}" # second underlying read-only branch (optional)
1208 if [ "${UNIONTYPE}" = "aufs" ]
1213 elif [ "${UNIONTYPE}" = "unionfs-fuse" ]
1222 case "${UNIONTYPE}" in
1224 unionmountopts="-o cow -o noinitgroups -o default_permissions -o allow_other -o use_ino -o suid"
1225 unionmountopts="${unionmountopts} ${unionrw}=${rw_opt}"
1226 if [ -n "${unionro1}" ]
1228 unionmountopts="${unionmountopts}:${unionro1}=${ro_opt}"
1230 if [ -n "${unionro2}" ]
1232 unionmountopts="${unionmountopts}:${unionro2}=${ro_opt}"
1234 ( sysctl -w fs.file-max=391524 ; ulimit -HSn 16384
1235 unionfs-fuse ${unionmountopts} "${unionmountpoint}" ) && \
1236 ( mkdir -p /run/sendsigs.omit.d
1237 pidof unionfs-fuse >> /run/sendsigs.omit.d/unionfs-fuse || true )
1241 # XXX: can unionro2 be used? (overlayfs only handles two dirs, but perhaps they can be chained?)
1242 # XXX: and can unionro1 be optional? i.e. can overlayfs skip lowerdir?
1243 unionmountopts="-o noatime,lowerdir=${unionro1},upperdir=${unionrw}"
1244 mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
1248 unionmountopts="-o noatime,${noxino_opt},dirs=${unionrw}=${rw_opt}"
1249 if [ -n "${unionro1}" ]
1251 unionmountopts="${unionmountopts}:${unionro1}=${ro_opt}"
1253 if [ -n "${unionro2}" ]
1255 unionmountopts="${unionmountopts}:${unionro2}=${ro_opt}"
1257 mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
1262 get_custom_mounts ()
1264 # Side-effect: leaves $devices with live.persist mounted in ${rootmnt}/live/persistent
1265 # Side-effect: prints info to file $custom_mounts
1267 local custom_mounts=${1}
1271 local bindings="/tmp/bindings.list"
1272 local links="/tmp/links.list"
1273 rm -rf ${bindings} ${links} 2> /dev/null
1275 for device in ${devices}
1277 if [ ! -b "${device}" ]
1282 local device_name="$(basename ${device})"
1283 local backing=$(mount_persistent_media ${device})
1284 if [ -z "${backing}" ]
1289 local include_list="${backing}/${persistence_list}"
1290 if [ ! -r "${include_list}" ]
1295 if [ -n "${DEBUG}" ] && [ -e "${include_list}" ]
1297 cp ${include_list} ${rootmnt}/live/persistent/${persistence_list}.${device_name}
1300 while read dir options # < ${include_list}
1302 if echo ${dir} | grep -qe "^[[:space:]]*\(#.*\)\?$"
1304 # skipping empty or commented lines
1308 if trim_path ${dir} | grep -q -e "^[^/]" -e "^/$" -e "^/live\(/.*\)\?$" -e "^/\(.*/\)\?\.\.\?\(/.*\)\?$"
1310 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)"
1315 local opt_linkfiles=""
1316 for opt in $(echo ${options} | tr ',' ' ');
1320 opt_source=${opt#source=}
1328 log_warning_msg "Skipping custom mount with unkown option: ${opt}"
1334 local source="${dir}"
1335 if [ -n "${opt_source}" ]
1337 if echo ${opt_source} | grep -q -e "^/" -e "^\(.*/\)\?\.\.\?\(/.*\)\?$" && [ "${source}" != "." ]
1339 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"
1342 source="${opt_source}"
1346 local full_source="$(trim_path ${backing}/${source})"
1347 local full_dest="$(trim_path ${rootmnt}/${dir})"
1348 if [ -n "${opt_linkfiles}" ]
1350 echo "${device} ${full_source} ${full_dest} ${options}" >> ${links}
1352 echo "${device} ${full_source} ${full_dest} ${options}" >> ${bindings}
1354 done < ${include_list}
1357 # We sort the list according to destination so we're sure that
1358 # we won't hide a previous mount. We also ignore duplicate
1359 # destinations in a more or less arbitrary way.
1360 [ -e "${bindings}" ] && sort -k3 -sbu ${bindings} >> ${custom_mounts} && rm ${bindings}
1362 # After all mounts are considered we add symlinks so they
1363 # won't be hidden by some mount.
1364 [ -e "${links}" ] && cat ${links} >> ${custom_mounts} && rm ${links}
1366 # We need to make sure that no two custom mounts have the same sources
1367 # or are nested; if that is the case, too much weird stuff can happen.
1368 local prev_source="impossible source" # first iteration must not match
1370 # This sort will ensure that a source /a comes right before a source
1371 # /a/b so we only need to look at the previous source
1372 sort -k2 -b ${custom_mounts} |
1373 while read device source dest options
1375 if echo ${source} | grep -qe "^${prev_source}\(/.*\)\?$"
1377 panic "Two persistent mounts have the same or nested sources: ${source} on ${dest}, and ${prev_source} on ${prev_dest}"
1379 prev_source=${source}
1384 activate_custom_mounts ()
1386 local custom_mounts="${1}" # the ouput from get_custom_mounts()
1387 local used_devices=""
1389 while read device source dest options # < ${custom_mounts}
1391 local opt_bind="yes"
1392 local opt_linkfiles=""
1394 for opt in $(echo ${options} | tr ',' ' ');
1399 unset opt_linkfiles opt_union
1403 unset opt_bind opt_union
1407 unset opt_bind opt_linkfiles
1412 if [ -n "$(what_is_mounted_on "${dest}")" ]
1414 log_warning_msg "Skipping custom mount ${dest}: $(what_is_mounted_on "${dest}") is already mounted there"
1418 if [ ! -d "${dest}" ]
1420 # create the destination and delete existing files in
1421 # its path that are in the way
1423 for dir in $(echo ${dest} | sed -e 's|/\+| |g')
1425 path=$(trim_path ${path}/${dir})
1433 if echo ${path} | grep -qe "^${rootmnt}/*home/[^/]\+"
1435 # if ${dest} is in /home try fixing proper ownership by assuming that the intended user is the first, which is usually the case
1436 # FIXME: this should really be handled by live-config since we don't know for sure which uid a certain user has until then
1437 chown 1000:1000 ${path}
1443 # if ${source} doesn't exist on our persistent media
1444 # we bootstrap it with $dest from the live filesystem.
1445 # this both makes sense and is critical if we're
1446 # dealing with /etc or other system dir.
1447 if [ ! -d "${source}" ]
1449 if [ -n "${PERSISTENT_READONLY}" ]
1452 elif [ -n "${opt_union}" ] || [ -n "${opt_linkfiles}" ]
1454 # unions and don't need to be bootstrapped
1455 # linkfiles dirs can't be bootstrapped in a sensible way
1456 mkdir -p "${source}"
1457 chown_ref "${dest}" "${source}"
1458 chmod_ref "${dest}" "${source}"
1459 elif [ -n "${opt_bind}" ]
1461 # ensure that $dest is not copied *into* $source
1462 mkdir -p "$(dirname ${source})"
1463 cp -a "${dest}" "${source}"
1467 # XXX: If CONFIG_AUFS_ROBR is added to the Debian kernel we can
1468 # ignore the loop below and set rofs_dest_backing=$dest
1469 local rofs_dest_backing=""
1470 if [ -n "${opt_linkfiles}"]
1472 for d in ${rootmnt}/live/rofs/*
1474 if [ -n "${rootmnt}" ]
1476 rofs_dest_backing="${d}/$(echo ${dest} | sed -e "s|${rootmnt}||")"
1478 rofs_dest_backing="${d}/${dest}"
1480 if [ -d "${rofs_dest_backing}" ]
1484 rofs_dest_backing=""
1489 if [ -n "${opt_linkfiles}" ] && [ -z "${PERSISTENT_READONLY}" ]
1491 link_files ${source} ${dest} ${rootmnt}
1492 elif [ -n "${opt_linkfiles}" ] && [ -n "${PERSISTENT_READONLY}" ]
1494 mkdir -p ${rootmnt}/live/persistent
1495 local links_source=$(mktemp -d ${rootmnt}/live/persistent/links-source-XXXXXX)
1496 chown_ref ${source} ${links_source}
1497 chmod_ref ${source} ${links_source}
1498 # We put the cow dir in the below strange place to
1499 # make it absolutely certain that the link source
1500 # has its own directory and isn't nested with some
1501 # other custom mount (if so that mount's files would
1502 # be linked, causing breakage.
1503 if [ -n "${rootmnt}" ]
1505 local cow_dir="/cow/live/persistent/$(basename ${links_source})"
1507 # This is happens if persistence is activated
1509 local cow_dir="/live/cow/live/persistent/$(basename ${links_source})"
1512 chown_ref "${source}" "${cow_dir}"
1513 chmod_ref "${source}" "${cow_dir}"
1514 do_union ${links_source} ${cow_dir} ${source} ${rofs_dest_backing}
1515 link_files ${links_source} ${dest} ${rootmnt}
1516 elif [ -n "${opt_union}" ] && [ -z "${PERSISTENT_READONLY}" ]
1518 do_union ${dest} ${source} ${rofs_dest_backing}
1519 elif [ -n "${opt_bind}" ] && [ -z "${PERSISTENT_READONLY}" ]
1521 mount --bind "${source}" "${dest}"
1522 elif [ -n "${opt_bind}" -o -n "${opt_union}" ] && [ -n "${PERSISTENT_READONLY}" ]
1524 # bind-mount and union mount are handled the same
1525 # in read-only mode, but note that rofs_dest_backing
1526 # is non-empty (and necessary) only for unions
1527 if [ -n "${rootmnt}" ]
1529 local cow_dir="$(echo ${dest} | sed -e "s|^${rootmnt}|/cow/|")"
1531 # This is happens if persistence is activated
1533 local cow_dir="/live/cow/${dest}"
1535 if [ -e "${cow_dir}" ] && [ -z "${opt_linkfiles}" ]
1537 # If an earlier custom mount has files here
1538 # it will "block" the current mount's files
1539 # which is undesirable
1543 chown_ref "${source}" "${cow_dir}"
1544 chmod_ref "${source}" "${cow_dir}"
1545 do_union ${dest} ${cow_dir} ${source} ${rofs_dest_backing}
1548 PERSISTENCE_IS_ON="1"
1549 export PERSISTENCE_IS_ON
1551 if echo ${used_devices} | grep -qve "^\(.* \)\?${device}\( .*\)\?$"
1553 used_devices="${used_devices} ${device}"
1555 done < ${custom_mounts}
1557 echo ${used_devices}
1560 fix_home_rw_compatibility ()
1564 if [ -n "${PERSISTENT_READONLY}" ]
1569 local backing="$(mount_persistent_media ${device})"
1570 if [ -z "${backing}" ]
1575 local include_list="${backing}/${persistence_list}"
1576 if [ ! -r "${include_list}" ]
1578 echo "# home-rw backwards compatibility:
1579 /home source=." > "${include_list}"