7 DIRECTORY="${1}/${LIVE_MEDIA_PATH}"
8 for FILESYSTEM in squashfs ext2 ext3 ext4 xfs dir jffs
10 if ls "${DIRECTORY}/"*.${FILESYSTEM} > /dev/null 2>&1
22 if [ -n "$IGNORE_BOOTID" ] ; then
23 echo " * Ignoring verification of bootid.txt as requested via ignore_bootid.">>/boot.log
27 if [ -n "$BOOTID" ] && ! [ -r "${path}/conf/bootid.txt" ] ; then
28 echo " * Warning: bootid=... specified but no bootid.txt found on currently requested device.">>/boot.log
32 [ -r "${path}/conf/bootid.txt" ] || return 0
34 bootid_conf=$(cat "${path}/conf/bootid.txt")
36 if [ -z "$BOOTID" -a -z "$IGNORE_BOOTID" ]
38 echo " * Warning: bootid.txt found but ignore_bootid / bootid=.. bootoption missing...">>/boot.log
42 if [ "$BOOTID" = "$bootid_conf" ]
44 echo " * Successfully verified /conf/bootid.txt from ISO, continuing... ">>/boot.log
46 echo " * Warning: BOOTID of ISO does not match. Retrying and continuing search...">>/boot.log
56 if [ "${IGNORE_UUID}" ] || [ ! -e /conf/uuid.conf ]
62 uuid="$(cat /conf/uuid.conf)"
64 for try_uuid_file in "${path}/.disk/live-uuid"*
66 [ -e "${try_uuid_file}" ] || continue
68 try_uuid="$(cat "${try_uuid_file}")"
70 if [ "${uuid}" = "${try_uuid}" ]
82 *.squashfs|*.ext2|*.ext3|*.ext4|*.jffs2)
83 echo $(setup_loop "${1}" "loop" "/sys/block/loop*" '0' "${LIVE_MEDIA_ENCRYPTION}" "${2}")
91 panic "Unrecognized live filesystem: ${1}"
96 mount_images_in_directory ()
102 if is_live_path "${directory}"
104 [ -n "${mac}" ] && adddirectory="${directory}/${LIVE_MEDIA_PATH}/${mac}"
105 setup_unionfs "${directory}/${LIVE_MEDIA_PATH}" "${rootmnt}" "${adddirectory}"
107 panic "No supported filesystem images found at /${LIVE_MEDIA_PATH}."
113 sysfs_path="${1#/sys}"
115 if udevadm info --query=all --path="${sysfs_path}" | egrep -q "DEVTYPE=disk"
118 elif echo "${sysfs_path}" | grep -q '^/block/vd[a-z]$'
121 elif echo ${sysfs_path} | grep -q "^/block/dm-"
124 elif echo ${sysfs_path} | grep -q "^/block/mtdblock"
137 skip_uuid_check="${3}"
139 # support for fromiso=.../isofrom=....
142 ISO_DEVICE=$(dirname $FROMISO)
143 if ! [ -b $ISO_DEVICE ]
145 # to support unusual device names like /dev/cciss/c0d0p1
146 # as well we have to identify the block device name, let's
147 # do that for up to 15 levels
149 while [ -n "$ISO_DEVICE" ] && [ "$i" -gt 0 ]
151 ISO_DEVICE=$(dirname ${ISO_DEVICE})
152 [ -b "$ISO_DEVICE" ] && break
157 if [ "$ISO_DEVICE" = "/" ]
159 # not a block device, check if it's an iso file, for
160 # example an ISO when booting on an ONIE system
161 if echo "${FROMISO}" | grep -q "\.iso$"
163 fs_type=$(get_fstype "${FROMISO}")
164 if is_supported_fs ${fs_type}
166 mkdir /run/live/fromiso
167 mount -t $fs_type "${FROMISO}" /run/live/fromiso
170 echo "Warning: unable to mount ${FROMISO}." >>/boot.log
172 devname="/run/live/fromiso"
175 echo "Warning: device for bootoption fromiso= ($FROMISO) not found.">>/boot.log
178 fs_type=$(get_fstype "${ISO_DEVICE}")
179 if is_supported_fs ${fs_type}
181 mkdir /run/live/fromiso
182 mount -t $fs_type "$ISO_DEVICE" /run/live/fromiso
183 ISO_NAME="$(echo $FROMISO | sed "s|$ISO_DEVICE||")"
184 loopdevname=$(setup_loop "/run/live/fromiso/${ISO_NAME}" "loop" "/sys/block/loop*" "" '')
185 devname="${loopdevname}"
187 echo "Warning: unable to mount $ISO_DEVICE." >>/boot.log
192 if [ -z "${devname}" ]
194 devname=$(sys2dev "${sysdev}")
197 if [ -d "${devname}" ]
199 mount -o bind "${devname}" $mountpoint || continue
201 if is_live_path $mountpoint
211 for device in ${devname}
216 if [ -x /scripts/local-top/lvm2 ]
218 ROOT="$device" resume="" /scripts/local-top/lvm2 >>/boot.log
223 # Adding raid support
224 if [ -x /scripts/local-top/mdadm ]
226 [ -r /conf/conf.d/md ] && cp /conf/conf.d/md /conf/conf.d/md.orig
227 echo "MD_DEVS=$device " >> /conf/conf.d/md
228 /scripts/local-top/mdadm >>/boot.log
229 [ -r /conf/conf.d/md.orig ] && mv /conf/conf.d/md.orig /conf/conf.d/md
236 [ -n "$device" ] && devname="$device"
238 [ -e "$devname" ] || continue
240 if [ -n "${LIVE_MEDIA_OFFSET}" ]
242 loopdevname=$(setup_loop "${devname}" "loop" "/sys/block/loop*" "${LIVE_MEDIA_OFFSET}" '')
243 devname="${loopdevname}"
246 fstype=$(get_fstype "${devname}")
248 if is_supported_fs ${fstype}
250 devuid=$(blkid -o value -s UUID "$devname")
251 [ -n "$devuid" ] && grep -qs "\<$devuid\>" /var/lib/live/boot/devices-already-tried-to-mount && continue
253 for _PARAMETER in ${LIVE_BOOT_CMDLINE}
255 case "${_PARAMETER}" in
262 if [ "${PERSISTENCE_FSCK}" = "true" ] || [ "${PERSISTENCE_FSCK}" = "yes" ] || [ "${FORCEFSCK}" = "true" ]
265 if [ "$FORCEFSCK" = "true" ]
271 if [ "$FSCKFIX" = "true" ] || [ "$FSCKFIX" = "yes" ]
276 fsck $fix $force ${devname} >> fsck.log 2>&1
279 mount -t ${fstype} -o ro,noatime "${devname}" ${mountpoint} || continue
280 [ -n "$devuid" ] && echo "$devuid" >> /var/lib/live/boot/devices-already-tried-to-mount
282 if [ -n "${FINDISO}" ]
284 if [ -f ${mountpoint}/${FINDISO} ]
287 mkdir -p /run/live/findiso
288 mount -t ${fstype} -o ro,noatime "${devname}" /run/live/findiso
289 loopdevname=$(setup_loop "/run/live/findiso/${FINDISO}" "loop" "/sys/block/loop*" 0 "")
290 devname="${loopdevname}"
291 mount -t iso9660 -o ro,noatime "${devname}" ${mountpoint}
297 if is_live_path ${mountpoint} && \
298 ([ "${skip_uuid_check}" ] || grml_match_bootid ${mountpoint})
303 umount ${mountpoint} 2>/dev/null
307 if [ -n "${LIVE_MEDIA_OFFSET}" ]
309 losetup -d "${loopdevname}"
319 # don't start autodetection before timeout has expired
320 if [ -n "${LIVE_MEDIA_TIMEOUT}" ]
322 if [ "${timeout}" -lt "${LIVE_MEDIA_TIMEOUT}" ]
328 # first look at the one specified in the command line
329 case "${LIVE_MEDIA}" in
331 for sysblock in $(removable_usb_dev "sys")
333 for dev in $(subdevices "${sysblock}")
335 if check_dev "${dev}"
345 for sysblock in $(removable_dev "sys")
347 for dev in $(subdevices "${sysblock}")
349 if check_dev "${dev}"
359 if [ ! -z "${LIVE_MEDIA}" ]
361 if check_dev "null" "${LIVE_MEDIA}" "skip_uuid_check"
369 # or do the scan of block devices
370 # prefer removable devices over non-removable devices, so scan them first
371 devices_to_scan="$(removable_dev 'sys') $(non_removable_dev 'sys')"
373 for sysblock in $devices_to_scan
375 devname=$(sys2dev "${sysblock}")
376 [ -e "$devname" ] || continue
377 fstype=$(get_fstype "${devname}")
379 if /lib/udev/cdrom_id ${devname} > /dev/null
381 if check_dev "null" "${devname}"
385 elif is_nice_device "${sysblock}"
387 for dev in $(subdevices "${sysblock}")
389 if check_dev "${dev}"
400 is_in_list_separator_helper ()
402 local sep element list
408 echo ${list} | grep -qe "^\(.*${sep}\)\?${element}\(${sep}.*\)\?$"
411 is_in_space_sep_list ()
416 is_in_list_separator_helper "[[:space:]]" "${element}" "${*}"
419 is_in_comma_sep_list ()
424 is_in_list_separator_helper "," "${element}" "${*}"
430 echo "/dev/$(udevadm info -q name -p ${sysdev} 2>/dev/null|| echo ${sysdev##*/})"
438 for dev in "${sysblock}"/* "${sysblock}"
440 if [ -e "${dev}/dev" ]
451 black_listed_devices="${1}"
452 white_listed_devices="${2}"
454 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "loop|ram|fd")
456 fulldevname=$(sys2dev "${sysblock}")
458 if is_in_space_sep_list ${fulldevname} ${black_listed_devices} || \
459 [ -n "${white_listed_devices}" ] && \
460 ! is_in_space_sep_list ${fulldevname} ${white_listed_devices}
462 # skip this device entirely
466 for dev in $(subdevices "${sysblock}")
468 devname=$(sys2dev "${dev}")
470 if is_in_space_sep_list ${devname} ${black_listed_devices}
472 # skip this subdevice
485 # Validate input first
486 if [ -z "${fstype}" ]
491 # get_fstype might report "unknown" or "swap", ignore it as no such kernel module exists
492 if [ "${fstype}" = "unknown" ] || [ "${fstype}" = "swap" ]
497 # Try to look if it is already supported by the kernel
498 # For ntfs, since user space program ntfs-3g will be used. Check ntfs-3g instead of kernel module.
499 if [ "${fstype}" = "ntfs" ]; then
500 if type ntfs-3g >/dev/null 2>&1; then
506 if grep -q ${fstype} /proc/filesystems
510 # Then try to add support for it the gentle way using the initramfs capabilities
511 modprobe -q -b ${fstype}
512 if grep -q ${fstype} /proc/filesystems
515 # Then try the hard way if /root is already reachable
517 kmodule="/root/lib/modules/`uname -r`/${fstype}/${fstype}.ko"
518 if [ -e "${kmodule}" ]
521 if grep -q ${fstype} /proc/filesystems
534 blkid -s TYPE -o value $1 2>/dev/null
541 grep -m1 "^${device} " /proc/mounts | cut -f2 -d ' '
546 # remove all unnecessary /:s in the path, including last one (except
547 # if path is just "/")
548 echo ${1} | sed 's|//\+|/|g' | sed 's|^\(.*[^/]\)/$|\1|'
551 what_is_mounted_on ()
554 dir="$(trim_path ${1})"
555 grep -m1 "^[^ ]\+ ${dir} " /proc/mounts | cut -d' ' -f1
560 local reference targets owner
564 owner=$(stat -c %u:%g "${reference}")
565 chown -h ${owner} ${targets}
570 local reference targets rights
574 rights=$(stat -c %a "${reference}")
575 chmod ${rights} ${targets}
591 mounts="$(awk '{print $2}' /proc/mounts)"
592 testpath="$(realpath ${testpath})"
596 if echo "${mounts}" | grep -qs "^${testpath}"
598 set -- $(echo "${mounts}" | grep "^${testpath}" | lastline)
602 testpath=$(dirname $testpath)
609 # Returns used/free fs kbytes + 5% more
610 # You could pass a block device as ${1} or the mount point as ${2}
616 if [ -z "${mountp}" ]
618 mountp="$(where_is_mounted ${dev})"
620 if [ -z "${mountp}" ]
622 mountp="/mnt/tmp_fs_size"
625 mount -t $(get_fstype "${dev}") -o ro "${dev}" "${mountp}" || log_warning_msg "cannot mount -t $(get_fstype ${dev}) -o ro ${dev} ${mountp}"
631 if [ "${used}" = "used" ]
633 size=$(du -ks ${mountp} | cut -f1)
634 size=$(expr ${size} + ${size} / 20 ) # FIXME: 5% more to be sure
637 size="$(df -kP | grep -s ${mountp} | awk '{print $4}')"
640 if [ -n "${doumount}" ]
642 umount "${mountp}" || log_warning_msg "cannot umount ${mountp}"
652 if [ -x /bin/loadkeys -a -r /etc/boottime.kmap.gz ]
654 loadkeys --quiet /etc/boottime.kmap.gz
660 local fspath module pattern offset encryption readonly
668 # the output of setup_loop is evaluated in other functions,
669 # modprobe leaks kernel options like "libata.dma=0"
670 # as "options libata dma=0" on stdout, causing serious
671 # problems therefor, so instead always avoid output to stdout
672 modprobe -q -b "${module}" 1>/dev/null
676 for loopdev in ${pattern}
678 if [ "$(cat ${loopdev}/size)" -eq 0 ]
680 dev=$(sys2dev "${loopdev}")
683 if [ -n "${readonly}" ]
685 if losetup --help 2>&1 | grep -q -- "-r\b"
687 options="${options} -r"
691 if [ -n "${offset}" ] && [ 0 -lt "${offset}" ]
693 options="${options} -o ${offset}"
696 if [ -z "${encryption}" ]
698 losetup ${options} "${dev}" "${fspath}"
700 # Loop AES encryption
705 echo -n "Enter passphrase for root filesystem: " >&6
707 echo "${passphrase}" > /tmp/passphrase
709 exec 9</tmp/passphrase
710 losetup ${options} -e "${encryption}" -p 9 "${dev}" "${fspath}"
713 rm -f /tmp/passphrase
715 if [ 0 -eq ${error} ]
722 echo -n "There was an error decrypting the root filesystem ... Retry? [Y/n] " >&6
725 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
738 panic "No loop devices available"
748 old_mountp="$(where_is_mounted ${dev})"
750 if [ -n "${old_mountp}" ]
752 if [ "${opts}" != "ro" ]
754 mount -o remount,"${opts}" "${dev}" "${old_mountp}" || panic "Remounting ${dev} ${opts} on ${old_mountp} failed"
757 mount -o bind "${old_mountp}" "${mountp}" || panic "Cannot bind-mount ${old_mountp} on ${mountp}"
759 if [ -z "${fstype}" ]
761 fstype=$(get_fstype "${dev}")
763 mount -t "${fstype}" -o "${opts}" "${dev}" "${mountp}" || \
764 ( echo "SKIPPING: Cannot mount ${dev} on ${mountp}, fstype=${fstype}, options=${opts}" >> boot.log && return 0 )
768 # Try to mount $device to the place expected by live-boot. If $device
769 # is already mounted somewhere, move it to the expected place. If $device
770 # ends with a "/" this is a directory path.
771 # If we're only probing $device (to check if it has custom persistence)
772 # $probe should be set, which suppresses warnings upon failure. On
773 # success, print the mount point for $device.
774 mount_persistence_media ()
776 local device probe backing old_backing fstype mount_opts
780 # get_custom_mounts() might call this with a directory path instead
781 # of a block device path. This means we have found sub-directory path
782 # underneath /run/live/persistence, so we're done
783 if [ -d "${device}" ]
789 if [ ! -b "${device}" ]
794 backing="/run/live/persistence/$(basename ${device})"
796 mkdir -p "${backing}"
797 old_backing="$(where_is_mounted ${device})"
798 if [ -z "${old_backing}" ]
800 fstype="$(get_fstype ${device})"
801 mount_opts="rw,noatime"
802 if [ -n "${PERSISTENCE_READONLY}" ]
804 mount_opts="ro,noatime"
806 if mount -t "${fstype}" -o "${mount_opts}" "${device}" "${backing}" >/dev/null 2>&1
811 [ -z "${probe}" ] && log_warning_msg "Failed to mount persistence media ${device}"
815 elif [ "${backing}" != "${old_backing}" ]
817 if ! mount -o move ${old_backing} ${backing} >/dev/null
819 [ -z "${probe}" ] && log_warning_msg "Failed to move persistence media ${device}"
823 mount_opts="rw,noatime"
824 if [ -n "${PERSISTENCE_READONLY}" ]
826 mount_opts="ro,noatime"
828 if ! mount -o "remount,${mount_opts}" "${backing}" >/dev/null
830 log_warning_msg "Failed to remount persistence media ${device} writable"
831 # Don't unmount or rmdir the new mountpoint in this case
836 # This means that $device has already been mounted on
837 # the place expected by live-boot, so we're done.
843 close_persistence_media ()
847 backing="$(where_is_mounted ${device})"
849 if [ -d "${backing}" ]
851 umount "${backing}" >/dev/null 2>&1
852 rmdir "${backing}" >/dev/null 2>&1
855 if is_active_luks_mapping ${device}
857 cryptsetup luksClose ${device}
864 name="$(basename ${dev})"
866 if [ -n "${PERSISTENCE_READONLY}" ]
868 opts="${opts} --readonly"
871 if cryptsetup status "${name}" >/dev/null 2>&1
873 re="^[[:space:]]*device:[[:space:]]*\([^[:space:]]*\)$"
874 opened_dev=$(cryptsetup status ${name} 2>/dev/null | grep "${re}" | sed "s|${re}|\1|")
875 if [ "${opened_dev}" = "${dev}" ]
877 luks_device="/dev/mapper/${name}"
881 log_warning_msg "Cannot open luks device ${dev} since ${opened_dev} already is opened with its name"
889 if [ -x /bin/plymouth ]
894 case "${_PLYMOUTH}" in
898 cryptkeyscript="plymouth ask-for-password --prompt"
899 # Plymouth will add a : if it is a non-graphical prompt
900 cryptkeyprompt="Please unlock disk ${dev}"
904 cryptkeyscript="/lib/cryptsetup/askpass"
905 cryptkeyprompt="Please unlock disk ${dev}: "
911 $cryptkeyscript "$cryptkeyprompt" | \
912 cryptsetup -T 1 luksOpen ${dev} ${name} ${opts}
916 luks_device="/dev/mapper/${name}"
922 retryprompt="There was an error decrypting ${dev} ... Retry? [Y/n]"
924 case "${_PLYMOUTH}" in
926 plymouth display-message --text "${retryprompt}"
927 answer=$(plymouth watch-keystroke --keys="YNyn")
931 echo -n "${retryprompt} " >&6
936 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
938 case "${_PLYMOUTH}" in
940 plymouth display-message --text ""
953 blkid -s PART_ENTRY_NAME -p -o value ${dev} 2>/dev/null
960 [ "$(blkid -s PART_ENTRY_SCHEME -p -o value ${dev} 2>/dev/null)" = "gpt" ]
963 probe_for_gpt_name ()
965 local overlays dev gpt_dev gpt_name
970 if is_active_luks_mapping ${dev}
972 # if $dev is an opened luks device, we need to check
973 # GPT stuff on the backing device
974 gpt_dev=$(get_luks_backing_device "${dev}")
977 if ! is_gpt_device ${gpt_dev}
982 gpt_name=$(get_gpt_name ${gpt_dev})
983 for label in ${overlays}
985 if [ "${gpt_name}" = "${label}" ]
987 echo "${label}=${dev}"
992 probe_for_fs_label ()
998 for label in ${overlays}
1000 if [ "$(blkid -s LABEL -o value $dev 2>/dev/null)" = "${label}" ]
1002 echo "${label}=${dev}"
1007 probe_for_file_name ()
1009 local overlays dev ret backing
1014 backing="$(mount_persistence_media ${dev} probe)"
1015 if [ -z "${backing}" ]
1020 for label in ${overlays}
1022 path=${backing}/${PERSISTENCE_PATH}/${label}
1026 loopdev=$(setup_loop "${path}" "loop" "/sys/block/loop*")
1027 ret="${ret} ${label}=${loopdev}"
1035 # unmount and remove mountpoint
1036 umount ${backing} > /dev/null 2>&1 || true
1037 rmdir ${backing} > /dev/null 2>&1 || true
1041 probe_for_directory_name ()
1043 local overlays dev ret backing
1048 backing="$(mount_persistence_media ${dev} probe)"
1049 if [ -z "${backing}" ]
1054 for label in ${overlays}
1056 path=${backing}/${PERSISTENCE_PATH}/${label}
1059 # in this case the "device" ends with a "/"
1060 ret="${ret} ${label}=${backing}/${PERSISTENCE_PATH}/${label%%/}/"
1068 # unmount and remove mountpoint
1069 umount ${backing} > /dev/null 2>&1 || true
1070 rmdir ${backing} > /dev/null 2>&1 || true
1074 find_persistence_media ()
1076 # Scans devices for overlays, and returns a whitespace
1077 # separated list of how to use them. Only overlays with a partition
1078 # label or file name in ${overlays} are returned.
1080 # When scanning a LUKS device, the user will be asked to enter the
1081 # passphrase; on failure to enter it, or if no persistence partitions
1082 # or files were found, the LUKS device is closed.
1084 # For all other cases (overlay partition and overlay file) the
1085 # return value is "${label}=${device}", where ${device} a device that
1086 # can mount the content. In the case of an overlay file, the device
1087 # containing the file will remain mounted as a side-effect.
1089 # No devices in ${black_listed_devices} will be scanned, and if
1090 # ${white_list_devices} is non-empty, only devices in it will be
1093 local overlays white_listed_devices ret black_listed_devices
1095 white_listed_devices="${2}"
1099 # The devices that are hosting the actual live rootfs should not be
1100 # used for persistence storage since otherwise you might mount a
1101 # parent directory on top of a sub-directory of the same filesystem
1102 # in one union together.
1104 black_listed_devices=""
1105 for d in /run/live/rootfs/* /run/live/findiso /run/live/fromiso
1107 black_listed_devices="${black_listed_devices} $(what_is_mounted_on d)"
1110 for dev in $(storage_devices "${black_listed_devices}" "${white_listed_devices}")
1112 local result luks_device
1116 # Check if it's a luks device; we'll have to open the device
1117 # in order to probe any filesystem it contains, like we do
1118 # below. activate_custom_mounts() also depends on that any luks
1119 # device already has been opened.
1120 if is_in_comma_sep_list luks ${PERSISTENCE_ENCRYPTION} && is_luks_partition ${dev}
1122 if luks_device=$(open_luks_device "${dev}")
1124 dev="${luks_device}"
1126 # skip $dev since we failed/chose not to open it
1129 elif ! is_in_comma_sep_list none ${PERSISTENCE_ENCRYPTION}
1131 # skip $dev since we don't allow unencrypted storage
1135 # Probe for matching GPT partition names or filesystem labels
1136 if is_in_comma_sep_list filesystem ${PERSISTENCE_STORAGE}
1138 result=$(probe_for_gpt_name "${overlays}" ${dev})
1139 if [ -n "${result}" ]
1141 ret="${ret} ${result}"
1145 result=$(probe_for_fs_label "${overlays}" ${dev})
1146 if [ -n "${result}" ]
1148 ret="${ret} ${result}"
1153 # Probe for files with matching name on mounted partition
1154 if is_in_comma_sep_list file ${PERSISTENCE_STORAGE}
1156 result=$(probe_for_file_name "${overlays}" ${dev})
1157 if [ -n "${result}" ]
1160 loopdevice=${result##*=}
1161 if is_in_comma_sep_list luks ${PERSISTENCE_ENCRYPTION} && is_luks_partition ${loopdevice}
1165 if luksfile=$(open_luks_device "${loopdevice}")
1167 result=${result%%=*}
1168 result="${result}=${luksfile}"
1170 losetup -d $loopdevice
1174 ret="${ret} ${result}"
1179 # Probe for directory with matching name on mounted partition
1180 if is_in_comma_sep_list directory ${PERSISTENCE_STORAGE}
1182 result=$(probe_for_directory_name "${overlays}" ${dev})
1183 if [ -n "${result}" ]
1185 ret="${ret} ${result}"
1190 # Close luks device if it isn't used
1191 if [ -z "${result}" ] && [ -n "${luks_device}" ] && is_active_luks_mapping "${luks_device}"
1193 cryptsetup luksClose "${luks_device}"
1207 for adaptor in /sys/class/net/*
1209 status="$(cat ${adaptor}/iflink)"
1211 if [ "${status}" -eq 2 ]
1213 mac="$(cat ${adaptor}/address)"
1214 mac="$(echo ${mac} | sed 's/:/-/g' | tr '[a-z]' '[A-Z]')"
1221 is_luks_partition ()
1224 cryptsetup isLuks "${device}" 1>/dev/null 2>&1
1227 is_active_luks_mapping ()
1230 cryptsetup status "${device}" 1>/dev/null 2>&1
1233 get_luks_backing_device ()
1236 cryptsetup status ${device} 2> /dev/null | \
1237 awk '{if ($1 == "device:") print $2}'
1242 output_format="${1}"
1246 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1248 if [ ! -d "${sysblock}" ]; then
1253 if [ "$(cat ${sysblock}/removable)" = "1" ]
1255 if [ -z "${want_usb}" ]
1259 if readlink ${sysblock} | grep -q usb
1266 if [ "${dev_ok}" = "true" ]
1268 case "${output_format}" in
1270 ret="${ret} ${sysblock}"
1273 devname=$(sys2dev "${sysblock}")
1274 ret="${ret} ${devname}"
1283 removable_usb_dev ()
1285 output_format="${1}"
1287 removable_dev "${output_format}" "want_usb"
1290 non_removable_dev ()
1292 output_format="${1}"
1295 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1297 if [ ! -d "${sysblock}" ]; then
1301 if [ "$(cat ${sysblock}/removable)" = "0" ]
1303 case "${output_format}" in
1305 ret="${ret} ${sysblock}"
1308 devname=$(sys2dev "${sysblock}")
1309 ret="${ret} ${devname}"
1320 # create source's directory structure in dest, and recursively
1321 # create symlinks in dest to to all files in source. if mask
1322 # is non-empty, remove mask from all source paths when
1323 # creating links (will be necessary if we change root, which
1324 # live-boot normally does (into $rootmnt)).
1325 local src_dir dest_dir src_transform
1327 # remove multiple /:s and ensure ending on /
1328 src_dir="$(trim_path ${1})/"
1329 dest_dir="$(trim_path ${2})/"
1330 src_transform="${3}"
1332 # This check can only trigger on the inital, non-recursive call since
1333 # we create the destination before recursive calls
1334 if [ ! -d "${dest_dir}" ]
1336 log_warning_msg "Must link_files into a directory"
1340 find "${src_dir}" -mindepth 1 -maxdepth 1 | \
1343 local dest final_src
1344 dest="${dest_dir}$(basename "${src}")"
1347 if [ -z "$(ls -A "${src}")" ]
1351 if [ ! -d "${dest}" ]
1354 chown_ref "${src}" "${dest}"
1355 chmod_ref "${src}" "${dest}"
1357 link_files "${src}" "${dest}" "${src_transform}"
1360 if [ -n "${src_transform}" ]
1362 final_src="$(echo ${final_src} | sed "${src_transform}")"
1364 rm -rf "${dest}" 2> /dev/null
1365 ln -s "${final_src}" "${dest}"
1366 chown_ref "${src}" "${dest}"
1373 local unionmountpoint unionrw unionro
1374 unionmountpoint="${1}" # directory where the union is mounted
1376 unionrw="${1}" # branch where the union changes are stored
1378 unionro="${*}" # space separated list of read-only branches (optional)
1380 case "${UNIONTYPE}" in
1386 unionmountopts="-o noatime,${noxino_opt},dirs=${unionrw}=${rw_opt}"
1387 if [ -n "${unionro}" ]
1389 for rofs in ${unionro}
1391 unionmountopts="${unionmountopts}:${rofs}=${ro_opt}"
1397 # XXX: can unionro be optional? i.e. can overlay skip lowerdir?
1398 if [ -z "${unionro}" ]
1400 panic "overlay needs at least one lower filesystem (read-only branch)."
1402 # Multiple lower layers can now be given using the the colon (":") as a
1403 # separator character between the directory names.
1404 unionro="$(echo ${unionro} | sed -e 's| |:|g')"
1405 # overlayfs requires:
1406 # + a workdir to become mounted
1407 # + workdir and upperdir to reside under the same mount
1408 # + workdir and upperdir to be in separate directories
1409 mkdir -p "${unionrw}/rw"
1410 mkdir -p "${unionrw}/work"
1411 unionmountopts="-o noatime,lowerdir=${unionro},upperdir=${unionrw}/rw,workdir=${unionrw}/work"
1415 mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
1418 get_custom_mounts ()
1420 # Side-effect: leaves $devices with persistence.conf mounted in /run/live/persistence
1421 # Side-effect: prints info to file $custom_mounts
1423 local custom_mounts devices bindings links
1428 bindings="/tmp/bindings.list"
1429 links="/tmp/links.list"
1430 rm -rf ${bindings} ${links} 2> /dev/null
1432 for device in ${devices}
1434 local device_name backing include_list
1435 device_name="$(basename ${device})"
1436 backing=$(mount_persistence_media ${device})
1437 if [ -z "${backing}" ]
1442 if [ -r "${backing}/${persistence_list}" ]
1444 include_list="${backing}/${persistence_list}"
1449 if [ -n "${LIVE_BOOT_DEBUG}" ] && [ -e "${include_list}" ]
1451 cp ${include_list} /run/live/persistence/${persistence_list}.${device_name}
1454 while read dir options # < ${include_list}
1456 if echo ${dir} | grep -qe "^[[:space:]]*\(#.*\)\?$"
1458 # skipping empty or commented lines
1462 if trim_path ${dir} | grep -q -e "^[^/]" -e "^/lib" -e "^/run/live\(/.*\)\?$" -e "^/\(.*/\)\?\.\.\?\(/.*\)\?$"
1464 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."
1468 local opt_source opt_link source full_source full_dest
1471 for opt in $(echo ${options} | tr ',' ' ');
1475 opt_source=${opt#source=}
1483 log_warning_msg "Skipping custom mount with unknown option: ${opt}"
1490 if [ -n "${opt_source}" ]
1492 if echo ${opt_source} | grep -q -e "^/" -e "^\(.*/\)\?\.\.\?\(/.*\)\?$" && [ "${opt_source}" != "." ]
1494 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"
1497 source="${opt_source}"
1501 full_source="$(trim_path ${backing}/${source})"
1502 full_dest="$(trim_path ${rootmnt}/${dir})"
1503 if [ -n "${opt_link}" ]
1505 echo "${device} ${full_source} ${full_dest} ${options}" >> ${links}
1507 echo "${device} ${full_source} ${full_dest} ${options}" >> ${bindings}
1509 done < ${include_list}
1512 # We sort the list according to destination so we're sure that
1513 # we won't hide a previous mount. We also ignore duplicate
1514 # destinations in a more or less arbitrary way.
1515 [ -e "${bindings}" ] && sort -k3 -sbu ${bindings} >> ${custom_mounts} && rm ${bindings}
1517 # After all mounts are considered we add symlinks so they
1518 # won't be hidden by some mount.
1519 [ -e "${links}" ] && cat ${links} >> ${custom_mounts} && rm ${links}
1521 # We need to make sure that no two custom mounts have the same sources
1522 # or are nested; if that is the case, too much weird stuff can happen.
1523 local prev_source prev_dest
1524 prev_source="impossible source" # first iteration must not match
1526 # This sort will ensure that a source /a comes right before a source
1527 # /a/b so we only need to look at the previous source
1528 [ -e ${custom_mounts} ] && sort -k2 -b ${custom_mounts} |
1529 while read device source dest options
1531 if echo ${source} | grep -qe "^${prev_source}\(/.*\)\?$"
1533 panic "Two persistence mounts have the same or nested sources: ${source} on ${dest}, and ${prev_source} on ${prev_dest}"
1535 prev_source=${source}
1540 activate_custom_mounts ()
1542 local custom_mounts used_devices
1543 custom_mounts="${1}" # the ouput from get_custom_mounts()
1546 while read device source dest options # < ${custom_mounts}
1548 local opt_bind opt_link opt_union
1552 for opt in $(echo ${options} | tr ',' ' ');
1557 unset opt_link opt_union
1561 unset opt_bind opt_union
1565 unset opt_bind opt_link
1570 if [ -n "$(what_is_mounted_on "${dest}")" ]
1572 if [ "${dest}" = "${rootmnt}" ]
1576 log_warning_msg "Skipping custom mount ${dest}: $(what_is_mounted_on "${dest}") is already mounted there"
1581 if [ ! -d "${dest}" ]
1583 # create the destination and delete existing files in
1584 # its path that are in the way
1586 for dir in $(echo ${dest} | sed -e 's|/\+| |g')
1588 path=$(trim_path ${path}/${dir})
1596 if echo ${path} | grep -qe "^${rootmnt}/*home/[^/]\+"
1598 # if ${dest} is in /home try fixing proper ownership by assuming that the intended user is the first, which is usually the case
1599 # FIXME: this should really be handled by live-config since we don't know for sure which uid a certain user has until then
1600 chown 1000:1000 ${path}
1606 # if ${source} doesn't exist on our persistence media
1607 # we bootstrap it with $dest from the live filesystem.
1608 # this both makes sense and is critical if we're
1609 # dealing with /etc or other system dir.
1610 if [ ! -d "${source}" ]
1612 if [ -n "${PERSISTENCE_READONLY}" ]
1615 elif [ -n "${opt_union}" ] || [ -n "${opt_link}" ]
1617 # unions and don't need to be bootstrapped
1618 # link dirs can't be bootstrapped in a sensible way
1619 mkdir -p "${source}"
1620 chown_ref "${dest}" "${source}"
1621 chmod_ref "${dest}" "${source}"
1622 elif [ -n "${opt_bind}" ]
1624 # ensure that $dest is not copied *into* $source
1625 mkdir -p "$(dirname ${source})"
1626 cp -a "${dest}" "${source}"
1630 # XXX: If CONFIG_AUFS_ROBR is added to the Debian kernel we can
1631 # ignore the loop below and set rootfs_dest_backing=$dest
1632 local rootfs_dest_backing
1633 rootfs_dest_backing=""
1634 if [ -n "${opt_link}" ] || [ -n "${opt_union}" ]
1636 for d in /run/live/rootfs/*
1638 if [ -n "${rootmnt}" ]
1640 fs="${d}/$(echo ${dest} | sed -e "s|${rootmnt}||")"
1646 rootfs_dest_backing="${rootfs_dest_backing} ${fs}"
1651 local cow_dir links_source
1652 if [ -n "${opt_link}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1654 link_files ${source} ${dest} ""
1655 elif [ -n "${opt_link}" ] && [ -n "${PERSISTENCE_READONLY}" ]
1657 mkdir -p /run/live/persistence
1658 links_source=$(mktemp -d /run/live/persistence/links-source-XXXXXX)
1659 chown_ref ${source} ${links_source}
1660 chmod_ref ${source} ${links_source}
1661 # We put the cow dir in the below strange place to
1662 # make it absolutely certain that the link source
1663 # has its own directory and isn't nested with some
1664 # other custom mount (if so that mount's files would
1665 # be linked, causing breakage.
1666 cow_dir="/run/live/overlay/run/live/persistence/$(basename ${links_source})"
1668 chown_ref "${source}" "${cow_dir}"
1669 chmod_ref "${source}" "${cow_dir}"
1670 do_union ${links_source} ${cow_dir} ${source} ${rootfs_dest_backing}
1671 link_files ${links_source} ${dest} "s|^${rootmnt}||"
1672 elif [ -n "${opt_union}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1674 do_union ${dest} ${source} ${rootfs_dest_backing}
1675 elif [ -n "${opt_bind}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1677 mount -o bind "${source}" "${dest}"
1678 elif [ -n "${opt_bind}" -o -n "${opt_union}" ] && [ -n "${PERSISTENCE_READONLY}" ]
1680 # bind-mount and union mount are handled the same
1681 # in read-only mode, but note that rootfs_dest_backing
1682 # is non-empty (and necessary) only for unions
1683 cow_dir="/run/live/overlay/${dest}"
1684 if [ -e "${cow_dir}" ] && [ -z "${opt_link}" ]
1686 # If an earlier custom mount has files here
1687 # it will "block" the current mount's files
1688 # which is undesirable
1692 chown_ref "${source}" "${cow_dir}"
1693 chmod_ref "${source}" "${cow_dir}"
1694 if [ "${UNIONTYPE}" = "overlay" ]
1696 # When we use overlay we add the "/rw" postfix to our source when using it
1697 # as upper layer. Therefore we also have to add it here when using it as
1699 source="${source}/rw"
1701 do_union ${dest} ${cow_dir} ${source} ${rootfs_dest_backing}
1704 PERSISTENCE_IS_ON="1"
1705 export PERSISTENCE_IS_ON
1707 if echo ${used_devices} | grep -qve "^\(.* \)\?${device}\( .*\)\?$"
1709 used_devices="${used_devices} ${device}"
1711 done < ${custom_mounts}
1713 echo ${used_devices}
1720 [ $(stat -fc%d:%D "${directory}") != $(stat -fc%d:%D "${directory}/..") ]