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"
101 skip_uuid_check="${3}"
103 # support for fromiso=.../isofrom=....
106 ISO_DEVICE=$(dirname $FROMISO)
107 if ! [ -b $ISO_DEVICE ]
109 # to support unusual device names like /dev/cciss/c0d0p1
110 # as well we have to identify the block device name, let's
111 # do that for up to 15 levels
113 while [ -n "$ISO_DEVICE" ] && [ "$i" -gt 0 ]
115 ISO_DEVICE=$(dirname ${ISO_DEVICE})
116 [ -b "$ISO_DEVICE" ] && break
121 if [ "$ISO_DEVICE" = "/" ]
123 echo "Warning: device for bootoption fromiso= ($FROMISO) not found.">>/boot.log
125 fs_type=$(get_fstype "${ISO_DEVICE}")
126 if is_supported_fs ${fs_type}
129 mount -t $fs_type "$ISO_DEVICE" /live/fromiso
130 ISO_NAME="$(echo $FROMISO | sed "s|$ISO_DEVICE||")"
131 loopdevname=$(setup_loop "/live/fromiso/${ISO_NAME}" "loop" "/sys/block/loop*" "" '')
132 devname="${loopdevname}"
134 echo "Warning: unable to mount $ISO_DEVICE." >>/boot.log
139 if [ -z "${devname}" ]
141 devname=$(sys2dev "${sysdev}")
144 if [ -d "${devname}" ]
146 mount -o bind "${devname}" $mountpoint || continue
148 if is_live_path $mountpoint
158 for device in ${devname}
163 if [ -x /scripts/local-top/lvm2 ]
165 ROOT="$device" resume="" /scripts/local-top/lvm2 >>/boot.log
170 # Adding raid support
171 if [ -x /scripts/local-top/mdadm ]
173 [ -r /conf/conf.d/md ] && cp /conf/conf.d/md /conf/conf.d/md.orig
174 echo "MD_DEVS=$device " >> /conf/conf.d/md
175 /scripts/local-top/mdadm >>/boot.log
176 [ -r /conf/conf.d/md.orig ] && mv /conf/conf.d/md.orig /conf/conf.d/md
183 [ -n "$device" ] && devname="$device"
185 [ -e "$devname" ] || continue
187 if [ -n "${LIVE_MEDIA_OFFSET}" ]
189 loopdevname=$(setup_loop "${devname}" "loop" "/sys/block/loop*" "${LIVE_MEDIA_OFFSET}" '')
190 devname="${loopdevname}"
193 fstype=$(get_fstype "${devname}")
195 if is_supported_fs ${fstype}
197 devuid=$(blkid -o value -s UUID "$devname")
198 [ -n "$devuid" ] && grep -qs "\<$devuid\>" /var/lib/live/boot/devices-already-tried-to-mount && continue
200 for _PARAMETER in ${LIVE_BOOT_CMDLINE}
202 case "${_PARAMETER}" in
209 if [ "${PERSISTENCE_FSCK}" = "true" ] || [ "${PERSISTENCE_FSCK}" = "yes" ] || [ "${FORCEFSCK}" = "true" ]
212 if [ "$FORCEFSCK" = "true" ]
218 if [ "$FSCKFIX" = "true" ] || [ "$FSCKFIX" = "yes" ]
223 fsck $fix $force ${devname} >> fsck.log 2>&1
226 mount -t ${fstype} -o ro,noatime "${devname}" ${mountpoint} || continue
227 [ -n "$devuid" ] && echo "$devuid" >> /var/lib/live/boot/devices-already-tried-to-mount
229 if [ -n "${FINDISO}" ]
231 if [ -f ${mountpoint}/${FINDISO} ]
234 mkdir -p /run/live/findiso
235 mount -t ${fstype} -o ro,noatime "${devname}" /run/live/findiso
236 loopdevname=$(setup_loop "/run/live/findiso/${FINDISO}" "loop" "/sys/block/loop*" 0 "")
237 devname="${loopdevname}"
238 mount -t iso9660 -o ro,noatime "${devname}" ${mountpoint}
244 if is_live_path ${mountpoint} && \
245 ([ "${skip_uuid_check}" ] || matches_uuid ${mountpoint})
250 umount ${mountpoint} 2>/dev/null
254 if [ -n "${LIVE_MEDIA_OFFSET}" ]
256 losetup -d "${loopdevname}"
266 # don't start autodetection before timeout has expired
267 if [ -n "${LIVE_MEDIA_TIMEOUT}" ]
269 if [ "${timeout}" -lt "${LIVE_MEDIA_TIMEOUT}" ]
275 # first look at the one specified in the command line
276 case "${LIVE_MEDIA}" in
278 for sysblock in $(removable_usb_dev "sys")
280 for dev in $(subdevices "${sysblock}")
282 if check_dev "${dev}"
292 for sysblock in $(removable_dev "sys")
294 for dev in $(subdevices "${sysblock}")
296 if check_dev "${dev}"
306 if [ ! -z "${LIVE_MEDIA}" ]
308 if check_dev "null" "${LIVE_MEDIA}" "skip_uuid_check"
316 # or do the scan of block devices
317 # prefer removable devices over non-removable devices, so scan them first
318 devices_to_scan="$(removable_dev 'sys') $(non_removable_dev 'sys')"
320 for sysblock in $devices_to_scan
322 devname=$(sys2dev "${sysblock}")
323 [ -e "$devname" ] || continue
324 fstype=$(get_fstype "${devname}")
326 if /lib/udev/cdrom_id ${devname} > /dev/null
328 if check_dev "null" "${devname}"
332 elif is_nice_device "${sysblock}"
334 for dev in $(subdevices "${sysblock}")
336 if check_dev "${dev}"
341 elif [ "${fstype}" = "squashfs" -o \
342 "${fstype}" = "btrfs" -o \
343 "${fstype}" = "ext2" -o \
344 "${fstype}" = "ext3" -o \
345 "${fstype}" = "ext4" -o \
346 "${fstype}" = "jffs2" ]
348 # This is an ugly hack situation, the block device has
349 # an image directly on it. It's hopefully
350 # live-boot, so take it and run with it.
351 ln -s "${devname}" "${devname}.${fstype}"
352 echo "${devname}.${fstype}"
360 is_in_list_separator_helper ()
362 local sep element list
368 echo ${list} | grep -qe "^\(.*${sep}\)\?${element}\(${sep}.*\)\?$"
371 is_in_space_sep_list ()
376 is_in_list_separator_helper "[[:space:]]" "${element}" "${*}"
379 is_in_comma_sep_list ()
384 is_in_list_separator_helper "," "${element}" "${*}"
390 echo "/dev/$(udevadm info -q name -p ${sysdev} 2>/dev/null|| echo ${sysdev##*/})"
398 for dev in "${sysblock}"/* "${sysblock}"
400 if [ -e "${dev}/dev" ]
411 black_listed_devices="${1}"
412 white_listed_devices="${2}"
414 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "loop|ram|fd")
416 fulldevname=$(sys2dev "${sysblock}")
418 if is_in_space_sep_list ${fulldevname} ${black_listed_devices} || \
419 [ -n "${white_listed_devices}" ] && \
420 ! is_in_space_sep_list ${fulldevname} ${white_listed_devices}
422 # skip this device entirely
426 for dev in $(subdevices "${sysblock}")
428 devname=$(sys2dev "${dev}")
430 if is_in_space_sep_list ${devname} ${black_listed_devices}
432 # skip this subdevice
445 # Validate input first
446 if [ -z "${fstype}" ]
451 # get_fstype might report "unknown" or "swap", ignore it as no such kernel module exists
452 if [ "${fstype}" = "unknown" ] || [ "${fstype}" = "swap" ]
457 # Try to look if it is already supported by the kernel
458 if grep -q ${fstype} /proc/filesystems
462 # Then try to add support for it the gentle way using the initramfs capabilities
463 modprobe -q -b ${fstype}
464 if grep -q ${fstype} /proc/filesystems
467 # Then try the hard way if /root is already reachable
469 kmodule="/root/lib/modules/`uname -r`/${fstype}/${fstype}.ko"
470 if [ -e "${kmodule}" ]
473 if grep -q ${fstype} /proc/filesystems
486 blkid -s TYPE -o value $1 2>/dev/null
493 grep -m1 "^${device} " /proc/mounts | cut -f2 -d ' '
498 # remove all unnecessary /:s in the path, including last one (except
499 # if path is just "/")
500 echo ${1} | sed 's|//\+|/|g' | sed 's|^\(.*[^/]\)/$|\1|'
503 what_is_mounted_on ()
506 dir="$(trim_path ${1})"
507 grep -m1 "^[^ ]\+ ${dir} " /proc/mounts | cut -d' ' -f1
512 local reference targets owner
516 owner=$(stat -c %u:%g "${reference}")
517 chown -h ${owner} ${targets}
522 local reference targets rights
526 rights=$(stat -c %a "${reference}")
527 chmod ${rights} ${targets}
543 mounts="$(awk '{print $2}' /proc/mounts)"
544 testpath="$(realpath ${testpath})"
548 if echo "${mounts}" | grep -qs "^${testpath}"
550 set -- $(echo "${mounts}" | grep "^${testpath}" | lastline)
554 testpath=$(dirname $testpath)
561 # Returns used/free fs kbytes + 5% more
562 # You could pass a block device as ${1} or the mount point as ${2}
568 if [ -z "${mountp}" ]
570 mountp="$(where_is_mounted ${dev})"
572 if [ -z "${mountp}" ]
574 mountp="/mnt/tmp_fs_size"
577 mount -t $(get_fstype "${dev}") -o ro "${dev}" "${mountp}" || log_warning_msg "cannot mount -t $(get_fstype ${dev}) -o ro ${dev} ${mountp}"
583 if [ "${used}" = "used" ]
585 size=$(du -ks ${mountp} | cut -f1)
586 size=$(expr ${size} + ${size} / 20 ) # FIXME: 5% more to be sure
589 size="$(df -kP | grep -s ${mountp} | awk '{print $4}')"
592 if [ -n "${doumount}" ]
594 umount "${mountp}" || log_warning_msg "cannot umount ${mountp}"
604 if [ -x /bin/loadkeys -a -r /etc/boottime.kmap.gz ]
606 loadkeys --quiet /etc/boottime.kmap.gz
612 local fspath module pattern offset encryption readonly
620 # the output of setup_loop is evaluated in other functions,
621 # modprobe leaks kernel options like "libata.dma=0"
622 # as "options libata dma=0" on stdout, causing serious
623 # problems therefor, so instead always avoid output to stdout
624 modprobe -q -b "${module}" 1>/dev/null
628 for loopdev in ${pattern}
630 if [ "$(cat ${loopdev}/size)" -eq 0 ]
632 dev=$(sys2dev "${loopdev}")
635 if [ -n "${readonly}" ]
637 if losetup --help 2>&1 | grep -q -- "-r\b"
639 options="${options} -r"
643 if [ -n "${offset}" ] && [ 0 -lt "${offset}" ]
645 options="${options} -o ${offset}"
648 if [ -z "${encryption}" ]
650 losetup ${options} "${dev}" "${fspath}"
652 # Loop AES encryption
657 echo -n "Enter passphrase for root filesystem: " >&6
659 echo "${passphrase}" > /tmp/passphrase
661 exec 9</tmp/passphrase
662 losetup ${options} -e "${encryption}" -p 9 "${dev}" "${fspath}"
665 rm -f /tmp/passphrase
667 if [ 0 -eq ${error} ]
674 echo -n "There was an error decrypting the root filesystem ... Retry? [Y/n] " >&6
677 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
690 panic "No loop devices available"
700 old_mountp="$(where_is_mounted ${dev})"
702 if [ -n "${old_mountp}" ]
704 if [ "${opts}" != "ro" ]
706 mount -o remount,"${opts}" "${dev}" "${old_mountp}" || panic "Remounting ${dev} ${opts} on ${old_mountp} failed"
709 mount -o bind "${old_mountp}" "${mountp}" || panic "Cannot bind-mount ${old_mountp} on ${mountp}"
711 if [ -z "${fstype}" ]
713 fstype=$(get_fstype "${dev}")
715 mount -t "${fstype}" -o "${opts}" "${dev}" "${mountp}" || \
716 ( echo "SKIPPING: Cannot mount ${dev} on ${mountp}, fstype=${fstype}, options=${opts}" > boot.log && return 0 )
720 # Try to mount $device to the place expected by live-boot. If $device
721 # is already mounted somewhere, move it to the expected place. If $device
722 # ends with a "/" this is a directory path.
723 # If we're only probing $device (to check if it has custom persistence)
724 # $probe should be set, which suppresses warnings upon failure. On
725 # success, print the mount point for $device.
726 mount_persistence_media ()
728 local device probe backing old_backing fstype mount_opts
732 # get_custom_mounts() might call this with a directory path instead
733 # of a block device path. This means we have found sub-directory path
734 # underneath /run/live/persistence, so we're done
735 if [ -d "${device}" ]
741 if [ ! -b "${device}" ]
746 backing="/run/live/persistence/$(basename ${device})"
748 mkdir -p "${backing}"
749 old_backing="$(where_is_mounted ${device})"
750 if [ -z "${old_backing}" ]
752 fstype="$(get_fstype ${device})"
753 mount_opts="rw,noatime"
754 if [ -n "${PERSISTENCE_READONLY}" ]
756 mount_opts="ro,noatime"
758 if mount -t "${fstype}" -o "${mount_opts}" "${device}" "${backing}" >/dev/null
763 [ -z "${probe}" ] && log_warning_msg "Failed to mount persistence media ${device}"
767 elif [ "${backing}" != "${old_backing}" ]
769 if ! mount -o move ${old_backing} ${backing} >/dev/null
771 [ -z "${probe}" ] && log_warning_msg "Failed to move persistence media ${device}"
775 mount_opts="rw,noatime"
776 if [ -n "${PERSISTENCE_READONLY}" ]
778 mount_opts="ro,noatime"
780 if ! mount -o "remount,${mount_opts}" "${backing}" >/dev/null
782 log_warning_msg "Failed to remount persistence media ${device} writable"
783 # Don't unmount or rmdir the new mountpoint in this case
788 # This means that $device has already been mounted on
789 # the place expected by live-boot, so we're done.
795 close_persistence_media ()
799 backing="$(where_is_mounted ${device})"
801 if [ -d "${backing}" ]
803 umount "${backing}" >/dev/null 2>&1
804 rmdir "${backing}" >/dev/null 2>&1
807 if is_active_luks_mapping ${device}
809 cryptsetup luksClose ${device}
816 name="$(basename ${dev})"
818 if [ -n "${PERSISTENCE_READONLY}" ]
820 opts="${opts} --readonly"
823 if cryptsetup status "${name}" >/dev/null 2>&1
825 re="^[[:space:]]*device:[[:space:]]*\([^[:space:]]*\)$"
826 opened_dev=$(cryptsetup status ${name} 2>/dev/null | grep "${re}" | sed "s|${re}|\1|")
827 if [ "${opened_dev}" = "${dev}" ]
829 luks_device="/dev/mapper/${name}"
833 log_warning_msg "Cannot open luks device ${dev} since ${opened_dev} already is opened with its name"
841 if [ -x /bin/plymouth ]
846 case "${_PLYMOUTH}" in
850 cryptkeyscript="plymouth ask-for-password --prompt"
851 # Plymouth will add a : if it is a non-graphical prompt
852 cryptkeyprompt="Please unlock disk ${dev}"
856 cryptkeyscript="/lib/cryptsetup/askpass"
857 cryptkeyprompt="Please unlock disk ${dev}: "
863 $cryptkeyscript "$cryptkeyprompt" | \
864 cryptsetup -T 1 luksOpen ${dev} ${name} ${opts}
868 luks_device="/dev/mapper/${name}"
874 retryprompt="There was an error decrypting ${dev} ... Retry? [Y/n]"
876 case "${_PLYMOUTH}" in
878 plymouth display-message --text "${retryprompt}"
879 answer=$(plymouth watch-keystroke --keys="YNyn")
883 echo -n "${retryprompt} " >&6
888 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
890 case "${_PLYMOUTH}" in
892 plymouth display-message --text ""
905 blkid -s PART_ENTRY_NAME -p -o value ${dev} 2>/dev/null
912 [ "$(blkid -s PART_ENTRY_SCHEME -p -o value ${dev} 2>/dev/null)" = "gpt" ]
915 probe_for_gpt_name ()
917 local overlays dev gpt_dev gpt_name
922 if is_active_luks_mapping ${dev}
924 # if $dev is an opened luks device, we need to check
925 # GPT stuff on the backing device
926 gpt_dev=$(get_luks_backing_device "${dev}")
929 if ! is_gpt_device ${gpt_dev}
934 gpt_name=$(get_gpt_name ${gpt_dev})
935 for label in ${overlays}
937 if [ "${gpt_name}" = "${label}" ]
939 echo "${label}=${dev}"
944 probe_for_fs_label ()
950 for label in ${overlays}
952 if [ "$(blkid -s LABEL -o value $dev 2>/dev/null)" = "${label}" ]
954 echo "${label}=${dev}"
959 probe_for_file_name ()
961 local overlays dev ret backing
966 backing="$(mount_persistence_media ${dev} probe)"
967 if [ -z "${backing}" ]
972 for label in ${overlays}
974 path=${backing}/${PERSISTENCE_PATH}/${label}
978 loopdev=$(setup_loop "${path}" "loop" "/sys/block/loop*")
979 ret="${ret} ${label}=${loopdev}"
987 # unmount and remove mountpoint
988 umount ${backing} > /dev/null 2>&1 || true
989 rmdir ${backing} > /dev/null 2>&1 || true
993 probe_for_directory_name ()
995 local overlays dev ret backing
1000 backing="$(mount_persistence_media ${dev} probe)"
1001 if [ -z "${backing}" ]
1006 for label in ${overlays}
1008 path=${backing}/${PERSISTENCE_PATH}/${label}
1011 # in this case the "device" ends with a "/"
1012 ret="${ret} ${label}=${backing}/${PERSISTENCE_PATH}/${label%%/}/"
1020 # unmount and remove mountpoint
1021 umount ${backing} > /dev/null 2>&1 || true
1022 rmdir ${backing} > /dev/null 2>&1 || true
1026 find_persistence_media ()
1028 # Scans devices for overlays, and returns a whitespace
1029 # separated list of how to use them. Only overlays with a partition
1030 # label or file name in ${overlays} are returned.
1032 # When scanning a LUKS device, the user will be asked to enter the
1033 # passphrase; on failure to enter it, or if no persistence partitions
1034 # or files were found, the LUKS device is closed.
1036 # For all other cases (overlay partition and overlay file) the
1037 # return value is "${label}=${device}", where ${device} a device that
1038 # can mount the content. In the case of an overlay file, the device
1039 # containing the file will remain mounted as a side-effect.
1041 # No devices in ${black_listed_devices} will be scanned, and if
1042 # ${white_list_devices} is non-empty, only devices in it will be
1045 local overlays white_listed_devices ret black_listed_devices
1047 white_listed_devices="${2}"
1051 # The devices that are hosting the actual live rootfs should not be
1052 # used for persistence storage since otherwise you might mount a
1053 # parent directory on top of a sub-directory of the same filesystem
1054 # in one union together.
1056 black_listed_devices=""
1057 for d in /live/rootfs/* /live/findiso /live/fromiso
1059 black_listed_devices="${black_listed_devices} $(what_is_mounted_on d)"
1062 for dev in $(storage_devices "${black_listed_devices}" "${white_listed_devices}")
1064 local result luks_device
1068 # Check if it's a luks device; we'll have to open the device
1069 # in order to probe any filesystem it contains, like we do
1070 # below. activate_custom_mounts() also depends on that any luks
1071 # device already has been opened.
1072 if is_in_comma_sep_list luks ${PERSISTENCE_ENCRYPTION} && is_luks_partition ${dev}
1074 if luks_device=$(open_luks_device "${dev}")
1076 dev="${luks_device}"
1078 # skip $dev since we failed/chose not to open it
1081 elif ! is_in_comma_sep_list none ${PERSISTENCE_ENCRYPTION}
1083 # skip $dev since we don't allow unencrypted storage
1087 # Probe for matching GPT partition names or filesystem labels
1088 if is_in_comma_sep_list filesystem ${PERSISTENCE_STORAGE}
1090 result=$(probe_for_gpt_name "${overlays}" ${dev})
1091 if [ -n "${result}" ]
1093 ret="${ret} ${result}"
1097 result=$(probe_for_fs_label "${overlays}" ${dev})
1098 if [ -n "${result}" ]
1100 ret="${ret} ${result}"
1105 # Probe for files with matching name on mounted partition
1106 if is_in_comma_sep_list file ${PERSISTENCE_STORAGE}
1108 result=$(probe_for_file_name "${overlays}" ${dev})
1109 if [ -n "${result}" ]
1112 loopdevice=${result##*=}
1113 if is_in_comma_sep_list luks ${PERSISTENCE_ENCRYPTION} && is_luks_partition ${loopdevice}
1117 if luksfile=$(open_luks_device "${loopdevice}")
1119 result=${result%%=*}
1120 result="${result}=${luksfile}"
1122 losetup -d $loopdevice
1126 ret="${ret} ${result}"
1131 # Probe for directory with matching name on mounted partition
1132 if is_in_comma_sep_list directory ${PERSISTENCE_STORAGE}
1134 result=$(probe_for_directory_name "${overlays}" ${dev})
1135 if [ -n "${result}" ]
1137 ret="${ret} ${result}"
1142 # Close luks device if it isn't used
1143 if [ -z "${result}" ] && [ -n "${luks_device}" ] && is_active_luks_mapping "${luks_device}"
1145 cryptsetup luksClose "${luks_device}"
1159 for adaptor in /sys/class/net/*
1161 status="$(cat ${adaptor}/iflink)"
1163 if [ "${status}" -eq 2 ]
1165 mac="$(cat ${adaptor}/address)"
1166 mac="$(echo ${mac} | sed 's/:/-/g' | tr '[a-z]' '[A-Z]')"
1173 is_luks_partition ()
1176 cryptsetup isLuks "${device}" 1>/dev/null 2>&1
1179 is_active_luks_mapping ()
1182 cryptsetup status "${device}" 1>/dev/null 2>&1
1185 get_luks_backing_device ()
1188 cryptsetup status ${device} 2> /dev/null | \
1189 awk '{if ($1 == "device:") print $2}'
1194 output_format="${1}"
1198 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1200 if [ ! -d "${sysblock}" ]; then
1205 if [ "$(cat ${sysblock}/removable)" = "1" ]
1207 if [ -z "${want_usb}" ]
1211 if readlink ${sysblock} | grep -q usb
1218 if [ "${dev_ok}" = "true" ]
1220 case "${output_format}" in
1222 ret="${ret} ${sysblock}"
1225 devname=$(sys2dev "${sysblock}")
1226 ret="${ret} ${devname}"
1235 removable_usb_dev ()
1237 output_format="${1}"
1239 removable_dev "${output_format}" "want_usb"
1242 non_removable_dev ()
1244 output_format="${1}"
1247 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1249 if [ ! -d "${sysblock}" ]; then
1253 if [ "$(cat ${sysblock}/removable)" = "0" ]
1255 case "${output_format}" in
1257 ret="${ret} ${sysblock}"
1260 devname=$(sys2dev "${sysblock}")
1261 ret="${ret} ${devname}"
1272 # create source's directory structure in dest, and recursively
1273 # create symlinks in dest to to all files in source. if mask
1274 # is non-empty, remove mask from all source paths when
1275 # creating links (will be necessary if we change root, which
1276 # live-boot normally does (into $rootmnt)).
1277 local src_dir dest_dir src_transform
1279 # remove multiple /:s and ensure ending on /
1280 src_dir="$(trim_path ${1})/"
1281 dest_dir="$(trim_path ${2})/"
1282 src_transform="${3}"
1284 # This check can only trigger on the inital, non-recursive call since
1285 # we create the destination before recursive calls
1286 if [ ! -d "${dest_dir}" ]
1288 log_warning_msg "Must link_files into a directory"
1292 find "${src_dir}" -mindepth 1 -maxdepth 1 | \
1295 local dest final_src
1296 dest="${dest_dir}$(basename "${src}")"
1299 if [ -z "$(ls -A "${src}")" ]
1303 if [ ! -d "${dest}" ]
1306 chown_ref "${src}" "${dest}"
1307 chmod_ref "${src}" "${dest}"
1309 link_files "${src}" "${dest}" "${src_transform}"
1312 if [ -n "${src_transform}" ]
1314 final_src="$(echo ${final_src} | sed "${src_transform}")"
1316 rm -rf "${dest}" 2> /dev/null
1317 ln -s "${final_src}" "${dest}"
1318 chown_ref "${src}" "${dest}"
1325 local unionmountpoint unionrw unionro
1326 unionmountpoint="${1}" # directory where the union is mounted
1328 unionrw="${1}" # branch where the union changes are stored
1330 unionro="${*}" # space separated list of read-only branches (optional)
1332 case "${UNIONTYPE}" in
1338 unionmountopts="-o noatime,${noxino_opt},dirs=${unionrw}=${rw_opt}"
1339 if [ -n "${unionro}" ]
1341 for rofs in ${unionro}
1343 unionmountopts="${unionmountopts}:${rofs}=${ro_opt}"
1349 # XXX: can unionro be optional? i.e. can overlay skip lowerdir?
1350 if [ -z "${unionro}" ]
1352 panic "overlay needs at least one lower filesystem (read-only branch)."
1354 # Multiple lower layers can now be given using the the colon (":") as a
1355 # separator character between the directory names.
1356 unionro="$(echo ${unionro} | sed -e 's| |:|g')"
1357 # overlayfs requires:
1358 # + a workdir to become mounted
1359 # + workdir and upperdir to reside under the same mount
1360 # + workdir and upperdir to be in separate directories
1361 mkdir "${unionrw}/rw"
1362 mkdir "${unionrw}/work"
1363 unionmountopts="-o noatime,lowerdir=${unionro},upperdir=${unionrw}/rw,workdir=${unionrw}/work"
1367 mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
1370 get_custom_mounts ()
1372 # Side-effect: leaves $devices with persistence.conf mounted in /live/persistence
1373 # Side-effect: prints info to file $custom_mounts
1375 local custom_mounts devices bindings links
1380 bindings="/tmp/bindings.list"
1381 links="/tmp/links.list"
1382 rm -rf ${bindings} ${links} 2> /dev/null
1384 for device in ${devices}
1386 local device_name backing include_list
1387 device_name="$(basename ${device})"
1388 backing=$(mount_persistence_media ${device})
1389 if [ -z "${backing}" ]
1394 if [ -r "${backing}/${persistence_list}" ]
1396 include_list="${backing}/${persistence_list}"
1401 if [ -n "${LIVE_BOOT_DEBUG}" ] && [ -e "${include_list}" ]
1403 cp ${include_list} /live/persistence/${persistence_list}.${device_name}
1406 while read dir options # < ${include_list}
1408 if echo ${dir} | grep -qe "^[[:space:]]*\(#.*\)\?$"
1410 # skipping empty or commented lines
1414 if trim_path ${dir} | grep -q -e "^[^/]" -e "^/lib" -e "^/run/live\(/.*\)\?$" -e "^/\(.*/\)\?\.\.\?\(/.*\)\?$"
1416 log_warning_msg "Skipping unsafe custom mount ${dir}: must be an absolute path containing neither the \".\" nor \"..\" special dirs, and cannot be \"/lib\", or \"/run/live\" or any of its sub-directories."
1420 local opt_source opt_link source full_source full_dest
1423 for opt in $(echo ${options} | tr ',' ' ');
1427 opt_source=${opt#source=}
1435 log_warning_msg "Skipping custom mount with unknown option: ${opt}"
1442 if [ -n "${opt_source}" ]
1444 if echo ${opt_source} | grep -q -e "^/" -e "^\(.*/\)\?\.\.\?\(/.*\)\?$" && [ "${opt_source}" != "." ]
1446 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"
1449 source="${opt_source}"
1453 full_source="$(trim_path ${backing}/${source})"
1454 full_dest="$(trim_path ${rootmnt}/${dir})"
1455 if [ -n "${opt_link}" ]
1457 echo "${device} ${full_source} ${full_dest} ${options}" >> ${links}
1459 echo "${device} ${full_source} ${full_dest} ${options}" >> ${bindings}
1461 done < ${include_list}
1464 # We sort the list according to destination so we're sure that
1465 # we won't hide a previous mount. We also ignore duplicate
1466 # destinations in a more or less arbitrary way.
1467 [ -e "${bindings}" ] && sort -k3 -sbu ${bindings} >> ${custom_mounts} && rm ${bindings}
1469 # After all mounts are considered we add symlinks so they
1470 # won't be hidden by some mount.
1471 [ -e "${links}" ] && cat ${links} >> ${custom_mounts} && rm ${links}
1473 # We need to make sure that no two custom mounts have the same sources
1474 # or are nested; if that is the case, too much weird stuff can happen.
1475 local prev_source prev_dest
1476 prev_source="impossible source" # first iteration must not match
1478 # This sort will ensure that a source /a comes right before a source
1479 # /a/b so we only need to look at the previous source
1480 [ -e ${custom_mounts} ] && sort -k2 -b ${custom_mounts} |
1481 while read device source dest options
1483 if echo ${source} | grep -qe "^${prev_source}\(/.*\)\?$"
1485 panic "Two persistence mounts have the same or nested sources: ${source} on ${dest}, and ${prev_source} on ${prev_dest}"
1487 prev_source=${source}
1492 activate_custom_mounts ()
1494 local custom_mounts used_devices
1495 custom_mounts="${1}" # the ouput from get_custom_mounts()
1498 while read device source dest options # < ${custom_mounts}
1500 local opt_bind opt_link opt_union
1504 for opt in $(echo ${options} | tr ',' ' ');
1509 unset opt_link opt_union
1513 unset opt_bind opt_union
1517 unset opt_bind opt_link
1522 if [ -n "$(what_is_mounted_on "${dest}")" ]
1524 if [ "${dest}" = "${rootmnt}" ]
1528 log_warning_msg "Skipping custom mount ${dest}: $(what_is_mounted_on "${dest}") is already mounted there"
1533 if [ ! -d "${dest}" ]
1535 # create the destination and delete existing files in
1536 # its path that are in the way
1538 for dir in $(echo ${dest} | sed -e 's|/\+| |g')
1540 path=$(trim_path ${path}/${dir})
1548 if echo ${path} | grep -qe "^${rootmnt}/*home/[^/]\+"
1550 # if ${dest} is in /home try fixing proper ownership by assuming that the intended user is the first, which is usually the case
1551 # FIXME: this should really be handled by live-config since we don't know for sure which uid a certain user has until then
1552 chown 1000:1000 ${path}
1558 # if ${source} doesn't exist on our persistence media
1559 # we bootstrap it with $dest from the live filesystem.
1560 # this both makes sense and is critical if we're
1561 # dealing with /etc or other system dir.
1562 if [ ! -d "${source}" ]
1564 if [ -n "${PERSISTENCE_READONLY}" ]
1567 elif [ -n "${opt_union}" ] || [ -n "${opt_link}" ]
1569 # unions and don't need to be bootstrapped
1570 # link dirs can't be bootstrapped in a sensible way
1571 mkdir -p "${source}"
1572 chown_ref "${dest}" "${source}"
1573 chmod_ref "${dest}" "${source}"
1574 elif [ -n "${opt_bind}" ]
1576 # ensure that $dest is not copied *into* $source
1577 mkdir -p "$(dirname ${source})"
1578 cp -a "${dest}" "${source}"
1582 # XXX: If CONFIG_AUFS_ROBR is added to the Debian kernel we can
1583 # ignore the loop below and set rootfs_dest_backing=$dest
1584 local rootfs_dest_backing
1585 rootfs_dest_backing=""
1586 if [ -n "${opt_link}" ] || [ -n "${opt_union}" ]
1588 for d in /live/rootfs/*
1590 if [ -n "${rootmnt}" ]
1592 fs="${d}/$(echo ${dest} | sed -e "s|${rootmnt}||")"
1598 rootfs_dest_backing="${rootfs_dest_backing} ${fs}"
1603 local cow_dir links_source
1604 if [ -n "${opt_link}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1606 link_files ${source} ${dest} ""
1607 elif [ -n "${opt_link}" ] && [ -n "${PERSISTENCE_READONLY}" ]
1609 mkdir -p /run/live/persistence
1610 links_source=$(mktemp -d /run/live/persistence/links-source-XXXXXX)
1611 chown_ref ${source} ${links_source}
1612 chmod_ref ${source} ${links_source}
1613 # We put the cow dir in the below strange place to
1614 # make it absolutely certain that the link source
1615 # has its own directory and isn't nested with some
1616 # other custom mount (if so that mount's files would
1617 # be linked, causing breakage.
1618 cow_dir="/run/live/overlay/run/live/persistence/$(basename ${links_source})"
1620 chown_ref "${source}" "${cow_dir}"
1621 chmod_ref "${source}" "${cow_dir}"
1622 do_union ${links_source} ${cow_dir} ${source} ${rootfs_dest_backing}
1623 link_files ${links_source} ${dest} "s|^${rootmnt}||"
1624 elif [ -n "${opt_union}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1626 do_union ${dest} ${source} ${rootfs_dest_backing}
1627 elif [ -n "${opt_bind}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1629 mount -o bind "${source}" "${dest}"
1630 elif [ -n "${opt_bind}" -o -n "${opt_union}" ] && [ -n "${PERSISTENCE_READONLY}" ]
1632 # bind-mount and union mount are handled the same
1633 # in read-only mode, but note that rootfs_dest_backing
1634 # is non-empty (and necessary) only for unions
1635 cow_dir="/run/live/overlay/${dest}"
1636 if [ -e "${cow_dir}" ] && [ -z "${opt_link}" ]
1638 # If an earlier custom mount has files here
1639 # it will "block" the current mount's files
1640 # which is undesirable
1644 chown_ref "${source}" "${cow_dir}"
1645 chmod_ref "${source}" "${cow_dir}"
1646 if [ "${UNIONTYPE}" = "overlay" ]
1648 # When we use overlay we add the "/rw" postfix to our source when using it
1649 # as upper layer. Therefore we also have to add it here when using it as
1651 source="${source}/rw"
1653 do_union ${dest} ${cow_dir} ${source} ${rootfs_dest_backing}
1656 PERSISTENCE_IS_ON="1"
1657 export PERSISTENCE_IS_ON
1659 if echo ${used_devices} | grep -qve "^\(.* \)\?${device}\( .*\)\?$"
1661 used_devices="${used_devices} ${device}"
1663 done < ${custom_mounts}
1665 echo ${used_devices}
1672 [ $(stat -fc%d:%D "${directory}") != $(stat -fc%d:%D "${directory}/..") ]