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"
136 skip_uuid_check="${3}"
138 # support for fromiso=.../isofrom=....
141 ISO_DEVICE=$(dirname $FROMISO)
142 if ! [ -b $ISO_DEVICE ]
144 # to support unusual device names like /dev/cciss/c0d0p1
145 # as well we have to identify the block device name, let's
146 # do that for up to 15 levels
148 while [ -n "$ISO_DEVICE" ] && [ "$i" -gt 0 ]
150 ISO_DEVICE=$(dirname ${ISO_DEVICE})
151 [ -b "$ISO_DEVICE" ] && break
156 if [ "$ISO_DEVICE" = "/" ]
158 echo "Warning: device for bootoption fromiso= ($FROMISO) not found.">>/boot.log
160 fs_type=$(get_fstype "${ISO_DEVICE}")
161 if is_supported_fs ${fs_type}
164 mount -t $fs_type "$ISO_DEVICE" /live/fromiso
165 ISO_NAME="$(echo $FROMISO | sed "s|$ISO_DEVICE||")"
166 loopdevname=$(setup_loop "/live/fromiso/${ISO_NAME}" "loop" "/sys/block/loop*" "" '')
167 devname="${loopdevname}"
169 echo "Warning: unable to mount $ISO_DEVICE." >>/boot.log
174 if [ -z "${devname}" ]
176 devname=$(sys2dev "${sysdev}")
179 if [ -d "${devname}" ]
181 mount -o bind "${devname}" $mountpoint || continue
183 if is_live_path $mountpoint
193 for device in ${devname}
198 if [ -x /scripts/local-top/lvm2 ]
200 ROOT="$device" resume="" /scripts/local-top/lvm2 >>/boot.log
205 # Adding raid support
206 if [ -x /scripts/local-top/mdadm ]
208 [ -r /conf/conf.d/md ] && cp /conf/conf.d/md /conf/conf.d/md.orig
209 echo "MD_DEVS=$device " >> /conf/conf.d/md
210 /scripts/local-top/mdadm >>/boot.log
211 [ -r /conf/conf.d/md.orig ] && mv /conf/conf.d/md.orig /conf/conf.d/md
218 [ -n "$device" ] && devname="$device"
220 [ -e "$devname" ] || continue
222 if [ -n "${LIVE_MEDIA_OFFSET}" ]
224 loopdevname=$(setup_loop "${devname}" "loop" "/sys/block/loop*" "${LIVE_MEDIA_OFFSET}" '')
225 devname="${loopdevname}"
228 fstype=$(get_fstype "${devname}")
230 if is_supported_fs ${fstype}
232 devuid=$(blkid -o value -s UUID "$devname")
233 [ -n "$devuid" ] && grep -qs "\<$devuid\>" /var/lib/live/boot/devices-already-tried-to-mount && continue
234 mount -t ${fstype} -o ro,noatime "${devname}" ${mountpoint} || continue
235 [ -n "$devuid" ] && echo "$devuid" >> /var/lib/live/boot/devices-already-tried-to-mount
237 if [ -n "${FINDISO}" ]
239 if [ -f ${mountpoint}/${FINDISO} ]
242 mkdir -p /live/findiso
243 mount -t ${fstype} -o ro,noatime "${devname}" /live/findiso
244 loopdevname=$(setup_loop "/live/findiso/${FINDISO}" "loop" "/sys/block/loop*" 0 "")
245 devname="${loopdevname}"
246 mount -t iso9660 -o ro,noatime "${devname}" ${mountpoint}
252 if is_live_path ${mountpoint} && \
253 ([ "${skip_uuid_check}" ] || grml_match_bootid ${mountpoint})
258 umount ${mountpoint} 2>/dev/null
262 if [ -n "${LIVE_MEDIA_OFFSET}" ]
264 losetup -d "${loopdevname}"
274 # don't start autodetection before timeout has expired
275 if [ -n "${LIVE_MEDIA_TIMEOUT}" ]
277 if [ "${timeout}" -lt "${LIVE_MEDIA_TIMEOUT}" ]
283 # first look at the one specified in the command line
284 case "${LIVE_MEDIA}" in
286 for sysblock in $(removable_usb_dev "sys")
288 for dev in $(subdevices "${sysblock}")
290 if check_dev "${dev}"
300 for sysblock in $(removable_dev "sys")
302 for dev in $(subdevices "${sysblock}")
304 if check_dev "${dev}"
314 if [ ! -z "${LIVE_MEDIA}" ]
316 if check_dev "null" "${LIVE_MEDIA}" "skip_uuid_check"
324 # or do the scan of block devices
325 # prefer removable devices over non-removable devices, so scan them first
326 devices_to_scan="$(removable_dev 'sys') $(non_removable_dev 'sys')"
328 for sysblock in $devices_to_scan
330 devname=$(sys2dev "${sysblock}")
331 [ -e "$devname" ] || continue
332 fstype=$(get_fstype "${devname}")
334 if /lib/udev/cdrom_id ${devname} > /dev/null
336 if check_dev "null" "${devname}"
340 elif is_nice_device "${sysblock}"
342 for dev in $(subdevices "${sysblock}")
344 if check_dev "${dev}"
355 is_in_list_separator_helper ()
357 local sep element list
363 echo ${list} | grep -qe "^\(.*${sep}\)\?${element}\(${sep}.*\)\?$"
366 is_in_space_sep_list ()
371 is_in_list_separator_helper "[[:space:]]" "${element}" "${*}"
374 is_in_comma_sep_list ()
379 is_in_list_separator_helper "," "${element}" "${*}"
385 echo "/dev/$(udevadm info -q name -p ${sysdev} 2>/dev/null|| echo ${sysdev##*/})"
393 for dev in "${sysblock}"/* "${sysblock}"
395 if [ -e "${dev}/dev" ]
406 black_listed_devices="${1}"
407 white_listed_devices="${2}"
409 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "loop|ram|fd")
411 fulldevname=$(sys2dev "${sysblock}")
413 if is_in_space_sep_list ${fulldevname} ${black_listed_devices} || \
414 [ -n "${white_listed_devices}" ] && \
415 ! is_in_space_sep_list ${fulldevname} ${white_listed_devices}
417 # skip this device entirely
421 for dev in $(subdevices "${sysblock}")
423 devname=$(sys2dev "${dev}")
425 if is_in_space_sep_list ${devname} ${black_listed_devices}
427 # skip this subdevice
440 # Validate input first
441 if [ -z "${fstype}" ]
446 # get_fstype might report "unknown" or "swap", ignore it as no such kernel module exists
447 if [ "${fstype}" = "unknown" ] || [ "${fstype}" = "swap" ]
452 # Try to look if it is already supported by the kernel
453 if grep -q ${fstype} /proc/filesystems
457 # Then try to add support for it the gentle way using the initramfs capabilities
458 modprobe -q -b ${fstype}
459 if grep -q ${fstype} /proc/filesystems
462 # Then try the hard way if /root is already reachable
464 kmodule="/root/lib/modules/`uname -r`/${fstype}/${fstype}.ko"
465 if [ -e "${kmodule}" ]
468 if grep -q ${fstype} /proc/filesystems
481 blkid -s TYPE -o value $1 2>/dev/null
488 grep -m1 "^${device} " /proc/mounts | cut -f2 -d ' '
493 # remove all unnecessary /:s in the path, including last one (except
494 # if path is just "/")
495 echo ${1} | sed 's|//\+|/|g' | sed 's|^\(.*[^/]\)/$|\1|'
498 what_is_mounted_on ()
501 dir="$(trim_path ${1})"
502 grep -m1 "^[^ ]\+ ${dir} " /proc/mounts | cut -d' ' -f1
507 local reference targets owner
511 owner=$(stat -c %u:%g "${reference}")
512 chown -h ${owner} ${targets}
517 local reference targets rights
521 rights=$(stat -c %a "${reference}")
522 chmod ${rights} ${targets}
538 mounts="$(awk '{print $2}' /proc/mounts)"
539 testpath="$(realpath ${testpath})"
543 if echo "${mounts}" | grep -qs "^${testpath}"
545 set -- $(echo "${mounts}" | grep "^${testpath}" | lastline)
549 testpath=$(dirname $testpath)
556 # Returns used/free fs kbytes + 5% more
557 # You could pass a block device as ${1} or the mount point as ${2}
563 if [ -z "${mountp}" ]
565 mountp="$(where_is_mounted ${dev})"
567 if [ -z "${mountp}" ]
569 mountp="/mnt/tmp_fs_size"
572 mount -t $(get_fstype "${dev}") -o ro "${dev}" "${mountp}" || log_warning_msg "cannot mount -t $(get_fstype ${dev}) -o ro ${dev} ${mountp}"
578 if [ "${used}" = "used" ]
580 size=$(du -ks ${mountp} | cut -f1)
581 size=$(expr ${size} + ${size} / 20 ) # FIXME: 5% more to be sure
584 size="$(df -kP | grep -s ${mountp} | awk '{print $4}')"
587 if [ -n "${doumount}" ]
589 umount "${mountp}" || log_warning_msg "cannot umount ${mountp}"
599 if [ -x /bin/loadkeys -a -r /etc/boottime.kmap.gz ]
601 loadkeys --quiet /etc/boottime.kmap.gz
607 local fspath module pattern offset encryption readonly
615 # the output of setup_loop is evaluated in other functions,
616 # modprobe leaks kernel options like "libata.dma=0"
617 # as "options libata dma=0" on stdout, causing serious
618 # problems therefor, so instead always avoid output to stdout
619 modprobe -q -b "${module}" 1>/dev/null
623 for loopdev in ${pattern}
625 if [ "$(cat ${loopdev}/size)" -eq 0 ]
627 dev=$(sys2dev "${loopdev}")
630 if [ -n "${readonly}" ]
632 if losetup --help 2>&1 | grep -q -- "-r\b"
634 options="${options} -r"
638 if [ -n "${offset}" ] && [ 0 -lt "${offset}" ]
640 options="${options} -o ${offset}"
643 if [ -z "${encryption}" ]
645 losetup ${options} "${dev}" "${fspath}"
647 # Loop AES encryption
652 echo -n "Enter passphrase for root filesystem: " >&6
654 echo "${passphrase}" > /tmp/passphrase
656 exec 9</tmp/passphrase
657 losetup ${options} -e "${encryption}" -p 9 "${dev}" "${fspath}"
660 rm -f /tmp/passphrase
662 if [ 0 -eq ${error} ]
669 echo -n "There was an error decrypting the root filesystem ... Retry? [Y/n] " >&6
672 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
685 panic "No loop devices available"
695 old_mountp="$(where_is_mounted ${dev})"
697 if [ -n "${old_mountp}" ]
699 if [ "${opts}" != "ro" ]
701 mount -o remount,"${opts}" "${dev}" "${old_mountp}" || panic "Remounting ${dev} ${opts} on ${old_mountp} failed"
704 mount -o bind "${old_mountp}" "${mountp}" || panic "Cannot bind-mount ${old_mountp} on ${mountp}"
706 if [ -z "${fstype}" ]
708 fstype=$(get_fstype "${dev}")
710 mount -t "${fstype}" -o "${opts}" "${dev}" "${mountp}" || \
711 ( echo "SKIPPING: Cannot mount ${dev} on ${mountp}, fstype=${fstype}, options=${opts}" > boot.log && return 0 )
715 # Try to mount $device to the place expected by live-boot. If $device
716 # is already mounted somewhere, move it to the expected place. If $device
717 # ends with a "/" this is a directory path.
718 # If we're only probing $device (to check if it has custom persistence)
719 # $probe should be set, which suppresses warnings upon failure. On
720 # success, print the mount point for $device.
721 mount_persistence_media ()
723 local device probe backing old_backing fstype mount_opts
727 # get_custom_mounts() might call this with a directory path instead
728 # of a block device path. This means we have found sub-directory path
729 # underneath /lib/live/mounts/persistence, so we're done
730 if [ -d "${device}" ]
736 if [ ! -b "${device}" ]
741 backing="/live/persistence/$(basename ${device})"
743 mkdir -p "${backing}"
744 old_backing="$(where_is_mounted ${device})"
745 if [ -z "${old_backing}" ]
747 fstype="$(get_fstype ${device})"
748 mount_opts="rw,noatime"
749 if [ -n "${PERSISTENCE_READONLY}" ]
751 mount_opts="ro,noatime"
753 if mount -t "${fstype}" -o "${mount_opts}" "${device}" "${backing}" >/dev/null
758 [ -z "${probe}" ] && log_warning_msg "Failed to mount persistence media ${device}"
762 elif [ "${backing}" != "${old_backing}" ]
764 if ! mount -o move ${old_backing} ${backing} >/dev/null
766 [ -z "${probe}" ] && log_warning_msg "Failed to move persistence media ${device}"
770 mount_opts="rw,noatime"
771 if [ -n "${PERSISTENCE_READONLY}" ]
773 mount_opts="ro,noatime"
775 if ! mount -o "remount,${mount_opts}" "${backing}" >/dev/null
777 log_warning_msg "Failed to remount persistence media ${device} writable"
778 # Don't unmount or rmdir the new mountpoint in this case
783 # This means that $device has already been mounted on
784 # the place expected by live-boot, so we're done.
790 close_persistence_media ()
794 backing="$(where_is_mounted ${device})"
796 if [ -d "${backing}" ]
798 umount "${backing}" >/dev/null 2>&1
799 rmdir "${backing}" >/dev/null 2>&1
802 if is_active_luks_mapping ${device}
804 cryptsetup luksClose ${device}
811 name="$(basename ${dev})"
813 if [ -n "${PERSISTENCE_READONLY}" ]
815 opts="${opts} --readonly"
818 if cryptsetup status "${name}" >/dev/null 2>&1
820 re="^[[:space:]]*device:[[:space:]]*\([^[:space:]]*\)$"
821 opened_dev=$(cryptsetup status ${name} 2>/dev/null | grep "${re}" | sed "s|${re}|\1|")
822 if [ "${opened_dev}" = "${dev}" ]
824 luks_device="/dev/mapper/${name}"
828 log_warning_msg "Cannot open luks device ${dev} since ${opened_dev} already is opened with its name"
836 if [ -x /bin/plymouth ]
841 case "${_PLYMOUTH}" in
845 cryptkeyscript="plymouth ask-for-password --prompt"
846 # Plymouth will add a : if it is a non-graphical prompt
847 cryptkeyprompt="Please unlock disk ${dev}"
851 cryptkeyscript="/lib/cryptsetup/askpass"
852 cryptkeyprompt="Please unlock disk ${dev}: "
858 $cryptkeyscript "$cryptkeyprompt" | \
859 cryptsetup -T 1 luksOpen ${dev} ${name} ${opts}
863 luks_device="/dev/mapper/${name}"
869 retryprompt="There was an error decrypting ${dev} ... Retry? [Y/n]"
871 case "${_PLYMOUTH}" in
873 plymouth display-message --text "${retryprompt}"
874 answer=$(plymouth watch-keystroke --keys="YNyn")
878 echo -n "${retryprompt} " >&6
883 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
885 case "${_PLYMOUTH}" in
887 plymouth display-message --text ""
900 blkid -s PART_ENTRY_NAME -p -o value ${dev} 2>/dev/null
907 [ "$(blkid -s PART_ENTRY_SCHEME -p -o value ${dev} 2>/dev/null)" = "gpt" ]
910 probe_for_gpt_name ()
912 local overlays dev gpt_dev gpt_name
917 if is_active_luks_mapping ${dev}
919 # if $dev is an opened luks device, we need to check
920 # GPT stuff on the backing device
921 gpt_dev=$(get_luks_backing_device "${dev}")
924 if ! is_gpt_device ${gpt_dev}
929 gpt_name=$(get_gpt_name ${gpt_dev})
930 for label in ${overlays}
932 if [ "${gpt_name}" = "${label}" ]
934 echo "${label}=${dev}"
939 probe_for_fs_label ()
945 for label in ${overlays}
947 if [ "$(blkid -s LABEL -o value $dev 2>/dev/null)" = "${label}" ]
949 echo "${label}=${dev}"
954 probe_for_file_name ()
956 local overlays dev ret backing
961 backing="$(mount_persistence_media ${dev} probe)"
962 if [ -z "${backing}" ]
967 for label in ${overlays}
969 path=${backing}/${PERSISTENCE_PATH}/${label}
973 loopdev=$(setup_loop "${path}" "loop" "/sys/block/loop*")
974 ret="${ret} ${label}=${loopdev}"
982 # unmount and remove mountpoint
983 umount ${backing} > /dev/null 2>&1 || true
984 rmdir ${backing} > /dev/null 2>&1 || true
988 probe_for_directory_name ()
990 local overlays dev ret backing
995 backing="$(mount_persistence_media ${dev} probe)"
996 if [ -z "${backing}" ]
1001 for label in ${overlays}
1003 path=${backing}/${PERSISTENCE_PATH}/${label}
1006 # in this case the "device" ends with a "/"
1007 ret="${ret} ${label}=${backing}/${PERSISTENCE_PATH}/${label%%/}/"
1015 # unmount and remove mountpoint
1016 umount ${backing} > /dev/null 2>&1 || true
1017 rmdir ${backing} > /dev/null 2>&1 || true
1021 find_persistence_media ()
1023 # Scans devices for overlays, and returns a whitespace
1024 # separated list of how to use them. Only overlays with a partition
1025 # label or file name in ${overlays} are returned.
1027 # When scanning a LUKS device, the user will be asked to enter the
1028 # passphrase; on failure to enter it, or if no persistence partitions
1029 # or files were found, the LUKS device is closed.
1031 # For all other cases (overlay partition and overlay file) the
1032 # return value is "${label}=${device}", where ${device} a device that
1033 # can mount the content. In the case of an overlay file, the device
1034 # containing the file will remain mounted as a side-effect.
1036 # No devices in ${black_listed_devices} will be scanned, and if
1037 # ${white_list_devices} is non-empty, only devices in it will be
1040 local overlays white_listed_devices ret black_listed_devices
1042 white_listed_devices="${2}"
1046 # The devices that are hosting the actual live rootfs should not be
1047 # used for persistence storage since otherwise you might mount a
1048 # parent directory on top of a sub-directory of the same filesystem
1049 # in one union together.
1051 black_listed_devices=""
1052 for d in /live/rootfs/* /live/findiso /live/fromiso
1054 black_listed_devices="${black_listed_devices} $(what_is_mounted_on d)"
1057 for dev in $(storage_devices "${black_listed_devices}" "${white_listed_devices}")
1059 local result luks_device
1063 # Check if it's a luks device; we'll have to open the device
1064 # in order to probe any filesystem it contains, like we do
1065 # below. activate_custom_mounts() also depends on that any luks
1066 # device already has been opened.
1067 if is_in_comma_sep_list luks ${PERSISTENCE_ENCRYPTION} && is_luks_partition ${dev}
1069 if luks_device=$(open_luks_device "${dev}")
1071 dev="${luks_device}"
1073 # skip $dev since we failed/chose not to open it
1076 elif ! is_in_comma_sep_list none ${PERSISTENCE_ENCRYPTION}
1078 # skip $dev since we don't allow unencrypted storage
1082 # Probe for matching GPT partition names or filesystem labels
1083 if is_in_comma_sep_list filesystem ${PERSISTENCE_STORAGE}
1085 result=$(probe_for_gpt_name "${overlays}" ${dev})
1086 if [ -n "${result}" ]
1088 ret="${ret} ${result}"
1092 result=$(probe_for_fs_label "${overlays}" ${dev})
1093 if [ -n "${result}" ]
1095 ret="${ret} ${result}"
1100 # Probe for files with matching name on mounted partition
1101 if is_in_comma_sep_list file ${PERSISTENCE_STORAGE}
1103 result=$(probe_for_file_name "${overlays}" ${dev})
1104 if [ -n "${result}" ]
1107 loopdevice=${result##*=}
1108 if is_in_comma_sep_list luks ${PERSISTENCE_ENCRYPTION} && is_luks_partition ${loopdevice}
1112 if luksfile=$(open_luks_device "${loopdevice}")
1114 result=${result%%=*}
1115 result="${result}=${luksfile}"
1117 losetup -d $loopdevice
1121 ret="${ret} ${result}"
1126 # Probe for directory with matching name on mounted partition
1127 if is_in_comma_sep_list directory ${PERSISTENCE_STORAGE}
1129 result=$(probe_for_directory_name "${overlays}" ${dev})
1130 if [ -n "${result}" ]
1132 ret="${ret} ${result}"
1137 # Close luks device if it isn't used
1138 if [ -z "${result}" ] && [ -n "${luks_device}" ] && is_active_luks_mapping "${luks_device}"
1140 cryptsetup luksClose "${luks_device}"
1154 for adaptor in /sys/class/net/*
1156 status="$(cat ${adaptor}/iflink)"
1158 if [ "${status}" -eq 2 ]
1160 mac="$(cat ${adaptor}/address)"
1161 mac="$(echo ${mac} | sed 's/:/-/g' | tr '[a-z]' '[A-Z]')"
1168 is_luks_partition ()
1171 cryptsetup isLuks "${device}" 1>/dev/null 2>&1
1174 is_active_luks_mapping ()
1177 cryptsetup status "${device}" 1>/dev/null 2>&1
1180 get_luks_backing_device ()
1183 cryptsetup status ${device} 2> /dev/null | \
1184 awk '{if ($1 == "device:") print $2}'
1189 output_format="${1}"
1193 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1195 if [ ! -d "${sysblock}" ]; then
1200 if [ "$(cat ${sysblock}/removable)" = "1" ]
1202 if [ -z "${want_usb}" ]
1206 if readlink ${sysblock} | grep -q usb
1213 if [ "${dev_ok}" = "true" ]
1215 case "${output_format}" in
1217 ret="${ret} ${sysblock}"
1220 devname=$(sys2dev "${sysblock}")
1221 ret="${ret} ${devname}"
1230 removable_usb_dev ()
1232 output_format="${1}"
1234 removable_dev "${output_format}" "want_usb"
1237 non_removable_dev ()
1239 output_format="${1}"
1242 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1244 if [ ! -d "${sysblock}" ]; then
1248 if [ "$(cat ${sysblock}/removable)" = "0" ]
1250 case "${output_format}" in
1252 ret="${ret} ${sysblock}"
1255 devname=$(sys2dev "${sysblock}")
1256 ret="${ret} ${devname}"
1267 # create source's directory structure in dest, and recursively
1268 # create symlinks in dest to to all files in source. if mask
1269 # is non-empty, remove mask from all source paths when
1270 # creating links (will be necessary if we change root, which
1271 # live-boot normally does (into $rootmnt)).
1272 local src_dir dest_dir src_transform
1274 # remove multiple /:s and ensure ending on /
1275 src_dir="$(trim_path ${1})/"
1276 dest_dir="$(trim_path ${2})/"
1277 src_transform="${3}"
1279 # This check can only trigger on the inital, non-recursive call since
1280 # we create the destination before recursive calls
1281 if [ ! -d "${dest_dir}" ]
1283 log_warning_msg "Must link_files into a directory"
1287 find "${src_dir}" -mindepth 1 -maxdepth 1 | \
1290 local dest final_src
1291 dest="${dest_dir}$(basename "${src}")"
1294 if [ -z "$(ls -A "${src}")" ]
1298 if [ ! -d "${dest}" ]
1301 chown_ref "${src}" "${dest}"
1302 chmod_ref "${src}" "${dest}"
1304 link_files "${src}" "${dest}" "${src_transform}"
1307 if [ -n "${src_transform}" ]
1309 final_src="$(echo ${final_src} | sed "${src_transform}")"
1311 rm -rf "${dest}" 2> /dev/null
1312 ln -s "${final_src}" "${dest}"
1313 chown_ref "${src}" "${dest}"
1320 local unionmountpoint unionrw unionro
1321 unionmountpoint="${1}" # directory where the union is mounted
1323 unionrw="${1}" # branch where the union changes are stored
1325 unionro="${*}" # space separated list of read-only branches (optional)
1327 case "${UNIONTYPE}" in
1333 unionmountopts="-o noatime,${noxino_opt},dirs=${unionrw}=${rw_opt}"
1334 if [ -n "${unionro}" ]
1336 for rofs in ${unionro}
1338 unionmountopts="${unionmountopts}:${rofs}=${ro_opt}"
1344 # XXX: can unionro be optional? i.e. can overlay skip lowerdir?
1345 if [ -z "${unionro}" ]
1347 panic "overlay needs at least one lower filesystem (read-only branch)."
1349 # Multiple lower layers can now be given using the the colon (":") as a
1350 # separator character between the directory names.
1351 unionro="$(echo ${unionro} | sed -e 's| |:|g')"
1352 # overlayfs requires:
1353 # + a workdir to become mounted
1354 # + workdir and upperdir to reside under the same mount
1355 # + workdir and upperdir to be in separate directories
1356 mkdir "${unionrw}/rw"
1357 mkdir "${unionrw}/work"
1358 unionmountopts="-o noatime,lowerdir=${unionro},upperdir=${unionrw}/rw,workdir=${unionrw}/work"
1362 mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
1365 get_custom_mounts ()
1367 # Side-effect: leaves $devices with persistence.conf mounted in /live/persistence
1368 # Side-effect: prints info to file $custom_mounts
1370 local custom_mounts devices bindings links
1375 bindings="/tmp/bindings.list"
1376 links="/tmp/links.list"
1377 rm -rf ${bindings} ${links} 2> /dev/null
1379 for device in ${devices}
1381 local device_name backing include_list
1382 device_name="$(basename ${device})"
1383 backing=$(mount_persistence_media ${device})
1384 if [ -z "${backing}" ]
1389 if [ -r "${backing}/${persistence_list}" ]
1391 include_list="${backing}/${persistence_list}"
1396 if [ -n "${LIVE_BOOT_DEBUG}" ] && [ -e "${include_list}" ]
1398 cp ${include_list} /live/persistence/${persistence_list}.${device_name}
1401 while read dir options # < ${include_list}
1403 if echo ${dir} | grep -qe "^[[:space:]]*\(#.*\)\?$"
1405 # skipping empty or commented lines
1409 if trim_path ${dir} | grep -q -e "^[^/]" -e "^/lib" -e "^/lib/live\(/.*\)\?$" -e "^/\(.*/\)\?\.\.\?\(/.*\)\?$"
1411 log_warning_msg "Skipping unsafe custom mount ${dir}: must be an absolute path containing neither the \".\" nor \"..\" special dirs, and cannot be \"/lib\", or \"/lib/live\" or any of its sub-directories."
1415 local opt_source opt_link source full_source full_dest
1418 for opt in $(echo ${options} | tr ',' ' ');
1422 opt_source=${opt#source=}
1430 log_warning_msg "Skipping custom mount with unknown option: ${opt}"
1437 if [ -n "${opt_source}" ]
1439 if echo ${opt_source} | grep -q -e "^/" -e "^\(.*/\)\?\.\.\?\(/.*\)\?$" && [ "${opt_source}" != "." ]
1441 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"
1444 source="${opt_source}"
1448 full_source="$(trim_path ${backing}/${source})"
1449 full_dest="$(trim_path ${rootmnt}/${dir})"
1450 if [ -n "${opt_link}" ]
1452 echo "${device} ${full_source} ${full_dest} ${options}" >> ${links}
1454 echo "${device} ${full_source} ${full_dest} ${options}" >> ${bindings}
1456 done < ${include_list}
1459 # We sort the list according to destination so we're sure that
1460 # we won't hide a previous mount. We also ignore duplicate
1461 # destinations in a more or less arbitrary way.
1462 [ -e "${bindings}" ] && sort -k3 -sbu ${bindings} >> ${custom_mounts} && rm ${bindings}
1464 # After all mounts are considered we add symlinks so they
1465 # won't be hidden by some mount.
1466 [ -e "${links}" ] && cat ${links} >> ${custom_mounts} && rm ${links}
1468 # We need to make sure that no two custom mounts have the same sources
1469 # or are nested; if that is the case, too much weird stuff can happen.
1470 local prev_source prev_dest
1471 prev_source="impossible source" # first iteration must not match
1473 # This sort will ensure that a source /a comes right before a source
1474 # /a/b so we only need to look at the previous source
1475 [ -e ${custom_mounts} ] && sort -k2 -b ${custom_mounts} |
1476 while read device source dest options
1478 if echo ${source} | grep -qe "^${prev_source}\(/.*\)\?$"
1480 panic "Two persistence mounts have the same or nested sources: ${source} on ${dest}, and ${prev_source} on ${prev_dest}"
1482 prev_source=${source}
1487 activate_custom_mounts ()
1489 local custom_mounts used_devices
1490 custom_mounts="${1}" # the ouput from get_custom_mounts()
1493 while read device source dest options # < ${custom_mounts}
1495 local opt_bind opt_link opt_union
1499 for opt in $(echo ${options} | tr ',' ' ');
1504 unset opt_link opt_union
1508 unset opt_bind opt_union
1512 unset opt_bind opt_link
1517 if [ -n "$(what_is_mounted_on "${dest}")" ]
1519 if [ "${dest}" = "${rootmnt}" ]
1523 log_warning_msg "Skipping custom mount ${dest}: $(what_is_mounted_on "${dest}") is already mounted there"
1528 if [ ! -d "${dest}" ]
1530 # create the destination and delete existing files in
1531 # its path that are in the way
1533 for dir in $(echo ${dest} | sed -e 's|/\+| |g')
1535 path=$(trim_path ${path}/${dir})
1543 if echo ${path} | grep -qe "^${rootmnt}/*home/[^/]\+"
1545 # if ${dest} is in /home try fixing proper ownership by assuming that the intended user is the first, which is usually the case
1546 # FIXME: this should really be handled by live-config since we don't know for sure which uid a certain user has until then
1547 chown 1000:1000 ${path}
1553 # if ${source} doesn't exist on our persistence media
1554 # we bootstrap it with $dest from the live filesystem.
1555 # this both makes sense and is critical if we're
1556 # dealing with /etc or other system dir.
1557 if [ ! -d "${source}" ]
1559 if [ -n "${PERSISTENCE_READONLY}" ]
1562 elif [ -n "${opt_union}" ] || [ -n "${opt_link}" ]
1564 # unions and don't need to be bootstrapped
1565 # link dirs can't be bootstrapped in a sensible way
1566 mkdir -p "${source}"
1567 chown_ref "${dest}" "${source}"
1568 chmod_ref "${dest}" "${source}"
1569 elif [ -n "${opt_bind}" ]
1571 # ensure that $dest is not copied *into* $source
1572 mkdir -p "$(dirname ${source})"
1573 cp -a "${dest}" "${source}"
1577 # XXX: If CONFIG_AUFS_ROBR is added to the Debian kernel we can
1578 # ignore the loop below and set rootfs_dest_backing=$dest
1579 local rootfs_dest_backing
1580 rootfs_dest_backing=""
1581 if [ -n "${opt_link}" ] || [ -n "${opt_union}" ]
1583 for d in /live/rootfs/*
1585 if [ -n "${rootmnt}" ]
1587 fs="${d}/$(echo ${dest} | sed -e "s|${rootmnt}||")"
1593 rootfs_dest_backing="${rootfs_dest_backing} ${fs}"
1598 local cow_dir links_source
1599 if [ -n "${opt_link}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1601 link_files ${source} ${dest} "s|^/live/|/lib/live/mount/|"
1602 elif [ -n "${opt_link}" ] && [ -n "${PERSISTENCE_READONLY}" ]
1604 mkdir -p ${rootmnt}/lib/live/mount/persistence
1605 links_source=$(mktemp -d ${rootmnt}/lib/live/mount/persistence/links-source-XXXXXX)
1606 chown_ref ${source} ${links_source}
1607 chmod_ref ${source} ${links_source}
1608 # We put the cow dir in the below strange place to
1609 # make it absolutely certain that the link source
1610 # has its own directory and isn't nested with some
1611 # other custom mount (if so that mount's files would
1612 # be linked, causing breakage.
1613 cow_dir="/live/overlay/lib/live/mount/persistence/$(basename ${links_source})"
1615 chown_ref "${source}" "${cow_dir}"
1616 chmod_ref "${source}" "${cow_dir}"
1617 do_union ${links_source} ${cow_dir} ${source} ${rootfs_dest_backing}
1618 link_files ${links_source} ${dest} "s|^${rootmnt}||"
1619 elif [ -n "${opt_union}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1621 do_union ${dest} ${source} ${rootfs_dest_backing}
1622 elif [ -n "${opt_bind}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1624 mount -o bind "${source}" "${dest}"
1625 elif [ -n "${opt_bind}" -o -n "${opt_union}" ] && [ -n "${PERSISTENCE_READONLY}" ]
1627 # bind-mount and union mount are handled the same
1628 # in read-only mode, but note that rootfs_dest_backing
1629 # is non-empty (and necessary) only for unions
1630 cow_dir="/live/overlay/${dest}"
1631 if [ -e "${cow_dir}" ] && [ -z "${opt_link}" ]
1633 # If an earlier custom mount has files here
1634 # it will "block" the current mount's files
1635 # which is undesirable
1639 chown_ref "${source}" "${cow_dir}"
1640 chmod_ref "${source}" "${cow_dir}"
1641 do_union ${dest} ${cow_dir} ${source} ${rootfs_dest_backing}
1644 PERSISTENCE_IS_ON="1"
1645 export PERSISTENCE_IS_ON
1647 if echo ${used_devices} | grep -qve "^\(.* \)\?${device}\( .*\)\?$"
1649 used_devices="${used_devices} ${device}"
1651 done < ${custom_mounts}
1653 echo ${used_devices}
1660 [ $(stat -fc%d:%D "${directory}") != $(stat -fc%d:%D "${directory}/..") ]