Adding temporary notes in a readme file.
[debian-live-boot/hramrach.git] / scripts / boot / misc-helpers.sh
blobcc39fcbb230984b267b9d4f1ded11f126c98bc36
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="${1}"
70 if [ "$(echo ${pattern})" != "${pattern}" ]
71 then
72 return 0
75 return 1
78 mount_images_in_directory ()
80 directory="${1}"
81 rootmnt="${2}"
82 mac="${3}"
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"
90 then
91 [ -n "${mac}" ] && adddirectory="${directory}/${LIVE_MEDIA_PATH}/${mac}"
92 setup_unionfs "${directory}/${LIVE_MEDIA_PATH}" "${rootmnt}" "${adddirectory}"
93 else
94 panic "No supported filesystem images found at /${LIVE_MEDIA_PATH}."
98 is_nice_device ()
100 sysfs_path="${1#/sys}"
102 if [ -e /lib/udev/path_id ]
103 then
104 # squeeze
105 PATH_ID="/lib/udev/path_id"
106 else
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)"
112 then
113 return 0
114 elif echo "${sysfs_path}" | grep -q '^/block/vd[a-z]$'
115 then
116 return 0
117 elif echo ${sysfs_path} | grep -q "^/block/dm-"
118 then
119 return 0
120 elif echo ${sysfs_path} | grep -q "^/block/mtdblock"
121 then
122 return 0
125 return 1
128 check_dev ()
130 sysdev="${1}"
131 devname="${2}"
132 skip_uuid_check="${3}"
134 # support for fromiso=.../isofrom=....
135 if [ -n "$FROMISO" ]
136 then
137 ISO_DEVICE=$(dirname $FROMISO)
138 if ! [ -b $ISO_DEVICE ]
139 then
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
143 i=15
144 while [ -n "$ISO_DEVICE" ] && [ "$i" -gt 0 ]
146 ISO_DEVICE=$(dirname ${ISO_DEVICE})
147 [ -b "$ISO_DEVICE" ] && break
148 i=$(($i -1))
149 done
152 if [ "$ISO_DEVICE" = "/" ]
153 then
154 echo "Warning: device for bootoption fromiso= ($FROMISO) not found.">>/boot.log
155 else
156 fs_type=$(get_fstype "${ISO_DEVICE}")
157 if is_supported_fs ${fs_type}
158 then
159 mkdir /live/fromiso
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}"
164 else
165 echo "Warning: unable to mount $ISO_DEVICE." >>/boot.log
170 if [ -z "${devname}" ]
171 then
172 devname=$(sys2dev "${sysdev}")
175 if [ -d "${devname}" ]
176 then
177 mount -o bind "${devname}" $mountpoint || continue
179 if is_live_path $mountpoint
180 then
181 echo $mountpoint
182 return 0
183 else
184 umount $mountpoint
188 IFS=","
189 for device in ${devname}
191 case "$device" in
192 *mapper*)
193 # Adding lvm support
194 if [ -x /scripts/local-top/lvm2 ]
195 then
196 ROOT="$device" resume="" /scripts/local-top/lvm2
200 /dev/md*)
201 # Adding raid support
202 if [ -x /scripts/local-top/mdadm ]
203 then
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
210 esac
211 done
212 unset IFS
214 [ -n "$device" ] && devname="$device"
216 [ -e "$devname" ] || continue
218 if [ -n "${LIVE_MEDIA_OFFSET}" ]
219 then
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}
227 then
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}" ]
234 then
235 if [ -f ${mountpoint}/${FINDISO} ]
236 then
237 umount ${mountpoint}
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}
243 else
244 umount ${mountpoint}
248 if is_live_path ${mountpoint} && \
249 ([ "${skip_uuid_check}" ] || matches_uuid ${mountpoint})
250 then
251 echo ${mountpoint}
252 return 0
253 else
254 umount ${mountpoint} 2>/dev/null
258 if [ -n "${LIVE_MEDIA_OFFSET}" ]
259 then
260 losetup -d "${loopdevname}"
263 return 1
266 find_livefs ()
268 timeout="${1}"
270 # don't start autodetection before timeout has expired
271 if [ -n "${LIVE_MEDIA_TIMEOUT}" ]
272 then
273 if [ "${timeout}" -lt "${LIVE_MEDIA_TIMEOUT}" ]
274 then
275 return 1
279 # first look at the one specified in the command line
280 case "${LIVE_MEDIA}" in
281 removable-usb)
282 for sysblock in $(removable_usb_dev "sys")
284 for dev in $(subdevices "${sysblock}")
286 if check_dev "${dev}"
287 then
288 return 0
290 done
291 done
292 return 1
295 removable)
296 for sysblock in $(removable_dev "sys")
298 for dev in $(subdevices "${sysblock}")
300 if check_dev "${dev}"
301 then
302 return 0
304 done
305 done
306 return 1
310 if [ ! -z "${LIVE_MEDIA}" ]
311 then
312 if check_dev "null" "${LIVE_MEDIA}" "skip_uuid_check"
313 then
314 return 0
318 esac
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
331 then
332 if check_dev "null" "${devname}"
333 then
334 return 0
336 elif is_nice_device "${sysblock}"
337 then
338 for dev in $(subdevices "${sysblock}")
340 if check_dev "${dev}"
341 then
342 return 0
344 done
345 elif [ "${fstype}" = "squashfs" -o \
346 "${fstype}" = "btrfs" -o \
347 "${fstype}" = "ext2" -o \
348 "${fstype}" = "ext3" -o \
349 "${fstype}" = "ext4" -o \
350 "${fstype}" = "jffs2" ]
351 then
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}"
357 return 0
359 done
361 return 1
364 really_export ()
366 STRING="${1}"
367 VALUE="$(eval echo -n \${$STRING})"
369 if [ -f /live.vars ] && grep -sq "export ${STRING}" /live.vars
370 then
371 sed -i -e 's/\('${STRING}'=\).*$/\1'${VALUE}'/' /live.vars
372 else
373 echo "export ${STRING}=\"${VALUE}\"" >> /live.vars
376 eval export "${STRING}"="${VALUE}"
379 is_in_list_separator_helper ()
381 local sep=${1}
382 shift
383 local element=${1}
384 shift
385 local list=${*}
386 echo ${list} | grep -qe "^\(.*${sep}\)\?${element}\(${sep}.*\)\?$"
389 is_in_space_sep_list ()
391 local element=${1}
392 shift
393 is_in_list_separator_helper "[[:space:]]" "${element}" "${*}"
396 is_in_comma_sep_list ()
398 local element=${1}
399 shift
400 is_in_list_separator_helper "," "${element}" "${*}"
403 sys2dev ()
405 sysdev=${1#/sys}
406 echo "/dev/$($udevinfo -q name -p ${sysdev} 2>/dev/null|| echo ${sysdev##*/})"
409 subdevices ()
411 sysblock=${1}
412 r=""
414 for dev in "${sysblock}"/* "${sysblock}"
416 if [ -e "${dev}/dev" ]
417 then
418 r="${r} ${dev}"
420 done
422 echo ${r}
425 storage_devices()
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}
437 then
438 # skip this device entirely
439 continue
442 for dev in $(subdevices "${sysblock}")
444 devname=$(sys2dev "${dev}")
446 if is_in_space_sep_list ${devname} ${black_listed_devices}
447 then
448 # skip this subdevice
449 continue
450 else
451 echo "${devname}"
453 done
454 done
457 is_supported_fs ()
459 fstype="${1}"
461 # Validate input first
462 if [ -z "${fstype}" ]
463 then
464 return 1
467 # Try to look if it is already supported by the kernel
468 if grep -q ${fstype} /proc/filesystems
469 then
470 return 0
471 else
472 # Then try to add support for it the gentle way using the initramfs capabilities
473 modprobe ${fstype}
474 if grep -q ${fstype} /proc/filesystems
475 then
476 return 0
477 # Then try the hard way if /root is already reachable
478 else
479 kmodule="/root/lib/modules/`uname -r`/${fstype}/${fstype}.ko"
480 if [ -e "${kmodule}" ]
481 then
482 insmod "${kmodule}"
483 if grep -q ${fstype} /proc/filesystems
484 then
485 return 0
491 return 1
494 get_fstype ()
496 /sbin/blkid -s TYPE -o value $1 2>/dev/null
499 where_is_mounted ()
501 device=${1}
502 # return first found
503 grep -m1 "^${device} " /proc/mounts | cut -f2 -d ' '
506 trim_path ()
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
519 chown_ref ()
521 local reference="${1}"
522 shift
523 local targets=${@}
524 local owner=$(stat -c %u:%g "${reference}")
525 chown -h ${owner} ${targets}
528 chmod_ref ()
530 local reference="${1}"
531 shift
532 local targets=${@}
533 local rights=$(stat -c %a "${reference}")
534 chmod ${rights} ${targets}
537 lastline ()
539 while read lines
541 line=${lines}
542 done
544 echo "${line}"
547 base_path ()
549 testpath="${1}"
550 mounts="$(awk '{print $2}' /proc/mounts)"
551 testpath="$(busybox realpath ${testpath})"
553 while true
555 if echo "${mounts}" | grep -qs "^${testpath}"
556 then
557 set -- $(echo "${mounts}" | grep "^${testpath}" | lastline)
558 echo ${1}
559 break
560 else
561 testpath=$(dirname $testpath)
563 done
566 fs_size ()
568 # Returns used/free fs kbytes + 5% more
569 # You could pass a block device as ${1} or the mount point as ${2}
571 dev="${1}"
572 mountp="${2}"
573 used="${3}"
575 if [ -z "${mountp}" ]
576 then
577 mountp="$(where_is_mounted ${dev})"
579 if [ -z "${mountp}" ]
580 then
581 mountp="/mnt/tmp_fs_size"
583 mkdir -p "${mountp}"
584 mount -t $(get_fstype "${dev}") -o ro "${dev}" "${mountp}" || log_warning_msg "cannot mount -t $(get_fstype ${dev}) -o ro ${dev} ${mountp}"
586 doumount=1
590 if [ "${used}" = "used" ]
591 then
592 size=$(du -ks ${mountp} | cut -f1)
593 size=$(expr ${size} + ${size} / 20 ) # FIXME: 5% more to be sure
594 else
595 # free space
596 size="$(df -k | grep -s ${mountp} | awk '{print $4}')"
599 if [ -n "${doumount}" ]
600 then
601 umount "${mountp}" || log_warning_msg "cannot umount ${mountp}"
602 rmdir "${mountp}"
605 echo "${size}"
608 load_keymap ()
610 # Load custom keymap
611 if [ -x /bin/loadkeys -a -r /etc/boottime.kmap.gz ]
612 then
613 loadkeys /etc/boottime.kmap.gz
617 setup_loop ()
619 local fspath=${1}
620 local module=${2}
621 local pattern=${3}
622 local offset=${4}
623 local encryption=${5}
624 local readonly=${6}
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
632 udevadm settle
634 for loopdev in ${pattern}
636 if [ "$(cat ${loopdev}/size)" -eq 0 ]
637 then
638 dev=$(sys2dev "${loopdev}")
639 options=''
641 if [ -n "${readonly}" ]
642 then
643 if losetup --help 2>&1 | grep -q -- "-r\b"
644 then
645 options="${options} -r"
649 if [ -n "${offset}" ] && [ 0 -lt "${offset}" ]
650 then
651 options="${options} -o ${offset}"
654 if [ -z "${encryption}" ]
655 then
656 losetup ${options} "${dev}" "${fspath}"
657 else
658 # Loop AES encryption
659 while true
661 load_keymap
663 echo -n "Enter passphrase for root filesystem: " >&6
664 read -s passphrase
665 echo "${passphrase}" > /tmp/passphrase
666 unset passphrase
667 exec 9</tmp/passphrase
668 /sbin/losetup ${options} -e "${encryption}" -p 9 "${dev}" "${fspath}"
669 error=${?}
670 exec 9<&-
671 rm -f /tmp/passphrase
673 if [ 0 -eq ${error} ]
674 then
675 unset error
676 break
679 echo
680 echo -n "There was an error decrypting the root filesystem ... Retry? [Y/n] " >&6
681 read answer
683 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
684 then
685 unset answer
686 break
688 done
691 echo "${dev}"
692 return 0
694 done
696 panic "No loop devices available"
699 try_mount ()
701 dev="${1}"
702 mountp="${2}"
703 opts="${3}"
704 fstype="${4}"
706 old_mountp="$(where_is_mounted ${dev})"
708 if [ -n "${old_mountp}" ]
709 then
710 if [ "${opts}" != "ro" ]
711 then
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}"
716 else
717 if [ -z "${fstype}" ]
718 then
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 mount_persistence_media ()
728 local device=${1}
729 local probe=${2}
731 local backing="/live/persistence/$(basename ${device})"
733 mkdir -p "${backing}"
734 local old_backing="$(where_is_mounted ${device})"
735 if [ -z "${old_backing}" ]
736 then
737 local fstype="$(get_fstype ${device})"
738 local mount_opts="rw,noatime"
739 if [ -n "${PERSISTENCE_READONLY}" ]
740 then
741 mount_opts="ro,noatime"
743 if mount -t "${fstype}" -o "${mount_opts}" "${device}" "${backing}" >/dev/null
744 then
745 echo ${backing}
746 return 0
747 else
748 [ -z "${probe}" ] && log_warning_msg "Failed to mount persistence media ${device}"
749 rmdir "${backing}"
750 return 1
752 elif [ "${backing}" != "${old_backing}" ]
753 then
754 if mount --move ${old_backing} ${backing} >/dev/null
755 then
756 echo ${backing}
757 return 0
758 else
759 [ -z "${probe}" ] && log_warning_msg "Failed to move persistence media ${device}"
760 rmdir "${backing}"
761 return 1
764 return 0
767 close_persistence_media ()
769 local device=${1}
770 local backing="$(where_is_mounted ${device})"
772 if [ -d "${backing}" ]
773 then
774 umount "${backing}" >/dev/null 2>&1
775 rmdir "${backing}" >/dev/null 2>&1
778 if is_active_luks_mapping ${device}
779 then
780 /sbin/cryptsetup luksClose ${device}
784 open_luks_device ()
786 dev="${1}"
787 name="$(basename ${dev})"
788 opts="--key-file=-"
789 if [ -n "${PERSISTENCE_READONLY}" ]
790 then
791 opts="${opts} --readonly"
794 if /sbin/cryptsetup status "${name}" >/dev/null 2>&1
795 then
796 re="^[[:space:]]*device:[[:space:]]*\([^[:space:]]*\)$"
797 opened_dev=$(cryptsetup status ${name} 2>/dev/null | grep "${re}" | sed "s|${re}|\1|")
798 if [ "${opened_dev}" = "${dev}" ]
799 then
800 luks_device="/dev/mapper/${name}"
801 echo ${luks_device}
802 return 0
803 else
804 log_warning_msg "Cannot open luks device ${dev} since ${opened_dev} already is opened with its name"
805 return 1
809 load_keymap
811 while true
813 /lib/cryptsetup/askpass "Enter passphrase for ${dev}: " | \
814 /sbin/cryptsetup -T 1 luksOpen ${dev} ${name} ${opts}
816 if [ 0 -eq ${?} ]
817 then
818 luks_device="/dev/mapper/${name}"
819 echo ${luks_device}
820 return 0
823 echo >&6
824 echo -n "There was an error decrypting ${dev} ... Retry? [Y/n] " >&6
825 read answer
827 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
828 then
829 return 2
831 done
834 get_gpt_name ()
836 local dev="${1}"
837 /sbin/blkid -s PART_ENTRY_NAME -p -o value ${dev} 2>/dev/null
840 is_gpt_device ()
842 local dev="${1}"
843 [ "$(/sbin/blkid -s PART_ENTRY_SCHEME -p -o value ${dev} 2>/dev/null)" = "gpt" ]
846 probe_for_gpt_name ()
848 local overlays="${1}"
849 local dev="${2}"
851 local gpt_dev="${dev}"
852 if is_active_luks_mapping ${dev}
853 then
854 # if $dev is an opened luks device, we need to check
855 # GPT stuff on the backing device
856 gpt_dev=$(get_luks_backing_device "${dev}")
859 if ! is_gpt_device ${gpt_dev}
860 then
861 return
864 local gpt_name=$(get_gpt_name ${gpt_dev})
865 for label in ${overlays}
867 if [ "${gpt_name}" = "${label}" ]
868 then
869 echo "${label}=${dev}"
871 done
874 probe_for_fs_label ()
876 local overlays="${1}"
877 local dev="${2}"
879 for label in ${overlays}
881 if [ "$(/sbin/blkid -s LABEL -o value $dev 2>/dev/null)" = "${label}" ]
882 then
883 echo "${label}=${dev}"
885 done
888 probe_for_file_name ()
890 local overlays="${1}"
891 local dev="${2}"
893 local ret=""
894 local backing="$(mount_persistence_media ${dev} probe)"
895 if [ -z "${backing}" ]
896 then
897 return
900 for label in ${overlays}
902 path=${backing}/${PERSISTENCE_PATH}${label}
903 if [ -f "${path}" ]
904 then
905 local loopdev=$(setup_loop "${path}" "loop" "/sys/block/loop*")
906 ret="${ret} ${label}=${loopdev}"
908 done
910 if [ -n "${ret}" ]
911 then
912 echo ${ret}
913 else
914 umount ${backing} > /dev/null 2>&1 || true
918 find_persistence_media ()
920 # Scans devices for overlays, and returns a whitespace
921 # separated list of how to use them. Only overlays with a partition
922 # label or file name in ${overlays} are returned.
924 # When scanning a LUKS device, the user will be asked to enter the
925 # passphrase; on failure to enter it, or if no persistence partitions
926 # or files were found, the LUKS device is closed.
928 # For all other cases (overlay partition and overlay file) the
929 # return value is "${label}=${device}", where ${device} a device that
930 # can mount the content. In the case of an overlay file, the device
931 # containing the file will remain mounted as a side-effect.
933 # No devices in ${black_listed_devices} will be scanned, and if
934 # ${white_list_devices} is non-empty, only devices in it will be
935 # scanned.
937 local overlays="${1}"
938 local white_listed_devices="${2}"
939 local ret=""
941 local black_listed_devices="$(what_is_mounted_on /live/image)"
943 for dev in $(storage_devices "${black_listed_devices}" "${white_listed_devices}")
945 local result=""
947 local luks_device=""
948 # Check if it's a luks device; we'll have to open the device
949 # in order to probe any filesystem it contains, like we do
950 # below. activate_custom_mounts() also depends on that any luks
951 # device already has been opened.
952 if is_in_comma_sep_list luks ${PERSISTENCE_ENCRYPTION} && is_luks_partition ${dev}
953 then
954 if luks_device=$(open_luks_device "${dev}")
955 then
956 dev="${luks_device}"
957 else
958 # skip $dev since we failed/chose not to open it
959 continue
961 elif ! is_in_comma_sep_list none ${PERSISTENCE_ENCRYPTION}
962 then
963 # skip $dev since we don't allow unencrypted storage
964 continue
967 # Probe for matching GPT partition names or filesystem labels
968 if is_in_comma_sep_list filesystem ${PERSISTENCE_STORAGE}
969 then
970 result=$(probe_for_gpt_name "${overlays}" ${dev})
971 if [ -n "${result}" ]
972 then
973 ret="${ret} ${result}"
974 continue
977 result=$(probe_for_fs_label "${overlays}" ${dev})
978 if [ -n "${result}" ]
979 then
980 ret="${ret} ${result}"
981 continue
985 # Probe for files with matching name on mounted partition
986 if is_in_comma_sep_list file ${PERSISTENCE_STORAGE}
987 then
988 result=$(probe_for_file_name "${overlays}" ${dev})
989 if [ -n "${result}" ]
990 then
991 ret="${ret} ${result}"
992 continue
996 # Close luks device if it isn't used
997 if [ -z "${result}" ] && [ -n "${luks_device}" ] && is_active_luks_mapping "${luks_device}"
998 then
999 /sbin/cryptsetup luksClose "${luks_device}"
1001 done
1003 if [ -n "${ret}" ]
1004 then
1005 echo ${ret}
1009 get_mac ()
1011 mac=""
1013 for adaptor in /sys/class/net/*
1015 status="$(cat ${adaptor}/iflink)"
1017 if [ "${status}" -eq 2 ]
1018 then
1019 mac="$(cat ${adaptor}/address)"
1020 mac="$(echo ${mac} | sed 's/:/-/g' | tr '[a-z]' '[A-Z]')"
1022 done
1024 echo ${mac}
1027 is_luks_partition ()
1029 device="${1}"
1030 /sbin/cryptsetup isLuks "${device}" 1>/dev/null 2>&1
1033 is_active_luks_mapping ()
1035 device="${1}"
1036 /sbin/cryptsetup status "${device}" 1>/dev/null 2>&1
1039 get_luks_backing_device ()
1041 device=${1}
1042 cryptsetup status ${device} 2> /dev/null | \
1043 awk '{if ($1 == "device:") print $2}'
1046 removable_dev ()
1048 output_format="${1}"
1049 want_usb="${2}"
1050 ret=
1052 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1054 dev_ok=
1055 if [ "$(cat ${sysblock}/removable)" = "1" ]
1056 then
1057 if [ -z "${want_usb}" ]
1058 then
1059 dev_ok="true"
1060 else
1061 if readlink ${sysblock} | grep -q usb
1062 then
1063 dev_ok="true"
1068 if [ "${dev_ok}" = "true" ]
1069 then
1070 case "${output_format}" in
1071 sys)
1072 ret="${ret} ${sysblock}"
1075 devname=$(sys2dev "${sysblock}")
1076 ret="${ret} ${devname}"
1078 esac
1080 done
1082 echo "${ret}"
1085 removable_usb_dev ()
1087 output_format="${1}"
1089 removable_dev "${output_format}" "want_usb"
1092 non_removable_dev ()
1094 output_format="${1}"
1095 ret=
1097 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1099 if [ "$(cat ${sysblock}/removable)" = "0" ]
1100 then
1101 case "${output_format}" in
1102 sys)
1103 ret="${ret} ${sysblock}"
1106 devname=$(sys2dev "${sysblock}")
1107 ret="${ret} ${devname}"
1109 esac
1111 done
1113 echo "${ret}"
1116 link_files ()
1118 # create source's directory structure in dest, and recursively
1119 # create symlinks in dest to to all files in source. if mask
1120 # is non-empty, remove mask from all source paths when
1121 # creating links (will be necessary if we change root, which
1122 # live-boot normally does (into $rootmnt)).
1124 # remove multiple /:s and ensure ending on /
1125 local src_dir="$(trim_path ${1})/"
1126 local dest_dir="$(trim_path ${2})/"
1127 local src_mask="${3}"
1129 # This check can only trigger on the inital, non-recursive call since
1130 # we create the destination before recursive calls
1131 if [ ! -d "${dest_dir}" ]
1132 then
1133 log_warning_msg "Must link_files into a directory"
1134 return
1137 find "${src_dir}" -mindepth 1 -maxdepth 1 | \
1138 while read src
1140 local dest="${dest_dir}$(basename "${src}")"
1141 if [ -d "${src}" ]
1142 then
1143 if [ -z "$(ls -A "${src}")" ]
1144 then
1145 continue
1147 if [ ! -d "${dest}" ]
1148 then
1149 mkdir -p "${dest}"
1150 chown_ref "${src}" "${dest}"
1151 chmod_ref "${src}" "${dest}"
1153 link_files "${src}" "${dest}" "${src_mask}"
1154 else
1155 local final_src=${src}
1156 if [ -n "${src_mask}" ]
1157 then
1158 final_src="$(echo ${final_src} | sed "s|^${src_mask}||")"
1160 rm -rf "${dest}" 2> /dev/null
1161 ln -s "${final_src}" "${dest}"
1162 chown_ref "${src}" "${dest}"
1164 done
1167 do_union ()
1169 local unionmountpoint="${1}" # directory where the union is mounted
1170 local unionrw="${2}" # branch where the union changes are stored
1171 local unionro1="${3}" # first underlying read-only branch (optional)
1172 local unionro2="${4}" # second underlying read-only branch (optional)
1174 case "${UNIONTYPE}" in
1175 aufs)
1176 rw_opt="rw"
1177 ro_opt="rr+wh"
1178 noxino_opt="noxino"
1181 unionfs-fuse)
1182 rw_opt="RW"
1183 ro_opt="RO"
1187 rw_opt="rw"
1188 ro_opt="ro"
1190 esac
1192 case "${UNIONTYPE}" in
1193 unionfs-fuse)
1194 unionmountopts="-o cow -o noinitgroups -o default_permissions -o allow_other -o use_ino -o suid"
1195 unionmountopts="${unionmountopts} ${unionrw}=${rw_opt}"
1196 if [ -n "${unionro1}" ]
1197 then
1198 unionmountopts="${unionmountopts}:${unionro1}=${ro_opt}"
1200 if [ -n "${unionro2}" ]
1201 then
1202 unionmountopts="${unionmountopts}:${unionro2}=${ro_opt}"
1204 ( sysctl -w fs.file-max=391524 ; ulimit -HSn 16384
1205 unionfs-fuse ${unionmountopts} "${unionmountpoint}" ) && \
1206 ( mkdir -p /run/sendsigs.omit.d
1207 pidof unionfs-fuse >> /run/sendsigs.omit.d/unionfs-fuse || true )
1210 overlayfs)
1211 # XXX: can unionro2 be used? (overlayfs only handles two dirs, but perhaps they can be chained?)
1212 # XXX: and can unionro1 be optional? i.e. can overlayfs skip lowerdir?
1213 unionmountopts="-o noatime,lowerdir=${unionro1},upperdir=${unionrw}"
1214 mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
1218 unionmountopts="-o noatime,${noxino_opt},dirs=${unionrw}=${rw_opt}"
1219 if [ -n "${unionro1}" ]
1220 then
1221 unionmountopts="${unionmountopts}:${unionro1}=${ro_opt}"
1223 if [ -n "${unionro2}" ]
1224 then
1225 unionmountopts="${unionmountopts}:${unionro2}=${ro_opt}"
1227 mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
1229 esac
1232 get_custom_mounts ()
1234 # Side-effect: leaves $devices with live-persistence.conf mounted in /live/persistence
1235 # Side-effect: prints info to file $custom_mounts
1237 local custom_mounts=${1}
1238 shift
1239 local devices=${@}
1241 local bindings="/tmp/bindings.list"
1242 local links="/tmp/links.list"
1243 rm -rf ${bindings} ${links} 2> /dev/null
1245 for device in ${devices}
1247 if [ ! -b "${device}" ]
1248 then
1249 continue
1252 local device_name="$(basename ${device})"
1253 local backing=$(mount_persistence_media ${device})
1254 if [ -z "${backing}" ]
1255 then
1256 continue
1259 local include_list="${backing}/${persistence_list}"
1260 if [ ! -r "${include_list}" ]
1261 then
1262 continue
1265 if [ -n "${DEBUG}" ] && [ -e "${include_list}" ]
1266 then
1267 cp ${include_list} /live/persistence/${persistence_list}.${device_name}
1270 while read dir options # < ${include_list}
1272 if echo ${dir} | grep -qe "^[[:space:]]*\(#.*\)\?$"
1273 then
1274 # skipping empty or commented lines
1275 continue
1278 if trim_path ${dir} | grep -q -e "^[^/]" -e "^/live\(/.*\)\?$" -e "^/\(.*/\)\?\.\.\?\(/.*\)\?$"
1279 then
1280 log_warning_msg "Skipping unsafe custom mount ${dir}: must be an absolute path containing neither the \".\" nor \"..\" special dirs, and cannot be \"/live\" or any sub-directory therein."
1281 continue
1284 local opt_source=""
1285 local opt_link=""
1286 for opt in $(echo ${options} | tr ',' ' ');
1288 case "${opt}" in
1289 source=*)
1290 opt_source=${opt#source=}
1292 link)
1293 opt_link="true"
1295 union|bind)
1298 log_warning_msg "Skipping custom mount with unkown option: ${opt}"
1299 continue 2
1301 esac
1302 done
1304 local source="${dir}"
1305 if [ -n "${opt_source}" ]
1306 then
1307 if echo ${opt_source} | grep -q -e "^/" -e "^\(.*/\)\?\.\.\?\(/.*\)\?$" && [ "${source}" != "." ]
1308 then
1309 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"
1310 continue
1311 else
1312 source="${opt_source}"
1316 local full_source="$(trim_path ${backing}/${source})"
1317 local full_dest="$(trim_path ${rootmnt}/${dir})"
1318 if [ -n "${opt_link}" ]
1319 then
1320 echo "${device} ${full_source} ${full_dest} ${options}" >> ${links}
1321 else
1322 echo "${device} ${full_source} ${full_dest} ${options}" >> ${bindings}
1324 done < ${include_list}
1325 done
1327 # We sort the list according to destination so we're sure that
1328 # we won't hide a previous mount. We also ignore duplicate
1329 # destinations in a more or less arbitrary way.
1330 [ -e "${bindings}" ] && sort -k3 -sbu ${bindings} >> ${custom_mounts} && rm ${bindings}
1332 # After all mounts are considered we add symlinks so they
1333 # won't be hidden by some mount.
1334 [ -e "${links}" ] && cat ${links} >> ${custom_mounts} && rm ${links}
1336 # We need to make sure that no two custom mounts have the same sources
1337 # or are nested; if that is the case, too much weird stuff can happen.
1338 local prev_source="impossible source" # first iteration must not match
1339 local prev_dest=""
1340 # This sort will ensure that a source /a comes right before a source
1341 # /a/b so we only need to look at the previous source
1342 sort -k2 -b ${custom_mounts} |
1343 while read device source dest options
1345 if echo ${source} | grep -qe "^${prev_source}\(/.*\)\?$"
1346 then
1347 panic "Two persistence mounts have the same or nested sources: ${source} on ${dest}, and ${prev_source} on ${prev_dest}"
1349 prev_source=${source}
1350 prev_dest=${dest}
1351 done
1354 activate_custom_mounts ()
1356 local custom_mounts="${1}" # the ouput from get_custom_mounts()
1357 local used_devices=""
1359 while read device source dest options # < ${custom_mounts}
1361 local opt_bind="true"
1362 local opt_link=""
1363 local opt_union=""
1364 for opt in $(echo ${options} | tr ',' ' ');
1366 case "${opt}" in
1367 bind)
1368 opt_bind="true"
1369 unset opt_link opt_union
1371 link)
1372 opt_link="true"
1373 unset opt_bind opt_union
1375 union)
1376 opt_union="true"
1377 unset opt_bind opt_link
1379 esac
1380 done
1382 if [ -n "$(what_is_mounted_on "${dest}")" ]
1383 then
1384 if [ "${dest}" = "${rootmnt}" ]
1385 then
1386 umount "${dest}"
1387 else
1388 log_warning_msg "Skipping custom mount ${dest}: $(what_is_mounted_on "${dest}") is already mounted there"
1389 continue
1393 if [ ! -d "${dest}" ]
1394 then
1395 # create the destination and delete existing files in
1396 # its path that are in the way
1397 path="/"
1398 for dir in $(echo ${dest} | sed -e 's|/\+| |g')
1400 path=$(trim_path ${path}/${dir})
1401 if [ -f ${path} ]
1402 then
1403 rm -f ${path}
1405 if [ ! -e ${path} ]
1406 then
1407 mkdir -p ${path}
1408 if echo ${path} | grep -qe "^${rootmnt}/*home/[^/]\+"
1409 then
1410 # if ${dest} is in /home try fixing proper ownership by assuming that the intended user is the first, which is usually the case
1411 # FIXME: this should really be handled by live-config since we don't know for sure which uid a certain user has until then
1412 chown 1000:1000 ${path}
1415 done
1418 # if ${source} doesn't exist on our persistence media
1419 # we bootstrap it with $dest from the live filesystem.
1420 # this both makes sense and is critical if we're
1421 # dealing with /etc or other system dir.
1422 if [ ! -d "${source}" ]
1423 then
1424 if [ -n "${PERSISTENCE_READONLY}" ]
1425 then
1426 continue
1427 elif [ -n "${opt_union}" ] || [ -n "${opt_link}" ]
1428 then
1429 # unions and don't need to be bootstrapped
1430 # link dirs can't be bootstrapped in a sensible way
1431 mkdir -p "${source}"
1432 chown_ref "${dest}" "${source}"
1433 chmod_ref "${dest}" "${source}"
1434 elif [ -n "${opt_bind}" ]
1435 then
1436 # ensure that $dest is not copied *into* $source
1437 mkdir -p "$(dirname ${source})"
1438 cp -a "${dest}" "${source}"
1442 # XXX: If CONFIG_AUFS_ROBR is added to the Debian kernel we can
1443 # ignore the loop below and set rofs_dest_backing=$dest
1444 local rofs_dest_backing=""
1445 if [ -n "${opt_link}"]
1446 then
1447 for d in /live/rofs/*
1449 if [ -n "${rootmnt}" ]
1450 then
1451 rofs_dest_backing="${d}/$(echo ${dest} | sed -e "s|${rootmnt}||")"
1452 else
1453 rofs_dest_backing="${d}/${dest}"
1455 if [ -d "${rofs_dest_backing}" ]
1456 then
1457 break
1458 else
1459 rofs_dest_backing=""
1461 done
1464 if [ -n "${opt_link}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1465 then
1466 link_files ${source} ${dest} ${rootmnt}
1467 elif [ -n "${opt_link}" ] && [ -n "${PERSISTENCE_READONLY}" ]
1468 then
1469 mkdir -p /live/persistence
1470 local links_source=$(mktemp -d /live/persistence/links-source-XXXXXX)
1471 chown_ref ${source} ${links_source}
1472 chmod_ref ${source} ${links_source}
1473 # We put the cow dir in the below strange place to
1474 # make it absolutely certain that the link source
1475 # has its own directory and isn't nested with some
1476 # other custom mount (if so that mount's files would
1477 # be linked, causing breakage.
1478 local cow_dir="/live/overlay/live/persistence/$(basename ${links_source})"
1479 mkdir -p ${cow_dir}
1480 chown_ref "${source}" "${cow_dir}"
1481 chmod_ref "${source}" "${cow_dir}"
1482 do_union ${links_source} ${cow_dir} ${source} ${rofs_dest_backing}
1483 link_files ${links_source} ${dest} ${rootmnt}
1484 elif [ -n "${opt_union}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1485 then
1486 do_union ${dest} ${source} ${rofs_dest_backing}
1487 elif [ -n "${opt_bind}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1488 then
1489 mount --bind "${source}" "${dest}"
1490 elif [ -n "${opt_bind}" -o -n "${opt_union}" ] && [ -n "${PERSISTENCE_READONLY}" ]
1491 then
1492 # bind-mount and union mount are handled the same
1493 # in read-only mode, but note that rofs_dest_backing
1494 # is non-empty (and necessary) only for unions
1495 if [ -n "${rootmnt}" ]
1496 then
1497 local cow_dir="$(echo ${dest} | sed -e "s|^${rootmnt}|/live/overlay/|")"
1498 else
1499 # This is happens if persistence is activated
1500 # post boot
1501 local cow_dir="/live/overlay/${dest}"
1503 if [ -e "${cow_dir}" ] && [ -z "${opt_link}" ]
1504 then
1505 # If an earlier custom mount has files here
1506 # it will "block" the current mount's files
1507 # which is undesirable
1508 rm -rf "${cow_dir}"
1510 mkdir -p ${cow_dir}
1511 chown_ref "${source}" "${cow_dir}"
1512 chmod_ref "${source}" "${cow_dir}"
1513 do_union ${dest} ${cow_dir} ${source} ${rofs_dest_backing}
1516 PERSISTENCE_IS_ON="1"
1517 export PERSISTENCE_IS_ON
1519 if echo ${used_devices} | grep -qve "^\(.* \)\?${device}\( .*\)\?$"
1520 then
1521 used_devices="${used_devices} ${device}"
1523 done < ${custom_mounts}
1525 echo ${used_devices}
1528 fix_backwards_compatibility ()
1530 local device=${1}
1531 local dir=${2}
1532 local opt=${3}
1534 if [ -n "${PERSISTENCE_READONLY}" ]
1535 then
1536 return
1539 local backing="$(mount_persistence_media ${device})"
1540 if [ -z "${backing}" ]
1541 then
1542 return
1545 local include_list="${backing}/${persistence_list}"
1546 if [ ! -r "${include_list}" ]
1547 then
1548 echo "# persistence backwards compatibility:
1549 ${dir} ${opt},source=." > "${include_list}"
1553 is_mountpoint ()
1555 directory="$1"
1557 [ $(stat -fc%d:%D "${directory}") != $(stat -fc%d:%D "${directory}/..") ]