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 # not a block device, check if it's an iso file, for
124 # example an ISO when booting on an ONIE system
125 if echo "${FROMISO}" | grep -q "\.iso$"
127 fs_type=$(get_fstype "${FROMISO}")
128 if is_supported_fs ${fs_type}
130 mkdir /run/live/fromiso
131 mount -t $fs_type "${FROMISO}" /run/live/fromiso
134 echo "Warning: unable to mount ${FROMISO}." >>/boot.log
136 devname="/run/live/fromiso"
139 echo "Warning: device for bootoption fromiso= ($FROMISO) not found.">>/boot.log
142 fs_type=$(get_fstype "${ISO_DEVICE}")
143 if is_supported_fs ${fs_type}
145 mkdir /run/live/fromiso
146 mount -t $fs_type "$ISO_DEVICE" /run/live/fromiso
147 ISO_NAME="$(echo $FROMISO | sed "s|$ISO_DEVICE||")"
148 loopdevname=$(setup_loop "/run/live/fromiso/${ISO_NAME}" "loop" "/sys/block/loop*" "" '')
149 devname="${loopdevname}"
151 echo "Warning: unable to mount $ISO_DEVICE." >>/boot.log
156 if [ -z "${devname}" ]
158 devname=$(sys2dev "${sysdev}")
161 if [ -d "${devname}" ]
163 mount -o bind "${devname}" $mountpoint || continue
165 if is_live_path $mountpoint
175 for device in ${devname}
180 if [ -x /scripts/local-top/lvm2 ]
182 ROOT="$device" resume="" /scripts/local-top/lvm2 >>/boot.log
187 # Adding raid support
188 if [ -x /scripts/local-top/mdadm ]
190 [ -r /conf/conf.d/md ] && cp /conf/conf.d/md /conf/conf.d/md.orig
191 echo "MD_DEVS=$device " >> /conf/conf.d/md
192 /scripts/local-top/mdadm >>/boot.log
193 [ -r /conf/conf.d/md.orig ] && mv /conf/conf.d/md.orig /conf/conf.d/md
200 [ -n "$device" ] && devname="$device"
202 [ -e "$devname" ] || continue
204 if [ -n "${LIVE_MEDIA_OFFSET}" ]
206 loopdevname=$(setup_loop "${devname}" "loop" "/sys/block/loop*" "${LIVE_MEDIA_OFFSET}" '')
207 devname="${loopdevname}"
210 fstype=$(get_fstype "${devname}")
212 if is_supported_fs ${fstype}
214 devuid=$(blkid -o value -s UUID "$devname")
215 [ -n "$devuid" ] && grep -qs "\<$devuid\>" /var/lib/live/boot/devices-already-tried-to-mount && continue
217 for _PARAMETER in ${LIVE_BOOT_CMDLINE}
219 case "${_PARAMETER}" in
226 if [ "${PERSISTENCE_FSCK}" = "true" ] || [ "${PERSISTENCE_FSCK}" = "yes" ] || [ "${FORCEFSCK}" = "true" ]
229 if [ "$FORCEFSCK" = "true" ]
235 if [ "$FSCKFIX" = "true" ] || [ "$FSCKFIX" = "yes" ]
240 fsck $fix $force ${devname} >> fsck.log 2>&1
243 mount -t ${fstype} -o ro,noatime "${devname}" ${mountpoint} || continue
244 [ -n "$devuid" ] && echo "$devuid" >> /var/lib/live/boot/devices-already-tried-to-mount
246 if [ -n "${FINDISO}" ]
248 if [ -f ${mountpoint}/${FINDISO} ]
251 mkdir -p /run/live/findiso
252 mount -t ${fstype} -o ro,noatime "${devname}" /run/live/findiso
253 loopdevname=$(setup_loop "/run/live/findiso/${FINDISO}" "loop" "/sys/block/loop*" 0 "")
254 devname="${loopdevname}"
255 mount -t iso9660 -o ro,noatime "${devname}" ${mountpoint}
261 if is_live_path ${mountpoint} && \
262 ([ "${skip_uuid_check}" ] || matches_uuid ${mountpoint})
267 umount ${mountpoint} 2>/dev/null
271 if [ -n "${LIVE_MEDIA_OFFSET}" ]
273 losetup -d "${loopdevname}"
283 # don't start autodetection before timeout has expired
284 if [ -n "${LIVE_MEDIA_TIMEOUT}" ]
286 if [ "${timeout}" -lt "${LIVE_MEDIA_TIMEOUT}" ]
292 # first look at the one specified in the command line
293 case "${LIVE_MEDIA}" in
295 for sysblock in $(removable_usb_dev "sys")
297 for dev in $(subdevices "${sysblock}")
299 if check_dev "${dev}"
309 for sysblock in $(removable_dev "sys")
311 for dev in $(subdevices "${sysblock}")
313 if check_dev "${dev}"
323 if [ ! -z "${LIVE_MEDIA}" ]
325 if check_dev "null" "${LIVE_MEDIA}" "skip_uuid_check"
333 # or do the scan of block devices
334 # prefer removable devices over non-removable devices, so scan them first
335 devices_to_scan="$(removable_dev 'sys') $(non_removable_dev 'sys')"
337 for sysblock in $devices_to_scan
339 devname=$(sys2dev "${sysblock}")
340 [ -e "$devname" ] || continue
341 fstype=$(get_fstype "${devname}")
343 if /lib/udev/cdrom_id ${devname} > /dev/null
345 if check_dev "null" "${devname}"
349 elif is_nice_device "${sysblock}"
351 for dev in $(subdevices "${sysblock}")
353 if check_dev "${dev}"
358 elif [ "${fstype}" = "squashfs" -o \
359 "${fstype}" = "btrfs" -o \
360 "${fstype}" = "ext2" -o \
361 "${fstype}" = "ext3" -o \
362 "${fstype}" = "ext4" -o \
363 "${fstype}" = "jffs2" ]
365 # This is an ugly hack situation, the block device has
366 # an image directly on it. It's hopefully
367 # live-boot, so take it and run with it.
368 ln -s "${devname}" "${devname}.${fstype}"
369 echo "${devname}.${fstype}"
377 is_in_list_separator_helper ()
379 local sep element list
385 echo ${list} | grep -qe "^\(.*${sep}\)\?${element}\(${sep}.*\)\?$"
388 is_in_space_sep_list ()
393 is_in_list_separator_helper "[[:space:]]" "${element}" "${*}"
396 is_in_comma_sep_list ()
401 is_in_list_separator_helper "," "${element}" "${*}"
407 echo "/dev/$(udevadm info -q name -p ${sysdev} 2>/dev/null|| echo ${sysdev##*/})"
415 for dev in "${sysblock}"/* "${sysblock}"
417 if [ -e "${dev}/dev" ]
428 black_listed_devices="${1}"
429 white_listed_devices="${2}"
431 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "loop|ram|fd")
433 fulldevname=$(sys2dev "${sysblock}")
435 if is_in_space_sep_list ${fulldevname} ${black_listed_devices} || \
436 [ -n "${white_listed_devices}" ] && \
437 ! is_in_space_sep_list ${fulldevname} ${white_listed_devices}
439 # skip this device entirely
443 for dev in $(subdevices "${sysblock}")
445 devname=$(sys2dev "${dev}")
447 if is_in_space_sep_list ${devname} ${black_listed_devices}
449 # skip this subdevice
462 # Validate input first
463 if [ -z "${fstype}" ]
468 # get_fstype might report "unknown" or "swap", ignore it as no such kernel module exists
469 if [ "${fstype}" = "unknown" ] || [ "${fstype}" = "swap" ]
474 # Try to look if it is already supported by the kernel
475 # For ntfs, since user space program ntfs-3g will be used. Check ntfs-3g instead of kernel module.
476 if [ "${fstype}" = "ntfs" ]; then
477 if type ntfs-3g >/dev/null 2>&1; then
483 if grep -q ${fstype} /proc/filesystems
487 # Then try to add support for it the gentle way using the initramfs capabilities
488 modprobe -q -b ${fstype}
489 if grep -q ${fstype} /proc/filesystems
492 # Then try the hard way if /root is already reachable
494 kmodule="/root/lib/modules/`uname -r`/${fstype}/${fstype}.ko"
495 if [ -e "${kmodule}" ]
498 if grep -q ${fstype} /proc/filesystems
511 blkid -s TYPE -o value $1 2>/dev/null
518 grep -m1 "^${device} " /proc/mounts | cut -f2 -d ' '
523 # remove all unnecessary /:s in the path, including last one (except
524 # if path is just "/")
525 echo ${1} | sed 's|//\+|/|g' | sed 's|^\(.*[^/]\)/$|\1|'
528 what_is_mounted_on ()
531 dir="$(trim_path ${1})"
532 grep -m1 "^[^ ]\+ ${dir} " /proc/mounts | cut -d' ' -f1
537 local reference targets owner
541 owner=$(stat -c %u:%g "${reference}")
542 chown -h ${owner} ${targets}
547 local reference targets rights
551 rights=$(stat -c %a "${reference}")
552 chmod ${rights} ${targets}
568 mounts="$(awk '{print $2}' /proc/mounts)"
569 testpath="$(realpath ${testpath})"
573 if echo "${mounts}" | grep -qs "^${testpath}"
575 set -- $(echo "${mounts}" | grep "^${testpath}" | lastline)
579 testpath=$(dirname $testpath)
586 # Returns used/free fs kbytes + 5% more
587 # You could pass a block device as ${1} or the mount point as ${2}
593 if [ -z "${mountp}" ]
595 mountp="$(where_is_mounted ${dev})"
597 if [ -z "${mountp}" ]
599 mountp="/mnt/tmp_fs_size"
602 mount -t $(get_fstype "${dev}") -o ro "${dev}" "${mountp}" || log_warning_msg "cannot mount -t $(get_fstype ${dev}) -o ro ${dev} ${mountp}"
608 if [ "${used}" = "used" ]
610 size=$(du -ks ${mountp} | cut -f1)
611 size=$(expr ${size} + ${size} / 20 ) # FIXME: 5% more to be sure
614 size="$(df -kP | grep -s ${mountp} | awk '{print $4}')"
617 if [ -n "${doumount}" ]
619 umount "${mountp}" || log_warning_msg "cannot umount ${mountp}"
629 if [ -x /bin/loadkeys -a -r /etc/boottime.kmap.gz ]
631 loadkeys --quiet /etc/boottime.kmap.gz
637 local fspath module pattern offset encryption readonly
645 # the output of setup_loop is evaluated in other functions,
646 # modprobe leaks kernel options like "libata.dma=0"
647 # as "options libata dma=0" on stdout, causing serious
648 # problems therefor, so instead always avoid output to stdout
649 modprobe -q -b "${module}" 1>/dev/null
653 for loopdev in ${pattern}
655 if [ "$(cat ${loopdev}/size)" -eq 0 ]
657 dev=$(sys2dev "${loopdev}")
660 if [ -n "${readonly}" ]
662 if losetup --help 2>&1 | grep -q -- "-r\b"
664 options="${options} -r"
668 if [ -n "${offset}" ] && [ 0 -lt "${offset}" ]
670 options="${options} -o ${offset}"
673 if [ -z "${encryption}" ]
675 losetup ${options} "${dev}" "${fspath}"
677 # Loop AES encryption
682 echo -n "Enter passphrase for root filesystem: " >&6
684 echo "${passphrase}" > /tmp/passphrase
686 exec 9</tmp/passphrase
687 losetup ${options} -e "${encryption}" -p 9 "${dev}" "${fspath}"
690 rm -f /tmp/passphrase
692 if [ 0 -eq ${error} ]
699 echo -n "There was an error decrypting the root filesystem ... Retry? [Y/n] " >&6
702 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
715 panic "No loop devices available"
725 old_mountp="$(where_is_mounted ${dev})"
727 if [ -n "${old_mountp}" ]
729 if [ "${opts}" != "ro" ]
731 mount -o remount,"${opts}" "${dev}" "${old_mountp}" || panic "Remounting ${dev} ${opts} on ${old_mountp} failed"
734 mount -o bind "${old_mountp}" "${mountp}" || panic "Cannot bind-mount ${old_mountp} on ${mountp}"
736 if [ -z "${fstype}" ]
738 fstype=$(get_fstype "${dev}")
740 mount -t "${fstype}" -o "${opts}" "${dev}" "${mountp}" || \
741 ( echo "SKIPPING: Cannot mount ${dev} on ${mountp}, fstype=${fstype}, options=${opts}" > boot.log && return 0 )
745 # Try to mount $device to the place expected by live-boot. If $device
746 # is already mounted somewhere, move it to the expected place. If $device
747 # ends with a "/" this is a directory path.
748 # If we're only probing $device (to check if it has custom persistence)
749 # $probe should be set, which suppresses warnings upon failure. On
750 # success, print the mount point for $device.
751 mount_persistence_media ()
753 local device probe backing old_backing fstype mount_opts
757 # get_custom_mounts() might call this with a directory path instead
758 # of a block device path. This means we have found sub-directory path
759 # underneath /run/live/persistence, so we're done
760 if [ -d "${device}" ]
766 if [ ! -b "${device}" ]
771 backing="/run/live/persistence/$(basename ${device})"
773 mkdir -p "${backing}"
774 old_backing="$(where_is_mounted ${device})"
775 if [ -z "${old_backing}" ]
777 fstype="$(get_fstype ${device})"
778 mount_opts="rw,noatime"
779 if [ -n "${PERSISTENCE_READONLY}" ]
781 mount_opts="ro,noatime"
783 if mount -t "${fstype}" -o "${mount_opts}" "${device}" "${backing}" >/dev/null 2>&1
788 [ -z "${probe}" ] && log_warning_msg "Failed to mount persistence media ${device}"
792 elif [ "${backing}" != "${old_backing}" ]
794 if ! mount -o move ${old_backing} ${backing} >/dev/null
796 [ -z "${probe}" ] && log_warning_msg "Failed to move persistence media ${device}"
800 mount_opts="rw,noatime"
801 if [ -n "${PERSISTENCE_READONLY}" ]
803 mount_opts="ro,noatime"
805 if ! mount -o "remount,${mount_opts}" "${backing}" >/dev/null
807 log_warning_msg "Failed to remount persistence media ${device} writable"
808 # Don't unmount or rmdir the new mountpoint in this case
813 # This means that $device has already been mounted on
814 # the place expected by live-boot, so we're done.
820 close_persistence_media ()
824 backing="$(where_is_mounted ${device})"
826 if [ -d "${backing}" ]
828 umount "${backing}" >/dev/null 2>&1
829 rmdir "${backing}" >/dev/null 2>&1
832 if is_active_luks_mapping ${device}
834 cryptsetup luksClose ${device}
841 name="$(basename ${dev})"
843 if [ -n "${PERSISTENCE_READONLY}" ]
845 opts="${opts} --readonly"
848 if cryptsetup status "${name}" >/dev/null 2>&1
850 re="^[[:space:]]*device:[[:space:]]*\([^[:space:]]*\)$"
851 opened_dev=$(cryptsetup status ${name} 2>/dev/null | grep "${re}" | sed "s|${re}|\1|")
852 if [ "${opened_dev}" = "${dev}" ]
854 luks_device="/dev/mapper/${name}"
858 log_warning_msg "Cannot open luks device ${dev} since ${opened_dev} already is opened with its name"
866 if [ -x /bin/plymouth ]
871 case "${_PLYMOUTH}" in
875 cryptkeyscript="plymouth ask-for-password --prompt"
876 # Plymouth will add a : if it is a non-graphical prompt
877 cryptkeyprompt="Please unlock disk ${dev}"
881 cryptkeyscript="/lib/cryptsetup/askpass"
882 cryptkeyprompt="Please unlock disk ${dev}: "
888 $cryptkeyscript "$cryptkeyprompt" | \
889 cryptsetup -T 1 luksOpen ${dev} ${name} ${opts}
893 luks_device="/dev/mapper/${name}"
899 retryprompt="There was an error decrypting ${dev} ... Retry? [Y/n]"
901 case "${_PLYMOUTH}" in
903 plymouth display-message --text "${retryprompt}"
904 answer=$(plymouth watch-keystroke --keys="YNyn")
908 echo -n "${retryprompt} " >&6
913 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
915 case "${_PLYMOUTH}" in
917 plymouth display-message --text ""
930 blkid -s PART_ENTRY_NAME -p -o value ${dev} 2>/dev/null
937 [ "$(blkid -s PART_ENTRY_SCHEME -p -o value ${dev} 2>/dev/null)" = "gpt" ]
940 probe_for_gpt_name ()
942 local overlays dev gpt_dev gpt_name
947 if is_active_luks_mapping ${dev}
949 # if $dev is an opened luks device, we need to check
950 # GPT stuff on the backing device
951 gpt_dev=$(get_luks_backing_device "${dev}")
954 if ! is_gpt_device ${gpt_dev}
959 gpt_name=$(get_gpt_name ${gpt_dev})
960 for label in ${overlays}
962 if [ "${gpt_name}" = "${label}" ]
964 echo "${label}=${dev}"
969 probe_for_fs_label ()
975 for label in ${overlays}
977 if [ "$(blkid -s LABEL -o value $dev 2>/dev/null)" = "${label}" ]
979 echo "${label}=${dev}"
984 probe_for_file_name ()
986 local overlays dev ret backing
991 backing="$(mount_persistence_media ${dev} probe)"
992 if [ -z "${backing}" ]
997 for label in ${overlays}
999 path=${backing}/${PERSISTENCE_PATH}/${label}
1003 loopdev=$(setup_loop "${path}" "loop" "/sys/block/loop*")
1004 ret="${ret} ${label}=${loopdev}"
1012 # unmount and remove mountpoint
1013 umount ${backing} > /dev/null 2>&1 || true
1014 rmdir ${backing} > /dev/null 2>&1 || true
1018 probe_for_directory_name ()
1020 local overlays dev ret backing
1025 backing="$(mount_persistence_media ${dev} probe)"
1026 if [ -z "${backing}" ]
1031 for label in ${overlays}
1033 path=${backing}/${PERSISTENCE_PATH}/${label}
1036 # in this case the "device" ends with a "/"
1037 ret="${ret} ${label}=${backing}/${PERSISTENCE_PATH}/${label%%/}/"
1045 # unmount and remove mountpoint
1046 umount ${backing} > /dev/null 2>&1 || true
1047 rmdir ${backing} > /dev/null 2>&1 || true
1051 find_persistence_media ()
1053 # Scans devices for overlays, and returns a whitespace
1054 # separated list of how to use them. Only overlays with a partition
1055 # label or file name in ${overlays} are returned.
1057 # When scanning a LUKS device, the user will be asked to enter the
1058 # passphrase; on failure to enter it, or if no persistence partitions
1059 # or files were found, the LUKS device is closed.
1061 # For all other cases (overlay partition and overlay file) the
1062 # return value is "${label}=${device}", where ${device} a device that
1063 # can mount the content. In the case of an overlay file, the device
1064 # containing the file will remain mounted as a side-effect.
1066 # No devices in ${black_listed_devices} will be scanned, and if
1067 # ${white_list_devices} is non-empty, only devices in it will be
1070 local overlays white_listed_devices ret black_listed_devices
1072 white_listed_devices="${2}"
1076 # The devices that are hosting the actual live rootfs should not be
1077 # used for persistence storage since otherwise you might mount a
1078 # parent directory on top of a sub-directory of the same filesystem
1079 # in one union together.
1081 black_listed_devices=""
1082 for d in /run/live/rootfs/* /run/live/findiso /run/live/fromiso
1084 black_listed_devices="${black_listed_devices} $(what_is_mounted_on d)"
1087 for dev in $(storage_devices "${black_listed_devices}" "${white_listed_devices}")
1089 local result luks_device
1093 # Check if it's a luks device; we'll have to open the device
1094 # in order to probe any filesystem it contains, like we do
1095 # below. activate_custom_mounts() also depends on that any luks
1096 # device already has been opened.
1097 if is_in_comma_sep_list luks ${PERSISTENCE_ENCRYPTION} && is_luks_partition ${dev}
1099 if luks_device=$(open_luks_device "${dev}")
1101 dev="${luks_device}"
1103 # skip $dev since we failed/chose not to open it
1106 elif ! is_in_comma_sep_list none ${PERSISTENCE_ENCRYPTION}
1108 # skip $dev since we don't allow unencrypted storage
1112 # Probe for matching GPT partition names or filesystem labels
1113 if is_in_comma_sep_list filesystem ${PERSISTENCE_STORAGE}
1115 result=$(probe_for_gpt_name "${overlays}" ${dev})
1116 if [ -n "${result}" ]
1118 ret="${ret} ${result}"
1122 result=$(probe_for_fs_label "${overlays}" ${dev})
1123 if [ -n "${result}" ]
1125 ret="${ret} ${result}"
1130 # Probe for files with matching name on mounted partition
1131 if is_in_comma_sep_list file ${PERSISTENCE_STORAGE}
1133 result=$(probe_for_file_name "${overlays}" ${dev})
1134 if [ -n "${result}" ]
1137 loopdevice=${result##*=}
1138 if is_in_comma_sep_list luks ${PERSISTENCE_ENCRYPTION} && is_luks_partition ${loopdevice}
1142 if luksfile=$(open_luks_device "${loopdevice}")
1144 result=${result%%=*}
1145 result="${result}=${luksfile}"
1147 losetup -d $loopdevice
1151 ret="${ret} ${result}"
1156 # Probe for directory with matching name on mounted partition
1157 if is_in_comma_sep_list directory ${PERSISTENCE_STORAGE}
1159 result=$(probe_for_directory_name "${overlays}" ${dev})
1160 if [ -n "${result}" ]
1162 ret="${ret} ${result}"
1167 # Close luks device if it isn't used
1168 if [ -z "${result}" ] && [ -n "${luks_device}" ] && is_active_luks_mapping "${luks_device}"
1170 cryptsetup luksClose "${luks_device}"
1184 for adaptor in /sys/class/net/*
1186 status="$(cat ${adaptor}/iflink)"
1188 if [ "${status}" -eq 2 ]
1190 mac="$(cat ${adaptor}/address)"
1191 mac="$(echo ${mac} | sed 's/:/-/g' | tr '[a-z]' '[A-Z]')"
1198 is_luks_partition ()
1201 cryptsetup isLuks "${device}" 1>/dev/null 2>&1
1204 is_active_luks_mapping ()
1207 cryptsetup status "${device}" 1>/dev/null 2>&1
1210 get_luks_backing_device ()
1213 cryptsetup status ${device} 2> /dev/null | \
1214 awk '{if ($1 == "device:") print $2}'
1219 output_format="${1}"
1223 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1225 if [ ! -d "${sysblock}" ]; then
1230 if [ "$(cat ${sysblock}/removable)" = "1" ]
1232 if [ -z "${want_usb}" ]
1236 if readlink ${sysblock} | grep -q usb
1243 if [ "${dev_ok}" = "true" ]
1245 case "${output_format}" in
1247 ret="${ret} ${sysblock}"
1250 devname=$(sys2dev "${sysblock}")
1251 ret="${ret} ${devname}"
1260 removable_usb_dev ()
1262 output_format="${1}"
1264 removable_dev "${output_format}" "want_usb"
1267 non_removable_dev ()
1269 output_format="${1}"
1272 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1274 if [ ! -d "${sysblock}" ]; then
1278 if [ "$(cat ${sysblock}/removable)" = "0" ]
1280 case "${output_format}" in
1282 ret="${ret} ${sysblock}"
1285 devname=$(sys2dev "${sysblock}")
1286 ret="${ret} ${devname}"
1297 # create source's directory structure in dest, and recursively
1298 # create symlinks in dest to to all files in source. if mask
1299 # is non-empty, remove mask from all source paths when
1300 # creating links (will be necessary if we change root, which
1301 # live-boot normally does (into $rootmnt)).
1302 local src_dir dest_dir src_transform
1304 # remove multiple /:s and ensure ending on /
1305 src_dir="$(trim_path ${1})/"
1306 dest_dir="$(trim_path ${2})/"
1307 src_transform="${3}"
1309 # This check can only trigger on the inital, non-recursive call since
1310 # we create the destination before recursive calls
1311 if [ ! -d "${dest_dir}" ]
1313 log_warning_msg "Must link_files into a directory"
1317 find "${src_dir}" -mindepth 1 -maxdepth 1 | \
1320 local dest final_src
1321 dest="${dest_dir}$(basename "${src}")"
1324 if [ -z "$(ls -A "${src}")" ]
1328 if [ ! -d "${dest}" ]
1331 chown_ref "${src}" "${dest}"
1332 chmod_ref "${src}" "${dest}"
1334 link_files "${src}" "${dest}" "${src_transform}"
1337 if [ -n "${src_transform}" ]
1339 final_src="$(echo ${final_src} | sed "${src_transform}")"
1341 rm -rf "${dest}" 2> /dev/null
1342 ln -s "${final_src}" "${dest}"
1343 chown_ref "${src}" "${dest}"
1350 local unionmountpoint unionrw unionro
1351 unionmountpoint="${1}" # directory where the union is mounted
1353 unionrw="${1}" # branch where the union changes are stored
1355 unionro="${*}" # space separated list of read-only branches (optional)
1357 case "${UNIONTYPE}" in
1363 unionmountopts="-o noatime,${noxino_opt},dirs=${unionrw}=${rw_opt}"
1364 if [ -n "${unionro}" ]
1366 for rofs in ${unionro}
1368 unionmountopts="${unionmountopts}:${rofs}=${ro_opt}"
1374 # XXX: can unionro be optional? i.e. can overlay skip lowerdir?
1375 if [ -z "${unionro}" ]
1377 panic "overlay needs at least one lower filesystem (read-only branch)."
1379 # Multiple lower layers can now be given using the the colon (":") as a
1380 # separator character between the directory names.
1381 unionro="$(echo ${unionro} | sed -e 's| |:|g')"
1382 # overlayfs requires:
1383 # + a workdir to become mounted
1384 # + workdir and upperdir to reside under the same mount
1385 # + workdir and upperdir to be in separate directories
1386 mkdir -p "${unionrw}/rw"
1387 mkdir -p "${unionrw}/work"
1388 unionmountopts="-o noatime,lowerdir=${unionro},upperdir=${unionrw}/rw,workdir=${unionrw}/work"
1392 mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
1395 get_custom_mounts ()
1397 # Side-effect: leaves $devices with persistence.conf mounted in /run/live/persistence
1398 # Side-effect: prints info to file $custom_mounts
1400 local custom_mounts devices bindings links
1405 bindings="/tmp/bindings.list"
1406 links="/tmp/links.list"
1407 rm -rf ${bindings} ${links} 2> /dev/null
1409 for device in ${devices}
1411 local device_name backing include_list
1412 device_name="$(basename ${device})"
1413 backing=$(mount_persistence_media ${device})
1414 if [ -z "${backing}" ]
1419 if [ -r "${backing}/${persistence_list}" ]
1421 include_list="${backing}/${persistence_list}"
1426 if [ -n "${LIVE_BOOT_DEBUG}" ] && [ -e "${include_list}" ]
1428 cp ${include_list} /run/live/persistence/${persistence_list}.${device_name}
1431 while read dir options # < ${include_list}
1433 if echo ${dir} | grep -qe "^[[:space:]]*\(#.*\)\?$"
1435 # skipping empty or commented lines
1439 if trim_path ${dir} | grep -q -e "^[^/]" -e "^/lib" -e "^/run/live\(/.*\)\?$" -e "^/\(.*/\)\?\.\.\?\(/.*\)\?$"
1441 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."
1445 local opt_source opt_link source full_source full_dest
1448 for opt in $(echo ${options} | tr ',' ' ');
1452 opt_source=${opt#source=}
1460 log_warning_msg "Skipping custom mount with unknown option: ${opt}"
1467 if [ -n "${opt_source}" ]
1469 if echo ${opt_source} | grep -q -e "^/" -e "^\(.*/\)\?\.\.\?\(/.*\)\?$" && [ "${opt_source}" != "." ]
1471 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"
1474 source="${opt_source}"
1478 full_source="$(trim_path ${backing}/${source})"
1479 full_dest="$(trim_path ${rootmnt}/${dir})"
1480 if [ -n "${opt_link}" ]
1482 echo "${device} ${full_source} ${full_dest} ${options}" >> ${links}
1484 echo "${device} ${full_source} ${full_dest} ${options}" >> ${bindings}
1486 done < ${include_list}
1489 # We sort the list according to destination so we're sure that
1490 # we won't hide a previous mount. We also ignore duplicate
1491 # destinations in a more or less arbitrary way.
1492 [ -e "${bindings}" ] && sort -k3 -sbu ${bindings} >> ${custom_mounts} && rm ${bindings}
1494 # After all mounts are considered we add symlinks so they
1495 # won't be hidden by some mount.
1496 [ -e "${links}" ] && cat ${links} >> ${custom_mounts} && rm ${links}
1498 # We need to make sure that no two custom mounts have the same sources
1499 # or are nested; if that is the case, too much weird stuff can happen.
1500 local prev_source prev_dest
1501 prev_source="impossible source" # first iteration must not match
1503 # This sort will ensure that a source /a comes right before a source
1504 # /a/b so we only need to look at the previous source
1505 [ -e ${custom_mounts} ] && sort -k2 -b ${custom_mounts} |
1506 while read device source dest options
1508 if echo ${source} | grep -qe "^${prev_source}\(/.*\)\?$"
1510 panic "Two persistence mounts have the same or nested sources: ${source} on ${dest}, and ${prev_source} on ${prev_dest}"
1512 prev_source=${source}
1517 activate_custom_mounts ()
1519 local custom_mounts used_devices
1520 custom_mounts="${1}" # the ouput from get_custom_mounts()
1523 while read device source dest options # < ${custom_mounts}
1525 local opt_bind opt_link opt_union
1529 for opt in $(echo ${options} | tr ',' ' ');
1534 unset opt_link opt_union
1538 unset opt_bind opt_union
1542 unset opt_bind opt_link
1547 if [ -n "$(what_is_mounted_on "${dest}")" ]
1549 if [ "${dest}" = "${rootmnt}" ]
1553 log_warning_msg "Skipping custom mount ${dest}: $(what_is_mounted_on "${dest}") is already mounted there"
1558 if [ ! -d "${dest}" ]
1560 # create the destination and delete existing files in
1561 # its path that are in the way
1563 for dir in $(echo ${dest} | sed -e 's|/\+| |g')
1565 path=$(trim_path ${path}/${dir})
1573 if echo ${path} | grep -qe "^${rootmnt}/*home/[^/]\+"
1575 # if ${dest} is in /home try fixing proper ownership by assuming that the intended user is the first, which is usually the case
1576 # FIXME: this should really be handled by live-config since we don't know for sure which uid a certain user has until then
1577 chown 1000:1000 ${path}
1583 # if ${source} doesn't exist on our persistence media
1584 # we bootstrap it with $dest from the live filesystem.
1585 # this both makes sense and is critical if we're
1586 # dealing with /etc or other system dir.
1587 if [ ! -d "${source}" ]
1589 if [ -n "${PERSISTENCE_READONLY}" ]
1592 elif [ -n "${opt_union}" ] || [ -n "${opt_link}" ]
1594 # unions and don't need to be bootstrapped
1595 # link dirs can't be bootstrapped in a sensible way
1596 mkdir -p "${source}"
1597 chown_ref "${dest}" "${source}"
1598 chmod_ref "${dest}" "${source}"
1599 elif [ -n "${opt_bind}" ]
1601 # ensure that $dest is not copied *into* $source
1602 mkdir -p "$(dirname ${source})"
1603 cp -a "${dest}" "${source}"
1607 # XXX: If CONFIG_AUFS_ROBR is added to the Debian kernel we can
1608 # ignore the loop below and set rootfs_dest_backing=$dest
1609 local rootfs_dest_backing
1610 rootfs_dest_backing=""
1611 if [ -n "${opt_link}" ] || [ -n "${opt_union}" ]
1613 for d in /run/live/rootfs/*
1615 if [ -n "${rootmnt}" ]
1617 fs="${d}/$(echo ${dest} | sed -e "s|${rootmnt}||")"
1623 rootfs_dest_backing="${rootfs_dest_backing} ${fs}"
1628 local cow_dir links_source
1629 if [ -n "${opt_link}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1631 link_files ${source} ${dest} ""
1632 elif [ -n "${opt_link}" ] && [ -n "${PERSISTENCE_READONLY}" ]
1634 mkdir -p /run/live/persistence
1635 links_source=$(mktemp -d /run/live/persistence/links-source-XXXXXX)
1636 chown_ref ${source} ${links_source}
1637 chmod_ref ${source} ${links_source}
1638 # We put the cow dir in the below strange place to
1639 # make it absolutely certain that the link source
1640 # has its own directory and isn't nested with some
1641 # other custom mount (if so that mount's files would
1642 # be linked, causing breakage.
1643 cow_dir="/run/live/overlay/run/live/persistence/$(basename ${links_source})"
1645 chown_ref "${source}" "${cow_dir}"
1646 chmod_ref "${source}" "${cow_dir}"
1647 do_union ${links_source} ${cow_dir} ${source} ${rootfs_dest_backing}
1648 link_files ${links_source} ${dest} "s|^${rootmnt}||"
1649 elif [ -n "${opt_union}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1651 do_union ${dest} ${source} ${rootfs_dest_backing}
1652 elif [ -n "${opt_bind}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1654 mount -o bind "${source}" "${dest}"
1655 elif [ -n "${opt_bind}" -o -n "${opt_union}" ] && [ -n "${PERSISTENCE_READONLY}" ]
1657 # bind-mount and union mount are handled the same
1658 # in read-only mode, but note that rootfs_dest_backing
1659 # is non-empty (and necessary) only for unions
1660 cow_dir="/run/live/overlay/${dest}"
1661 if [ -e "${cow_dir}" ] && [ -z "${opt_link}" ]
1663 # If an earlier custom mount has files here
1664 # it will "block" the current mount's files
1665 # which is undesirable
1669 chown_ref "${source}" "${cow_dir}"
1670 chmod_ref "${source}" "${cow_dir}"
1671 if [ "${UNIONTYPE}" = "overlay" ]
1673 # When we use overlay we add the "/rw" postfix to our source when using it
1674 # as upper layer. Therefore we also have to add it here when using it as
1676 source="${source}/rw"
1678 do_union ${dest} ${cow_dir} ${source} ${rootfs_dest_backing}
1681 PERSISTENCE_IS_ON="1"
1682 export PERSISTENCE_IS_ON
1684 if echo ${used_devices} | grep -qve "^\(.* \)\?${device}\( .*\)\?$"
1686 used_devices="${used_devices} ${device}"
1688 done < ${custom_mounts}
1690 echo ${used_devices}
1697 [ $(stat -fc%d:%D "${directory}") != $(stat -fc%d:%D "${directory}/..") ]