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 # Before actually doing anything, make sure that no auto-generated
272 # mdadm.conf is around in the initramfs.
273 # Short explanation of why this is needed:
274 # /usr/share/initramfs-tools/hooks/mdadm is executed as part of
275 # mkinitramfs, which generates an /etc/mdadm/mdadm.conf file if one
276 # doesn't exist yet (which should always be the case in our new
277 # chroot) based on the host system configuration. Eventually, this
278 # is copied into the generated initramfs and then causes issues on
280 # It's safe to delete the file within the initramfs. The actual
281 # squashfs root will not be affected by that (and, additionally,
282 # a FAI script makes sure that the mdadm.conf in there gets wiped
283 # of any auto-generated arrays).
284 rm -rf '/etc/mdadm/mdadm.conf'
286 # Back in the day, when there was still a local-top mdadm script, we
287 # used to select specific devices to be auto-assembled.
288 # This functionality was dropped in the local-block script, so just
289 # scan and assemble all RAID devices.
290 /scripts/local-block/mdadm >>/boot.log
297 # Back to fromiso handling, if necessary.
298 # We should now have the necessary lv/RAID devices available, so try to use them.
301 if [ "${iso_device}" != '/' ]
303 # Need to extract actual ISO file path later on,
305 iso_name="${FROMISO}"
307 if [ '0' -ne "${fs_type_auto}" ]; then
308 # Try to auto-detect file system if not
309 # explicitly provided.
310 fs_type=$(get_fstype "${iso_device}")
312 # Delete file system type override.
313 iso_name="${iso_name#*:}"
315 # At this point, the backing device should always be
316 # at the very front, so remove that - leaving only the
318 iso_name="$(echo "${iso_name}" | sed "s|^${iso_device}||")"
319 if is_supported_fs ${fs_type}
321 mkdir /run/live/fromiso
322 mount -t "${fs_type}" -o 'ro' "${iso_device}" '/run/live/fromiso'
323 loopdevname=$(setup_loop "/run/live/fromiso/${iso_name}" "loop" "/sys/block/loop*" "" '')
324 devname="${loopdevname}"
326 # Reset device variable, we won't need it.
329 echo "Warning: unable to mount ${iso_device} (type ${fs_type})." >>/boot.log
334 [ -n "$device" ] && devname="$device"
336 [ -e "$devname" ] || continue
338 if [ -n "${LIVE_MEDIA_OFFSET}" ]
340 loopdevname=$(setup_loop "${devname}" "loop" "/sys/block/loop*" "${LIVE_MEDIA_OFFSET}" '')
341 devname="${loopdevname}"
344 fstype=$(get_fstype "${devname}")
346 if is_supported_fs ${fstype}
348 devuid=$(blkid -o value -s UUID "$devname")
349 [ -n "$devuid" ] && grep -qs "\<$devuid\>" /var/lib/live/boot/devices-already-tried-to-mount && continue
351 for _PARAMETER in ${LIVE_BOOT_CMDLINE}
353 case "${_PARAMETER}" in
360 if [ "${PERSISTENCE_FSCK}" = "true" ] || [ "${PERSISTENCE_FSCK}" = "yes" ] || [ "${FORCEFSCK}" = "true" ]
363 if [ "$FORCEFSCK" = "true" ]
369 if [ "$FSCKFIX" = "true" ] || [ "$FSCKFIX" = "yes" ]
374 fsck $fix $force ${devname} >> fsck.log 2>&1
377 mount -t ${fstype} -o ro,noatime "${devname}" ${mountpoint} || continue
378 [ -n "$devuid" ] && echo "$devuid" >> /var/lib/live/boot/devices-already-tried-to-mount
380 if [ -n "${FINDISO}" ]
382 if [ -f ${mountpoint}/${FINDISO} ]
385 mkdir -p /run/live/findiso
386 mount -t ${fstype} -o ro,noatime "${devname}" /run/live/findiso
387 loopdevname=$(setup_loop "/run/live/findiso/${FINDISO}" "loop" "/sys/block/loop*" 0 "")
388 devname="${loopdevname}"
389 mount -t iso9660 -o ro,noatime "${devname}" ${mountpoint}
395 if is_live_path ${mountpoint} && \
396 ([ "${skip_uuid_check}" ] || grml_match_bootid ${mountpoint})
401 umount ${mountpoint} 2>/dev/null
405 if [ -n "${LIVE_MEDIA_OFFSET}" ]
407 losetup -d "${loopdevname}"
417 # don't start autodetection before timeout has expired
418 if [ -n "${LIVE_MEDIA_TIMEOUT}" ]
420 if [ "${timeout}" -lt "${LIVE_MEDIA_TIMEOUT}" ]
426 # first look at the one specified in the command line
427 case "${LIVE_MEDIA}" in
429 for sysblock in $(removable_usb_dev "sys")
431 for dev in $(subdevices "${sysblock}")
433 if check_dev "${dev}"
443 for sysblock in $(removable_dev "sys")
445 for dev in $(subdevices "${sysblock}")
447 if check_dev "${dev}"
457 if [ ! -z "${LIVE_MEDIA}" ]
459 if check_dev "null" "${LIVE_MEDIA}" "skip_uuid_check"
467 # or do the scan of block devices
468 # prefer removable devices over non-removable devices, so scan them first
469 devices_to_scan="$(removable_dev 'sys') $(non_removable_dev 'sys')"
471 for sysblock in $devices_to_scan
473 devname=$(sys2dev "${sysblock}")
474 [ -e "$devname" ] || continue
475 fstype=$(get_fstype "${devname}")
477 if /lib/udev/cdrom_id ${devname} > /dev/null
479 if check_dev "null" "${devname}"
483 elif is_nice_device "${sysblock}"
485 for dev in $(subdevices "${sysblock}")
487 if check_dev "${dev}"
498 is_in_list_separator_helper ()
500 local sep element list
506 echo ${list} | grep -qe "^\(.*${sep}\)\?${element}\(${sep}.*\)\?$"
509 is_in_space_sep_list ()
514 is_in_list_separator_helper "[[:space:]]" "${element}" "${*}"
517 is_in_comma_sep_list ()
522 is_in_list_separator_helper "," "${element}" "${*}"
528 echo "/dev/$(udevadm info -q name -p ${sysdev} 2>/dev/null|| echo ${sysdev##*/})"
536 for dev in "${sysblock}"/* "${sysblock}"
538 if [ -e "${dev}/dev" ]
549 black_listed_devices="${1}"
550 white_listed_devices="${2}"
552 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "loop|ram|fd")
554 fulldevname=$(sys2dev "${sysblock}")
556 if is_in_space_sep_list ${fulldevname} ${black_listed_devices} || \
557 [ -n "${white_listed_devices}" ] && \
558 ! is_in_space_sep_list ${fulldevname} ${white_listed_devices}
560 # skip this device entirely
564 for dev in $(subdevices "${sysblock}")
566 devname=$(sys2dev "${dev}")
568 if is_in_space_sep_list ${devname} ${black_listed_devices}
570 # skip this subdevice
583 # Validate input first
584 if [ -z "${fstype}" ]
589 # get_fstype might report "unknown" or "swap", ignore it as no such kernel module exists
590 if [ "${fstype}" = "unknown" ] || [ "${fstype}" = "swap" ]
595 # Try to look if it is already supported by the kernel
596 # For ntfs, since user space program ntfs-3g will be used. Check ntfs-3g instead of kernel module.
597 if [ "${fstype}" = "ntfs" ]; then
598 if type ntfs-3g >/dev/null 2>&1; then
604 if grep -q ${fstype} /proc/filesystems
608 # Then try to add support for it the gentle way using the initramfs capabilities
609 modprobe -q -b ${fstype}
610 if grep -q ${fstype} /proc/filesystems
613 # Then try the hard way if /root is already reachable
615 kmodule="/root/lib/modules/`uname -r`/${fstype}/${fstype}.ko"
616 if [ -e "${kmodule}" ]
619 if grep -q ${fstype} /proc/filesystems
632 blkid -s TYPE -o value $1 2>/dev/null
639 grep -m1 "^${device} " /proc/mounts | cut -f2 -d ' '
644 # remove all unnecessary /:s in the path, including last one (except
645 # if path is just "/")
646 echo ${1} | sed 's|//\+|/|g' | sed 's|^\(.*[^/]\)/$|\1|'
649 what_is_mounted_on ()
652 dir="$(trim_path ${1})"
653 grep -m1 "^[^ ]\+ ${dir} " /proc/mounts | cut -d' ' -f1
658 local reference targets owner
662 owner=$(stat -c %u:%g "${reference}")
663 chown -h ${owner} ${targets}
668 local reference targets rights
672 rights=$(stat -c %a "${reference}")
673 chmod ${rights} ${targets}
689 mounts="$(awk '{print $2}' /proc/mounts)"
690 testpath="$(realpath ${testpath})"
694 if echo "${mounts}" | grep -qs "^${testpath}"
696 set -- $(echo "${mounts}" | grep "^${testpath}" | lastline)
700 testpath=$(dirname $testpath)
707 # Returns used/free fs kbytes + 5% more
708 # You could pass a block device as ${1} or the mount point as ${2}
714 if [ -z "${mountp}" ]
716 mountp="$(where_is_mounted ${dev})"
718 if [ -z "${mountp}" ]
720 mountp="/mnt/tmp_fs_size"
723 mount -t $(get_fstype "${dev}") -o ro "${dev}" "${mountp}" || log_warning_msg "cannot mount -t $(get_fstype ${dev}) -o ro ${dev} ${mountp}"
729 if [ "${used}" = "used" ]
731 size=$(du -ks ${mountp} | cut -f1)
732 size=$(expr ${size} + ${size} / 20 ) # FIXME: 5% more to be sure
735 size="$(df -kP | grep -s ${mountp} | awk '{print $4}')"
738 if [ -n "${doumount}" ]
740 umount "${mountp}" || log_warning_msg "cannot umount ${mountp}"
750 if [ -x /bin/loadkeys -a -r /etc/boottime.kmap.gz ]
752 loadkeys --quiet /etc/boottime.kmap.gz
758 local fspath module pattern offset encryption readonly
766 # the output of setup_loop is evaluated in other functions,
767 # modprobe leaks kernel options like "libata.dma=0"
768 # as "options libata dma=0" on stdout, causing serious
769 # problems therefor, so instead always avoid output to stdout
770 modprobe -q -b "${module}" 1>/dev/null
774 for loopdev in ${pattern}
776 if [ "$(cat ${loopdev}/size)" -eq 0 ]
778 dev=$(sys2dev "${loopdev}")
781 if [ -n "${readonly}" ]
783 if losetup --help 2>&1 | grep -q -- "-r\b"
785 options="${options} -r"
789 if [ -n "${offset}" ] && [ 0 -lt "${offset}" ]
791 options="${options} -o ${offset}"
794 if [ -z "${encryption}" ]
796 losetup ${options} "${dev}" "${fspath}"
798 # Loop AES encryption
803 echo -n "Enter passphrase for root filesystem: " >&6
805 echo "${passphrase}" > /tmp/passphrase
807 exec 9</tmp/passphrase
808 losetup ${options} -e "${encryption}" -p 9 "${dev}" "${fspath}"
811 rm -f /tmp/passphrase
813 if [ 0 -eq ${error} ]
820 echo -n "There was an error decrypting the root filesystem ... Retry? [Y/n] " >&6
823 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
836 panic "No loop devices available"
846 old_mountp="$(where_is_mounted ${dev})"
848 if [ -n "${old_mountp}" ]
850 if [ "${opts}" != "ro" ]
852 mount -o remount,"${opts}" "${dev}" "${old_mountp}" || panic "Remounting ${dev} ${opts} on ${old_mountp} failed"
855 mount -o bind "${old_mountp}" "${mountp}" || panic "Cannot bind-mount ${old_mountp} on ${mountp}"
857 if [ -z "${fstype}" ]
859 fstype=$(get_fstype "${dev}")
861 mount -t "${fstype}" -o "${opts}" "${dev}" "${mountp}" || \
862 ( echo "SKIPPING: Cannot mount ${dev} on ${mountp}, fstype=${fstype}, options=${opts}" >> boot.log && return 0 )
866 # Try to mount $device to the place expected by live-boot. If $device
867 # is already mounted somewhere, move it to the expected place. If $device
868 # ends with a "/" this is a directory path.
869 # If we're only probing $device (to check if it has custom persistence)
870 # $probe should be set, which suppresses warnings upon failure. On
871 # success, print the mount point for $device.
872 mount_persistence_media ()
874 local device probe backing old_backing fstype mount_opts
878 # get_custom_mounts() might call this with a directory path instead
879 # of a block device path. This means we have found sub-directory path
880 # underneath /run/live/persistence, so we're done
881 if [ -d "${device}" ]
887 if [ ! -b "${device}" ]
892 backing="/run/live/persistence/$(basename ${device})"
894 mkdir -p "${backing}"
895 old_backing="$(where_is_mounted ${device})"
896 if [ -z "${old_backing}" ]
898 fstype="$(get_fstype ${device})"
899 mount_opts="rw,noatime"
900 if [ -n "${PERSISTENCE_READONLY}" ]
902 mount_opts="ro,noatime"
904 if mount -t "${fstype}" -o "${mount_opts}" "${device}" "${backing}" >/dev/null 2>&1
909 [ -z "${probe}" ] && log_warning_msg "Failed to mount persistence media ${device}"
913 elif [ "${backing}" != "${old_backing}" ]
915 if ! mount -o move ${old_backing} ${backing} >/dev/null
917 [ -z "${probe}" ] && log_warning_msg "Failed to move persistence media ${device}"
921 mount_opts="rw,noatime"
922 if [ -n "${PERSISTENCE_READONLY}" ]
924 mount_opts="ro,noatime"
926 if ! mount -o "remount,${mount_opts}" "${backing}" >/dev/null
928 log_warning_msg "Failed to remount persistence media ${device} writable"
929 # Don't unmount or rmdir the new mountpoint in this case
934 # This means that $device has already been mounted on
935 # the place expected by live-boot, so we're done.
941 close_persistence_media ()
945 backing="$(where_is_mounted ${device})"
947 if [ -d "${backing}" ]
949 umount "${backing}" >/dev/null 2>&1
950 rmdir "${backing}" >/dev/null 2>&1
953 if is_active_luks_mapping ${device}
955 cryptsetup luksClose ${device}
962 name="$(basename ${dev})"
964 if [ -n "${PERSISTENCE_READONLY}" ]
966 opts="${opts} --readonly"
969 if cryptsetup status "${name}" >/dev/null 2>&1
971 re="^[[:space:]]*device:[[:space:]]*\([^[:space:]]*\)$"
972 opened_dev=$(cryptsetup status ${name} 2>/dev/null | grep "${re}" | sed "s|${re}|\1|")
973 if [ "${opened_dev}" = "${dev}" ]
975 luks_device="/dev/mapper/${name}"
979 log_warning_msg "Cannot open luks device ${dev} since ${opened_dev} already is opened with its name"
987 if [ -x /bin/plymouth ]
992 case "${_PLYMOUTH}" in
996 cryptkeyscript="plymouth ask-for-password --prompt"
997 # Plymouth will add a : if it is a non-graphical prompt
998 cryptkeyprompt="Please unlock disk ${dev}"
1002 cryptkeyscript="/lib/cryptsetup/askpass"
1003 cryptkeyprompt="Please unlock disk ${dev}: "
1009 $cryptkeyscript "$cryptkeyprompt" | \
1010 cryptsetup -T 1 luksOpen ${dev} ${name} ${opts}
1014 luks_device="/dev/mapper/${name}"
1020 retryprompt="There was an error decrypting ${dev} ... Retry? [Y/n]"
1022 case "${_PLYMOUTH}" in
1024 plymouth display-message --text "${retryprompt}"
1025 answer=$(plymouth watch-keystroke --keys="YNyn")
1029 echo -n "${retryprompt} " >&6
1034 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
1036 case "${_PLYMOUTH}" in
1038 plymouth display-message --text ""
1051 blkid -s PART_ENTRY_NAME -p -o value ${dev} 2>/dev/null
1058 [ "$(blkid -s PART_ENTRY_SCHEME -p -o value ${dev} 2>/dev/null)" = "gpt" ]
1061 probe_for_gpt_name ()
1063 local overlays dev gpt_dev gpt_name
1068 if is_active_luks_mapping ${dev}
1070 # if $dev is an opened luks device, we need to check
1071 # GPT stuff on the backing device
1072 gpt_dev=$(get_luks_backing_device "${dev}")
1075 if ! is_gpt_device ${gpt_dev}
1080 gpt_name=$(get_gpt_name ${gpt_dev})
1081 for label in ${overlays}
1083 if [ "${gpt_name}" = "${label}" ]
1085 echo "${label}=${dev}"
1090 probe_for_fs_label ()
1096 for label in ${overlays}
1098 if [ "$(blkid -s LABEL -o value $dev 2>/dev/null)" = "${label}" ]
1100 echo "${label}=${dev}"
1105 probe_for_file_name ()
1107 local overlays dev ret backing
1112 backing="$(mount_persistence_media ${dev} probe)"
1113 if [ -z "${backing}" ]
1118 for label in ${overlays}
1120 path=${backing}/${PERSISTENCE_PATH}/${label}
1124 loopdev=$(setup_loop "${path}" "loop" "/sys/block/loop*")
1125 ret="${ret} ${label}=${loopdev}"
1133 # unmount and remove mountpoint
1134 umount ${backing} > /dev/null 2>&1 || true
1135 rmdir ${backing} > /dev/null 2>&1 || true
1139 probe_for_directory_name ()
1141 local overlays dev ret backing
1146 backing="$(mount_persistence_media ${dev} probe)"
1147 if [ -z "${backing}" ]
1152 for label in ${overlays}
1154 path=${backing}/${PERSISTENCE_PATH}/${label}
1157 # in this case the "device" ends with a "/"
1158 ret="${ret} ${label}=${backing}/${PERSISTENCE_PATH}/${label%%/}/"
1166 # unmount and remove mountpoint
1167 umount ${backing} > /dev/null 2>&1 || true
1168 rmdir ${backing} > /dev/null 2>&1 || true
1172 find_persistence_media ()
1174 # Scans devices for overlays, and returns a whitespace
1175 # separated list of how to use them. Only overlays with a partition
1176 # label or file name in ${overlays} are returned.
1178 # When scanning a LUKS device, the user will be asked to enter the
1179 # passphrase; on failure to enter it, or if no persistence partitions
1180 # or files were found, the LUKS device is closed.
1182 # For all other cases (overlay partition and overlay file) the
1183 # return value is "${label}=${device}", where ${device} a device that
1184 # can mount the content. In the case of an overlay file, the device
1185 # containing the file will remain mounted as a side-effect.
1187 # No devices in ${black_listed_devices} will be scanned, and if
1188 # ${white_list_devices} is non-empty, only devices in it will be
1191 local overlays white_listed_devices ret black_listed_devices
1193 white_listed_devices="${2}"
1197 # The devices that are hosting the actual live rootfs should not be
1198 # used for persistence storage since otherwise you might mount a
1199 # parent directory on top of a sub-directory of the same filesystem
1200 # in one union together.
1202 black_listed_devices=""
1203 for d in /run/live/rootfs/* /run/live/findiso /run/live/fromiso
1205 black_listed_devices="${black_listed_devices} $(what_is_mounted_on d)"
1208 for dev in $(storage_devices "${black_listed_devices}" "${white_listed_devices}")
1210 local result luks_device
1214 # Check if it's a luks device; we'll have to open the device
1215 # in order to probe any filesystem it contains, like we do
1216 # below. activate_custom_mounts() also depends on that any luks
1217 # device already has been opened.
1218 if is_in_comma_sep_list luks ${PERSISTENCE_ENCRYPTION} && is_luks_partition ${dev}
1220 if luks_device=$(open_luks_device "${dev}")
1222 dev="${luks_device}"
1224 # skip $dev since we failed/chose not to open it
1227 elif ! is_in_comma_sep_list none ${PERSISTENCE_ENCRYPTION}
1229 # skip $dev since we don't allow unencrypted storage
1233 # Probe for matching GPT partition names or filesystem labels
1234 if is_in_comma_sep_list filesystem ${PERSISTENCE_STORAGE}
1236 result=$(probe_for_gpt_name "${overlays}" ${dev})
1237 if [ -n "${result}" ]
1239 ret="${ret} ${result}"
1243 result=$(probe_for_fs_label "${overlays}" ${dev})
1244 if [ -n "${result}" ]
1246 ret="${ret} ${result}"
1251 # Probe for files with matching name on mounted partition
1252 if is_in_comma_sep_list file ${PERSISTENCE_STORAGE}
1254 result=$(probe_for_file_name "${overlays}" ${dev})
1255 if [ -n "${result}" ]
1258 loopdevice=${result##*=}
1259 if is_in_comma_sep_list luks ${PERSISTENCE_ENCRYPTION} && is_luks_partition ${loopdevice}
1263 if luksfile=$(open_luks_device "${loopdevice}")
1265 result=${result%%=*}
1266 result="${result}=${luksfile}"
1268 losetup -d $loopdevice
1272 ret="${ret} ${result}"
1277 # Probe for directory with matching name on mounted partition
1278 if is_in_comma_sep_list directory ${PERSISTENCE_STORAGE}
1280 result=$(probe_for_directory_name "${overlays}" ${dev})
1281 if [ -n "${result}" ]
1283 ret="${ret} ${result}"
1288 # Close luks device if it isn't used
1289 if [ -z "${result}" ] && [ -n "${luks_device}" ] && is_active_luks_mapping "${luks_device}"
1291 cryptsetup luksClose "${luks_device}"
1305 for adaptor in /sys/class/net/*
1307 status="$(cat ${adaptor}/iflink)"
1309 if [ "${status}" -eq 2 ]
1311 mac="$(cat ${adaptor}/address)"
1312 mac="$(echo ${mac} | sed 's/:/-/g' | tr '[a-z]' '[A-Z]')"
1319 is_luks_partition ()
1322 cryptsetup isLuks "${device}" 1>/dev/null 2>&1
1325 is_active_luks_mapping ()
1328 cryptsetup status "${device}" 1>/dev/null 2>&1
1331 get_luks_backing_device ()
1334 cryptsetup status ${device} 2> /dev/null | \
1335 awk '{if ($1 == "device:") print $2}'
1340 output_format="${1}"
1344 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1346 if [ ! -d "${sysblock}" ]; then
1351 if [ "$(cat ${sysblock}/removable)" = "1" ]
1353 if [ -z "${want_usb}" ]
1357 if readlink ${sysblock} | grep -q usb
1364 if [ "${dev_ok}" = "true" ]
1366 case "${output_format}" in
1368 ret="${ret} ${sysblock}"
1371 devname=$(sys2dev "${sysblock}")
1372 ret="${ret} ${devname}"
1381 removable_usb_dev ()
1383 output_format="${1}"
1385 removable_dev "${output_format}" "want_usb"
1388 non_removable_dev ()
1390 output_format="${1}"
1393 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1395 if [ ! -d "${sysblock}" ]; then
1399 if [ "$(cat ${sysblock}/removable)" = "0" ]
1401 case "${output_format}" in
1403 ret="${ret} ${sysblock}"
1406 devname=$(sys2dev "${sysblock}")
1407 ret="${ret} ${devname}"
1418 # create source's directory structure in dest, and recursively
1419 # create symlinks in dest to to all files in source. if mask
1420 # is non-empty, remove mask from all source paths when
1421 # creating links (will be necessary if we change root, which
1422 # live-boot normally does (into $rootmnt)).
1423 local src_dir dest_dir src_transform
1425 # remove multiple /:s and ensure ending on /
1426 src_dir="$(trim_path ${1})/"
1427 dest_dir="$(trim_path ${2})/"
1428 src_transform="${3}"
1430 # This check can only trigger on the inital, non-recursive call since
1431 # we create the destination before recursive calls
1432 if [ ! -d "${dest_dir}" ]
1434 log_warning_msg "Must link_files into a directory"
1438 find "${src_dir}" -mindepth 1 -maxdepth 1 | \
1441 local dest final_src
1442 dest="${dest_dir}$(basename "${src}")"
1445 if [ -z "$(ls -A "${src}")" ]
1449 if [ ! -d "${dest}" ]
1452 chown_ref "${src}" "${dest}"
1453 chmod_ref "${src}" "${dest}"
1455 link_files "${src}" "${dest}" "${src_transform}"
1458 if [ -n "${src_transform}" ]
1460 final_src="$(echo ${final_src} | sed "${src_transform}")"
1462 rm -rf "${dest}" 2> /dev/null
1463 ln -s "${final_src}" "${dest}"
1464 chown_ref "${src}" "${dest}"
1471 local unionmountpoint unionrw unionro
1472 unionmountpoint="${1}" # directory where the union is mounted
1474 unionrw="${1}" # branch where the union changes are stored
1476 unionro="${*}" # space separated list of read-only branches (optional)
1478 case "${UNIONTYPE}" in
1484 unionmountopts="-o noatime,${noxino_opt},dirs=${unionrw}=${rw_opt}"
1485 if [ -n "${unionro}" ]
1487 for rofs in ${unionro}
1489 unionmountopts="${unionmountopts}:${rofs}=${ro_opt}"
1495 # XXX: can unionro be optional? i.e. can overlay skip lowerdir?
1496 if [ -z "${unionro}" ]
1498 panic "overlay needs at least one lower filesystem (read-only branch)."
1500 # Multiple lower layers can now be given using the the colon (":") as a
1501 # separator character between the directory names.
1502 unionro="$(echo ${unionro} | sed -e 's| |:|g')"
1503 # overlayfs requires:
1504 # + a workdir to become mounted
1505 # + workdir and upperdir to reside under the same mount
1506 # + workdir and upperdir to be in separate directories
1507 mkdir -p "${unionrw}/rw"
1508 mkdir -p "${unionrw}/work"
1509 unionmountopts="-o noatime,lowerdir=${unionro},upperdir=${unionrw}/rw,workdir=${unionrw}/work"
1513 mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
1516 get_custom_mounts ()
1518 # Side-effect: leaves $devices with persistence.conf mounted in /run/live/persistence
1519 # Side-effect: prints info to file $custom_mounts
1521 local custom_mounts devices bindings links
1526 bindings="/tmp/bindings.list"
1527 links="/tmp/links.list"
1528 rm -rf ${bindings} ${links} 2> /dev/null
1530 for device in ${devices}
1532 local device_name backing include_list
1533 device_name="$(basename ${device})"
1534 backing=$(mount_persistence_media ${device})
1535 if [ -z "${backing}" ]
1540 if [ -r "${backing}/${persistence_list}" ]
1542 include_list="${backing}/${persistence_list}"
1547 if [ -n "${LIVE_BOOT_DEBUG}" ] && [ -e "${include_list}" ]
1549 cp ${include_list} /run/live/persistence/${persistence_list}.${device_name}
1552 while read dir options # < ${include_list}
1554 if echo ${dir} | grep -qe "^[[:space:]]*\(#.*\)\?$"
1556 # skipping empty or commented lines
1560 if trim_path ${dir} | grep -q -e "^[^/]" -e "^/lib" -e "^/run/live\(/.*\)\?$" -e "^/\(.*/\)\?\.\.\?\(/.*\)\?$"
1562 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."
1566 local opt_source opt_link source full_source full_dest
1569 for opt in $(echo ${options} | tr ',' ' ');
1573 opt_source=${opt#source=}
1581 log_warning_msg "Skipping custom mount with unknown option: ${opt}"
1588 if [ -n "${opt_source}" ]
1590 if echo ${opt_source} | grep -q -e "^/" -e "^\(.*/\)\?\.\.\?\(/.*\)\?$" && [ "${opt_source}" != "." ]
1592 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"
1595 source="${opt_source}"
1599 full_source="$(trim_path ${backing}/${source})"
1600 full_dest="$(trim_path ${rootmnt}/${dir})"
1601 if [ -n "${opt_link}" ]
1603 echo "${device} ${full_source} ${full_dest} ${options}" >> ${links}
1605 echo "${device} ${full_source} ${full_dest} ${options}" >> ${bindings}
1607 done < ${include_list}
1610 # We sort the list according to destination so we're sure that
1611 # we won't hide a previous mount. We also ignore duplicate
1612 # destinations in a more or less arbitrary way.
1613 [ -e "${bindings}" ] && sort -k3 -sbu ${bindings} >> ${custom_mounts} && rm ${bindings}
1615 # After all mounts are considered we add symlinks so they
1616 # won't be hidden by some mount.
1617 [ -e "${links}" ] && cat ${links} >> ${custom_mounts} && rm ${links}
1619 # We need to make sure that no two custom mounts have the same sources
1620 # or are nested; if that is the case, too much weird stuff can happen.
1621 local prev_source prev_dest
1622 prev_source="impossible source" # first iteration must not match
1624 # This sort will ensure that a source /a comes right before a source
1625 # /a/b so we only need to look at the previous source
1626 [ -e ${custom_mounts} ] && sort -k2 -b ${custom_mounts} |
1627 while read device source dest options
1629 if echo ${source} | grep -qe "^${prev_source}\(/.*\)\?$"
1631 panic "Two persistence mounts have the same or nested sources: ${source} on ${dest}, and ${prev_source} on ${prev_dest}"
1633 prev_source=${source}
1638 activate_custom_mounts ()
1640 local custom_mounts used_devices
1641 custom_mounts="${1}" # the ouput from get_custom_mounts()
1644 while read device source dest options # < ${custom_mounts}
1646 local opt_bind opt_link opt_union
1650 for opt in $(echo ${options} | tr ',' ' ');
1655 unset opt_link opt_union
1659 unset opt_bind opt_union
1663 unset opt_bind opt_link
1668 if [ -n "$(what_is_mounted_on "${dest}")" ]
1670 if [ "${dest}" = "${rootmnt}" ]
1674 log_warning_msg "Skipping custom mount ${dest}: $(what_is_mounted_on "${dest}") is already mounted there"
1679 if [ ! -d "${dest}" ]
1681 # create the destination and delete existing files in
1682 # its path that are in the way
1684 for dir in $(echo ${dest} | sed -e 's|/\+| |g')
1686 path=$(trim_path ${path}/${dir})
1694 if echo ${path} | grep -qe "^${rootmnt}/*home/[^/]\+"
1696 # if ${dest} is in /home try fixing proper ownership by assuming that the intended user is the first, which is usually the case
1697 # FIXME: this should really be handled by live-config since we don't know for sure which uid a certain user has until then
1698 chown 1000:1000 ${path}
1704 # if ${source} doesn't exist on our persistence media
1705 # we bootstrap it with $dest from the live filesystem.
1706 # this both makes sense and is critical if we're
1707 # dealing with /etc or other system dir.
1708 if [ ! -d "${source}" ]
1710 if [ -n "${PERSISTENCE_READONLY}" ]
1713 elif [ -n "${opt_union}" ] || [ -n "${opt_link}" ]
1715 # unions and don't need to be bootstrapped
1716 # link dirs can't be bootstrapped in a sensible way
1717 mkdir -p "${source}"
1718 chown_ref "${dest}" "${source}"
1719 chmod_ref "${dest}" "${source}"
1720 elif [ -n "${opt_bind}" ]
1722 # ensure that $dest is not copied *into* $source
1723 mkdir -p "$(dirname ${source})"
1724 cp -a "${dest}" "${source}"
1728 # XXX: If CONFIG_AUFS_ROBR is added to the Debian kernel we can
1729 # ignore the loop below and set rootfs_dest_backing=$dest
1730 local rootfs_dest_backing
1731 rootfs_dest_backing=""
1732 if [ -n "${opt_link}" ] || [ -n "${opt_union}" ]
1734 for d in /run/live/rootfs/*
1736 if [ -n "${rootmnt}" ]
1738 fs="${d}/$(echo ${dest} | sed -e "s|${rootmnt}||")"
1744 rootfs_dest_backing="${rootfs_dest_backing} ${fs}"
1749 local cow_dir links_source
1750 if [ -n "${opt_link}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1752 link_files ${source} ${dest} ""
1753 elif [ -n "${opt_link}" ] && [ -n "${PERSISTENCE_READONLY}" ]
1755 mkdir -p /run/live/persistence
1756 links_source=$(mktemp -d /run/live/persistence/links-source-XXXXXX)
1757 chown_ref ${source} ${links_source}
1758 chmod_ref ${source} ${links_source}
1759 # We put the cow dir in the below strange place to
1760 # make it absolutely certain that the link source
1761 # has its own directory and isn't nested with some
1762 # other custom mount (if so that mount's files would
1763 # be linked, causing breakage.
1764 cow_dir="/run/live/overlay/run/live/persistence/$(basename ${links_source})"
1766 chown_ref "${source}" "${cow_dir}"
1767 chmod_ref "${source}" "${cow_dir}"
1768 do_union ${links_source} ${cow_dir} ${source} ${rootfs_dest_backing}
1769 link_files ${links_source} ${dest} "s|^${rootmnt}||"
1770 elif [ -n "${opt_union}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1772 do_union ${dest} ${source} ${rootfs_dest_backing}
1773 elif [ -n "${opt_bind}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1775 mount -o bind "${source}" "${dest}"
1776 elif [ -n "${opt_bind}" -o -n "${opt_union}" ] && [ -n "${PERSISTENCE_READONLY}" ]
1778 # bind-mount and union mount are handled the same
1779 # in read-only mode, but note that rootfs_dest_backing
1780 # is non-empty (and necessary) only for unions
1781 cow_dir="/run/live/overlay/${dest}"
1782 if [ -e "${cow_dir}" ] && [ -z "${opt_link}" ]
1784 # If an earlier custom mount has files here
1785 # it will "block" the current mount's files
1786 # which is undesirable
1790 chown_ref "${source}" "${cow_dir}"
1791 chmod_ref "${source}" "${cow_dir}"
1792 if [ "${UNIONTYPE}" = "overlay" ]
1794 # When we use overlay we add the "/rw" postfix to our source when using it
1795 # as upper layer. Therefore we also have to add it here when using it as
1797 source="${source}/rw"
1799 do_union ${dest} ${cow_dir} ${source} ${rootfs_dest_backing}
1802 PERSISTENCE_IS_ON="1"
1803 export PERSISTENCE_IS_ON
1805 if echo ${used_devices} | grep -qve "^\(.* \)\?${device}\( .*\)\?$"
1807 used_devices="${used_devices} ${device}"
1809 done < ${custom_mounts}
1811 echo ${used_devices}
1818 [ $(stat -fc%d:%D "${directory}") != $(stat -fc%d:%D "${directory}/..") ]