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
22 if [ -n "$IGNORE_BOOTID" ] ; then
23 echo " * Ignoring verification of bootid.txt as requested via ignore_bootid.">>/boot.log
27 if [ -n "$BOOTID" ] && ! [ -r "${path}/conf/bootid.txt" ] ; then
28 echo " * Warning: bootid=... specified but no bootid.txt found on currently requested device.">>/boot.log
32 [ -r "${path}/conf/bootid.txt" ] || return 0
34 bootid_conf=$(cat "${path}/conf/bootid.txt")
36 if [ -z "$BOOTID" -a -z "$IGNORE_BOOTID" ]
38 echo " * Warning: bootid.txt found but ignore_bootid / bootid=.. bootoption missing...">>/boot.log
42 if [ "$BOOTID" = "$bootid_conf" ]
44 echo " * Successfully verified /conf/bootid.txt from ISO, continuing... ">>/boot.log
46 echo " * Warning: BOOTID of ISO does not match. Retrying and continuing search...">>/boot.log
56 if [ "${IGNORE_UUID}" ] || [ ! -e /conf/uuid.conf ]
62 uuid="$(cat /conf/uuid.conf)"
64 for try_uuid_file in "${path}/.disk/live-uuid"*
66 [ -e "${try_uuid_file}" ] || continue
68 try_uuid="$(cat "${try_uuid_file}")"
70 if [ "${uuid}" = "${try_uuid}" ]
82 *.squashfs|*.ext2|*.ext3|*.ext4|*.jffs2)
83 echo $(setup_loop "${1}" "loop" "/sys/block/loop*" '0' "${LIVE_MEDIA_ENCRYPTION}" "${2}")
91 panic "Unrecognized live filesystem: ${1}"
96 mount_images_in_directory ()
102 if is_live_path "${directory}"
104 [ -n "${mac}" ] && adddirectory="${directory}/${LIVE_MEDIA_PATH}/${mac}"
105 setup_unionfs "${directory}/${LIVE_MEDIA_PATH}" "${rootmnt}" "${adddirectory}"
107 panic "No supported filesystem images found at /${LIVE_MEDIA_PATH}."
113 sysfs_path="${1#/sys}"
115 if udevadm info --query=all --path="${sysfs_path}" | egrep -q "DEVTYPE=disk"
118 elif echo "${sysfs_path}" | grep -q '^/block/vd[a-z]$'
121 elif echo ${sysfs_path} | grep -q "^/block/dm-"
124 elif echo ${sysfs_path} | grep -q "^/block/mtdblock"
137 skip_uuid_check="${3}"
139 # support for fromiso=.../isofrom=....
143 fs_type="${FROMISO%%:*}"
145 iso_device="${FROMISO}"
146 if echo "${fs_type}" | grep -q '[^[:alnum:]_-]'; then
147 # Not a valid file system name. Treat as part of the
148 # path, and, especially, use autodetection.
151 # Looks like a file system specification, treat it
154 iso_device="${iso_device#*:}"
156 iso_device=$(dirname "${iso_device}")
157 if ! [ -b "${iso_device}" ]
159 # to support unusual device names like /dev/cciss/c0d0p1
160 # as well we have to identify the block device name, let's
161 # do that for up to 15 levels
163 while [ -n "${iso_device}" ] && [ "${i}" -gt '0' ]
165 iso_device=$(dirname "${iso_device}")
166 if [ -b "${iso_device}" ]; then
167 # Proper device, we're done.
170 # Otherwise, it *could* be a proper device
171 # that just hasn't been started yet - think
173 case "${iso_device}" in
174 '/dev/mapper/'*|'/dev/md/'*)
175 # We're looking for paths like
176 # /dev/{mapper,md}/foo here.
177 if printf '%s\n' "${iso_device}" | \
178 grep -Eqs '^/dev/(mapper|md)/[^/]+$'; then
179 # Okay, looks like a device we'll
180 # have to start later on, done here.
185 # This is different from the path above.
186 # Here, we're looking for something like
187 # /dev/md0, /dev/md126 and the like.
188 if printf '%s\n' "${iso_device}" | \
189 grep -Eqs '^/dev/md[[:digit:]]+$'; then
190 # Syntax matches, keep as device
191 # name for later when RAIDs were
202 if [ "${iso_device}" = '/' ]
204 # not a block device, check if it's an iso file, for
205 # example an ISO when booting on an ONIE system
206 if echo "${FROMISO}" | grep -q "\.iso$"
208 if [ '0' -ne "${fs_type_auto}" ]; then
209 # Autodetect fs type if not overridden.
210 fs_type=$(get_fstype "${FROMISO}")
212 if is_supported_fs ${fs_type}
214 mkdir /run/live/fromiso
215 mount -t "${fs_type}" -o 'ro' "${FROMISO}" '/run/live/fromiso'
218 echo "Warning: unable to mount ${FROMISO} (type ${fs_type})." >>/boot.log
220 devname="/run/live/fromiso"
223 echo "Warning: device for bootoption fromiso= ($FROMISO) not found.">>/boot.log
226 # Otherwise, it must be a block device, so record that as the devname.
227 devname="${iso_device}"
230 # Usual handling in the non-fromiso case.
231 if [ -z "${devname}" ]
233 devname=$(sys2dev "${sysdev}")
237 # Common code path again: if devname is a directory, we can just go ahead
238 # and bind-mount it to our target mount point.
239 if [ -d "${devname}" ]
241 mount -o bind "${devname}" $mountpoint || continue
243 if is_live_path $mountpoint
252 # Check if we have to start logical volumes or RAID arrays.
253 # Do this in the common code path so it's only executed once.
256 for device in ${devname}
261 if [ -x /scripts/local-top/lvm2 ]
263 ROOT="$device" resume="" /scripts/local-top/lvm2 >>/boot.log
268 # Adding raid support
269 if [ -x /scripts/local-block/mdadm ]
271 # Back in the day, when there was still a local-top mdadm script, we
272 # used to select specific devices to be auto-assembled.
273 # This functionality was dropped in the local-block script, so just
274 # scan and assemble all RAID devices.
275 /scripts/local-block/mdadm >>/boot.log
282 # Back to fromiso handling, if necessary.
283 # We should now have the necessary lv/RAID devices available, so try to use them.
286 if [ "${iso_device}" != '/' ]
288 # Need to extract actual ISO file path later on,
290 iso_name="${FROMISO}"
292 if [ '0' -ne "${fs_type_auto}" ]; then
293 # Try to auto-detect file system if not
294 # explicitly provided.
295 fs_type=$(get_fstype "${iso_device}")
297 # Delete file system type override.
298 iso_name="${iso_name#*:}"
300 # At this point, the backing device should always be
301 # at the very front, so remove that - leaving only the
303 iso_name="$(echo "${iso_name}" | sed "s|^${iso_device}||")"
304 if is_supported_fs ${fs_type}
306 mkdir /run/live/fromiso
307 mount -t "${fs_type}" -o 'ro' "${iso_device}" '/run/live/fromiso'
308 loopdevname=$(setup_loop "/run/live/fromiso/${iso_name}" "loop" "/sys/block/loop*" "" '')
309 devname="${loopdevname}"
311 # Reset device variable, we won't need it.
314 echo "Warning: unable to mount ${iso_device} (type ${fs_type})." >>/boot.log
319 [ -n "$device" ] && devname="$device"
321 [ -e "$devname" ] || continue
323 if [ -n "${LIVE_MEDIA_OFFSET}" ]
325 loopdevname=$(setup_loop "${devname}" "loop" "/sys/block/loop*" "${LIVE_MEDIA_OFFSET}" '')
326 devname="${loopdevname}"
329 fstype=$(get_fstype "${devname}")
331 if is_supported_fs ${fstype}
333 devuid=$(blkid -o value -s UUID "$devname")
334 [ -n "$devuid" ] && grep -qs "\<$devuid\>" /var/lib/live/boot/devices-already-tried-to-mount && continue
336 for _PARAMETER in ${LIVE_BOOT_CMDLINE}
338 case "${_PARAMETER}" in
345 if [ "${PERSISTENCE_FSCK}" = "true" ] || [ "${PERSISTENCE_FSCK}" = "yes" ] || [ "${FORCEFSCK}" = "true" ]
348 if [ "$FORCEFSCK" = "true" ]
354 if [ "$FSCKFIX" = "true" ] || [ "$FSCKFIX" = "yes" ]
359 fsck $fix $force ${devname} >> fsck.log 2>&1
362 mount -t ${fstype} -o ro,noatime "${devname}" ${mountpoint} || continue
363 [ -n "$devuid" ] && echo "$devuid" >> /var/lib/live/boot/devices-already-tried-to-mount
365 if [ -n "${FINDISO}" ]
367 if [ -f ${mountpoint}/${FINDISO} ]
370 mkdir -p /run/live/findiso
371 mount -t ${fstype} -o ro,noatime "${devname}" /run/live/findiso
372 loopdevname=$(setup_loop "/run/live/findiso/${FINDISO}" "loop" "/sys/block/loop*" 0 "")
373 devname="${loopdevname}"
374 mount -t iso9660 -o ro,noatime "${devname}" ${mountpoint}
380 if is_live_path ${mountpoint} && \
381 ([ "${skip_uuid_check}" ] || grml_match_bootid ${mountpoint})
386 umount ${mountpoint} 2>/dev/null
390 if [ -n "${LIVE_MEDIA_OFFSET}" ]
392 losetup -d "${loopdevname}"
402 # don't start autodetection before timeout has expired
403 if [ -n "${LIVE_MEDIA_TIMEOUT}" ]
405 if [ "${timeout}" -lt "${LIVE_MEDIA_TIMEOUT}" ]
411 # first look at the one specified in the command line
412 case "${LIVE_MEDIA}" in
414 for sysblock in $(removable_usb_dev "sys")
416 for dev in $(subdevices "${sysblock}")
418 if check_dev "${dev}"
428 for sysblock in $(removable_dev "sys")
430 for dev in $(subdevices "${sysblock}")
432 if check_dev "${dev}"
442 if [ ! -z "${LIVE_MEDIA}" ]
444 if check_dev "null" "${LIVE_MEDIA}" "skip_uuid_check"
452 # or do the scan of block devices
453 # prefer removable devices over non-removable devices, so scan them first
454 devices_to_scan="$(removable_dev 'sys') $(non_removable_dev 'sys')"
456 for sysblock in $devices_to_scan
458 devname=$(sys2dev "${sysblock}")
459 [ -e "$devname" ] || continue
460 fstype=$(get_fstype "${devname}")
462 if /lib/udev/cdrom_id ${devname} > /dev/null
464 if check_dev "null" "${devname}"
468 elif is_nice_device "${sysblock}"
470 for dev in $(subdevices "${sysblock}")
472 if check_dev "${dev}"
483 is_in_list_separator_helper ()
485 local sep element list
491 echo ${list} | grep -qe "^\(.*${sep}\)\?${element}\(${sep}.*\)\?$"
494 is_in_space_sep_list ()
499 is_in_list_separator_helper "[[:space:]]" "${element}" "${*}"
502 is_in_comma_sep_list ()
507 is_in_list_separator_helper "," "${element}" "${*}"
513 echo "/dev/$(udevadm info -q name -p ${sysdev} 2>/dev/null|| echo ${sysdev##*/})"
521 for dev in "${sysblock}"/* "${sysblock}"
523 if [ -e "${dev}/dev" ]
534 black_listed_devices="${1}"
535 white_listed_devices="${2}"
537 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "loop|ram|fd")
539 fulldevname=$(sys2dev "${sysblock}")
541 if is_in_space_sep_list ${fulldevname} ${black_listed_devices} || \
542 [ -n "${white_listed_devices}" ] && \
543 ! is_in_space_sep_list ${fulldevname} ${white_listed_devices}
545 # skip this device entirely
549 for dev in $(subdevices "${sysblock}")
551 devname=$(sys2dev "${dev}")
553 if is_in_space_sep_list ${devname} ${black_listed_devices}
555 # skip this subdevice
568 # Validate input first
569 if [ -z "${fstype}" ]
574 # get_fstype might report "unknown" or "swap", ignore it as no such kernel module exists
575 if [ "${fstype}" = "unknown" ] || [ "${fstype}" = "swap" ]
580 # Try to look if it is already supported by the kernel
581 # For ntfs, since user space program ntfs-3g will be used. Check ntfs-3g instead of kernel module.
582 if [ "${fstype}" = "ntfs" ]; then
583 if type ntfs-3g >/dev/null 2>&1; then
589 if grep -q ${fstype} /proc/filesystems
593 # Then try to add support for it the gentle way using the initramfs capabilities
594 modprobe -q -b ${fstype}
595 if grep -q ${fstype} /proc/filesystems
598 # Then try the hard way if /root is already reachable
600 kmodule="/root/lib/modules/`uname -r`/${fstype}/${fstype}.ko"
601 if [ -e "${kmodule}" ]
604 if grep -q ${fstype} /proc/filesystems
617 blkid -s TYPE -o value $1 2>/dev/null
624 grep -m1 "^${device} " /proc/mounts | cut -f2 -d ' '
629 # remove all unnecessary /:s in the path, including last one (except
630 # if path is just "/")
631 echo ${1} | sed 's|//\+|/|g' | sed 's|^\(.*[^/]\)/$|\1|'
634 what_is_mounted_on ()
637 dir="$(trim_path ${1})"
638 grep -m1 "^[^ ]\+ ${dir} " /proc/mounts | cut -d' ' -f1
643 local reference targets owner
647 owner=$(stat -c %u:%g "${reference}")
648 chown -h ${owner} ${targets}
653 local reference targets rights
657 rights=$(stat -c %a "${reference}")
658 chmod ${rights} ${targets}
674 mounts="$(awk '{print $2}' /proc/mounts)"
675 testpath="$(realpath ${testpath})"
679 if echo "${mounts}" | grep -qs "^${testpath}"
681 set -- $(echo "${mounts}" | grep "^${testpath}" | lastline)
685 testpath=$(dirname $testpath)
692 # Returns used/free fs kbytes + 5% more
693 # You could pass a block device as ${1} or the mount point as ${2}
699 if [ -z "${mountp}" ]
701 mountp="$(where_is_mounted ${dev})"
703 if [ -z "${mountp}" ]
705 mountp="/mnt/tmp_fs_size"
708 mount -t $(get_fstype "${dev}") -o ro "${dev}" "${mountp}" || log_warning_msg "cannot mount -t $(get_fstype ${dev}) -o ro ${dev} ${mountp}"
714 if [ "${used}" = "used" ]
716 size=$(du -ks ${mountp} | cut -f1)
717 size=$(expr ${size} + ${size} / 20 ) # FIXME: 5% more to be sure
720 size="$(df -kP | grep -s ${mountp} | awk '{print $4}')"
723 if [ -n "${doumount}" ]
725 umount "${mountp}" || log_warning_msg "cannot umount ${mountp}"
735 if [ -x /bin/loadkeys -a -r /etc/boottime.kmap.gz ]
737 loadkeys --quiet /etc/boottime.kmap.gz
743 local fspath module pattern offset encryption readonly
751 # the output of setup_loop is evaluated in other functions,
752 # modprobe leaks kernel options like "libata.dma=0"
753 # as "options libata dma=0" on stdout, causing serious
754 # problems therefor, so instead always avoid output to stdout
755 modprobe -q -b "${module}" 1>/dev/null
759 for loopdev in ${pattern}
761 if [ "$(cat ${loopdev}/size)" -eq 0 ]
763 dev=$(sys2dev "${loopdev}")
766 if [ -n "${readonly}" ]
768 if losetup --help 2>&1 | grep -q -- "-r\b"
770 options="${options} -r"
774 if [ -n "${offset}" ] && [ 0 -lt "${offset}" ]
776 options="${options} -o ${offset}"
779 if [ -z "${encryption}" ]
781 losetup ${options} "${dev}" "${fspath}"
783 # Loop AES encryption
788 echo -n "Enter passphrase for root filesystem: " >&6
790 echo "${passphrase}" > /tmp/passphrase
792 exec 9</tmp/passphrase
793 losetup ${options} -e "${encryption}" -p 9 "${dev}" "${fspath}"
796 rm -f /tmp/passphrase
798 if [ 0 -eq ${error} ]
805 echo -n "There was an error decrypting the root filesystem ... Retry? [Y/n] " >&6
808 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
821 panic "No loop devices available"
831 old_mountp="$(where_is_mounted ${dev})"
833 if [ -n "${old_mountp}" ]
835 if [ "${opts}" != "ro" ]
837 mount -o remount,"${opts}" "${dev}" "${old_mountp}" || panic "Remounting ${dev} ${opts} on ${old_mountp} failed"
840 mount -o bind "${old_mountp}" "${mountp}" || panic "Cannot bind-mount ${old_mountp} on ${mountp}"
842 if [ -z "${fstype}" ]
844 fstype=$(get_fstype "${dev}")
846 mount -t "${fstype}" -o "${opts}" "${dev}" "${mountp}" || \
847 ( echo "SKIPPING: Cannot mount ${dev} on ${mountp}, fstype=${fstype}, options=${opts}" >> boot.log && return 0 )
851 # Try to mount $device to the place expected by live-boot. If $device
852 # is already mounted somewhere, move it to the expected place. If $device
853 # ends with a "/" this is a directory path.
854 # If we're only probing $device (to check if it has custom persistence)
855 # $probe should be set, which suppresses warnings upon failure. On
856 # success, print the mount point for $device.
857 mount_persistence_media ()
859 local device probe backing old_backing fstype mount_opts
863 # get_custom_mounts() might call this with a directory path instead
864 # of a block device path. This means we have found sub-directory path
865 # underneath /run/live/persistence, so we're done
866 if [ -d "${device}" ]
872 if [ ! -b "${device}" ]
877 backing="/run/live/persistence/$(basename ${device})"
879 mkdir -p "${backing}"
880 old_backing="$(where_is_mounted ${device})"
881 if [ -z "${old_backing}" ]
883 fstype="$(get_fstype ${device})"
884 mount_opts="rw,noatime"
885 if [ -n "${PERSISTENCE_READONLY}" ]
887 mount_opts="ro,noatime"
889 if mount -t "${fstype}" -o "${mount_opts}" "${device}" "${backing}" >/dev/null 2>&1
894 [ -z "${probe}" ] && log_warning_msg "Failed to mount persistence media ${device}"
898 elif [ "${backing}" != "${old_backing}" ]
900 if ! mount -o move ${old_backing} ${backing} >/dev/null
902 [ -z "${probe}" ] && log_warning_msg "Failed to move persistence media ${device}"
906 mount_opts="rw,noatime"
907 if [ -n "${PERSISTENCE_READONLY}" ]
909 mount_opts="ro,noatime"
911 if ! mount -o "remount,${mount_opts}" "${backing}" >/dev/null
913 log_warning_msg "Failed to remount persistence media ${device} writable"
914 # Don't unmount or rmdir the new mountpoint in this case
919 # This means that $device has already been mounted on
920 # the place expected by live-boot, so we're done.
926 close_persistence_media ()
930 backing="$(where_is_mounted ${device})"
932 if [ -d "${backing}" ]
934 umount "${backing}" >/dev/null 2>&1
935 rmdir "${backing}" >/dev/null 2>&1
938 if is_active_luks_mapping ${device}
940 cryptsetup luksClose ${device}
947 name="$(basename ${dev})"
949 if [ -n "${PERSISTENCE_READONLY}" ]
951 opts="${opts} --readonly"
954 if cryptsetup status "${name}" >/dev/null 2>&1
956 re="^[[:space:]]*device:[[:space:]]*\([^[:space:]]*\)$"
957 opened_dev=$(cryptsetup status ${name} 2>/dev/null | grep "${re}" | sed "s|${re}|\1|")
958 if [ "${opened_dev}" = "${dev}" ]
960 luks_device="/dev/mapper/${name}"
964 log_warning_msg "Cannot open luks device ${dev} since ${opened_dev} already is opened with its name"
972 if [ -x /bin/plymouth ]
977 case "${_PLYMOUTH}" in
981 cryptkeyscript="plymouth ask-for-password --prompt"
982 # Plymouth will add a : if it is a non-graphical prompt
983 cryptkeyprompt="Please unlock disk ${dev}"
987 cryptkeyscript="/lib/cryptsetup/askpass"
988 cryptkeyprompt="Please unlock disk ${dev}: "
994 $cryptkeyscript "$cryptkeyprompt" | \
995 cryptsetup -T 1 luksOpen ${dev} ${name} ${opts}
999 luks_device="/dev/mapper/${name}"
1005 retryprompt="There was an error decrypting ${dev} ... Retry? [Y/n]"
1007 case "${_PLYMOUTH}" in
1009 plymouth display-message --text "${retryprompt}"
1010 answer=$(plymouth watch-keystroke --keys="YNyn")
1014 echo -n "${retryprompt} " >&6
1019 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
1021 case "${_PLYMOUTH}" in
1023 plymouth display-message --text ""
1036 blkid -s PART_ENTRY_NAME -p -o value ${dev} 2>/dev/null
1043 [ "$(blkid -s PART_ENTRY_SCHEME -p -o value ${dev} 2>/dev/null)" = "gpt" ]
1046 probe_for_gpt_name ()
1048 local overlays dev gpt_dev gpt_name
1053 if is_active_luks_mapping ${dev}
1055 # if $dev is an opened luks device, we need to check
1056 # GPT stuff on the backing device
1057 gpt_dev=$(get_luks_backing_device "${dev}")
1060 if ! is_gpt_device ${gpt_dev}
1065 gpt_name=$(get_gpt_name ${gpt_dev})
1066 for label in ${overlays}
1068 if [ "${gpt_name}" = "${label}" ]
1070 echo "${label}=${dev}"
1075 probe_for_fs_label ()
1081 for label in ${overlays}
1083 if [ "$(blkid -s LABEL -o value $dev 2>/dev/null)" = "${label}" ]
1085 echo "${label}=${dev}"
1090 probe_for_file_name ()
1092 local overlays dev ret backing
1097 backing="$(mount_persistence_media ${dev} probe)"
1098 if [ -z "${backing}" ]
1103 for label in ${overlays}
1105 path=${backing}/${PERSISTENCE_PATH}/${label}
1109 loopdev=$(setup_loop "${path}" "loop" "/sys/block/loop*")
1110 ret="${ret} ${label}=${loopdev}"
1118 # unmount and remove mountpoint
1119 umount ${backing} > /dev/null 2>&1 || true
1120 rmdir ${backing} > /dev/null 2>&1 || true
1124 probe_for_directory_name ()
1126 local overlays dev ret backing
1131 backing="$(mount_persistence_media ${dev} probe)"
1132 if [ -z "${backing}" ]
1137 for label in ${overlays}
1139 path=${backing}/${PERSISTENCE_PATH}/${label}
1142 # in this case the "device" ends with a "/"
1143 ret="${ret} ${label}=${backing}/${PERSISTENCE_PATH}/${label%%/}/"
1151 # unmount and remove mountpoint
1152 umount ${backing} > /dev/null 2>&1 || true
1153 rmdir ${backing} > /dev/null 2>&1 || true
1157 find_persistence_media ()
1159 # Scans devices for overlays, and returns a whitespace
1160 # separated list of how to use them. Only overlays with a partition
1161 # label or file name in ${overlays} are returned.
1163 # When scanning a LUKS device, the user will be asked to enter the
1164 # passphrase; on failure to enter it, or if no persistence partitions
1165 # or files were found, the LUKS device is closed.
1167 # For all other cases (overlay partition and overlay file) the
1168 # return value is "${label}=${device}", where ${device} a device that
1169 # can mount the content. In the case of an overlay file, the device
1170 # containing the file will remain mounted as a side-effect.
1172 # No devices in ${black_listed_devices} will be scanned, and if
1173 # ${white_list_devices} is non-empty, only devices in it will be
1176 local overlays white_listed_devices ret black_listed_devices
1178 white_listed_devices="${2}"
1182 # The devices that are hosting the actual live rootfs should not be
1183 # used for persistence storage since otherwise you might mount a
1184 # parent directory on top of a sub-directory of the same filesystem
1185 # in one union together.
1187 black_listed_devices=""
1188 for d in /run/live/rootfs/* /run/live/findiso /run/live/fromiso
1190 black_listed_devices="${black_listed_devices} $(what_is_mounted_on d)"
1193 for dev in $(storage_devices "${black_listed_devices}" "${white_listed_devices}")
1195 local result luks_device
1199 # Check if it's a luks device; we'll have to open the device
1200 # in order to probe any filesystem it contains, like we do
1201 # below. activate_custom_mounts() also depends on that any luks
1202 # device already has been opened.
1203 if is_in_comma_sep_list luks ${PERSISTENCE_ENCRYPTION} && is_luks_partition ${dev}
1205 if luks_device=$(open_luks_device "${dev}")
1207 dev="${luks_device}"
1209 # skip $dev since we failed/chose not to open it
1212 elif ! is_in_comma_sep_list none ${PERSISTENCE_ENCRYPTION}
1214 # skip $dev since we don't allow unencrypted storage
1218 # Probe for matching GPT partition names or filesystem labels
1219 if is_in_comma_sep_list filesystem ${PERSISTENCE_STORAGE}
1221 result=$(probe_for_gpt_name "${overlays}" ${dev})
1222 if [ -n "${result}" ]
1224 ret="${ret} ${result}"
1228 result=$(probe_for_fs_label "${overlays}" ${dev})
1229 if [ -n "${result}" ]
1231 ret="${ret} ${result}"
1236 # Probe for files with matching name on mounted partition
1237 if is_in_comma_sep_list file ${PERSISTENCE_STORAGE}
1239 result=$(probe_for_file_name "${overlays}" ${dev})
1240 if [ -n "${result}" ]
1243 loopdevice=${result##*=}
1244 if is_in_comma_sep_list luks ${PERSISTENCE_ENCRYPTION} && is_luks_partition ${loopdevice}
1248 if luksfile=$(open_luks_device "${loopdevice}")
1250 result=${result%%=*}
1251 result="${result}=${luksfile}"
1253 losetup -d $loopdevice
1257 ret="${ret} ${result}"
1262 # Probe for directory with matching name on mounted partition
1263 if is_in_comma_sep_list directory ${PERSISTENCE_STORAGE}
1265 result=$(probe_for_directory_name "${overlays}" ${dev})
1266 if [ -n "${result}" ]
1268 ret="${ret} ${result}"
1273 # Close luks device if it isn't used
1274 if [ -z "${result}" ] && [ -n "${luks_device}" ] && is_active_luks_mapping "${luks_device}"
1276 cryptsetup luksClose "${luks_device}"
1290 for adaptor in /sys/class/net/*
1292 status="$(cat ${adaptor}/iflink)"
1294 if [ "${status}" -eq 2 ]
1296 mac="$(cat ${adaptor}/address)"
1297 mac="$(echo ${mac} | sed 's/:/-/g' | tr '[a-z]' '[A-Z]')"
1304 is_luks_partition ()
1307 cryptsetup isLuks "${device}" 1>/dev/null 2>&1
1310 is_active_luks_mapping ()
1313 cryptsetup status "${device}" 1>/dev/null 2>&1
1316 get_luks_backing_device ()
1319 cryptsetup status ${device} 2> /dev/null | \
1320 awk '{if ($1 == "device:") print $2}'
1325 output_format="${1}"
1329 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1331 if [ ! -d "${sysblock}" ]; then
1336 if [ "$(cat ${sysblock}/removable)" = "1" ]
1338 if [ -z "${want_usb}" ]
1342 if readlink ${sysblock} | grep -q usb
1349 if [ "${dev_ok}" = "true" ]
1351 case "${output_format}" in
1353 ret="${ret} ${sysblock}"
1356 devname=$(sys2dev "${sysblock}")
1357 ret="${ret} ${devname}"
1366 removable_usb_dev ()
1368 output_format="${1}"
1370 removable_dev "${output_format}" "want_usb"
1373 non_removable_dev ()
1375 output_format="${1}"
1378 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1380 if [ ! -d "${sysblock}" ]; then
1384 if [ "$(cat ${sysblock}/removable)" = "0" ]
1386 case "${output_format}" in
1388 ret="${ret} ${sysblock}"
1391 devname=$(sys2dev "${sysblock}")
1392 ret="${ret} ${devname}"
1403 # create source's directory structure in dest, and recursively
1404 # create symlinks in dest to to all files in source. if mask
1405 # is non-empty, remove mask from all source paths when
1406 # creating links (will be necessary if we change root, which
1407 # live-boot normally does (into $rootmnt)).
1408 local src_dir dest_dir src_transform
1410 # remove multiple /:s and ensure ending on /
1411 src_dir="$(trim_path ${1})/"
1412 dest_dir="$(trim_path ${2})/"
1413 src_transform="${3}"
1415 # This check can only trigger on the inital, non-recursive call since
1416 # we create the destination before recursive calls
1417 if [ ! -d "${dest_dir}" ]
1419 log_warning_msg "Must link_files into a directory"
1423 find "${src_dir}" -mindepth 1 -maxdepth 1 | \
1426 local dest final_src
1427 dest="${dest_dir}$(basename "${src}")"
1430 if [ -z "$(ls -A "${src}")" ]
1434 if [ ! -d "${dest}" ]
1437 chown_ref "${src}" "${dest}"
1438 chmod_ref "${src}" "${dest}"
1440 link_files "${src}" "${dest}" "${src_transform}"
1443 if [ -n "${src_transform}" ]
1445 final_src="$(echo ${final_src} | sed "${src_transform}")"
1447 rm -rf "${dest}" 2> /dev/null
1448 ln -s "${final_src}" "${dest}"
1449 chown_ref "${src}" "${dest}"
1456 local unionmountpoint unionrw unionro
1457 unionmountpoint="${1}" # directory where the union is mounted
1459 unionrw="${1}" # branch where the union changes are stored
1461 unionro="${*}" # space separated list of read-only branches (optional)
1463 case "${UNIONTYPE}" in
1469 unionmountopts="-o noatime,${noxino_opt},dirs=${unionrw}=${rw_opt}"
1470 if [ -n "${unionro}" ]
1472 for rofs in ${unionro}
1474 unionmountopts="${unionmountopts}:${rofs}=${ro_opt}"
1480 # XXX: can unionro be optional? i.e. can overlay skip lowerdir?
1481 if [ -z "${unionro}" ]
1483 panic "overlay needs at least one lower filesystem (read-only branch)."
1485 # Multiple lower layers can now be given using the the colon (":") as a
1486 # separator character between the directory names.
1487 unionro="$(echo ${unionro} | sed -e 's| |:|g')"
1488 # overlayfs requires:
1489 # + a workdir to become mounted
1490 # + workdir and upperdir to reside under the same mount
1491 # + workdir and upperdir to be in separate directories
1492 mkdir -p "${unionrw}/rw"
1493 mkdir -p "${unionrw}/work"
1494 unionmountopts="-o noatime,lowerdir=${unionro},upperdir=${unionrw}/rw,workdir=${unionrw}/work"
1498 mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
1501 get_custom_mounts ()
1503 # Side-effect: leaves $devices with persistence.conf mounted in /run/live/persistence
1504 # Side-effect: prints info to file $custom_mounts
1506 local custom_mounts devices bindings links
1511 bindings="/tmp/bindings.list"
1512 links="/tmp/links.list"
1513 rm -rf ${bindings} ${links} 2> /dev/null
1515 for device in ${devices}
1517 local device_name backing include_list
1518 device_name="$(basename ${device})"
1519 backing=$(mount_persistence_media ${device})
1520 if [ -z "${backing}" ]
1525 if [ -r "${backing}/${persistence_list}" ]
1527 include_list="${backing}/${persistence_list}"
1532 if [ -n "${LIVE_BOOT_DEBUG}" ] && [ -e "${include_list}" ]
1534 cp ${include_list} /run/live/persistence/${persistence_list}.${device_name}
1537 while read dir options # < ${include_list}
1539 if echo ${dir} | grep -qe "^[[:space:]]*\(#.*\)\?$"
1541 # skipping empty or commented lines
1545 if trim_path ${dir} | grep -q -e "^[^/]" -e "^/lib" -e "^/run/live\(/.*\)\?$" -e "^/\(.*/\)\?\.\.\?\(/.*\)\?$"
1547 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."
1551 local opt_source opt_link source full_source full_dest
1554 for opt in $(echo ${options} | tr ',' ' ');
1558 opt_source=${opt#source=}
1566 log_warning_msg "Skipping custom mount with unknown option: ${opt}"
1573 if [ -n "${opt_source}" ]
1575 if echo ${opt_source} | grep -q -e "^/" -e "^\(.*/\)\?\.\.\?\(/.*\)\?$" && [ "${opt_source}" != "." ]
1577 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"
1580 source="${opt_source}"
1584 full_source="$(trim_path ${backing}/${source})"
1585 full_dest="$(trim_path ${rootmnt}/${dir})"
1586 if [ -n "${opt_link}" ]
1588 echo "${device} ${full_source} ${full_dest} ${options}" >> ${links}
1590 echo "${device} ${full_source} ${full_dest} ${options}" >> ${bindings}
1592 done < ${include_list}
1595 # We sort the list according to destination so we're sure that
1596 # we won't hide a previous mount. We also ignore duplicate
1597 # destinations in a more or less arbitrary way.
1598 [ -e "${bindings}" ] && sort -k3 -sbu ${bindings} >> ${custom_mounts} && rm ${bindings}
1600 # After all mounts are considered we add symlinks so they
1601 # won't be hidden by some mount.
1602 [ -e "${links}" ] && cat ${links} >> ${custom_mounts} && rm ${links}
1604 # We need to make sure that no two custom mounts have the same sources
1605 # or are nested; if that is the case, too much weird stuff can happen.
1606 local prev_source prev_dest
1607 prev_source="impossible source" # first iteration must not match
1609 # This sort will ensure that a source /a comes right before a source
1610 # /a/b so we only need to look at the previous source
1611 [ -e ${custom_mounts} ] && sort -k2 -b ${custom_mounts} |
1612 while read device source dest options
1614 if echo ${source} | grep -qe "^${prev_source}\(/.*\)\?$"
1616 panic "Two persistence mounts have the same or nested sources: ${source} on ${dest}, and ${prev_source} on ${prev_dest}"
1618 prev_source=${source}
1623 activate_custom_mounts ()
1625 local custom_mounts used_devices
1626 custom_mounts="${1}" # the ouput from get_custom_mounts()
1629 while read device source dest options # < ${custom_mounts}
1631 local opt_bind opt_link opt_union
1635 for opt in $(echo ${options} | tr ',' ' ');
1640 unset opt_link opt_union
1644 unset opt_bind opt_union
1648 unset opt_bind opt_link
1653 if [ -n "$(what_is_mounted_on "${dest}")" ]
1655 if [ "${dest}" = "${rootmnt}" ]
1659 log_warning_msg "Skipping custom mount ${dest}: $(what_is_mounted_on "${dest}") is already mounted there"
1664 if [ ! -d "${dest}" ]
1666 # create the destination and delete existing files in
1667 # its path that are in the way
1669 for dir in $(echo ${dest} | sed -e 's|/\+| |g')
1671 path=$(trim_path ${path}/${dir})
1679 if echo ${path} | grep -qe "^${rootmnt}/*home/[^/]\+"
1681 # if ${dest} is in /home try fixing proper ownership by assuming that the intended user is the first, which is usually the case
1682 # FIXME: this should really be handled by live-config since we don't know for sure which uid a certain user has until then
1683 chown 1000:1000 ${path}
1689 # if ${source} doesn't exist on our persistence media
1690 # we bootstrap it with $dest from the live filesystem.
1691 # this both makes sense and is critical if we're
1692 # dealing with /etc or other system dir.
1693 if [ ! -d "${source}" ]
1695 if [ -n "${PERSISTENCE_READONLY}" ]
1698 elif [ -n "${opt_union}" ] || [ -n "${opt_link}" ]
1700 # unions and don't need to be bootstrapped
1701 # link dirs can't be bootstrapped in a sensible way
1702 mkdir -p "${source}"
1703 chown_ref "${dest}" "${source}"
1704 chmod_ref "${dest}" "${source}"
1705 elif [ -n "${opt_bind}" ]
1707 # ensure that $dest is not copied *into* $source
1708 mkdir -p "$(dirname ${source})"
1709 cp -a "${dest}" "${source}"
1713 # XXX: If CONFIG_AUFS_ROBR is added to the Debian kernel we can
1714 # ignore the loop below and set rootfs_dest_backing=$dest
1715 local rootfs_dest_backing
1716 rootfs_dest_backing=""
1717 if [ -n "${opt_link}" ] || [ -n "${opt_union}" ]
1719 for d in /run/live/rootfs/*
1721 if [ -n "${rootmnt}" ]
1723 fs="${d}/$(echo ${dest} | sed -e "s|${rootmnt}||")"
1729 rootfs_dest_backing="${rootfs_dest_backing} ${fs}"
1734 local cow_dir links_source
1735 if [ -n "${opt_link}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1737 link_files ${source} ${dest} ""
1738 elif [ -n "${opt_link}" ] && [ -n "${PERSISTENCE_READONLY}" ]
1740 mkdir -p /run/live/persistence
1741 links_source=$(mktemp -d /run/live/persistence/links-source-XXXXXX)
1742 chown_ref ${source} ${links_source}
1743 chmod_ref ${source} ${links_source}
1744 # We put the cow dir in the below strange place to
1745 # make it absolutely certain that the link source
1746 # has its own directory and isn't nested with some
1747 # other custom mount (if so that mount's files would
1748 # be linked, causing breakage.
1749 cow_dir="/run/live/overlay/run/live/persistence/$(basename ${links_source})"
1751 chown_ref "${source}" "${cow_dir}"
1752 chmod_ref "${source}" "${cow_dir}"
1753 do_union ${links_source} ${cow_dir} ${source} ${rootfs_dest_backing}
1754 link_files ${links_source} ${dest} "s|^${rootmnt}||"
1755 elif [ -n "${opt_union}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1757 do_union ${dest} ${source} ${rootfs_dest_backing}
1758 elif [ -n "${opt_bind}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1760 mount -o bind "${source}" "${dest}"
1761 elif [ -n "${opt_bind}" -o -n "${opt_union}" ] && [ -n "${PERSISTENCE_READONLY}" ]
1763 # bind-mount and union mount are handled the same
1764 # in read-only mode, but note that rootfs_dest_backing
1765 # is non-empty (and necessary) only for unions
1766 cow_dir="/run/live/overlay/${dest}"
1767 if [ -e "${cow_dir}" ] && [ -z "${opt_link}" ]
1769 # If an earlier custom mount has files here
1770 # it will "block" the current mount's files
1771 # which is undesirable
1775 chown_ref "${source}" "${cow_dir}"
1776 chmod_ref "${source}" "${cow_dir}"
1777 if [ "${UNIONTYPE}" = "overlay" ]
1779 # When we use overlay we add the "/rw" postfix to our source when using it
1780 # as upper layer. Therefore we also have to add it here when using it as
1782 source="${source}/rw"
1784 do_union ${dest} ${cow_dir} ${source} ${rootfs_dest_backing}
1787 PERSISTENCE_IS_ON="1"
1788 export PERSISTENCE_IS_ON
1790 if echo ${used_devices} | grep -qve "^\(.* \)\?${device}\( .*\)\?$"
1792 used_devices="${used_devices} ${device}"
1794 done < ${custom_mounts}
1796 echo ${used_devices}
1803 [ $(stat -fc%d:%D "${directory}") != $(stat -fc%d:%D "${directory}/..") ]