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 if grep -q ${fstype} /proc/filesystems
479 # Then try to add support for it the gentle way using the initramfs capabilities
480 modprobe -q -b ${fstype}
481 if grep -q ${fstype} /proc/filesystems
484 # Then try the hard way if /root is already reachable
486 kmodule="/root/lib/modules/`uname -r`/${fstype}/${fstype}.ko"
487 if [ -e "${kmodule}" ]
490 if grep -q ${fstype} /proc/filesystems
503 blkid -s TYPE -o value $1 2>/dev/null
510 grep -m1 "^${device} " /proc/mounts | cut -f2 -d ' '
515 # remove all unnecessary /:s in the path, including last one (except
516 # if path is just "/")
517 echo ${1} | sed 's|//\+|/|g' | sed 's|^\(.*[^/]\)/$|\1|'
520 what_is_mounted_on ()
523 dir="$(trim_path ${1})"
524 grep -m1 "^[^ ]\+ ${dir} " /proc/mounts | cut -d' ' -f1
529 local reference targets owner
533 owner=$(stat -c %u:%g "${reference}")
534 chown -h ${owner} ${targets}
539 local reference targets rights
543 rights=$(stat -c %a "${reference}")
544 chmod ${rights} ${targets}
560 mounts="$(awk '{print $2}' /proc/mounts)"
561 testpath="$(realpath ${testpath})"
565 if echo "${mounts}" | grep -qs "^${testpath}"
567 set -- $(echo "${mounts}" | grep "^${testpath}" | lastline)
571 testpath=$(dirname $testpath)
578 # Returns used/free fs kbytes + 5% more
579 # You could pass a block device as ${1} or the mount point as ${2}
585 if [ -z "${mountp}" ]
587 mountp="$(where_is_mounted ${dev})"
589 if [ -z "${mountp}" ]
591 mountp="/mnt/tmp_fs_size"
594 mount -t $(get_fstype "${dev}") -o ro "${dev}" "${mountp}" || log_warning_msg "cannot mount -t $(get_fstype ${dev}) -o ro ${dev} ${mountp}"
600 if [ "${used}" = "used" ]
602 size=$(du -ks ${mountp} | cut -f1)
603 size=$(expr ${size} + ${size} / 20 ) # FIXME: 5% more to be sure
606 size="$(df -kP | grep -s ${mountp} | awk '{print $4}')"
609 if [ -n "${doumount}" ]
611 umount "${mountp}" || log_warning_msg "cannot umount ${mountp}"
621 if [ -x /bin/loadkeys -a -r /etc/boottime.kmap.gz ]
623 loadkeys --quiet /etc/boottime.kmap.gz
629 local fspath module pattern offset encryption readonly
637 # the output of setup_loop is evaluated in other functions,
638 # modprobe leaks kernel options like "libata.dma=0"
639 # as "options libata dma=0" on stdout, causing serious
640 # problems therefor, so instead always avoid output to stdout
641 modprobe -q -b "${module}" 1>/dev/null
645 for loopdev in ${pattern}
647 if [ "$(cat ${loopdev}/size)" -eq 0 ]
649 dev=$(sys2dev "${loopdev}")
652 if [ -n "${readonly}" ]
654 if losetup --help 2>&1 | grep -q -- "-r\b"
656 options="${options} -r"
660 if [ -n "${offset}" ] && [ 0 -lt "${offset}" ]
662 options="${options} -o ${offset}"
665 if [ -z "${encryption}" ]
667 losetup ${options} "${dev}" "${fspath}"
669 # Loop AES encryption
674 echo -n "Enter passphrase for root filesystem: " >&6
676 echo "${passphrase}" > /tmp/passphrase
678 exec 9</tmp/passphrase
679 losetup ${options} -e "${encryption}" -p 9 "${dev}" "${fspath}"
682 rm -f /tmp/passphrase
684 if [ 0 -eq ${error} ]
691 echo -n "There was an error decrypting the root filesystem ... Retry? [Y/n] " >&6
694 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
707 panic "No loop devices available"
717 old_mountp="$(where_is_mounted ${dev})"
719 if [ -n "${old_mountp}" ]
721 if [ "${opts}" != "ro" ]
723 mount -o remount,"${opts}" "${dev}" "${old_mountp}" || panic "Remounting ${dev} ${opts} on ${old_mountp} failed"
726 mount -o bind "${old_mountp}" "${mountp}" || panic "Cannot bind-mount ${old_mountp} on ${mountp}"
728 if [ -z "${fstype}" ]
730 fstype=$(get_fstype "${dev}")
732 mount -t "${fstype}" -o "${opts}" "${dev}" "${mountp}" || \
733 ( echo "SKIPPING: Cannot mount ${dev} on ${mountp}, fstype=${fstype}, options=${opts}" > boot.log && return 0 )
737 # Try to mount $device to the place expected by live-boot. If $device
738 # is already mounted somewhere, move it to the expected place. If $device
739 # ends with a "/" this is a directory path.
740 # If we're only probing $device (to check if it has custom persistence)
741 # $probe should be set, which suppresses warnings upon failure. On
742 # success, print the mount point for $device.
743 mount_persistence_media ()
745 local device probe backing old_backing fstype mount_opts
749 # get_custom_mounts() might call this with a directory path instead
750 # of a block device path. This means we have found sub-directory path
751 # underneath /run/live/persistence, so we're done
752 if [ -d "${device}" ]
758 if [ ! -b "${device}" ]
763 backing="/run/live/persistence/$(basename ${device})"
765 mkdir -p "${backing}"
766 old_backing="$(where_is_mounted ${device})"
767 if [ -z "${old_backing}" ]
769 fstype="$(get_fstype ${device})"
770 mount_opts="rw,noatime"
771 if [ -n "${PERSISTENCE_READONLY}" ]
773 mount_opts="ro,noatime"
775 if mount -t "${fstype}" -o "${mount_opts}" "${device}" "${backing}" >/dev/null
780 [ -z "${probe}" ] && log_warning_msg "Failed to mount persistence media ${device}"
784 elif [ "${backing}" != "${old_backing}" ]
786 if ! mount -o move ${old_backing} ${backing} >/dev/null
788 [ -z "${probe}" ] && log_warning_msg "Failed to move persistence media ${device}"
792 mount_opts="rw,noatime"
793 if [ -n "${PERSISTENCE_READONLY}" ]
795 mount_opts="ro,noatime"
797 if ! mount -o "remount,${mount_opts}" "${backing}" >/dev/null
799 log_warning_msg "Failed to remount persistence media ${device} writable"
800 # Don't unmount or rmdir the new mountpoint in this case
805 # This means that $device has already been mounted on
806 # the place expected by live-boot, so we're done.
812 close_persistence_media ()
816 backing="$(where_is_mounted ${device})"
818 if [ -d "${backing}" ]
820 umount "${backing}" >/dev/null 2>&1
821 rmdir "${backing}" >/dev/null 2>&1
824 if is_active_luks_mapping ${device}
826 cryptsetup luksClose ${device}
833 name="$(basename ${dev})"
835 if [ -n "${PERSISTENCE_READONLY}" ]
837 opts="${opts} --readonly"
840 if cryptsetup status "${name}" >/dev/null 2>&1
842 re="^[[:space:]]*device:[[:space:]]*\([^[:space:]]*\)$"
843 opened_dev=$(cryptsetup status ${name} 2>/dev/null | grep "${re}" | sed "s|${re}|\1|")
844 if [ "${opened_dev}" = "${dev}" ]
846 luks_device="/dev/mapper/${name}"
850 log_warning_msg "Cannot open luks device ${dev} since ${opened_dev} already is opened with its name"
858 if [ -x /bin/plymouth ]
863 case "${_PLYMOUTH}" in
867 cryptkeyscript="plymouth ask-for-password --prompt"
868 # Plymouth will add a : if it is a non-graphical prompt
869 cryptkeyprompt="Please unlock disk ${dev}"
873 cryptkeyscript="/lib/cryptsetup/askpass"
874 cryptkeyprompt="Please unlock disk ${dev}: "
880 $cryptkeyscript "$cryptkeyprompt" | \
881 cryptsetup -T 1 luksOpen ${dev} ${name} ${opts}
885 luks_device="/dev/mapper/${name}"
891 retryprompt="There was an error decrypting ${dev} ... Retry? [Y/n]"
893 case "${_PLYMOUTH}" in
895 plymouth display-message --text "${retryprompt}"
896 answer=$(plymouth watch-keystroke --keys="YNyn")
900 echo -n "${retryprompt} " >&6
905 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
907 case "${_PLYMOUTH}" in
909 plymouth display-message --text ""
922 blkid -s PART_ENTRY_NAME -p -o value ${dev} 2>/dev/null
929 [ "$(blkid -s PART_ENTRY_SCHEME -p -o value ${dev} 2>/dev/null)" = "gpt" ]
932 probe_for_gpt_name ()
934 local overlays dev gpt_dev gpt_name
939 if is_active_luks_mapping ${dev}
941 # if $dev is an opened luks device, we need to check
942 # GPT stuff on the backing device
943 gpt_dev=$(get_luks_backing_device "${dev}")
946 if ! is_gpt_device ${gpt_dev}
951 gpt_name=$(get_gpt_name ${gpt_dev})
952 for label in ${overlays}
954 if [ "${gpt_name}" = "${label}" ]
956 echo "${label}=${dev}"
961 probe_for_fs_label ()
967 for label in ${overlays}
969 if [ "$(blkid -s LABEL -o value $dev 2>/dev/null)" = "${label}" ]
971 echo "${label}=${dev}"
976 probe_for_file_name ()
978 local overlays dev ret backing
983 backing="$(mount_persistence_media ${dev} probe)"
984 if [ -z "${backing}" ]
989 for label in ${overlays}
991 path=${backing}/${PERSISTENCE_PATH}/${label}
995 loopdev=$(setup_loop "${path}" "loop" "/sys/block/loop*")
996 ret="${ret} ${label}=${loopdev}"
1004 # unmount and remove mountpoint
1005 umount ${backing} > /dev/null 2>&1 || true
1006 rmdir ${backing} > /dev/null 2>&1 || true
1010 probe_for_directory_name ()
1012 local overlays dev ret backing
1017 backing="$(mount_persistence_media ${dev} probe)"
1018 if [ -z "${backing}" ]
1023 for label in ${overlays}
1025 path=${backing}/${PERSISTENCE_PATH}/${label}
1028 # in this case the "device" ends with a "/"
1029 ret="${ret} ${label}=${backing}/${PERSISTENCE_PATH}/${label%%/}/"
1037 # unmount and remove mountpoint
1038 umount ${backing} > /dev/null 2>&1 || true
1039 rmdir ${backing} > /dev/null 2>&1 || true
1043 find_persistence_media ()
1045 # Scans devices for overlays, and returns a whitespace
1046 # separated list of how to use them. Only overlays with a partition
1047 # label or file name in ${overlays} are returned.
1049 # When scanning a LUKS device, the user will be asked to enter the
1050 # passphrase; on failure to enter it, or if no persistence partitions
1051 # or files were found, the LUKS device is closed.
1053 # For all other cases (overlay partition and overlay file) the
1054 # return value is "${label}=${device}", where ${device} a device that
1055 # can mount the content. In the case of an overlay file, the device
1056 # containing the file will remain mounted as a side-effect.
1058 # No devices in ${black_listed_devices} will be scanned, and if
1059 # ${white_list_devices} is non-empty, only devices in it will be
1062 local overlays white_listed_devices ret black_listed_devices
1064 white_listed_devices="${2}"
1068 # The devices that are hosting the actual live rootfs should not be
1069 # used for persistence storage since otherwise you might mount a
1070 # parent directory on top of a sub-directory of the same filesystem
1071 # in one union together.
1073 black_listed_devices=""
1074 for d in /run/live/rootfs/* /run/live/findiso /run/live/fromiso
1076 black_listed_devices="${black_listed_devices} $(what_is_mounted_on d)"
1079 for dev in $(storage_devices "${black_listed_devices}" "${white_listed_devices}")
1081 local result luks_device
1085 # Check if it's a luks device; we'll have to open the device
1086 # in order to probe any filesystem it contains, like we do
1087 # below. activate_custom_mounts() also depends on that any luks
1088 # device already has been opened.
1089 if is_in_comma_sep_list luks ${PERSISTENCE_ENCRYPTION} && is_luks_partition ${dev}
1091 if luks_device=$(open_luks_device "${dev}")
1093 dev="${luks_device}"
1095 # skip $dev since we failed/chose not to open it
1098 elif ! is_in_comma_sep_list none ${PERSISTENCE_ENCRYPTION}
1100 # skip $dev since we don't allow unencrypted storage
1104 # Probe for matching GPT partition names or filesystem labels
1105 if is_in_comma_sep_list filesystem ${PERSISTENCE_STORAGE}
1107 result=$(probe_for_gpt_name "${overlays}" ${dev})
1108 if [ -n "${result}" ]
1110 ret="${ret} ${result}"
1114 result=$(probe_for_fs_label "${overlays}" ${dev})
1115 if [ -n "${result}" ]
1117 ret="${ret} ${result}"
1122 # Probe for files with matching name on mounted partition
1123 if is_in_comma_sep_list file ${PERSISTENCE_STORAGE}
1125 result=$(probe_for_file_name "${overlays}" ${dev})
1126 if [ -n "${result}" ]
1129 loopdevice=${result##*=}
1130 if is_in_comma_sep_list luks ${PERSISTENCE_ENCRYPTION} && is_luks_partition ${loopdevice}
1134 if luksfile=$(open_luks_device "${loopdevice}")
1136 result=${result%%=*}
1137 result="${result}=${luksfile}"
1139 losetup -d $loopdevice
1143 ret="${ret} ${result}"
1148 # Probe for directory with matching name on mounted partition
1149 if is_in_comma_sep_list directory ${PERSISTENCE_STORAGE}
1151 result=$(probe_for_directory_name "${overlays}" ${dev})
1152 if [ -n "${result}" ]
1154 ret="${ret} ${result}"
1159 # Close luks device if it isn't used
1160 if [ -z "${result}" ] && [ -n "${luks_device}" ] && is_active_luks_mapping "${luks_device}"
1162 cryptsetup luksClose "${luks_device}"
1176 for adaptor in /sys/class/net/*
1178 status="$(cat ${adaptor}/iflink)"
1180 if [ "${status}" -eq 2 ]
1182 mac="$(cat ${adaptor}/address)"
1183 mac="$(echo ${mac} | sed 's/:/-/g' | tr '[a-z]' '[A-Z]')"
1190 is_luks_partition ()
1193 cryptsetup isLuks "${device}" 1>/dev/null 2>&1
1196 is_active_luks_mapping ()
1199 cryptsetup status "${device}" 1>/dev/null 2>&1
1202 get_luks_backing_device ()
1205 cryptsetup status ${device} 2> /dev/null | \
1206 awk '{if ($1 == "device:") print $2}'
1211 output_format="${1}"
1215 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1217 if [ ! -d "${sysblock}" ]; then
1222 if [ "$(cat ${sysblock}/removable)" = "1" ]
1224 if [ -z "${want_usb}" ]
1228 if readlink ${sysblock} | grep -q usb
1235 if [ "${dev_ok}" = "true" ]
1237 case "${output_format}" in
1239 ret="${ret} ${sysblock}"
1242 devname=$(sys2dev "${sysblock}")
1243 ret="${ret} ${devname}"
1252 removable_usb_dev ()
1254 output_format="${1}"
1256 removable_dev "${output_format}" "want_usb"
1259 non_removable_dev ()
1261 output_format="${1}"
1264 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1266 if [ ! -d "${sysblock}" ]; then
1270 if [ "$(cat ${sysblock}/removable)" = "0" ]
1272 case "${output_format}" in
1274 ret="${ret} ${sysblock}"
1277 devname=$(sys2dev "${sysblock}")
1278 ret="${ret} ${devname}"
1289 # create source's directory structure in dest, and recursively
1290 # create symlinks in dest to to all files in source. if mask
1291 # is non-empty, remove mask from all source paths when
1292 # creating links (will be necessary if we change root, which
1293 # live-boot normally does (into $rootmnt)).
1294 local src_dir dest_dir src_transform
1296 # remove multiple /:s and ensure ending on /
1297 src_dir="$(trim_path ${1})/"
1298 dest_dir="$(trim_path ${2})/"
1299 src_transform="${3}"
1301 # This check can only trigger on the inital, non-recursive call since
1302 # we create the destination before recursive calls
1303 if [ ! -d "${dest_dir}" ]
1305 log_warning_msg "Must link_files into a directory"
1309 find "${src_dir}" -mindepth 1 -maxdepth 1 | \
1312 local dest final_src
1313 dest="${dest_dir}$(basename "${src}")"
1316 if [ -z "$(ls -A "${src}")" ]
1320 if [ ! -d "${dest}" ]
1323 chown_ref "${src}" "${dest}"
1324 chmod_ref "${src}" "${dest}"
1326 link_files "${src}" "${dest}" "${src_transform}"
1329 if [ -n "${src_transform}" ]
1331 final_src="$(echo ${final_src} | sed "${src_transform}")"
1333 rm -rf "${dest}" 2> /dev/null
1334 ln -s "${final_src}" "${dest}"
1335 chown_ref "${src}" "${dest}"
1342 local unionmountpoint unionrw unionro
1343 unionmountpoint="${1}" # directory where the union is mounted
1345 unionrw="${1}" # branch where the union changes are stored
1347 unionro="${*}" # space separated list of read-only branches (optional)
1349 case "${UNIONTYPE}" in
1355 unionmountopts="-o noatime,${noxino_opt},dirs=${unionrw}=${rw_opt}"
1356 if [ -n "${unionro}" ]
1358 for rofs in ${unionro}
1360 unionmountopts="${unionmountopts}:${rofs}=${ro_opt}"
1366 # XXX: can unionro be optional? i.e. can overlay skip lowerdir?
1367 if [ -z "${unionro}" ]
1369 panic "overlay needs at least one lower filesystem (read-only branch)."
1371 # Multiple lower layers can now be given using the the colon (":") as a
1372 # separator character between the directory names.
1373 unionro="$(echo ${unionro} | sed -e 's| |:|g')"
1374 # overlayfs requires:
1375 # + a workdir to become mounted
1376 # + workdir and upperdir to reside under the same mount
1377 # + workdir and upperdir to be in separate directories
1378 mkdir "${unionrw}/rw"
1379 mkdir "${unionrw}/work"
1380 unionmountopts="-o noatime,lowerdir=${unionro},upperdir=${unionrw}/rw,workdir=${unionrw}/work"
1384 mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
1387 get_custom_mounts ()
1389 # Side-effect: leaves $devices with persistence.conf mounted in /run/live/persistence
1390 # Side-effect: prints info to file $custom_mounts
1392 local custom_mounts devices bindings links
1397 bindings="/tmp/bindings.list"
1398 links="/tmp/links.list"
1399 rm -rf ${bindings} ${links} 2> /dev/null
1401 for device in ${devices}
1403 local device_name backing include_list
1404 device_name="$(basename ${device})"
1405 backing=$(mount_persistence_media ${device})
1406 if [ -z "${backing}" ]
1411 if [ -r "${backing}/${persistence_list}" ]
1413 include_list="${backing}/${persistence_list}"
1418 if [ -n "${LIVE_BOOT_DEBUG}" ] && [ -e "${include_list}" ]
1420 cp ${include_list} /run/live/persistence/${persistence_list}.${device_name}
1423 while read dir options # < ${include_list}
1425 if echo ${dir} | grep -qe "^[[:space:]]*\(#.*\)\?$"
1427 # skipping empty or commented lines
1431 if trim_path ${dir} | grep -q -e "^[^/]" -e "^/lib" -e "^/run/live\(/.*\)\?$" -e "^/\(.*/\)\?\.\.\?\(/.*\)\?$"
1433 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."
1437 local opt_source opt_link source full_source full_dest
1440 for opt in $(echo ${options} | tr ',' ' ');
1444 opt_source=${opt#source=}
1452 log_warning_msg "Skipping custom mount with unknown option: ${opt}"
1459 if [ -n "${opt_source}" ]
1461 if echo ${opt_source} | grep -q -e "^/" -e "^\(.*/\)\?\.\.\?\(/.*\)\?$" && [ "${opt_source}" != "." ]
1463 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"
1466 source="${opt_source}"
1470 full_source="$(trim_path ${backing}/${source})"
1471 full_dest="$(trim_path ${rootmnt}/${dir})"
1472 if [ -n "${opt_link}" ]
1474 echo "${device} ${full_source} ${full_dest} ${options}" >> ${links}
1476 echo "${device} ${full_source} ${full_dest} ${options}" >> ${bindings}
1478 done < ${include_list}
1481 # We sort the list according to destination so we're sure that
1482 # we won't hide a previous mount. We also ignore duplicate
1483 # destinations in a more or less arbitrary way.
1484 [ -e "${bindings}" ] && sort -k3 -sbu ${bindings} >> ${custom_mounts} && rm ${bindings}
1486 # After all mounts are considered we add symlinks so they
1487 # won't be hidden by some mount.
1488 [ -e "${links}" ] && cat ${links} >> ${custom_mounts} && rm ${links}
1490 # We need to make sure that no two custom mounts have the same sources
1491 # or are nested; if that is the case, too much weird stuff can happen.
1492 local prev_source prev_dest
1493 prev_source="impossible source" # first iteration must not match
1495 # This sort will ensure that a source /a comes right before a source
1496 # /a/b so we only need to look at the previous source
1497 [ -e ${custom_mounts} ] && sort -k2 -b ${custom_mounts} |
1498 while read device source dest options
1500 if echo ${source} | grep -qe "^${prev_source}\(/.*\)\?$"
1502 panic "Two persistence mounts have the same or nested sources: ${source} on ${dest}, and ${prev_source} on ${prev_dest}"
1504 prev_source=${source}
1509 activate_custom_mounts ()
1511 local custom_mounts used_devices
1512 custom_mounts="${1}" # the ouput from get_custom_mounts()
1515 while read device source dest options # < ${custom_mounts}
1517 local opt_bind opt_link opt_union
1521 for opt in $(echo ${options} | tr ',' ' ');
1526 unset opt_link opt_union
1530 unset opt_bind opt_union
1534 unset opt_bind opt_link
1539 if [ -n "$(what_is_mounted_on "${dest}")" ]
1541 if [ "${dest}" = "${rootmnt}" ]
1545 log_warning_msg "Skipping custom mount ${dest}: $(what_is_mounted_on "${dest}") is already mounted there"
1550 if [ ! -d "${dest}" ]
1552 # create the destination and delete existing files in
1553 # its path that are in the way
1555 for dir in $(echo ${dest} | sed -e 's|/\+| |g')
1557 path=$(trim_path ${path}/${dir})
1565 if echo ${path} | grep -qe "^${rootmnt}/*home/[^/]\+"
1567 # if ${dest} is in /home try fixing proper ownership by assuming that the intended user is the first, which is usually the case
1568 # FIXME: this should really be handled by live-config since we don't know for sure which uid a certain user has until then
1569 chown 1000:1000 ${path}
1575 # if ${source} doesn't exist on our persistence media
1576 # we bootstrap it with $dest from the live filesystem.
1577 # this both makes sense and is critical if we're
1578 # dealing with /etc or other system dir.
1579 if [ ! -d "${source}" ]
1581 if [ -n "${PERSISTENCE_READONLY}" ]
1584 elif [ -n "${opt_union}" ] || [ -n "${opt_link}" ]
1586 # unions and don't need to be bootstrapped
1587 # link dirs can't be bootstrapped in a sensible way
1588 mkdir -p "${source}"
1589 chown_ref "${dest}" "${source}"
1590 chmod_ref "${dest}" "${source}"
1591 elif [ -n "${opt_bind}" ]
1593 # ensure that $dest is not copied *into* $source
1594 mkdir -p "$(dirname ${source})"
1595 cp -a "${dest}" "${source}"
1599 # XXX: If CONFIG_AUFS_ROBR is added to the Debian kernel we can
1600 # ignore the loop below and set rootfs_dest_backing=$dest
1601 local rootfs_dest_backing
1602 rootfs_dest_backing=""
1603 if [ -n "${opt_link}" ] || [ -n "${opt_union}" ]
1605 for d in /run/live/rootfs/*
1607 if [ -n "${rootmnt}" ]
1609 fs="${d}/$(echo ${dest} | sed -e "s|${rootmnt}||")"
1615 rootfs_dest_backing="${rootfs_dest_backing} ${fs}"
1620 local cow_dir links_source
1621 if [ -n "${opt_link}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1623 link_files ${source} ${dest} ""
1624 elif [ -n "${opt_link}" ] && [ -n "${PERSISTENCE_READONLY}" ]
1626 mkdir -p /run/live/persistence
1627 links_source=$(mktemp -d /run/live/persistence/links-source-XXXXXX)
1628 chown_ref ${source} ${links_source}
1629 chmod_ref ${source} ${links_source}
1630 # We put the cow dir in the below strange place to
1631 # make it absolutely certain that the link source
1632 # has its own directory and isn't nested with some
1633 # other custom mount (if so that mount's files would
1634 # be linked, causing breakage.
1635 cow_dir="/run/live/overlay/run/live/persistence/$(basename ${links_source})"
1637 chown_ref "${source}" "${cow_dir}"
1638 chmod_ref "${source}" "${cow_dir}"
1639 do_union ${links_source} ${cow_dir} ${source} ${rootfs_dest_backing}
1640 link_files ${links_source} ${dest} "s|^${rootmnt}||"
1641 elif [ -n "${opt_union}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1643 do_union ${dest} ${source} ${rootfs_dest_backing}
1644 elif [ -n "${opt_bind}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1646 mount -o bind "${source}" "${dest}"
1647 elif [ -n "${opt_bind}" -o -n "${opt_union}" ] && [ -n "${PERSISTENCE_READONLY}" ]
1649 # bind-mount and union mount are handled the same
1650 # in read-only mode, but note that rootfs_dest_backing
1651 # is non-empty (and necessary) only for unions
1652 cow_dir="/run/live/overlay/${dest}"
1653 if [ -e "${cow_dir}" ] && [ -z "${opt_link}" ]
1655 # If an earlier custom mount has files here
1656 # it will "block" the current mount's files
1657 # which is undesirable
1661 chown_ref "${source}" "${cow_dir}"
1662 chmod_ref "${source}" "${cow_dir}"
1663 if [ "${UNIONTYPE}" = "overlay" ]
1665 # When we use overlay we add the "/rw" postfix to our source when using it
1666 # as upper layer. Therefore we also have to add it here when using it as
1668 source="${source}/rw"
1670 do_union ${dest} ${cow_dir} ${source} ${rootfs_dest_backing}
1673 PERSISTENCE_IS_ON="1"
1674 export PERSISTENCE_IS_ON
1676 if echo ${used_devices} | grep -qve "^\(.* \)\?${device}\( .*\)\?$"
1678 used_devices="${used_devices} ${device}"
1680 done < ${custom_mounts}
1682 echo ${used_devices}
1689 [ $(stat -fc%d:%D "${directory}") != $(stat -fc%d:%D "${directory}/..") ]