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 # Try to look if it is already supported by the kernel
447 if grep -q ${fstype} /proc/filesystems
451 # Then try to add support for it the gentle way using the initramfs capabilities
452 modprobe -q -b ${fstype}
453 if grep -q ${fstype} /proc/filesystems
456 # Then try the hard way if /root is already reachable
458 kmodule="/root/lib/modules/`uname -r`/${fstype}/${fstype}.ko"
459 if [ -e "${kmodule}" ]
462 if grep -q ${fstype} /proc/filesystems
475 /sbin/blkid -s TYPE -o value $1 2>/dev/null
482 grep -m1 "^${device} " /proc/mounts | cut -f2 -d ' '
487 # remove all unnecessary /:s in the path, including last one (except
488 # if path is just "/")
489 echo ${1} | sed 's|//\+|/|g' | sed 's|^\(.*[^/]\)/$|\1|'
492 what_is_mounted_on ()
495 dir="$(trim_path ${1})"
496 grep -m1 "^[^ ]\+ ${dir} " /proc/mounts | cut -d' ' -f1
501 local reference targets owner
505 owner=$(stat -c %u:%g "${reference}")
506 chown -h ${owner} ${targets}
511 local reference targets rights
515 rights=$(stat -c %a "${reference}")
516 chmod ${rights} ${targets}
532 mounts="$(awk '{print $2}' /proc/mounts)"
533 testpath="$(busybox realpath ${testpath})"
537 if echo "${mounts}" | grep -qs "^${testpath}"
539 set -- $(echo "${mounts}" | grep "^${testpath}" | lastline)
543 testpath=$(dirname $testpath)
550 # Returns used/free fs kbytes + 5% more
551 # You could pass a block device as ${1} or the mount point as ${2}
557 if [ -z "${mountp}" ]
559 mountp="$(where_is_mounted ${dev})"
561 if [ -z "${mountp}" ]
563 mountp="/mnt/tmp_fs_size"
566 mount -t $(get_fstype "${dev}") -o ro "${dev}" "${mountp}" || log_warning_msg "cannot mount -t $(get_fstype ${dev}) -o ro ${dev} ${mountp}"
572 if [ "${used}" = "used" ]
574 size=$(du -ks ${mountp} | cut -f1)
575 size=$(expr ${size} + ${size} / 20 ) # FIXME: 5% more to be sure
578 size="$(df -kP | grep -s ${mountp} | awk '{print $4}')"
581 if [ -n "${doumount}" ]
583 umount "${mountp}" || log_warning_msg "cannot umount ${mountp}"
593 if [ -x /bin/loadkeys -a -r /etc/boottime.kmap.gz ]
595 loadkeys --quiet /etc/boottime.kmap.gz
601 local fspath module pattern offset encryption readonly
609 # the output of setup_loop is evaluated in other functions,
610 # modprobe leaks kernel options like "libata.dma=0"
611 # as "options libata dma=0" on stdout, causing serious
612 # problems therefor, so instead always avoid output to stdout
613 modprobe -q -b "${module}" 1>/dev/null
617 for loopdev in ${pattern}
619 if [ "$(cat ${loopdev}/size)" -eq 0 ]
621 dev=$(sys2dev "${loopdev}")
624 if [ -n "${readonly}" ]
626 if losetup --help 2>&1 | grep -q -- "-r\b"
628 options="${options} -r"
632 if [ -n "${offset}" ] && [ 0 -lt "${offset}" ]
634 options="${options} -o ${offset}"
637 if [ -z "${encryption}" ]
639 losetup ${options} "${dev}" "${fspath}"
641 # Loop AES encryption
646 echo -n "Enter passphrase for root filesystem: " >&6
648 echo "${passphrase}" > /tmp/passphrase
650 exec 9</tmp/passphrase
651 /sbin/losetup ${options} -e "${encryption}" -p 9 "${dev}" "${fspath}"
654 rm -f /tmp/passphrase
656 if [ 0 -eq ${error} ]
663 echo -n "There was an error decrypting the root filesystem ... Retry? [Y/n] " >&6
666 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
679 panic "No loop devices available"
689 old_mountp="$(where_is_mounted ${dev})"
691 if [ -n "${old_mountp}" ]
693 if [ "${opts}" != "ro" ]
695 mount -o remount,"${opts}" "${dev}" "${old_mountp}" || panic "Remounting ${dev} ${opts} on ${old_mountp} failed"
698 mount -o bind "${old_mountp}" "${mountp}" || panic "Cannot bind-mount ${old_mountp} on ${mountp}"
700 if [ -z "${fstype}" ]
702 fstype=$(get_fstype "${dev}")
704 mount -t "${fstype}" -o "${opts}" "${dev}" "${mountp}" || \
705 ( echo "SKIPPING: Cannot mount ${dev} on ${mountp}, fstype=${fstype}, options=${opts}" > boot.log && return 0 )
709 # Try to mount $device to the place expected by live-boot. If $device
710 # is already mounted somewhere, move it to the expected place. If $device
711 # ends with a "/" this is a directory path.
712 # If we're only probing $device (to check if it has custom persistence)
713 # $probe should be set, which suppresses warnings upon failure. On
714 # success, print the mount point for $device.
715 mount_persistence_media ()
717 local device probe backing old_backing fstype mount_opts
721 # get_custom_mounts() might call this with a directory path instead
722 # of a block device path. This means we have found sub-directory path
723 # underneath /lib/live/mounts/persistence, so we're done
724 if [ -d "${device}" ]
730 if [ ! -b "${device}" ]
735 backing="/live/persistence/$(basename ${device})"
737 mkdir -p "${backing}"
738 old_backing="$(where_is_mounted ${device})"
739 if [ -z "${old_backing}" ]
741 fstype="$(get_fstype ${device})"
742 mount_opts="rw,noatime"
743 if [ -n "${PERSISTENCE_READONLY}" ]
745 mount_opts="ro,noatime"
747 if mount -t "${fstype}" -o "${mount_opts}" "${device}" "${backing}" >/dev/null
752 [ -z "${probe}" ] && log_warning_msg "Failed to mount persistence media ${device}"
756 elif [ "${backing}" != "${old_backing}" ]
758 if ! mount --move ${old_backing} ${backing} >/dev/null
760 [ -z "${probe}" ] && log_warning_msg "Failed to move persistence media ${device}"
764 mount_opts="rw,noatime"
765 if [ -n "${PERSISTENCE_READONLY}" ]
767 mount_opts="ro,noatime"
769 if ! mount -o "remount,${mount_opts}" "${backing}" >/dev/null
771 log_warning_msg "Failed to remount persistence media ${device} writable"
772 # Don't unmount or rmdir the new mountpoint in this case
777 # This means that $device has already been mounted on
778 # the place expected by live-boot, so we're done.
784 close_persistence_media ()
788 backing="$(where_is_mounted ${device})"
790 if [ -d "${backing}" ]
792 umount "${backing}" >/dev/null 2>&1
793 rmdir "${backing}" >/dev/null 2>&1
796 if is_active_luks_mapping ${device}
798 /sbin/cryptsetup luksClose ${device}
805 name="$(basename ${dev})"
807 if [ -n "${PERSISTENCE_READONLY}" ]
809 opts="${opts} --readonly"
812 if /sbin/cryptsetup status "${name}" >/dev/null 2>&1
814 re="^[[:space:]]*device:[[:space:]]*\([^[:space:]]*\)$"
815 opened_dev=$(cryptsetup status ${name} 2>/dev/null | grep "${re}" | sed "s|${re}|\1|")
816 if [ "${opened_dev}" = "${dev}" ]
818 luks_device="/dev/mapper/${name}"
822 log_warning_msg "Cannot open luks device ${dev} since ${opened_dev} already is opened with its name"
830 if [ -x /bin/plymouth ]
835 case "${_PLYMOUTH}" in
839 cryptkeyscript="plymouth ask-for-password --prompt"
840 # Plymouth will add a : if it is a non-graphical prompt
841 cryptkeyprompt="Please unlock disk ${dev}"
845 cryptkeyscript="/lib/cryptsetup/askpass"
846 cryptkeyprompt="Please unlock disk ${dev}: "
852 $cryptkeyscript "$cryptkeyprompt" | \
853 /sbin/cryptsetup -T 1 luksOpen ${dev} ${name} ${opts}
857 luks_device="/dev/mapper/${name}"
863 retryprompt="There was an error decrypting ${dev} ... Retry? [Y/n]"
865 case "${_PLYMOUTH}" in
867 plymouth display-message --text "${retryprompt}"
868 answer=$(plymouth watch-keystroke --keys="YNyn")
872 echo -n "${retryprompt} " >&6
877 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
879 case "${_PLYMOUTH}" in
881 plymouth display-message --text ""
894 /sbin/blkid -s PART_ENTRY_NAME -p -o value ${dev} 2>/dev/null
901 [ "$(/sbin/blkid -s PART_ENTRY_SCHEME -p -o value ${dev} 2>/dev/null)" = "gpt" ]
904 probe_for_gpt_name ()
906 local overlays dev gpt_dev gpt_name
911 if is_active_luks_mapping ${dev}
913 # if $dev is an opened luks device, we need to check
914 # GPT stuff on the backing device
915 gpt_dev=$(get_luks_backing_device "${dev}")
918 if ! is_gpt_device ${gpt_dev}
923 gpt_name=$(get_gpt_name ${gpt_dev})
924 for label in ${overlays}
926 if [ "${gpt_name}" = "${label}" ]
928 echo "${label}=${dev}"
933 probe_for_fs_label ()
939 for label in ${overlays}
941 if [ "$(/sbin/blkid -s LABEL -o value $dev 2>/dev/null)" = "${label}" ]
943 echo "${label}=${dev}"
948 probe_for_file_name ()
950 local overlays dev ret backing
955 backing="$(mount_persistence_media ${dev} probe)"
956 if [ -z "${backing}" ]
961 for label in ${overlays}
963 path=${backing}/${PERSISTENCE_PATH}/${label}
967 loopdev=$(setup_loop "${path}" "loop" "/sys/block/loop*")
968 ret="${ret} ${label}=${loopdev}"
976 # unmount and remove mountpoint
977 umount ${backing} > /dev/null 2>&1 || true
978 rmdir ${backing} > /dev/null 2>&1 || true
982 probe_for_directory_name ()
984 local overlays dev ret backing
989 backing="$(mount_persistence_media ${dev} probe)"
990 if [ -z "${backing}" ]
995 for label in ${overlays}
997 path=${backing}/${PERSISTENCE_PATH}/${label}
1000 # in this case the "device" ends with a "/"
1001 ret="${ret} ${label}=${backing}/${PERSISTENCE_PATH}/${label%%/}/"
1009 # unmount and remove mountpoint
1010 umount ${backing} > /dev/null 2>&1 || true
1011 rmdir ${backing} > /dev/null 2>&1 || true
1015 find_persistence_media ()
1017 # Scans devices for overlays, and returns a whitespace
1018 # separated list of how to use them. Only overlays with a partition
1019 # label or file name in ${overlays} are returned.
1021 # When scanning a LUKS device, the user will be asked to enter the
1022 # passphrase; on failure to enter it, or if no persistence partitions
1023 # or files were found, the LUKS device is closed.
1025 # For all other cases (overlay partition and overlay file) the
1026 # return value is "${label}=${device}", where ${device} a device that
1027 # can mount the content. In the case of an overlay file, the device
1028 # containing the file will remain mounted as a side-effect.
1030 # No devices in ${black_listed_devices} will be scanned, and if
1031 # ${white_list_devices} is non-empty, only devices in it will be
1034 local overlays white_listed_devices ret black_listed_devices
1036 white_listed_devices="${2}"
1040 # The devices that are hosting the actual live rootfs should not be
1041 # used for persistence storage since otherwise you might mount a
1042 # parent directory on top of a sub-directory of the same filesystem
1043 # in one union together.
1045 black_listed_devices=""
1046 for d in /live/rootfs/* /live/findiso /live/fromiso
1048 black_listed_devices="${black_listed_devices} $(what_is_mounted_on d)"
1051 for dev in $(storage_devices "${black_listed_devices}" "${white_listed_devices}")
1053 local result luks_device
1057 # Check if it's a luks device; we'll have to open the device
1058 # in order to probe any filesystem it contains, like we do
1059 # below. activate_custom_mounts() also depends on that any luks
1060 # device already has been opened.
1061 if is_in_comma_sep_list luks ${PERSISTENCE_ENCRYPTION} && is_luks_partition ${dev}
1063 if luks_device=$(open_luks_device "${dev}")
1065 dev="${luks_device}"
1067 # skip $dev since we failed/chose not to open it
1070 elif ! is_in_comma_sep_list none ${PERSISTENCE_ENCRYPTION}
1072 # skip $dev since we don't allow unencrypted storage
1076 # Probe for matching GPT partition names or filesystem labels
1077 if is_in_comma_sep_list filesystem ${PERSISTENCE_STORAGE}
1079 result=$(probe_for_gpt_name "${overlays}" ${dev})
1080 if [ -n "${result}" ]
1082 ret="${ret} ${result}"
1086 result=$(probe_for_fs_label "${overlays}" ${dev})
1087 if [ -n "${result}" ]
1089 ret="${ret} ${result}"
1094 # Probe for files with matching name on mounted partition
1095 if is_in_comma_sep_list file ${PERSISTENCE_STORAGE}
1097 result=$(probe_for_file_name "${overlays}" ${dev})
1098 if [ -n "${result}" ]
1101 loopdevice=${result##*=}
1102 if is_in_comma_sep_list luks ${PERSISTENCE_ENCRYPTION} && is_luks_partition ${loopdevice}
1106 if luksfile=$(open_luks_device "${loopdevice}")
1108 result=${result%%=*}
1109 result="${result}=${luksfile}"
1111 losetup -d $loopdevice
1115 ret="${ret} ${result}"
1120 # Probe for directory with matching name on mounted partition
1121 if is_in_comma_sep_list directory ${PERSISTENCE_STORAGE}
1123 result=$(probe_for_directory_name "${overlays}" ${dev})
1124 if [ -n "${result}" ]
1126 ret="${ret} ${result}"
1131 # Close luks device if it isn't used
1132 if [ -z "${result}" ] && [ -n "${luks_device}" ] && is_active_luks_mapping "${luks_device}"
1134 /sbin/cryptsetup luksClose "${luks_device}"
1148 for adaptor in /sys/class/net/*
1150 status="$(cat ${adaptor}/iflink)"
1152 if [ "${status}" -eq 2 ]
1154 mac="$(cat ${adaptor}/address)"
1155 mac="$(echo ${mac} | sed 's/:/-/g' | tr '[a-z]' '[A-Z]')"
1162 is_luks_partition ()
1165 /sbin/cryptsetup isLuks "${device}" 1>/dev/null 2>&1
1168 is_active_luks_mapping ()
1171 /sbin/cryptsetup status "${device}" 1>/dev/null 2>&1
1174 get_luks_backing_device ()
1177 cryptsetup status ${device} 2> /dev/null | \
1178 awk '{if ($1 == "device:") print $2}'
1183 output_format="${1}"
1187 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1190 if [ "$(cat ${sysblock}/removable)" = "1" ]
1192 if [ -z "${want_usb}" ]
1196 if readlink ${sysblock} | grep -q usb
1203 if [ "${dev_ok}" = "true" ]
1205 case "${output_format}" in
1207 ret="${ret} ${sysblock}"
1210 devname=$(sys2dev "${sysblock}")
1211 ret="${ret} ${devname}"
1220 removable_usb_dev ()
1222 output_format="${1}"
1224 removable_dev "${output_format}" "want_usb"
1227 non_removable_dev ()
1229 output_format="${1}"
1232 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1234 if [ "$(cat ${sysblock}/removable)" = "0" ]
1236 case "${output_format}" in
1238 ret="${ret} ${sysblock}"
1241 devname=$(sys2dev "${sysblock}")
1242 ret="${ret} ${devname}"
1253 # create source's directory structure in dest, and recursively
1254 # create symlinks in dest to to all files in source. if mask
1255 # is non-empty, remove mask from all source paths when
1256 # creating links (will be necessary if we change root, which
1257 # live-boot normally does (into $rootmnt)).
1258 local src_dir dest_dir src_transform
1260 # remove multiple /:s and ensure ending on /
1261 src_dir="$(trim_path ${1})/"
1262 dest_dir="$(trim_path ${2})/"
1263 src_transform="${3}"
1265 # This check can only trigger on the inital, non-recursive call since
1266 # we create the destination before recursive calls
1267 if [ ! -d "${dest_dir}" ]
1269 log_warning_msg "Must link_files into a directory"
1273 find "${src_dir}" -mindepth 1 -maxdepth 1 | \
1276 local dest final_src
1277 dest="${dest_dir}$(basename "${src}")"
1280 if [ -z "$(ls -A "${src}")" ]
1284 if [ ! -d "${dest}" ]
1287 chown_ref "${src}" "${dest}"
1288 chmod_ref "${src}" "${dest}"
1290 link_files "${src}" "${dest}" "${src_transform}"
1293 if [ -n "${src_transform}" ]
1295 final_src="$(echo ${final_src} | sed "${src_transform}")"
1297 rm -rf "${dest}" 2> /dev/null
1298 ln -s "${final_src}" "${dest}"
1299 chown_ref "${src}" "${dest}"
1306 local unionmountpoint unionrw unionro
1307 unionmountpoint="${1}" # directory where the union is mounted
1309 unionrw="${1}" # branch where the union changes are stored
1311 unionro="${*}" # space separated list of read-only branches (optional)
1313 case "${UNIONTYPE}" in
1319 unionmountopts="-o noatime,${noxino_opt},dirs=${unionrw}=${rw_opt}"
1320 if [ -n "${unionro}" ]
1322 for rofs in ${unionro}
1324 unionmountopts="${unionmountopts}:${rofs}=${ro_opt}"
1330 # XXX: can multiple unionro be used? (overlay only handles two dirs, but perhaps they can be chained?)
1331 # XXX: and can unionro be optional? i.e. can overlay skip lowerdir?
1332 if echo ${unionro} | grep -q " "
1334 panic "Multiple lower filesystems are currently not supported with overlay (unionro = ${unionro})."
1335 elif [ -z "${unionro}" ]
1337 panic "overlay needs at least one lower filesystem (read-only branch)."
1339 # overlayfs requires:
1340 # + a workdir to become mounted
1341 # + workdir and upperdir to reside under the same mount
1342 # + workdir and upperdir to be in separate directories
1343 mkdir "${unionrw}/rw"
1344 mkdir "${unionrw}/work"
1345 unionmountopts="-o noatime,lowerdir=${unionro},upperdir=${unionrw}/rw,workdir=${unionrw}/work"
1349 mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
1352 get_custom_mounts ()
1354 # Side-effect: leaves $devices with persistence.conf mounted in /live/persistence
1355 # Side-effect: prints info to file $custom_mounts
1357 local custom_mounts devices bindings links
1362 bindings="/tmp/bindings.list"
1363 links="/tmp/links.list"
1364 rm -rf ${bindings} ${links} 2> /dev/null
1366 for device in ${devices}
1368 local device_name backing include_list
1369 device_name="$(basename ${device})"
1370 backing=$(mount_persistence_media ${device})
1371 if [ -z "${backing}" ]
1376 if [ -r "${backing}/${persistence_list}" ]
1378 include_list="${backing}/${persistence_list}"
1383 if [ -n "${LIVE_BOOT_DEBUG}" ] && [ -e "${include_list}" ]
1385 cp ${include_list} /live/persistence/${persistence_list}.${device_name}
1388 while read dir options # < ${include_list}
1390 if echo ${dir} | grep -qe "^[[:space:]]*\(#.*\)\?$"
1392 # skipping empty or commented lines
1396 if trim_path ${dir} | grep -q -e "^[^/]" -e "^/lib" -e "^/lib/live\(/.*\)\?$" -e "^/\(.*/\)\?\.\.\?\(/.*\)\?$"
1398 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."
1402 local opt_source opt_link source full_source full_dest
1405 for opt in $(echo ${options} | tr ',' ' ');
1409 opt_source=${opt#source=}
1417 log_warning_msg "Skipping custom mount with unknown option: ${opt}"
1424 if [ -n "${opt_source}" ]
1426 if echo ${opt_source} | grep -q -e "^/" -e "^\(.*/\)\?\.\.\?\(/.*\)\?$" && [ "${opt_source}" != "." ]
1428 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"
1431 source="${opt_source}"
1435 full_source="$(trim_path ${backing}/${source})"
1436 full_dest="$(trim_path ${rootmnt}/${dir})"
1437 if [ -n "${opt_link}" ]
1439 echo "${device} ${full_source} ${full_dest} ${options}" >> ${links}
1441 echo "${device} ${full_source} ${full_dest} ${options}" >> ${bindings}
1443 done < ${include_list}
1446 # We sort the list according to destination so we're sure that
1447 # we won't hide a previous mount. We also ignore duplicate
1448 # destinations in a more or less arbitrary way.
1449 [ -e "${bindings}" ] && sort -k3 -sbu ${bindings} >> ${custom_mounts} && rm ${bindings}
1451 # After all mounts are considered we add symlinks so they
1452 # won't be hidden by some mount.
1453 [ -e "${links}" ] && cat ${links} >> ${custom_mounts} && rm ${links}
1455 # We need to make sure that no two custom mounts have the same sources
1456 # or are nested; if that is the case, too much weird stuff can happen.
1457 local prev_source prev_dest
1458 prev_source="impossible source" # first iteration must not match
1460 # This sort will ensure that a source /a comes right before a source
1461 # /a/b so we only need to look at the previous source
1462 [ -e ${custom_mounts} ] && sort -k2 -b ${custom_mounts} |
1463 while read device source dest options
1465 if echo ${source} | grep -qe "^${prev_source}\(/.*\)\?$"
1467 panic "Two persistence mounts have the same or nested sources: ${source} on ${dest}, and ${prev_source} on ${prev_dest}"
1469 prev_source=${source}
1474 activate_custom_mounts ()
1476 local custom_mounts used_devices
1477 custom_mounts="${1}" # the ouput from get_custom_mounts()
1480 while read device source dest options # < ${custom_mounts}
1482 local opt_bind opt_link opt_union
1486 for opt in $(echo ${options} | tr ',' ' ');
1491 unset opt_link opt_union
1495 unset opt_bind opt_union
1499 unset opt_bind opt_link
1504 if [ -n "$(what_is_mounted_on "${dest}")" ]
1506 if [ "${dest}" = "${rootmnt}" ]
1510 log_warning_msg "Skipping custom mount ${dest}: $(what_is_mounted_on "${dest}") is already mounted there"
1515 if [ ! -d "${dest}" ]
1517 # create the destination and delete existing files in
1518 # its path that are in the way
1520 for dir in $(echo ${dest} | sed -e 's|/\+| |g')
1522 path=$(trim_path ${path}/${dir})
1530 if echo ${path} | grep -qe "^${rootmnt}/*home/[^/]\+"
1532 # if ${dest} is in /home try fixing proper ownership by assuming that the intended user is the first, which is usually the case
1533 # FIXME: this should really be handled by live-config since we don't know for sure which uid a certain user has until then
1534 chown 1000:1000 ${path}
1540 # if ${source} doesn't exist on our persistence media
1541 # we bootstrap it with $dest from the live filesystem.
1542 # this both makes sense and is critical if we're
1543 # dealing with /etc or other system dir.
1544 if [ ! -d "${source}" ]
1546 if [ -n "${PERSISTENCE_READONLY}" ]
1549 elif [ -n "${opt_union}" ] || [ -n "${opt_link}" ]
1551 # unions and don't need to be bootstrapped
1552 # link dirs can't be bootstrapped in a sensible way
1553 mkdir -p "${source}"
1554 chown_ref "${dest}" "${source}"
1555 chmod_ref "${dest}" "${source}"
1556 elif [ -n "${opt_bind}" ]
1558 # ensure that $dest is not copied *into* $source
1559 mkdir -p "$(dirname ${source})"
1560 cp -a "${dest}" "${source}"
1564 # XXX: If CONFIG_AUFS_ROBR is added to the Debian kernel we can
1565 # ignore the loop below and set rootfs_dest_backing=$dest
1566 local rootfs_dest_backing
1567 rootfs_dest_backing=""
1568 if [ -n "${opt_link}" ] || [ -n "${opt_union}" ]
1570 for d in /live/rootfs/*
1572 if [ -n "${rootmnt}" ]
1574 fs="${d}/$(echo ${dest} | sed -e "s|${rootmnt}||")"
1580 rootfs_dest_backing="${rootfs_dest_backing} ${fs}"
1585 local cow_dir links_source
1586 if [ -n "${opt_link}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1588 link_files ${source} ${dest} "s|^/live/|/lib/live/mount/|"
1589 elif [ -n "${opt_link}" ] && [ -n "${PERSISTENCE_READONLY}" ]
1591 mkdir -p ${rootmnt}/lib/live/mount/persistence
1592 links_source=$(mktemp -d ${rootmnt}/lib/live/mount/persistence/links-source-XXXXXX)
1593 chown_ref ${source} ${links_source}
1594 chmod_ref ${source} ${links_source}
1595 # We put the cow dir in the below strange place to
1596 # make it absolutely certain that the link source
1597 # has its own directory and isn't nested with some
1598 # other custom mount (if so that mount's files would
1599 # be linked, causing breakage.
1600 cow_dir="/live/overlay/lib/live/mount/persistence/$(basename ${links_source})"
1602 chown_ref "${source}" "${cow_dir}"
1603 chmod_ref "${source}" "${cow_dir}"
1604 do_union ${links_source} ${cow_dir} ${source} ${rootfs_dest_backing}
1605 link_files ${links_source} ${dest} "s|^${rootmnt}||"
1606 elif [ -n "${opt_union}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1608 do_union ${dest} ${source} ${rootfs_dest_backing}
1609 elif [ -n "${opt_bind}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1611 mount --bind "${source}" "${dest}"
1612 elif [ -n "${opt_bind}" -o -n "${opt_union}" ] && [ -n "${PERSISTENCE_READONLY}" ]
1614 # bind-mount and union mount are handled the same
1615 # in read-only mode, but note that rootfs_dest_backing
1616 # is non-empty (and necessary) only for unions
1617 cow_dir="/live/overlay/${dest}"
1618 if [ -e "${cow_dir}" ] && [ -z "${opt_link}" ]
1620 # If an earlier custom mount has files here
1621 # it will "block" the current mount's files
1622 # which is undesirable
1626 chown_ref "${source}" "${cow_dir}"
1627 chmod_ref "${source}" "${cow_dir}"
1628 do_union ${dest} ${cow_dir} ${source} ${rootfs_dest_backing}
1631 PERSISTENCE_IS_ON="1"
1632 export PERSISTENCE_IS_ON
1634 if echo ${used_devices} | grep -qve "^\(.* \)\?${device}\( .*\)\?$"
1636 used_devices="${used_devices} ${device}"
1638 done < ${custom_mounts}
1640 echo ${used_devices}
1647 [ $(stat -fc%d:%D "${directory}") != $(stat -fc%d:%D "${directory}/..") ]