Skip to content

Commit

Permalink
fix(ds-identify): relocate /run on *BSD (canonical#4677)
Browse files Browse the repository at this point in the history
shuffle code-paths around such that we initialize RUN_PATH, and all the
things that depend on it after figuring out which OS we're on, while
still alowing these global variables to be overwritten from the outside.
On BSD /run is not ephemeral, so here we set RUN_PATH to /var/run.
Ensure everything is initialized for 'print_info', not just for 'main'.

For the tests, we equally ensure that for not-Linux systems the data is
written into `rootd + var/run` instead of `rootd + run`.

Sponsored by: The FreeBSD Foundation
  • Loading branch information
igalic committed Jan 4, 2024
1 parent d24dd5d commit 0486c63
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 14 deletions.
24 changes: 16 additions & 8 deletions tests/unittests/test_ds_identify.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,14 @@
populate_dir_with_ts,
)

UNAME_MYSYS = (
"Linux 4.4.0-62-generic #83-Ubuntu SMP Wed Jan 18 14:10:15 UTC 2017 x86_64"
)
UNAME_MYSYS = "Linux #83-Ubuntu SMP Wed Jan 18 14:10:15 UTC 2017 x86_64"
UNAME_PPC64EL = (
"Linux 4.4.0-83-generic #106-Ubuntu SMP "
"Mon Jun 26 17:53:54 UTC 2017 "
"Linux #106-Ubuntu SMP mon Jun 26 17:53:54 UTC 2017 "
"ppc64le ppc64le ppc64le"
)
UNAME_FREEBSD = (
"FreeBSD 12.1-RELEASE-p10 FreeBSD 12.1-RELEASE-p10 GENERIC amd64"
"FreeBSD FreeBSD 14.0-RELEASE-p3 releng/14.0-n265398-20fae1e1699"
"GENERIC-MMCCAM amd64"
)
UNAME_OPENBSD = "OpenBSD GENERIC.MP#1397 amd64"

Expand Down Expand Up @@ -340,7 +338,17 @@ def write_mock(data):
},
]

written = [d["name"] for d in mocks]
uname = "Linux"
runpath = "run"
written = []
for d in mocks:
written.append(d["name"])
if d["name"] == "uname":
uname = d["out"].split(" ")[0]
# set runpath so that BSDs use /var/run rather than /run
if uname != "Linux":
runpath = "var/run"

for data in mocks:
mocklines.append(write_mock(data))
for d in default_mocks:
Expand All @@ -363,7 +371,7 @@ def write_mock(data):
err = e.stderr

cfg = None
cfg_out = os.path.join(rootd, "run/cloud-init/cloud.cfg")
cfg_out = os.path.join(rootd, runpath, "cloud-init/cloud.cfg")
if os.path.exists(cfg_out):
contents = util.load_file(cfg_out)
try:
Expand Down
36 changes: 30 additions & 6 deletions tools/ds-identify
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ DI_DISABLED="disabled"
DI_DEBUG_LEVEL="${DEBUG_LEVEL:-1}"

PATH_ROOT=${PATH_ROOT:-""}
PATH_RUN=${PATH_RUN:-"${PATH_ROOT}/run"}
PATH_SYS_CLASS_DMI_ID=${PATH_SYS_CLASS_DMI_ID:-${PATH_ROOT}/sys/class/dmi/id}
PATH_SYS_HYPERVISOR=${PATH_SYS_HYPERVISOR:-${PATH_ROOT}/sys/hypervisor}
PATH_SYS_CLASS_BLOCK=${PATH_SYS_CLASS_BLOCK:-${PATH_ROOT}/sys/class/block}
Expand All @@ -82,11 +81,17 @@ PATH_PROC_UPTIME=${PATH_PROC_UPTIME:-${PATH_ROOT}/proc/uptime}
PATH_ETC_CLOUD="${PATH_ETC_CLOUD:-${PATH_ROOT}/etc/cloud}"
PATH_ETC_CI_CFG="${PATH_ETC_CI_CFG:-${PATH_ETC_CLOUD}/cloud.cfg}"
PATH_ETC_CI_CFG_D="${PATH_ETC_CI_CFG_D:-${PATH_ETC_CI_CFG}.d}"
PATH_RUN_CI="${PATH_RUN_CI:-${PATH_RUN}/cloud-init}"
PATH_RUN_CI_CFG=${PATH_RUN_CI_CFG:-${PATH_RUN_CI}/cloud.cfg}
PATH_RUN_DI_RESULT=${PATH_RUN_DI_RESULT:-${PATH_RUN_CI}/.ds-identify.result}

DI_LOG="${DI_LOG:-${PATH_RUN_CI}/ds-identify.log}"
# Declare global here, so they can be overwritten from the outside.
# if not overwritten from the outside, we'll populate them with system default
# paths, once we have determined the system we're running on.
# This is done in set_run_path(), which must run after read_uname_info().
PATH_RUN="${PATH_RUN:-}"
PATH_RUN_CI="${PATH_RUN_CI:-}"
PATH_RUN_CI_CFG="${PATH_RUN_CI_CFG:-}"
PATH_RUN_DI_RESULT="${PATH_RUN_DI_RESULT:-}"

DI_LOG="${DI_LOG:-}"
_DI_LOGGED=""

# set DI_MAIN='noop' in environment to source this file with no main called.
Expand Down Expand Up @@ -1579,6 +1584,9 @@ collect_info() {
}

print_info() {
DI_LOG=stderr
ensure_sane_path
read_uname_info
collect_info
_print_info
}
Expand Down Expand Up @@ -1835,12 +1843,25 @@ read_uptime() {
return
}

set_run_path() {
if [ "$DI_UNAME_KERNEL_NAME" = "Linux" ]; then
PATH_RUN=${PATH_RUN:-"${PATH_ROOT}/run"}
else
PATH_RUN=${PATH_RUN:-"${PATH_ROOT}/var/run"}
fi

PATH_RUN_CI="${PATH_RUN_CI:-${PATH_RUN}/cloud-init}"
PATH_RUN_CI_CFG=${PATH_RUN_CI_CFG:-${PATH_RUN_CI}/cloud.cfg}
PATH_RUN_DI_RESULT=${PATH_RUN_DI_RESULT:-${PATH_RUN_CI}/.ds-identify.result}

DI_LOG="${DI_LOG:-${PATH_RUN_CI}/ds-identify.log}"
}

_main() {
local dscheck_fn="" ret_dis=1 ret_en=0

read_uptime
debug 1 "[up ${_RET}s]" "ds-identify $*"
read_uname_info
read_virt
read_kernel_cmdline
if is_disabled; then
Expand Down Expand Up @@ -1963,6 +1984,9 @@ _main() {
main() {
local ret=""
ensure_sane_path
read_uname_info
set_run_path

[ -d "$PATH_RUN_CI" ] || mkdir -p "$PATH_RUN_CI"
if [ "${1:+$1}" != "--force" ] && [ -f "$PATH_RUN_CI_CFG" ] &&
[ -f "$PATH_RUN_DI_RESULT" ]; then
Expand Down

0 comments on commit 0486c63

Please sign in to comment.