systemctl: new option --drop-in for specifying drop-in filename

Previously 'systemctl edit' would only operate on
'override.conf', but users may need more than that.
Thus the new option '--drop-in' is added to allow
users to specify the drop-in file name.

Closes #25767
This commit is contained in:
Mike Yuan 2022-12-17 21:07:32 +08:00 committed by Zbigniew Jędrzejewski-Szmek
parent 616b8101b7
commit f206809b97
4 changed files with 44 additions and 2 deletions

View File

@ -1061,6 +1061,9 @@ Jan 12 10:46:45 example.com bluetoothd[8900]: gatt-time-server: Input/output err
temporary files which will be written to the real location if the
editor exits successfully.</para>
<para>If <option>--drop-in=</option> is specified, the given drop-in file name
will be used instead of the default <filename>override.conf</filename>.</para>
<para>If <option>--full</option> is specified, this will copy the
original units instead of creating drop-in files.</para>
@ -2417,6 +2420,15 @@ Jan 12 10:46:45 example.com bluetoothd[8900]: gatt-time-server: Input/output err
<listitem><para>When used with <command>bind</command>, creates a read-only bind mount.</para></listitem>
</varlistentry>
<varlistentry>
<term><option>--drop-in=</option></term>
<listitem>
<para>When used with <command>edit</command>, use the given drop-in file name instead of
<filename>override.conf</filename>.</para>
</listitem>
</varlistentry>
<xi:include href="user-system-options.xml" xpointer="host" />
<xi:include href="user-system-options.xml" xpointer="machine" />

View File

@ -426,12 +426,32 @@ static int find_paths_to_edit(
_cleanup_(hashmap_freep) Hashmap *cached_name_map = NULL, *cached_id_map = NULL;
_cleanup_(edit_file_free_all) EditFile *edit_files = NULL;
_cleanup_(lookup_paths_free) LookupPaths lp = {};
_cleanup_free_ char *drop_in_alloc = NULL, *suffix = NULL;
const char *drop_in;
size_t n_edit_files = 0;
int r;
assert(names);
assert(ret_edit_files);
if (isempty(arg_drop_in))
drop_in = "override.conf";
else if (!endswith(arg_drop_in, ".conf")) {
drop_in_alloc = strjoin(arg_drop_in, ".conf");
if (!drop_in_alloc)
return log_oom();
drop_in = drop_in_alloc;
} else
drop_in = arg_drop_in;
if (!filename_is_valid(drop_in))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid drop-in file name '%s'.", drop_in);
suffix = strjoin(".d/", drop_in);
if (!suffix)
return log_oom();
r = lookup_paths_init(&lp, arg_scope, 0, arg_root);
if (r < 0)
return r;
@ -468,7 +488,7 @@ static int find_paths_to_edit(
r = unit_file_create_new(
&lp,
*name,
arg_full ? NULL : ".d/override.conf",
arg_full ? NULL : suffix,
NULL,
edit_files + n_edit_files);
} else {
@ -508,7 +528,7 @@ static int find_paths_to_edit(
r = unit_file_create_new(
&lp,
unit_name,
".d/override.conf",
suffix,
unit_paths,
edit_files + n_edit_files);
}

View File

@ -118,6 +118,7 @@ TimestampStyle arg_timestamp_style = TIMESTAMP_PRETTY;
bool arg_read_only = false;
bool arg_mkdir = false;
bool arg_marked = false;
const char *arg_drop_in = NULL;
STATIC_DESTRUCTOR_REGISTER(arg_types, strv_freep);
STATIC_DESTRUCTOR_REGISTER(arg_states, strv_freep);
@ -131,6 +132,7 @@ STATIC_DESTRUCTOR_REGISTER(arg_reboot_argument, unsetp);
STATIC_DESTRUCTOR_REGISTER(arg_host, unsetp);
STATIC_DESTRUCTOR_REGISTER(arg_boot_loader_entry, unsetp);
STATIC_DESTRUCTOR_REGISTER(arg_clean_what, strv_freep);
STATIC_DESTRUCTOR_REGISTER(arg_drop_in, unsetp);
static int systemctl_help(void) {
_cleanup_free_ char *link = NULL;
@ -316,6 +318,7 @@ static int systemctl_help(void) {
" --read-only Create read-only bind mount\n"
" --mkdir Create directory before mounting, if missing\n"
" --marked Restart/reload previously marked units\n"
" --drop-in=NAME Edit unit files using the specified drop-in file name\n"
"\nSee the %2$s for details.\n",
program_invocation_short_name,
link,
@ -438,6 +441,7 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
ARG_MKDIR,
ARG_MARKED,
ARG_NO_WARN,
ARG_DROP_IN,
};
static const struct option options[] = {
@ -500,6 +504,7 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
{ "read-only", no_argument, NULL, ARG_READ_ONLY },
{ "mkdir", no_argument, NULL, ARG_MKDIR },
{ "marked", no_argument, NULL, ARG_MARKED },
{ "drop-in", required_argument, NULL, ARG_DROP_IN },
{}
};
@ -936,6 +941,10 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
arg_no_warn = true;
break;
case ARG_DROP_IN:
arg_drop_in = optarg;
break;
case '.':
/* Output an error mimicking getopt, and print a hint afterwards */
log_error("%s: invalid option -- '.'", program_invocation_name);

View File

@ -97,6 +97,7 @@ extern TimestampStyle arg_timestamp_style;
extern bool arg_read_only;
extern bool arg_mkdir;
extern bool arg_marked;
extern const char *arg_drop_in;
static inline const char* arg_job_mode(void) {
return _arg_job_mode ?: "replace";