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 ()
84 if match_files_in_dir "${directory}/${LIVE_MEDIA_PATH}/*.squashfs" ||
85 match_files_in_dir "${directory}/${LIVE_MEDIA_PATH}/*.ext2" ||
86 match_files_in_dir "${directory}/${LIVE_MEDIA_PATH}/*.ext3" ||
87 match_files_in_dir "${directory}/${LIVE_MEDIA_PATH}/*.ext4" ||
88 match_files_in_dir "${directory}/${LIVE_MEDIA_PATH}/*.jffs2" ||
89 match_files_in_dir "${directory}/${LIVE_MEDIA_PATH}/*.dir"
91 [ -n "${mac}" ] && adddirectory="${directory}/${LIVE_MEDIA_PATH}/${mac}"
92 setup_unionfs "${directory}/${LIVE_MEDIA_PATH}" "${rootmnt}" "${adddirectory}"
94 panic "No supported filesystem images found at /${LIVE_MEDIA_PATH}."
100 sysfs_path="${1#/sys}"
102 if [ -e /lib/udev/path_id ]
105 PATH_ID="/lib/udev/path_id"
107 # wheezy/sid (udev >= 174)
108 PATH_ID="/sbin/udevadm test-builtin path_id"
111 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)"
114 elif echo "${sysfs_path}" | grep -q '^/block/vd[a-z]$'
117 elif echo ${sysfs_path} | grep -q "^/block/dm-"
120 elif echo ${sysfs_path} | grep -q "^/block/mtdblock"
132 skip_uuid_check="${3}"
134 # support for fromiso=.../isofrom=....
137 ISO_DEVICE=$(dirname $FROMISO)
138 if ! [ -b $ISO_DEVICE ]
140 # to support unusual device names like /dev/cciss/c0d0p1
141 # as well we have to identify the block device name, let's
142 # do that for up to 15 levels
144 while [ -n "$ISO_DEVICE" ] && [ "$i" -gt 0 ]
146 ISO_DEVICE=$(dirname ${ISO_DEVICE})
147 [ -b "$ISO_DEVICE" ] && break
152 if [ "$ISO_DEVICE" = "/" ]
154 echo "Warning: device for bootoption fromiso= ($FROMISO) not found.">>/boot.log
156 fs_type=$(get_fstype "${ISO_DEVICE}")
157 if is_supported_fs ${fs_type}
160 mount -t $fs_type "$ISO_DEVICE" /live/fromiso
161 ISO_NAME="$(echo $FROMISO | sed "s|$ISO_DEVICE||")"
162 loopdevname=$(setup_loop "/live/fromiso/${ISO_NAME}" "loop" "/sys/block/loop*" "" '')
163 devname="${loopdevname}"
165 echo "Warning: unable to mount $ISO_DEVICE." >>/boot.log
170 if [ -z "${devname}" ]
172 devname=$(sys2dev "${sysdev}")
175 if [ -d "${devname}" ]
177 mount -o bind "${devname}" $mountpoint || continue
179 if is_live_path $mountpoint
189 for device in ${devname}
194 if [ -x /scripts/local-top/lvm2 ]
196 ROOT="$device" resume="" /scripts/local-top/lvm2
201 # Adding raid support
202 if [ -x /scripts/local-top/mdadm ]
204 cp /conf/conf.d/md /conf/conf.d/md.orig
205 echo "MD_DEVS=$device " >> /conf/conf.d/md
206 /scripts/local-top/mdadm
207 mv /conf/conf.d/md.orig /conf/conf.d/md
214 [ -n "$device" ] && devname="$device"
216 [ -e "$devname" ] || continue
218 if [ -n "${LIVE_MEDIA_OFFSET}" ]
220 loopdevname=$(setup_loop "${devname}" "loop" "/sys/block/loop*" "${LIVE_MEDIA_OFFSET}" '')
221 devname="${loopdevname}"
224 fstype=$(get_fstype "${devname}")
226 if is_supported_fs ${fstype}
228 devuid=$(blkid -o value -s UUID "$devname")
229 [ -n "$devuid" ] && grep -qs "\<$devuid\>" $tried && continue
230 mount -t ${fstype} -o ro,noatime "${devname}" ${mountpoint} || continue
231 [ -n "$devuid" ] && echo "$devuid" >> $tried
233 if [ -n "${FINDISO}" ]
235 if [ -f ${mountpoint}/${FINDISO} ]
238 mkdir -p /live/findiso
239 mount -t ${fstype} -o ro,noatime "${devname}" /live/findiso
240 loopdevname=$(setup_loop "/live/findiso/${FINDISO}" "loop" "/sys/block/loop*" 0 "")
241 devname="${loopdevname}"
242 mount -t iso9660 -o ro,noatime "${devname}" ${mountpoint}
248 if is_live_path ${mountpoint} && \
249 ([ "${skip_uuid_check}" ] || matches_uuid ${mountpoint})
254 umount ${mountpoint} 2>/dev/null
258 if [ -n "${LIVE_MEDIA_OFFSET}" ]
260 losetup -d "${loopdevname}"
270 # don't start autodetection before timeout has expired
271 if [ -n "${LIVE_MEDIA_TIMEOUT}" ]
273 if [ "${timeout}" -lt "${LIVE_MEDIA_TIMEOUT}" ]
279 # first look at the one specified in the command line
280 case "${LIVE_MEDIA}" in
282 for sysblock in $(removable_usb_dev "sys")
284 for dev in $(subdevices "${sysblock}")
286 if check_dev "${dev}"
296 for sysblock in $(removable_dev "sys")
298 for dev in $(subdevices "${sysblock}")
300 if check_dev "${dev}"
310 if [ ! -z "${LIVE_MEDIA}" ]
312 if check_dev "null" "${LIVE_MEDIA}" "skip_uuid_check"
320 # or do the scan of block devices
321 # prefer removable devices over non-removable devices, so scan them first
322 devices_to_scan="$(removable_dev 'sys') $(non_removable_dev 'sys')"
324 for sysblock in $devices_to_scan
326 devname=$(sys2dev "${sysblock}")
327 [ -e "$devname" ] || continue
328 fstype=$(get_fstype "${devname}")
330 if /lib/udev/cdrom_id ${devname} > /dev/null
332 if check_dev "null" "${devname}"
336 elif is_nice_device "${sysblock}"
338 for dev in $(subdevices "${sysblock}")
340 if check_dev "${dev}"
345 elif [ "${fstype}" = "squashfs" -o \
346 "${fstype}" = "btrfs" -o \
347 "${fstype}" = "ext2" -o \
348 "${fstype}" = "ext3" -o \
349 "${fstype}" = "ext4" -o \
350 "${fstype}" = "jffs2" ]
352 # This is an ugly hack situation, the block device has
353 # an image directly on it. It's hopefully
354 # live-boot, so take it and run with it.
355 ln -s "${devname}" "${devname}.${fstype}"
356 echo "${devname}.${fstype}"
367 VALUE="$(eval echo -n \${$STRING})"
369 if [ -f /live.vars ] && grep -sq "export ${STRING}" /live.vars
371 sed -i -e 's/\('${STRING}'=\).*$/\1'${VALUE}'/' /live.vars
373 echo "export ${STRING}=\"${VALUE}\"" >> /live.vars
376 eval export "${STRING}"="${VALUE}"
379 is_in_list_separator_helper ()
386 echo ${list} | grep -qe "^\(.*${sep}\)\?${element}\(${sep}.*\)\?$"
389 is_in_space_sep_list ()
393 is_in_list_separator_helper "[[:space:]]" "${element}" "${*}"
396 is_in_comma_sep_list ()
400 is_in_list_separator_helper "," "${element}" "${*}"
406 echo "/dev/$($udevinfo -q name -p ${sysdev} 2>/dev/null|| echo ${sysdev##*/})"
414 for dev in "${sysblock}"/* "${sysblock}"
416 if [ -e "${dev}/dev" ]
427 black_listed_devices="${1}"
428 white_listed_devices="${2}"
430 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "loop|ram|fd")
432 fulldevname=$(sys2dev "${sysblock}")
434 if is_in_space_sep_list ${fulldevname} ${black_listed_devices} || \
435 [ -n "${white_listed_devices}" ] && \
436 ! is_in_space_sep_list ${fulldevname} ${white_listed_devices}
438 # skip this device entirely
442 for dev in $(subdevices "${sysblock}")
444 devname=$(sys2dev "${dev}")
446 if is_in_space_sep_list ${devname} ${black_listed_devices}
448 # skip this subdevice
461 # Validate input first
462 if [ -z "${fstype}" ]
467 # Try to look if it is already supported by the kernel
468 if grep -q ${fstype} /proc/filesystems
472 # Then try to add support for it the gentle way using the initramfs capabilities
474 if grep -q ${fstype} /proc/filesystems
477 # Then try the hard way if /root is already reachable
479 kmodule="/root/lib/modules/`uname -r`/${fstype}/${fstype}.ko"
480 if [ -e "${kmodule}" ]
483 if grep -q ${fstype} /proc/filesystems
496 /sbin/blkid -s TYPE -o value $1 2>/dev/null
503 grep -m1 "^${device} " /proc/mounts | cut -f2 -d ' '
508 # remove all unnecessary /:s in the path, including last one (except
509 # if path is just "/")
510 echo ${1} | sed 's|//\+|/|g' | sed 's|^\(.*[^/]\)/$|\1|'
513 what_is_mounted_on ()
515 local dir="$(trim_path ${1})"
516 grep -m1 "^[^ ]\+ ${dir} " /proc/mounts | cut -d' ' -f1
521 local reference="${1}"
524 local owner=$(stat -c %u:%g "${reference}")
525 chown -h ${owner} ${targets}
530 local reference="${1}"
533 local rights=$(stat -c %a "${reference}")
534 chmod ${rights} ${targets}
550 mounts="$(awk '{print $2}' /proc/mounts)"
551 testpath="$(busybox realpath ${testpath})"
555 if echo "${mounts}" | grep -qs "^${testpath}"
557 set -- $(echo "${mounts}" | grep "^${testpath}" | lastline)
561 testpath=$(dirname $testpath)
568 # Returns used/free fs kbytes + 5% more
569 # You could pass a block device as ${1} or the mount point as ${2}
575 if [ -z "${mountp}" ]
577 mountp="$(where_is_mounted ${dev})"
579 if [ -z "${mountp}" ]
581 mountp="/mnt/tmp_fs_size"
584 mount -t $(get_fstype "${dev}") -o ro "${dev}" "${mountp}" || log_warning_msg "cannot mount -t $(get_fstype ${dev}) -o ro ${dev} ${mountp}"
590 if [ "${used}" = "used" ]
592 size=$(du -ks ${mountp} | cut -f1)
593 size=$(expr ${size} + ${size} / 20 ) # FIXME: 5% more to be sure
596 size="$(df -k | grep -s ${mountp} | awk '{print $4}')"
599 if [ -n "${doumount}" ]
601 umount "${mountp}" || log_warning_msg "cannot umount ${mountp}"
611 if [ -x /bin/loadkeys -a -r /etc/boottime.kmap.gz ]
613 loadkeys /etc/boottime.kmap.gz
623 local encryption=${5}
626 # the output of setup_loop is evaluated in other functions,
627 # modprobe leaks kernel options like "libata.dma=0"
628 # as "options libata dma=0" on stdout, causing serious
629 # problems therefor, so instead always avoid output to stdout
630 modprobe -q -b "${module}" 1>/dev/null
634 for loopdev in ${pattern}
636 if [ "$(cat ${loopdev}/size)" -eq 0 ]
638 dev=$(sys2dev "${loopdev}")
641 if [ -n "${readonly}" ]
643 if losetup --help 2>&1 | grep -q -- "-r\b"
645 options="${options} -r"
649 if [ -n "${offset}" ] && [ 0 -lt "${offset}" ]
651 options="${options} -o ${offset}"
654 if [ -z "${encryption}" ]
656 losetup ${options} "${dev}" "${fspath}"
658 # Loop AES encryption
663 echo -n "Enter passphrase for root filesystem: " >&6
665 echo "${passphrase}" > /tmp/passphrase
667 exec 9</tmp/passphrase
668 /sbin/losetup ${options} -e "${encryption}" -p 9 "${dev}" "${fspath}"
671 rm -f /tmp/passphrase
673 if [ 0 -eq ${error} ]
680 echo -n "There was an error decrypting the root filesystem ... Retry? [Y/n] " >&6
683 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
696 panic "No loop devices available"
706 old_mountp="$(where_is_mounted ${dev})"
708 if [ -n "${old_mountp}" ]
710 if [ "${opts}" != "ro" ]
712 mount -o remount,"${opts}" "${dev}" "${old_mountp}" || panic "Remounting ${dev} ${opts} on ${old_mountp} failed"
715 mount -o bind "${old_mountp}" "${mountp}" || panic "Cannot bind-mount ${old_mountp} on ${mountp}"
717 if [ -z "${fstype}" ]
719 fstype=$(get_fstype "${dev}")
721 mount -t "${fstype}" -o "${opts}" "${dev}" "${mountp}" || \
722 ( echo "SKIPPING: Cannot mount ${dev} on ${mountp}, fstype=${fstype}, options=${opts}" > boot.log && return 0 )
726 # Try to mount $device to the place expected by live-boot. If $device
727 # is already mounted somewhere, move it to the expected place. If
728 # we're only probing $device (to check if it has custom persistence)
729 # $probe should be set, which suppresses warnings upon failure. On
730 # success, print the mount point for $device.
731 mount_persistence_media ()
736 local backing="${rootmnt}/lib/live/mount/persistence/$(basename ${device})"
738 mkdir -p "${backing}"
739 local old_backing="$(where_is_mounted ${device})"
740 if [ -z "${old_backing}" ]
742 local fstype="$(get_fstype ${device})"
743 local mount_opts="rw,noatime"
744 if [ -n "${PERSISTENCE_READONLY}" ]
746 mount_opts="ro,noatime"
748 if mount -t "${fstype}" -o "${mount_opts}" "${device}" "${backing}" >/dev/null
753 [ -z "${probe}" ] && log_warning_msg "Failed to mount persistence media ${device}"
757 elif [ "${backing}" != "${old_backing}" ]
759 if mount --move ${old_backing} ${backing} >/dev/null
764 [ -z "${probe}" ] && log_warning_msg "Failed to move persistence media ${device}"
769 # This means that $device has already been mounted on
770 # the place expected by live-boot, so we're done.
776 close_persistence_media ()
779 local backing="$(where_is_mounted ${device})"
781 if [ -d "${backing}" ]
783 umount "${backing}" >/dev/null 2>&1
784 rmdir "${backing}" >/dev/null 2>&1
787 if is_active_luks_mapping ${device}
789 /sbin/cryptsetup luksClose ${device}
796 name="$(basename ${dev})"
798 if [ -n "${PERSISTENCE_READONLY}" ]
800 opts="${opts} --readonly"
803 if /sbin/cryptsetup status "${name}" >/dev/null 2>&1
805 re="^[[:space:]]*device:[[:space:]]*\([^[:space:]]*\)$"
806 opened_dev=$(cryptsetup status ${name} 2>/dev/null | grep "${re}" | sed "s|${re}|\1|")
807 if [ "${opened_dev}" = "${dev}" ]
809 luks_device="/dev/mapper/${name}"
813 log_warning_msg "Cannot open luks device ${dev} since ${opened_dev} already is opened with its name"
822 /lib/cryptsetup/askpass "Enter passphrase for ${dev}: " | \
823 /sbin/cryptsetup -T 1 luksOpen ${dev} ${name} ${opts}
827 luks_device="/dev/mapper/${name}"
833 echo -n "There was an error decrypting ${dev} ... Retry? [Y/n] " >&6
836 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
846 /sbin/blkid -s PART_ENTRY_NAME -p -o value ${dev} 2>/dev/null
852 [ "$(/sbin/blkid -s PART_ENTRY_SCHEME -p -o value ${dev} 2>/dev/null)" = "gpt" ]
855 probe_for_gpt_name ()
857 local overlays="${1}"
860 local gpt_dev="${dev}"
861 if is_active_luks_mapping ${dev}
863 # if $dev is an opened luks device, we need to check
864 # GPT stuff on the backing device
865 gpt_dev=$(get_luks_backing_device "${dev}")
868 if ! is_gpt_device ${gpt_dev}
873 local gpt_name=$(get_gpt_name ${gpt_dev})
874 for label in ${overlays}
876 if [ "${gpt_name}" = "${label}" ]
878 echo "${label}=${dev}"
883 probe_for_fs_label ()
885 local overlays="${1}"
888 for label in ${overlays}
890 if [ "$(/sbin/blkid -s LABEL -o value $dev 2>/dev/null)" = "${label}" ]
892 echo "${label}=${dev}"
897 probe_for_file_name ()
899 local overlays="${1}"
903 local backing="$(mount_persistence_media ${dev} probe)"
904 if [ -z "${backing}" ]
909 for label in ${overlays}
911 path=${backing}/${PERSISTENCE_PATH}${label}
914 local loopdev=$(setup_loop "${path}" "loop" "/sys/block/loop*")
915 ret="${ret} ${label}=${loopdev}"
923 # unmount and remove mountpoint
924 umount ${backing} > /dev/null 2>&1 || true
925 rmdir ${backing} > /dev/null 2>&1 || true
929 find_persistence_media ()
931 # Scans devices for overlays, and returns a whitespace
932 # separated list of how to use them. Only overlays with a partition
933 # label or file name in ${overlays} are returned.
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 all other cases (overlay partition and overlay file) the
940 # return value is "${label}=${device}", where ${device} a device that
941 # can mount the content. In the case of an overlay file, the device
942 # containing the file will remain mounted as a side-effect.
944 # No devices in ${black_listed_devices} will be scanned, and if
945 # ${white_list_devices} is non-empty, only devices in it will be
948 local overlays="${1}"
949 local white_listed_devices="${2}"
952 local black_listed_devices="$(what_is_mounted_on ${rootmnt}/lib/live/medium)"
954 for dev in $(storage_devices "${black_listed_devices}" "${white_listed_devices}")
959 # Check if it's a luks device; we'll have to open the device
960 # in order to probe any filesystem it contains, like we do
961 # below. activate_custom_mounts() also depends on that any luks
962 # device already has been opened.
963 if is_in_comma_sep_list luks ${PERSISTENCE_ENCRYPTION} && is_luks_partition ${dev}
965 if luks_device=$(open_luks_device "${dev}")
969 # skip $dev since we failed/chose not to open it
972 elif ! is_in_comma_sep_list none ${PERSISTENCE_ENCRYPTION}
974 # skip $dev since we don't allow unencrypted storage
978 # Probe for matching GPT partition names or filesystem labels
979 if is_in_comma_sep_list filesystem ${PERSISTENCE_STORAGE}
981 result=$(probe_for_gpt_name "${overlays}" ${dev})
982 if [ -n "${result}" ]
984 ret="${ret} ${result}"
988 result=$(probe_for_fs_label "${overlays}" ${dev})
989 if [ -n "${result}" ]
991 ret="${ret} ${result}"
996 # Probe for files with matching name on mounted partition
997 if is_in_comma_sep_list file ${PERSISTENCE_STORAGE}
999 result=$(probe_for_file_name "${overlays}" ${dev})
1000 if [ -n "${result}" ]
1002 ret="${ret} ${result}"
1007 # Close luks device if it isn't used
1008 if [ -z "${result}" ] && [ -n "${luks_device}" ] && is_active_luks_mapping "${luks_device}"
1010 /sbin/cryptsetup luksClose "${luks_device}"
1024 for adaptor in /sys/class/net/*
1026 status="$(cat ${adaptor}/iflink)"
1028 if [ "${status}" -eq 2 ]
1030 mac="$(cat ${adaptor}/address)"
1031 mac="$(echo ${mac} | sed 's/:/-/g' | tr '[a-z]' '[A-Z]')"
1038 is_luks_partition ()
1041 /sbin/cryptsetup isLuks "${device}" 1>/dev/null 2>&1
1044 is_active_luks_mapping ()
1047 /sbin/cryptsetup status "${device}" 1>/dev/null 2>&1
1050 get_luks_backing_device ()
1053 cryptsetup status ${device} 2> /dev/null | \
1054 awk '{if ($1 == "device:") print $2}'
1059 output_format="${1}"
1063 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1066 if [ "$(cat ${sysblock}/removable)" = "1" ]
1068 if [ -z "${want_usb}" ]
1072 if readlink ${sysblock} | grep -q usb
1079 if [ "${dev_ok}" = "true" ]
1081 case "${output_format}" in
1083 ret="${ret} ${sysblock}"
1086 devname=$(sys2dev "${sysblock}")
1087 ret="${ret} ${devname}"
1096 removable_usb_dev ()
1098 output_format="${1}"
1100 removable_dev "${output_format}" "want_usb"
1103 non_removable_dev ()
1105 output_format="${1}"
1108 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1110 if [ "$(cat ${sysblock}/removable)" = "0" ]
1112 case "${output_format}" in
1114 ret="${ret} ${sysblock}"
1117 devname=$(sys2dev "${sysblock}")
1118 ret="${ret} ${devname}"
1129 # create source's directory structure in dest, and recursively
1130 # create symlinks in dest to to all files in source. if mask
1131 # is non-empty, remove mask from all source paths when
1132 # creating links (will be necessary if we change root, which
1133 # live-boot normally does (into $rootmnt)).
1135 # remove multiple /:s and ensure ending on /
1136 local src_dir="$(trim_path ${1})/"
1137 local dest_dir="$(trim_path ${2})/"
1138 local src_mask="${3}"
1140 # This check can only trigger on the inital, non-recursive call since
1141 # we create the destination before recursive calls
1142 if [ ! -d "${dest_dir}" ]
1144 log_warning_msg "Must link_files into a directory"
1148 find "${src_dir}" -mindepth 1 -maxdepth 1 | \
1151 local dest="${dest_dir}$(basename "${src}")"
1154 if [ -z "$(ls -A "${src}")" ]
1158 if [ ! -d "${dest}" ]
1161 chown_ref "${src}" "${dest}"
1162 chmod_ref "${src}" "${dest}"
1164 link_files "${src}" "${dest}" "${src_mask}"
1166 local final_src=${src}
1167 if [ -n "${src_mask}" ]
1169 final_src="$(echo ${final_src} | sed "s|^${src_mask}||")"
1171 rm -rf "${dest}" 2> /dev/null
1172 ln -s "${final_src}" "${dest}"
1173 chown_ref "${src}" "${dest}"
1180 local unionmountpoint="${1}" # directory where the union is mounted
1181 local unionrw="${2}" # branch where the union changes are stored
1182 local unionro1="${3}" # first underlying read-only branch (optional)
1183 local unionro2="${4}" # second underlying read-only branch (optional)
1185 case "${UNIONTYPE}" in
1203 case "${UNIONTYPE}" in
1205 unionmountopts="-o cow -o noinitgroups -o default_permissions -o allow_other -o use_ino -o suid"
1206 unionmountopts="${unionmountopts} ${unionrw}=${rw_opt}"
1207 if [ -n "${unionro1}" ]
1209 unionmountopts="${unionmountopts}:${unionro1}=${ro_opt}"
1211 if [ -n "${unionro2}" ]
1213 unionmountopts="${unionmountopts}:${unionro2}=${ro_opt}"
1215 ( sysctl -w fs.file-max=391524 ; ulimit -HSn 16384
1216 unionfs-fuse ${unionmountopts} "${unionmountpoint}" ) && \
1217 ( mkdir -p /run/sendsigs.omit.d
1218 pidof unionfs-fuse >> /run/sendsigs.omit.d/unionfs-fuse || true )
1222 # XXX: can unionro2 be used? (overlayfs only handles two dirs, but perhaps they can be chained?)
1223 # XXX: and can unionro1 be optional? i.e. can overlayfs skip lowerdir?
1224 unionmountopts="-o noatime,lowerdir=${unionro1},upperdir=${unionrw}"
1225 mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
1229 unionmountopts="-o noatime,${noxino_opt},dirs=${unionrw}=${rw_opt}"
1230 if [ -n "${unionro1}" ]
1232 unionmountopts="${unionmountopts}:${unionro1}=${ro_opt}"
1234 if [ -n "${unionro2}" ]
1236 unionmountopts="${unionmountopts}:${unionro2}=${ro_opt}"
1238 mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
1243 get_custom_mounts ()
1245 # Side-effect: leaves $devices with persistence.conf mounted in ${rootmnt}/lib/live/mount/persistence
1246 # Side-effect: prints info to file $custom_mounts
1248 local custom_mounts=${1}
1252 local bindings="/tmp/bindings.list"
1253 local links="/tmp/links.list"
1254 rm -rf ${bindings} ${links} 2> /dev/null
1256 for device in ${devices}
1258 if [ ! -b "${device}" ]
1263 local device_name="$(basename ${device})"
1264 local backing=$(mount_persistence_media ${device})
1265 if [ -z "${backing}" ]
1271 if [ -r "${backing}/${persistence_list}" ]
1273 include_list="${backing}/${persistence_list}"
1274 elif [ -r "${backing}/${old_persistence_list}" ]
1276 include_list="${backing}/${old_persistence_list}"
1281 if [ -n "${DEBUG}" ] && [ -e "${include_list}" ]
1283 cp ${include_list} ${rootmnt}/lib/live/mount/persistence/${persistence_list}.${device_name}
1286 while read dir options # < ${include_list}
1288 if echo ${dir} | grep -qe "^[[:space:]]*\(#.*\)\?$"
1290 # skipping empty or commented lines
1294 if trim_path ${dir} | grep -q -e "^[^/]" -e "^/lib" -e "^/lib/live\(/.*\)\?$" -e "^/\(.*/\)\?\.\.\?\(/.*\)\?$"
1296 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."
1302 for opt in $(echo ${options} | tr ',' ' ');
1306 opt_source=${opt#source=}
1314 log_warning_msg "Skipping custom mount with unkown option: ${opt}"
1320 local source="${dir}"
1321 if [ -n "${opt_source}" ]
1323 if echo ${opt_source} | grep -q -e "^/" -e "^\(.*/\)\?\.\.\?\(/.*\)\?$" && [ "${opt_source}" != "." ]
1325 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"
1328 source="${opt_source}"
1332 local full_source="$(trim_path ${backing}/${source})"
1333 local full_dest="$(trim_path ${rootmnt}/${dir})"
1334 if [ -n "${opt_link}" ]
1336 echo "${device} ${full_source} ${full_dest} ${options}" >> ${links}
1338 echo "${device} ${full_source} ${full_dest} ${options}" >> ${bindings}
1340 done < ${include_list}
1343 # We sort the list according to destination so we're sure that
1344 # we won't hide a previous mount. We also ignore duplicate
1345 # destinations in a more or less arbitrary way.
1346 [ -e "${bindings}" ] && sort -k3 -sbu ${bindings} >> ${custom_mounts} && rm ${bindings}
1348 # After all mounts are considered we add symlinks so they
1349 # won't be hidden by some mount.
1350 [ -e "${links}" ] && cat ${links} >> ${custom_mounts} && rm ${links}
1352 # We need to make sure that no two custom mounts have the same sources
1353 # or are nested; if that is the case, too much weird stuff can happen.
1354 local prev_source="impossible source" # first iteration must not match
1356 # This sort will ensure that a source /a comes right before a source
1357 # /a/b so we only need to look at the previous source
1358 sort -k2 -b ${custom_mounts} |
1359 while read device source dest options
1361 if echo ${source} | grep -qe "^${prev_source}\(/.*\)\?$"
1363 panic "Two persistence mounts have the same or nested sources: ${source} on ${dest}, and ${prev_source} on ${prev_dest}"
1365 prev_source=${source}
1370 activate_custom_mounts ()
1372 local custom_mounts="${1}" # the ouput from get_custom_mounts()
1373 local used_devices=""
1375 while read device source dest options # < ${custom_mounts}
1377 local opt_bind="true"
1380 for opt in $(echo ${options} | tr ',' ' ');
1385 unset opt_link opt_union
1389 unset opt_bind opt_union
1393 unset opt_bind opt_link
1398 if [ -n "$(what_is_mounted_on "${dest}")" ]
1400 if [ "${dest}" = "${rootmnt}" ]
1404 log_warning_msg "Skipping custom mount ${dest}: $(what_is_mounted_on "${dest}") is already mounted there"
1409 if [ ! -d "${dest}" ]
1411 # create the destination and delete existing files in
1412 # its path that are in the way
1414 for dir in $(echo ${dest} | sed -e 's|/\+| |g')
1416 path=$(trim_path ${path}/${dir})
1424 if echo ${path} | grep -qe "^${rootmnt}/*home/[^/]\+"
1426 # if ${dest} is in /home try fixing proper ownership by assuming that the intended user is the first, which is usually the case
1427 # FIXME: this should really be handled by live-config since we don't know for sure which uid a certain user has until then
1428 chown 1000:1000 ${path}
1434 # if ${source} doesn't exist on our persistence media
1435 # we bootstrap it with $dest from the live filesystem.
1436 # this both makes sense and is critical if we're
1437 # dealing with /etc or other system dir.
1438 if [ ! -d "${source}" ]
1440 if [ -n "${PERSISTENCE_READONLY}" ]
1443 elif [ -n "${opt_union}" ] || [ -n "${opt_link}" ]
1445 # unions and don't need to be bootstrapped
1446 # link dirs can't be bootstrapped in a sensible way
1447 mkdir -p "${source}"
1448 chown_ref "${dest}" "${source}"
1449 chmod_ref "${dest}" "${source}"
1450 elif [ -n "${opt_bind}" ]
1452 # ensure that $dest is not copied *into* $source
1453 mkdir -p "$(dirname ${source})"
1454 cp -a "${dest}" "${source}"
1458 # XXX: If CONFIG_AUFS_ROBR is added to the Debian kernel we can
1459 # ignore the loop below and set rootfs_dest_backing=$dest
1460 local rootfs_dest_backing=""
1461 if [ -n "${opt_link}"]
1463 for d in ${rootmnt}/lib/live/mount/rootfs/*
1465 if [ -n "${rootmnt}" ]
1467 rootfs_dest_backing="${d}/$(echo ${dest} | sed -e "s|${rootmnt}||")"
1469 rootfs_dest_backing="${d}/${dest}"
1471 if [ -d "${rootfs_dest_backing}" ]
1475 rootfs_dest_backing=""
1480 if [ -n "${opt_link}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1482 link_files ${source} ${dest} ${rootmnt}
1483 elif [ -n "${opt_link}" ] && [ -n "${PERSISTENCE_READONLY}" ]
1485 mkdir -p ${rootmnt}/lib/live/mount/persistence
1486 local links_source=$(mktemp -d ${rootmnt}/lib/live/mount/persistence/links-source-XXXXXX)
1487 chown_ref ${source} ${links_source}
1488 chmod_ref ${source} ${links_source}
1489 # We put the cow dir in the below strange place to
1490 # make it absolutely certain that the link source
1491 # has its own directory and isn't nested with some
1492 # other custom mount (if so that mount's files would
1493 # be linked, causing breakage.
1494 local cow_dir="${rootmnt}/lib/live/mount/overlay/lib/live/mount/persistence/$(basename ${links_source})"
1496 chown_ref "${source}" "${cow_dir}"
1497 chmod_ref "${source}" "${cow_dir}"
1498 do_union ${links_source} ${cow_dir} ${source} ${rootfs_dest_backing}
1499 link_files ${links_source} ${dest} ${rootmnt}
1500 elif [ -n "${opt_union}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1502 do_union ${dest} ${source} ${rootfs_dest_backing}
1503 elif [ -n "${opt_bind}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1505 mount --bind "${source}" "${dest}"
1506 elif [ -n "${opt_bind}" -o -n "${opt_union}" ] && [ -n "${PERSISTENCE_READONLY}" ]
1508 # bind-mount and union mount are handled the same
1509 # in read-only mode, but note that rootfs_dest_backing
1510 # is non-empty (and necessary) only for unions
1511 local cow_dir="${rootmnt}/lib/live/mount/overlay/${dest}"
1512 if [ -e "${cow_dir}" ] && [ -z "${opt_link}" ]
1514 # If an earlier custom mount has files here
1515 # it will "block" the current mount's files
1516 # which is undesirable
1520 chown_ref "${source}" "${cow_dir}"
1521 chmod_ref "${source}" "${cow_dir}"
1522 do_union ${dest} ${cow_dir} ${source} ${rootfs_dest_backing}
1525 PERSISTENCE_IS_ON="1"
1526 export PERSISTENCE_IS_ON
1528 if echo ${used_devices} | grep -qve "^\(.* \)\?${device}\( .*\)\?$"
1530 used_devices="${used_devices} ${device}"
1532 done < ${custom_mounts}
1534 echo ${used_devices}
1537 fix_backwards_compatibility ()
1543 if [ -n "${PERSISTENCE_READONLY}" ]
1548 local backing="$(mount_persistence_media ${device})"
1549 if [ -z "${backing}" ]
1554 local include_list="${backing}/${persistence_list}"
1555 if [ ! -r "${include_list}" ]
1557 echo "# persistence backwards compatibility:
1558 ${dir} ${opt},source=." > "${include_list}"
1566 [ $(stat -fc%d:%D "${directory}") != $(stat -fc%d:%D "${directory}/..") ]