From 471fdebfdea843648acb5abeacf5ae559c3ad77a Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Mon, 22 Aug 2022 13:36:54 +0900 Subject: [PATCH 1/2] hostname: make chassis type actually obtained from ACPI when nothing from DMI Fixes a bug introduced by 8c8b1800e90d4307397300ef32b0f6d95efad057. Fixes #24384. --- src/hostname/hostnamed.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hostname/hostnamed.c b/src/hostname/hostnamed.c index 5f09e6d0eb5..8c0b63503fa 100644 --- a/src/hostname/hostnamed.c +++ b/src/hostname/hostnamed.c @@ -415,7 +415,7 @@ static char* context_get_chassis(Context *c) { if (!isempty(c->data[PROP_CHASSIS])) return strdup(c->data[PROP_CHASSIS]); - if (get_dmi_data("ID_CHASSIS", NULL, &dmi) >= 0) + if (get_dmi_data("ID_CHASSIS", NULL, &dmi) > 0) return dmi; fallback = fallback_chassis(); From 1283eea8f4298fd8749db4906129c83ec1b1ae2e Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Mon, 22 Aug 2022 14:05:22 +0900 Subject: [PATCH 2/2] test: add test case for chassis type --- test/units/testsuite-71.sh | 46 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/test/units/testsuite-71.sh b/test/units/testsuite-71.sh index 34fcaad961d..2382cccb3a0 100755 --- a/test/units/testsuite-71.sh +++ b/test/units/testsuite-71.sh @@ -44,9 +44,55 @@ test_hostname() { fi } +restore_machine_info() { + if [[ -e /tmp/machine-info.bak ]]; then + mv /tmp/machine-info.bak /etc/machine-info + else + rm -f /etc/machine-info + fi +} + +get_chassis() ( + # shellcheck source=/dev/null + . /etc/machine-info + + echo "$CHASSIS" +) + +test_chassis() { + local i + + if [[ -f /etc/machine-info ]]; then + cp /etc/machine-info /tmp/machine-info.bak + fi + + trap restore_machine_info RETURN + + # Invalid chassis type is refused + assert_rc 1 hostnamectl chassis hoge + + # Valid chassis types + for i in vm container desktop laptop convertible server tablet handset watch embedded; do + hostnamectl chassis "$i" + assert_eq "$(hostnamectl chassis)" "$i" + assert_eq "$(get_chassis)" "$i" + done + + systemctl stop systemd-hostnamed.service + rm -f /etc/machine-info + + # fallback chassis type + if systemd-detect-virt --quiet --container; then + assert_eq "$(hostnamectl chassis)" container + elif systemd-detect-virt --quiet --vm; then + assert_eq "$(hostnamectl chassis)" vm + fi +} + : >/failed test_hostname +test_chassis touch /testok rm /failed