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
523 echo ${1} | sed 's|//|/|g' | sed 's|/$||'
526 what_is_mounted_on ()
528 local dir="$(trim_path ${1})"
529 grep -m1 "^[^ ]\+ ${dir} " /proc/mounts | cut -d' ' -f1
545 mounts="$(awk '{print $2}' /proc/mounts)"
546 testpath="$(busybox realpath ${testpath})"
550 if echo "${mounts}" | grep -qs "^${testpath}"
552 set -- $(echo "${mounts}" | grep "^${testpath}" | lastline)
556 testpath=$(dirname $testpath)
563 # Returns used/free fs kbytes + 5% more
564 # You could pass a block device as ${1} or the mount point as ${2}
570 if [ -z "${mountp}" ]
572 mountp="$(where_is_mounted ${dev})"
574 if [ -z "${mountp}" ]
576 mountp="/mnt/tmp_fs_size"
579 mount -t $(get_fstype "${dev}") -o ro "${dev}" "${mountp}" || log_warning_msg "cannot mount -t $(get_fstype ${dev}) -o ro ${dev} ${mountp}"
585 if [ "${used}" = "used" ]
587 size=$(du -ks ${mountp} | cut -f1)
588 size=$(expr ${size} + ${size} / 20 ) # FIXME: 5% more to be sure
591 size="$(df -k | grep -s ${mountp} | awk '{print $4}')"
594 if [ -n "${doumount}" ]
596 umount "${mountp}" || log_warning_msg "cannot umount ${mountp}"
606 if [ -x /bin/loadkeys -a -r /etc/boottime.kmap.gz ]
608 loadkeys /etc/boottime.kmap.gz
618 local encryption=${5}
621 # the output of setup_loop is evaluated in other functions,
622 # modprobe leaks kernel options like "libata.dma=0"
623 # as "options libata dma=0" on stdout, causing serious
624 # problems therefor, so instead always avoid output to stdout
625 modprobe -q -b "${module}" 1>/dev/null
629 for loopdev in ${pattern}
631 if [ "$(cat ${loopdev}/size)" -eq 0 ]
633 dev=$(sys2dev "${loopdev}")
636 if [ -n "${readonly}" ]
638 if losetup --help 2>&1 | grep -q -- "-r\b"
640 options="${options} -r"
644 if [ -n "${offset}" ] && [ 0 -lt "${offset}" ]
646 options="${options} -o ${offset}"
649 if [ -z "${encryption}" ]
651 losetup ${options} "${dev}" "${fspath}"
653 # Loop AES encryption
658 echo -n "Enter passphrase for root filesystem: " >&6
660 echo "${passphrase}" > /tmp/passphrase
662 exec 9</tmp/passphrase
663 /sbin/losetup ${options} -e "${encryption}" -p 9 "${dev}" "${fspath}"
666 rm -f /tmp/passphrase
668 if [ 0 -eq ${error} ]
675 echo -n "There was an error decrypting the root filesystem ... Retry? [Y/n] " >&6
678 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
691 panic "No loop devices available"
701 old_mountp="$(where_is_mounted ${dev})"
703 if [ -n "${old_mountp}" ]
705 if [ "${opts}" != "ro" ]
707 mount -o remount,"${opts}" "${dev}" "${old_mountp}" || panic "Remounting ${dev} ${opts} on ${old_mountp} failed"
710 mount -o bind "${old_mountp}" "${mountp}" || panic "Cannot bind-mount ${old_mountp} on ${mountp}"
712 if [ -z "${fstype}" ]
714 fstype=$(get_fstype "${dev}")
716 mount -t "${fstype}" -o "${opts}" "${dev}" "${mountp}" || \
717 ( echo "SKIPPING: Cannot mount ${dev} on ${mountp}, fstype=${fstype}, options=${opts}" > live-boot.log && return 0 )
721 mount_persistent_media ()
726 # We can't mount into ${rootmnt}/live before ${rootmnt} has been
727 # mounted since that would cover our mountpoint.
728 if [ -n "${rootmnt}" ] && [ -z "$(what_is_mounted_on ${rootmnt})" ]
730 backing="/$(basename ${device})-backing"
732 backing="${rootmnt}/live/persistent/$(basename ${device})"
735 mkdir -p "${backing}"
736 local old_backing="$(where_is_mounted ${device})"
737 if [ -z "${old_backing}" ]
739 local fstype="$(get_fstype ${device})"
740 local mount_opts="rw,noatime"
741 if [ -n "${PERSISTENT_READONLY}" ]
743 mount_opts="ro,noatime"
745 if mount -t "${fstype}" -o "${mount_opts}" "${device}" "${backing}" >/dev/null
750 log_warning_msg "Failed to mount persistent media ${device}"
753 elif [ "${backing}" != "${old_backing}" ]
755 if mount --move ${old_backing} ${backing} >/dev/null
760 log_warning_msg "Failed to move persistent media ${device}"
767 close_persistent_media () {
769 local backing="$(where_is_mounted ${device})"
771 if [ -d "${backing}" ]
773 umount "${backing}" >/dev/null 2>&1
774 rmdir "${backing}" >/dev/null 2>&1
777 if is_active_luks_mapping ${device}
779 /sbin/cryptsetup luksClose ${device}
786 name="$(basename ${dev})"
788 if [ -n "${PERSISTENT_READONLY}" ]
790 opts="${opts} --readonly"
793 if /sbin/cryptsetup status "${name}" >/dev/null 2>&1
795 re="^[[:space:]]*device:[[:space:]]*\([^[:space:]]*\)$"
796 opened_dev=$(cryptsetup status ${name} 2>/dev/null | grep "${re}" | sed "s|${re}|\1|")
797 if [ "${opened_dev}" = "${dev}" ]
799 luks_device="/dev/mapper/${name}"
803 log_warning_msg "Cannot open luks device ${dev} since ${opened_dev} already is opened with its name"
812 /lib/cryptsetup/askpass "Enter passphrase for ${dev}: " | \
813 /sbin/cryptsetup -T 1 luksOpen ${dev} ${name} ${opts}
817 luks_device="/dev/mapper/${name}"
823 echo -n "There was an error decrypting ${dev} ... Retry? [Y/n] " >&6
826 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
836 /sbin/blkid -s PART_ENTRY_NAME -p -o value ${dev} 2>/dev/null
842 [ "$(/sbin/blkid -s PART_ENTRY_SCHEME -p -o value ${dev} 2>/dev/null)" = "gpt" ]
845 probe_for_gpt_name ()
847 local overlays="${1}"
848 local snapshots="${2}"
851 local gpt_dev="${dev}"
852 if is_active_luks_mapping ${dev}
854 # if $dev is an opened luks device, we need to check
855 # GPT stuff on the backing device
856 gpt_dev=$(get_luks_backing_device "${dev}")
859 if ! is_gpt_device ${gpt_dev}
864 local gpt_name=$(get_gpt_name ${gpt_dev})
865 for label in ${overlays} ${snapshots}
867 if [ "${gpt_name}" = "${label}" ]
869 echo "${label}=${dev}"
874 probe_for_fs_label ()
876 local overlays="${1}"
877 local snapshots="${2}"
880 for label in ${overlays} ${snapshots}
882 if [ "$(/sbin/blkid -s LABEL -o value $dev 2>/dev/null)" = "${label}" ]
884 echo "${label}=${dev}"
889 probe_for_file_name ()
891 local overlays="${1}"
892 local snapshots="${2}"
896 local backing="$(mount_persistent_media ${dev})"
897 if [ -z "${backing}" ]
902 for label in ${overlays}
904 path=${backing}/${PERSISTENT_PATH}${label}
907 local loopdev=$(setup_loop "${path}" "loop" "/sys/block/loop*")
908 ret="${ret} ${label}=${loopdev}"
911 for label in ${snapshots}
913 for ext in squashfs cpio.gz ext2 ext3 ext4 jffs2
915 path="${PERSISTENT_PATH}${label}.${ext}"
916 if [ -f "${backing}/${path}" ]
918 ret="${ret} ${label}=${dev}:${backing}:${path}"
927 umount ${backing} > /dev/null 2>&1 || true
931 find_persistent_media ()
933 # Scans devices for overlays and snapshots, and returns a whitespace
934 # separated list of how to use them. Only overlays with a partition
935 # label or file name in ${overlays} are returned, and ditto for
936 # snapshots with labels in ${snapshots}.
938 # When scanning a LUKS device, the user will be asked to enter the
939 # passphrase; on failure to enter it, or if no persistent partitions
940 # or files were found, the LUKS device is closed.
942 # For a snapshot file the return value is ${label}=${snapdata}", where
943 # ${snapdata} is the parameter used for try_snap().
945 # For all other cases (overlay/snapshot partition and overlay file) the
946 # return value is "${label}=${device}", where ${device} a device that
947 # can mount the content. In the case of an overlay file, the device
948 # containing the file will remain mounted as a side-effect.
950 # No devices in ${black_listed_devices} will be scanned, and if
951 # ${white_list_devices} is non-empty, only devices in it will be
954 local overlays="${1}"
955 local snapshots="${2}"
956 local white_listed_devices="${3}"
959 for dev in $(storage_devices "" "${white_listed_devices}")
964 # Check if it's a luks device; we'll have to open the device
965 # in order to probe any filesystem it contains, like we do
966 # below. activate_custom_mounts() also depends on that any luks
967 # device already has been opened.
968 if echo ${PERSISTENT_ENCRYPTION} | grep -qe "\<luks\>" && \
969 is_luks_partition ${dev}
971 if luks_device=$(open_luks_device "${dev}")
975 # skip $dev since we failed/chose not to open it
978 elif echo ${PERSISTENT_ENCRYPTION} | grep -qve "\<none\>"
980 # skip $dev since we don't allow unencrypted storage
984 # Probe for matching GPT partition names or filesystem labels
985 if echo ${PERSISTENT_STORAGE} | grep -qe "\<filesystem\>"
987 result=$(probe_for_gpt_name "${overlays}" "${snapshots}" ${dev})
988 if [ -n "${result}" ]
990 ret="${ret} ${result}"
994 result=$(probe_for_fs_label "${overlays}" "${snapshots}" ${dev})
995 if [ -n "${result}" ]
997 ret="${ret} ${result}"
1002 # Probe for files with matching name on mounted partition
1003 if echo ${PERSISTENT_STORAGE} | grep -qe "\<file\>"
1005 result=$(probe_for_file_name "${overlays}" "${snapshots}" ${dev})
1006 if [ -n "${result}" ]
1008 ret="${ret} ${result}"
1013 # Close luks device if it isn't used
1014 if [ -z "${result}" ] && [ -n "${luks_device}" ] && \
1015 is_active_luks_mapping "${luks_device}"
1017 /sbin/cryptsetup luksClose "${luks_device}"
1031 for adaptor in /sys/class/net/*
1033 status="$(cat ${adaptor}/iflink)"
1035 if [ "${status}" -eq 2 ]
1037 mac="$(cat ${adaptor}/address)"
1038 mac="$(echo ${mac} | sed 's/:/-/g' | tr '[a-z]' '[A-Z]')"
1045 is_luks_partition ()
1048 /sbin/cryptsetup isLuks "${device}" 1>/dev/null 2>&1
1051 is_active_luks_mapping ()
1054 /sbin/cryptsetup status "${device}" 1>/dev/null 2>&1
1057 get_luks_backing_device () {
1059 cryptsetup status ${device} 2> /dev/null | \
1060 awk '{if ($1 == "device:") print $2}'
1065 output_format="${1}"
1069 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1072 if [ "$(cat ${sysblock}/removable)" = "1" ]
1074 if [ -z "${want_usb}" ]
1078 if readlink ${sysblock} | grep -q usb
1085 if [ "${dev_ok}" = "yes" ]
1087 case "${output_format}" in
1089 ret="${ret} ${sysblock}"
1092 devname=$(sys2dev "${sysblock}")
1093 ret="${ret} ${devname}"
1102 removable_usb_dev ()
1104 output_format="${1}"
1106 removable_dev "${output_format}" "want_usb"
1109 non_removable_dev ()
1111 output_format="${1}"
1114 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1116 if [ "$(cat ${sysblock}/removable)" = "0" ]
1118 case "${output_format}" in
1120 ret="${ret} ${sysblock}"
1123 devname=$(sys2dev "${sysblock}")
1124 ret="${ret} ${devname}"
1135 # create source's directory structure in dest, and recursively
1136 # create symlinks in dest to to all files in source. if mask
1137 # is non-empty, remove mask from all source paths when
1138 # creating links (will be necessary if we change root, which
1139 # live-boot normally does (into $rootmnt)).
1141 # remove multiple /:s and ensure ending on /
1142 local src_dir="$(echo "${1}"/ | sed -e 's|/\+|/|g')"
1143 local dest_dir="$(echo "${2}"/ | sed -e 's|/\+|/|g')"
1144 local src_mask="${3}"
1146 # This check can only trigger on the inital, non-recursive call since
1147 # we create the destination before recursive calls
1148 if [ ! -d "${dest_dir}" ]
1150 log_warning_msg "Must link_files into a directory"
1154 find "${src_dir}" -mindepth 1 -maxdepth 1 | while read src; do
1155 local dest="${dest_dir}$(basename "${src}")"
1158 if [ -z "$(ls -A "${src}")" ]
1162 if [ ! -d "${dest}" ]
1165 prev="$(dirname "${dest}")"
1166 chown --reference "${prev}" "${dest}"
1167 chmod --reference "${prev}" "${dest}"
1169 link_files "${src}" "${dest}" "${src_mask}"
1171 if [ -n "${src_mask}" ]
1173 src="$(echo ${src} | sed "s|^${src_mask}||")"
1175 rm -rf "${dest}" 2> /dev/null
1176 ln -s "${src}" "${dest}"
1183 local unionmountpoint="${1}" # directory where the union is mounted
1184 local unionrw="${2}" # branch where the union changes are stored
1185 local unionro1="${3}" # first underlying read-only branch (optional)
1186 local unionro2="${4}" # second underlying read-only branch (optional)
1188 if [ "${UNIONTYPE}" = "aufs" ]
1193 elif [ "${UNIONTYPE}" = "unionfs-fuse" ]
1202 case "${UNIONTYPE}" in
1204 unionmountopts="-o cow -o noinitgroups -o default_permissions -o allow_other -o use_ino -o suid"
1205 unionmountopts="${unionmountopts} ${unionrw}=${rw_opt}"
1206 if [ -n "${unionro1}" ]
1208 unionmountopts="${unionmountopts}:${unionro1}=${ro_opt}"
1210 if [ -n "${unionro2}" ]
1212 unionmountopts="${unionmountopts}:${unionro2}=${ro_opt}"
1214 ( sysctl -w fs.file-max=391524 ; ulimit -HSn 16384
1215 unionfs-fuse ${unionmountopts} "${unionmountpoint}" ) && \
1216 ( mkdir -p /run/sendsigs.omit.d
1217 pidof unionfs-fuse >> /run/sendsigs.omit.d/unionfs-fuse || true )
1221 # XXX: can unionro2 be used? (overlayfs only handles two dirs, but perhaps they can be chained?)
1222 # XXX: and can unionro1 be optional? i.e. can overlayfs skip lowerdir?
1223 unionmountopts="-o noatime,lowerdir=${unionro1},upperdir=${unionrw}"
1224 mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
1228 unionmountopts="-o noatime,${noxino_opt},dirs=${unionrw}=${rw_opt}"
1229 if [ -n "${unionro1}" ]
1231 unionmountopts="${unionmountopts}:${unionro1}=${ro_opt}"
1233 if [ -n "${unionro2}" ]
1235 unionmountopts="${unionmountopts}:${unionro2}=${ro_opt}"
1237 mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
1242 get_custom_mounts ()
1244 # Side-effect: leaves $devices with live.persist mounted in ${rootmnt}/live/persistent
1245 # Side-effect: prints info to file $custom_mounts
1247 local custom_mounts=${1}
1251 local bindings="/tmp/bindings.list"
1252 local links="/tmp/links.list"
1253 rm -rf ${bindings} ${links} 2> /dev/null
1255 for device in ${devices}
1257 if [ ! -b "${device}" ]
1262 local device_name="$(basename ${device})"
1263 local backing=$(mount_persistent_media ${device})
1264 if [ -z "${backing}" ]
1269 local include_list="${backing}/${persistence_list}"
1270 if [ ! -r "${include_list}" ]
1275 if [ -n "${DEBUG}" ] && [ -e "${include_list}" ]
1277 cp ${include_list} ${rootmnt}/live/persistent/${persistence_list}.${device_name}
1280 while read dir options # < ${include_list}
1282 if echo ${dir} | grep -qe "^[[:space:]]*\(#.*\)\?$"
1284 # skipping empty or commented lines
1288 if trim_path ${dir} | grep -q -e "^[^/]" -e "^/$" -e "^/live\(/.*\)\?$" -e "^/\(.*/\)\?\.\.\?\(/.*\)\?$"
1290 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)"
1295 local opt_linkfiles=""
1296 for opt in $(echo ${options} | tr ',' ' ');
1300 opt_source=${opt#source=}
1308 log_warning_msg "Skipping custom mount with unkown option: ${opt}"
1314 local source="${dir}"
1315 if [ -n "${opt_source}" ]
1317 if echo ${opt_source} | grep -q -e "^/" -e "^\(.*/\)\?\.\.\?\(/.*\)\?$" && [ "${source}" != "." ]
1319 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 \"..\""
1322 source="${opt_source}"
1326 local full_source="$(trim_path ${backing}/${source})"
1327 local full_dest="$(trim_path ${rootmnt}/${dir})"
1328 if [ -n "${opt_linkfiles}" ]
1330 echo "${device} ${full_source} ${full_dest} ${options}" >> ${links}
1332 echo "${device} ${full_source} ${full_dest} ${options}" >> ${bindings}
1334 done < ${include_list}
1337 # We sort the list according to destination so we're sure that
1338 # we won't hide a previous mount. We also ignore duplicate
1339 # destinations in a more or less arbitrary way.
1340 [ -e "${bindings}" ] && sort -k3 -sbu ${bindings} >> ${custom_mounts} && rm ${bindings}
1342 # After all mounts are considered we add symlinks so they
1343 # won't be hidden by some mount.
1344 [ -e "${links}" ] && sort -k3 -sbu ${links} >> ${custom_mounts} && rm ${links}
1347 activate_custom_mounts ()
1349 local custom_mounts="${1}" # the ouput from get_custom_mounts()
1350 local used_devices=""
1352 while read device source dest options # < ${custom_mounts}
1354 local opt_linkfiles=""
1356 for opt in $(echo ${options} | tr ',' ' ');
1368 if [ -n "${opt_linkfiles}" ] && [ -n "${opt_union}" ]
1370 log_warning_msg "Skipping custom mount ${dest} with options ${options}: \"linkfiles\" and \"union\" are mutually exclusive options"
1373 if [ -n "$(what_is_mounted_on "${dest}")" ]
1375 log_warning_msg "Skipping custom mount ${dest}: $(what_is_mounted_on "${dest}") is already mounted there"
1379 # FIXME: we don't handle already existing
1380 # non-directory files in the paths of both $source and
1383 if [ ! -d "${dest}" ]
1385 # if ${dest} is in /home/$user, try fixing
1387 # FIXME: this should really be handled by
1388 # live-config since we don't know for sure
1389 # which uid a certain user has until then
1390 if trim_path ${dest} | grep -qe "^${rootmnt}/*home/[^/]\+"
1393 for dir in $(echo ${dest} | sed -e 's|/\+| |g')
1399 # assume that the intended user is the first, which is usually the case
1400 chown 1000:1000 ${path}
1408 # if ${source} doesn't exist on our persistent media
1409 # we bootstrap it with $dest from the live filesystem.
1410 # this both makes sense and is critical if we're
1411 # dealing with /etc or other system dir.
1412 if [ ! -d "${source}" ]
1414 if [ -n "${PERSISTENT_READONLY}" ]
1417 elif [ -n "${opt_union}" ] || [ -n "${opt_linkfiles}" ]
1419 # unions and don't need to be bootstrapped
1420 # linkfiles dirs can't be bootstrapped in a sensible way
1422 chown --reference "${dest}" "${source}"
1423 chmod --reference "${dest}" "${source}"
1425 # ensure that $dest is not copied *into* $source
1426 mkdir -p "$(dirname ${source})"
1427 cp -a "${dest}" "${source}"
1431 # XXX: If CONFIG_AUFS_ROBR is added to the Debian kernel we can
1432 # ignore the loop below and set rofs_dest_backing=$dest
1433 rofs_dest_backing=""
1434 for d in ${rootmnt}/live/rofs/*
1436 if [ -n "${rootmnt}" ]
1438 rofs_dest_backing="${d}/$(echo ${dest} | sed -e "s|${rootmnt}||")"
1440 rofs_dest_backing="${d}/${dest}"
1442 if [ -d "${rofs_dest_backing}" ]
1446 rofs_dest_backing=""
1450 if [ -z "${PERSISTENT_READONLY}" ]
1452 if [ -n "${opt_linkfiles}" ]
1454 links_source="${source}"
1455 links_dest="${dest}"
1456 elif [ -n "${opt_union}" ]
1458 do_union ${dest} ${source} ${rofs_dest_backing}
1460 mount --bind "${source}" "${dest}"
1463 if [ -n "${opt_linkfiles}" ]
1465 links_dest="${dest}"
1466 dest="$(mktemp -d ${persistent_backing}/links_source-XXXXXX)"
1467 links_source="${dest}"
1469 if [ -n "${rootmnt}" ]
1471 cow_dir="$(echo ${dest} | sed -e "s|${rootmnt}|/cow/|")"
1473 cow_dir="/live/cow/${dest}"
1476 chown --reference "${source}" "${cow_dir}"
1477 chmod --reference "${source}" "${cow_dir}"
1478 do_union ${dest} ${cow_dir} ${source} ${rofs_dest_backing}
1481 if [ -n "${opt_linkfiles}" ]
1483 link_files "${links_source}" "${links_dest}" "${rootmnt}"
1486 PERSISTENCE_IS_ON="1"
1487 export PERSISTENCE_IS_ON
1489 if echo ${used_devices} | grep -qve "^\(.* \)\?${device}\( .*\)\?$"
1491 used_devices="${used_devices} ${device}"
1493 done < ${custom_mounts}
1495 echo ${used_devices}
1498 fix_home_rw_compatibility ()
1502 if [ -n "${PERSISTENT_READONLY}" ]
1507 local backing="$(mount_persistent_media ${device})"
1508 if [ -z "${backing}" ]
1513 local include_list="${backing}/${persistence_list}"
1514 if [ ! -r "${include_list}" ]
1516 echo "# home-rw backwards compatibility:
1517 /home source=." > "${include_list}"