9 if [ -d "${DIRECTORY}"/"${LIVE_MEDIA_PATH}" ]
11 for FILESYSTEM in squashfs ext2 ext3 ext4 xfs dir jffs2
13 if [ "$(echo ${DIRECTORY}/${LIVE_MEDIA_PATH}/*.${FILESYSTEM})" != "${DIRECTORY}/${LIVE_MEDIA_PATH}/*.${FILESYSTEM}" ]
25 if [ "${IGNORE_UUID}" ] || [ ! -e /conf/uuid.conf ]
31 uuid="$(cat /conf/uuid.conf)"
33 for try_uuid_file in "${path}/.disk/live-uuid"*
35 [ -e "${try_uuid_file}" ] || continue
37 try_uuid="$(cat "${try_uuid_file}")"
39 if [ "${uuid}" = "${try_uuid}" ]
51 *.squashfs|*.ext2|*.ext3|*.ext4|*.jffs2)
52 echo $(setup_loop "${1}" "loop" "/sys/block/loop*" '0' "${LIVE_MEDIA_ENCRYPTION}" "${2}")
60 panic "Unrecognized live filesystem: ${1}"
67 # Does any files match pattern ${1} ?
70 if [ "$(echo ${pattern})" != "${pattern}" ]
78 mount_images_in_directory ()
85 if match_files_in_dir "${directory}/${LIVE_MEDIA_PATH}/*.squashfs" ||
86 match_files_in_dir "${directory}/${LIVE_MEDIA_PATH}/*.ext2" ||
87 match_files_in_dir "${directory}/${LIVE_MEDIA_PATH}/*.ext3" ||
88 match_files_in_dir "${directory}/${LIVE_MEDIA_PATH}/*.ext4" ||
89 match_files_in_dir "${directory}/${LIVE_MEDIA_PATH}/*.jffs2" ||
90 match_files_in_dir "${directory}/${LIVE_MEDIA_PATH}/*.dir"
92 [ -n "${mac}" ] && adddirectory="${directory}/${LIVE_MEDIA_PATH}/${mac}"
93 setup_unionfs "${directory}/${LIVE_MEDIA_PATH}" "${rootmnt}" "${adddirectory}"
95 panic "No supported filesystem images found at /${LIVE_MEDIA_PATH}."
101 sysfs_path="${1#/sys}"
103 if [ -e /lib/udev/path_id ]
106 PATH_ID="/lib/udev/path_id"
108 # wheezy/sid (udev >= 174)
109 PATH_ID="/sbin/udevadm test-builtin path_id"
112 if ${PATH_ID} "${sysfs_path}" | egrep -q "ID_PATH=(usb|pci-[^-]*-(ide|sas|scsi|usb|virtio)|platform-sata_mv|platform-orion-ehci|platform-mmc|platform-mxsdhci)"
115 elif echo "${sysfs_path}" | grep -q '^/block/vd[a-z]$'
118 elif echo ${sysfs_path} | grep -q "^/block/dm-"
121 elif echo ${sysfs_path} | grep -q "^/block/mtdblock"
133 skip_uuid_check="${3}"
135 # support for fromiso=.../isofrom=....
138 ISO_DEVICE=$(dirname $FROMISO)
139 if ! [ -b $ISO_DEVICE ]
141 # to support unusual device names like /dev/cciss/c0d0p1
142 # as well we have to identify the block device name, let's
143 # do that for up to 15 levels
145 while [ -n "$ISO_DEVICE" ] && [ "$i" -gt 0 ]
147 ISO_DEVICE=$(dirname ${ISO_DEVICE})
148 [ -b "$ISO_DEVICE" ] && break
153 if [ "$ISO_DEVICE" = "/" ]
155 echo "Warning: device for bootoption fromiso= ($FROMISO) not found.">>/boot.log
157 fs_type=$(get_fstype "${ISO_DEVICE}")
158 if is_supported_fs ${fs_type}
161 mount -t $fs_type "$ISO_DEVICE" /live/fromiso
162 ISO_NAME="$(echo $FROMISO | sed "s|$ISO_DEVICE||")"
163 loopdevname=$(setup_loop "/live/fromiso/${ISO_NAME}" "loop" "/sys/block/loop*" "" '')
164 devname="${loopdevname}"
166 echo "Warning: unable to mount $ISO_DEVICE." >>/boot.log
171 if [ -z "${devname}" ]
173 devname=$(sys2dev "${sysdev}")
176 if [ -d "${devname}" ]
178 mount -o bind "${devname}" $mountpoint || continue
180 if is_live_path $mountpoint
190 for device in ${devname}
195 if [ -x /scripts/local-top/lvm2 ]
197 ROOT="$device" resume="" /scripts/local-top/lvm2
202 # Adding raid support
203 if [ -x /scripts/local-top/mdadm ]
205 cp /conf/conf.d/md /conf/conf.d/md.orig
206 echo "MD_DEVS=$device " >> /conf/conf.d/md
207 /scripts/local-top/mdadm
208 mv /conf/conf.d/md.orig /conf/conf.d/md
215 [ -n "$device" ] && devname="$device"
217 [ -e "$devname" ] || continue
219 if [ -n "${LIVE_MEDIA_OFFSET}" ]
221 loopdevname=$(setup_loop "${devname}" "loop" "/sys/block/loop*" "${LIVE_MEDIA_OFFSET}" '')
222 devname="${loopdevname}"
225 fstype=$(get_fstype "${devname}")
227 if is_supported_fs ${fstype}
229 devuid=$(blkid -o value -s UUID "$devname")
230 [ -n "$devuid" ] && grep -qs "\<$devuid\>" $tried && continue
231 mount -t ${fstype} -o ro,noatime "${devname}" ${mountpoint} || continue
232 [ -n "$devuid" ] && echo "$devuid" >> $tried
234 if [ -n "${FINDISO}" ]
236 if [ -f ${mountpoint}/${FINDISO} ]
239 mkdir -p /live/findiso
240 mount -t ${fstype} -o ro,noatime "${devname}" /live/findiso
241 loopdevname=$(setup_loop "/live/findiso/${FINDISO}" "loop" "/sys/block/loop*" 0 "")
242 devname="${loopdevname}"
243 mount -t iso9660 -o ro,noatime "${devname}" ${mountpoint}
249 if is_live_path ${mountpoint} && \
250 ([ "${skip_uuid_check}" ] || matches_uuid ${mountpoint})
255 umount ${mountpoint} 2>/dev/null
259 if [ -n "${LIVE_MEDIA_OFFSET}" ]
261 losetup -d "${loopdevname}"
271 # don't start autodetection before timeout has expired
272 if [ -n "${LIVE_MEDIA_TIMEOUT}" ]
274 if [ "${timeout}" -lt "${LIVE_MEDIA_TIMEOUT}" ]
280 # first look at the one specified in the command line
281 case "${LIVE_MEDIA}" in
283 for sysblock in $(removable_usb_dev "sys")
285 for dev in $(subdevices "${sysblock}")
287 if check_dev "${dev}"
297 for sysblock in $(removable_dev "sys")
299 for dev in $(subdevices "${sysblock}")
301 if check_dev "${dev}"
311 if [ ! -z "${LIVE_MEDIA}" ]
313 if check_dev "null" "${LIVE_MEDIA}" "skip_uuid_check"
321 # or do the scan of block devices
322 # prefer removable devices over non-removable devices, so scan them first
323 devices_to_scan="$(removable_dev 'sys') $(non_removable_dev 'sys')"
325 for sysblock in $devices_to_scan
327 devname=$(sys2dev "${sysblock}")
328 [ -e "$devname" ] || continue
329 fstype=$(get_fstype "${devname}")
331 if /lib/udev/cdrom_id ${devname} > /dev/null
333 if check_dev "null" "${devname}"
337 elif is_nice_device "${sysblock}"
339 for dev in $(subdevices "${sysblock}")
341 if check_dev "${dev}"
346 elif [ "${fstype}" = "squashfs" -o \
347 "${fstype}" = "btrfs" -o \
348 "${fstype}" = "ext2" -o \
349 "${fstype}" = "ext3" -o \
350 "${fstype}" = "ext4" -o \
351 "${fstype}" = "jffs2" ]
353 # This is an ugly hack situation, the block device has
354 # an image directly on it. It's hopefully
355 # live-boot, so take it and run with it.
356 ln -s "${devname}" "${devname}.${fstype}"
357 echo "${devname}.${fstype}"
368 VALUE="$(eval echo -n \${$STRING})"
370 if [ -f /live.vars ] && grep -sq "export ${STRING}" /live.vars
372 sed -i -e 's/\('${STRING}'=\).*$/\1'${VALUE}'/' /live.vars
374 echo "export ${STRING}=\"${VALUE}\"" >> /live.vars
377 eval export "${STRING}"="${VALUE}"
380 is_in_list_separator_helper () {
386 echo ${list} | grep -qe "^\(.*${sep}\)\?${element}\(${sep}.*\)\?$"
389 is_in_space_sep_list () {
392 is_in_list_separator_helper "[[:space:]]" "${element}" "${*}"
395 is_in_comma_sep_list () {
398 is_in_list_separator_helper "," "${element}" "${*}"
404 echo "/dev/$($udevinfo -q name -p ${sysdev} 2>/dev/null|| echo ${sysdev##*/})"
412 for dev in "${sysblock}"/* "${sysblock}"
414 if [ -e "${dev}/dev" ]
425 black_listed_devices="${1}"
426 white_listed_devices="${2}"
428 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "loop|ram|fd")
430 fulldevname=$(sys2dev "${sysblock}")
432 if is_in_space_sep_list ${fulldevname} ${black_listed_devices} || \
433 [ -n "${white_listed_devices}" ] && \
434 ! is_in_space_sep_list ${fulldevname} ${white_listed_devices}
436 # skip this device entirely
440 for dev in $(subdevices "${sysblock}")
442 devname=$(sys2dev "${dev}")
444 if is_in_space_sep_list ${devname} ${black_listed_devices}
446 # skip this subdevice
459 # Validate input first
460 if [ -z "${fstype}" ]
465 # Try to look if it is already supported by the kernel
466 if grep -q ${fstype} /proc/filesystems
470 # Then try to add support for it the gentle way using the initramfs capabilities
472 if grep -q ${fstype} /proc/filesystems
475 # Then try the hard way if /root is already reachable
477 kmodule="/root/lib/modules/`uname -r`/${fstype}/${fstype}.ko"
478 if [ -e "${kmodule}" ]
481 if grep -q ${fstype} /proc/filesystems
494 /sbin/blkid -s TYPE -o value $1 2>/dev/null
501 grep -m1 "^${device} " /proc/mounts | cut -f2 -d ' '
505 # remove all unnecessary /:s in the path, including last one (except
506 # if path is just "/")
507 echo ${1} | sed 's|//\+|/|g' | sed 's|^\(.*[^/]\)/$|\1|'
510 what_is_mounted_on ()
512 local dir="$(trim_path ${1})"
513 grep -m1 "^[^ ]\+ ${dir} " /proc/mounts | cut -d' ' -f1
518 local reference="${1}"
521 local owner=$(stat -c %u:%g "${reference}")
522 chown -h ${owner} ${targets}
527 local reference="${1}"
530 local rights=$(stat -c %a "${reference}")
531 chmod ${rights} ${targets}
547 mounts="$(awk '{print $2}' /proc/mounts)"
548 testpath="$(busybox realpath ${testpath})"
552 if echo "${mounts}" | grep -qs "^${testpath}"
554 set -- $(echo "${mounts}" | grep "^${testpath}" | lastline)
558 testpath=$(dirname $testpath)
565 # Returns used/free fs kbytes + 5% more
566 # You could pass a block device as ${1} or the mount point as ${2}
572 if [ -z "${mountp}" ]
574 mountp="$(where_is_mounted ${dev})"
576 if [ -z "${mountp}" ]
578 mountp="/mnt/tmp_fs_size"
581 mount -t $(get_fstype "${dev}") -o ro "${dev}" "${mountp}" || log_warning_msg "cannot mount -t $(get_fstype ${dev}) -o ro ${dev} ${mountp}"
587 if [ "${used}" = "used" ]
589 size=$(du -ks ${mountp} | cut -f1)
590 size=$(expr ${size} + ${size} / 20 ) # FIXME: 5% more to be sure
593 size="$(df -k | grep -s ${mountp} | awk '{print $4}')"
596 if [ -n "${doumount}" ]
598 umount "${mountp}" || log_warning_msg "cannot umount ${mountp}"
608 if [ -x /bin/loadkeys -a -r /etc/boottime.kmap.gz ]
610 loadkeys /etc/boottime.kmap.gz
620 local encryption=${5}
623 # the output of setup_loop is evaluated in other functions,
624 # modprobe leaks kernel options like "libata.dma=0"
625 # as "options libata dma=0" on stdout, causing serious
626 # problems therefor, so instead always avoid output to stdout
627 modprobe -q -b "${module}" 1>/dev/null
631 for loopdev in ${pattern}
633 if [ "$(cat ${loopdev}/size)" -eq 0 ]
635 dev=$(sys2dev "${loopdev}")
638 if [ -n "${readonly}" ]
640 if losetup --help 2>&1 | grep -q -- "-r\b"
642 options="${options} -r"
646 if [ -n "${offset}" ] && [ 0 -lt "${offset}" ]
648 options="${options} -o ${offset}"
651 if [ -z "${encryption}" ]
653 losetup ${options} "${dev}" "${fspath}"
655 # Loop AES encryption
660 echo -n "Enter passphrase for root filesystem: " >&6
662 echo "${passphrase}" > /tmp/passphrase
664 exec 9</tmp/passphrase
665 /sbin/losetup ${options} -e "${encryption}" -p 9 "${dev}" "${fspath}"
668 rm -f /tmp/passphrase
670 if [ 0 -eq ${error} ]
677 echo -n "There was an error decrypting the root filesystem ... Retry? [Y/n] " >&6
680 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
693 panic "No loop devices available"
703 old_mountp="$(where_is_mounted ${dev})"
705 if [ -n "${old_mountp}" ]
707 if [ "${opts}" != "ro" ]
709 mount -o remount,"${opts}" "${dev}" "${old_mountp}" || panic "Remounting ${dev} ${opts} on ${old_mountp} failed"
712 mount -o bind "${old_mountp}" "${mountp}" || panic "Cannot bind-mount ${old_mountp} on ${mountp}"
714 if [ -z "${fstype}" ]
716 fstype=$(get_fstype "${dev}")
718 mount -t "${fstype}" -o "${opts}" "${dev}" "${mountp}" || \
719 ( echo "SKIPPING: Cannot mount ${dev} on ${mountp}, fstype=${fstype}, options=${opts}" > boot.log && return 0 )
723 mount_persistence_media ()
728 local backing="/live/persistence/$(basename ${device})"
730 mkdir -p "${backing}"
731 local old_backing="$(where_is_mounted ${device})"
732 if [ -z "${old_backing}" ]
734 local fstype="$(get_fstype ${device})"
735 local mount_opts="rw,noatime"
736 if [ -n "${PERSISTENCE_READONLY}" ]
738 mount_opts="ro,noatime"
740 if mount -t "${fstype}" -o "${mount_opts}" "${device}" "${backing}" >/dev/null
745 [ -z "${probe}" ] && log_warning_msg "Failed to mount persistence media ${device}"
749 elif [ "${backing}" != "${old_backing}" ]
751 if mount --move ${old_backing} ${backing} >/dev/null
756 [ -z "${probe}" ] && log_warning_msg "Failed to move persistence media ${device}"
764 close_persistence_media () {
766 local backing="$(where_is_mounted ${device})"
768 if [ -d "${backing}" ]
770 umount "${backing}" >/dev/null 2>&1
771 rmdir "${backing}" >/dev/null 2>&1
774 if is_active_luks_mapping ${device}
776 /sbin/cryptsetup luksClose ${device}
783 name="$(basename ${dev})"
785 if [ -n "${PERSISTENCE_READONLY}" ]
787 opts="${opts} --readonly"
790 if /sbin/cryptsetup status "${name}" >/dev/null 2>&1
792 re="^[[:space:]]*device:[[:space:]]*\([^[:space:]]*\)$"
793 opened_dev=$(cryptsetup status ${name} 2>/dev/null | grep "${re}" | sed "s|${re}|\1|")
794 if [ "${opened_dev}" = "${dev}" ]
796 luks_device="/dev/mapper/${name}"
800 log_warning_msg "Cannot open luks device ${dev} since ${opened_dev} already is opened with its name"
809 /lib/cryptsetup/askpass "Enter passphrase for ${dev}: " | \
810 /sbin/cryptsetup -T 1 luksOpen ${dev} ${name} ${opts}
814 luks_device="/dev/mapper/${name}"
820 echo -n "There was an error decrypting ${dev} ... Retry? [Y/n] " >&6
823 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
833 /sbin/blkid -s PART_ENTRY_NAME -p -o value ${dev} 2>/dev/null
839 [ "$(/sbin/blkid -s PART_ENTRY_SCHEME -p -o value ${dev} 2>/dev/null)" = "gpt" ]
842 probe_for_gpt_name ()
844 local overlays="${1}"
847 local gpt_dev="${dev}"
848 if is_active_luks_mapping ${dev}
850 # if $dev is an opened luks device, we need to check
851 # GPT stuff on the backing device
852 gpt_dev=$(get_luks_backing_device "${dev}")
855 if ! is_gpt_device ${gpt_dev}
860 local gpt_name=$(get_gpt_name ${gpt_dev})
861 for label in ${overlays}
863 if [ "${gpt_name}" = "${label}" ]
865 echo "${label}=${dev}"
870 probe_for_fs_label ()
872 local overlays="${1}"
875 for label in ${overlays}
877 if [ "$(/sbin/blkid -s LABEL -o value $dev 2>/dev/null)" = "${label}" ]
879 echo "${label}=${dev}"
884 probe_for_file_name ()
886 local overlays="${1}"
890 local backing="$(mount_persistence_media ${dev} probe)"
891 if [ -z "${backing}" ]
896 for label in ${overlays}
898 path=${backing}/${PERSISTENCE_PATH}${label}
901 local loopdev=$(setup_loop "${path}" "loop" "/sys/block/loop*")
902 ret="${ret} ${label}=${loopdev}"
910 umount ${backing} > /dev/null 2>&1 || true
914 find_persistence_media ()
916 # Scans devices for overlays, and returns a whitespace
917 # separated list of how to use them. Only overlays with a partition
918 # label or file name in ${overlays} are returned.
920 # When scanning a LUKS device, the user will be asked to enter the
921 # passphrase; on failure to enter it, or if no persistence partitions
922 # or files were found, the LUKS device is closed.
924 # For all other cases (overlay partition and overlay file) the
925 # return value is "${label}=${device}", where ${device} a device that
926 # can mount the content. In the case of an overlay file, the device
927 # containing the file will remain mounted as a side-effect.
929 # No devices in ${black_listed_devices} will be scanned, and if
930 # ${white_list_devices} is non-empty, only devices in it will be
933 local overlays="${1}"
934 local white_listed_devices="${2}"
937 local black_listed_devices="$(what_is_mounted_on /live/image)"
939 for dev in $(storage_devices "${black_listed_devices}" "${white_listed_devices}")
944 # Check if it's a luks device; we'll have to open the device
945 # in order to probe any filesystem it contains, like we do
946 # below. activate_custom_mounts() also depends on that any luks
947 # device already has been opened.
948 if is_in_comma_sep_list luks ${PERSISTENCE_ENCRYPTION} && \
949 is_luks_partition ${dev}
951 if luks_device=$(open_luks_device "${dev}")
955 # skip $dev since we failed/chose not to open it
958 elif ! is_in_comma_sep_list none ${PERSISTENCE_ENCRYPTION}
960 # skip $dev since we don't allow unencrypted storage
964 # Probe for matching GPT partition names or filesystem labels
965 if is_in_comma_sep_list filesystem ${PERSISTENCE_STORAGE}
967 result=$(probe_for_gpt_name "${overlays}" ${dev})
968 if [ -n "${result}" ]
970 ret="${ret} ${result}"
974 result=$(probe_for_fs_label "${overlays}" ${dev})
975 if [ -n "${result}" ]
977 ret="${ret} ${result}"
982 # Probe for files with matching name on mounted partition
983 if is_in_comma_sep_list file ${PERSISTENCE_STORAGE}
985 result=$(probe_for_file_name "${overlays}" ${dev})
986 if [ -n "${result}" ]
988 ret="${ret} ${result}"
993 # Close luks device if it isn't used
994 if [ -z "${result}" ] && [ -n "${luks_device}" ] && \
995 is_active_luks_mapping "${luks_device}"
997 /sbin/cryptsetup luksClose "${luks_device}"
1011 for adaptor in /sys/class/net/*
1013 status="$(cat ${adaptor}/iflink)"
1015 if [ "${status}" -eq 2 ]
1017 mac="$(cat ${adaptor}/address)"
1018 mac="$(echo ${mac} | sed 's/:/-/g' | tr '[a-z]' '[A-Z]')"
1025 is_luks_partition ()
1028 /sbin/cryptsetup isLuks "${device}" 1>/dev/null 2>&1
1031 is_active_luks_mapping ()
1034 /sbin/cryptsetup status "${device}" 1>/dev/null 2>&1
1037 get_luks_backing_device () {
1039 cryptsetup status ${device} 2> /dev/null | \
1040 awk '{if ($1 == "device:") print $2}'
1045 output_format="${1}"
1049 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1052 if [ "$(cat ${sysblock}/removable)" = "1" ]
1054 if [ -z "${want_usb}" ]
1058 if readlink ${sysblock} | grep -q usb
1065 if [ "${dev_ok}" = "true" ]
1067 case "${output_format}" in
1069 ret="${ret} ${sysblock}"
1072 devname=$(sys2dev "${sysblock}")
1073 ret="${ret} ${devname}"
1082 removable_usb_dev ()
1084 output_format="${1}"
1086 removable_dev "${output_format}" "want_usb"
1089 non_removable_dev ()
1091 output_format="${1}"
1094 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1096 if [ "$(cat ${sysblock}/removable)" = "0" ]
1098 case "${output_format}" in
1100 ret="${ret} ${sysblock}"
1103 devname=$(sys2dev "${sysblock}")
1104 ret="${ret} ${devname}"
1115 # create source's directory structure in dest, and recursively
1116 # create symlinks in dest to to all files in source. if mask
1117 # is non-empty, remove mask from all source paths when
1118 # creating links (will be necessary if we change root, which
1119 # live-boot normally does (into $rootmnt)).
1121 # remove multiple /:s and ensure ending on /
1122 local src_dir="$(trim_path ${1})/"
1123 local dest_dir="$(trim_path ${2})/"
1124 local src_mask="${3}"
1126 # This check can only trigger on the inital, non-recursive call since
1127 # we create the destination before recursive calls
1128 if [ ! -d "${dest_dir}" ]
1130 log_warning_msg "Must link_files into a directory"
1134 find "${src_dir}" -mindepth 1 -maxdepth 1 | while read src; do
1135 local dest="${dest_dir}$(basename "${src}")"
1138 if [ -z "$(ls -A "${src}")" ]
1142 if [ ! -d "${dest}" ]
1145 chown_ref "${src}" "${dest}"
1146 chmod_ref "${src}" "${dest}"
1148 link_files "${src}" "${dest}" "${src_mask}"
1150 local final_src=${src}
1151 if [ -n "${src_mask}" ]
1153 final_src="$(echo ${final_src} | sed "s|^${src_mask}||")"
1155 rm -rf "${dest}" 2> /dev/null
1156 ln -s "${final_src}" "${dest}"
1157 chown_ref "${src}" "${dest}"
1164 local unionmountpoint="${1}" # directory where the union is mounted
1165 local unionrw="${2}" # branch where the union changes are stored
1166 local unionro1="${3}" # first underlying read-only branch (optional)
1167 local unionro2="${4}" # second underlying read-only branch (optional)
1169 if [ "${UNIONTYPE}" = "aufs" ]
1174 elif [ "${UNIONTYPE}" = "unionfs-fuse" ]
1183 case "${UNIONTYPE}" in
1185 unionmountopts="-o cow -o noinitgroups -o default_permissions -o allow_other -o use_ino -o suid"
1186 unionmountopts="${unionmountopts} ${unionrw}=${rw_opt}"
1187 if [ -n "${unionro1}" ]
1189 unionmountopts="${unionmountopts}:${unionro1}=${ro_opt}"
1191 if [ -n "${unionro2}" ]
1193 unionmountopts="${unionmountopts}:${unionro2}=${ro_opt}"
1195 ( sysctl -w fs.file-max=391524 ; ulimit -HSn 16384
1196 unionfs-fuse ${unionmountopts} "${unionmountpoint}" ) && \
1197 ( mkdir -p /run/sendsigs.omit.d
1198 pidof unionfs-fuse >> /run/sendsigs.omit.d/unionfs-fuse || true )
1202 # XXX: can unionro2 be used? (overlayfs only handles two dirs, but perhaps they can be chained?)
1203 # XXX: and can unionro1 be optional? i.e. can overlayfs skip lowerdir?
1204 unionmountopts="-o noatime,lowerdir=${unionro1},upperdir=${unionrw}"
1205 mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
1209 unionmountopts="-o noatime,${noxino_opt},dirs=${unionrw}=${rw_opt}"
1210 if [ -n "${unionro1}" ]
1212 unionmountopts="${unionmountopts}:${unionro1}=${ro_opt}"
1214 if [ -n "${unionro2}" ]
1216 unionmountopts="${unionmountopts}:${unionro2}=${ro_opt}"
1218 mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
1223 get_custom_mounts ()
1225 # Side-effect: leaves $devices with live-persistence.conf mounted in /live/persistence
1226 # Side-effect: prints info to file $custom_mounts
1228 local custom_mounts=${1}
1232 local bindings="/tmp/bindings.list"
1233 local links="/tmp/links.list"
1234 rm -rf ${bindings} ${links} 2> /dev/null
1236 for device in ${devices}
1238 if [ ! -b "${device}" ]
1243 local device_name="$(basename ${device})"
1244 local backing=$(mount_persistence_media ${device})
1245 if [ -z "${backing}" ]
1250 local include_list="${backing}/${persistence_list}"
1251 if [ ! -r "${include_list}" ]
1256 if [ -n "${DEBUG}" ] && [ -e "${include_list}" ]
1258 cp ${include_list} /live/persistence/${persistence_list}.${device_name}
1261 while read dir options # < ${include_list}
1263 if echo ${dir} | grep -qe "^[[:space:]]*\(#.*\)\?$"
1265 # skipping empty or commented lines
1269 if trim_path ${dir} | grep -q -e "^[^/]" -e "^/live\(/.*\)\?$" -e "^/\(.*/\)\?\.\.\?\(/.*\)\?$"
1271 log_warning_msg "Skipping unsafe custom mount ${dir}: must be an absolute path containing neither the \".\" nor \"..\" special dirs, and cannot be \"/live\" or any sub-directory therein."
1277 for opt in $(echo ${options} | tr ',' ' ');
1281 opt_source=${opt#source=}
1289 log_warning_msg "Skipping custom mount with unkown option: ${opt}"
1295 local source="${dir}"
1296 if [ -n "${opt_source}" ]
1298 if echo ${opt_source} | grep -q -e "^/" -e "^\(.*/\)\?\.\.\?\(/.*\)\?$" && [ "${source}" != "." ]
1300 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"
1303 source="${opt_source}"
1307 local full_source="$(trim_path ${backing}/${source})"
1308 local full_dest="$(trim_path ${rootmnt}/${dir})"
1309 if [ -n "${opt_link}" ]
1311 echo "${device} ${full_source} ${full_dest} ${options}" >> ${links}
1313 echo "${device} ${full_source} ${full_dest} ${options}" >> ${bindings}
1315 done < ${include_list}
1318 # We sort the list according to destination so we're sure that
1319 # we won't hide a previous mount. We also ignore duplicate
1320 # destinations in a more or less arbitrary way.
1321 [ -e "${bindings}" ] && sort -k3 -sbu ${bindings} >> ${custom_mounts} && rm ${bindings}
1323 # After all mounts are considered we add symlinks so they
1324 # won't be hidden by some mount.
1325 [ -e "${links}" ] && cat ${links} >> ${custom_mounts} && rm ${links}
1327 # We need to make sure that no two custom mounts have the same sources
1328 # or are nested; if that is the case, too much weird stuff can happen.
1329 local prev_source="impossible source" # first iteration must not match
1331 # This sort will ensure that a source /a comes right before a source
1332 # /a/b so we only need to look at the previous source
1333 sort -k2 -b ${custom_mounts} |
1334 while read device source dest options
1336 if echo ${source} | grep -qe "^${prev_source}\(/.*\)\?$"
1338 panic "Two persistence mounts have the same or nested sources: ${source} on ${dest}, and ${prev_source} on ${prev_dest}"
1340 prev_source=${source}
1345 activate_custom_mounts ()
1347 local custom_mounts="${1}" # the ouput from get_custom_mounts()
1348 local used_devices=""
1350 while read device source dest options # < ${custom_mounts}
1352 local opt_bind="true"
1355 for opt in $(echo ${options} | tr ',' ' ');
1360 unset opt_link opt_union
1364 unset opt_bind opt_union
1368 unset opt_bind opt_link
1373 if [ -n "$(what_is_mounted_on "${dest}")" ]
1375 if [ "${dest}" = "${rootmnt}" ]
1379 log_warning_msg "Skipping custom mount ${dest}: $(what_is_mounted_on "${dest}") is already mounted there"
1384 if [ ! -d "${dest}" ]
1386 # create the destination and delete existing files in
1387 # its path that are in the way
1389 for dir in $(echo ${dest} | sed -e 's|/\+| |g')
1391 path=$(trim_path ${path}/${dir})
1399 if echo ${path} | grep -qe "^${rootmnt}/*home/[^/]\+"
1401 # if ${dest} is in /home try fixing proper ownership by assuming that the intended user is the first, which is usually the case
1402 # FIXME: this should really be handled by live-config since we don't know for sure which uid a certain user has until then
1403 chown 1000:1000 ${path}
1409 # if ${source} doesn't exist on our persistence media
1410 # we bootstrap it with $dest from the live filesystem.
1411 # this both makes sense and is critical if we're
1412 # dealing with /etc or other system dir.
1413 if [ ! -d "${source}" ]
1415 if [ -n "${PERSISTENCE_READONLY}" ]
1418 elif [ -n "${opt_union}" ] || [ -n "${opt_link}" ]
1420 # unions and don't need to be bootstrapped
1421 # link dirs can't be bootstrapped in a sensible way
1422 mkdir -p "${source}"
1423 chown_ref "${dest}" "${source}"
1424 chmod_ref "${dest}" "${source}"
1425 elif [ -n "${opt_bind}" ]
1427 # ensure that $dest is not copied *into* $source
1428 mkdir -p "$(dirname ${source})"
1429 cp -a "${dest}" "${source}"
1433 # XXX: If CONFIG_AUFS_ROBR is added to the Debian kernel we can
1434 # ignore the loop below and set rofs_dest_backing=$dest
1435 local rofs_dest_backing=""
1436 if [ -n "${opt_link}"]
1438 for d in /live/rofs/*
1440 if [ -n "${rootmnt}" ]
1442 rofs_dest_backing="${d}/$(echo ${dest} | sed -e "s|${rootmnt}||")"
1444 rofs_dest_backing="${d}/${dest}"
1446 if [ -d "${rofs_dest_backing}" ]
1450 rofs_dest_backing=""
1455 if [ -n "${opt_link}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1457 link_files ${source} ${dest} ${rootmnt}
1458 elif [ -n "${opt_link}" ] && [ -n "${PERSISTENCE_READONLY}" ]
1460 mkdir -p /live/persistence
1461 local links_source=$(mktemp -d /live/persistence/links-source-XXXXXX)
1462 chown_ref ${source} ${links_source}
1463 chmod_ref ${source} ${links_source}
1464 # We put the cow dir in the below strange place to
1465 # make it absolutely certain that the link source
1466 # has its own directory and isn't nested with some
1467 # other custom mount (if so that mount's files would
1468 # be linked, causing breakage.
1469 local cow_dir="/live/overlay/live/persistence/$(basename ${links_source})"
1471 chown_ref "${source}" "${cow_dir}"
1472 chmod_ref "${source}" "${cow_dir}"
1473 do_union ${links_source} ${cow_dir} ${source} ${rofs_dest_backing}
1474 link_files ${links_source} ${dest} ${rootmnt}
1475 elif [ -n "${opt_union}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1477 do_union ${dest} ${source} ${rofs_dest_backing}
1478 elif [ -n "${opt_bind}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1480 mount --bind "${source}" "${dest}"
1481 elif [ -n "${opt_bind}" -o -n "${opt_union}" ] && [ -n "${PERSISTENCE_READONLY}" ]
1483 # bind-mount and union mount are handled the same
1484 # in read-only mode, but note that rofs_dest_backing
1485 # is non-empty (and necessary) only for unions
1486 if [ -n "${rootmnt}" ]
1488 local cow_dir="$(echo ${dest} | sed -e "s|^${rootmnt}|/live/overlay/|")"
1490 # This is happens if persistence is activated
1492 local cow_dir="/live/overlay/${dest}"
1494 if [ -e "${cow_dir}" ] && [ -z "${opt_link}" ]
1496 # If an earlier custom mount has files here
1497 # it will "block" the current mount's files
1498 # which is undesirable
1502 chown_ref "${source}" "${cow_dir}"
1503 chmod_ref "${source}" "${cow_dir}"
1504 do_union ${dest} ${cow_dir} ${source} ${rofs_dest_backing}
1507 PERSISTENCE_IS_ON="1"
1508 export PERSISTENCE_IS_ON
1510 if echo ${used_devices} | grep -qve "^\(.* \)\?${device}\( .*\)\?$"
1512 used_devices="${used_devices} ${device}"
1514 done < ${custom_mounts}
1516 echo ${used_devices}
1519 fix_backwards_compatibility ()
1525 if [ -n "${PERSISTENCE_READONLY}" ]
1530 local backing="$(mount_persistence_media ${device})"
1531 if [ -z "${backing}" ]
1536 local include_list="${backing}/${persistence_list}"
1537 if [ ! -r "${include_list}" ]
1539 echo "# persistence backwards compatibility:
1540 ${dir} ${opt},source=." > "${include_list}"
1548 [ $(stat -fc%d:%D "${directory}") != $(stat -fc%d:%D "${directory}/..") ]