Dropping helper function used in one place only which can be replaced with a simple...
[debian-live-boot.git] / components / 9990-misc-helpers.sh
blob245e9da1dd18b3b7af550a3436b59f94fdf657c7
1 #!/bin/sh
3 #set -e
5 is_live_path()
7 DIRECTORY="${1}/${LIVE_MEDIA_PATH}"
8 for FILESYSTEM in squashfs ext2 ext3 ext4 xfs dir jffs
9 do
10 if ls "${DIRECTORY}/"*.${FILESYSTEM} > /dev/null 2>&1
11 then
12 return 0
14 done
15 return 1
18 matches_uuid ()
20 if [ "${IGNORE_UUID}" ] || [ ! -e /conf/uuid.conf ]
21 then
22 return 0
25 path="${1}"
26 uuid="$(cat /conf/uuid.conf)"
28 for try_uuid_file in "${path}/.disk/live-uuid"*
30 [ -e "${try_uuid_file}" ] || continue
32 try_uuid="$(cat "${try_uuid_file}")"
34 if [ "${uuid}" = "${try_uuid}" ]
35 then
36 return 0
38 done
40 return 1
43 get_backing_device ()
45 case "${1}" in
46 *.squashfs|*.ext2|*.ext3|*.ext4|*.jffs2)
47 echo $(setup_loop "${1}" "loop" "/sys/block/loop*" '0' "${LIVE_MEDIA_ENCRYPTION}" "${2}")
50 *.dir)
51 echo "directory"
55 panic "Unrecognized live filesystem: ${1}"
57 esac
60 mount_images_in_directory ()
62 directory="${1}"
63 rootmnt="${2}"
64 mac="${3}"
66 if is_live_path "${directory}"
67 then
68 [ -n "${mac}" ] && adddirectory="${directory}/${LIVE_MEDIA_PATH}/${mac}"
69 setup_unionfs "${directory}/${LIVE_MEDIA_PATH}" "${rootmnt}" "${adddirectory}"
70 else
71 panic "No supported filesystem images found at /${LIVE_MEDIA_PATH}."
75 is_nice_device ()
77 sysfs_path="${1#/sys}"
79 if udevadm test-builtin 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)"
80 then
81 return 0
82 elif echo "${sysfs_path}" | grep -q '^/block/vd[a-z]$'
83 then
84 return 0
85 elif echo ${sysfs_path} | grep -q "^/block/dm-"
86 then
87 return 0
88 elif echo ${sysfs_path} | grep -q "^/block/mtdblock"
89 then
90 return 0
93 return 1
96 check_dev ()
98 sysdev="${1}"
99 devname="${2}"
100 skip_uuid_check="${3}"
102 # support for fromiso=.../isofrom=....
103 if [ -n "$FROMISO" ]
104 then
105 ISO_DEVICE=$(dirname $FROMISO)
106 if ! [ -b $ISO_DEVICE ]
107 then
108 # to support unusual device names like /dev/cciss/c0d0p1
109 # as well we have to identify the block device name, let's
110 # do that for up to 15 levels
111 i=15
112 while [ -n "$ISO_DEVICE" ] && [ "$i" -gt 0 ]
114 ISO_DEVICE=$(dirname ${ISO_DEVICE})
115 [ -b "$ISO_DEVICE" ] && break
116 i=$(($i -1))
117 done
120 if [ "$ISO_DEVICE" = "/" ]
121 then
122 echo "Warning: device for bootoption fromiso= ($FROMISO) not found.">>/boot.log
123 else
124 fs_type=$(get_fstype "${ISO_DEVICE}")
125 if is_supported_fs ${fs_type}
126 then
127 mkdir /live/fromiso
128 mount -t $fs_type "$ISO_DEVICE" /live/fromiso
129 ISO_NAME="$(echo $FROMISO | sed "s|$ISO_DEVICE||")"
130 loopdevname=$(setup_loop "/live/fromiso/${ISO_NAME}" "loop" "/sys/block/loop*" "" '')
131 devname="${loopdevname}"
132 else
133 echo "Warning: unable to mount $ISO_DEVICE." >>/boot.log
138 if [ -z "${devname}" ]
139 then
140 devname=$(sys2dev "${sysdev}")
143 if [ -d "${devname}" ]
144 then
145 mount -o bind "${devname}" $mountpoint || continue
147 if is_live_path $mountpoint
148 then
149 echo $mountpoint
150 return 0
151 else
152 umount $mountpoint
156 IFS=","
157 for device in ${devname}
159 case "$device" in
160 *mapper*)
161 # Adding lvm support
162 if [ -x /scripts/local-top/lvm2 ]
163 then
164 ROOT="$device" resume="" /scripts/local-top/lvm2
168 /dev/md*)
169 # Adding raid support
170 if [ -x /scripts/local-top/mdadm ]
171 then
172 cp /conf/conf.d/md /conf/conf.d/md.orig
173 echo "MD_DEVS=$device " >> /conf/conf.d/md
174 /scripts/local-top/mdadm
175 mv /conf/conf.d/md.orig /conf/conf.d/md
178 esac
179 done
180 unset IFS
182 [ -n "$device" ] && devname="$device"
184 [ -e "$devname" ] || continue
186 if [ -n "${LIVE_MEDIA_OFFSET}" ]
187 then
188 loopdevname=$(setup_loop "${devname}" "loop" "/sys/block/loop*" "${LIVE_MEDIA_OFFSET}" '')
189 devname="${loopdevname}"
192 fstype=$(get_fstype "${devname}")
194 if is_supported_fs ${fstype}
195 then
196 devuid=$(blkid -o value -s UUID "$devname")
197 [ -n "$devuid" ] && grep -qs "\<$devuid\>" /var/lib/live/boot/devices-already-tried-to-mount && continue
198 mount -t ${fstype} -o ro,noatime "${devname}" ${mountpoint} || continue
199 [ -n "$devuid" ] && echo "$devuid" >> /var/lib/live/boot/devices-already-tried-to-mount
201 if [ -n "${FINDISO}" ]
202 then
203 if [ -f ${mountpoint}/${FINDISO} ]
204 then
205 umount ${mountpoint}
206 mkdir -p /live/findiso
207 mount -t ${fstype} -o ro,noatime "${devname}" /live/findiso
208 loopdevname=$(setup_loop "/live/findiso/${FINDISO}" "loop" "/sys/block/loop*" 0 "")
209 devname="${loopdevname}"
210 mount -t iso9660 -o ro,noatime "${devname}" ${mountpoint}
211 else
212 umount ${mountpoint}
216 if is_live_path ${mountpoint} && \
217 ([ "${skip_uuid_check}" ] || matches_uuid ${mountpoint})
218 then
219 echo ${mountpoint}
220 return 0
221 else
222 umount ${mountpoint} 2>/dev/null
226 if [ -n "${LIVE_MEDIA_OFFSET}" ]
227 then
228 losetup -d "${loopdevname}"
231 return 1
234 find_livefs ()
236 timeout="${1}"
238 # don't start autodetection before timeout has expired
239 if [ -n "${LIVE_MEDIA_TIMEOUT}" ]
240 then
241 if [ "${timeout}" -lt "${LIVE_MEDIA_TIMEOUT}" ]
242 then
243 return 1
247 # first look at the one specified in the command line
248 case "${LIVE_MEDIA}" in
249 removable-usb)
250 for sysblock in $(removable_usb_dev "sys")
252 for dev in $(subdevices "${sysblock}")
254 if check_dev "${dev}"
255 then
256 return 0
258 done
259 done
260 return 1
263 removable)
264 for sysblock in $(removable_dev "sys")
266 for dev in $(subdevices "${sysblock}")
268 if check_dev "${dev}"
269 then
270 return 0
272 done
273 done
274 return 1
278 if [ ! -z "${LIVE_MEDIA}" ]
279 then
280 if check_dev "null" "${LIVE_MEDIA}" "skip_uuid_check"
281 then
282 return 0
286 esac
288 # or do the scan of block devices
289 # prefer removable devices over non-removable devices, so scan them first
290 devices_to_scan="$(removable_dev 'sys') $(non_removable_dev 'sys')"
292 for sysblock in $devices_to_scan
294 devname=$(sys2dev "${sysblock}")
295 [ -e "$devname" ] || continue
296 fstype=$(get_fstype "${devname}")
298 if /lib/udev/cdrom_id ${devname} > /dev/null
299 then
300 if check_dev "null" "${devname}"
301 then
302 return 0
304 elif is_nice_device "${sysblock}"
305 then
306 for dev in $(subdevices "${sysblock}")
308 if check_dev "${dev}"
309 then
310 return 0
312 done
313 elif [ "${fstype}" = "squashfs" -o \
314 "${fstype}" = "btrfs" -o \
315 "${fstype}" = "ext2" -o \
316 "${fstype}" = "ext3" -o \
317 "${fstype}" = "ext4" -o \
318 "${fstype}" = "jffs2" ]
319 then
320 # This is an ugly hack situation, the block device has
321 # an image directly on it. It's hopefully
322 # live-boot, so take it and run with it.
323 ln -s "${devname}" "${devname}.${fstype}"
324 echo "${devname}.${fstype}"
325 return 0
327 done
329 return 1
332 really_export ()
334 STRING="${1}"
335 VALUE="$(eval echo -n \${$STRING})"
337 if [ -f /live.vars ] && grep -sq "export ${STRING}" /live.vars
338 then
339 sed -i -e 's/\('${STRING}'=\).*$/\1'${VALUE}'/' /live.vars
340 else
341 echo "export ${STRING}=\"${VALUE}\"" >> /live.vars
344 eval export "${STRING}"="${VALUE}"
347 is_in_list_separator_helper ()
349 local sep element list
350 sep=${1}
351 shift
352 element=${1}
353 shift
354 list=${*}
355 echo ${list} | grep -qe "^\(.*${sep}\)\?${element}\(${sep}.*\)\?$"
358 is_in_space_sep_list ()
360 local element
361 element=${1}
362 shift
363 is_in_list_separator_helper "[[:space:]]" "${element}" "${*}"
366 is_in_comma_sep_list ()
368 local element
369 element=${1}
370 shift
371 is_in_list_separator_helper "," "${element}" "${*}"
374 sys2dev ()
376 sysdev=${1#/sys}
377 echo "/dev/$(udevadm info -q name -p ${sysdev} 2>/dev/null|| echo ${sysdev##*/})"
380 subdevices ()
382 sysblock=${1}
383 r=""
385 for dev in "${sysblock}"/* "${sysblock}"
387 if [ -e "${dev}/dev" ]
388 then
389 r="${r} ${dev}"
391 done
393 echo ${r}
396 storage_devices()
398 black_listed_devices="${1}"
399 white_listed_devices="${2}"
401 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "loop|ram|fd")
403 fulldevname=$(sys2dev "${sysblock}")
405 if is_in_space_sep_list ${fulldevname} ${black_listed_devices} || \
406 [ -n "${white_listed_devices}" ] && \
407 ! is_in_space_sep_list ${fulldevname} ${white_listed_devices}
408 then
409 # skip this device entirely
410 continue
413 for dev in $(subdevices "${sysblock}")
415 devname=$(sys2dev "${dev}")
417 if is_in_space_sep_list ${devname} ${black_listed_devices}
418 then
419 # skip this subdevice
420 continue
421 else
422 echo "${devname}"
424 done
425 done
428 is_supported_fs ()
430 fstype="${1}"
432 # Validate input first
433 if [ -z "${fstype}" ]
434 then
435 return 1
438 # Try to look if it is already supported by the kernel
439 if grep -q ${fstype} /proc/filesystems
440 then
441 return 0
442 else
443 # Then try to add support for it the gentle way using the initramfs capabilities
444 modprobe ${fstype}
445 if grep -q ${fstype} /proc/filesystems
446 then
447 return 0
448 # Then try the hard way if /root is already reachable
449 else
450 kmodule="/root/lib/modules/`uname -r`/${fstype}/${fstype}.ko"
451 if [ -e "${kmodule}" ]
452 then
453 insmod "${kmodule}"
454 if grep -q ${fstype} /proc/filesystems
455 then
456 return 0
462 return 1
465 get_fstype ()
467 /sbin/blkid -s TYPE -o value $1 2>/dev/null
470 where_is_mounted ()
472 device=${1}
473 # return first found
474 grep -m1 "^${device} " /proc/mounts | cut -f2 -d ' '
477 trim_path ()
479 # remove all unnecessary /:s in the path, including last one (except
480 # if path is just "/")
481 echo ${1} | sed 's|//\+|/|g' | sed 's|^\(.*[^/]\)/$|\1|'
484 what_is_mounted_on ()
486 local dir
487 dir="$(trim_path ${1})"
488 grep -m1 "^[^ ]\+ ${dir} " /proc/mounts | cut -d' ' -f1
491 chown_ref ()
493 local reference targets owner
494 reference="${1}"
495 shift
496 targets=${@}
497 owner=$(stat -c %u:%g "${reference}")
498 chown -h ${owner} ${targets}
501 chmod_ref ()
503 local reference targets rights
504 reference="${1}"
505 shift
506 targets=${@}
507 rights=$(stat -c %a "${reference}")
508 chmod ${rights} ${targets}
511 lastline ()
513 while read lines
515 line=${lines}
516 done
518 echo "${line}"
521 base_path ()
523 testpath="${1}"
524 mounts="$(awk '{print $2}' /proc/mounts)"
525 testpath="$(busybox realpath ${testpath})"
527 while true
529 if echo "${mounts}" | grep -qs "^${testpath}"
530 then
531 set -- $(echo "${mounts}" | grep "^${testpath}" | lastline)
532 echo ${1}
533 break
534 else
535 testpath=$(dirname $testpath)
537 done
540 fs_size ()
542 # Returns used/free fs kbytes + 5% more
543 # You could pass a block device as ${1} or the mount point as ${2}
545 dev="${1}"
546 mountp="${2}"
547 used="${3}"
549 if [ -z "${mountp}" ]
550 then
551 mountp="$(where_is_mounted ${dev})"
553 if [ -z "${mountp}" ]
554 then
555 mountp="/mnt/tmp_fs_size"
557 mkdir -p "${mountp}"
558 mount -t $(get_fstype "${dev}") -o ro "${dev}" "${mountp}" || log_warning_msg "cannot mount -t $(get_fstype ${dev}) -o ro ${dev} ${mountp}"
560 doumount=1
564 if [ "${used}" = "used" ]
565 then
566 size=$(du -ks ${mountp} | cut -f1)
567 size=$(expr ${size} + ${size} / 20 ) # FIXME: 5% more to be sure
568 else
569 # free space
570 size="$(df -kP | grep -s ${mountp} | awk '{print $4}')"
573 if [ -n "${doumount}" ]
574 then
575 umount "${mountp}" || log_warning_msg "cannot umount ${mountp}"
576 rmdir "${mountp}"
579 echo "${size}"
582 load_keymap ()
584 # Load custom keymap
585 if [ -x /bin/loadkeys -a -r /etc/boottime.kmap.gz ]
586 then
587 loadkeys --quiet /etc/boottime.kmap.gz
591 setup_loop ()
593 local fspath module pattern offset encryption readonly
594 fspath=${1}
595 module=${2}
596 pattern=${3}
597 offset=${4}
598 encryption=${5}
599 readonly=${6}
601 # the output of setup_loop is evaluated in other functions,
602 # modprobe leaks kernel options like "libata.dma=0"
603 # as "options libata dma=0" on stdout, causing serious
604 # problems therefor, so instead always avoid output to stdout
605 modprobe -q -b "${module}" 1>/dev/null
607 udevadm settle
609 for loopdev in ${pattern}
611 if [ "$(cat ${loopdev}/size)" -eq 0 ]
612 then
613 dev=$(sys2dev "${loopdev}")
614 options=''
616 if [ -n "${readonly}" ]
617 then
618 if losetup --help 2>&1 | grep -q -- "-r\b"
619 then
620 options="${options} -r"
624 if [ -n "${offset}" ] && [ 0 -lt "${offset}" ]
625 then
626 options="${options} -o ${offset}"
629 if [ -z "${encryption}" ]
630 then
631 losetup ${options} "${dev}" "${fspath}"
632 else
633 # Loop AES encryption
634 while true
636 load_keymap
638 echo -n "Enter passphrase for root filesystem: " >&6
639 read -s passphrase
640 echo "${passphrase}" > /tmp/passphrase
641 unset passphrase
642 exec 9</tmp/passphrase
643 /sbin/losetup ${options} -e "${encryption}" -p 9 "${dev}" "${fspath}"
644 error=${?}
645 exec 9<&-
646 rm -f /tmp/passphrase
648 if [ 0 -eq ${error} ]
649 then
650 unset error
651 break
654 echo
655 echo -n "There was an error decrypting the root filesystem ... Retry? [Y/n] " >&6
656 read answer
658 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
659 then
660 unset answer
661 break
663 done
666 echo "${dev}"
667 return 0
669 done
671 panic "No loop devices available"
674 try_mount ()
676 dev="${1}"
677 mountp="${2}"
678 opts="${3}"
679 fstype="${4}"
681 old_mountp="$(where_is_mounted ${dev})"
683 if [ -n "${old_mountp}" ]
684 then
685 if [ "${opts}" != "ro" ]
686 then
687 mount -o remount,"${opts}" "${dev}" "${old_mountp}" || panic "Remounting ${dev} ${opts} on ${old_mountp} failed"
690 mount -o bind "${old_mountp}" "${mountp}" || panic "Cannot bind-mount ${old_mountp} on ${mountp}"
691 else
692 if [ -z "${fstype}" ]
693 then
694 fstype=$(get_fstype "${dev}")
696 mount -t "${fstype}" -o "${opts}" "${dev}" "${mountp}" || \
697 ( echo "SKIPPING: Cannot mount ${dev} on ${mountp}, fstype=${fstype}, options=${opts}" > boot.log && return 0 )
701 # Try to mount $device to the place expected by live-boot. If $device
702 # is already mounted somewhere, move it to the expected place. If
703 # we're only probing $device (to check if it has custom persistence)
704 # $probe should be set, which suppresses warnings upon failure. On
705 # success, print the mount point for $device.
706 mount_persistence_media ()
708 local device probe backing old_backing fstype mount_opts
709 device=${1}
710 probe=${2}
712 backing="/live/persistence/$(basename ${device})"
714 mkdir -p "${backing}"
715 old_backing="$(where_is_mounted ${device})"
716 if [ -z "${old_backing}" ]
717 then
718 fstype="$(get_fstype ${device})"
719 mount_opts="rw,noatime"
720 if [ -n "${PERSISTENCE_READONLY}" ]
721 then
722 mount_opts="ro,noatime"
724 if mount -t "${fstype}" -o "${mount_opts}" "${device}" "${backing}" >/dev/null
725 then
726 echo ${backing}
727 return 0
728 else
729 [ -z "${probe}" ] && log_warning_msg "Failed to mount persistence media ${device}"
730 rmdir "${backing}"
731 return 1
733 elif [ "${backing}" != "${old_backing}" ]
734 then
735 if mount --move ${old_backing} ${backing} >/dev/null
736 then
737 echo ${backing}
738 return 0
739 else
740 [ -z "${probe}" ] && log_warning_msg "Failed to move persistence media ${device}"
741 rmdir "${backing}"
742 return 1
744 else
745 # This means that $device has already been mounted on
746 # the place expected by live-boot, so we're done.
747 echo ${backing}
748 return 0
752 close_persistence_media ()
754 local device backing
755 device=${1}
756 backing="$(where_is_mounted ${device})"
758 if [ -d "${backing}" ]
759 then
760 umount "${backing}" >/dev/null 2>&1
761 rmdir "${backing}" >/dev/null 2>&1
764 if is_active_luks_mapping ${device}
765 then
766 /sbin/cryptsetup luksClose ${device}
770 open_luks_device ()
772 dev="${1}"
773 name="$(basename ${dev})"
774 opts="--key-file=-"
775 if [ -n "${PERSISTENCE_READONLY}" ]
776 then
777 opts="${opts} --readonly"
780 if /sbin/cryptsetup status "${name}" >/dev/null 2>&1
781 then
782 re="^[[:space:]]*device:[[:space:]]*\([^[:space:]]*\)$"
783 opened_dev=$(cryptsetup status ${name} 2>/dev/null | grep "${re}" | sed "s|${re}|\1|")
784 if [ "${opened_dev}" = "${dev}" ]
785 then
786 luks_device="/dev/mapper/${name}"
787 echo ${luks_device}
788 return 0
789 else
790 log_warning_msg "Cannot open luks device ${dev} since ${opened_dev} already is opened with its name"
791 return 1
795 load_keymap
797 while true
799 /lib/cryptsetup/askpass "Enter passphrase for ${dev}: " | \
800 /sbin/cryptsetup -T 1 luksOpen ${dev} ${name} ${opts}
802 if [ 0 -eq ${?} ]
803 then
804 luks_device="/dev/mapper/${name}"
805 echo ${luks_device}
806 return 0
809 echo >&6
810 echo -n "There was an error decrypting ${dev} ... Retry? [Y/n] " >&6
811 read answer
813 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
814 then
815 return 2
817 done
820 get_gpt_name ()
822 local dev
823 dev="${1}"
824 /sbin/blkid -s PART_ENTRY_NAME -p -o value ${dev} 2>/dev/null
827 is_gpt_device ()
829 local dev
830 dev="${1}"
831 [ "$(/sbin/blkid -s PART_ENTRY_SCHEME -p -o value ${dev} 2>/dev/null)" = "gpt" ]
834 probe_for_gpt_name ()
836 local overlays dev gpt_dev gpt_name
837 overlays="${1}"
838 dev="${2}"
840 gpt_dev="${dev}"
841 if is_active_luks_mapping ${dev}
842 then
843 # if $dev is an opened luks device, we need to check
844 # GPT stuff on the backing device
845 gpt_dev=$(get_luks_backing_device "${dev}")
848 if ! is_gpt_device ${gpt_dev}
849 then
850 return
853 gpt_name=$(get_gpt_name ${gpt_dev})
854 for label in ${overlays}
856 if [ "${gpt_name}" = "${label}" ]
857 then
858 echo "${label}=${dev}"
860 done
863 probe_for_fs_label ()
865 local overlays dev
866 overlays="${1}"
867 dev="${2}"
869 for label in ${overlays}
871 if [ "$(/sbin/blkid -s LABEL -o value $dev 2>/dev/null)" = "${label}" ]
872 then
873 echo "${label}=${dev}"
875 done
878 probe_for_file_name ()
880 local overlays dev ret backing
881 overlays="${1}"
882 dev="${2}"
884 ret=""
885 backing="$(mount_persistence_media ${dev} probe)"
886 if [ -z "${backing}" ]
887 then
888 return
891 for label in ${overlays}
893 path=${backing}/${PERSISTENCE_PATH}${label}
894 if [ -f "${path}" ]
895 then
896 local loopdev
897 loopdev=$(setup_loop "${path}" "loop" "/sys/block/loop*")
898 ret="${ret} ${label}=${loopdev}"
900 done
902 if [ -n "${ret}" ]
903 then
904 echo ${ret}
905 else
906 # unmount and remove mountpoint
907 umount ${backing} > /dev/null 2>&1 || true
908 rmdir ${backing} > /dev/null 2>&1 || true
912 find_persistence_media ()
914 # Scans devices for overlays, and returns a whitespace
915 # separated list of how to use them. Only overlays with a partition
916 # label or file name in ${overlays} are returned.
918 # When scanning a LUKS device, the user will be asked to enter the
919 # passphrase; on failure to enter it, or if no persistence partitions
920 # or files were found, the LUKS device is closed.
922 # For all other cases (overlay partition and overlay file) the
923 # return value is "${label}=${device}", where ${device} a device that
924 # can mount the content. In the case of an overlay file, the device
925 # containing the file will remain mounted as a side-effect.
927 # No devices in ${black_listed_devices} will be scanned, and if
928 # ${white_list_devices} is non-empty, only devices in it will be
929 # scanned.
931 local overlays white_listed_devices ret black_listed_devices
932 overlays="${1}"
933 white_listed_devices="${2}"
934 ret=""
936 black_listed_devices="$(what_is_mounted_on /live/medium) $(what_is_mounted_on /live/findiso) $(what_is_mounted_on /live/fromiso)"
938 for dev in $(storage_devices "${black_listed_devices}" "${white_listed_devices}")
940 local result luks_device
941 result=""
943 luks_device=""
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} && is_luks_partition ${dev}
949 then
950 if luks_device=$(open_luks_device "${dev}")
951 then
952 dev="${luks_device}"
953 else
954 # skip $dev since we failed/chose not to open it
955 continue
957 elif ! is_in_comma_sep_list none ${PERSISTENCE_ENCRYPTION}
958 then
959 # skip $dev since we don't allow unencrypted storage
960 continue
963 # Probe for matching GPT partition names or filesystem labels
964 if is_in_comma_sep_list filesystem ${PERSISTENCE_STORAGE}
965 then
966 result=$(probe_for_gpt_name "${overlays}" ${dev})
967 if [ -n "${result}" ]
968 then
969 ret="${ret} ${result}"
970 continue
973 result=$(probe_for_fs_label "${overlays}" ${dev})
974 if [ -n "${result}" ]
975 then
976 ret="${ret} ${result}"
977 continue
981 # Probe for files with matching name on mounted partition
982 if is_in_comma_sep_list file ${PERSISTENCE_STORAGE}
983 then
984 result=$(probe_for_file_name "${overlays}" ${dev})
985 if [ -n "${result}" ]
986 then
987 local loopdevice
988 loopdevice=${result##*=}
989 if is_in_comma_sep_list luks ${PERSISTENCE_ENCRYPTION} && is_luks_partition ${loopdevice}
990 then
991 local luksfile
992 luksfile=""
993 if luksfile=$(open_luks_device "${loopdevice}")
994 then
995 result=${result%%=*}
996 result="${result}=${luksfile}"
997 else
998 losetup -d $loopdevice
999 result=""
1002 ret="${ret} ${result}"
1003 continue
1007 # Close luks device if it isn't used
1008 if [ -z "${result}" ] && [ -n "${luks_device}" ] && is_active_luks_mapping "${luks_device}"
1009 then
1010 /sbin/cryptsetup luksClose "${luks_device}"
1012 done
1014 if [ -n "${ret}" ]
1015 then
1016 echo ${ret}
1020 get_mac ()
1022 mac=""
1024 for adaptor in /sys/class/net/*
1026 status="$(cat ${adaptor}/iflink)"
1028 if [ "${status}" -eq 2 ]
1029 then
1030 mac="$(cat ${adaptor}/address)"
1031 mac="$(echo ${mac} | sed 's/:/-/g' | tr '[a-z]' '[A-Z]')"
1033 done
1035 echo ${mac}
1038 is_luks_partition ()
1040 device="${1}"
1041 /sbin/cryptsetup isLuks "${device}" 1>/dev/null 2>&1
1044 is_active_luks_mapping ()
1046 device="${1}"
1047 /sbin/cryptsetup status "${device}" 1>/dev/null 2>&1
1050 get_luks_backing_device ()
1052 device=${1}
1053 cryptsetup status ${device} 2> /dev/null | \
1054 awk '{if ($1 == "device:") print $2}'
1057 removable_dev ()
1059 output_format="${1}"
1060 want_usb="${2}"
1061 ret=
1063 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1065 dev_ok=
1066 if [ "$(cat ${sysblock}/removable)" = "1" ]
1067 then
1068 if [ -z "${want_usb}" ]
1069 then
1070 dev_ok="true"
1071 else
1072 if readlink ${sysblock} | grep -q usb
1073 then
1074 dev_ok="true"
1079 if [ "${dev_ok}" = "true" ]
1080 then
1081 case "${output_format}" in
1082 sys)
1083 ret="${ret} ${sysblock}"
1086 devname=$(sys2dev "${sysblock}")
1087 ret="${ret} ${devname}"
1089 esac
1091 done
1093 echo "${ret}"
1096 removable_usb_dev ()
1098 output_format="${1}"
1100 removable_dev "${output_format}" "want_usb"
1103 non_removable_dev ()
1105 output_format="${1}"
1106 ret=
1108 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1110 if [ "$(cat ${sysblock}/removable)" = "0" ]
1111 then
1112 case "${output_format}" in
1113 sys)
1114 ret="${ret} ${sysblock}"
1117 devname=$(sys2dev "${sysblock}")
1118 ret="${ret} ${devname}"
1120 esac
1122 done
1124 echo "${ret}"
1127 link_files ()
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)).
1134 local src_dir dest_dir src_transform
1136 # remove multiple /:s and ensure ending on /
1137 src_dir="$(trim_path ${1})/"
1138 dest_dir="$(trim_path ${2})/"
1139 src_transform="${3}"
1141 # This check can only trigger on the inital, non-recursive call since
1142 # we create the destination before recursive calls
1143 if [ ! -d "${dest_dir}" ]
1144 then
1145 log_warning_msg "Must link_files into a directory"
1146 return
1149 find "${src_dir}" -mindepth 1 -maxdepth 1 | \
1150 while read src
1152 local dest final_src
1153 dest="${dest_dir}$(basename "${src}")"
1154 if [ -d "${src}" ]
1155 then
1156 if [ -z "$(ls -A "${src}")" ]
1157 then
1158 continue
1160 if [ ! -d "${dest}" ]
1161 then
1162 mkdir -p "${dest}"
1163 chown_ref "${src}" "${dest}"
1164 chmod_ref "${src}" "${dest}"
1166 link_files "${src}" "${dest}" "${src_transform}"
1167 else
1168 final_src=${src}
1169 if [ -n "${src_transform}" ]
1170 then
1171 final_src="$(echo ${final_src} | sed "${src_transform}")"
1173 rm -rf "${dest}" 2> /dev/null
1174 ln -s "${final_src}" "${dest}"
1175 chown_ref "${src}" "${dest}"
1177 done
1180 do_union ()
1182 local unionmountpoint unionrw unionro
1183 unionmountpoint="${1}" # directory where the union is mounted
1184 shift
1185 unionrw="${1}" # branch where the union changes are stored
1186 shift
1187 unionro="${*}" # space separated list of read-only branches (optional)
1189 case "${UNIONTYPE}" in
1190 aufs)
1191 rw_opt="rw"
1192 ro_opt="rr+wh"
1193 noxino_opt="noxino"
1196 unionfs-fuse)
1197 rw_opt="RW"
1198 ro_opt="RO"
1202 rw_opt="rw"
1203 ro_opt="ro"
1205 esac
1207 case "${UNIONTYPE}" in
1208 unionfs-fuse)
1209 unionmountopts="-o cow -o noinitgroups -o default_permissions -o allow_other -o use_ino -o suid"
1210 unionmountopts="${unionmountopts} ${unionrw}=${rw_opt}"
1211 if [ -n "${unionro}" ]
1212 then
1213 for rofs in ${unionro}
1215 unionmountopts="${unionmountopts}:${rofs}=${ro_opt}"
1216 done
1218 ( sysctl -w fs.file-max=391524 ; ulimit -HSn 16384
1219 unionfs-fuse ${unionmountopts} "${unionmountpoint}" ) && \
1220 ( mkdir -p /run/sendsigs.omit.d
1221 pidof unionfs-fuse >> /run/sendsigs.omit.d/unionfs-fuse || true )
1224 overlayfs)
1225 # XXX: can multiple unionro be used? (overlayfs only handles two dirs, but perhaps they can be chained?)
1226 # XXX: and can unionro be optional? i.e. can overlayfs skip lowerdir?
1227 if echo ${unionro} | grep -q " "
1228 then
1229 panic "Multiple lower filesystems are currently not supported with overlayfs (unionro = ${unionro})."
1230 elif [ -z "${unionro}" ]
1231 then
1232 panic "Overlayfs needs at least one lower filesystem (read-only branch)."
1234 unionmountopts="-o noatime,lowerdir=${unionro},upperdir=${unionrw}"
1235 mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
1239 unionmountopts="-o noatime,${noxino_opt},dirs=${unionrw}=${rw_opt}"
1240 if [ -n "${unionro}" ]
1241 then
1242 for rofs in ${unionro}
1244 unionmountopts="${unionmountopts}:${rofs}=${ro_opt}"
1245 done
1247 mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
1249 esac
1252 get_custom_mounts ()
1254 # Side-effect: leaves $devices with persistence.conf mounted in /live/persistence
1255 # Side-effect: prints info to file $custom_mounts
1257 local custom_mounts devices bindings links
1258 custom_mounts=${1}
1259 shift
1260 devices=${@}
1262 bindings="/tmp/bindings.list"
1263 links="/tmp/links.list"
1264 rm -rf ${bindings} ${links} 2> /dev/null
1266 for device in ${devices}
1268 if [ ! -b "${device}" ]
1269 then
1270 continue
1273 local device_name backing include_list
1274 device_name="$(basename ${device})"
1275 backing=$(mount_persistence_media ${device})
1276 if [ -z "${backing}" ]
1277 then
1278 continue
1281 if [ -r "${backing}/${persistence_list}" ]
1282 then
1283 include_list="${backing}/${persistence_list}"
1284 else
1285 continue
1288 if [ -n "${LIVE_BOOT_DEBUG}" ] && [ -e "${include_list}" ]
1289 then
1290 cp ${include_list} /live/persistence/${persistence_list}.${device_name}
1293 while read dir options # < ${include_list}
1295 if echo ${dir} | grep -qe "^[[:space:]]*\(#.*\)\?$"
1296 then
1297 # skipping empty or commented lines
1298 continue
1301 if trim_path ${dir} | grep -q -e "^[^/]" -e "^/lib" -e "^/lib/live\(/.*\)\?$" -e "^/\(.*/\)\?\.\.\?\(/.*\)\?$"
1302 then
1303 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."
1304 continue
1307 local opt_source opt_link source full_source full_dest
1308 opt_source=""
1309 opt_link=""
1310 for opt in $(echo ${options} | tr ',' ' ');
1312 case "${opt}" in
1313 source=*)
1314 opt_source=${opt#source=}
1316 link)
1317 opt_link="true"
1319 union|bind)
1322 log_warning_msg "Skipping custom mount with unkown option: ${opt}"
1323 continue 2
1325 esac
1326 done
1328 source="${dir}"
1329 if [ -n "${opt_source}" ]
1330 then
1331 if echo ${opt_source} | grep -q -e "^/" -e "^\(.*/\)\?\.\.\?\(/.*\)\?$" && [ "${opt_source}" != "." ]
1332 then
1333 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"
1334 continue
1335 else
1336 source="${opt_source}"
1340 full_source="$(trim_path ${backing}/${source})"
1341 full_dest="$(trim_path ${rootmnt}/${dir})"
1342 if [ -n "${opt_link}" ]
1343 then
1344 echo "${device} ${full_source} ${full_dest} ${options}" >> ${links}
1345 else
1346 echo "${device} ${full_source} ${full_dest} ${options}" >> ${bindings}
1348 done < ${include_list}
1349 done
1351 # We sort the list according to destination so we're sure that
1352 # we won't hide a previous mount. We also ignore duplicate
1353 # destinations in a more or less arbitrary way.
1354 [ -e "${bindings}" ] && sort -k3 -sbu ${bindings} >> ${custom_mounts} && rm ${bindings}
1356 # After all mounts are considered we add symlinks so they
1357 # won't be hidden by some mount.
1358 [ -e "${links}" ] && cat ${links} >> ${custom_mounts} && rm ${links}
1360 # We need to make sure that no two custom mounts have the same sources
1361 # or are nested; if that is the case, too much weird stuff can happen.
1362 local prev_source prev_dest
1363 prev_source="impossible source" # first iteration must not match
1364 prev_dest=""
1365 # This sort will ensure that a source /a comes right before a source
1366 # /a/b so we only need to look at the previous source
1367 sort -k2 -b ${custom_mounts} |
1368 while read device source dest options
1370 if echo ${source} | grep -qe "^${prev_source}\(/.*\)\?$"
1371 then
1372 panic "Two persistence mounts have the same or nested sources: ${source} on ${dest}, and ${prev_source} on ${prev_dest}"
1374 prev_source=${source}
1375 prev_dest=${dest}
1376 done
1379 activate_custom_mounts ()
1381 local custom_mounts used_devices
1382 custom_mounts="${1}" # the ouput from get_custom_mounts()
1383 used_devices=""
1385 while read device source dest options # < ${custom_mounts}
1387 local opt_bind opt_link opt_union
1388 opt_bind="true"
1389 opt_link=""
1390 opt_union=""
1391 for opt in $(echo ${options} | tr ',' ' ');
1393 case "${opt}" in
1394 bind)
1395 opt_bind="true"
1396 unset opt_link opt_union
1398 link)
1399 opt_link="true"
1400 unset opt_bind opt_union
1402 union)
1403 opt_union="true"
1404 unset opt_bind opt_link
1406 esac
1407 done
1409 if [ -n "$(what_is_mounted_on "${dest}")" ]
1410 then
1411 if [ "${dest}" = "${rootmnt}" ]
1412 then
1413 umount "${dest}"
1414 else
1415 log_warning_msg "Skipping custom mount ${dest}: $(what_is_mounted_on "${dest}") is already mounted there"
1416 continue
1420 if [ ! -d "${dest}" ]
1421 then
1422 # create the destination and delete existing files in
1423 # its path that are in the way
1424 path="/"
1425 for dir in $(echo ${dest} | sed -e 's|/\+| |g')
1427 path=$(trim_path ${path}/${dir})
1428 if [ -f ${path} ]
1429 then
1430 rm -f ${path}
1432 if [ ! -e ${path} ]
1433 then
1434 mkdir -p ${path}
1435 if echo ${path} | grep -qe "^${rootmnt}/*home/[^/]\+"
1436 then
1437 # if ${dest} is in /home try fixing proper ownership by assuming that the intended user is the first, which is usually the case
1438 # FIXME: this should really be handled by live-config since we don't know for sure which uid a certain user has until then
1439 chown 1000:1000 ${path}
1442 done
1445 # if ${source} doesn't exist on our persistence media
1446 # we bootstrap it with $dest from the live filesystem.
1447 # this both makes sense and is critical if we're
1448 # dealing with /etc or other system dir.
1449 if [ ! -d "${source}" ]
1450 then
1451 if [ -n "${PERSISTENCE_READONLY}" ]
1452 then
1453 continue
1454 elif [ -n "${opt_union}" ] || [ -n "${opt_link}" ]
1455 then
1456 # unions and don't need to be bootstrapped
1457 # link dirs can't be bootstrapped in a sensible way
1458 mkdir -p "${source}"
1459 chown_ref "${dest}" "${source}"
1460 chmod_ref "${dest}" "${source}"
1461 elif [ -n "${opt_bind}" ]
1462 then
1463 # ensure that $dest is not copied *into* $source
1464 mkdir -p "$(dirname ${source})"
1465 cp -a "${dest}" "${source}"
1469 # XXX: If CONFIG_AUFS_ROBR is added to the Debian kernel we can
1470 # ignore the loop below and set rootfs_dest_backing=$dest
1471 local rootfs_dest_backing
1472 rootfs_dest_backing=""
1473 if [ -n "${opt_link}" ] || [ -n "${opt_union}" ]
1474 then
1475 for d in /live/rootfs/*
1477 if [ -n "${rootmnt}" ]
1478 then
1479 fs="${d}/$(echo ${dest} | sed -e "s|${rootmnt}||")"
1480 else
1481 fs="${d}/${dest}"
1483 if [ -d "${fs}" ]
1484 then
1485 rootfs_dest_backing="${rootfs_dest_backing} ${fs}"
1487 done
1490 local cow_dir links_source
1491 if [ -n "${opt_link}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1492 then
1493 link_files ${source} ${dest} "s|^/live/|/lib/live/mount/|"
1494 elif [ -n "${opt_link}" ] && [ -n "${PERSISTENCE_READONLY}" ]
1495 then
1496 mkdir -p ${rootmnt}/lib/live/mount/persistence
1497 links_source=$(mktemp -d ${rootmnt}/lib/live/mount/persistence/links-source-XXXXXX)
1498 chown_ref ${source} ${links_source}
1499 chmod_ref ${source} ${links_source}
1500 # We put the cow dir in the below strange place to
1501 # make it absolutely certain that the link source
1502 # has its own directory and isn't nested with some
1503 # other custom mount (if so that mount's files would
1504 # be linked, causing breakage.
1505 cow_dir="/live/overlay/lib/live/mount/persistence/$(basename ${links_source})"
1506 mkdir -p ${cow_dir}
1507 chown_ref "${source}" "${cow_dir}"
1508 chmod_ref "${source}" "${cow_dir}"
1509 do_union ${links_source} ${cow_dir} ${source} ${rootfs_dest_backing}
1510 link_files ${links_source} ${dest} "s|^${rootmnt}||"
1511 elif [ -n "${opt_union}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1512 then
1513 do_union ${dest} ${source} ${rootfs_dest_backing}
1514 elif [ -n "${opt_bind}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1515 then
1516 mount --bind "${source}" "${dest}"
1517 elif [ -n "${opt_bind}" -o -n "${opt_union}" ] && [ -n "${PERSISTENCE_READONLY}" ]
1518 then
1519 # bind-mount and union mount are handled the same
1520 # in read-only mode, but note that rootfs_dest_backing
1521 # is non-empty (and necessary) only for unions
1522 cow_dir="/live/overlay/${dest}"
1523 if [ -e "${cow_dir}" ] && [ -z "${opt_link}" ]
1524 then
1525 # If an earlier custom mount has files here
1526 # it will "block" the current mount's files
1527 # which is undesirable
1528 rm -rf "${cow_dir}"
1530 mkdir -p ${cow_dir}
1531 chown_ref "${source}" "${cow_dir}"
1532 chmod_ref "${source}" "${cow_dir}"
1533 do_union ${dest} ${cow_dir} ${source} ${rootfs_dest_backing}
1536 PERSISTENCE_IS_ON="1"
1537 export PERSISTENCE_IS_ON
1539 if echo ${used_devices} | grep -qve "^\(.* \)\?${device}\( .*\)\?$"
1540 then
1541 used_devices="${used_devices} ${device}"
1543 done < ${custom_mounts}
1545 echo ${used_devices}
1548 is_mountpoint ()
1550 directory="$1"
1552 [ $(stat -fc%d:%D "${directory}") != $(stat -fc%d:%D "${directory}/..") ]