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 if grep -q "^${device} " /proc/mounts
520 # return the first found
521 grep -m1 "^${device} " /proc/mounts | cut -f2 -d ' '
538 mounts="$(awk '{print $2}' /proc/mounts)"
539 testpath="$(busybox realpath ${testpath})"
543 if echo "${mounts}" | grep -qs "^${testpath}"
545 set -- $(echo "${mounts}" | grep "^${testpath}" | lastline)
549 testpath=$(dirname $testpath)
556 # Returns used/free fs kbytes + 5% more
557 # You could pass a block device as ${1} or the mount point as ${2}
563 if [ -z "${mountp}" ]
565 mountp="$(where_is_mounted ${dev})"
567 if [ -z "${mountp}" ]
569 mountp="/mnt/tmp_fs_size"
572 mount -t $(get_fstype "${dev}") -o ro "${dev}" "${mountp}" || log_warning_msg "cannot mount -t $(get_fstype ${dev}) -o ro ${dev} ${mountp}"
578 if [ "${used}" = "used" ]
580 size=$(du -ks ${mountp} | cut -f1)
581 size=$(expr ${size} + ${size} / 20 ) # FIXME: 5% more to be sure
584 size="$(df -k | grep -s ${mountp} | awk '{print $4}')"
587 if [ -n "${doumount}" ]
589 umount "${mountp}" || log_warning_msg "cannot umount ${mountp}"
599 if [ -x /bin/loadkeys -a -r /etc/boottime.kmap.gz ]
601 loadkeys /etc/boottime.kmap.gz
611 local encryption=${5}
614 # the output of setup_loop is evaluated in other functions,
615 # modprobe leaks kernel options like "libata.dma=0"
616 # as "options libata dma=0" on stdout, causing serious
617 # problems therefor, so instead always avoid output to stdout
618 modprobe -q -b "${module}" 1>/dev/null
622 for loopdev in ${pattern}
624 if [ "$(cat ${loopdev}/size)" -eq 0 ]
626 dev=$(sys2dev "${loopdev}")
629 if [ -n "${readonly}" ]
631 if losetup --help 2>&1 | grep -q -- "-r\b"
633 options="${options} -r"
637 if [ -n "${offset}" ] && [ 0 -lt "${offset}" ]
639 options="${options} -o ${offset}"
642 if [ -z "${encryption}" ]
644 losetup ${options} "${dev}" "${fspath}"
646 # Loop AES encryption
651 echo -n "Enter passphrase for root filesystem: " >&6
653 echo "${passphrase}" > /tmp/passphrase
655 exec 9</tmp/passphrase
656 /sbin/losetup ${options} -e "${encryption}" -p 9 "${dev}" "${fspath}"
659 rm -f /tmp/passphrase
661 if [ 0 -eq ${error} ]
668 echo -n "There was an error decrypting the root filesystem ... Retry? [Y/n] " >&6
671 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
684 panic "No loop devices available"
694 old_mountp="$(where_is_mounted ${dev})"
696 if [ -n "${old_mountp}" ]
698 if [ "${opts}" != "ro" ]
700 mount -o remount,"${opts}" "${dev}" "${old_mountp}" || panic "Remounting ${dev} ${opts} on ${old_mountp} failed"
703 mount -o bind "${old_mountp}" "${mountp}" || panic "Cannot bind-mount ${old_mountp} on ${mountp}"
705 if [ -z "${fstype}" ]
707 fstype=$(get_fstype "${dev}")
709 mount -t "${fstype}" -o "${opts}" "${dev}" "${mountp}" || \
710 ( echo "SKIPPING: Cannot mount ${dev} on ${mountp}, fstype=${fstype}, options=${opts}" > live-boot.log && return 0 )
717 name="$(basename ${dev})"
719 if [ -n "${PERSISTENT_READONLY}" ]
721 opts="${opts} --readonly"
724 if /sbin/cryptsetup status "${name}" >/dev/null 2>&1
726 re="^[[:space:]]*device:[[:space:]]*\([^[:space:]]*\)$"
727 opened_dev=$(cryptsetup status ${name} 2>/dev/null | grep "${re}" | sed "s|${re}|\1|")
728 if [ "${opened_dev}" = "${dev}" ]
730 luks_device="/dev/mapper/${name}"
734 log_warning_msg "Cannot open luks device ${dev} since ${opened_dev} already is opened with its name"
743 /lib/cryptsetup/askpass "Enter passphrase for ${dev}: " | \
744 /sbin/cryptsetup -T 1 luksOpen ${dev} ${name} ${opts}
748 luks_device="/dev/mapper/${name}"
754 echo -n "There was an error decrypting ${dev} ... Retry? [Y/n] " >&6
757 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
767 /sbin/blkid -s PART_ENTRY_NAME -p -o value ${dev} 2>/dev/null
773 [ "$(/sbin/blkid -s PART_ENTRY_SCHEME -p -o value ${dev} 2>/dev/null)" = "gpt" ]
776 probe_for_gpt_name ()
778 local overlays="${1}"
779 local snapshots="${2}"
782 if ! is_gpt_device ${dev} || \
783 ( echo ${PERSISTENT_ENCRYPTION} | grep -qve "\<luks\>" && \
784 /sbin/cryptsetup isLuks ${dev} > /dev/null 2>&1 )
788 for label in ${overlays} ${snapshots}
790 if [ "$(get_gpt_name ${dev})" = "${label}" ]
792 echo "${label}=${dev}"
797 probe_for_fs_label ()
799 local overlays="${1}"
800 local snapshots="${2}"
803 for label in ${overlays} ${snapshots}
805 if [ "$(/sbin/blkid -s LABEL -o value $dev 2>/dev/null)" = "${label}" ]
807 echo "${label}=${dev}"
812 probe_for_file_name ()
814 local overlays="${1}"
815 local snapshots="${2}"
818 local devfstype="$(get_fstype ${dev})"
819 local backing="${rootmnt}/live/persistent/$(basename ${dev})"
821 if is_supported_fs ${devfstype} && mkdir -p "${backing}" && \
822 try_mount "${dev}" "${backing}" "rw" "${devfstype}"
824 for label in ${overlays}
826 path=${backing}/${PERSISTENT_PATH}${label}
829 local loopdev=$(setup_loop "${path}" "loop" "/sys/block/loop*")
830 ret="${ret} ${label}=${loopdev}"
833 for label in ${snapshots}
835 for ext in squashfs cpio.gz ext2 ext3 ext4 jffs2
837 path="${PERSISTENT_PATH}${label}.${ext}"
838 if [ -f "${backing}/${path}" ]
840 ret="${ret} ${label}=${dev}:${backing}:${path}"
849 umount ${backing} > /dev/null 2>&1 || true
854 find_persistent_media ()
856 # Scans devices for overlays and snapshots, and returns a whitespace
857 # separated list of how to use them. Only overlays with a partition
858 # label or file name in ${overlays} are returned, and ditto for
859 # snapshots with labels in ${snapshots}.
861 # When scanning a LUKS device, the user will be asked to enter the
862 # passphrase; on failure to enter it, or if no persistent partitions
863 # or files were found, the LUKS device is closed.
865 # For a snapshot file the return value is ${label}=${snapdata}", where
866 # ${snapdata} is the parameter used for try_snap().
868 # For all other cases (overlay/snapshot partition and overlay file) the
869 # return value is "${label}=${device}", where ${device} a device that
870 # can mount the content. In the case of an overlay file, the device
871 # containing the file will remain mounted as a side-effect.
873 # No devices in ${black_listed_devices} will be scanned, and if
874 # ${white_list_devices} is non-empty, only devices in it will be
877 local overlays="${1}"
878 local snapshots="${2}"
879 local white_listed_devices="${3}"
882 for dev in $(storage_devices "" "${white_listed_devices}")
888 # Check if it's a luks device; we'll have to open the device
889 # in order to probe any filesystem it contains, like we do
890 # below. do_custom_mounts() also depends on that any luks
891 # device already has been opened.
892 if echo ${PERSISTENT_ENCRYPTION} | grep -qe "\<luks\>" && \
893 /sbin/cryptsetup isLuks ${dev} >/dev/null 2>&1
895 if luks_device=$(open_luks_device "${dev}")
900 # skip $dev since we failed/chose not to open it
903 elif echo ${PERSISTENT_ENCRYPTION} | grep -qve "\<none\>"
905 # skip $dev since we don't allow unencrypted storage
909 # Probe for matching GPT partition names or filesystem labels
910 if echo ${PERSISTENT_STORAGE} | grep -qe "\<filesystem\>"
912 local gpt_dev="${dev}"
913 if [ -n "${luks_device}" ]
915 # When we probe GPT partitions we need to look
916 # at the real device, not the virtual, opened
918 gpt_dev="${real_dev}"
920 result=$(probe_for_gpt_name "${overlays}" "${snapshots}" ${gpt_dev})
921 if [ -n "${result}" ]
923 ret="${ret} ${result}"
927 result=$(probe_for_fs_label "${overlays}" "${snapshots}" ${dev})
928 if [ -n "${result}" ]
930 ret="${ret} ${result}"
935 # Probe for files with matching name on mounted partition
936 if echo ${PERSISTENT_STORAGE} | grep -qe "\<file\>"
938 result=$(probe_for_file_name "${overlays}" "${snapshots}" ${dev})
939 if [ -n "${result}" ]
941 ret="${ret} ${result}"
946 # Close luks device if it isn't used
947 if [ -z "${result}" ] && [ -n "${luks_device}" ] && \
948 /sbin/cryptsetup status "${luks_device}" 1> /dev/null 2>&1
950 /sbin/cryptsetup luksClose "${luks_device}"
964 for adaptor in /sys/class/net/*
966 status="$(cat ${adaptor}/iflink)"
968 if [ "${status}" -eq 2 ]
970 mac="$(cat ${adaptor}/address)"
971 mac="$(echo ${mac} | sed 's/:/-/g' | tr '[a-z]' '[A-Z]')"
981 if [ -x /sbin/cryptsetup ]
983 /sbin/cryptsetup isLuks "${devname}" 2>/dev/null || ret=${?}
997 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1000 if [ "$(cat ${sysblock}/removable)" = "1" ]
1002 if [ -z "${want_usb}" ]
1006 if readlink ${sysblock} | grep -q usb
1013 if [ "${dev_ok}" = "yes" ]
1015 case "${output_format}" in
1017 ret="${ret} ${sysblock}"
1020 devname=$(sys2dev "${sysblock}")
1021 ret="${ret} ${devname}"
1030 removable_usb_dev ()
1032 output_format="${1}"
1034 removable_dev "${output_format}" "want_usb"
1037 non_removable_dev ()
1039 output_format="${1}"
1042 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1044 if [ "$(cat ${sysblock}/removable)" = "0" ]
1046 case "${output_format}" in
1048 ret="${ret} ${sysblock}"
1051 devname=$(sys2dev "${sysblock}")
1052 ret="${ret} ${devname}"
1063 # create source's directory structure in dest, and recursively
1064 # create symlinks in dest to to all files in source. if mask
1065 # is non-empty, remove mask from all source paths when
1066 # creating links (will be necessary if we change root, which
1067 # live-boot normally does (into $rootmnt)).
1069 # remove multiple /:s and ensure ending on /
1070 local src_dir="$(echo "${1}"/ | sed -e 's|/\+|/|g')"
1071 local dest_dir="$(echo "${2}"/ | sed -e 's|/\+|/|g')"
1072 local src_mask="${3}"
1074 # This check can only trigger on the inital, non-recursive call since
1075 # we create the destination before recursive calls
1076 if [ ! -d "${dest_dir}" ];
1078 log_warning_msg "Must link_files into a directory"
1082 find "${src_dir}" -mindepth 1 -maxdepth 1 | while read x; do
1084 local dest="${dest_dir}$(basename "${x}")"
1087 if [ -z "$(ls -A "${src}")" ];
1091 if [ ! -d "${dest}" ];
1094 prev="$(dirname "${dest}")"
1095 chown --reference "${prev}" "${dest}"
1096 chmod --reference "${prev}" "${dest}"
1098 link_files "${src}" "${dest}" "${src_mask}"
1100 if [ -n "${src_mask}" ]
1102 src="$(echo ${src} | sed "s|^${src_mask}||")"
1104 rm -rf "${dest}" 2> /dev/null
1105 ln -s "${src}" "${dest}"
1112 local unionmountpoint="${1}" # directory where the union is mounted
1113 local unionrw="${2}" # branch where the union changes are stored
1114 local unionro1="${3}" # first underlying read-only branch (optional)
1115 local unionro2="${4}" # second underlying read-only branch (optional)
1117 if [ "${UNIONTYPE}" = "aufs" ]
1122 elif [ "${UNIONTYPE}" = "unionfs-fuse" ]
1131 case "${UNIONTYPE}" in
1133 unionmountopts="-o cow -o noinitgroups -o default_permissions -o allow_other -o use_ino -o suid"
1134 unionmountopts="${unionmountopts} ${unionrw}=${rw_opt}"
1135 if [ -n "${unionro1}" ]
1137 unionmountopts="${unionmountopts}:${unionro1}=${ro_opt}"
1139 if [ -n "${unionro2}" ]
1141 unionmountopts="${unionmountopts}:${unionro2}=${ro_opt}"
1143 ( sysctl -w fs.file-max=391524 ; ulimit -HSn 16384
1144 unionfs-fuse ${unionmountopts} "${unionmountpoint}" ) && \
1145 ( mkdir -p /run/sendsigs.omit.d
1146 pidof unionfs-fuse >> /run/sendsigs.omit.d/unionfs-fuse || true )
1150 # XXX: can unionro2 be used? (overlayfs only handles two dirs, but perhaps they can be chained?)
1151 # XXX: and can unionro1 be optional? i.e. can overlayfs skip lowerdir?
1152 unionmountopts="-o noatime,lowerdir=${unionro1},upperdir=${unionrw}"
1153 mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
1157 unionmountopts="-o noatime,${noxino_opt},dirs=${unionrw}=${rw_opt}"
1158 if [ -n "${unionro1}" ]
1160 unionmountopts="${unionmountopts}:${unionro1}=${ro_opt}"
1162 if [ -n "${unionro2}" ]
1164 unionmountopts="${unionmountopts}:${unionro2}=${ro_opt}"
1166 mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
1171 get_custom_mounts ()
1173 # Side-effect: leaves $devices with live.persist mounted in ${rootmnt}/live/persistent
1174 # Side-effect: prints info to file $custom_mounts
1176 local devices="${1}"
1177 local custom_mounts="${2}" # print result to this file
1178 local rootmnt="${3}" # should be set empty post-live-boot
1180 local bindings="/bindings.list"
1181 local links="/links.list"
1182 rm -rf ${bindings} ${links} 2> /dev/null
1183 local persistent_backing="${rootmnt}/live/persistent"
1185 for device in ${devices}
1187 if [ ! -b "${device}" ]
1192 local device_name="$(basename ${device})"
1193 local device_used=""
1194 # $device may already have been mounted by
1195 # probe_for_file_name() in find_persistent_media() ...
1196 local backing=$(where_is_mounted ${device})
1197 if [ -z "${backing}" ]
1199 # ... otherwise we mount it now
1200 backing="${persistent_backing}/${device_name}"
1201 mkdir -p "${backing}"
1202 local device_fstype="$(get_fstype ${device})"
1203 if [ -z "${PERSISTENT_READONLY}" ]
1205 device_mount_opts="rw,noatime"
1207 device_mount_opts="ro,noatime"
1209 if ! mount -t "${device_fstype}" -o "${device_mount_opts}" "${device}" "${backing}" >/dev/null 2>&1
1211 log_warning_msg "Could not mount persistent media ${device} (${device_fstype})"
1214 local include_list="${backing}/${persistence_list}"
1215 if [ ! -r "${include_list}" ]
1217 umount "${backing}" >/dev/null 2>&1
1218 rmdir "${backing}" >/dev/null 2>&1
1219 if /sbin/cryptsetup status ${device_name} >/dev/null 2>&1
1221 /sbin/cryptsetup luksClose "${device_name}"
1226 [ "${DEBUG}" = "Yes" ] && cp ${include_list} ${persistent_backing}/${persistence_list}.${device_name}
1227 while read source dest options # < ${include_list}
1229 if echo ${source} | grep -qe "^[[:space:]]*\(#.*\)\?$"
1231 # skipping empty or commented lines
1235 if echo ${dest} | grep -qe "^[^/]"
1239 elif [ -z "${dest}" ]
1244 if echo ${dest} | grep -qe "^/\+$\|^/\+live\(/.*\)\?$"
1246 # mounting on / or /live could cause trouble
1247 log_warning_msg "Skipping unsafe custom mount on ${dest}"
1251 for opt in $(echo ${options} | tr ',' ' ');
1257 log_warning_msg "Skipping custom mount with unkown option: ${opt}"
1263 # FIXME: handle case: we already have /a/b in
1264 # $bindings added from current $device, but
1265 # now we find /a -- /a should replace /a/b in
1268 # FIXME: handle case: we have /a in $bindings
1269 # from current $device, now we find /a/b, so
1272 # ensure that no multiple-/ occur in paths
1273 local full_source="$(echo ${backing}/${source}/ | sed -e 's|/\+|/|g')"
1274 local full_dest="$(echo ${rootmnt}/${dest}/ | sed -e 's|/\+|/|g')"
1276 if echo ${options} | grep -qe "\<linkfiles\>";
1278 echo "${full_source} ${full_dest} ${options}" >> ${links}
1280 echo "${full_source} ${full_dest} ${options}" >> ${bindings}
1282 done < ${include_list}
1284 if [ -z "${device_used}" ]
1286 # this device was not used for / earlier, or
1287 # custom mount point now, so it's useless
1293 # We sort the list according to destination so we're sure that
1294 # we won't hide a previous mount. We also ignore duplicate
1295 # destinations in a more or less arbitrary way.
1296 [ -e "${bindings}" ] && sort -k2 -sbu ${bindings} >> ${custom_mounts} && rm ${bindings}
1298 # After all mounts are considered we add symlinks so they
1299 # won't be hidden by some mount.
1300 [ -e "${links}" ] && sort -k2 -sbu ${links} >> ${custom_mounts} && rm ${links}
1305 local custom_mounts="${1}" # the ouput from get_custom_mounts()
1306 local rootmnt="${2}" # should be set empty post-live-boot
1308 while read source dest options # < ${custom_mounts}
1310 local opt_linkfiles=""
1312 for opt in $(echo ${options} | tr ',' ' ');
1324 if mountpoint -q "${dest}";
1326 log_warning_msg "Skipping custom mount ${source} on ${dest}: destination is already a mount point"
1330 # FIXME: we don't handle already existing
1331 # non-directory files in the paths of both $source and
1334 if [ ! -d "${dest}" ]
1336 # if ${dest} is in /home/$user, try fixing
1338 # FIXME: this should really be handled by
1339 # live-config since we don't know for sure
1340 # which uid a certain user has until then
1341 if echo ${dest} | grep -qe "^${rootmnt}/*home/\+[^/]\+"
1344 for dir in $(echo ${dest} | sed -e 's|/\+| |g')
1350 # assume that the intended user is the first, which is usually the case
1351 chown 1000:1000 ${path}
1359 # if ${source} doesn't exist on our persistent media
1360 # we bootstrap it with $dest from the live filesystem.
1361 # this both makes sense and is critical if we're
1362 # dealing with /etc or other system dir.
1363 if [ ! -d "${source}" ]
1365 if [ -n "${PERSISTENT_READONLY}" ] || [ -n "${opt_linkfiles}" ]
1368 elif [ -n "${opt_union}" ]
1370 # union's don't need to be bootstrapped
1373 # ensure that $dest is not copied *into* $source
1374 mkdir -p "$(dirname ${source})"
1375 cp -a "${dest}" "${source}"
1379 rofs_dest_backing=""
1380 for d in ${rootmnt}/live/rofs/*
1382 if [ -n "${rootmnt}" ]
1384 rofs_dest_backing="${d}/$(echo ${dest} | sed -e "s|${rootmnt}||")"
1386 rofs_dest_backing="${d}/${dest}"
1389 if [ -d "${rofs_dest_backing}" ]
1393 rofs_dest_backing=""
1397 if [ -z "${PERSISTENT_READONLY}" ]
1399 if [ -n "${opt_linkfiles}" ]
1401 links_source="${source}"
1402 links_dest="${dest}"
1403 elif [ -n "${opt_union}" ]
1405 do_union ${dest} ${source} ${rofs_dest_backing}
1407 mount --bind "${source}" "${dest}"
1410 if [ -n "${opt_linkfiles}" ]
1412 links_dest="${dest}"
1413 dest="$(mktemp -d ${persistent_backing}/links_source-XXXXXX)"
1414 links_source="${dest}"
1416 if [ -n "${rootmnt}" ]
1418 cow_dir="$(echo ${dest} | sed -e "s|${rootmnt}|/cow/|")"
1420 cow_dir="/live/cow/${dest}"
1423 do_union ${dest} ${cow_dir} ${source} ${rofs_dest_backing}
1426 if [ -n "${opt_linkfiles}" ]
1428 link_files "${links_source}" "${links_dest}" "${rootmnt}"
1431 PERSISTENCE_IS_ON="1"
1432 export PERSISTENCE_IS_ON
1433 done < ${custom_mounts}
1436 fix_home_rw_compatibility ()
1440 if [ -n "${PERSISTENT_READONLY}" ]
1445 local backing="$(where_is_mounted ${device})"
1446 if [ -z "${backing}" ]
1448 backing="${rootmnt}/live/persistent/$(basename ${device})"
1449 mkdir -p "${backing}"
1450 local device_fstype="$(get_fstype ${device})"
1451 local device_mount_opts="rw,noatime"
1452 if ! mount -t "${device_fstype}" -o "${device_mount_opts}" "${device}" "${backing}" >/dev/null 2>&1
1457 local include_list="${backing}/${persistence_list}"
1458 if [ ! -r "${include_list}" ]
1460 echo "# home-rw backwards compatibility:
1461 . /home" > "${include_list}"