Skip to content

Commit 7cfef4b

Browse files
yuwatabluca
authored andcommitted
battery-check: allow to skip by passing systemd.battery-check=0
1 parent 0bf091a commit 7cfef4b

File tree

4 files changed

+53
-1
lines changed

4 files changed

+53
-1
lines changed

NEWS

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,8 @@ CHANGES WITH 254 in spe:
698698
charge level of the system. In case the charge level is very low the
699699
user is notified (graphically via Plymouth – if available – as well
700700
as in text form on the console), and the system is turned off after a
701-
10s delay.
701+
10s delay. The feature can be disabled by passing
702+
systemd.battery-check=0 through the kernel command line.
702703

703704
* The 'passwdqc' library is now supported as an alternative to the
704705
'pwquality' library and it can be selected at build time.

man/systemd-battery-check.service.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,26 @@
6262
</para>
6363
</refsect1>
6464

65+
<refsect1>
66+
<title>Kernel Command Line</title>
67+
68+
<para>The following variables are understood:</para>
69+
70+
<variablelist class='kernel-commandline-options'>
71+
<varlistentry>
72+
<term><varname>systemd.battery-check=<replaceable>BOOL</replaceable></varname></term>
73+
74+
<listitem>
75+
<para>Takes a boolean. If specified with false, <command>systemd-battery-check</command> command
76+
will return immediately with exit status 0 without checking battery capacity and AC power
77+
existence, and the service <filename>systemd-battery-check.service</filename> will succeed. This
78+
may be useful when the command wrongly detects and reports battery capacity percentage or AC power
79+
existence, or when you want to boot the system forcibly.</para>
80+
</listitem>
81+
</varlistentry>
82+
</variablelist>
83+
</refsect1>
84+
6585
<refsect1>
6686
<title>See Also</title>
6787
<para>

src/battery-check/battery-check.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
#include "io-util.h"
1515
#include "log.h"
1616
#include "main-func.h"
17+
#include "parse-util.h"
1718
#include "pretty-print.h"
19+
#include "proc-cmdline.h"
1820
#include "socket-util.h"
1921
#include "terminal-util.h"
2022
#include "time-util.h"
@@ -24,6 +26,8 @@
2426
#define BATTERY_RESTORED_MESSAGE \
2527
"A.C. power restored, continuing."
2628

29+
static bool arg_doit = true;
30+
2731
static int help(void) {
2832
_cleanup_free_ char *link = NULL;
2933
int r;
@@ -84,6 +88,23 @@ static int plymouth_send_message(const char *mode, const char *message) {
8488
return 0;
8589
}
8690

91+
static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
92+
int r;
93+
94+
assert(key);
95+
96+
if (streq(key, "systemd.battery-check")) {
97+
98+
r = value ? parse_boolean(value) : 1;
99+
if (r < 0)
100+
log_warning_errno(r, "Failed to parse %s switch, ignoring: %s", key, value);
101+
else
102+
arg_doit = r;
103+
}
104+
105+
return 0;
106+
}
107+
87108
static int parse_argv(int argc, char * argv[]) {
88109

89110
enum {
@@ -132,10 +153,19 @@ static int run(int argc, char *argv[]) {
132153

133154
log_setup();
134155

156+
r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX);
157+
if (r < 0)
158+
log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
159+
135160
r = parse_argv(argc, argv);
136161
if (r <= 0)
137162
return r;
138163

164+
if (!arg_doit) {
165+
log_info("Checking battery status and AC power existence is disabled by the kernel command line, skipping execution.");
166+
return 0;
167+
}
168+
139169
r = battery_is_discharging_and_low();
140170
if (r < 0) {
141171
log_warning_errno(r, "Failed to check battery status, ignoring: %m");

units/systemd-battery-check.service.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Description=Check battery level during early boot
1212
Documentation=man:systemd-battery-check.service(8)
1313
ConditionVirtualization=no
1414
ConditionDirectoryNotEmpty=/sys/class/power_supply/
15+
ConditionKernelCommandLine=!systemd.battery-check=0
1516
AssertPathExists=/etc/initrd-release
1617
DefaultDependencies=no
1718
After=plymouth-start.service

0 commit comments

Comments
 (0)