1 # live-boot helper functions, used by live-boot on boot and by live-snapshot
3 if [ ! -x "/bin/fstype" ]
5 # klibc not in path -> not in initramfs
6 export PATH="${PATH}:/usr/lib/klibc/bin"
9 # handle upgrade path from old udev (using udevinfo) to
10 # recent versions of udev (using udevadm info)
11 if [ -x /sbin/udevadm ]
13 udevinfo='/sbin/udevadm info'
18 old_root_overlay_label="live-rw"
19 old_home_overlay_label="home-rw"
20 custom_overlay_label="custom-ov"
21 root_snapshot_label="live-sn"
22 old_root_snapshot_label="live-sn"
23 home_snapshot_label="home-sn"
24 persistence_list="live-persistence.conf"
31 for ARGUMENT in $(cat /proc/cmdline)
40 export NOACCESSIBILITY NOFASTBOOT NOFSTAB NONETWORKING
44 ACCESS="${ARGUMENT#access=}"
49 DEFCONSOLE="${ARGUMENT#*=}"
65 # Force dhcp even while netbooting
66 # Use for debugging in case somebody works on fixing dhclient
76 DEVICE="${ARGUMENT#ethdevice=}"
78 export DEVICE ETHDEVICE
82 ETHDEV_TIMEOUT="${ARGUMENT#ethdevice-timeout=}"
87 FETCH="${ARGUMENT#fetch=}"
92 FINDISO="${ARGUMENT#findiso=}"
97 FORCEPERSISTENCEFSCK="Yes"
98 export FORCEPERSISTENCEFSCK
102 FTPFS="${ARGUMENT#ftpfs=}"
107 HTTPFS="${ARGUMENT#httpfs=}"
112 ISCSI="${ARGUMENT#iscsi=}"
113 #ip:port - separated by ;
114 ISCSI_PORTAL="${ISCSI%;*}"
115 if echo "${ISCSI_PORTAL}" | grep -q , ; then
116 ISCSI_SERVER="${ISCSI_PORTAL%,*}"
117 ISCSI_PORT="${ISCSI_PORTAL#*,}"
120 ISCSI_TARGET="${ISCSI#*;}"
121 export ISCSI ISCSI_PORTAL ISCSI_TARGET ISCSI_SERVER ISCSI_PORT
125 FROMISO="${ARGUMENT#*=}"
135 INTEGRITY_CHECK="Yes"
136 export INTEGRITY_CHECK
140 STATICIP="${ARGUMENT#ip=}"
142 if [ -z "${STATICIP}" ]
155 live-media=*|bootfrom=*)
156 LIVE_MEDIA="${ARGUMENT#*=}"
160 live-media-encryption=*|encryption=*)
161 LIVE_MEDIA_ENCRYPTION="${ARGUMENT#*=}"
162 export LIVE_MEDIA_ENCRYPTION
166 LIVE_MEDIA_OFFSET="${ARGUMENT#live-media-offset=}"
167 export LIVE_MEDIA_OFFSET
171 LIVE_MEDIA_PATH="${ARGUMENT#live-media-path=}"
172 export LIVE_MEDIA_PATH
175 live-media-timeout=*)
176 LIVE_MEDIA_TIMEOUT="${ARGUMENT#live-media-timeout=}"
177 export LIVE_MEDIA_TIMEOUT
181 MODULE="${ARGUMENT#module=}"
186 NETBOOT="${ARGUMENT#netboot=}"
191 NFSOPTS="${ARGUMENT#nfsopts=}"
196 NFS_COW="${ARGUMENT#nfsoverlay=}"
201 NOACCESSIBILITY="Yes"
202 export NOACCESSIBILITY
221 ramdisk_size="${ARGUMENT#ramdisk-size=}"
234 persistence-encryption=*)
235 PERSISTENCE_ENCRYPTION="${ARGUMENT#*=}"
236 export PERSISTENCE_ENCRYPTION
240 PERSISTENCE_MEDIA="${ARGUMENT#*=}"
241 export PERSISTENCE_MEDIA
243 persistence-method=*)
244 PERSISTENCE_METHOD="${ARGUMENT#*=}"
245 export PERSISTENCE_METHOD
249 PERSISTENCE_PATH="${ARGUMENT#persistence-path=}"
250 export PERSISTENCE_PATH
252 persistence-read-only)
253 PERSISTENCE_READONLY="Yes"
254 export PERSISTENCE_READONLY
257 persistence-storage=*)
258 PERSISTENCE_STORAGE="${ARGUMENT#persistence-storage=}"
259 export PERSISTENCE_STORAGE
262 persistence-subtext=*)
263 old_root_overlay_label="${old_root_overlay_label}-${ARGUMENT#persistence-subtext=}"
264 old_home_overlay_label="${old_home_overlay_label}-${ARGUMENT#persistence-subtext=}"
265 custom_overlay_label="${custom_overlay_label}-${ARGUMENT#persistence-subtext=}"
266 root_snapshot_label="${root_snapshot_label}-${ARGUMENT#persistence-subtext=}"
267 old_root_snapshot_label="${root_snapshot_label}-${ARGUMENT#persistence-subtext=}"
268 home_snapshot_label="${home_snapshot_label}-${ARGUMENT#persistence-subtext=}"
282 NOPROMPT="${ARGUMENT#noprompt=}"
287 QUICKUSBMODULES="Yes"
288 export QUICKUSBMODULES
291 preseed/file=*|file=*)
292 LOCATIONS="${ARGUMENT#*=} ${LOCATIONS}"
302 question="${ARGUMENT%%=*}"
303 value="${ARGUMENT#*=}"
304 PRESEEDS="${PRESEEDS}\"${question}=${value}\" "
319 TODISK="${ARGUMENT#todisk=}"
330 MODULETORAM="${ARGUMENT#toram=}"
331 export TORAM MODULETORAM
345 SKIP_UNION_MOUNTS="Yes"
346 export SKIP_UNION_MOUNTS
350 ROOT="${ARGUMENT#root=}"
355 UNIONTYPE="${ARGUMENT#union=}"
361 # sort of compatibility with netboot.h from linux docs
362 if [ -z "${NETBOOT}" ]
364 if [ "${ROOT}" = "/dev/nfs" ]
368 elif [ "${ROOT}" = "/dev/cifs" ]
375 if [ -z "${MODULE}" ]
381 if [ -z "${UNIONTYPE}" ]
387 if [ -z "${PERSISTENCE_ENCRYPTION}" ]
389 PERSISTENCE_ENCRYPTION="none"
390 export PERSISTENCE_ENCRYPTION
391 elif is_in_comma_sep_list luks ${PERSISTENCE_ENCRYPTION}
393 if ! modprobe dm-crypt
395 log_warning_msg "Unable to load module dm-crypt"
396 PERSISTENCE_ENCRYPTION=$(echo ${PERSISTENCE_ENCRYPTION} | sed -e 's/\<luks,\|,\?luks$//g')
397 export PERSISTENCE_ENCRYPTION
400 if [ ! -x /lib/cryptsetup/askpass ] || [ ! -x /sbin/cryptsetup ]
402 log_warning_msg "cryptsetup in unavailable"
403 PERSISTENCE_ENCRYPTION=$(echo ${PERSISTENCE_ENCRYPTION} | sed -e 's/\<luks,\|,\?luks$//g')
404 export PERSISTENCE_ENCRYPTION
408 if [ -z "${PERSISTENCE_METHOD}" ]
410 PERSISTENCE_METHOD="snapshot,overlay"
411 export PERSISTENCE_METHOD
414 if [ -z "${PERSISTENCE_STORAGE}" ]
416 PERSISTENCE_STORAGE="filesystem,file"
417 export PERSISTENCE_STORAGE
421 is_in_list_separator_helper () {
427 echo ${list} | grep -qe "^\(.*${sep}\)\?${element}\(${sep}.*\)\?$"
430 is_in_space_sep_list () {
433 is_in_list_separator_helper "[[:space:]]" "${element}" "${*}"
436 is_in_comma_sep_list () {
439 is_in_list_separator_helper "," "${element}" "${*}"
445 echo "/dev/$($udevinfo -q name -p ${sysdev} 2>/dev/null|| echo ${sysdev##*/})"
453 for dev in "${sysblock}"/* "${sysblock}"
455 if [ -e "${dev}/dev" ]
466 black_listed_devices="${1}"
467 white_listed_devices="${2}"
469 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "loop|ram|fd")
471 fulldevname=$(sys2dev "${sysblock}")
473 if is_in_space_sep_list ${fulldevname} ${black_listed_devices} || \
474 [ -n "${white_listed_devices}" ] && \
475 ! is_in_space_sep_list ${fulldevname} ${white_listed_devices}
477 # skip this device entirely
481 for dev in $(subdevices "${sysblock}")
483 devname=$(sys2dev "${dev}")
485 if is_in_space_sep_list ${devname} ${black_listed_devices}
487 # skip this subdevice
500 # Validate input first
501 if [ -z "${fstype}" ]
506 # Try to look if it is already supported by the kernel
507 if grep -q ${fstype} /proc/filesystems
511 # Then try to add support for it the gentle way using the initramfs capabilities
513 if grep -q ${fstype} /proc/filesystems
516 # Then try the hard way if /root is already reachable
518 kmodule="/root/lib/modules/`uname -r`/${fstype}/${fstype}.ko"
519 if [ -e "${kmodule}" ]
522 if grep -q ${fstype} /proc/filesystems
535 /sbin/blkid -s TYPE -o value $1 2>/dev/null
542 grep -m1 "^${device} " /proc/mounts | cut -f2 -d ' '
546 # remove all unnecessary /:s in the path, including last one (except
547 # if path is just "/")
548 echo ${1} | sed 's|//\+|/|g' | sed 's|^\(.*[^/]\)/$|\1|'
551 what_is_mounted_on ()
553 local dir="$(trim_path ${1})"
554 grep -m1 "^[^ ]\+ ${dir} " /proc/mounts | cut -d' ' -f1
559 local reference="${1}"
562 local owner=$(stat -c %u:%g "${reference}")
563 chown -h ${owner} ${targets}
568 local reference="${1}"
571 local rights=$(stat -c %a "${reference}")
572 chmod ${rights} ${targets}
588 mounts="$(awk '{print $2}' /proc/mounts)"
589 testpath="$(busybox realpath ${testpath})"
593 if echo "${mounts}" | grep -qs "^${testpath}"
595 set -- $(echo "${mounts}" | grep "^${testpath}" | lastline)
599 testpath=$(dirname $testpath)
606 # Returns used/free fs kbytes + 5% more
607 # You could pass a block device as ${1} or the mount point as ${2}
613 if [ -z "${mountp}" ]
615 mountp="$(where_is_mounted ${dev})"
617 if [ -z "${mountp}" ]
619 mountp="/mnt/tmp_fs_size"
622 mount -t $(get_fstype "${dev}") -o ro "${dev}" "${mountp}" || log_warning_msg "cannot mount -t $(get_fstype ${dev}) -o ro ${dev} ${mountp}"
628 if [ "${used}" = "used" ]
630 size=$(du -ks ${mountp} | cut -f1)
631 size=$(expr ${size} + ${size} / 20 ) # FIXME: 5% more to be sure
634 size="$(df -k | grep -s ${mountp} | awk '{print $4}')"
637 if [ -n "${doumount}" ]
639 umount "${mountp}" || log_warning_msg "cannot umount ${mountp}"
649 if [ -x /bin/loadkeys -a -r /etc/boottime.kmap.gz ]
651 loadkeys /etc/boottime.kmap.gz
661 local encryption=${5}
664 # the output of setup_loop is evaluated in other functions,
665 # modprobe leaks kernel options like "libata.dma=0"
666 # as "options libata dma=0" on stdout, causing serious
667 # problems therefor, so instead always avoid output to stdout
668 modprobe -q -b "${module}" 1>/dev/null
672 for loopdev in ${pattern}
674 if [ "$(cat ${loopdev}/size)" -eq 0 ]
676 dev=$(sys2dev "${loopdev}")
679 if [ -n "${readonly}" ]
681 if losetup --help 2>&1 | grep -q -- "-r\b"
683 options="${options} -r"
687 if [ -n "${offset}" ] && [ 0 -lt "${offset}" ]
689 options="${options} -o ${offset}"
692 if [ -z "${encryption}" ]
694 losetup ${options} "${dev}" "${fspath}"
696 # Loop AES encryption
701 echo -n "Enter passphrase for root filesystem: " >&6
703 echo "${passphrase}" > /tmp/passphrase
705 exec 9</tmp/passphrase
706 /sbin/losetup ${options} -e "${encryption}" -p 9 "${dev}" "${fspath}"
709 rm -f /tmp/passphrase
711 if [ 0 -eq ${error} ]
718 echo -n "There was an error decrypting the root filesystem ... Retry? [Y/n] " >&6
721 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
734 panic "No loop devices available"
744 old_mountp="$(where_is_mounted ${dev})"
746 if [ -n "${old_mountp}" ]
748 if [ "${opts}" != "ro" ]
750 mount -o remount,"${opts}" "${dev}" "${old_mountp}" || panic "Remounting ${dev} ${opts} on ${old_mountp} failed"
753 mount -o bind "${old_mountp}" "${mountp}" || panic "Cannot bind-mount ${old_mountp} on ${mountp}"
755 if [ -z "${fstype}" ]
757 fstype=$(get_fstype "${dev}")
759 mount -t "${fstype}" -o "${opts}" "${dev}" "${mountp}" || \
760 ( echo "SKIPPING: Cannot mount ${dev} on ${mountp}, fstype=${fstype}, options=${opts}" > boot.log && return 0 )
764 mount_persistence_media ()
769 local backing="/live/persistence/$(basename ${device})"
771 mkdir -p "${backing}"
772 local old_backing="$(where_is_mounted ${device})"
773 if [ -z "${old_backing}" ]
775 local fstype="$(get_fstype ${device})"
776 local mount_opts="rw,noatime"
777 if [ -n "${PERSISTENCE_READONLY}" ]
779 mount_opts="ro,noatime"
781 if mount -t "${fstype}" -o "${mount_opts}" "${device}" "${backing}" >/dev/null
786 [ -z "${probe}" ] && log_warning_msg "Failed to mount persistence media ${device}"
790 elif [ "${backing}" != "${old_backing}" ]
792 if mount --move ${old_backing} ${backing} >/dev/null
797 [ -z "${probe}" ] && log_warning_msg "Failed to move persistence media ${device}"
805 close_persistence_media () {
807 local backing="$(where_is_mounted ${device})"
809 if [ -d "${backing}" ]
811 umount "${backing}" >/dev/null 2>&1
812 rmdir "${backing}" >/dev/null 2>&1
815 if is_active_luks_mapping ${device}
817 /sbin/cryptsetup luksClose ${device}
824 name="$(basename ${dev})"
826 if [ -n "${PERSISTENCE_READONLY}" ]
828 opts="${opts} --readonly"
831 if /sbin/cryptsetup status "${name}" >/dev/null 2>&1
833 re="^[[:space:]]*device:[[:space:]]*\([^[:space:]]*\)$"
834 opened_dev=$(cryptsetup status ${name} 2>/dev/null | grep "${re}" | sed "s|${re}|\1|")
835 if [ "${opened_dev}" = "${dev}" ]
837 luks_device="/dev/mapper/${name}"
841 log_warning_msg "Cannot open luks device ${dev} since ${opened_dev} already is opened with its name"
850 /lib/cryptsetup/askpass "Enter passphrase for ${dev}: " | \
851 /sbin/cryptsetup -T 1 luksOpen ${dev} ${name} ${opts}
855 luks_device="/dev/mapper/${name}"
861 echo -n "There was an error decrypting ${dev} ... Retry? [Y/n] " >&6
864 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
874 /sbin/blkid -s PART_ENTRY_NAME -p -o value ${dev} 2>/dev/null
880 [ "$(/sbin/blkid -s PART_ENTRY_SCHEME -p -o value ${dev} 2>/dev/null)" = "gpt" ]
883 probe_for_gpt_name ()
885 local overlays="${1}"
886 local snapshots="${2}"
889 local gpt_dev="${dev}"
890 if is_active_luks_mapping ${dev}
892 # if $dev is an opened luks device, we need to check
893 # GPT stuff on the backing device
894 gpt_dev=$(get_luks_backing_device "${dev}")
897 if ! is_gpt_device ${gpt_dev}
902 local gpt_name=$(get_gpt_name ${gpt_dev})
903 for label in ${overlays} ${snapshots}
905 if [ "${gpt_name}" = "${label}" ]
907 echo "${label}=${dev}"
912 probe_for_fs_label ()
914 local overlays="${1}"
915 local snapshots="${2}"
918 for label in ${overlays} ${snapshots}
920 if [ "$(/sbin/blkid -s LABEL -o value $dev 2>/dev/null)" = "${label}" ]
922 echo "${label}=${dev}"
927 probe_for_file_name ()
929 local overlays="${1}"
930 local snapshots="${2}"
934 local backing="$(mount_persistence_media ${dev} probe)"
935 if [ -z "${backing}" ]
940 for label in ${overlays}
942 path=${backing}/${PERSISTENCE_PATH}${label}
945 local loopdev=$(setup_loop "${path}" "loop" "/sys/block/loop*")
946 ret="${ret} ${label}=${loopdev}"
949 for label in ${snapshots}
951 for ext in squashfs cpio.gz ext2 ext3 ext4 jffs2
953 path="${PERSISTENCE_PATH}${label}.${ext}"
954 if [ -f "${backing}/${path}" ]
956 ret="${ret} ${label}=${dev}:${backing}:${path}"
965 umount ${backing} > /dev/null 2>&1 || true
969 find_persistence_media ()
971 # Scans devices for overlays and snapshots, and returns a whitespace
972 # separated list of how to use them. Only overlays with a partition
973 # label or file name in ${overlays} are returned, and ditto for
974 # snapshots with labels in ${snapshots}.
976 # When scanning a LUKS device, the user will be asked to enter the
977 # passphrase; on failure to enter it, or if no persistence partitions
978 # or files were found, the LUKS device is closed.
980 # For a snapshot file the return value is ${label}=${snapdata}", where
981 # ${snapdata} is the parameter used for try_snap().
983 # For all other cases (overlay/snapshot partition and overlay file) the
984 # return value is "${label}=${device}", where ${device} a device that
985 # can mount the content. In the case of an overlay file, the device
986 # containing the file will remain mounted as a side-effect.
988 # No devices in ${black_listed_devices} will be scanned, and if
989 # ${white_list_devices} is non-empty, only devices in it will be
992 local overlays="${1}"
993 local snapshots="${2}"
994 local white_listed_devices="${3}"
997 local black_listed_devices="$(what_is_mounted_on /live/image)"
999 for dev in $(storage_devices "${black_listed_devices}" "${white_listed_devices}")
1003 local luks_device=""
1004 # Check if it's a luks device; we'll have to open the device
1005 # in order to probe any filesystem it contains, like we do
1006 # below. activate_custom_mounts() also depends on that any luks
1007 # device already has been opened.
1008 if is_in_comma_sep_list luks ${PERSISTENCE_ENCRYPTION} && \
1009 is_luks_partition ${dev}
1011 if luks_device=$(open_luks_device "${dev}")
1013 dev="${luks_device}"
1015 # skip $dev since we failed/chose not to open it
1018 elif ! is_in_comma_sep_list none ${PERSISTENCE_ENCRYPTION}
1020 # skip $dev since we don't allow unencrypted storage
1024 # Probe for matching GPT partition names or filesystem labels
1025 if is_in_comma_sep_list filesystem ${PERSISTENCE_STORAGE}
1027 result=$(probe_for_gpt_name "${overlays}" "${snapshots}" ${dev})
1028 if [ -n "${result}" ]
1030 ret="${ret} ${result}"
1034 result=$(probe_for_fs_label "${overlays}" "${snapshots}" ${dev})
1035 if [ -n "${result}" ]
1037 ret="${ret} ${result}"
1042 # Probe for files with matching name on mounted partition
1043 if is_in_comma_sep_list file ${PERSISTENCE_STORAGE}
1045 result=$(probe_for_file_name "${overlays}" "${snapshots}" ${dev})
1046 if [ -n "${result}" ]
1048 ret="${ret} ${result}"
1053 # Close luks device if it isn't used
1054 if [ -z "${result}" ] && [ -n "${luks_device}" ] && \
1055 is_active_luks_mapping "${luks_device}"
1057 /sbin/cryptsetup luksClose "${luks_device}"
1071 for adaptor in /sys/class/net/*
1073 status="$(cat ${adaptor}/iflink)"
1075 if [ "${status}" -eq 2 ]
1077 mac="$(cat ${adaptor}/address)"
1078 mac="$(echo ${mac} | sed 's/:/-/g' | tr '[a-z]' '[A-Z]')"
1085 is_luks_partition ()
1088 /sbin/cryptsetup isLuks "${device}" 1>/dev/null 2>&1
1091 is_active_luks_mapping ()
1094 /sbin/cryptsetup status "${device}" 1>/dev/null 2>&1
1097 get_luks_backing_device () {
1099 cryptsetup status ${device} 2> /dev/null | \
1100 awk '{if ($1 == "device:") print $2}'
1105 output_format="${1}"
1109 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1112 if [ "$(cat ${sysblock}/removable)" = "1" ]
1114 if [ -z "${want_usb}" ]
1118 if readlink ${sysblock} | grep -q usb
1125 if [ "${dev_ok}" = "yes" ]
1127 case "${output_format}" in
1129 ret="${ret} ${sysblock}"
1132 devname=$(sys2dev "${sysblock}")
1133 ret="${ret} ${devname}"
1142 removable_usb_dev ()
1144 output_format="${1}"
1146 removable_dev "${output_format}" "want_usb"
1149 non_removable_dev ()
1151 output_format="${1}"
1154 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1156 if [ "$(cat ${sysblock}/removable)" = "0" ]
1158 case "${output_format}" in
1160 ret="${ret} ${sysblock}"
1163 devname=$(sys2dev "${sysblock}")
1164 ret="${ret} ${devname}"
1175 # create source's directory structure in dest, and recursively
1176 # create symlinks in dest to to all files in source. if mask
1177 # is non-empty, remove mask from all source paths when
1178 # creating links (will be necessary if we change root, which
1179 # live-boot normally does (into $rootmnt)).
1181 # remove multiple /:s and ensure ending on /
1182 local src_dir="$(trim_path ${1})/"
1183 local dest_dir="$(trim_path ${2})/"
1184 local src_mask="${3}"
1186 # This check can only trigger on the inital, non-recursive call since
1187 # we create the destination before recursive calls
1188 if [ ! -d "${dest_dir}" ]
1190 log_warning_msg "Must link_files into a directory"
1194 find "${src_dir}" -mindepth 1 -maxdepth 1 | while read src; do
1195 local dest="${dest_dir}$(basename "${src}")"
1198 if [ -z "$(ls -A "${src}")" ]
1202 if [ ! -d "${dest}" ]
1205 chown_ref "${src}" "${dest}"
1206 chmod_ref "${src}" "${dest}"
1208 link_files "${src}" "${dest}" "${src_mask}"
1210 local final_src=${src}
1211 if [ -n "${src_mask}" ]
1213 final_src="$(echo ${final_src} | sed "s|^${src_mask}||")"
1215 rm -rf "${dest}" 2> /dev/null
1216 ln -s "${final_src}" "${dest}"
1217 chown_ref "${src}" "${dest}"
1224 local unionmountpoint="${1}" # directory where the union is mounted
1225 local unionrw="${2}" # branch where the union changes are stored
1226 local unionro1="${3}" # first underlying read-only branch (optional)
1227 local unionro2="${4}" # second underlying read-only branch (optional)
1229 if [ "${UNIONTYPE}" = "aufs" ]
1234 elif [ "${UNIONTYPE}" = "unionfs-fuse" ]
1243 case "${UNIONTYPE}" in
1245 unionmountopts="-o cow -o noinitgroups -o default_permissions -o allow_other -o use_ino -o suid"
1246 unionmountopts="${unionmountopts} ${unionrw}=${rw_opt}"
1247 if [ -n "${unionro1}" ]
1249 unionmountopts="${unionmountopts}:${unionro1}=${ro_opt}"
1251 if [ -n "${unionro2}" ]
1253 unionmountopts="${unionmountopts}:${unionro2}=${ro_opt}"
1255 ( sysctl -w fs.file-max=391524 ; ulimit -HSn 16384
1256 unionfs-fuse ${unionmountopts} "${unionmountpoint}" ) && \
1257 ( mkdir -p /run/sendsigs.omit.d
1258 pidof unionfs-fuse >> /run/sendsigs.omit.d/unionfs-fuse || true )
1262 # XXX: can unionro2 be used? (overlayfs only handles two dirs, but perhaps they can be chained?)
1263 # XXX: and can unionro1 be optional? i.e. can overlayfs skip lowerdir?
1264 unionmountopts="-o noatime,lowerdir=${unionro1},upperdir=${unionrw}"
1265 mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
1269 unionmountopts="-o noatime,${noxino_opt},dirs=${unionrw}=${rw_opt}"
1270 if [ -n "${unionro1}" ]
1272 unionmountopts="${unionmountopts}:${unionro1}=${ro_opt}"
1274 if [ -n "${unionro2}" ]
1276 unionmountopts="${unionmountopts}:${unionro2}=${ro_opt}"
1278 mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
1283 get_custom_mounts ()
1285 # Side-effect: leaves $devices with live-persistence.conf mounted in /live/persistence
1286 # Side-effect: prints info to file $custom_mounts
1288 local custom_mounts=${1}
1292 local bindings="/tmp/bindings.list"
1293 local links="/tmp/links.list"
1294 rm -rf ${bindings} ${links} 2> /dev/null
1296 for device in ${devices}
1298 if [ ! -b "${device}" ]
1303 local device_name="$(basename ${device})"
1304 local backing=$(mount_persistence_media ${device})
1305 if [ -z "${backing}" ]
1310 local include_list="${backing}/${persistence_list}"
1311 if [ ! -r "${include_list}" ]
1316 if [ -n "${DEBUG}" ] && [ -e "${include_list}" ]
1318 cp ${include_list} /live/persistence/${persistence_list}.${device_name}
1321 while read dir options # < ${include_list}
1323 if echo ${dir} | grep -qe "^[[:space:]]*\(#.*\)\?$"
1325 # skipping empty or commented lines
1329 if trim_path ${dir} | grep -q -e "^[^/]" -e "^/live\(/.*\)\?$" -e "^/\(.*/\)\?\.\.\?\(/.*\)\?$"
1331 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."
1337 for opt in $(echo ${options} | tr ',' ' ');
1341 opt_source=${opt#source=}
1349 log_warning_msg "Skipping custom mount with unkown option: ${opt}"
1355 local source="${dir}"
1356 if [ -n "${opt_source}" ]
1358 if echo ${opt_source} | grep -q -e "^/" -e "^\(.*/\)\?\.\.\?\(/.*\)\?$" && [ "${source}" != "." ]
1360 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"
1363 source="${opt_source}"
1367 local full_source="$(trim_path ${backing}/${source})"
1368 local full_dest="$(trim_path ${rootmnt}/${dir})"
1369 if [ -n "${opt_link}" ]
1371 echo "${device} ${full_source} ${full_dest} ${options}" >> ${links}
1373 echo "${device} ${full_source} ${full_dest} ${options}" >> ${bindings}
1375 done < ${include_list}
1378 # We sort the list according to destination so we're sure that
1379 # we won't hide a previous mount. We also ignore duplicate
1380 # destinations in a more or less arbitrary way.
1381 [ -e "${bindings}" ] && sort -k3 -sbu ${bindings} >> ${custom_mounts} && rm ${bindings}
1383 # After all mounts are considered we add symlinks so they
1384 # won't be hidden by some mount.
1385 [ -e "${links}" ] && cat ${links} >> ${custom_mounts} && rm ${links}
1387 # We need to make sure that no two custom mounts have the same sources
1388 # or are nested; if that is the case, too much weird stuff can happen.
1389 local prev_source="impossible source" # first iteration must not match
1391 # This sort will ensure that a source /a comes right before a source
1392 # /a/b so we only need to look at the previous source
1393 sort -k2 -b ${custom_mounts} |
1394 while read device source dest options
1396 if echo ${source} | grep -qe "^${prev_source}\(/.*\)\?$"
1398 panic "Two persistence mounts have the same or nested sources: ${source} on ${dest}, and ${prev_source} on ${prev_dest}"
1400 prev_source=${source}
1405 activate_custom_mounts ()
1407 local custom_mounts="${1}" # the ouput from get_custom_mounts()
1408 local used_devices=""
1410 while read device source dest options # < ${custom_mounts}
1412 local opt_bind="yes"
1415 for opt in $(echo ${options} | tr ',' ' ');
1420 unset opt_link opt_union
1424 unset opt_bind opt_union
1428 unset opt_bind opt_link
1433 if [ -n "$(what_is_mounted_on "${dest}")" ]
1435 if [ "${dest}" = "${rootmnt}" ]
1439 log_warning_msg "Skipping custom mount ${dest}: $(what_is_mounted_on "${dest}") is already mounted there"
1444 if [ ! -d "${dest}" ]
1446 # create the destination and delete existing files in
1447 # its path that are in the way
1449 for dir in $(echo ${dest} | sed -e 's|/\+| |g')
1451 path=$(trim_path ${path}/${dir})
1459 if echo ${path} | grep -qe "^${rootmnt}/*home/[^/]\+"
1461 # if ${dest} is in /home try fixing proper ownership by assuming that the intended user is the first, which is usually the case
1462 # FIXME: this should really be handled by live-config since we don't know for sure which uid a certain user has until then
1463 chown 1000:1000 ${path}
1469 # if ${source} doesn't exist on our persistence media
1470 # we bootstrap it with $dest from the live filesystem.
1471 # this both makes sense and is critical if we're
1472 # dealing with /etc or other system dir.
1473 if [ ! -d "${source}" ]
1475 if [ -n "${PERSISTENCE_READONLY}" ]
1478 elif [ -n "${opt_union}" ] || [ -n "${opt_link}" ]
1480 # unions and don't need to be bootstrapped
1481 # link dirs can't be bootstrapped in a sensible way
1482 mkdir -p "${source}"
1483 chown_ref "${dest}" "${source}"
1484 chmod_ref "${dest}" "${source}"
1485 elif [ -n "${opt_bind}" ]
1487 # ensure that $dest is not copied *into* $source
1488 mkdir -p "$(dirname ${source})"
1489 cp -a "${dest}" "${source}"
1493 # XXX: If CONFIG_AUFS_ROBR is added to the Debian kernel we can
1494 # ignore the loop below and set rofs_dest_backing=$dest
1495 local rofs_dest_backing=""
1496 if [ -n "${opt_link}"]
1498 for d in /live/rofs/*
1500 if [ -n "${rootmnt}" ]
1502 rofs_dest_backing="${d}/$(echo ${dest} | sed -e "s|${rootmnt}||")"
1504 rofs_dest_backing="${d}/${dest}"
1506 if [ -d "${rofs_dest_backing}" ]
1510 rofs_dest_backing=""
1515 if [ -n "${opt_link}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1517 link_files ${source} ${dest} ${rootmnt}
1518 elif [ -n "${opt_link}" ] && [ -n "${PERSISTENCE_READONLY}" ]
1520 mkdir -p /live/persistence
1521 local links_source=$(mktemp -d /live/persistence/links-source-XXXXXX)
1522 chown_ref ${source} ${links_source}
1523 chmod_ref ${source} ${links_source}
1524 # We put the cow dir in the below strange place to
1525 # make it absolutely certain that the link source
1526 # has its own directory and isn't nested with some
1527 # other custom mount (if so that mount's files would
1528 # be linked, causing breakage.
1529 local cow_dir="/live/overlay/live/persistence/$(basename ${links_source})"
1531 chown_ref "${source}" "${cow_dir}"
1532 chmod_ref "${source}" "${cow_dir}"
1533 do_union ${links_source} ${cow_dir} ${source} ${rofs_dest_backing}
1534 link_files ${links_source} ${dest} ${rootmnt}
1535 elif [ -n "${opt_union}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1537 do_union ${dest} ${source} ${rofs_dest_backing}
1538 elif [ -n "${opt_bind}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1540 mount --bind "${source}" "${dest}"
1541 elif [ -n "${opt_bind}" -o -n "${opt_union}" ] && [ -n "${PERSISTENCE_READONLY}" ]
1543 # bind-mount and union mount are handled the same
1544 # in read-only mode, but note that rofs_dest_backing
1545 # is non-empty (and necessary) only for unions
1546 if [ -n "${rootmnt}" ]
1548 local cow_dir="$(echo ${dest} | sed -e "s|^${rootmnt}|/live/overlay/|")"
1550 # This is happens if persistence is activated
1552 local cow_dir="/live/overlay/${dest}"
1554 if [ -e "${cow_dir}" ] && [ -z "${opt_link}" ]
1556 # If an earlier custom mount has files here
1557 # it will "block" the current mount's files
1558 # which is undesirable
1562 chown_ref "${source}" "${cow_dir}"
1563 chmod_ref "${source}" "${cow_dir}"
1564 do_union ${dest} ${cow_dir} ${source} ${rofs_dest_backing}
1567 PERSISTENCE_IS_ON="1"
1568 export PERSISTENCE_IS_ON
1570 if echo ${used_devices} | grep -qve "^\(.* \)\?${device}\( .*\)\?$"
1572 used_devices="${used_devices} ${device}"
1574 done < ${custom_mounts}
1576 echo ${used_devices}
1579 fix_backwards_compatibility ()
1585 if [ -n "${PERSISTENCE_READONLY}" ]
1590 local backing="$(mount_persistence_media ${device})"
1591 if [ -z "${backing}" ]
1596 local include_list="${backing}/${persistence_list}"
1597 if [ ! -r "${include_list}" ]
1599 echo "# persistence backwards compatibility:
1600 ${dir} ${opt},source=." > "${include_list}"
1608 [ $(stat -fc%d:%D "${directory}") != $(stat -fc%d:%D "${directory}/..") ]