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" ]
766 /sbin/blkid -s PART_ENTRY_NAME -p -o value ${dev} 2>/dev/null
771 [ "$(/sbin/blkid -s PART_ENTRY_SCHEME -p -o value ${dev} 2>/dev/null)" = "gpt" ]
774 probe_for_gpt_name ()
776 local overlays="${1}"
777 local snapshots="${2}"
780 if ! is_gpt_device ${dev} || \
781 ( echo ${PERSISTENT_ENCRYPTION} | grep -qve "\<luks\>" && \
782 /sbin/cryptsetup isLuks ${dev} > /dev/null 2>&1 )
786 for label in ${overlays} ${snapshots}
788 if [ "$(get_gpt_name ${dev})" = "${label}" ]
790 echo "${label}=${dev}"
795 probe_for_fs_label () {
796 local overlays="${1}"
797 local snapshots="${2}"
800 for label in ${overlays} ${snapshots}
802 if [ "$(/sbin/blkid -s LABEL -o value $dev 2>/dev/null)" = "${label}" ]
804 echo "${label}=${dev}"
809 probe_for_file_name () {
810 local overlays="${1}"
811 local snapshots="${2}"
814 local devfstype="$(get_fstype ${dev})"
815 local backing="${rootmnt}/live/persistent/$(basename ${dev})"
817 if is_supported_fs ${devfstype} && mkdir -p "${backing}" && \
818 try_mount "${dev}" "${backing}" "rw" "${devfstype}"
820 for label in ${overlays}
822 path=${backing}/${PERSISTENT_PATH}${label}
825 local loopdev=$(setup_loop "${path}" "loop" "/sys/block/loop*")
826 ret="${ret} ${label}=${loopdev}"
829 for label in ${snapshots}
831 for ext in squashfs cpio.gz ext2 ext3 ext4 jffs2
833 path="${PERSISTENT_PATH}${label}.${ext}"
834 if [ -f "${backing}/${path}" ]
836 ret="${ret} ${label}=${dev}:${backing}:${path}"
845 umount ${backing} > /dev/null 2>&1 || true
850 find_persistent_media ()
852 # Scans devices for overlays and snapshots, and returns a whitespace
853 # separated list of how to use them. Only overlays with a partition
854 # label or file name in ${overlays} are returned, and ditto for
855 # snapshots with labels in ${snapshots}.
857 # When scanning a LUKS device, the user will be asked to enter the
858 # passphrase; on failure to enter it, or if no persistent partitions
859 # or files were found, the LUKS device is closed.
861 # For a snapshot file the return value is ${label}=${snapdata}", where
862 # ${snapdata} is the parameter used for try_snap().
864 # For all other cases (overlay/snapshot partition and overlay file) the
865 # return value is "${label}=${device}", where ${device} a device that
866 # can mount the content. In the case of an overlay file, the device
867 # containing the file will remain mounted as a side-effect.
869 # No devices in ${black_listed_devices} will be scanned, and if
870 # ${white_list_devices} is non-empty, only devices in it will be
873 local overlays="${1}"
874 local snapshots="${2}"
875 local white_listed_devices="${3}"
878 for dev in $(storage_devices "" "${white_listed_devices}")
884 # Check if it's a luks device; we'll have to open the device
885 # in order to probe any filesystem it contains, like we do
886 # below. do_custom_mounts() also depends on that any luks
887 # device already has been opened.
888 if echo ${PERSISTENT_ENCRYPTION} | grep -qe "\<luks\>" && \
889 /sbin/cryptsetup isLuks ${dev} >/dev/null 2>&1
891 if luks_device=$(open_luks_device "${dev}")
896 # skip $dev since we failed/chose not to open it
899 elif echo ${PERSISTENT_ENCRYPTION} | grep -qve "\<none\>"
901 # skip $dev since we don't allow unencrypted storage
905 # Probe for matching GPT partition names or filesystem labels
906 if echo ${PERSISTENT_STORAGE} | grep -qe "\<filesystem\>"
908 local gpt_dev="${dev}"
909 if [ -n "${luks_device}" ]
911 # When we probe GPT partitions we need to look
912 # at the real device, not the virtual, opened
914 gpt_dev="${real_dev}"
916 result=$(probe_for_gpt_name "${overlays}" "${snapshots}" ${gpt_dev})
917 if [ -n "${result}" ]
919 ret="${ret} ${result}"
923 result=$(probe_for_fs_label "${overlays}" "${snapshots}" ${dev})
924 if [ -n "${result}" ]
926 ret="${ret} ${result}"
931 # Probe for files with matching name on mounted partition
932 if echo ${PERSISTENT_STORAGE} | grep -qe "\<file\>"
934 result=$(probe_for_file_name "${overlays}" "${snapshots}" ${dev})
935 if [ -n "${result}" ]
937 ret="${ret} ${result}"
942 # Close luks device if it isn't used
943 if [ -z "${result}" ] && [ -n "${luks_device}" ] && \
944 /sbin/cryptsetup status "${luks_device}" 1> /dev/null 2>&1
946 /sbin/cryptsetup luksClose "${luks_device}"
960 for adaptor in /sys/class/net/*
962 status="$(cat ${adaptor}/iflink)"
964 if [ "${status}" -eq 2 ]
966 mac="$(cat ${adaptor}/address)"
967 mac="$(echo ${mac} | sed 's/:/-/g' | tr '[a-z]' '[A-Z]')"
977 if [ -x /sbin/cryptsetup ]
979 /sbin/cryptsetup isLuks "${devname}" 2>/dev/null || ret=${?}
993 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
996 if [ "$(cat ${sysblock}/removable)" = "1" ]
998 if [ -z "${want_usb}" ]
1002 if readlink ${sysblock} | grep -q usb
1009 if [ "${dev_ok}" = "yes" ]
1011 case "${output_format}" in
1013 ret="${ret} ${sysblock}"
1016 devname=$(sys2dev "${sysblock}")
1017 ret="${ret} ${devname}"
1026 removable_usb_dev ()
1028 output_format="${1}"
1030 removable_dev "${output_format}" "want_usb"
1033 non_removable_dev ()
1035 output_format="${1}"
1038 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1040 if [ "$(cat ${sysblock}/removable)" = "0" ]
1042 case "${output_format}" in
1044 ret="${ret} ${sysblock}"
1047 devname=$(sys2dev "${sysblock}")
1048 ret="${ret} ${devname}"
1059 # create source's directory structure in dest, and recursively
1060 # create symlinks in dest to to all files in source. if mask
1061 # is non-empty, remove mask from all source paths when
1062 # creating links (will be necessary if we change root, which
1063 # live-boot normally does (into $rootmnt)).
1065 # remove multiple /:s and ensure ending on /
1066 local src_dir="$(echo "${1}"/ | sed -e 's|/\+|/|g')"
1067 local dest_dir="$(echo "${2}"/ | sed -e 's|/\+|/|g')"
1068 local src_mask="${3}"
1070 # This check can only trigger on the inital, non-recursive call since
1071 # we create the destination before recursive calls
1072 if [ ! -d "${dest_dir}" ];
1074 log_warning_msg "Must link_files into a directory"
1078 find "${src_dir}" -mindepth 1 -maxdepth 1 | while read x; do
1080 local dest="${dest_dir}$(basename "${x}")"
1083 if [ -z "$(ls -A "${src}")" ];
1087 if [ ! -d "${dest}" ];
1090 prev="$(dirname "${dest}")"
1091 chown --reference "${prev}" "${dest}"
1092 chmod --reference "${prev}" "${dest}"
1094 link_files "${src}" "${dest}" "${src_mask}"
1096 if [ -n "${src_mask}" ]
1098 src="$(echo ${src} | sed "s|^${src_mask}||")"
1100 rm -rf "${dest}" 2> /dev/null
1101 ln -s "${src}" "${dest}"
1107 local unionmountpoint="${1}" # directory where the union is mounted
1108 local unionrw="${2}" # branch where the union changes are stored
1109 local unionro1="${3}" # first underlying read-only branch (optional)
1110 local unionro2="${4}" # second underlying read-only branch (optional)
1112 if [ "${UNIONTYPE}" = "aufs" ]
1117 elif [ "${UNIONTYPE}" = "unionfs-fuse" ]
1126 case "${UNIONTYPE}" in
1128 unionmountopts="-o cow -o noinitgroups -o default_permissions -o allow_other -o use_ino -o suid"
1129 unionmountopts="${unionmountopts} ${unionrw}=${rw_opt}"
1130 if [ -n "${unionro1}" ]
1132 unionmountopts="${unionmountopts}:${unionro1}=${ro_opt}"
1134 if [ -n "${unionro2}" ]
1136 unionmountopts="${unionmountopts}:${unionro2}=${ro_opt}"
1138 ( sysctl -w fs.file-max=391524 ; ulimit -HSn 16384
1139 unionfs-fuse ${unionmountopts} "${unionmountpoint}" ) && \
1140 ( mkdir -p /run/sendsigs.omit.d
1141 pidof unionfs-fuse >> /run/sendsigs.omit.d/unionfs-fuse || true )
1145 # XXX: can unionro2 be used? (overlayfs only handles two dirs, but perhaps they can be chained?)
1146 # XXX: and can unionro1 be optional? i.e. can overlayfs skip lowerdir?
1147 unionmountopts="-o noatime,lowerdir=${unionro1},upperdir=${unionrw}"
1148 mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
1152 unionmountopts="-o noatime,${noxino_opt},dirs=${unionrw}=${rw_opt}"
1153 if [ -n "${unionro1}" ]
1155 unionmountopts="${unionmountopts}:${unionro1}=${ro_opt}"
1157 if [ -n "${unionro2}" ]
1159 unionmountopts="${unionmountopts}:${unionro2}=${ro_opt}"
1161 mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
1166 get_custom_mounts () {
1167 # Side-effect: leaves $devices with live.persist mounted in ${rootmnt}/live/persistent
1168 # Side-effect: prints info to file $custom_mounts
1170 local devices="${1}"
1171 local custom_mounts="${2}" # print result to this file
1172 local rootmnt="${3}" # should be set empty post-live-boot
1174 local bindings="/bindings.list"
1175 local links="/links.list"
1176 rm -rf ${bindings} ${links} 2> /dev/null
1177 local persistent_backing="${rootmnt}/live/persistent"
1179 for device in ${devices}
1181 if [ ! -b "${device}" ]
1186 local device_name="$(basename ${device})"
1187 local device_used=""
1188 # $device may already have been mounted by
1189 # probe_for_file_name() in find_persistent_media() ...
1190 local backing=$(where_is_mounted ${device})
1191 if [ -z "${backing}" ]
1193 # ... otherwise we mount it now
1194 backing="${persistent_backing}/${device_name}"
1195 mkdir -p "${backing}"
1196 local device_fstype="$(get_fstype ${device})"
1197 if [ -z "${PERSISTENT_READONLY}" ]
1199 device_mount_opts="rw,noatime"
1201 device_mount_opts="ro,noatime"
1203 if ! mount -t "${device_fstype}" -o "${device_mount_opts}" "${device}" "${backing}" >/dev/null 2>&1
1205 log_warning_msg "Could not mount persistent media ${device} (${device_fstype})"
1208 local include_list="${backing}/${persistence_list}"
1209 if [ ! -r "${include_list}" ]
1211 umount "${backing}" >/dev/null 2>&1
1212 rmdir "${backing}" >/dev/null 2>&1
1213 if /sbin/cryptsetup status ${device_name} >/dev/null 2>&1
1215 /sbin/cryptsetup luksClose "${device_name}"
1220 [ "${DEBUG}" = "Yes" ] && cp ${include_list} ${persistent_backing}/${persistence_list}.${device_name}
1221 while read source dest options # < ${include_list}
1223 if echo ${source} | grep -qe "^[[:space:]]*\(#.*\)\?$"
1225 # skipping empty or commented lines
1229 if echo ${dest} | grep -qe "^[^/]"
1233 elif [ -z "${dest}" ]
1238 if echo ${dest} | grep -qe "^/\+$\|^/\+live\(/.*\)\?$"
1240 # mounting on / or /live could cause trouble
1241 log_warning_msg "Skipping unsafe custom mount on ${dest}"
1245 for opt in $(echo ${options} | tr ',' ' ');
1251 log_warning_msg "Skipping custom mount with unkown option: ${opt}"
1257 # FIXME: handle case: we already have /a/b in
1258 # $bindings added from current $device, but
1259 # now we find /a -- /a should replace /a/b in
1262 # FIXME: handle case: we have /a in $bindings
1263 # from current $device, now we find /a/b, so
1266 # ensure that no multiple-/ occur in paths
1267 local full_source="$(echo ${backing}/${source}/ | sed -e 's|/\+|/|g')"
1268 local full_dest="$(echo ${rootmnt}/${dest}/ | sed -e 's|/\+|/|g')"
1270 if echo ${options} | grep -qe "\<linkfiles\>";
1272 echo "${full_source} ${full_dest} ${options}" >> ${links}
1274 echo "${full_source} ${full_dest} ${options}" >> ${bindings}
1276 done < ${include_list}
1278 if [ -z "${device_used}" ]
1280 # this device was not used for / earlier, or
1281 # custom mount point now, so it's useless
1287 # We sort the list according to destination so we're sure that
1288 # we won't hide a previous mount. We also ignore duplicate
1289 # destinations in a more or less arbitrary way.
1290 [ -e "${bindings}" ] && sort -k2 -sbu ${bindings} >> ${custom_mounts} && rm ${bindings}
1292 # After all mounts are considered we add symlinks so they
1293 # won't be hidden by some mount.
1294 [ -e "${links}" ] && sort -k2 -sbu ${links} >> ${custom_mounts} && rm ${links}
1297 do_custom_mounts () {
1298 local custom_mounts="${1}" # the ouput from get_custom_mounts()
1299 local rootmnt="${2}" # should be set empty post-live-boot
1301 while read source dest options # < ${custom_mounts}
1303 local opt_linkfiles=""
1305 for opt in $(echo ${options} | tr ',' ' ');
1317 if mountpoint -q "${dest}";
1319 log_warning_msg "Skipping custom mount ${source} on ${dest}: destination is already a mount point"
1323 # FIXME: we don't handle already existing
1324 # non-directory files in the paths of both $source and
1327 if [ ! -d "${dest}" ]
1329 # if ${dest} is in /home/$user, try fixing
1331 # FIXME: this should really be handled by
1332 # live-config since we don't know for sure
1333 # which uid a certain user has until then
1334 if echo ${dest} | grep -qe "^${rootmnt}/*home/\+[^/]\+"
1337 for dir in $(echo ${dest} | sed -e 's|/\+| |g')
1343 # assume that the intended user is the first, which is usually the case
1344 chown 1000:1000 ${path}
1352 # if ${source} doesn't exist on our persistent media
1353 # we bootstrap it with $dest from the live filesystem.
1354 # this both makes sense and is critical if we're
1355 # dealing with /etc or other system dir.
1356 if [ ! -d "${source}" ]
1358 if [ -n "${PERSISTENT_READONLY}" ] || [ -n "${opt_linkfiles}" ]
1361 elif [ -n "${opt_union}" ]
1363 # union's don't need to be bootstrapped
1366 # ensure that $dest is not copied *into* $source
1367 mkdir -p "$(dirname ${source})"
1368 cp -a "${dest}" "${source}"
1372 rofs_dest_backing=""
1373 for d in ${rootmnt}/live/rofs/*
1375 if [ -n "${rootmnt}" ]
1377 rofs_dest_backing="${d}/$(echo ${dest} | sed -e "s|${rootmnt}||")"
1379 rofs_dest_backing="${d}/${dest}"
1382 if [ -d "${rofs_dest_backing}" ]
1386 rofs_dest_backing=""
1390 if [ -z "${PERSISTENT_READONLY}" ]
1392 if [ -n "${opt_linkfiles}" ]
1394 links_source="${source}"
1395 links_dest="${dest}"
1396 elif [ -n "${opt_union}" ]
1398 do_union ${dest} ${source} ${rofs_dest_backing}
1400 mount --bind "${source}" "${dest}"
1403 if [ -n "${opt_linkfiles}" ]
1405 links_dest="${dest}"
1406 dest="$(mktemp -d ${persistent_backing}/links_source-XXXXXX)"
1407 links_source="${dest}"
1409 if [ -n "${rootmnt}" ]
1411 cow_dir="$(echo ${dest} | sed -e "s|${rootmnt}|/cow/|")"
1413 cow_dir="/live/cow/${dest}"
1416 do_union ${dest} ${cow_dir} ${source} ${rofs_dest_backing}
1419 if [ -n "${opt_linkfiles}" ]
1421 link_files "${links_source}" "${links_dest}" "${rootmnt}"
1424 PERSISTENCE_IS_ON="1"
1425 export PERSISTENCE_IS_ON
1426 done < ${custom_mounts}
1429 fix_home_rw_compatibility ()
1433 if [ -n "${PERSISTENT_READONLY}" ]
1438 local backing="$(where_is_mounted ${device})"
1439 if [ -z "${backing}" ]
1441 backing="${rootmnt}/live/persistent/$(basename ${device})"
1442 mkdir -p "${backing}"
1443 local device_fstype="$(get_fstype ${device})"
1444 local device_mount_opts="rw,noatime"
1445 if ! mount -t "${device_fstype}" -o "${device_mount_opts}" "${device}" "${backing}" >/dev/null 2>&1
1450 local include_list="${backing}/${persistence_list}"
1451 if [ ! -r "${include_list}" ]
1453 echo "# home-rw backwards compatibility:
1454 . /home" > "${include_list}"