1
0
mirror of https://github.com/systemd/systemd.git synced 2025-02-25 21:57:32 +03:00

machinectl: add --now to start/stop containers when enabling/disabling

Closes #26154
This commit is contained in:
Mike Yuan 2023-01-24 01:20:14 +08:00 committed by Luca Boccassi
parent 425e293427
commit c2434a61f2
2 changed files with 46 additions and 3 deletions

View File

@ -234,7 +234,11 @@
<citerefentry><refentrytitle>systemd-nspawn</refentrytitle><manvolnum>1</manvolnum></citerefentry>.
This enables or disables <filename>systemd-nspawn@.service</filename>, instantiated for the specified
machine name, similarly to the effect of <command>systemctl enable</command> or <command>systemctl
disable</command> on the service name.</para></listitem>
disable</command> on the service name.</para>
<para>This command implicitly reloads the system manager configuration after completing the operation.
Note that this command does not implicitly start or power off the containers that are being operated on.
If this is desired, combine the command with the <option>--now</option> switch.</para></listitem>
</varlistentry>
<varlistentry>
@ -776,6 +780,17 @@
<literal>signature</literal>.</para></listitem>
</varlistentry>
<varlistentry>
<term><option>--now</option></term>
<listitem>
<para>When used with <command>enable</command> or <command>disable</command>,
the containers will also be started or powered off. The start or poweroff
operation is only carried out when the respective enable or disable
operation has been successful.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><option>--force</option></term>

View File

@ -77,6 +77,7 @@ static bool arg_quiet = false;
static bool arg_ask_password = true;
static unsigned arg_lines = 10;
static OutputMode arg_output = OUTPUT_SHORT;
static bool arg_now = false;
static bool arg_force = false;
static ImportVerify arg_verify = IMPORT_VERIFY_SIGNATURE;
static const char* arg_format = NULL;
@ -1673,7 +1674,26 @@ static int enable_machine(int argc, char *argv[], void *userdata) {
goto finish;
}
r = 0;
if (arg_now) {
_cleanup_strv_free_ char **new_args = NULL;
new_args = strv_new(streq(argv[0], "enable") ? "start" : "poweroff");
if (!new_args) {
r = log_oom();
goto finish;
}
r = strv_extend_strv(&new_args, argv + 1, /* filter_duplicates = */ false);
if (r < 0) {
log_oom();
goto finish;
}
if (streq(argv[0], "enable"))
r = start_machine(strv_length(new_args), new_args, userdata);
else
r = poweroff_machine(strv_length(new_args), new_args, userdata);
}
finish:
install_changes_free(changes, n_changes);
@ -2471,8 +2491,10 @@ static int help(int argc, char *argv[], void *userdata) {
" json, json-pretty, json-sse, json-seq, cat,\n"
" verbose, export, with-unit)\n"
" --verify=MODE Verification mode for downloaded images (no,\n"
" checksum, signature)\n"
" checksum, signature)\n"
" --force Download image even if already exists\n"
" --now Start or power off container after enabling or\n"
" disabling it\n"
"\nSee the %s for details.\n",
program_invocation_short_name,
ansi_highlight(),
@ -2496,6 +2518,7 @@ static int parse_argv(int argc, char *argv[]) {
ARG_MKDIR,
ARG_NO_ASK_PASSWORD,
ARG_VERIFY,
ARG_NOW,
ARG_FORCE,
ARG_FORMAT,
ARG_UID,
@ -2522,6 +2545,7 @@ static int parse_argv(int argc, char *argv[]) {
{ "output", required_argument, NULL, 'o' },
{ "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
{ "verify", required_argument, NULL, ARG_VERIFY },
{ "now", no_argument, NULL, ARG_NOW },
{ "force", no_argument, NULL, ARG_FORCE },
{ "format", required_argument, NULL, ARG_FORMAT },
{ "uid", required_argument, NULL, ARG_UID },
@ -2698,6 +2722,10 @@ static int parse_argv(int argc, char *argv[]) {
arg_verify = r;
break;
case ARG_NOW:
arg_now = true;
break;
case ARG_FORCE:
arg_force = true;
break;