summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Yuan <me@yhndnzj.com>2023-01-24 01:20:14 +0800
committerLuca Boccassi <luca.boccassi@gmail.com>2023-01-24 11:09:03 +0000
commitc2434a61f2a7ae2380ec4de0cd7128e3b9bffbac (patch)
tree52e5e5e1527e98d9848f6ff6bf5ef519c7e16bab
parent425e293427e642a52ee0eb35e830f0301f2f582f (diff)
downloadsystemd-c2434a61f2a7ae2380ec4de0cd7128e3b9bffbac.tar.gz
machinectl: add --now to start/stop containers when enabling/disabling
Closes #26154
-rw-r--r--man/machinectl.xml17
-rw-r--r--src/machine/machinectl.c32
2 files changed, 46 insertions, 3 deletions
diff --git a/man/machinectl.xml b/man/machinectl.xml
index 9f3e0921a4..2ecbe0f0ab 100644
--- a/man/machinectl.xml
+++ b/man/machinectl.xml
@@ -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>
@@ -777,6 +781,17 @@
</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>
<listitem><para>When downloading a container or VM image, and
diff --git a/src/machine/machinectl.c b/src/machine/machinectl.c
index a147fae0bd..80eebf36d3 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;