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}"
845 local snapshots="${2}"
848 local gpt_dev="${dev}"
849 if is_active_luks_mapping ${dev}
851 # if $dev is an opened luks device, we need to check
852 # GPT stuff on the backing device
853 gpt_dev=$(get_luks_backing_device "${dev}")
856 if ! is_gpt_device ${gpt_dev}
861 local gpt_name=$(get_gpt_name ${gpt_dev})
862 for label in ${overlays} ${snapshots}
864 if [ "${gpt_name}" = "${label}" ]
866 echo "${label}=${dev}"
871 probe_for_fs_label ()
873 local overlays="${1}"
874 local snapshots="${2}"
877 for label in ${overlays} ${snapshots}
879 if [ "$(/sbin/blkid -s LABEL -o value $dev 2>/dev/null)" = "${label}" ]
881 echo "${label}=${dev}"
886 probe_for_file_name ()
888 local overlays="${1}"
889 local snapshots="${2}"
893 local backing="$(mount_persistence_media ${dev} probe)"
894 if [ -z "${backing}" ]
899 for label in ${overlays}
901 path=${backing}/${PERSISTENCE_PATH}${label}
904 local loopdev=$(setup_loop "${path}" "loop" "/sys/block/loop*")
905 ret="${ret} ${label}=${loopdev}"
908 for label in ${snapshots}
910 for ext in squashfs cpio.gz ext2 ext3 ext4 jffs2
912 path="${PERSISTENCE_PATH}${label}.${ext}"
913 if [ -f "${backing}/${path}" ]
915 ret="${ret} ${label}=${dev}:${backing}:${path}"
924 umount ${backing} > /dev/null 2>&1 || true
928 find_persistence_media ()
930 # Scans devices for overlays and snapshots, and returns a whitespace
931 # separated list of how to use them. Only overlays with a partition
932 # label or file name in ${overlays} are returned, and ditto for
933 # snapshots with labels in ${snapshots}.
935 # When scanning a LUKS device, the user will be asked to enter the
936 # passphrase; on failure to enter it, or if no persistence partitions
937 # or files were found, the LUKS device is closed.
939 # For a snapshot file the return value is ${label}=${snapdata}", where
940 # ${snapdata} is the parameter used for try_snap().
942 # For all other cases (overlay/snapshot partition and overlay file) the
943 # return value is "${label}=${device}", where ${device} a device that
944 # can mount the content. In the case of an overlay file, the device
945 # containing the file will remain mounted as a side-effect.
947 # No devices in ${black_listed_devices} will be scanned, and if
948 # ${white_list_devices} is non-empty, only devices in it will be
951 local overlays="${1}"
952 local snapshots="${2}"
953 local white_listed_devices="${3}"
956 local black_listed_devices="$(what_is_mounted_on /live/image)"
958 for dev in $(storage_devices "${black_listed_devices}" "${white_listed_devices}")
963 # Check if it's a luks device; we'll have to open the device
964 # in order to probe any filesystem it contains, like we do
965 # below. activate_custom_mounts() also depends on that any luks
966 # device already has been opened.
967 if is_in_comma_sep_list luks ${PERSISTENCE_ENCRYPTION} && \
968 is_luks_partition ${dev}
970 if luks_device=$(open_luks_device "${dev}")
974 # skip $dev since we failed/chose not to open it
977 elif ! is_in_comma_sep_list none ${PERSISTENCE_ENCRYPTION}
979 # skip $dev since we don't allow unencrypted storage
983 # Probe for matching GPT partition names or filesystem labels
984 if is_in_comma_sep_list filesystem ${PERSISTENCE_STORAGE}
986 result=$(probe_for_gpt_name "${overlays}" "${snapshots}" ${dev})
987 if [ -n "${result}" ]
989 ret="${ret} ${result}"
993 result=$(probe_for_fs_label "${overlays}" "${snapshots}" ${dev})
994 if [ -n "${result}" ]
996 ret="${ret} ${result}"
1001 # Probe for files with matching name on mounted partition
1002 if is_in_comma_sep_list file ${PERSISTENCE_STORAGE}
1004 result=$(probe_for_file_name "${overlays}" "${snapshots}" ${dev})
1005 if [ -n "${result}" ]
1007 ret="${ret} ${result}"
1012 # Close luks device if it isn't used
1013 if [ -z "${result}" ] && [ -n "${luks_device}" ] && \
1014 is_active_luks_mapping "${luks_device}"
1016 /sbin/cryptsetup luksClose "${luks_device}"
1030 for adaptor in /sys/class/net/*
1032 status="$(cat ${adaptor}/iflink)"
1034 if [ "${status}" -eq 2 ]
1036 mac="$(cat ${adaptor}/address)"
1037 mac="$(echo ${mac} | sed 's/:/-/g' | tr '[a-z]' '[A-Z]')"
1044 is_luks_partition ()
1047 /sbin/cryptsetup isLuks "${device}" 1>/dev/null 2>&1
1050 is_active_luks_mapping ()
1053 /sbin/cryptsetup status "${device}" 1>/dev/null 2>&1
1056 get_luks_backing_device () {
1058 cryptsetup status ${device} 2> /dev/null | \
1059 awk '{if ($1 == "device:") print $2}'
1064 output_format="${1}"
1068 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1071 if [ "$(cat ${sysblock}/removable)" = "1" ]
1073 if [ -z "${want_usb}" ]
1077 if readlink ${sysblock} | grep -q usb
1084 if [ "${dev_ok}" = "true" ]
1086 case "${output_format}" in
1088 ret="${ret} ${sysblock}"
1091 devname=$(sys2dev "${sysblock}")
1092 ret="${ret} ${devname}"
1101 removable_usb_dev ()
1103 output_format="${1}"
1105 removable_dev "${output_format}" "want_usb"
1108 non_removable_dev ()
1110 output_format="${1}"
1113 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1115 if [ "$(cat ${sysblock}/removable)" = "0" ]
1117 case "${output_format}" in
1119 ret="${ret} ${sysblock}"
1122 devname=$(sys2dev "${sysblock}")
1123 ret="${ret} ${devname}"
1134 # create source's directory structure in dest, and recursively
1135 # create symlinks in dest to to all files in source. if mask
1136 # is non-empty, remove mask from all source paths when
1137 # creating links (will be necessary if we change root, which
1138 # live-boot normally does (into $rootmnt)).
1140 # remove multiple /:s and ensure ending on /
1141 local src_dir="$(trim_path ${1})/"
1142 local dest_dir="$(trim_path ${2})/"
1143 local src_mask="${3}"
1145 # This check can only trigger on the inital, non-recursive call since
1146 # we create the destination before recursive calls
1147 if [ ! -d "${dest_dir}" ]
1149 log_warning_msg "Must link_files into a directory"
1153 find "${src_dir}" -mindepth 1 -maxdepth 1 | while read src; do
1154 local dest="${dest_dir}$(basename "${src}")"
1157 if [ -z "$(ls -A "${src}")" ]
1161 if [ ! -d "${dest}" ]
1164 chown_ref "${src}" "${dest}"
1165 chmod_ref "${src}" "${dest}"
1167 link_files "${src}" "${dest}" "${src_mask}"
1169 local final_src=${src}
1170 if [ -n "${src_mask}" ]
1172 final_src="$(echo ${final_src} | sed "s|^${src_mask}||")"
1174 rm -rf "${dest}" 2> /dev/null
1175 ln -s "${final_src}" "${dest}"
1176 chown_ref "${src}" "${dest}"
1183 local unionmountpoint="${1}" # directory where the union is mounted
1184 local unionrw="${2}" # branch where the union changes are stored
1185 local unionro1="${3}" # first underlying read-only branch (optional)
1186 local unionro2="${4}" # second underlying read-only branch (optional)
1188 if [ "${UNIONTYPE}" = "aufs" ]
1193 elif [ "${UNIONTYPE}" = "unionfs-fuse" ]
1202 case "${UNIONTYPE}" in
1204 unionmountopts="-o cow -o noinitgroups -o default_permissions -o allow_other -o use_ino -o suid"
1205 unionmountopts="${unionmountopts} ${unionrw}=${rw_opt}"
1206 if [ -n "${unionro1}" ]
1208 unionmountopts="${unionmountopts}:${unionro1}=${ro_opt}"
1210 if [ -n "${unionro2}" ]
1212 unionmountopts="${unionmountopts}:${unionro2}=${ro_opt}"
1214 ( sysctl -w fs.file-max=391524 ; ulimit -HSn 16384
1215 unionfs-fuse ${unionmountopts} "${unionmountpoint}" ) && \
1216 ( mkdir -p /run/sendsigs.omit.d
1217 pidof unionfs-fuse >> /run/sendsigs.omit.d/unionfs-fuse || true )
1221 # XXX: can unionro2 be used? (overlayfs only handles two dirs, but perhaps they can be chained?)
1222 # XXX: and can unionro1 be optional? i.e. can overlayfs skip lowerdir?
1223 unionmountopts="-o noatime,lowerdir=${unionro1},upperdir=${unionrw}"
1224 mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
1228 unionmountopts="-o noatime,${noxino_opt},dirs=${unionrw}=${rw_opt}"
1229 if [ -n "${unionro1}" ]
1231 unionmountopts="${unionmountopts}:${unionro1}=${ro_opt}"
1233 if [ -n "${unionro2}" ]
1235 unionmountopts="${unionmountopts}:${unionro2}=${ro_opt}"
1237 mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
1242 get_custom_mounts ()
1244 # Side-effect: leaves $devices with live-persistence.conf mounted in /live/persistence
1245 # Side-effect: prints info to file $custom_mounts
1247 local custom_mounts=${1}
1251 local bindings="/tmp/bindings.list"
1252 local links="/tmp/links.list"
1253 rm -rf ${bindings} ${links} 2> /dev/null
1255 for device in ${devices}
1257 if [ ! -b "${device}" ]
1262 local device_name="$(basename ${device})"
1263 local backing=$(mount_persistence_media ${device})
1264 if [ -z "${backing}" ]
1269 local include_list="${backing}/${persistence_list}"
1270 if [ ! -r "${include_list}" ]
1275 if [ -n "${DEBUG}" ] && [ -e "${include_list}" ]
1277 cp ${include_list} /live/persistence/${persistence_list}.${device_name}
1280 while read dir options # < ${include_list}
1282 if echo ${dir} | grep -qe "^[[:space:]]*\(#.*\)\?$"
1284 # skipping empty or commented lines
1288 if trim_path ${dir} | grep -q -e "^[^/]" -e "^/live\(/.*\)\?$" -e "^/\(.*/\)\?\.\.\?\(/.*\)\?$"
1290 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."
1296 for opt in $(echo ${options} | tr ',' ' ');
1300 opt_source=${opt#source=}
1308 log_warning_msg "Skipping custom mount with unkown option: ${opt}"
1314 local source="${dir}"
1315 if [ -n "${opt_source}" ]
1317 if echo ${opt_source} | grep -q -e "^/" -e "^\(.*/\)\?\.\.\?\(/.*\)\?$" && [ "${source}" != "." ]
1319 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"
1322 source="${opt_source}"
1326 local full_source="$(trim_path ${backing}/${source})"
1327 local full_dest="$(trim_path ${rootmnt}/${dir})"
1328 if [ -n "${opt_link}" ]
1330 echo "${device} ${full_source} ${full_dest} ${options}" >> ${links}
1332 echo "${device} ${full_source} ${full_dest} ${options}" >> ${bindings}
1334 done < ${include_list}
1337 # We sort the list according to destination so we're sure that
1338 # we won't hide a previous mount. We also ignore duplicate
1339 # destinations in a more or less arbitrary way.
1340 [ -e "${bindings}" ] && sort -k3 -sbu ${bindings} >> ${custom_mounts} && rm ${bindings}
1342 # After all mounts are considered we add symlinks so they
1343 # won't be hidden by some mount.
1344 [ -e "${links}" ] && cat ${links} >> ${custom_mounts} && rm ${links}
1346 # We need to make sure that no two custom mounts have the same sources
1347 # or are nested; if that is the case, too much weird stuff can happen.
1348 local prev_source="impossible source" # first iteration must not match
1350 # This sort will ensure that a source /a comes right before a source
1351 # /a/b so we only need to look at the previous source
1352 sort -k2 -b ${custom_mounts} |
1353 while read device source dest options
1355 if echo ${source} | grep -qe "^${prev_source}\(/.*\)\?$"
1357 panic "Two persistence mounts have the same or nested sources: ${source} on ${dest}, and ${prev_source} on ${prev_dest}"
1359 prev_source=${source}
1364 activate_custom_mounts ()
1366 local custom_mounts="${1}" # the ouput from get_custom_mounts()
1367 local used_devices=""
1369 while read device source dest options # < ${custom_mounts}
1371 local opt_bind="true"
1374 for opt in $(echo ${options} | tr ',' ' ');
1379 unset opt_link opt_union
1383 unset opt_bind opt_union
1387 unset opt_bind opt_link
1392 if [ -n "$(what_is_mounted_on "${dest}")" ]
1394 if [ "${dest}" = "${rootmnt}" ]
1398 log_warning_msg "Skipping custom mount ${dest}: $(what_is_mounted_on "${dest}") is already mounted there"
1403 if [ ! -d "${dest}" ]
1405 # create the destination and delete existing files in
1406 # its path that are in the way
1408 for dir in $(echo ${dest} | sed -e 's|/\+| |g')
1410 path=$(trim_path ${path}/${dir})
1418 if echo ${path} | grep -qe "^${rootmnt}/*home/[^/]\+"
1420 # if ${dest} is in /home try fixing proper ownership by assuming that the intended user is the first, which is usually the case
1421 # FIXME: this should really be handled by live-config since we don't know for sure which uid a certain user has until then
1422 chown 1000:1000 ${path}
1428 # if ${source} doesn't exist on our persistence media
1429 # we bootstrap it with $dest from the live filesystem.
1430 # this both makes sense and is critical if we're
1431 # dealing with /etc or other system dir.
1432 if [ ! -d "${source}" ]
1434 if [ -n "${PERSISTENCE_READONLY}" ]
1437 elif [ -n "${opt_union}" ] || [ -n "${opt_link}" ]
1439 # unions and don't need to be bootstrapped
1440 # link dirs can't be bootstrapped in a sensible way
1441 mkdir -p "${source}"
1442 chown_ref "${dest}" "${source}"
1443 chmod_ref "${dest}" "${source}"
1444 elif [ -n "${opt_bind}" ]
1446 # ensure that $dest is not copied *into* $source
1447 mkdir -p "$(dirname ${source})"
1448 cp -a "${dest}" "${source}"
1452 # XXX: If CONFIG_AUFS_ROBR is added to the Debian kernel we can
1453 # ignore the loop below and set rofs_dest_backing=$dest
1454 local rofs_dest_backing=""
1455 if [ -n "${opt_link}"]
1457 for d in /live/rofs/*
1459 if [ -n "${rootmnt}" ]
1461 rofs_dest_backing="${d}/$(echo ${dest} | sed -e "s|${rootmnt}||")"
1463 rofs_dest_backing="${d}/${dest}"
1465 if [ -d "${rofs_dest_backing}" ]
1469 rofs_dest_backing=""
1474 if [ -n "${opt_link}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1476 link_files ${source} ${dest} ${rootmnt}
1477 elif [ -n "${opt_link}" ] && [ -n "${PERSISTENCE_READONLY}" ]
1479 mkdir -p /live/persistence
1480 local links_source=$(mktemp -d /live/persistence/links-source-XXXXXX)
1481 chown_ref ${source} ${links_source}
1482 chmod_ref ${source} ${links_source}
1483 # We put the cow dir in the below strange place to
1484 # make it absolutely certain that the link source
1485 # has its own directory and isn't nested with some
1486 # other custom mount (if so that mount's files would
1487 # be linked, causing breakage.
1488 local cow_dir="/live/overlay/live/persistence/$(basename ${links_source})"
1490 chown_ref "${source}" "${cow_dir}"
1491 chmod_ref "${source}" "${cow_dir}"
1492 do_union ${links_source} ${cow_dir} ${source} ${rofs_dest_backing}
1493 link_files ${links_source} ${dest} ${rootmnt}
1494 elif [ -n "${opt_union}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1496 do_union ${dest} ${source} ${rofs_dest_backing}
1497 elif [ -n "${opt_bind}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1499 mount --bind "${source}" "${dest}"
1500 elif [ -n "${opt_bind}" -o -n "${opt_union}" ] && [ -n "${PERSISTENCE_READONLY}" ]
1502 # bind-mount and union mount are handled the same
1503 # in read-only mode, but note that rofs_dest_backing
1504 # is non-empty (and necessary) only for unions
1505 if [ -n "${rootmnt}" ]
1507 local cow_dir="$(echo ${dest} | sed -e "s|^${rootmnt}|/live/overlay/|")"
1509 # This is happens if persistence is activated
1511 local cow_dir="/live/overlay/${dest}"
1513 if [ -e "${cow_dir}" ] && [ -z "${opt_link}" ]
1515 # If an earlier custom mount has files here
1516 # it will "block" the current mount's files
1517 # which is undesirable
1521 chown_ref "${source}" "${cow_dir}"
1522 chmod_ref "${source}" "${cow_dir}"
1523 do_union ${dest} ${cow_dir} ${source} ${rofs_dest_backing}
1526 PERSISTENCE_IS_ON="1"
1527 export PERSISTENCE_IS_ON
1529 if echo ${used_devices} | grep -qve "^\(.* \)\?${device}\( .*\)\?$"
1531 used_devices="${used_devices} ${device}"
1533 done < ${custom_mounts}
1535 echo ${used_devices}
1538 fix_backwards_compatibility ()
1544 if [ -n "${PERSISTENCE_READONLY}" ]
1549 local backing="$(mount_persistence_media ${device})"
1550 if [ -z "${backing}" ]
1555 local include_list="${backing}/${persistence_list}"
1556 if [ ! -r "${include_list}" ]
1558 echo "# persistence backwards compatibility:
1559 ${dir} ${opt},source=." > "${include_list}"
1567 [ $(stat -fc%d:%D "${directory}") != $(stat -fc%d:%D "${directory}/..") ]