2 # SPDX-License-Identifier: LGPL-2.1-or-later
5 export DM_NAME
="integrity_test"
6 export FULL_DM_DEV_NAME
="/dev/mapper/${DM_NAME}"
7 export FS_UUID
="01234567-ffff-eeee-eeee-0123456789ab"
8 export GEN
="/var/run/systemd/generator"
14 if [ -z "${image_dir}" ]; then
18 if [ -f "${image_dir}/image" ]; then
19 if [ -e "${FULL_DM_DEV_NAME}" ]; then
20 integritysetup close
"${DM_NAME}"
32 cat <<EOF >"/etc/integritytab"
33 ${DM_NAME} ${loop} - integrity-algorithm=$1
37 image_dir
="$(mktemp -d -t -p / integrity.tmp.XXXXXX)"
38 if [ -z "${image_dir}" ] ||
[ ! -d "${image_dir}" ]; then
39 echo "mktemp under / failed"
43 dd if=/dev
/zero of
="${image_dir}/image" bs
=1048576 count
=64 ||
exit 1
44 dd if=/dev
/zero of
="${image_dir}/data" bs
=1048576 count
=64 ||
exit 1
45 loop
="$(losetup --show -f "${image_dir}/image
")"
47 if [[ ! -e ${loop} ]]; then
48 echo "Loopback device created not found!"
52 # Do one iteration with a separate data device, to test those branches
55 for algorithm
in crc32c crc32 sha1 sha256
57 if [ "${separate_data}" -eq 1 ]; then
58 data_option
="--data-device=${image_dir}/data"
62 integritysetup format
"${loop}" --batch-mode -I "${algorithm}" "${data_option}" ||
exit 1
63 integritysetup open
-I "${algorithm}" "${loop}" "${DM_NAME}" "${data_option}" ||
exit 1
64 mkfs.ext4
-U "${FS_UUID}" "${FULL_DM_DEV_NAME}" ||
exit 1
66 # Give userspace time to handle udev events for new FS showing up ...
69 integritysetup close
"${DM_NAME}" ||
exit 1
71 # create integritytab, generate units, start service
72 if [ "${separate_data}" -eq 1 ]; then
73 data_option
=",data-device=${image_dir}/data"
77 build_integrity_tab
"${algorithm}${data_option}"
79 # Cause the generator to re-run
80 systemctl daemon-reload ||
exit 1
82 # Check for existence of unit files...
83 if [[ ! -e "/run/systemd/generator/systemd-integritysetup@${DM_NAME}.service" ]]; then
84 echo "Service file does not exist!"
88 # Make sure we are in a consistent state, e.g. not already active before we start
89 systemctl stop systemd-integritysetup@
"${DM_NAME}".service ||
exit 1
90 systemctl start systemd-integritysetup@
"${DM_NAME}".service ||
exit 1
92 # Check the signature on the FS to ensure we can retrieve it and that is matches
93 if [ -e "${FULL_DM_DEV_NAME}" ]; then
94 # If a separate device is used for the metadata storage, then blkid will return one of the loop devices
95 if [ "${separate_data}" -eq 1 ]; then
96 dev_name
="$(integritysetup status ${DM_NAME} | grep '^\s*device:' | awk '{print $2}')"
98 dev_name
="${FULL_DM_DEV_NAME}"
100 if [ "${dev_name}" != "$(blkid -U "${FS_UUID}")" ]; then
101 echo "Failed to locate FS with matching UUID!"
105 echo "Failed to bring up integrity device!"
109 systemctl stop systemd-integritysetup@
"${DM_NAME}".service ||
exit 1
111 if [ -e "${FULL_DM_DEV_NAME}" ]; then
112 echo "Expecting ${FULL_DM_DEV_NAME} to not exist after stopping unit!"