From: Mike Yuan Date: Mon, 23 Jan 2023 17:20:14 +0000 (+0800) Subject: machinectl: add --now to start/stop containers when enabling/disabling X-Git-Tag: v253-rc1~17 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c2434a61f2a7ae2380ec4de0cd7128e3b9bffbac;p=thirdparty%2Fsystemd.git machinectl: add --now to start/stop containers when enabling/disabling Closes #26154 --- diff --git a/man/machinectl.xml b/man/machinectl.xml index 9f3e0921a4c..2ecbe0f0abf 100644 --- a/man/machinectl.xml +++ b/man/machinectl.xml @@ -234,7 +234,11 @@ systemd-nspawn1. This enables or disables systemd-nspawn@.service, instantiated for the specified machine name, similarly to the effect of systemctl enable or systemctl - disable on the service name. + disable on the service name. + + 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 switch. @@ -776,6 +780,17 @@ signature. + + + + + When used with enable or disable, + 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. + + + diff --git a/src/machine/machinectl.c b/src/machine/machinectl.c index a147fae0bda..80eebf36d37 100644 --- a/src/machine/machinectl.c +++ b/src/machine/machinectl.c @@ -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;