Adding upstream version 4.0~a1.
[debian-live-boot.git] / scripts / boot / 9990-misc-helpers.sh
blob358448b20e5559168c4e18346c88dc69e48261ad
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 /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)"
103 then
104 return 0
105 elif echo "${sysfs_path}" | grep -q '^/block/vd[a-z]$'
106 then
107 return 0
108 elif echo ${sysfs_path} | grep -q "^/block/dm-"
109 then
110 return 0
111 elif echo ${sysfs_path} | grep -q "^/block/mtdblock"
112 then
113 return 0
116 return 1
119 check_dev ()
121 sysdev="${1}"
122 devname="${2}"
123 skip_uuid_check="${3}"
125 # support for fromiso=.../isofrom=....
126 if [ -n "$FROMISO" ]
127 then
128 ISO_DEVICE=$(dirname $FROMISO)
129 if ! [ -b $ISO_DEVICE ]
130 then
131 # to support unusual device names like /dev/cciss/c0d0p1
132 # as well we have to identify the block device name, let's
133 # do that for up to 15 levels
134 i=15
135 while [ -n "$ISO_DEVICE" ] && [ "$i" -gt 0 ]
137 ISO_DEVICE=$(dirname ${ISO_DEVICE})
138 [ -b "$ISO_DEVICE" ] && break
139 i=$(($i -1))
140 done
143 if [ "$ISO_DEVICE" = "/" ]
144 then
145 echo "Warning: device for bootoption fromiso= ($FROMISO) not found.">>/boot.log
146 else
147 fs_type=$(get_fstype "${ISO_DEVICE}")
148 if is_supported_fs ${fs_type}
149 then
150 mkdir /live/fromiso
151 mount -t $fs_type "$ISO_DEVICE" /live/fromiso
152 ISO_NAME="$(echo $FROMISO | sed "s|$ISO_DEVICE||")"
153 loopdevname=$(setup_loop "/live/fromiso/${ISO_NAME}" "loop" "/sys/block/loop*" "" '')
154 devname="${loopdevname}"
155 else
156 echo "Warning: unable to mount $ISO_DEVICE." >>/boot.log
161 if [ -z "${devname}" ]
162 then
163 devname=$(sys2dev "${sysdev}")
166 if [ -d "${devname}" ]
167 then
168 mount -o bind "${devname}" $mountpoint || continue
170 if is_live_path $mountpoint
171 then
172 echo $mountpoint
173 return 0
174 else
175 umount $mountpoint
179 IFS=","
180 for device in ${devname}
182 case "$device" in
183 *mapper*)
184 # Adding lvm support
185 if [ -x /scripts/local-top/lvm2 ]
186 then
187 ROOT="$device" resume="" /scripts/local-top/lvm2
191 /dev/md*)
192 # Adding raid support
193 if [ -x /scripts/local-top/mdadm ]
194 then
195 cp /conf/conf.d/md /conf/conf.d/md.orig
196 echo "MD_DEVS=$device " >> /conf/conf.d/md
197 /scripts/local-top/mdadm
198 mv /conf/conf.d/md.orig /conf/conf.d/md
201 esac
202 done
203 unset IFS
205 [ -n "$device" ] && devname="$device"
207 [ -e "$devname" ] || continue
209 if [ -n "${LIVE_MEDIA_OFFSET}" ]
210 then
211 loopdevname=$(setup_loop "${devname}" "loop" "/sys/block/loop*" "${LIVE_MEDIA_OFFSET}" '')
212 devname="${loopdevname}"
215 fstype=$(get_fstype "${devname}")
217 if is_supported_fs ${fstype}
218 then
219 devuid=$(blkid -o value -s UUID "$devname")
220 [ -n "$devuid" ] && grep -qs "\<$devuid\>" $tried && continue
221 mount -t ${fstype} -o ro,noatime "${devname}" ${mountpoint} || continue
222 [ -n "$devuid" ] && echo "$devuid" >> $tried
224 if [ -n "${FINDISO}" ]
225 then
226 if [ -f ${mountpoint}/${FINDISO} ]
227 then
228 umount ${mountpoint}
229 mkdir -p /live/findiso
230 mount -t ${fstype} -o ro,noatime "${devname}" /live/findiso
231 loopdevname=$(setup_loop "/live/findiso/${FINDISO}" "loop" "/sys/block/loop*" 0 "")
232 devname="${loopdevname}"
233 mount -t iso9660 -o ro,noatime "${devname}" ${mountpoint}
234 else
235 umount ${mountpoint}
239 if is_live_path ${mountpoint} && \
240 ([ "${skip_uuid_check}" ] || matches_uuid ${mountpoint})
241 then
242 echo ${mountpoint}
243 return 0
244 else
245 umount ${mountpoint} 2>/dev/null
249 if [ -n "${LIVE_MEDIA_OFFSET}" ]
250 then
251 losetup -d "${loopdevname}"
254 return 1
257 find_livefs ()
259 timeout="${1}"
261 # don't start autodetection before timeout has expired
262 if [ -n "${LIVE_MEDIA_TIMEOUT}" ]
263 then
264 if [ "${timeout}" -lt "${LIVE_MEDIA_TIMEOUT}" ]
265 then
266 return 1
270 # first look at the one specified in the command line
271 case "${LIVE_MEDIA}" in
272 removable-usb)
273 for sysblock in $(removable_usb_dev "sys")
275 for dev in $(subdevices "${sysblock}")
277 if check_dev "${dev}"
278 then
279 return 0
281 done
282 done
283 return 1
286 removable)
287 for sysblock in $(removable_dev "sys")
289 for dev in $(subdevices "${sysblock}")
291 if check_dev "${dev}"
292 then
293 return 0
295 done
296 done
297 return 1
301 if [ ! -z "${LIVE_MEDIA}" ]
302 then
303 if check_dev "null" "${LIVE_MEDIA}" "skip_uuid_check"
304 then
305 return 0
309 esac
311 # or do the scan of block devices
312 # prefer removable devices over non-removable devices, so scan them first
313 devices_to_scan="$(removable_dev 'sys') $(non_removable_dev 'sys')"
315 for sysblock in $devices_to_scan
317 devname=$(sys2dev "${sysblock}")
318 [ -e "$devname" ] || continue
319 fstype=$(get_fstype "${devname}")
321 if /lib/udev/cdrom_id ${devname} > /dev/null
322 then
323 if check_dev "null" "${devname}"
324 then
325 return 0
327 elif is_nice_device "${sysblock}"
328 then
329 for dev in $(subdevices "${sysblock}")
331 if check_dev "${dev}"
332 then
333 return 0
335 done
336 elif [ "${fstype}" = "squashfs" -o \
337 "${fstype}" = "btrfs" -o \
338 "${fstype}" = "ext2" -o \
339 "${fstype}" = "ext3" -o \
340 "${fstype}" = "ext4" -o \
341 "${fstype}" = "jffs2" ]
342 then
343 # This is an ugly hack situation, the block device has
344 # an image directly on it. It's hopefully
345 # live-boot, so take it and run with it.
346 ln -s "${devname}" "${devname}.${fstype}"
347 echo "${devname}.${fstype}"
348 return 0
350 done
352 return 1
355 really_export ()
357 STRING="${1}"
358 VALUE="$(eval echo -n \${$STRING})"
360 if [ -f /live.vars ] && grep -sq "export ${STRING}" /live.vars
361 then
362 sed -i -e 's/\('${STRING}'=\).*$/\1'${VALUE}'/' /live.vars
363 else
364 echo "export ${STRING}=\"${VALUE}\"" >> /live.vars
367 eval export "${STRING}"="${VALUE}"
370 is_in_list_separator_helper ()
372 local sep=${1}
373 shift
374 local element=${1}
375 shift
376 local list=${*}
377 echo ${list} | grep -qe "^\(.*${sep}\)\?${element}\(${sep}.*\)\?$"
380 is_in_space_sep_list ()
382 local element=${1}
383 shift
384 is_in_list_separator_helper "[[:space:]]" "${element}" "${*}"
387 is_in_comma_sep_list ()
389 local element=${1}
390 shift
391 is_in_list_separator_helper "," "${element}" "${*}"
394 sys2dev ()
396 sysdev=${1#/sys}
397 echo "/dev/$($udevinfo -q name -p ${sysdev} 2>/dev/null|| echo ${sysdev##*/})"
400 subdevices ()
402 sysblock=${1}
403 r=""
405 for dev in "${sysblock}"/* "${sysblock}"
407 if [ -e "${dev}/dev" ]
408 then
409 r="${r} ${dev}"
411 done
413 echo ${r}
416 storage_devices()
418 black_listed_devices="${1}"
419 white_listed_devices="${2}"
421 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "loop|ram|fd")
423 fulldevname=$(sys2dev "${sysblock}")
425 if is_in_space_sep_list ${fulldevname} ${black_listed_devices} || \
426 [ -n "${white_listed_devices}" ] && \
427 ! is_in_space_sep_list ${fulldevname} ${white_listed_devices}
428 then
429 # skip this device entirely
430 continue
433 for dev in $(subdevices "${sysblock}")
435 devname=$(sys2dev "${dev}")
437 if is_in_space_sep_list ${devname} ${black_listed_devices}
438 then
439 # skip this subdevice
440 continue
441 else
442 echo "${devname}"
444 done
445 done
448 is_supported_fs ()
450 fstype="${1}"
452 # Validate input first
453 if [ -z "${fstype}" ]
454 then
455 return 1
458 # Try to look if it is already supported by the kernel
459 if grep -q ${fstype} /proc/filesystems
460 then
461 return 0
462 else
463 # Then try to add support for it the gentle way using the initramfs capabilities
464 modprobe ${fstype}
465 if grep -q ${fstype} /proc/filesystems
466 then
467 return 0
468 # Then try the hard way if /root is already reachable
469 else
470 kmodule="/root/lib/modules/`uname -r`/${fstype}/${fstype}.ko"
471 if [ -e "${kmodule}" ]
472 then
473 insmod "${kmodule}"
474 if grep -q ${fstype} /proc/filesystems
475 then
476 return 0
482 return 1
485 get_fstype ()
487 /sbin/blkid -s TYPE -o value $1 2>/dev/null
490 where_is_mounted ()
492 device=${1}
493 # return first found
494 grep -m1 "^${device} " /proc/mounts | cut -f2 -d ' '
497 trim_path ()
499 # remove all unnecessary /:s in the path, including last one (except
500 # if path is just "/")
501 echo ${1} | sed 's|//\+|/|g' | sed 's|^\(.*[^/]\)/$|\1|'
504 what_is_mounted_on ()
506 local dir="$(trim_path ${1})"
507 grep -m1 "^[^ ]\+ ${dir} " /proc/mounts | cut -d' ' -f1
510 chown_ref ()
512 local reference="${1}"
513 shift
514 local targets=${@}
515 local owner=$(stat -c %u:%g "${reference}")
516 chown -h ${owner} ${targets}
519 chmod_ref ()
521 local reference="${1}"
522 shift
523 local targets=${@}
524 local rights=$(stat -c %a "${reference}")
525 chmod ${rights} ${targets}
528 lastline ()
530 while read lines
532 line=${lines}
533 done
535 echo "${line}"
538 base_path ()
540 testpath="${1}"
541 mounts="$(awk '{print $2}' /proc/mounts)"
542 testpath="$(busybox realpath ${testpath})"
544 while true
546 if echo "${mounts}" | grep -qs "^${testpath}"
547 then
548 set -- $(echo "${mounts}" | grep "^${testpath}" | lastline)
549 echo ${1}
550 break
551 else
552 testpath=$(dirname $testpath)
554 done
557 fs_size ()
559 # Returns used/free fs kbytes + 5% more
560 # You could pass a block device as ${1} or the mount point as ${2}
562 dev="${1}"
563 mountp="${2}"
564 used="${3}"
566 if [ -z "${mountp}" ]
567 then
568 mountp="$(where_is_mounted ${dev})"
570 if [ -z "${mountp}" ]
571 then
572 mountp="/mnt/tmp_fs_size"
574 mkdir -p "${mountp}"
575 mount -t $(get_fstype "${dev}") -o ro "${dev}" "${mountp}" || log_warning_msg "cannot mount -t $(get_fstype ${dev}) -o ro ${dev} ${mountp}"
577 doumount=1
581 if [ "${used}" = "used" ]
582 then
583 size=$(du -ks ${mountp} | cut -f1)
584 size=$(expr ${size} + ${size} / 20 ) # FIXME: 5% more to be sure
585 else
586 # free space
587 size="$(df -k | grep -s ${mountp} | awk '{print $4}')"
590 if [ -n "${doumount}" ]
591 then
592 umount "${mountp}" || log_warning_msg "cannot umount ${mountp}"
593 rmdir "${mountp}"
596 echo "${size}"
599 load_keymap ()
601 # Load custom keymap
602 if [ -x /bin/loadkeys -a -r /etc/boottime.kmap.gz ]
603 then
604 loadkeys /etc/boottime.kmap.gz
608 setup_loop ()
610 local fspath=${1}
611 local module=${2}
612 local pattern=${3}
613 local offset=${4}
614 local encryption=${5}
615 local readonly=${6}
617 # the output of setup_loop is evaluated in other functions,
618 # modprobe leaks kernel options like "libata.dma=0"
619 # as "options libata dma=0" on stdout, causing serious
620 # problems therefor, so instead always avoid output to stdout
621 modprobe -q -b "${module}" 1>/dev/null
623 udevadm settle
625 for loopdev in ${pattern}
627 if [ "$(cat ${loopdev}/size)" -eq 0 ]
628 then
629 dev=$(sys2dev "${loopdev}")
630 options=''
632 if [ -n "${readonly}" ]
633 then
634 if losetup --help 2>&1 | grep -q -- "-r\b"
635 then
636 options="${options} -r"
640 if [ -n "${offset}" ] && [ 0 -lt "${offset}" ]
641 then
642 options="${options} -o ${offset}"
645 if [ -z "${encryption}" ]
646 then
647 losetup ${options} "${dev}" "${fspath}"
648 else
649 # Loop AES encryption
650 while true
652 load_keymap
654 echo -n "Enter passphrase for root filesystem: " >&6
655 read -s passphrase
656 echo "${passphrase}" > /tmp/passphrase
657 unset passphrase
658 exec 9</tmp/passphrase
659 /sbin/losetup ${options} -e "${encryption}" -p 9 "${dev}" "${fspath}"
660 error=${?}
661 exec 9<&-
662 rm -f /tmp/passphrase
664 if [ 0 -eq ${error} ]
665 then
666 unset error
667 break
670 echo
671 echo -n "There was an error decrypting the root filesystem ... Retry? [Y/n] " >&6
672 read answer
674 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
675 then
676 unset answer
677 break
679 done
682 echo "${dev}"
683 return 0
685 done
687 panic "No loop devices available"
690 try_mount ()
692 dev="${1}"
693 mountp="${2}"
694 opts="${3}"
695 fstype="${4}"
697 old_mountp="$(where_is_mounted ${dev})"
699 if [ -n "${old_mountp}" ]
700 then
701 if [ "${opts}" != "ro" ]
702 then
703 mount -o remount,"${opts}" "${dev}" "${old_mountp}" || panic "Remounting ${dev} ${opts} on ${old_mountp} failed"
706 mount -o bind "${old_mountp}" "${mountp}" || panic "Cannot bind-mount ${old_mountp} on ${mountp}"
707 else
708 if [ -z "${fstype}" ]
709 then
710 fstype=$(get_fstype "${dev}")
712 mount -t "${fstype}" -o "${opts}" "${dev}" "${mountp}" || \
713 ( echo "SKIPPING: Cannot mount ${dev} on ${mountp}, fstype=${fstype}, options=${opts}" > boot.log && return 0 )
717 mount_persistence_media ()
719 local device=${1}
720 local probe=${2}
722 local backing="/live/persistence/$(basename ${device})"
724 mkdir -p "${backing}"
725 local old_backing="$(where_is_mounted ${device})"
726 if [ -z "${old_backing}" ]
727 then
728 local fstype="$(get_fstype ${device})"
729 local mount_opts="rw,noatime"
730 if [ -n "${PERSISTENCE_READONLY}" ]
731 then
732 mount_opts="ro,noatime"
734 if mount -t "${fstype}" -o "${mount_opts}" "${device}" "${backing}" >/dev/null
735 then
736 echo ${backing}
737 return 0
738 else
739 [ -z "${probe}" ] && log_warning_msg "Failed to mount persistence media ${device}"
740 rmdir "${backing}"
741 return 1
743 elif [ "${backing}" != "${old_backing}" ]
744 then
745 if mount --move ${old_backing} ${backing} >/dev/null
746 then
747 echo ${backing}
748 return 0
749 else
750 [ -z "${probe}" ] && log_warning_msg "Failed to move persistence media ${device}"
751 rmdir "${backing}"
752 return 1
755 return 0
758 close_persistence_media ()
760 local device=${1}
761 local backing="$(where_is_mounted ${device})"
763 if [ -d "${backing}" ]
764 then
765 umount "${backing}" >/dev/null 2>&1
766 rmdir "${backing}" >/dev/null 2>&1
769 if is_active_luks_mapping ${device}
770 then
771 /sbin/cryptsetup luksClose ${device}
775 open_luks_device ()
777 dev="${1}"
778 name="$(basename ${dev})"
779 opts="--key-file=-"
780 if [ -n "${PERSISTENCE_READONLY}" ]
781 then
782 opts="${opts} --readonly"
785 if /sbin/cryptsetup status "${name}" >/dev/null 2>&1
786 then
787 re="^[[:space:]]*device:[[:space:]]*\([^[:space:]]*\)$"
788 opened_dev=$(cryptsetup status ${name} 2>/dev/null | grep "${re}" | sed "s|${re}|\1|")
789 if [ "${opened_dev}" = "${dev}" ]
790 then
791 luks_device="/dev/mapper/${name}"
792 echo ${luks_device}
793 return 0
794 else
795 log_warning_msg "Cannot open luks device ${dev} since ${opened_dev} already is opened with its name"
796 return 1
800 load_keymap
802 while true
804 /lib/cryptsetup/askpass "Enter passphrase for ${dev}: " | \
805 /sbin/cryptsetup -T 1 luksOpen ${dev} ${name} ${opts}
807 if [ 0 -eq ${?} ]
808 then
809 luks_device="/dev/mapper/${name}"
810 echo ${luks_device}
811 return 0
814 echo >&6
815 echo -n "There was an error decrypting ${dev} ... Retry? [Y/n] " >&6
816 read answer
818 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
819 then
820 return 2
822 done
825 get_gpt_name ()
827 local dev="${1}"
828 /sbin/blkid -s PART_ENTRY_NAME -p -o value ${dev} 2>/dev/null
831 is_gpt_device ()
833 local dev="${1}"
834 [ "$(/sbin/blkid -s PART_ENTRY_SCHEME -p -o value ${dev} 2>/dev/null)" = "gpt" ]
837 probe_for_gpt_name ()
839 local overlays="${1}"
840 local dev="${2}"
842 local gpt_dev="${dev}"
843 if is_active_luks_mapping ${dev}
844 then
845 # if $dev is an opened luks device, we need to check
846 # GPT stuff on the backing device
847 gpt_dev=$(get_luks_backing_device "${dev}")
850 if ! is_gpt_device ${gpt_dev}
851 then
852 return
855 local gpt_name=$(get_gpt_name ${gpt_dev})
856 for label in ${overlays}
858 if [ "${gpt_name}" = "${label}" ]
859 then
860 echo "${label}=${dev}"
862 done
865 probe_for_fs_label ()
867 local overlays="${1}"
868 local dev="${2}"
870 for label in ${overlays}
872 if [ "$(/sbin/blkid -s LABEL -o value $dev 2>/dev/null)" = "${label}" ]
873 then
874 echo "${label}=${dev}"
876 done
879 probe_for_file_name ()
881 local overlays="${1}"
882 local dev="${2}"
884 local ret=""
885 local 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=$(setup_loop "${path}" "loop" "/sys/block/loop*")
897 ret="${ret} ${label}=${loopdev}"
899 done
901 if [ -n "${ret}" ]
902 then
903 echo ${ret}
904 else
905 # unmount and remove mountpoint
906 umount ${backing} > /dev/null 2>&1 || true
907 rmdir ${backing} > /dev/null 2>&1 || true
911 find_persistence_media ()
913 # Scans devices for overlays, and returns a whitespace
914 # separated list of how to use them. Only overlays with a partition
915 # label or file name in ${overlays} are returned.
917 # When scanning a LUKS device, the user will be asked to enter the
918 # passphrase; on failure to enter it, or if no persistence partitions
919 # or files were found, the LUKS device is closed.
921 # For all other cases (overlay partition and overlay file) the
922 # return value is "${label}=${device}", where ${device} a device that
923 # can mount the content. In the case of an overlay file, the device
924 # containing the file will remain mounted as a side-effect.
926 # No devices in ${black_listed_devices} will be scanned, and if
927 # ${white_list_devices} is non-empty, only devices in it will be
928 # scanned.
930 local overlays="${1}"
931 local white_listed_devices="${2}"
932 local ret=""
934 local black_listed_devices="$(what_is_mounted_on /live/medium)"
936 for dev in $(storage_devices "${black_listed_devices}" "${white_listed_devices}")
938 local result=""
940 local luks_device=""
941 # Check if it's a luks device; we'll have to open the device
942 # in order to probe any filesystem it contains, like we do
943 # below. activate_custom_mounts() also depends on that any luks
944 # device already has been opened.
945 if is_in_comma_sep_list luks ${PERSISTENCE_ENCRYPTION} && is_luks_partition ${dev}
946 then
947 if luks_device=$(open_luks_device "${dev}")
948 then
949 dev="${luks_device}"
950 else
951 # skip $dev since we failed/chose not to open it
952 continue
954 elif ! is_in_comma_sep_list none ${PERSISTENCE_ENCRYPTION}
955 then
956 # skip $dev since we don't allow unencrypted storage
957 continue
960 # Probe for matching GPT partition names or filesystem labels
961 if is_in_comma_sep_list filesystem ${PERSISTENCE_STORAGE}
962 then
963 result=$(probe_for_gpt_name "${overlays}" ${dev})
964 if [ -n "${result}" ]
965 then
966 ret="${ret} ${result}"
967 continue
970 result=$(probe_for_fs_label "${overlays}" ${dev})
971 if [ -n "${result}" ]
972 then
973 ret="${ret} ${result}"
974 continue
978 # Probe for files with matching name on mounted partition
979 if is_in_comma_sep_list file ${PERSISTENCE_STORAGE}
980 then
981 result=$(probe_for_file_name "${overlays}" ${dev})
982 if [ -n "${result}" ]
983 then
984 ret="${ret} ${result}"
985 continue
989 # Close luks device if it isn't used
990 if [ -z "${result}" ] && [ -n "${luks_device}" ] && is_active_luks_mapping "${luks_device}"
991 then
992 /sbin/cryptsetup luksClose "${luks_device}"
994 done
996 if [ -n "${ret}" ]
997 then
998 echo ${ret}
1002 get_mac ()
1004 mac=""
1006 for adaptor in /sys/class/net/*
1008 status="$(cat ${adaptor}/iflink)"
1010 if [ "${status}" -eq 2 ]
1011 then
1012 mac="$(cat ${adaptor}/address)"
1013 mac="$(echo ${mac} | sed 's/:/-/g' | tr '[a-z]' '[A-Z]')"
1015 done
1017 echo ${mac}
1020 is_luks_partition ()
1022 device="${1}"
1023 /sbin/cryptsetup isLuks "${device}" 1>/dev/null 2>&1
1026 is_active_luks_mapping ()
1028 device="${1}"
1029 /sbin/cryptsetup status "${device}" 1>/dev/null 2>&1
1032 get_luks_backing_device ()
1034 device=${1}
1035 cryptsetup status ${device} 2> /dev/null | \
1036 awk '{if ($1 == "device:") print $2}'
1039 removable_dev ()
1041 output_format="${1}"
1042 want_usb="${2}"
1043 ret=
1045 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1047 dev_ok=
1048 if [ "$(cat ${sysblock}/removable)" = "1" ]
1049 then
1050 if [ -z "${want_usb}" ]
1051 then
1052 dev_ok="true"
1053 else
1054 if readlink ${sysblock} | grep -q usb
1055 then
1056 dev_ok="true"
1061 if [ "${dev_ok}" = "true" ]
1062 then
1063 case "${output_format}" in
1064 sys)
1065 ret="${ret} ${sysblock}"
1068 devname=$(sys2dev "${sysblock}")
1069 ret="${ret} ${devname}"
1071 esac
1073 done
1075 echo "${ret}"
1078 removable_usb_dev ()
1080 output_format="${1}"
1082 removable_dev "${output_format}" "want_usb"
1085 non_removable_dev ()
1087 output_format="${1}"
1088 ret=
1090 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1092 if [ "$(cat ${sysblock}/removable)" = "0" ]
1093 then
1094 case "${output_format}" in
1095 sys)
1096 ret="${ret} ${sysblock}"
1099 devname=$(sys2dev "${sysblock}")
1100 ret="${ret} ${devname}"
1102 esac
1104 done
1106 echo "${ret}"
1109 link_files ()
1111 # create source's directory structure in dest, and recursively
1112 # create symlinks in dest to to all files in source. if mask
1113 # is non-empty, remove mask from all source paths when
1114 # creating links (will be necessary if we change root, which
1115 # live-boot normally does (into $rootmnt)).
1117 # remove multiple /:s and ensure ending on /
1118 local src_dir="$(trim_path ${1})/"
1119 local dest_dir="$(trim_path ${2})/"
1120 local src_mask="${3}"
1122 # This check can only trigger on the inital, non-recursive call since
1123 # we create the destination before recursive calls
1124 if [ ! -d "${dest_dir}" ]
1125 then
1126 log_warning_msg "Must link_files into a directory"
1127 return
1130 find "${src_dir}" -mindepth 1 -maxdepth 1 | \
1131 while read src
1133 local dest="${dest_dir}$(basename "${src}")"
1134 if [ -d "${src}" ]
1135 then
1136 if [ -z "$(ls -A "${src}")" ]
1137 then
1138 continue
1140 if [ ! -d "${dest}" ]
1141 then
1142 mkdir -p "${dest}"
1143 chown_ref "${src}" "${dest}"
1144 chmod_ref "${src}" "${dest}"
1146 link_files "${src}" "${dest}" "${src_mask}"
1147 else
1148 local final_src=${src}
1149 if [ -n "${src_mask}" ]
1150 then
1151 final_src="$(echo ${final_src} | sed "s|^${src_mask}||")"
1153 rm -rf "${dest}" 2> /dev/null
1154 ln -s "${final_src}" "${dest}"
1155 chown_ref "${src}" "${dest}"
1157 done
1160 do_union ()
1162 local unionmountpoint="${1}" # directory where the union is mounted
1163 local unionrw="${2}" # branch where the union changes are stored
1164 local unionro1="${3}" # first underlying read-only branch (optional)
1165 local unionro2="${4}" # second underlying read-only branch (optional)
1167 case "${UNIONTYPE}" in
1168 aufs)
1169 rw_opt="rw"
1170 ro_opt="rr+wh"
1171 noxino_opt="noxino"
1174 unionfs-fuse)
1175 rw_opt="RW"
1176 ro_opt="RO"
1180 rw_opt="rw"
1181 ro_opt="ro"
1183 esac
1185 case "${UNIONTYPE}" in
1186 unionfs-fuse)
1187 unionmountopts="-o cow -o noinitgroups -o default_permissions -o allow_other -o use_ino -o suid"
1188 unionmountopts="${unionmountopts} ${unionrw}=${rw_opt}"
1189 if [ -n "${unionro1}" ]
1190 then
1191 unionmountopts="${unionmountopts}:${unionro1}=${ro_opt}"
1193 if [ -n "${unionro2}" ]
1194 then
1195 unionmountopts="${unionmountopts}:${unionro2}=${ro_opt}"
1197 ( sysctl -w fs.file-max=391524 ; ulimit -HSn 16384
1198 unionfs-fuse ${unionmountopts} "${unionmountpoint}" ) && \
1199 ( mkdir -p /run/sendsigs.omit.d
1200 pidof unionfs-fuse >> /run/sendsigs.omit.d/unionfs-fuse || true )
1203 overlayfs)
1204 # XXX: can unionro2 be used? (overlayfs only handles two dirs, but perhaps they can be chained?)
1205 # XXX: and can unionro1 be optional? i.e. can overlayfs skip lowerdir?
1206 unionmountopts="-o noatime,lowerdir=${unionro1},upperdir=${unionrw}"
1207 mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
1211 unionmountopts="-o noatime,${noxino_opt},dirs=${unionrw}=${rw_opt}"
1212 if [ -n "${unionro1}" ]
1213 then
1214 unionmountopts="${unionmountopts}:${unionro1}=${ro_opt}"
1216 if [ -n "${unionro2}" ]
1217 then
1218 unionmountopts="${unionmountopts}:${unionro2}=${ro_opt}"
1220 mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
1222 esac
1225 get_custom_mounts ()
1227 # Side-effect: leaves $devices with persistence.conf mounted in /live/persistence
1228 # Side-effect: prints info to file $custom_mounts
1230 local custom_mounts=${1}
1231 shift
1232 local devices=${@}
1234 local bindings="/tmp/bindings.list"
1235 local links="/tmp/links.list"
1236 rm -rf ${bindings} ${links} 2> /dev/null
1238 for device in ${devices}
1240 if [ ! -b "${device}" ]
1241 then
1242 continue
1245 local device_name="$(basename ${device})"
1246 local backing=$(mount_persistence_media ${device})
1247 if [ -z "${backing}" ]
1248 then
1249 continue
1252 local include_list
1253 if [ -r "${backing}/${persistence_list}" ]
1254 then
1255 include_list="${backing}/${persistence_list}"
1256 elif [ -r "${backing}/${old_persistence_list}" ]
1257 then
1258 include_list="${backing}/${old_persistence_list}"
1259 else
1260 continue
1263 if [ -n "${DEBUG}" ] && [ -e "${include_list}" ]
1264 then
1265 cp ${include_list} /live/persistence/${persistence_list}.${device_name}
1268 while read dir options # < ${include_list}
1270 if echo ${dir} | grep -qe "^[[:space:]]*\(#.*\)\?$"
1271 then
1272 # skipping empty or commented lines
1273 continue
1276 if trim_path ${dir} | grep -q -e "^[^/]" -e "^/live\(/.*\)\?$" -e "^/\(.*/\)\?\.\.\?\(/.*\)\?$"
1277 then
1278 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."
1279 continue
1282 local opt_source=""
1283 local opt_link=""
1284 for opt in $(echo ${options} | tr ',' ' ');
1286 case "${opt}" in
1287 source=*)
1288 opt_source=${opt#source=}
1290 link)
1291 opt_link="true"
1293 union|bind)
1296 log_warning_msg "Skipping custom mount with unkown option: ${opt}"
1297 continue 2
1299 esac
1300 done
1302 local source="${dir}"
1303 if [ -n "${opt_source}" ]
1304 then
1305 if echo ${opt_source} | grep -q -e "^/" -e "^\(.*/\)\?\.\.\?\(/.*\)\?$" && [ "${opt_source}" != "." ]
1306 then
1307 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"
1308 continue
1309 else
1310 source="${opt_source}"
1314 local full_source="$(trim_path ${backing}/${source})"
1315 local full_dest="$(trim_path ${rootmnt}/${dir})"
1316 if [ -n "${opt_link}" ]
1317 then
1318 echo "${device} ${full_source} ${full_dest} ${options}" >> ${links}
1319 else
1320 echo "${device} ${full_source} ${full_dest} ${options}" >> ${bindings}
1322 done < ${include_list}
1323 done
1325 # We sort the list according to destination so we're sure that
1326 # we won't hide a previous mount. We also ignore duplicate
1327 # destinations in a more or less arbitrary way.
1328 [ -e "${bindings}" ] && sort -k3 -sbu ${bindings} >> ${custom_mounts} && rm ${bindings}
1330 # After all mounts are considered we add symlinks so they
1331 # won't be hidden by some mount.
1332 [ -e "${links}" ] && cat ${links} >> ${custom_mounts} && rm ${links}
1334 # We need to make sure that no two custom mounts have the same sources
1335 # or are nested; if that is the case, too much weird stuff can happen.
1336 local prev_source="impossible source" # first iteration must not match
1337 local prev_dest=""
1338 # This sort will ensure that a source /a comes right before a source
1339 # /a/b so we only need to look at the previous source
1340 sort -k2 -b ${custom_mounts} |
1341 while read device source dest options
1343 if echo ${source} | grep -qe "^${prev_source}\(/.*\)\?$"
1344 then
1345 panic "Two persistence mounts have the same or nested sources: ${source} on ${dest}, and ${prev_source} on ${prev_dest}"
1347 prev_source=${source}
1348 prev_dest=${dest}
1349 done
1352 activate_custom_mounts ()
1354 local custom_mounts="${1}" # the ouput from get_custom_mounts()
1355 local used_devices=""
1357 while read device source dest options # < ${custom_mounts}
1359 local opt_bind="true"
1360 local opt_link=""
1361 local opt_union=""
1362 for opt in $(echo ${options} | tr ',' ' ');
1364 case "${opt}" in
1365 bind)
1366 opt_bind="true"
1367 unset opt_link opt_union
1369 link)
1370 opt_link="true"
1371 unset opt_bind opt_union
1373 union)
1374 opt_union="true"
1375 unset opt_bind opt_link
1377 esac
1378 done
1380 if [ -n "$(what_is_mounted_on "${dest}")" ]
1381 then
1382 if [ "${dest}" = "${rootmnt}" ]
1383 then
1384 umount "${dest}"
1385 else
1386 log_warning_msg "Skipping custom mount ${dest}: $(what_is_mounted_on "${dest}") is already mounted there"
1387 continue
1391 if [ ! -d "${dest}" ]
1392 then
1393 # create the destination and delete existing files in
1394 # its path that are in the way
1395 path="/"
1396 for dir in $(echo ${dest} | sed -e 's|/\+| |g')
1398 path=$(trim_path ${path}/${dir})
1399 if [ -f ${path} ]
1400 then
1401 rm -f ${path}
1403 if [ ! -e ${path} ]
1404 then
1405 mkdir -p ${path}
1406 if echo ${path} | grep -qe "^${rootmnt}/*home/[^/]\+"
1407 then
1408 # if ${dest} is in /home try fixing proper ownership by assuming that the intended user is the first, which is usually the case
1409 # FIXME: this should really be handled by live-config since we don't know for sure which uid a certain user has until then
1410 chown 1000:1000 ${path}
1413 done
1416 # if ${source} doesn't exist on our persistence media
1417 # we bootstrap it with $dest from the live filesystem.
1418 # this both makes sense and is critical if we're
1419 # dealing with /etc or other system dir.
1420 if [ ! -d "${source}" ]
1421 then
1422 if [ -n "${PERSISTENCE_READONLY}" ]
1423 then
1424 continue
1425 elif [ -n "${opt_union}" ] || [ -n "${opt_link}" ]
1426 then
1427 # unions and don't need to be bootstrapped
1428 # link dirs can't be bootstrapped in a sensible way
1429 mkdir -p "${source}"
1430 chown_ref "${dest}" "${source}"
1431 chmod_ref "${dest}" "${source}"
1432 elif [ -n "${opt_bind}" ]
1433 then
1434 # ensure that $dest is not copied *into* $source
1435 mkdir -p "$(dirname ${source})"
1436 cp -a "${dest}" "${source}"
1440 # XXX: If CONFIG_AUFS_ROBR is added to the Debian kernel we can
1441 # ignore the loop below and set rootfs_dest_backing=$dest
1442 local rootfs_dest_backing=""
1443 if [ -n "${opt_link}"]
1444 then
1445 for d in /live/rootfs/*
1447 if [ -n "${rootmnt}" ]
1448 then
1449 rootfs_dest_backing="${d}/$(echo ${dest} | sed -e "s|${rootmnt}||")"
1450 else
1451 rootfs_dest_backing="${d}/${dest}"
1453 if [ -d "${rootfs_dest_backing}" ]
1454 then
1455 break
1456 else
1457 rootfs_dest_backing=""
1459 done
1462 if [ -n "${opt_link}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1463 then
1464 link_files ${source} ${dest} ${rootmnt}
1465 elif [ -n "${opt_link}" ] && [ -n "${PERSISTENCE_READONLY}" ]
1466 then
1467 mkdir -p /live/persistence
1468 local links_source=$(mktemp -d /live/persistence/links-source-XXXXXX)
1469 chown_ref ${source} ${links_source}
1470 chmod_ref ${source} ${links_source}
1471 # We put the cow dir in the below strange place to
1472 # make it absolutely certain that the link source
1473 # has its own directory and isn't nested with some
1474 # other custom mount (if so that mount's files would
1475 # be linked, causing breakage.
1476 local cow_dir="/live/overlay/live/persistence/$(basename ${links_source})"
1477 mkdir -p ${cow_dir}
1478 chown_ref "${source}" "${cow_dir}"
1479 chmod_ref "${source}" "${cow_dir}"
1480 do_union ${links_source} ${cow_dir} ${source} ${rootfs_dest_backing}
1481 link_files ${links_source} ${dest} ${rootmnt}
1482 elif [ -n "${opt_union}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1483 then
1484 do_union ${dest} ${source} ${rootfs_dest_backing}
1485 elif [ -n "${opt_bind}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1486 then
1487 mount --bind "${source}" "${dest}"
1488 elif [ -n "${opt_bind}" -o -n "${opt_union}" ] && [ -n "${PERSISTENCE_READONLY}" ]
1489 then
1490 # bind-mount and union mount are handled the same
1491 # in read-only mode, but note that rootfs_dest_backing
1492 # is non-empty (and necessary) only for unions
1493 if [ -n "${rootmnt}" ]
1494 then
1495 local cow_dir="$(echo ${dest} | sed -e "s|^${rootmnt}|/live/overlay/|")"
1496 else
1497 # This is happens if persistence is activated
1498 # post boot
1499 local cow_dir="/live/overlay/${dest}"
1501 if [ -e "${cow_dir}" ] && [ -z "${opt_link}" ]
1502 then
1503 # If an earlier custom mount has files here
1504 # it will "block" the current mount's files
1505 # which is undesirable
1506 rm -rf "${cow_dir}"
1508 mkdir -p ${cow_dir}
1509 chown_ref "${source}" "${cow_dir}"
1510 chmod_ref "${source}" "${cow_dir}"
1511 do_union ${dest} ${cow_dir} ${source} ${rootfs_dest_backing}
1514 PERSISTENCE_IS_ON="1"
1515 export PERSISTENCE_IS_ON
1517 if echo ${used_devices} | grep -qve "^\(.* \)\?${device}\( .*\)\?$"
1518 then
1519 used_devices="${used_devices} ${device}"
1521 done < ${custom_mounts}
1523 echo ${used_devices}
1526 is_mountpoint ()
1528 directory="$1"
1530 [ $(stat -fc%d:%D "${directory}") != $(stat -fc%d:%D "${directory}/..") ]