Skip to content

Commit

Permalink
fix(ds-identify): relocate /run on *BSD
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'.

Sponsored by: The FreeBSD Foundation
  • Loading branch information
igalic committed Dec 13, 2023
1 parent 7aa56ed commit 12bcdfd
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 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,14 @@ 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, we'll set it properly in main()
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 +1581,10 @@ collect_info() {
}

print_info() {
DI_LOG=stderr
ensure_sane_path
read_uname_info
set_run_path
collect_info
_print_info
}
Expand Down Expand Up @@ -1835,12 +1841,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 +1982,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 12bcdfd

Please sign in to comment.