Fixing invalid syntax in test for opt_link.
[debian-live-boot.git] / scripts / boot / 9990-misc-helpers.sh
blobf2814ecd80447d46a6800d697e9a7954b494ebc2
1 #!/bin/sh
3 #set -e
5 is_live_path ()
7 DIRECTORY="${1}"
9 if [ -d "${DIRECTORY}"/"${LIVE_MEDIA_PATH}" ]
10 then
11 for FILESYSTEM in squashfs ext2 ext3 ext4 xfs dir jffs2
13 if [ "$(echo ${DIRECTORY}/${LIVE_MEDIA_PATH}/*.${FILESYSTEM})" != "${DIRECTORY}/${LIVE_MEDIA_PATH}/*.${FILESYSTEM}" ]
14 then
15 return 0
17 done
20 return 1
23 matches_uuid ()
25 if [ "${IGNORE_UUID}" ] || [ ! -e /conf/uuid.conf ]
26 then
27 return 0
30 path="${1}"
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}" ]
40 then
41 return 0
43 done
45 return 1
48 get_backing_device ()
50 case "${1}" in
51 *.squashfs|*.ext2|*.ext3|*.ext4|*.jffs2)
52 echo $(setup_loop "${1}" "loop" "/sys/block/loop*" '0' "${LIVE_MEDIA_ENCRYPTION}" "${2}")
55 *.dir)
56 echo "directory"
60 panic "Unrecognized live filesystem: ${1}"
62 esac
65 match_files_in_dir ()
67 # Does any files match pattern ${1} ?
68 local pattern
69 pattern="${1}"
71 if [ "$(echo ${pattern})" != "${pattern}" ]
72 then
73 return 0
76 return 1
79 mount_images_in_directory ()
81 directory="${1}"
82 rootmnt="${2}"
83 mac="${3}"
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"
91 then
92 [ -n "${mac}" ] && adddirectory="${directory}/${LIVE_MEDIA_PATH}/${mac}"
93 setup_unionfs "${directory}/${LIVE_MEDIA_PATH}" "${rootmnt}" "${adddirectory}"
94 else
95 panic "No supported filesystem images found at /${LIVE_MEDIA_PATH}."
99 is_nice_device ()
101 sysfs_path="${1#/sys}"
103 if /sbin/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)"
104 then
105 return 0
106 elif echo "${sysfs_path}" | grep -q '^/block/vd[a-z]$'
107 then
108 return 0
109 elif echo ${sysfs_path} | grep -q "^/block/dm-"
110 then
111 return 0
112 elif echo ${sysfs_path} | grep -q "^/block/mtdblock"
113 then
114 return 0
117 return 1
120 check_dev ()
122 sysdev="${1}"
123 devname="${2}"
124 skip_uuid_check="${3}"
126 # support for fromiso=.../isofrom=....
127 if [ -n "$FROMISO" ]
128 then
129 ISO_DEVICE=$(dirname $FROMISO)
130 if ! [ -b $ISO_DEVICE ]
131 then
132 # to support unusual device names like /dev/cciss/c0d0p1
133 # as well we have to identify the block device name, let's
134 # do that for up to 15 levels
135 i=15
136 while [ -n "$ISO_DEVICE" ] && [ "$i" -gt 0 ]
138 ISO_DEVICE=$(dirname ${ISO_DEVICE})
139 [ -b "$ISO_DEVICE" ] && break
140 i=$(($i -1))
141 done
144 if [ "$ISO_DEVICE" = "/" ]
145 then
146 echo "Warning: device for bootoption fromiso= ($FROMISO) not found.">>/boot.log
147 else
148 fs_type=$(get_fstype "${ISO_DEVICE}")
149 if is_supported_fs ${fs_type}
150 then
151 mkdir /live/fromiso
152 mount -t $fs_type "$ISO_DEVICE" /live/fromiso
153 ISO_NAME="$(echo $FROMISO | sed "s|$ISO_DEVICE||")"
154 loopdevname=$(setup_loop "/live/fromiso/${ISO_NAME}" "loop" "/sys/block/loop*" "" '')
155 devname="${loopdevname}"
156 else
157 echo "Warning: unable to mount $ISO_DEVICE." >>/boot.log
162 if [ -z "${devname}" ]
163 then
164 devname=$(sys2dev "${sysdev}")
167 if [ -d "${devname}" ]
168 then
169 mount -o bind "${devname}" $mountpoint || continue
171 if is_live_path $mountpoint
172 then
173 echo $mountpoint
174 return 0
175 else
176 umount $mountpoint
180 IFS=","
181 for device in ${devname}
183 case "$device" in
184 *mapper*)
185 # Adding lvm support
186 if [ -x /scripts/local-top/lvm2 ]
187 then
188 ROOT="$device" resume="" /scripts/local-top/lvm2
192 /dev/md*)
193 # Adding raid support
194 if [ -x /scripts/local-top/mdadm ]
195 then
196 cp /conf/conf.d/md /conf/conf.d/md.orig
197 echo "MD_DEVS=$device " >> /conf/conf.d/md
198 /scripts/local-top/mdadm
199 mv /conf/conf.d/md.orig /conf/conf.d/md
202 esac
203 done
204 unset IFS
206 [ -n "$device" ] && devname="$device"
208 [ -e "$devname" ] || continue
210 if [ -n "${LIVE_MEDIA_OFFSET}" ]
211 then
212 loopdevname=$(setup_loop "${devname}" "loop" "/sys/block/loop*" "${LIVE_MEDIA_OFFSET}" '')
213 devname="${loopdevname}"
216 fstype=$(get_fstype "${devname}")
218 if is_supported_fs ${fstype}
219 then
220 devuid=$(blkid -o value -s UUID "$devname")
221 [ -n "$devuid" ] && grep -qs "\<$devuid\>" $tried && continue
222 mount -t ${fstype} -o ro,noatime "${devname}" ${mountpoint} || continue
223 [ -n "$devuid" ] && echo "$devuid" >> $tried
225 if [ -n "${FINDISO}" ]
226 then
227 if [ -f ${mountpoint}/${FINDISO} ]
228 then
229 umount ${mountpoint}
230 mkdir -p /live/findiso
231 mount -t ${fstype} -o ro,noatime "${devname}" /live/findiso
232 loopdevname=$(setup_loop "/live/findiso/${FINDISO}" "loop" "/sys/block/loop*" 0 "")
233 devname="${loopdevname}"
234 mount -t iso9660 -o ro,noatime "${devname}" ${mountpoint}
235 else
236 umount ${mountpoint}
240 if is_live_path ${mountpoint} && \
241 ([ "${skip_uuid_check}" ] || matches_uuid ${mountpoint})
242 then
243 echo ${mountpoint}
244 return 0
245 else
246 umount ${mountpoint} 2>/dev/null
250 if [ -n "${LIVE_MEDIA_OFFSET}" ]
251 then
252 losetup -d "${loopdevname}"
255 return 1
258 find_livefs ()
260 timeout="${1}"
262 # don't start autodetection before timeout has expired
263 if [ -n "${LIVE_MEDIA_TIMEOUT}" ]
264 then
265 if [ "${timeout}" -lt "${LIVE_MEDIA_TIMEOUT}" ]
266 then
267 return 1
271 # first look at the one specified in the command line
272 case "${LIVE_MEDIA}" in
273 removable-usb)
274 for sysblock in $(removable_usb_dev "sys")
276 for dev in $(subdevices "${sysblock}")
278 if check_dev "${dev}"
279 then
280 return 0
282 done
283 done
284 return 1
287 removable)
288 for sysblock in $(removable_dev "sys")
290 for dev in $(subdevices "${sysblock}")
292 if check_dev "${dev}"
293 then
294 return 0
296 done
297 done
298 return 1
302 if [ ! -z "${LIVE_MEDIA}" ]
303 then
304 if check_dev "null" "${LIVE_MEDIA}" "skip_uuid_check"
305 then
306 return 0
310 esac
312 # or do the scan of block devices
313 # prefer removable devices over non-removable devices, so scan them first
314 devices_to_scan="$(removable_dev 'sys') $(non_removable_dev 'sys')"
316 for sysblock in $devices_to_scan
318 devname=$(sys2dev "${sysblock}")
319 [ -e "$devname" ] || continue
320 fstype=$(get_fstype "${devname}")
322 if /lib/udev/cdrom_id ${devname} > /dev/null
323 then
324 if check_dev "null" "${devname}"
325 then
326 return 0
328 elif is_nice_device "${sysblock}"
329 then
330 for dev in $(subdevices "${sysblock}")
332 if check_dev "${dev}"
333 then
334 return 0
336 done
337 elif [ "${fstype}" = "squashfs" -o \
338 "${fstype}" = "btrfs" -o \
339 "${fstype}" = "ext2" -o \
340 "${fstype}" = "ext3" -o \
341 "${fstype}" = "ext4" -o \
342 "${fstype}" = "jffs2" ]
343 then
344 # This is an ugly hack situation, the block device has
345 # an image directly on it. It's hopefully
346 # live-boot, so take it and run with it.
347 ln -s "${devname}" "${devname}.${fstype}"
348 echo "${devname}.${fstype}"
349 return 0
351 done
353 return 1
356 really_export ()
358 STRING="${1}"
359 VALUE="$(eval echo -n \${$STRING})"
361 if [ -f /live.vars ] && grep -sq "export ${STRING}" /live.vars
362 then
363 sed -i -e 's/\('${STRING}'=\).*$/\1'${VALUE}'/' /live.vars
364 else
365 echo "export ${STRING}=\"${VALUE}\"" >> /live.vars
368 eval export "${STRING}"="${VALUE}"
371 is_in_list_separator_helper ()
373 local sep element list
374 sep=${1}
375 shift
376 element=${1}
377 shift
378 list=${*}
379 echo ${list} | grep -qe "^\(.*${sep}\)\?${element}\(${sep}.*\)\?$"
382 is_in_space_sep_list ()
384 local element
385 element=${1}
386 shift
387 is_in_list_separator_helper "[[:space:]]" "${element}" "${*}"
390 is_in_comma_sep_list ()
392 local element
393 element=${1}
394 shift
395 is_in_list_separator_helper "," "${element}" "${*}"
398 sys2dev ()
400 sysdev=${1#/sys}
401 echo "/dev/$($udevinfo -q name -p ${sysdev} 2>/dev/null|| echo ${sysdev##*/})"
404 subdevices ()
406 sysblock=${1}
407 r=""
409 for dev in "${sysblock}"/* "${sysblock}"
411 if [ -e "${dev}/dev" ]
412 then
413 r="${r} ${dev}"
415 done
417 echo ${r}
420 storage_devices()
422 black_listed_devices="${1}"
423 white_listed_devices="${2}"
425 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "loop|ram|fd")
427 fulldevname=$(sys2dev "${sysblock}")
429 if is_in_space_sep_list ${fulldevname} ${black_listed_devices} || \
430 [ -n "${white_listed_devices}" ] && \
431 ! is_in_space_sep_list ${fulldevname} ${white_listed_devices}
432 then
433 # skip this device entirely
434 continue
437 for dev in $(subdevices "${sysblock}")
439 devname=$(sys2dev "${dev}")
441 if is_in_space_sep_list ${devname} ${black_listed_devices}
442 then
443 # skip this subdevice
444 continue
445 else
446 echo "${devname}"
448 done
449 done
452 is_supported_fs ()
454 fstype="${1}"
456 # Validate input first
457 if [ -z "${fstype}" ]
458 then
459 return 1
462 # Try to look if it is already supported by the kernel
463 if grep -q ${fstype} /proc/filesystems
464 then
465 return 0
466 else
467 # Then try to add support for it the gentle way using the initramfs capabilities
468 modprobe ${fstype}
469 if grep -q ${fstype} /proc/filesystems
470 then
471 return 0
472 # Then try the hard way if /root is already reachable
473 else
474 kmodule="/root/lib/modules/`uname -r`/${fstype}/${fstype}.ko"
475 if [ -e "${kmodule}" ]
476 then
477 insmod "${kmodule}"
478 if grep -q ${fstype} /proc/filesystems
479 then
480 return 0
486 return 1
489 get_fstype ()
491 /sbin/blkid -s TYPE -o value $1 2>/dev/null
494 where_is_mounted ()
496 device=${1}
497 # return first found
498 grep -m1 "^${device} " /proc/mounts | cut -f2 -d ' '
501 trim_path ()
503 # remove all unnecessary /:s in the path, including last one (except
504 # if path is just "/")
505 echo ${1} | sed 's|//\+|/|g' | sed 's|^\(.*[^/]\)/$|\1|'
508 what_is_mounted_on ()
510 local dir
511 dir="$(trim_path ${1})"
512 grep -m1 "^[^ ]\+ ${dir} " /proc/mounts | cut -d' ' -f1
515 chown_ref ()
517 local reference targets owner
518 reference="${1}"
519 shift
520 targets=${@}
521 owner=$(stat -c %u:%g "${reference}")
522 chown -h ${owner} ${targets}
525 chmod_ref ()
527 local reference targets rights
528 reference="${1}"
529 shift
530 targets=${@}
531 rights=$(stat -c %a "${reference}")
532 chmod ${rights} ${targets}
535 lastline ()
537 while read lines
539 line=${lines}
540 done
542 echo "${line}"
545 base_path ()
547 testpath="${1}"
548 mounts="$(awk '{print $2}' /proc/mounts)"
549 testpath="$(busybox realpath ${testpath})"
551 while true
553 if echo "${mounts}" | grep -qs "^${testpath}"
554 then
555 set -- $(echo "${mounts}" | grep "^${testpath}" | lastline)
556 echo ${1}
557 break
558 else
559 testpath=$(dirname $testpath)
561 done
564 fs_size ()
566 # Returns used/free fs kbytes + 5% more
567 # You could pass a block device as ${1} or the mount point as ${2}
569 dev="${1}"
570 mountp="${2}"
571 used="${3}"
573 if [ -z "${mountp}" ]
574 then
575 mountp="$(where_is_mounted ${dev})"
577 if [ -z "${mountp}" ]
578 then
579 mountp="/mnt/tmp_fs_size"
581 mkdir -p "${mountp}"
582 mount -t $(get_fstype "${dev}") -o ro "${dev}" "${mountp}" || log_warning_msg "cannot mount -t $(get_fstype ${dev}) -o ro ${dev} ${mountp}"
584 doumount=1
588 if [ "${used}" = "used" ]
589 then
590 size=$(du -ks ${mountp} | cut -f1)
591 size=$(expr ${size} + ${size} / 20 ) # FIXME: 5% more to be sure
592 else
593 # free space
594 size="$(df -kP | grep -s ${mountp} | awk '{print $4}')"
597 if [ -n "${doumount}" ]
598 then
599 umount "${mountp}" || log_warning_msg "cannot umount ${mountp}"
600 rmdir "${mountp}"
603 echo "${size}"
606 load_keymap ()
608 # Load custom keymap
609 if [ -x /bin/loadkeys -a -r /etc/boottime.kmap.gz ]
610 then
611 loadkeys --quiet /etc/boottime.kmap.gz
615 setup_loop ()
617 local fspath module pattern offset encryption readonly
618 fspath=${1}
619 module=${2}
620 pattern=${3}
621 offset=${4}
622 encryption=${5}
623 readonly=${6}
625 # the output of setup_loop is evaluated in other functions,
626 # modprobe leaks kernel options like "libata.dma=0"
627 # as "options libata dma=0" on stdout, causing serious
628 # problems therefor, so instead always avoid output to stdout
629 modprobe -q -b "${module}" 1>/dev/null
631 udevadm settle
633 for loopdev in ${pattern}
635 if [ "$(cat ${loopdev}/size)" -eq 0 ]
636 then
637 dev=$(sys2dev "${loopdev}")
638 options=''
640 if [ -n "${readonly}" ]
641 then
642 if losetup --help 2>&1 | grep -q -- "-r\b"
643 then
644 options="${options} -r"
648 if [ -n "${offset}" ] && [ 0 -lt "${offset}" ]
649 then
650 options="${options} -o ${offset}"
653 if [ -z "${encryption}" ]
654 then
655 losetup ${options} "${dev}" "${fspath}"
656 else
657 # Loop AES encryption
658 while true
660 load_keymap
662 echo -n "Enter passphrase for root filesystem: " >&6
663 read -s passphrase
664 echo "${passphrase}" > /tmp/passphrase
665 unset passphrase
666 exec 9</tmp/passphrase
667 /sbin/losetup ${options} -e "${encryption}" -p 9 "${dev}" "${fspath}"
668 error=${?}
669 exec 9<&-
670 rm -f /tmp/passphrase
672 if [ 0 -eq ${error} ]
673 then
674 unset error
675 break
678 echo
679 echo -n "There was an error decrypting the root filesystem ... Retry? [Y/n] " >&6
680 read answer
682 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
683 then
684 unset answer
685 break
687 done
690 echo "${dev}"
691 return 0
693 done
695 panic "No loop devices available"
698 try_mount ()
700 dev="${1}"
701 mountp="${2}"
702 opts="${3}"
703 fstype="${4}"
705 old_mountp="$(where_is_mounted ${dev})"
707 if [ -n "${old_mountp}" ]
708 then
709 if [ "${opts}" != "ro" ]
710 then
711 mount -o remount,"${opts}" "${dev}" "${old_mountp}" || panic "Remounting ${dev} ${opts} on ${old_mountp} failed"
714 mount -o bind "${old_mountp}" "${mountp}" || panic "Cannot bind-mount ${old_mountp} on ${mountp}"
715 else
716 if [ -z "${fstype}" ]
717 then
718 fstype=$(get_fstype "${dev}")
720 mount -t "${fstype}" -o "${opts}" "${dev}" "${mountp}" || \
721 ( echo "SKIPPING: Cannot mount ${dev} on ${mountp}, fstype=${fstype}, options=${opts}" > boot.log && return 0 )
725 # Try to mount $device to the place expected by live-boot. If $device
726 # is already mounted somewhere, move it to the expected place. If
727 # we're only probing $device (to check if it has custom persistence)
728 # $probe should be set, which suppresses warnings upon failure. On
729 # success, print the mount point for $device.
730 mount_persistence_media ()
732 local device probe backing old_backing fstype mount_opts
733 device=${1}
734 probe=${2}
736 backing="/live/persistence/$(basename ${device})"
738 mkdir -p "${backing}"
739 old_backing="$(where_is_mounted ${device})"
740 if [ -z "${old_backing}" ]
741 then
742 fstype="$(get_fstype ${device})"
743 mount_opts="rw,noatime"
744 if [ -n "${PERSISTENCE_READONLY}" ]
745 then
746 mount_opts="ro,noatime"
748 if mount -t "${fstype}" -o "${mount_opts}" "${device}" "${backing}" >/dev/null
749 then
750 echo ${backing}
751 return 0
752 else
753 [ -z "${probe}" ] && log_warning_msg "Failed to mount persistence media ${device}"
754 rmdir "${backing}"
755 return 1
757 elif [ "${backing}" != "${old_backing}" ]
758 then
759 if mount --move ${old_backing} ${backing} >/dev/null
760 then
761 echo ${backing}
762 return 0
763 else
764 [ -z "${probe}" ] && log_warning_msg "Failed to move persistence media ${device}"
765 rmdir "${backing}"
766 return 1
768 else
769 # This means that $device has already been mounted on
770 # the place expected by live-boot, so we're done.
771 echo ${backing}
772 return 0
776 close_persistence_media ()
778 local device backing
779 device=${1}
780 backing="$(where_is_mounted ${device})"
782 if [ -d "${backing}" ]
783 then
784 umount "${backing}" >/dev/null 2>&1
785 rmdir "${backing}" >/dev/null 2>&1
788 if is_active_luks_mapping ${device}
789 then
790 /sbin/cryptsetup luksClose ${device}
794 open_luks_device ()
796 dev="${1}"
797 name="$(basename ${dev})"
798 opts="--key-file=-"
799 if [ -n "${PERSISTENCE_READONLY}" ]
800 then
801 opts="${opts} --readonly"
804 if /sbin/cryptsetup status "${name}" >/dev/null 2>&1
805 then
806 re="^[[:space:]]*device:[[:space:]]*\([^[:space:]]*\)$"
807 opened_dev=$(cryptsetup status ${name} 2>/dev/null | grep "${re}" | sed "s|${re}|\1|")
808 if [ "${opened_dev}" = "${dev}" ]
809 then
810 luks_device="/dev/mapper/${name}"
811 echo ${luks_device}
812 return 0
813 else
814 log_warning_msg "Cannot open luks device ${dev} since ${opened_dev} already is opened with its name"
815 return 1
819 load_keymap
821 while true
823 /lib/cryptsetup/askpass "Enter passphrase for ${dev}: " | \
824 /sbin/cryptsetup -T 1 luksOpen ${dev} ${name} ${opts}
826 if [ 0 -eq ${?} ]
827 then
828 luks_device="/dev/mapper/${name}"
829 echo ${luks_device}
830 return 0
833 echo >&6
834 echo -n "There was an error decrypting ${dev} ... Retry? [Y/n] " >&6
835 read answer
837 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
838 then
839 return 2
841 done
844 get_gpt_name ()
846 local dev
847 dev="${1}"
848 /sbin/blkid -s PART_ENTRY_NAME -p -o value ${dev} 2>/dev/null
851 is_gpt_device ()
853 local dev
854 dev="${1}"
855 [ "$(/sbin/blkid -s PART_ENTRY_SCHEME -p -o value ${dev} 2>/dev/null)" = "gpt" ]
858 probe_for_gpt_name ()
860 local overlays dev gpt_dev gpt_name
861 overlays="${1}"
862 dev="${2}"
864 gpt_dev="${dev}"
865 if is_active_luks_mapping ${dev}
866 then
867 # if $dev is an opened luks device, we need to check
868 # GPT stuff on the backing device
869 gpt_dev=$(get_luks_backing_device "${dev}")
872 if ! is_gpt_device ${gpt_dev}
873 then
874 return
877 gpt_name=$(get_gpt_name ${gpt_dev})
878 for label in ${overlays}
880 if [ "${gpt_name}" = "${label}" ]
881 then
882 echo "${label}=${dev}"
884 done
887 probe_for_fs_label ()
889 local overlays dev
890 overlays="${1}"
891 dev="${2}"
893 for label in ${overlays}
895 if [ "$(/sbin/blkid -s LABEL -o value $dev 2>/dev/null)" = "${label}" ]
896 then
897 echo "${label}=${dev}"
899 done
902 probe_for_file_name ()
904 local overlays dev ret backing
905 overlays="${1}"
906 dev="${2}"
908 ret=""
909 backing="$(mount_persistence_media ${dev} probe)"
910 if [ -z "${backing}" ]
911 then
912 return
915 for label in ${overlays}
917 path=${backing}/${PERSISTENCE_PATH}${label}
918 if [ -f "${path}" ]
919 then
920 local loopdev
921 loopdev=$(setup_loop "${path}" "loop" "/sys/block/loop*")
922 ret="${ret} ${label}=${loopdev}"
924 done
926 if [ -n "${ret}" ]
927 then
928 echo ${ret}
929 else
930 # unmount and remove mountpoint
931 umount ${backing} > /dev/null 2>&1 || true
932 rmdir ${backing} > /dev/null 2>&1 || true
936 find_persistence_media ()
938 # Scans devices for overlays, and returns a whitespace
939 # separated list of how to use them. Only overlays with a partition
940 # label or file name in ${overlays} are returned.
942 # When scanning a LUKS device, the user will be asked to enter the
943 # passphrase; on failure to enter it, or if no persistence partitions
944 # or files were found, the LUKS device is closed.
946 # For all other cases (overlay partition and overlay file) the
947 # return value is "${label}=${device}", where ${device} a device that
948 # can mount the content. In the case of an overlay file, the device
949 # containing the file will remain mounted as a side-effect.
951 # No devices in ${black_listed_devices} will be scanned, and if
952 # ${white_list_devices} is non-empty, only devices in it will be
953 # scanned.
955 local overlays white_listed_devices ret black_listed_devices
956 overlays="${1}"
957 white_listed_devices="${2}"
958 ret=""
960 black_listed_devices="$(what_is_mounted_on /live/medium)"
962 for dev in $(storage_devices "${black_listed_devices}" "${white_listed_devices}")
964 local result luks_device
965 result=""
967 luks_device=""
968 # Check if it's a luks device; we'll have to open the device
969 # in order to probe any filesystem it contains, like we do
970 # below. activate_custom_mounts() also depends on that any luks
971 # device already has been opened.
972 if is_in_comma_sep_list luks ${PERSISTENCE_ENCRYPTION} && is_luks_partition ${dev}
973 then
974 if luks_device=$(open_luks_device "${dev}")
975 then
976 dev="${luks_device}"
977 else
978 # skip $dev since we failed/chose not to open it
979 continue
981 elif ! is_in_comma_sep_list none ${PERSISTENCE_ENCRYPTION}
982 then
983 # skip $dev since we don't allow unencrypted storage
984 continue
987 # Probe for matching GPT partition names or filesystem labels
988 if is_in_comma_sep_list filesystem ${PERSISTENCE_STORAGE}
989 then
990 result=$(probe_for_gpt_name "${overlays}" ${dev})
991 if [ -n "${result}" ]
992 then
993 ret="${ret} ${result}"
994 continue
997 result=$(probe_for_fs_label "${overlays}" ${dev})
998 if [ -n "${result}" ]
999 then
1000 ret="${ret} ${result}"
1001 continue
1005 # Probe for files with matching name on mounted partition
1006 if is_in_comma_sep_list file ${PERSISTENCE_STORAGE}
1007 then
1008 result=$(probe_for_file_name "${overlays}" ${dev})
1009 if [ -n "${result}" ]
1010 then
1011 ret="${ret} ${result}"
1012 continue
1016 # Close luks device if it isn't used
1017 if [ -z "${result}" ] && [ -n "${luks_device}" ] && is_active_luks_mapping "${luks_device}"
1018 then
1019 /sbin/cryptsetup luksClose "${luks_device}"
1021 done
1023 if [ -n "${ret}" ]
1024 then
1025 echo ${ret}
1029 get_mac ()
1031 mac=""
1033 for adaptor in /sys/class/net/*
1035 status="$(cat ${adaptor}/iflink)"
1037 if [ "${status}" -eq 2 ]
1038 then
1039 mac="$(cat ${adaptor}/address)"
1040 mac="$(echo ${mac} | sed 's/:/-/g' | tr '[a-z]' '[A-Z]')"
1042 done
1044 echo ${mac}
1047 is_luks_partition ()
1049 device="${1}"
1050 /sbin/cryptsetup isLuks "${device}" 1>/dev/null 2>&1
1053 is_active_luks_mapping ()
1055 device="${1}"
1056 /sbin/cryptsetup status "${device}" 1>/dev/null 2>&1
1059 get_luks_backing_device ()
1061 device=${1}
1062 cryptsetup status ${device} 2> /dev/null | \
1063 awk '{if ($1 == "device:") print $2}'
1066 removable_dev ()
1068 output_format="${1}"
1069 want_usb="${2}"
1070 ret=
1072 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1074 dev_ok=
1075 if [ "$(cat ${sysblock}/removable)" = "1" ]
1076 then
1077 if [ -z "${want_usb}" ]
1078 then
1079 dev_ok="true"
1080 else
1081 if readlink ${sysblock} | grep -q usb
1082 then
1083 dev_ok="true"
1088 if [ "${dev_ok}" = "true" ]
1089 then
1090 case "${output_format}" in
1091 sys)
1092 ret="${ret} ${sysblock}"
1095 devname=$(sys2dev "${sysblock}")
1096 ret="${ret} ${devname}"
1098 esac
1100 done
1102 echo "${ret}"
1105 removable_usb_dev ()
1107 output_format="${1}"
1109 removable_dev "${output_format}" "want_usb"
1112 non_removable_dev ()
1114 output_format="${1}"
1115 ret=
1117 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1119 if [ "$(cat ${sysblock}/removable)" = "0" ]
1120 then
1121 case "${output_format}" in
1122 sys)
1123 ret="${ret} ${sysblock}"
1126 devname=$(sys2dev "${sysblock}")
1127 ret="${ret} ${devname}"
1129 esac
1131 done
1133 echo "${ret}"
1136 link_files ()
1138 # create source's directory structure in dest, and recursively
1139 # create symlinks in dest to to all files in source. if mask
1140 # is non-empty, remove mask from all source paths when
1141 # creating links (will be necessary if we change root, which
1142 # live-boot normally does (into $rootmnt)).
1143 local src_dir dest_dir src_mask
1145 # remove multiple /:s and ensure ending on /
1146 src_dir="$(trim_path ${1})/"
1147 dest_dir="$(trim_path ${2})/"
1148 src_mask="${3}"
1150 # This check can only trigger on the inital, non-recursive call since
1151 # we create the destination before recursive calls
1152 if [ ! -d "${dest_dir}" ]
1153 then
1154 log_warning_msg "Must link_files into a directory"
1155 return
1158 find "${src_dir}" -mindepth 1 -maxdepth 1 | \
1159 while read src
1161 local dest final_src
1162 dest="${dest_dir}$(basename "${src}")"
1163 if [ -d "${src}" ]
1164 then
1165 if [ -z "$(ls -A "${src}")" ]
1166 then
1167 continue
1169 if [ ! -d "${dest}" ]
1170 then
1171 mkdir -p "${dest}"
1172 chown_ref "${src}" "${dest}"
1173 chmod_ref "${src}" "${dest}"
1175 link_files "${src}" "${dest}" "${src_mask}"
1176 else
1177 final_src=${src}
1178 if [ -n "${src_mask}" ]
1179 then
1180 final_src="$(echo ${final_src} | sed "s|^${src_mask}||")"
1182 rm -rf "${dest}" 2> /dev/null
1183 ln -s "${final_src}" "${dest}"
1184 chown_ref "${src}" "${dest}"
1186 done
1189 do_union ()
1191 local unionmountpoint unionrw unionro
1192 unionmountpoint="${1}" # directory where the union is mounted
1193 shift
1194 unionrw="${1}" # branch where the union changes are stored
1195 shift
1196 unionro="${*}" # space separated list of read-only branches (optional)
1198 case "${UNIONTYPE}" in
1199 aufs)
1200 rw_opt="rw"
1201 ro_opt="rr+wh"
1202 noxino_opt="noxino"
1205 unionfs-fuse)
1206 rw_opt="RW"
1207 ro_opt="RO"
1211 rw_opt="rw"
1212 ro_opt="ro"
1214 esac
1216 case "${UNIONTYPE}" in
1217 unionfs-fuse)
1218 unionmountopts="-o cow -o noinitgroups -o default_permissions -o allow_other -o use_ino -o suid"
1219 unionmountopts="${unionmountopts} ${unionrw}=${rw_opt}"
1220 if [ -n "${unionro}" ]
1221 then
1222 for rofs in ${unionro}
1224 unionmountopts="${unionmountopts}:${rofs}=${ro_opt}"
1225 done
1227 ( sysctl -w fs.file-max=391524 ; ulimit -HSn 16384
1228 unionfs-fuse ${unionmountopts} "${unionmountpoint}" ) && \
1229 ( mkdir -p /run/sendsigs.omit.d
1230 pidof unionfs-fuse >> /run/sendsigs.omit.d/unionfs-fuse || true )
1233 overlayfs)
1234 # XXX: can multiple unionro be used? (overlayfs only handles two dirs, but perhaps they can be chained?)
1235 # XXX: and can unionro be optional? i.e. can overlayfs skip lowerdir?
1236 if echo ${unionro} | grep -q " "
1237 then
1238 panic "Multiple lower filesystems are currently not supported with overlayfs (unionro = ${unionro})."
1239 elif [ -z "${unionro}" ]
1240 then
1241 panic "Overlayfs needs at least one lower filesystem (read-only branch)."
1243 unionmountopts="-o noatime,lowerdir=${unionro},upperdir=${unionrw}"
1244 mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
1248 unionmountopts="-o noatime,${noxino_opt},dirs=${unionrw}=${rw_opt}"
1249 if [ -n "${unionro}" ]
1250 then
1251 for rofs in ${unionro}
1253 unionmountopts="${unionmountopts}:${rofs}=${ro_opt}"
1254 done
1256 mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
1258 esac
1261 get_custom_mounts ()
1263 # Side-effect: leaves $devices with persistence.conf mounted in /live/persistence
1264 # Side-effect: prints info to file $custom_mounts
1266 local custom_mounts devices bindings links
1267 custom_mounts=${1}
1268 shift
1269 devices=${@}
1271 bindings="/tmp/bindings.list"
1272 links="/tmp/links.list"
1273 rm -rf ${bindings} ${links} 2> /dev/null
1275 for device in ${devices}
1277 if [ ! -b "${device}" ]
1278 then
1279 continue
1282 local device_name backing include_list
1283 device_name="$(basename ${device})"
1284 backing=$(mount_persistence_media ${device})
1285 if [ -z "${backing}" ]
1286 then
1287 continue
1290 if [ -r "${backing}/${persistence_list}" ]
1291 then
1292 include_list="${backing}/${persistence_list}"
1293 elif [ -r "${backing}/${old_persistence_list}" ]
1294 then
1295 include_list="${backing}/${old_persistence_list}"
1296 else
1297 continue
1300 if [ -n "${DEBUG}" ] && [ -e "${include_list}" ]
1301 then
1302 cp ${include_list} /live/persistence/${persistence_list}.${device_name}
1305 while read dir options # < ${include_list}
1307 if echo ${dir} | grep -qe "^[[:space:]]*\(#.*\)\?$"
1308 then
1309 # skipping empty or commented lines
1310 continue
1313 if trim_path ${dir} | grep -q -e "^[^/]" -e "^/lib" -e "^/lib/live\(/.*\)\?$" -e "^/\(.*/\)\?\.\.\?\(/.*\)\?$"
1314 then
1315 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."
1316 continue
1319 local opt_source opt_link source full_source full_dest
1320 opt_source=""
1321 opt_link=""
1322 for opt in $(echo ${options} | tr ',' ' ');
1324 case "${opt}" in
1325 source=*)
1326 opt_source=${opt#source=}
1328 link)
1329 opt_link="true"
1331 union|bind)
1334 log_warning_msg "Skipping custom mount with unkown option: ${opt}"
1335 continue 2
1337 esac
1338 done
1340 source="${dir}"
1341 if [ -n "${opt_source}" ]
1342 then
1343 if echo ${opt_source} | grep -q -e "^/" -e "^\(.*/\)\?\.\.\?\(/.*\)\?$" && [ "${opt_source}" != "." ]
1344 then
1345 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"
1346 continue
1347 else
1348 source="${opt_source}"
1352 full_source="$(trim_path ${backing}/${source})"
1353 full_dest="$(trim_path ${rootmnt}/${dir})"
1354 if [ -n "${opt_link}" ]
1355 then
1356 echo "${device} ${full_source} ${full_dest} ${options}" >> ${links}
1357 else
1358 echo "${device} ${full_source} ${full_dest} ${options}" >> ${bindings}
1360 done < ${include_list}
1361 done
1363 # We sort the list according to destination so we're sure that
1364 # we won't hide a previous mount. We also ignore duplicate
1365 # destinations in a more or less arbitrary way.
1366 [ -e "${bindings}" ] && sort -k3 -sbu ${bindings} >> ${custom_mounts} && rm ${bindings}
1368 # After all mounts are considered we add symlinks so they
1369 # won't be hidden by some mount.
1370 [ -e "${links}" ] && cat ${links} >> ${custom_mounts} && rm ${links}
1372 # We need to make sure that no two custom mounts have the same sources
1373 # or are nested; if that is the case, too much weird stuff can happen.
1374 local prev_source prev_dest
1375 prev_source="impossible source" # first iteration must not match
1376 prev_dest=""
1377 # This sort will ensure that a source /a comes right before a source
1378 # /a/b so we only need to look at the previous source
1379 sort -k2 -b ${custom_mounts} |
1380 while read device source dest options
1382 if echo ${source} | grep -qe "^${prev_source}\(/.*\)\?$"
1383 then
1384 panic "Two persistence mounts have the same or nested sources: ${source} on ${dest}, and ${prev_source} on ${prev_dest}"
1386 prev_source=${source}
1387 prev_dest=${dest}
1388 done
1391 activate_custom_mounts ()
1393 local custom_mounts used_devices
1394 custom_mounts="${1}" # the ouput from get_custom_mounts()
1395 used_devices=""
1397 while read device source dest options # < ${custom_mounts}
1399 local opt_bind opt_link opt_union
1400 opt_bind="true"
1401 opt_link=""
1402 opt_union=""
1403 for opt in $(echo ${options} | tr ',' ' ');
1405 case "${opt}" in
1406 bind)
1407 opt_bind="true"
1408 unset opt_link opt_union
1410 link)
1411 opt_link="true"
1412 unset opt_bind opt_union
1414 union)
1415 opt_union="true"
1416 unset opt_bind opt_link
1418 esac
1419 done
1421 if [ -n "$(what_is_mounted_on "${dest}")" ]
1422 then
1423 if [ "${dest}" = "${rootmnt}" ]
1424 then
1425 umount "${dest}"
1426 else
1427 log_warning_msg "Skipping custom mount ${dest}: $(what_is_mounted_on "${dest}") is already mounted there"
1428 continue
1432 if [ ! -d "${dest}" ]
1433 then
1434 # create the destination and delete existing files in
1435 # its path that are in the way
1436 path="/"
1437 for dir in $(echo ${dest} | sed -e 's|/\+| |g')
1439 path=$(trim_path ${path}/${dir})
1440 if [ -f ${path} ]
1441 then
1442 rm -f ${path}
1444 if [ ! -e ${path} ]
1445 then
1446 mkdir -p ${path}
1447 if echo ${path} | grep -qe "^${rootmnt}/*home/[^/]\+"
1448 then
1449 # if ${dest} is in /home try fixing proper ownership by assuming that the intended user is the first, which is usually the case
1450 # FIXME: this should really be handled by live-config since we don't know for sure which uid a certain user has until then
1451 chown 1000:1000 ${path}
1454 done
1457 # if ${source} doesn't exist on our persistence media
1458 # we bootstrap it with $dest from the live filesystem.
1459 # this both makes sense and is critical if we're
1460 # dealing with /etc or other system dir.
1461 if [ ! -d "${source}" ]
1462 then
1463 if [ -n "${PERSISTENCE_READONLY}" ]
1464 then
1465 continue
1466 elif [ -n "${opt_union}" ] || [ -n "${opt_link}" ]
1467 then
1468 # unions and don't need to be bootstrapped
1469 # link dirs can't be bootstrapped in a sensible way
1470 mkdir -p "${source}"
1471 chown_ref "${dest}" "${source}"
1472 chmod_ref "${dest}" "${source}"
1473 elif [ -n "${opt_bind}" ]
1474 then
1475 # ensure that $dest is not copied *into* $source
1476 mkdir -p "$(dirname ${source})"
1477 cp -a "${dest}" "${source}"
1481 # XXX: If CONFIG_AUFS_ROBR is added to the Debian kernel we can
1482 # ignore the loop below and set rootfs_dest_backing=$dest
1483 local rootfs_dest_backing
1484 rootfs_dest_backing=""
1485 if [ -n "${opt_link}" ]
1486 then
1487 for d in /live/rootfs/*
1489 if [ -n "${rootmnt}" ]
1490 then
1491 fs="${d}/$(echo ${dest} | sed -e "s|${rootmnt}||")"
1492 else
1493 fs="${d}/${dest}"
1495 if [ -d "${fs}" ]
1496 then
1497 rootfs_dest_backing="${rootfs_dest_backing} ${fs}"
1499 done
1502 local cow_dir links_source
1503 if [ -n "${opt_link}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1504 then
1505 link_files ${source} ${dest} ${rootmnt}
1506 elif [ -n "${opt_link}" ] && [ -n "${PERSISTENCE_READONLY}" ]
1507 then
1508 mkdir -p ${rootmnt}/lib/live/mount/persistence
1509 links_source=$(mktemp -d ${rootmnt}/lib/live/mount/persistence/links-source-XXXXXX)
1510 chown_ref ${source} ${links_source}
1511 chmod_ref ${source} ${links_source}
1512 # We put the cow dir in the below strange place to
1513 # make it absolutely certain that the link source
1514 # has its own directory and isn't nested with some
1515 # other custom mount (if so that mount's files would
1516 # be linked, causing breakage.
1517 cow_dir="/live/overlay/lib/live/mount/persistence/$(basename ${links_source})"
1518 mkdir -p ${cow_dir}
1519 chown_ref "${source}" "${cow_dir}"
1520 chmod_ref "${source}" "${cow_dir}"
1521 do_union ${links_source} ${cow_dir} ${source} ${rootfs_dest_backing}
1522 link_files ${links_source} ${dest} ${rootmnt}
1523 elif [ -n "${opt_union}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1524 then
1525 do_union ${dest} ${source} ${rootfs_dest_backing}
1526 elif [ -n "${opt_bind}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1527 then
1528 mount --bind "${source}" "${dest}"
1529 elif [ -n "${opt_bind}" -o -n "${opt_union}" ] && [ -n "${PERSISTENCE_READONLY}" ]
1530 then
1531 # bind-mount and union mount are handled the same
1532 # in read-only mode, but note that rootfs_dest_backing
1533 # is non-empty (and necessary) only for unions
1534 cow_dir="/live/overlay/${dest}"
1535 if [ -e "${cow_dir}" ] && [ -z "${opt_link}" ]
1536 then
1537 # If an earlier custom mount has files here
1538 # it will "block" the current mount's files
1539 # which is undesirable
1540 rm -rf "${cow_dir}"
1542 mkdir -p ${cow_dir}
1543 chown_ref "${source}" "${cow_dir}"
1544 chmod_ref "${source}" "${cow_dir}"
1545 do_union ${dest} ${cow_dir} ${source} ${rootfs_dest_backing}
1548 PERSISTENCE_IS_ON="1"
1549 export PERSISTENCE_IS_ON
1551 if echo ${used_devices} | grep -qve "^\(.* \)\?${device}\( .*\)\?$"
1552 then
1553 used_devices="${used_devices} ${device}"
1555 done < ${custom_mounts}
1557 echo ${used_devices}
1560 is_mountpoint ()
1562 directory="$1"
1564 [ $(stat -fc%d:%D "${directory}") != $(stat -fc%d:%D "${directory}/..") ]