Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(RHEL-1087) test: backport TEST-81-GENERATORS (fstab-generator only) #417

Merged
merged 3 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/cryptsetup/cryptsetup.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "device-util.h"
#include "escape.h"
#include "fileio.h"
#include "fstab-util.h"
#include "log.h"
#include "mount-util.h"
#include "parse-util.h"
Expand Down Expand Up @@ -318,7 +319,7 @@ static char *disk_mount_point(const char *label) {
if (asprintf(&device, "/dev/mapper/%s", label) < 0)
return NULL;

f = setmntent("/etc/fstab", "re");
f = setmntent(fstab_path(), "re");
if (!f)
return NULL;

Expand Down
32 changes: 19 additions & 13 deletions src/fstab-generator/fstab-generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,17 @@ static int add_swap(
if (r < 0)
return log_error_errno(r, "Failed to generate unit name: %m");

r = generator_open_unit_file(arg_dest, "/etc/fstab", name, &f);
r = generator_open_unit_file(arg_dest, fstab_path(), name, &f);
if (r < 0)
return r;

fputs("# Automatically generated by systemd-fstab-generator\n\n"
"[Unit]\n"
"SourcePath=/etc/fstab\n"
"Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n\n"
"[Swap]\n", f);
fprintf(f,
"# Automatically generated by systemd-fstab-generator\n\n"
"[Unit]\n"
"SourcePath=%s\n"
"Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n\n"
"[Swap]\n",
fstab_path());

r = write_what(f, what);
if (r < 0)
Expand Down Expand Up @@ -334,7 +336,7 @@ static int add_mount(
if (r < 0)
return log_error_errno(r, "Failed to generate unit name: %m");

r = generator_open_unit_file(dest, "/etc/fstab", name, &f);
r = generator_open_unit_file(dest, fstab_path(), name, &f);
if (r < 0)
return r;

Expand Down Expand Up @@ -451,7 +453,7 @@ static int add_mount(

fclose(f);

r = generator_open_unit_file(dest, "/etc/fstab", automount_name, &f);
r = generator_open_unit_file(dest, fstab_path(), automount_name, &f);
if (r < 0)
return r;

Expand Down Expand Up @@ -501,19 +503,23 @@ static int add_mount(
return 0;
}

static const char *sysroot_fstab_path(void) {
return getenv("SYSTEMD_SYSROOT_FSTAB") ?: "/sysroot/etc/fstab";
}

static int parse_fstab(bool initrd) {
_cleanup_endmntent_ FILE *f = NULL;
const char *fstab_path;
const char *fstab;
struct mntent *me;
int r = 0;

fstab_path = initrd ? "/sysroot/etc/fstab" : "/etc/fstab";
f = setmntent(fstab_path, "re");
fstab = initrd ? sysroot_fstab_path() : fstab_path();
f = setmntent(fstab, "re");
if (!f) {
if (errno == ENOENT)
return 0;

return log_error_errno(errno, "Failed to open %s: %m", fstab_path);
return log_error_errno(errno, "Failed to open %s: %m", fstab);
}

while ((me = getmntent(f))) {
Expand Down Expand Up @@ -592,7 +598,7 @@ static int parse_fstab(bool initrd) {
me->mnt_passno,
makefs*MAKEFS | growfs*GROWFS | noauto*NOAUTO | nofail*NOFAIL | automount*AUTOMOUNT,
post,
fstab_path);
fstab);
}

if (r >= 0 && k < 0)
Expand Down
5 changes: 3 additions & 2 deletions src/remount-fs/remount-fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <unistd.h>

#include "exit-status.h"
#include "fstab-util.h"
#include "log.h"
#include "mount-setup.h"
#include "mount-util.h"
Expand Down Expand Up @@ -39,14 +40,14 @@ int main(int argc, char *argv[]) {

umask(0022);

f = setmntent("/etc/fstab", "re");
f = setmntent(fstab_path(), "re");
if (!f) {
if (errno == ENOENT) {
r = 0;
goto finish;
}

r = log_error_errno(errno, "Failed to open /etc/fstab: %m");
r = log_error_errno(errno, "Failed to open %s: %m", fstab_path());
goto finish;
}

Expand Down
4 changes: 2 additions & 2 deletions src/shared/fstab-util.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ int fstab_has_fstype(const char *fstype) {
_cleanup_endmntent_ FILE *f = NULL;
struct mntent *m;

f = setmntent("/etc/fstab", "re");
f = setmntent(fstab_path(), "re");
if (!f)
return errno == ENOENT ? false : -errno;

Expand All @@ -41,7 +41,7 @@ int fstab_is_mount_point(const char *mount) {
_cleanup_endmntent_ FILE *f = NULL;
struct mntent *m;

f = setmntent("/etc/fstab", "re");
f = setmntent(fstab_path(), "re");
if (!f)
return errno == ENOENT ? false : -errno;

Expand Down
4 changes: 4 additions & 0 deletions src/shared/fstab-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,7 @@ static inline bool fstab_test_yes_no_option(const char *opts, const char *yes_no
}

char *fstab_node_to_udev_node(const char *p);

static inline const char *fstab_path(void) {
return secure_getenv("SYSTEMD_FSTAB") ?: "/etc/fstab";
}
1 change: 1 addition & 0 deletions test/TEST-81-GENERATORS/Makefile
78 changes: 78 additions & 0 deletions test/TEST-81-GENERATORS/generator-utils.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#!/usr/bin/env bash
# SPDX-License-Identifier: LGPL-2.1-or-later

link_endswith() {
[[ -h "${1:?}" && "$(readlink "${1:?}")" =~ ${2:?}$ ]]
}

link_eq() {
[[ -h "${1:?}" && "$(readlink "${1:?}")" == "${2:?}" ]]
}

# Get the value from a 'key=value' assignment
opt_get_arg() {
local arg

IFS="=" read -r _ arg <<< "${1:?}"
test -n "$arg"
echo "$arg"
}

in_initrd() {
[[ "${SYSTEMD_IN_INITRD:-0}" -ne 0 ]]
}

# Check if we're parsing host's fstab in initrd
in_initrd_host() {
in_initrd && [[ "${SYSTEMD_SYSROOT_FSTAB:-/dev/null}" != /dev/null ]]
}

in_container() {
systemd-detect-virt -qc
}

opt_filter() (
set +x
local opt split_options filtered_options

IFS="," read -ra split_options <<< "${1:?}"
for opt in "${split_options[@]}"; do
if [[ "$opt" =~ ${2:?} ]]; then
continue
fi

filtered_options+=("$opt")
done

IFS=","; printf "%s" "${filtered_options[*]}"
)

# Run the given generator $1 with target directory $2 - clean the target
# directory beforehand
run_and_list() {
local generator="${1:?}"
local out_dir="${2:?}"
local environ

# If $PID1_ENVIRON is set temporarily overmount /proc/1/environ with
# a temporary file that contains contents of $PID1_ENVIRON. This is
# necessary in cases where the generator reads the environment through
# getenv_for_pid(1, ...) or similar like getty-generator does.
#
# Note: $PID1_ENVIRON should be a NUL separated list of env assignments
if [[ -n "${PID1_ENVIRON:-}" ]]; then
environ="$(mktemp)"
echo -ne "${PID1_ENVIRON}\0" >"${environ:?}"
mount -v --bind "$environ" /proc/1/environ
fi

rm -fr "${out_dir:?}"/*
mkdir -p "$out_dir"/{normal,early,late}
SYSTEMD_LOG_LEVEL="${SYSTEMD_LOG_LEVEL:-debug}" "$generator" "$out_dir/normal" "$out_dir/early" "$out_dir/late"
ls -lR "$out_dir"

if [[ -n "${environ:-}" ]]; then
umount /proc/1/environ
rm -f "$environ"
fi
}
50 changes: 50 additions & 0 deletions test/TEST-81-GENERATORS/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/env bash
set -e
TEST_DESCRIPTION="Test systemd generators"

# shellcheck source=test/test-functions
. "$TEST_BASE_DIR/test-functions"

test_setup() {
create_empty_image
mkdir -p "${TESTDIR:?}/root"
mount "${LOOPDEV:?}p1" "$TESTDIR/root"

(
LOG_LEVEL=5
# shellcheck disable=SC2046
eval $(udevadm info --export --query=env --name="${LOOPDEV}p2")

setup_basic_environment

# mask some services that we do not want to run in these tests
ln -fs /dev/null "$initdir/etc/systemd/system/systemd-hwdb-update.service"
ln -fs /dev/null "$initdir/etc/systemd/system/systemd-journal-catalog-update.service"
ln -fs /dev/null "$initdir/etc/systemd/system/systemd-networkd.service"
ln -fs /dev/null "$initdir/etc/systemd/system/systemd-networkd.socket"
ln -fs /dev/null "$initdir/etc/systemd/system/systemd-resolved.service"
ln -fs /dev/null "$initdir/etc/systemd/system/systemd-machined.service"

# setup the testsuite service
cat >"$initdir/etc/systemd/system/testsuite.service" <<EOF
[Unit]
Description=Testsuite service

[Service]
ExecStart=/bin/bash -x /testsuite.sh
Type=oneshot
StandardOutput=tty
StandardError=tty
NotifyAccess=all
EOF
cp generator-utils.sh testsuite*.sh "$initdir/"

setup_testsuite
) || return 1
setup_nspawn_root

ddebug "umount $TESTDIR/root"
umount "$TESTDIR/root"
}

do_test "$@"
Loading