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
546 mounts="$(awk '{print $2}' /proc/mounts)"
547 testpath="$(busybox realpath ${testpath})"
551 if echo "${mounts}" | grep -qs "^${testpath}"
553 set -- $(echo "${mounts}" | grep "^${testpath}" | lastline)
557 testpath=$(dirname $testpath)
564 # Returns used/free fs kbytes + 5% more
565 # You could pass a block device as ${1} or the mount point as ${2}
571 if [ -z "${mountp}" ]
573 mountp="$(where_is_mounted ${dev})"
575 if [ -z "${mountp}" ]
577 mountp="/mnt/tmp_fs_size"
580 mount -t $(get_fstype "${dev}") -o ro "${dev}" "${mountp}" || log_warning_msg "cannot mount -t $(get_fstype ${dev}) -o ro ${dev} ${mountp}"
586 if [ "${used}" = "used" ]
588 size=$(du -ks ${mountp} | cut -f1)
589 size=$(expr ${size} + ${size} / 20 ) # FIXME: 5% more to be sure
592 size="$(df -k | grep -s ${mountp} | awk '{print $4}')"
595 if [ -n "${doumount}" ]
597 umount "${mountp}" || log_warning_msg "cannot umount ${mountp}"
607 if [ -x /bin/loadkeys -a -r /etc/boottime.kmap.gz ]
609 loadkeys /etc/boottime.kmap.gz
619 local encryption=${5}
622 # the output of setup_loop is evaluated in other functions,
623 # modprobe leaks kernel options like "libata.dma=0"
624 # as "options libata dma=0" on stdout, causing serious
625 # problems therefor, so instead always avoid output to stdout
626 modprobe -q -b "${module}" 1>/dev/null
630 for loopdev in ${pattern}
632 if [ "$(cat ${loopdev}/size)" -eq 0 ]
634 dev=$(sys2dev "${loopdev}")
637 if [ -n "${readonly}" ]
639 if losetup --help 2>&1 | grep -q -- "-r\b"
641 options="${options} -r"
645 if [ -n "${offset}" ] && [ 0 -lt "${offset}" ]
647 options="${options} -o ${offset}"
650 if [ -z "${encryption}" ]
652 losetup ${options} "${dev}" "${fspath}"
654 # Loop AES encryption
659 echo -n "Enter passphrase for root filesystem: " >&6
661 echo "${passphrase}" > /tmp/passphrase
663 exec 9</tmp/passphrase
664 /sbin/losetup ${options} -e "${encryption}" -p 9 "${dev}" "${fspath}"
667 rm -f /tmp/passphrase
669 if [ 0 -eq ${error} ]
676 echo -n "There was an error decrypting the root filesystem ... Retry? [Y/n] " >&6
679 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
692 panic "No loop devices available"
702 old_mountp="$(where_is_mounted ${dev})"
704 if [ -n "${old_mountp}" ]
706 if [ "${opts}" != "ro" ]
708 mount -o remount,"${opts}" "${dev}" "${old_mountp}" || panic "Remounting ${dev} ${opts} on ${old_mountp} failed"
711 mount -o bind "${old_mountp}" "${mountp}" || panic "Cannot bind-mount ${old_mountp} on ${mountp}"
713 if [ -z "${fstype}" ]
715 fstype=$(get_fstype "${dev}")
717 mount -t "${fstype}" -o "${opts}" "${dev}" "${mountp}" || \
718 ( echo "SKIPPING: Cannot mount ${dev} on ${mountp}, fstype=${fstype}, options=${opts}" > live-boot.log && return 0 )
722 mount_persistent_media ()
727 # We can't mount into ${rootmnt}/live before ${rootmnt} has been
728 # mounted since that would cover our mountpoint.
729 if [ -n "${rootmnt}" ] && [ -z "$(what_is_mounted_on ${rootmnt})" ]
731 backing="/$(basename ${device})-backing"
733 backing="${rootmnt}/live/persistent/$(basename ${device})"
736 mkdir -p "${backing}"
737 local old_backing="$(where_is_mounted ${device})"
738 if [ -z "${old_backing}" ]
740 local fstype="$(get_fstype ${device})"
741 local mount_opts="rw,noatime"
742 if [ -n "${PERSISTENT_READONLY}" ]
744 mount_opts="ro,noatime"
746 if mount -t "${fstype}" -o "${mount_opts}" "${device}" "${backing}" >/dev/null
751 log_warning_msg "Failed to mount persistent media ${device}"
754 elif [ "${backing}" != "${old_backing}" ]
756 if mount --move ${old_backing} ${backing} >/dev/null
761 log_warning_msg "Failed to move persistent media ${device}"
768 close_persistent_media () {
770 local backing="$(where_is_mounted ${device})"
772 if [ -d "${backing}" ]
774 umount "${backing}" >/dev/null 2>&1
775 rmdir "${backing}" >/dev/null 2>&1
778 if is_active_luks_mapping ${device}
780 /sbin/cryptsetup luksClose ${device}
787 name="$(basename ${dev})"
789 if [ -n "${PERSISTENT_READONLY}" ]
791 opts="${opts} --readonly"
794 if /sbin/cryptsetup status "${name}" >/dev/null 2>&1
796 re="^[[:space:]]*device:[[:space:]]*\([^[:space:]]*\)$"
797 opened_dev=$(cryptsetup status ${name} 2>/dev/null | grep "${re}" | sed "s|${re}|\1|")
798 if [ "${opened_dev}" = "${dev}" ]
800 luks_device="/dev/mapper/${name}"
804 log_warning_msg "Cannot open luks device ${dev} since ${opened_dev} already is opened with its name"
813 /lib/cryptsetup/askpass "Enter passphrase for ${dev}: " | \
814 /sbin/cryptsetup -T 1 luksOpen ${dev} ${name} ${opts}
818 luks_device="/dev/mapper/${name}"
824 echo -n "There was an error decrypting ${dev} ... Retry? [Y/n] " >&6
827 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
837 /sbin/blkid -s PART_ENTRY_NAME -p -o value ${dev} 2>/dev/null
843 [ "$(/sbin/blkid -s PART_ENTRY_SCHEME -p -o value ${dev} 2>/dev/null)" = "gpt" ]
846 probe_for_gpt_name ()
848 local overlays="${1}"
849 local snapshots="${2}"
852 local gpt_dev="${dev}"
853 if is_active_luks_mapping ${dev}
855 # if $dev is an opened luks device, we need to check
856 # GPT stuff on the backing device
857 gpt_dev=$(get_luks_backing_device "${dev}")
860 if ! is_gpt_device ${gpt_dev}
865 local gpt_name=$(get_gpt_name ${gpt_dev})
866 for label in ${overlays} ${snapshots}
868 if [ "${gpt_name}" = "${label}" ]
870 echo "${label}=${dev}"
875 probe_for_fs_label ()
877 local overlays="${1}"
878 local snapshots="${2}"
881 for label in ${overlays} ${snapshots}
883 if [ "$(/sbin/blkid -s LABEL -o value $dev 2>/dev/null)" = "${label}" ]
885 echo "${label}=${dev}"
890 probe_for_file_name ()
892 local overlays="${1}"
893 local snapshots="${2}"
897 local backing="$(mount_persistent_media ${dev})"
898 if [ -z "${backing}" ]
903 for label in ${overlays}
905 path=${backing}/${PERSISTENT_PATH}${label}
908 local loopdev=$(setup_loop "${path}" "loop" "/sys/block/loop*")
909 ret="${ret} ${label}=${loopdev}"
912 for label in ${snapshots}
914 for ext in squashfs cpio.gz ext2 ext3 ext4 jffs2
916 path="${PERSISTENT_PATH}${label}.${ext}"
917 if [ -f "${backing}/${path}" ]
919 ret="${ret} ${label}=${dev}:${backing}:${path}"
928 umount ${backing} > /dev/null 2>&1 || true
932 find_persistent_media ()
934 # Scans devices for overlays and snapshots, and returns a whitespace
935 # separated list of how to use them. Only overlays with a partition
936 # label or file name in ${overlays} are returned, and ditto for
937 # snapshots with labels in ${snapshots}.
939 # When scanning a LUKS device, the user will be asked to enter the
940 # passphrase; on failure to enter it, or if no persistent partitions
941 # or files were found, the LUKS device is closed.
943 # For a snapshot file the return value is ${label}=${snapdata}", where
944 # ${snapdata} is the parameter used for try_snap().
946 # For all other cases (overlay/snapshot partition and overlay file) the
947 # return value is "${label}=${device}", where ${device} a device that
948 # can mount the content. In the case of an overlay file, the device
949 # containing the file will remain mounted as a side-effect.
951 # No devices in ${black_listed_devices} will be scanned, and if
952 # ${white_list_devices} is non-empty, only devices in it will be
955 local overlays="${1}"
956 local snapshots="${2}"
957 local white_listed_devices="${3}"
960 for dev in $(storage_devices "" "${white_listed_devices}")
965 # Check if it's a luks device; we'll have to open the device
966 # in order to probe any filesystem it contains, like we do
967 # below. activate_custom_mounts() also depends on that any luks
968 # device already has been opened.
969 if echo ${PERSISTENT_ENCRYPTION} | grep -qe "\<luks\>" && \
970 is_luks_partition ${dev}
972 if luks_device=$(open_luks_device "${dev}")
976 # skip $dev since we failed/chose not to open it
979 elif echo ${PERSISTENT_ENCRYPTION} | grep -qve "\<none\>"
981 # skip $dev since we don't allow unencrypted storage
985 # Probe for matching GPT partition names or filesystem labels
986 if echo ${PERSISTENT_STORAGE} | grep -qe "\<filesystem\>"
988 result=$(probe_for_gpt_name "${overlays}" "${snapshots}" ${dev})
989 if [ -n "${result}" ]
991 ret="${ret} ${result}"
995 result=$(probe_for_fs_label "${overlays}" "${snapshots}" ${dev})
996 if [ -n "${result}" ]
998 ret="${ret} ${result}"
1003 # Probe for files with matching name on mounted partition
1004 if echo ${PERSISTENT_STORAGE} | grep -qe "\<file\>"
1006 result=$(probe_for_file_name "${overlays}" "${snapshots}" ${dev})
1007 if [ -n "${result}" ]
1009 ret="${ret} ${result}"
1014 # Close luks device if it isn't used
1015 if [ -z "${result}" ] && [ -n "${luks_device}" ] && \
1016 is_active_luks_mapping "${luks_device}"
1018 /sbin/cryptsetup luksClose "${luks_device}"
1032 for adaptor in /sys/class/net/*
1034 status="$(cat ${adaptor}/iflink)"
1036 if [ "${status}" -eq 2 ]
1038 mac="$(cat ${adaptor}/address)"
1039 mac="$(echo ${mac} | sed 's/:/-/g' | tr '[a-z]' '[A-Z]')"
1046 is_luks_partition ()
1049 /sbin/cryptsetup isLuks "${device}" 1>/dev/null 2>&1
1052 is_active_luks_mapping ()
1055 /sbin/cryptsetup status "${device}" 1>/dev/null 2>&1
1058 get_luks_backing_device () {
1060 cryptsetup status ${device} 2> /dev/null | \
1061 awk '{if ($1 == "device:") print $2}'
1066 output_format="${1}"
1070 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1073 if [ "$(cat ${sysblock}/removable)" = "1" ]
1075 if [ -z "${want_usb}" ]
1079 if readlink ${sysblock} | grep -q usb
1086 if [ "${dev_ok}" = "yes" ]
1088 case "${output_format}" in
1090 ret="${ret} ${sysblock}"
1093 devname=$(sys2dev "${sysblock}")
1094 ret="${ret} ${devname}"
1103 removable_usb_dev ()
1105 output_format="${1}"
1107 removable_dev "${output_format}" "want_usb"
1110 non_removable_dev ()
1112 output_format="${1}"
1115 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1117 if [ "$(cat ${sysblock}/removable)" = "0" ]
1119 case "${output_format}" in
1121 ret="${ret} ${sysblock}"
1124 devname=$(sys2dev "${sysblock}")
1125 ret="${ret} ${devname}"
1136 # create source's directory structure in dest, and recursively
1137 # create symlinks in dest to to all files in source. if mask
1138 # is non-empty, remove mask from all source paths when
1139 # creating links (will be necessary if we change root, which
1140 # live-boot normally does (into $rootmnt)).
1142 # remove multiple /:s and ensure ending on /
1143 local src_dir="$(echo "${1}"/ | sed -e 's|/\+|/|g')"
1144 local dest_dir="$(echo "${2}"/ | sed -e 's|/\+|/|g')"
1145 local src_mask="${3}"
1147 # This check can only trigger on the inital, non-recursive call since
1148 # we create the destination before recursive calls
1149 if [ ! -d "${dest_dir}" ]
1151 log_warning_msg "Must link_files into a directory"
1155 find "${src_dir}" -mindepth 1 -maxdepth 1 | while read src; do
1156 local dest="${dest_dir}$(basename "${src}")"
1159 if [ -z "$(ls -A "${src}")" ]
1163 if [ ! -d "${dest}" ]
1166 prev="$(dirname "${dest}")"
1167 chown --reference "${prev}" "${dest}"
1168 chmod --reference "${prev}" "${dest}"
1170 link_files "${src}" "${dest}" "${src_mask}"
1172 if [ -n "${src_mask}" ]
1174 src="$(echo ${src} | sed "s|^${src_mask}||")"
1176 rm -rf "${dest}" 2> /dev/null
1177 ln -s "${src}" "${dest}"
1184 local unionmountpoint="${1}" # directory where the union is mounted
1185 local unionrw="${2}" # branch where the union changes are stored
1186 local unionro1="${3}" # first underlying read-only branch (optional)
1187 local unionro2="${4}" # second underlying read-only branch (optional)
1189 if [ "${UNIONTYPE}" = "aufs" ]
1194 elif [ "${UNIONTYPE}" = "unionfs-fuse" ]
1203 case "${UNIONTYPE}" in
1205 unionmountopts="-o cow -o noinitgroups -o default_permissions -o allow_other -o use_ino -o suid"
1206 unionmountopts="${unionmountopts} ${unionrw}=${rw_opt}"
1207 if [ -n "${unionro1}" ]
1209 unionmountopts="${unionmountopts}:${unionro1}=${ro_opt}"
1211 if [ -n "${unionro2}" ]
1213 unionmountopts="${unionmountopts}:${unionro2}=${ro_opt}"
1215 ( sysctl -w fs.file-max=391524 ; ulimit -HSn 16384
1216 unionfs-fuse ${unionmountopts} "${unionmountpoint}" ) && \
1217 ( mkdir -p /run/sendsigs.omit.d
1218 pidof unionfs-fuse >> /run/sendsigs.omit.d/unionfs-fuse || true )
1222 # XXX: can unionro2 be used? (overlayfs only handles two dirs, but perhaps they can be chained?)
1223 # XXX: and can unionro1 be optional? i.e. can overlayfs skip lowerdir?
1224 unionmountopts="-o noatime,lowerdir=${unionro1},upperdir=${unionrw}"
1225 mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
1229 unionmountopts="-o noatime,${noxino_opt},dirs=${unionrw}=${rw_opt}"
1230 if [ -n "${unionro1}" ]
1232 unionmountopts="${unionmountopts}:${unionro1}=${ro_opt}"
1234 if [ -n "${unionro2}" ]
1236 unionmountopts="${unionmountopts}:${unionro2}=${ro_opt}"
1238 mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
1243 get_custom_mounts ()
1245 # Side-effect: leaves $devices with live.persist mounted in ${rootmnt}/live/persistent
1246 # Side-effect: prints info to file $custom_mounts
1248 local custom_mounts=${1}
1252 local bindings="/tmp/bindings.list"
1253 local links="/tmp/links.list"
1254 rm -rf ${bindings} ${links} 2> /dev/null
1256 for device in ${devices}
1258 if [ ! -b "${device}" ]
1263 local device_name="$(basename ${device})"
1264 local backing=$(mount_persistent_media ${device})
1265 if [ -z "${backing}" ]
1270 local include_list="${backing}/${persistence_list}"
1271 if [ ! -r "${include_list}" ]
1276 if [ -n "${DEBUG}" ] && [ -e "${include_list}" ]
1278 cp ${include_list} ${rootmnt}/live/persistent/${persistence_list}.${device_name}
1281 while read dir options # < ${include_list}
1283 if echo ${dir} | grep -qe "^[[:space:]]*\(#.*\)\?$"
1285 # skipping empty or commented lines
1289 if trim_path ${dir} | grep -q -e "^[^/]" -e "^/$" -e "^/live\(/.*\)\?$" -e "^/\(.*/\)\?\.\.\?\(/.*\)\?$"
1291 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)"
1296 local opt_linkfiles=""
1297 for opt in $(echo ${options} | tr ',' ' ');
1301 opt_source=${opt#source=}
1309 log_warning_msg "Skipping custom mount with unkown option: ${opt}"
1315 local source="${dir}"
1316 if [ -n "${opt_source}" ]
1318 if echo ${opt_source} | grep -q -e "^/" -e "^\(.*/\)\?\.\.\?\(/.*\)\?$" && [ "${source}" != "." ]
1320 log_warning_msg "Skipping unsafe custom mount with option source=${opt_source}: must be either \".\" (the media root) a relative path w.r.t. the media root that doesn't contain comas or the special dirs \".\" and \"..\""
1323 source="${opt_source}"
1327 local full_source="$(trim_path ${backing}/${source})"
1328 local full_dest="$(trim_path ${rootmnt}/${dir})"
1329 if [ -n "${opt_linkfiles}" ]
1331 echo "${device} ${full_source} ${full_dest} ${options}" >> ${links}
1333 echo "${device} ${full_source} ${full_dest} ${options}" >> ${bindings}
1335 done < ${include_list}
1338 # We sort the list according to destination so we're sure that
1339 # we won't hide a previous mount. We also ignore duplicate
1340 # destinations in a more or less arbitrary way.
1341 [ -e "${bindings}" ] && sort -k3 -sbu ${bindings} >> ${custom_mounts} && rm ${bindings}
1343 # After all mounts are considered we add symlinks so they
1344 # won't be hidden by some mount.
1345 [ -e "${links}" ] && cat ${links} >> ${custom_mounts} && rm ${links}
1347 # We need to make sure that no two custom mounts have the same sources
1348 # or are nested; if that is the case, too much weird stuff can happen.
1349 local prev_source="impossible source" # first iteration must not match
1351 # This sort will ensure that a source /a comes right before a source
1352 # /a/b so we only need to look at the previous source
1353 sort -k2 -b ${custom_mounts} |
1354 while read device source dest options
1356 if echo ${source} | grep -qe "^${prev_source}\(/.*\)\?$"
1358 panic "Two persistent mounts have the same or nested sources: ${source} on ${dest}, and ${prev_source} on ${prev_dest}"
1360 prev_source=${source}
1365 activate_custom_mounts ()
1367 local custom_mounts="${1}" # the ouput from get_custom_mounts()
1368 local used_devices=""
1370 while read device source dest options # < ${custom_mounts}
1372 local opt_linkfiles=""
1374 for opt in $(echo ${options} | tr ',' ' ');
1386 if [ -n "${opt_linkfiles}" ] && [ -n "${opt_union}" ]
1388 log_warning_msg "Skipping custom mount ${dest} with options ${options}: \"linkfiles\" and \"union\" are mutually exclusive options"
1391 if [ -n "$(what_is_mounted_on "${dest}")" ]
1393 log_warning_msg "Skipping custom mount ${dest}: $(what_is_mounted_on "${dest}") is already mounted there"
1397 # FIXME: we don't handle already existing
1398 # non-directory files in the paths of both $source and
1401 if [ ! -d "${dest}" ]
1403 # if ${dest} is in /home/$user, try fixing
1405 # FIXME: this should really be handled by
1406 # live-config since we don't know for sure
1407 # which uid a certain user has until then
1408 if trim_path ${dest} | grep -qe "^${rootmnt}/*home/[^/]\+"
1411 for dir in $(echo ${dest} | sed -e 's|/\+| |g')
1417 # assume that the intended user is the first, which is usually the case
1418 chown 1000:1000 ${path}
1426 # if ${source} doesn't exist on our persistent media
1427 # we bootstrap it with $dest from the live filesystem.
1428 # this both makes sense and is critical if we're
1429 # dealing with /etc or other system dir.
1430 if [ ! -d "${source}" ]
1432 if [ -n "${PERSISTENT_READONLY}" ]
1435 elif [ -n "${opt_union}" ] || [ -n "${opt_linkfiles}" ]
1437 # unions and don't need to be bootstrapped
1438 # linkfiles dirs can't be bootstrapped in a sensible way
1440 chown --reference "${dest}" "${source}"
1441 chmod --reference "${dest}" "${source}"
1443 # ensure that $dest is not copied *into* $source
1444 mkdir -p "$(dirname ${source})"
1445 cp -a "${dest}" "${source}"
1449 # XXX: If CONFIG_AUFS_ROBR is added to the Debian kernel we can
1450 # ignore the loop below and set rofs_dest_backing=$dest
1451 rofs_dest_backing=""
1452 for d in ${rootmnt}/live/rofs/*
1454 if [ -n "${rootmnt}" ]
1456 rofs_dest_backing="${d}/$(echo ${dest} | sed -e "s|${rootmnt}||")"
1458 rofs_dest_backing="${d}/${dest}"
1460 if [ -d "${rofs_dest_backing}" ]
1464 rofs_dest_backing=""
1468 if [ -z "${PERSISTENT_READONLY}" ]
1470 if [ -n "${opt_linkfiles}" ]
1472 links_source="${source}"
1473 links_dest="${dest}"
1474 elif [ -n "${opt_union}" ]
1476 do_union ${dest} ${source} ${rofs_dest_backing}
1478 mount --bind "${source}" "${dest}"
1481 if [ -n "${opt_linkfiles}" ]
1483 links_dest="${dest}"
1484 dest="$(mktemp -d ${persistent_backing}/links_source-XXXXXX)"
1485 links_source="${dest}"
1487 if [ -n "${rootmnt}" ]
1489 cow_dir="$(echo ${dest} | sed -e "s|${rootmnt}|/cow/|")"
1491 cow_dir="/live/cow/${dest}"
1494 chown --reference "${source}" "${cow_dir}"
1495 chmod --reference "${source}" "${cow_dir}"
1496 do_union ${dest} ${cow_dir} ${source} ${rofs_dest_backing}
1499 if [ -n "${opt_linkfiles}" ]
1501 link_files "${links_source}" "${links_dest}" "${rootmnt}"
1504 PERSISTENCE_IS_ON="1"
1505 export PERSISTENCE_IS_ON
1507 if echo ${used_devices} | grep -qve "^\(.* \)\?${device}\( .*\)\?$"
1509 used_devices="${used_devices} ${device}"
1511 done < ${custom_mounts}
1513 echo ${used_devices}
1516 fix_home_rw_compatibility ()
1520 if [ -n "${PERSISTENT_READONLY}" ]
1525 local backing="$(mount_persistent_media ${device})"
1526 if [ -z "${backing}" ]
1531 local include_list="${backing}/${persistence_list}"
1532 if [ ! -r "${include_list}" ]
1534 echo "# home-rw backwards compatibility:
1535 /home source=." > "${include_list}"