7 DIRECTORY="${1}/${LIVE_MEDIA_PATH}"
8 for FILESYSTEM in squashfs ext2 ext3 ext4 xfs dir jffs
10 if ls "${DIRECTORY}/"*.${FILESYSTEM} > /dev/null 2>&1
20 if [ "${IGNORE_UUID}" ] || [ ! -e /conf/uuid.conf ]
26 uuid="$(cat /conf/uuid.conf)"
28 for try_uuid_file in "${path}/.disk/live-uuid"*
30 [ -e "${try_uuid_file}" ] || continue
32 try_uuid="$(cat "${try_uuid_file}")"
34 if [ "${uuid}" = "${try_uuid}" ]
46 *.squashfs|*.ext2|*.ext3|*.ext4|*.jffs2)
47 echo $(setup_loop "${1}" "loop" "/sys/block/loop*" '0' "${LIVE_MEDIA_ENCRYPTION}" "${2}")
55 panic "Unrecognized live filesystem: ${1}"
60 mount_images_in_directory ()
66 if is_live_path "${directory}"
68 [ -n "${mac}" ] && adddirectory="${directory}/${LIVE_MEDIA_PATH}/${mac}"
69 setup_unionfs "${directory}/${LIVE_MEDIA_PATH}" "${rootmnt}" "${adddirectory}"
71 panic "No supported filesystem images found at /${LIVE_MEDIA_PATH}."
77 sysfs_path="${1#/sys}"
79 if udevadm info --query=all --path="${sysfs_path}" | egrep -q "DEVTYPE=disk"
82 elif echo "${sysfs_path}" | grep -q '^/block/vd[a-z]$'
85 elif echo ${sysfs_path} | grep -q "^/block/dm-"
88 elif echo ${sysfs_path} | grep -q "^/block/mtdblock"
100 skip_uuid_check="${3}"
102 # support for fromiso=.../isofrom=....
105 ISO_DEVICE=$(dirname $FROMISO)
106 if ! [ -b $ISO_DEVICE ]
108 # to support unusual device names like /dev/cciss/c0d0p1
109 # as well we have to identify the block device name, let's
110 # do that for up to 15 levels
112 while [ -n "$ISO_DEVICE" ] && [ "$i" -gt 0 ]
114 ISO_DEVICE=$(dirname ${ISO_DEVICE})
115 [ -b "$ISO_DEVICE" ] && break
120 if [ "$ISO_DEVICE" = "/" ]
122 echo "Warning: device for bootoption fromiso= ($FROMISO) not found.">>/boot.log
124 fs_type=$(get_fstype "${ISO_DEVICE}")
125 if is_supported_fs ${fs_type}
128 mount -t $fs_type "$ISO_DEVICE" /live/fromiso
129 ISO_NAME="$(echo $FROMISO | sed "s|$ISO_DEVICE||")"
130 loopdevname=$(setup_loop "/live/fromiso/${ISO_NAME}" "loop" "/sys/block/loop*" "" '')
131 devname="${loopdevname}"
133 echo "Warning: unable to mount $ISO_DEVICE." >>/boot.log
138 if [ -z "${devname}" ]
140 devname=$(sys2dev "${sysdev}")
143 if [ -d "${devname}" ]
145 mount -o bind "${devname}" $mountpoint || continue
147 if is_live_path $mountpoint
157 for device in ${devname}
162 if [ -x /scripts/local-top/lvm2 ]
164 ROOT="$device" resume="" /scripts/local-top/lvm2 >>/boot.log
169 # Adding raid support
170 if [ -x /scripts/local-top/mdadm ]
172 [ -r /conf/conf.d/md ] && cp /conf/conf.d/md /conf/conf.d/md.orig
173 echo "MD_DEVS=$device " >> /conf/conf.d/md
174 /scripts/local-top/mdadm >>/boot.log
175 [ -r /conf/conf.d/md.orig ] && mv /conf/conf.d/md.orig /conf/conf.d/md
182 [ -n "$device" ] && devname="$device"
184 [ -e "$devname" ] || continue
186 if [ -n "${LIVE_MEDIA_OFFSET}" ]
188 loopdevname=$(setup_loop "${devname}" "loop" "/sys/block/loop*" "${LIVE_MEDIA_OFFSET}" '')
189 devname="${loopdevname}"
192 fstype=$(get_fstype "${devname}")
194 if is_supported_fs ${fstype}
196 devuid=$(blkid -o value -s UUID "$devname")
197 [ -n "$devuid" ] && grep -qs "\<$devuid\>" /var/lib/live/boot/devices-already-tried-to-mount && continue
198 mount -t ${fstype} -o ro,noatime "${devname}" ${mountpoint} || continue
199 [ -n "$devuid" ] && echo "$devuid" >> /var/lib/live/boot/devices-already-tried-to-mount
201 if [ -n "${FINDISO}" ]
203 if [ -f ${mountpoint}/${FINDISO} ]
206 mkdir -p /live/findiso
207 mount -t ${fstype} -o ro,noatime "${devname}" /live/findiso
208 loopdevname=$(setup_loop "/live/findiso/${FINDISO}" "loop" "/sys/block/loop*" 0 "")
209 devname="${loopdevname}"
210 mount -t iso9660 -o ro,noatime "${devname}" ${mountpoint}
216 if is_live_path ${mountpoint} && \
217 ([ "${skip_uuid_check}" ] || matches_uuid ${mountpoint})
222 umount ${mountpoint} 2>/dev/null
226 if [ -n "${LIVE_MEDIA_OFFSET}" ]
228 losetup -d "${loopdevname}"
238 # don't start autodetection before timeout has expired
239 if [ -n "${LIVE_MEDIA_TIMEOUT}" ]
241 if [ "${timeout}" -lt "${LIVE_MEDIA_TIMEOUT}" ]
247 # first look at the one specified in the command line
248 case "${LIVE_MEDIA}" in
250 for sysblock in $(removable_usb_dev "sys")
252 for dev in $(subdevices "${sysblock}")
254 if check_dev "${dev}"
264 for sysblock in $(removable_dev "sys")
266 for dev in $(subdevices "${sysblock}")
268 if check_dev "${dev}"
278 if [ ! -z "${LIVE_MEDIA}" ]
280 if check_dev "null" "${LIVE_MEDIA}" "skip_uuid_check"
288 # or do the scan of block devices
289 # prefer removable devices over non-removable devices, so scan them first
290 devices_to_scan="$(removable_dev 'sys') $(non_removable_dev 'sys')"
292 for sysblock in $devices_to_scan
294 devname=$(sys2dev "${sysblock}")
295 [ -e "$devname" ] || continue
296 fstype=$(get_fstype "${devname}")
298 if /lib/udev/cdrom_id ${devname} > /dev/null
300 if check_dev "null" "${devname}"
304 elif is_nice_device "${sysblock}"
306 for dev in $(subdevices "${sysblock}")
308 if check_dev "${dev}"
313 elif [ "${fstype}" = "squashfs" -o \
314 "${fstype}" = "btrfs" -o \
315 "${fstype}" = "ext2" -o \
316 "${fstype}" = "ext3" -o \
317 "${fstype}" = "ext4" -o \
318 "${fstype}" = "jffs2" ]
320 # This is an ugly hack situation, the block device has
321 # an image directly on it. It's hopefully
322 # live-boot, so take it and run with it.
323 ln -s "${devname}" "${devname}.${fstype}"
324 echo "${devname}.${fstype}"
332 is_in_list_separator_helper ()
334 local sep element list
340 echo ${list} | grep -qe "^\(.*${sep}\)\?${element}\(${sep}.*\)\?$"
343 is_in_space_sep_list ()
348 is_in_list_separator_helper "[[:space:]]" "${element}" "${*}"
351 is_in_comma_sep_list ()
356 is_in_list_separator_helper "," "${element}" "${*}"
362 echo "/dev/$(udevadm info -q name -p ${sysdev} 2>/dev/null|| echo ${sysdev##*/})"
370 for dev in "${sysblock}"/* "${sysblock}"
372 if [ -e "${dev}/dev" ]
383 black_listed_devices="${1}"
384 white_listed_devices="${2}"
386 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "loop|ram|fd")
388 fulldevname=$(sys2dev "${sysblock}")
390 if is_in_space_sep_list ${fulldevname} ${black_listed_devices} || \
391 [ -n "${white_listed_devices}" ] && \
392 ! is_in_space_sep_list ${fulldevname} ${white_listed_devices}
394 # skip this device entirely
398 for dev in $(subdevices "${sysblock}")
400 devname=$(sys2dev "${dev}")
402 if is_in_space_sep_list ${devname} ${black_listed_devices}
404 # skip this subdevice
417 # Validate input first
418 if [ -z "${fstype}" ]
423 # get_fstype might report "unknown" or "swap", ignore it as no such kernel module exists
424 if [ "${fstype}" = "unknown" ] || [ "${fstype}" = "swap" ]
429 # Try to look if it is already supported by the kernel
430 if grep -q ${fstype} /proc/filesystems
434 # Then try to add support for it the gentle way using the initramfs capabilities
435 modprobe -q -b ${fstype}
436 if grep -q ${fstype} /proc/filesystems
439 # Then try the hard way if /root is already reachable
441 kmodule="/root/lib/modules/`uname -r`/${fstype}/${fstype}.ko"
442 if [ -e "${kmodule}" ]
445 if grep -q ${fstype} /proc/filesystems
458 blkid -s TYPE -o value $1 2>/dev/null
465 grep -m1 "^${device} " /proc/mounts | cut -f2 -d ' '
470 # remove all unnecessary /:s in the path, including last one (except
471 # if path is just "/")
472 echo ${1} | sed 's|//\+|/|g' | sed 's|^\(.*[^/]\)/$|\1|'
475 what_is_mounted_on ()
478 dir="$(trim_path ${1})"
479 grep -m1 "^[^ ]\+ ${dir} " /proc/mounts | cut -d' ' -f1
484 local reference targets owner
488 owner=$(stat -c %u:%g "${reference}")
489 chown -h ${owner} ${targets}
494 local reference targets rights
498 rights=$(stat -c %a "${reference}")
499 chmod ${rights} ${targets}
515 mounts="$(awk '{print $2}' /proc/mounts)"
516 testpath="$(realpath ${testpath})"
520 if echo "${mounts}" | grep -qs "^${testpath}"
522 set -- $(echo "${mounts}" | grep "^${testpath}" | lastline)
526 testpath=$(dirname $testpath)
533 # Returns used/free fs kbytes + 5% more
534 # You could pass a block device as ${1} or the mount point as ${2}
540 if [ -z "${mountp}" ]
542 mountp="$(where_is_mounted ${dev})"
544 if [ -z "${mountp}" ]
546 mountp="/mnt/tmp_fs_size"
549 mount -t $(get_fstype "${dev}") -o ro "${dev}" "${mountp}" || log_warning_msg "cannot mount -t $(get_fstype ${dev}) -o ro ${dev} ${mountp}"
555 if [ "${used}" = "used" ]
557 size=$(du -ks ${mountp} | cut -f1)
558 size=$(expr ${size} + ${size} / 20 ) # FIXME: 5% more to be sure
561 size="$(df -kP | grep -s ${mountp} | awk '{print $4}')"
564 if [ -n "${doumount}" ]
566 umount "${mountp}" || log_warning_msg "cannot umount ${mountp}"
576 if [ -x /bin/loadkeys -a -r /etc/boottime.kmap.gz ]
578 loadkeys --quiet /etc/boottime.kmap.gz
584 local fspath module pattern offset encryption readonly
592 # the output of setup_loop is evaluated in other functions,
593 # modprobe leaks kernel options like "libata.dma=0"
594 # as "options libata dma=0" on stdout, causing serious
595 # problems therefor, so instead always avoid output to stdout
596 modprobe -q -b "${module}" 1>/dev/null
600 for loopdev in ${pattern}
602 if [ "$(cat ${loopdev}/size)" -eq 0 ]
604 dev=$(sys2dev "${loopdev}")
607 if [ -n "${readonly}" ]
609 if losetup --help 2>&1 | grep -q -- "-r\b"
611 options="${options} -r"
615 if [ -n "${offset}" ] && [ 0 -lt "${offset}" ]
617 options="${options} -o ${offset}"
620 if [ -z "${encryption}" ]
622 losetup ${options} "${dev}" "${fspath}"
624 # Loop AES encryption
629 echo -n "Enter passphrase for root filesystem: " >&6
631 echo "${passphrase}" > /tmp/passphrase
633 exec 9</tmp/passphrase
634 losetup ${options} -e "${encryption}" -p 9 "${dev}" "${fspath}"
637 rm -f /tmp/passphrase
639 if [ 0 -eq ${error} ]
646 echo -n "There was an error decrypting the root filesystem ... Retry? [Y/n] " >&6
649 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
662 panic "No loop devices available"
672 old_mountp="$(where_is_mounted ${dev})"
674 if [ -n "${old_mountp}" ]
676 if [ "${opts}" != "ro" ]
678 mount -o remount,"${opts}" "${dev}" "${old_mountp}" || panic "Remounting ${dev} ${opts} on ${old_mountp} failed"
681 mount -o bind "${old_mountp}" "${mountp}" || panic "Cannot bind-mount ${old_mountp} on ${mountp}"
683 if [ -z "${fstype}" ]
685 fstype=$(get_fstype "${dev}")
687 mount -t "${fstype}" -o "${opts}" "${dev}" "${mountp}" || \
688 ( echo "SKIPPING: Cannot mount ${dev} on ${mountp}, fstype=${fstype}, options=${opts}" > boot.log && return 0 )
692 # Try to mount $device to the place expected by live-boot. If $device
693 # is already mounted somewhere, move it to the expected place. If $device
694 # ends with a "/" this is a directory path.
695 # If we're only probing $device (to check if it has custom persistence)
696 # $probe should be set, which suppresses warnings upon failure. On
697 # success, print the mount point for $device.
698 mount_persistence_media ()
700 local device probe backing old_backing fstype mount_opts
704 # get_custom_mounts() might call this with a directory path instead
705 # of a block device path. This means we have found sub-directory path
706 # underneath /lib/live/mounts/persistence, so we're done
707 if [ -d "${device}" ]
713 if [ ! -b "${device}" ]
718 backing="/live/persistence/$(basename ${device})"
720 mkdir -p "${backing}"
721 old_backing="$(where_is_mounted ${device})"
722 if [ -z "${old_backing}" ]
724 fstype="$(get_fstype ${device})"
725 mount_opts="rw,noatime"
726 if [ -n "${PERSISTENCE_READONLY}" ]
728 mount_opts="ro,noatime"
730 if mount -t "${fstype}" -o "${mount_opts}" "${device}" "${backing}" >/dev/null
735 [ -z "${probe}" ] && log_warning_msg "Failed to mount persistence media ${device}"
739 elif [ "${backing}" != "${old_backing}" ]
741 if ! mount -o move ${old_backing} ${backing} >/dev/null
743 [ -z "${probe}" ] && log_warning_msg "Failed to move persistence media ${device}"
747 mount_opts="rw,noatime"
748 if [ -n "${PERSISTENCE_READONLY}" ]
750 mount_opts="ro,noatime"
752 if ! mount -o "remount,${mount_opts}" "${backing}" >/dev/null
754 log_warning_msg "Failed to remount persistence media ${device} writable"
755 # Don't unmount or rmdir the new mountpoint in this case
760 # This means that $device has already been mounted on
761 # the place expected by live-boot, so we're done.
767 close_persistence_media ()
771 backing="$(where_is_mounted ${device})"
773 if [ -d "${backing}" ]
775 umount "${backing}" >/dev/null 2>&1
776 rmdir "${backing}" >/dev/null 2>&1
779 if is_active_luks_mapping ${device}
781 cryptsetup luksClose ${device}
788 name="$(basename ${dev})"
790 if [ -n "${PERSISTENCE_READONLY}" ]
792 opts="${opts} --readonly"
795 if cryptsetup status "${name}" >/dev/null 2>&1
797 re="^[[:space:]]*device:[[:space:]]*\([^[:space:]]*\)$"
798 opened_dev=$(cryptsetup status ${name} 2>/dev/null | grep "${re}" | sed "s|${re}|\1|")
799 if [ "${opened_dev}" = "${dev}" ]
801 luks_device="/dev/mapper/${name}"
805 log_warning_msg "Cannot open luks device ${dev} since ${opened_dev} already is opened with its name"
813 if [ -x /bin/plymouth ]
818 case "${_PLYMOUTH}" in
822 cryptkeyscript="plymouth ask-for-password --prompt"
823 # Plymouth will add a : if it is a non-graphical prompt
824 cryptkeyprompt="Please unlock disk ${dev}"
828 cryptkeyscript="/lib/cryptsetup/askpass"
829 cryptkeyprompt="Please unlock disk ${dev}: "
835 $cryptkeyscript "$cryptkeyprompt" | \
836 cryptsetup -T 1 luksOpen ${dev} ${name} ${opts}
840 luks_device="/dev/mapper/${name}"
846 retryprompt="There was an error decrypting ${dev} ... Retry? [Y/n]"
848 case "${_PLYMOUTH}" in
850 plymouth display-message --text "${retryprompt}"
851 answer=$(plymouth watch-keystroke --keys="YNyn")
855 echo -n "${retryprompt} " >&6
860 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
862 case "${_PLYMOUTH}" in
864 plymouth display-message --text ""
877 blkid -s PART_ENTRY_NAME -p -o value ${dev} 2>/dev/null
884 [ "$(blkid -s PART_ENTRY_SCHEME -p -o value ${dev} 2>/dev/null)" = "gpt" ]
887 probe_for_gpt_name ()
889 local overlays dev gpt_dev gpt_name
894 if is_active_luks_mapping ${dev}
896 # if $dev is an opened luks device, we need to check
897 # GPT stuff on the backing device
898 gpt_dev=$(get_luks_backing_device "${dev}")
901 if ! is_gpt_device ${gpt_dev}
906 gpt_name=$(get_gpt_name ${gpt_dev})
907 for label in ${overlays}
909 if [ "${gpt_name}" = "${label}" ]
911 echo "${label}=${dev}"
916 probe_for_fs_label ()
922 for label in ${overlays}
924 if [ "$(blkid -s LABEL -o value $dev 2>/dev/null)" = "${label}" ]
926 echo "${label}=${dev}"
931 probe_for_file_name ()
933 local overlays dev ret backing
938 backing="$(mount_persistence_media ${dev} probe)"
939 if [ -z "${backing}" ]
944 for label in ${overlays}
946 path=${backing}/${PERSISTENCE_PATH}/${label}
950 loopdev=$(setup_loop "${path}" "loop" "/sys/block/loop*")
951 ret="${ret} ${label}=${loopdev}"
959 # unmount and remove mountpoint
960 umount ${backing} > /dev/null 2>&1 || true
961 rmdir ${backing} > /dev/null 2>&1 || true
965 probe_for_directory_name ()
967 local overlays dev ret backing
972 backing="$(mount_persistence_media ${dev} probe)"
973 if [ -z "${backing}" ]
978 for label in ${overlays}
980 path=${backing}/${PERSISTENCE_PATH}/${label}
983 # in this case the "device" ends with a "/"
984 ret="${ret} ${label}=${backing}/${PERSISTENCE_PATH}/${label%%/}/"
992 # unmount and remove mountpoint
993 umount ${backing} > /dev/null 2>&1 || true
994 rmdir ${backing} > /dev/null 2>&1 || true
998 find_persistence_media ()
1000 # Scans devices for overlays, and returns a whitespace
1001 # separated list of how to use them. Only overlays with a partition
1002 # label or file name in ${overlays} are returned.
1004 # When scanning a LUKS device, the user will be asked to enter the
1005 # passphrase; on failure to enter it, or if no persistence partitions
1006 # or files were found, the LUKS device is closed.
1008 # For all other cases (overlay partition and overlay file) the
1009 # return value is "${label}=${device}", where ${device} a device that
1010 # can mount the content. In the case of an overlay file, the device
1011 # containing the file will remain mounted as a side-effect.
1013 # No devices in ${black_listed_devices} will be scanned, and if
1014 # ${white_list_devices} is non-empty, only devices in it will be
1017 local overlays white_listed_devices ret black_listed_devices
1019 white_listed_devices="${2}"
1023 # The devices that are hosting the actual live rootfs should not be
1024 # used for persistence storage since otherwise you might mount a
1025 # parent directory on top of a sub-directory of the same filesystem
1026 # in one union together.
1028 black_listed_devices=""
1029 for d in /live/rootfs/* /live/findiso /live/fromiso
1031 black_listed_devices="${black_listed_devices} $(what_is_mounted_on d)"
1034 for dev in $(storage_devices "${black_listed_devices}" "${white_listed_devices}")
1036 local result luks_device
1040 # Check if it's a luks device; we'll have to open the device
1041 # in order to probe any filesystem it contains, like we do
1042 # below. activate_custom_mounts() also depends on that any luks
1043 # device already has been opened.
1044 if is_in_comma_sep_list luks ${PERSISTENCE_ENCRYPTION} && is_luks_partition ${dev}
1046 if luks_device=$(open_luks_device "${dev}")
1048 dev="${luks_device}"
1050 # skip $dev since we failed/chose not to open it
1053 elif ! is_in_comma_sep_list none ${PERSISTENCE_ENCRYPTION}
1055 # skip $dev since we don't allow unencrypted storage
1059 # Probe for matching GPT partition names or filesystem labels
1060 if is_in_comma_sep_list filesystem ${PERSISTENCE_STORAGE}
1062 result=$(probe_for_gpt_name "${overlays}" ${dev})
1063 if [ -n "${result}" ]
1065 ret="${ret} ${result}"
1069 result=$(probe_for_fs_label "${overlays}" ${dev})
1070 if [ -n "${result}" ]
1072 ret="${ret} ${result}"
1077 # Probe for files with matching name on mounted partition
1078 if is_in_comma_sep_list file ${PERSISTENCE_STORAGE}
1080 result=$(probe_for_file_name "${overlays}" ${dev})
1081 if [ -n "${result}" ]
1084 loopdevice=${result##*=}
1085 if is_in_comma_sep_list luks ${PERSISTENCE_ENCRYPTION} && is_luks_partition ${loopdevice}
1089 if luksfile=$(open_luks_device "${loopdevice}")
1091 result=${result%%=*}
1092 result="${result}=${luksfile}"
1094 losetup -d $loopdevice
1098 ret="${ret} ${result}"
1103 # Probe for directory with matching name on mounted partition
1104 if is_in_comma_sep_list directory ${PERSISTENCE_STORAGE}
1106 result=$(probe_for_directory_name "${overlays}" ${dev})
1107 if [ -n "${result}" ]
1109 ret="${ret} ${result}"
1114 # Close luks device if it isn't used
1115 if [ -z "${result}" ] && [ -n "${luks_device}" ] && is_active_luks_mapping "${luks_device}"
1117 cryptsetup luksClose "${luks_device}"
1131 for adaptor in /sys/class/net/*
1133 status="$(cat ${adaptor}/iflink)"
1135 if [ "${status}" -eq 2 ]
1137 mac="$(cat ${adaptor}/address)"
1138 mac="$(echo ${mac} | sed 's/:/-/g' | tr '[a-z]' '[A-Z]')"
1145 is_luks_partition ()
1148 cryptsetup isLuks "${device}" 1>/dev/null 2>&1
1151 is_active_luks_mapping ()
1154 cryptsetup status "${device}" 1>/dev/null 2>&1
1157 get_luks_backing_device ()
1160 cryptsetup status ${device} 2> /dev/null | \
1161 awk '{if ($1 == "device:") print $2}'
1166 output_format="${1}"
1170 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1173 if [ "$(cat ${sysblock}/removable)" = "1" ]
1175 if [ -z "${want_usb}" ]
1179 if readlink ${sysblock} | grep -q usb
1186 if [ "${dev_ok}" = "true" ]
1188 case "${output_format}" in
1190 ret="${ret} ${sysblock}"
1193 devname=$(sys2dev "${sysblock}")
1194 ret="${ret} ${devname}"
1203 removable_usb_dev ()
1205 output_format="${1}"
1207 removable_dev "${output_format}" "want_usb"
1210 non_removable_dev ()
1212 output_format="${1}"
1215 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1217 if [ "$(cat ${sysblock}/removable)" = "0" ]
1219 case "${output_format}" in
1221 ret="${ret} ${sysblock}"
1224 devname=$(sys2dev "${sysblock}")
1225 ret="${ret} ${devname}"
1236 # create source's directory structure in dest, and recursively
1237 # create symlinks in dest to to all files in source. if mask
1238 # is non-empty, remove mask from all source paths when
1239 # creating links (will be necessary if we change root, which
1240 # live-boot normally does (into $rootmnt)).
1241 local src_dir dest_dir src_transform
1243 # remove multiple /:s and ensure ending on /
1244 src_dir="$(trim_path ${1})/"
1245 dest_dir="$(trim_path ${2})/"
1246 src_transform="${3}"
1248 # This check can only trigger on the inital, non-recursive call since
1249 # we create the destination before recursive calls
1250 if [ ! -d "${dest_dir}" ]
1252 log_warning_msg "Must link_files into a directory"
1256 find "${src_dir}" -mindepth 1 -maxdepth 1 | \
1259 local dest final_src
1260 dest="${dest_dir}$(basename "${src}")"
1263 if [ -z "$(ls -A "${src}")" ]
1267 if [ ! -d "${dest}" ]
1270 chown_ref "${src}" "${dest}"
1271 chmod_ref "${src}" "${dest}"
1273 link_files "${src}" "${dest}" "${src_transform}"
1276 if [ -n "${src_transform}" ]
1278 final_src="$(echo ${final_src} | sed "${src_transform}")"
1280 rm -rf "${dest}" 2> /dev/null
1281 ln -s "${final_src}" "${dest}"
1282 chown_ref "${src}" "${dest}"
1289 local unionmountpoint unionrw unionro
1290 unionmountpoint="${1}" # directory where the union is mounted
1292 unionrw="${1}" # branch where the union changes are stored
1294 unionro="${*}" # space separated list of read-only branches (optional)
1296 case "${UNIONTYPE}" in
1302 unionmountopts="-o noatime,${noxino_opt},dirs=${unionrw}=${rw_opt}"
1303 if [ -n "${unionro}" ]
1305 for rofs in ${unionro}
1307 unionmountopts="${unionmountopts}:${rofs}=${ro_opt}"
1313 # XXX: can unionro be optional? i.e. can overlay skip lowerdir?
1314 if [ -z "${unionro}" ]
1316 panic "overlay needs at least one lower filesystem (read-only branch)."
1318 # Multiple lower layers can now be given using the the colon (":") as a
1319 # separator character between the directory names.
1320 unionro="$(echo ${unionro} | sed -e 's| |:|g')"
1321 # overlayfs requires:
1322 # + a workdir to become mounted
1323 # + workdir and upperdir to reside under the same mount
1324 # + workdir and upperdir to be in separate directories
1325 mkdir "${unionrw}/rw"
1326 mkdir "${unionrw}/work"
1327 unionmountopts="-o noatime,lowerdir=${unionro},upperdir=${unionrw}/rw,workdir=${unionrw}/work"
1331 mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
1334 get_custom_mounts ()
1336 # Side-effect: leaves $devices with persistence.conf mounted in /live/persistence
1337 # Side-effect: prints info to file $custom_mounts
1339 local custom_mounts devices bindings links
1344 bindings="/tmp/bindings.list"
1345 links="/tmp/links.list"
1346 rm -rf ${bindings} ${links} 2> /dev/null
1348 for device in ${devices}
1350 local device_name backing include_list
1351 device_name="$(basename ${device})"
1352 backing=$(mount_persistence_media ${device})
1353 if [ -z "${backing}" ]
1358 if [ -r "${backing}/${persistence_list}" ]
1360 include_list="${backing}/${persistence_list}"
1365 if [ -n "${LIVE_BOOT_DEBUG}" ] && [ -e "${include_list}" ]
1367 cp ${include_list} /live/persistence/${persistence_list}.${device_name}
1370 while read dir options # < ${include_list}
1372 if echo ${dir} | grep -qe "^[[:space:]]*\(#.*\)\?$"
1374 # skipping empty or commented lines
1378 if trim_path ${dir} | grep -q -e "^[^/]" -e "^/lib" -e "^/lib/live\(/.*\)\?$" -e "^/\(.*/\)\?\.\.\?\(/.*\)\?$"
1380 log_warning_msg "Skipping unsafe custom mount ${dir}: must be an absolute path containing neither the \".\" nor \"..\" special dirs, and cannot be \"/lib\", or \"/lib/live\" or any of its sub-directories."
1384 local opt_source opt_link source full_source full_dest
1387 for opt in $(echo ${options} | tr ',' ' ');
1391 opt_source=${opt#source=}
1399 log_warning_msg "Skipping custom mount with unknown option: ${opt}"
1406 if [ -n "${opt_source}" ]
1408 if echo ${opt_source} | grep -q -e "^/" -e "^\(.*/\)\?\.\.\?\(/.*\)\?$" && [ "${opt_source}" != "." ]
1410 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"
1413 source="${opt_source}"
1417 full_source="$(trim_path ${backing}/${source})"
1418 full_dest="$(trim_path ${rootmnt}/${dir})"
1419 if [ -n "${opt_link}" ]
1421 echo "${device} ${full_source} ${full_dest} ${options}" >> ${links}
1423 echo "${device} ${full_source} ${full_dest} ${options}" >> ${bindings}
1425 done < ${include_list}
1428 # We sort the list according to destination so we're sure that
1429 # we won't hide a previous mount. We also ignore duplicate
1430 # destinations in a more or less arbitrary way.
1431 [ -e "${bindings}" ] && sort -k3 -sbu ${bindings} >> ${custom_mounts} && rm ${bindings}
1433 # After all mounts are considered we add symlinks so they
1434 # won't be hidden by some mount.
1435 [ -e "${links}" ] && cat ${links} >> ${custom_mounts} && rm ${links}
1437 # We need to make sure that no two custom mounts have the same sources
1438 # or are nested; if that is the case, too much weird stuff can happen.
1439 local prev_source prev_dest
1440 prev_source="impossible source" # first iteration must not match
1442 # This sort will ensure that a source /a comes right before a source
1443 # /a/b so we only need to look at the previous source
1444 [ -e ${custom_mounts} ] && sort -k2 -b ${custom_mounts} |
1445 while read device source dest options
1447 if echo ${source} | grep -qe "^${prev_source}\(/.*\)\?$"
1449 panic "Two persistence mounts have the same or nested sources: ${source} on ${dest}, and ${prev_source} on ${prev_dest}"
1451 prev_source=${source}
1456 activate_custom_mounts ()
1458 local custom_mounts used_devices
1459 custom_mounts="${1}" # the ouput from get_custom_mounts()
1462 while read device source dest options # < ${custom_mounts}
1464 local opt_bind opt_link opt_union
1468 for opt in $(echo ${options} | tr ',' ' ');
1473 unset opt_link opt_union
1477 unset opt_bind opt_union
1481 unset opt_bind opt_link
1486 if [ -n "$(what_is_mounted_on "${dest}")" ]
1488 if [ "${dest}" = "${rootmnt}" ]
1492 log_warning_msg "Skipping custom mount ${dest}: $(what_is_mounted_on "${dest}") is already mounted there"
1497 if [ ! -d "${dest}" ]
1499 # create the destination and delete existing files in
1500 # its path that are in the way
1502 for dir in $(echo ${dest} | sed -e 's|/\+| |g')
1504 path=$(trim_path ${path}/${dir})
1512 if echo ${path} | grep -qe "^${rootmnt}/*home/[^/]\+"
1514 # if ${dest} is in /home try fixing proper ownership by assuming that the intended user is the first, which is usually the case
1515 # FIXME: this should really be handled by live-config since we don't know for sure which uid a certain user has until then
1516 chown 1000:1000 ${path}
1522 # if ${source} doesn't exist on our persistence media
1523 # we bootstrap it with $dest from the live filesystem.
1524 # this both makes sense and is critical if we're
1525 # dealing with /etc or other system dir.
1526 if [ ! -d "${source}" ]
1528 if [ -n "${PERSISTENCE_READONLY}" ]
1531 elif [ -n "${opt_union}" ] || [ -n "${opt_link}" ]
1533 # unions and don't need to be bootstrapped
1534 # link dirs can't be bootstrapped in a sensible way
1535 mkdir -p "${source}"
1536 chown_ref "${dest}" "${source}"
1537 chmod_ref "${dest}" "${source}"
1538 elif [ -n "${opt_bind}" ]
1540 # ensure that $dest is not copied *into* $source
1541 mkdir -p "$(dirname ${source})"
1542 cp -a "${dest}" "${source}"
1546 # XXX: If CONFIG_AUFS_ROBR is added to the Debian kernel we can
1547 # ignore the loop below and set rootfs_dest_backing=$dest
1548 local rootfs_dest_backing
1549 rootfs_dest_backing=""
1550 if [ -n "${opt_link}" ] || [ -n "${opt_union}" ]
1552 for d in /live/rootfs/*
1554 if [ -n "${rootmnt}" ]
1556 fs="${d}/$(echo ${dest} | sed -e "s|${rootmnt}||")"
1562 rootfs_dest_backing="${rootfs_dest_backing} ${fs}"
1567 local cow_dir links_source
1568 if [ -n "${opt_link}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1570 link_files ${source} ${dest} "s|^/live/|/lib/live/mount/|"
1571 elif [ -n "${opt_link}" ] && [ -n "${PERSISTENCE_READONLY}" ]
1573 mkdir -p ${rootmnt}/lib/live/mount/persistence
1574 links_source=$(mktemp -d ${rootmnt}/lib/live/mount/persistence/links-source-XXXXXX)
1575 chown_ref ${source} ${links_source}
1576 chmod_ref ${source} ${links_source}
1577 # We put the cow dir in the below strange place to
1578 # make it absolutely certain that the link source
1579 # has its own directory and isn't nested with some
1580 # other custom mount (if so that mount's files would
1581 # be linked, causing breakage.
1582 cow_dir="/live/overlay/lib/live/mount/persistence/$(basename ${links_source})"
1584 chown_ref "${source}" "${cow_dir}"
1585 chmod_ref "${source}" "${cow_dir}"
1586 do_union ${links_source} ${cow_dir} ${source} ${rootfs_dest_backing}
1587 link_files ${links_source} ${dest} "s|^${rootmnt}||"
1588 elif [ -n "${opt_union}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1590 do_union ${dest} ${source} ${rootfs_dest_backing}
1591 elif [ -n "${opt_bind}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1593 mount -o bind "${source}" "${dest}"
1594 elif [ -n "${opt_bind}" -o -n "${opt_union}" ] && [ -n "${PERSISTENCE_READONLY}" ]
1596 # bind-mount and union mount are handled the same
1597 # in read-only mode, but note that rootfs_dest_backing
1598 # is non-empty (and necessary) only for unions
1599 cow_dir="/live/overlay/${dest}"
1600 if [ -e "${cow_dir}" ] && [ -z "${opt_link}" ]
1602 # If an earlier custom mount has files here
1603 # it will "block" the current mount's files
1604 # which is undesirable
1608 chown_ref "${source}" "${cow_dir}"
1609 chmod_ref "${source}" "${cow_dir}"
1610 do_union ${dest} ${cow_dir} ${source} ${rootfs_dest_backing}
1613 PERSISTENCE_IS_ON="1"
1614 export PERSISTENCE_IS_ON
1616 if echo ${used_devices} | grep -qve "^\(.* \)\?${device}\( .*\)\?$"
1618 used_devices="${used_devices} ${device}"
1620 done < ${custom_mounts}
1622 echo ${used_devices}
1629 [ $(stat -fc%d:%D "${directory}") != $(stat -fc%d:%D "${directory}/..") ]