]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
machinectl: add --now to start/stop containers when enabling/disabling
authorMike Yuan <me@yhndnzj.com>
Mon, 23 Jan 2023 17:20:14 +0000 (01:20 +0800)
committerLuca Boccassi <luca.boccassi@gmail.com>
Tue, 24 Jan 2023 11:09:03 +0000 (11:09 +0000)
Closes #26154

man/machinectl.xml
src/machine/machinectl.c

index 9f3e0921a4c130b78c8632303b7d76463d3600f8..2ecbe0f0abf5d7e21b0e8a0690c830cd2bf48222 100644 (file)
         <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>
         <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>
 
index a147fae0bda80144ce51f91d0531c9e11a555a28..80eebf36d372627332f60355e7191af4eeb65d29 100644 (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;