-
Notifications
You must be signed in to change notification settings - Fork 883
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
Move disable checks out of systemd generator #4399
Changes from all commits
e8fda14
2b33a31
f6d784e
8e5a59f
861a878
316e54d
8ab927a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,8 @@ CLOUD_SYSTEM_TARGET="/lib/systemd/system/cloud-init.target" | |
{% if variant in ["almalinux", "centos", "cloudlinux", "eurolinux", "fedora", | ||
"miraclelinux", "openeuler", "OpenCloudOS", "openmandriva", "rhel", "rocky", "TencentOS", "virtuozzo"] %} | ||
dsidentify="/usr/libexec/cloud-init/ds-identify" | ||
{% elif variant == "benchmark" %} | ||
dsidentify="/bin/true" | ||
{% else %} | ||
dsidentify="/usr/lib/cloud-init/ds-identify" | ||
{% endif %} | ||
|
@@ -40,105 +42,51 @@ debug() { | |
echo "$@" >> "$LOG" | ||
} | ||
|
||
etc_file() { | ||
local pprefix="${1:-/etc/cloud/cloud-init.}" | ||
_RET="unset" | ||
[ -f "${pprefix}$ENABLE" ] && _RET="$ENABLE" && return 0 | ||
[ -f "${pprefix}$DISABLE" ] && _RET="$DISABLE" && return 0 | ||
return 0 | ||
} | ||
|
||
read_proc_cmdline() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Both container and non-container cases are handled by ConditionKernelCommandline |
||
# return /proc/cmdline for non-container, and /proc/1/cmdline for container | ||
local ctname="systemd" | ||
if [ -n "$CONTAINER" ] && ctname=$CONTAINER || | ||
systemd-detect-virt --container --quiet; then | ||
if { _RET=$(tr '\0' ' ' < /proc/1/cmdline); } 2>/dev/null; then | ||
_RET_MSG="container[$ctname]: pid 1 cmdline" | ||
return | ||
fi | ||
_RET="" | ||
_RET_MSG="container[$ctname]: pid 1 cmdline not available" | ||
return 0 | ||
fi | ||
|
||
_RET_MSG="/proc/cmdline" | ||
read _RET < /proc/cmdline | ||
} | ||
|
||
kernel_cmdline() { | ||
local cmdline="" tok="" | ||
if [ -n "${KERNEL_CMDLINE+x}" ]; then | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes and no. It is not handled from a generator perspective. If I think the generator should specifically look for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is addressed in the latest commit. |
||
# use KERNEL_CMDLINE if present in environment even if empty | ||
cmdline=${KERNEL_CMDLINE} | ||
debug 1 "kernel command line from env KERNEL_CMDLINE: $cmdline" | ||
else | ||
read_proc_cmdline && cmdline="$_RET" && | ||
debug 1 "kernel command line ($_RET_MSG): $cmdline" | ||
fi | ||
_RET="unset" | ||
cmdline=" $cmdline " | ||
tok=${cmdline##* cloud-init=} | ||
[ "$tok" = "$cmdline" ] && _RET="unset" | ||
tok=${tok%% *} | ||
[ "$tok" = "$ENABLE" -o "$tok" = "$DISABLE" ] && _RET="$tok" | ||
return 0 | ||
} | ||
|
||
default() { | ||
_RET="$ENABLE" | ||
} | ||
|
||
check_for_datasource() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to define a function that gets called once. Function definition and subsequent lookup at invocation time have costs. Inline it. |
||
local ds_rc="" | ||
if [ ! -x "$dsidentify" ]; then | ||
debug 1 "no ds-identify in $dsidentify" | ||
return 0 | ||
fi | ||
$dsidentify | ||
ds_rc=$? | ||
debug 1 "ds-identify rc=$ds_rc" | ||
if [ "$ds_rc" = "0" ]; then | ||
return 0 | ||
fi | ||
return 1 | ||
} | ||
|
||
main() { | ||
local normal_d="$1" early_d="$2" late_d="$3" | ||
local target_name="multi-user.target" gen_d="$early_d" | ||
local link_path="$gen_d/${target_name}.wants/${CLOUD_TARGET_NAME}" | ||
local ds="" | ||
local ds="" ret="" | ||
|
||
debug 1 "$0 normal=$normal_d early=$early_d late=$late_d" | ||
debug 2 "$0 $*" | ||
|
||
local search result="error" ret="" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Setting TL;DR: Do the same thing, with less. |
||
for search in kernel_cmdline etc_file default; do | ||
if $search; then | ||
debug 1 "$search found $_RET" | ||
[ "$_RET" = "$ENABLE" -o "$_RET" = "$DISABLE" ] && | ||
result=$_RET && break | ||
else | ||
ret=$? | ||
debug 0 "search $search returned $ret" | ||
fi | ||
done | ||
# ds=found => enable | ||
# ds=notfound => disable | ||
# <any> => disable | ||
debug 1 "checking for datasource" | ||
|
||
# enable AND ds=found == enable | ||
# enable AND ds=notfound == disable | ||
# disable || <any> == disabled | ||
if [ "$result" = "$ENABLE" ]; then | ||
debug 1 "checking for datasource" | ||
check_for_datasource | ||
ds=$? | ||
if [ ! -x "$dsidentify" ]; then | ||
debug 1 "no ds-identify in $dsidentify" | ||
ds=0 | ||
fi | ||
$dsidentify | ||
ds=$? | ||
debug 1 "ds-identify rc=$ds" | ||
|
||
if [ "$ds" = "1" -o "$ds" = "2" ]; then | ||
if [ "$ds" = "1" ]; then | ||
debug 1 "cloud-init is enabled but no datasource found, disabling" | ||
result="$DISABLE" | ||
else | ||
debug 1 "cloud-init is disabled by kernel commandline or etc_file" | ||
fi | ||
fi | ||
if [ -f "$link_path" ]; then | ||
if rm -f "$link_path"; then | ||
debug 1 "disabled. removed existing $link_path" | ||
else | ||
ret=$? | ||
debug 0 "[$ret] disable failed, remove $link_path" | ||
fi | ||
else | ||
debug 1 "already disabled: no change needed [no $link_path]" | ||
fi | ||
if [ -e "$RUN_ENABLED_FILE" ]; then | ||
debug 1 "removing $RUN_ENABLED_FILE and creating $RUN_DISABLED_FILE" | ||
rm -f "$RUN_ENABLED_FILE" | ||
fi | ||
: > "$RUN_DISABLED_FILE" | ||
|
||
if [ "$result" = "$ENABLE" ]; then | ||
elif [ "$ds" = "0" ]; then | ||
if [ -e "$link_path" ]; then | ||
debug 1 "already enabled: no change needed" | ||
else | ||
|
@@ -157,22 +105,6 @@ main() { | |
rm -f $RUN_DISABLED_FILE | ||
fi | ||
: > "$RUN_ENABLED_FILE" | ||
elif [ "$result" = "$DISABLE" ]; then | ||
if [ -f "$link_path" ]; then | ||
if rm -f "$link_path"; then | ||
debug 1 "disabled. removed existing $link_path" | ||
else | ||
ret=$? | ||
debug 0 "[$ret] disable failed, remove $link_path" | ||
fi | ||
else | ||
debug 1 "already disabled: no change needed [no $link_path]" | ||
fi | ||
if [ -e "$RUN_ENABLED_FILE" ]; then | ||
debug 1 "removing $RUN_ENABLED_FILE and creating $RUN_DISABLED_FILE" | ||
rm -f "$RUN_ENABLED_FILE" | ||
fi | ||
: > "$RUN_DISABLED_FILE" | ||
else | ||
debug 0 "unexpected result '$result' 'ds=$ds'" | ||
ret=3 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,6 @@ | |
[Unit] | ||
Description=Cloud-init target | ||
After=multi-user.target | ||
ConditionPathExists=!/etc/cloud/cloud-init.disabled | ||
ConditionKernelCommandLine=!cloud-init=disabled | ||
ConditionEnvironment=!KERNEL_CMDLINE=cloud-init=disabled | ||
Comment on lines
+13
to
+15
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like these additional condition checks for visibility at systemctl status cloud-init.target level, but this duplicates conditions we've decided to keep on each subordinate There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll measure and report back. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @blackboxsw see the section in the PR description titled "Measuring with and without conditionals in .target" for the numbers There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks @holmanb. This looks good to me as well as your assessment above. Also I triggered those compound conditionals directly on the systemd-analyze conditional subcommand with comparable results. The avg time seems to be +- one millisecond with or without the 3 extra checks.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is now handled by ConditionPathExists.