Merge pull request #24385 from yuwata/hostname-chassis

hostname: fix fallback chassis type
This commit is contained in:
Luca Boccassi 2022-08-22 15:40:38 +01:00 committed by GitHub
commit d4001c518a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 1 deletions

View File

@ -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();

View File

@ -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