[systemd-devel] [PATCH 1/1] Allow systemd to run without assigning container to machine.slice

Dan Walsh dwalsh at redhat.com
Thu Jan 30 13:29:14 PST 2014


If I want to run a container as a service, it would be nice if it used the service
cgroup configuration
---
 src/nspawn/nspawn.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c
index 1394ee6..9042412 100644
--- a/src/nspawn/nspawn.c
+++ b/src/nspawn/nspawn.c
@@ -83,6 +83,7 @@ static char *arg_machine = NULL;
 static char *process_label = NULL;
 static char *file_label = NULL;
 static const char *arg_slice = NULL;
+static bool arg_service = false;
 static bool arg_private_network = false;
 static bool arg_read_only = false;
 static bool arg_boot = false;
@@ -134,6 +135,7 @@ static int help(void) {
                "  -Z --processlabel=LABEL  Set the MAC Label to be used by processes in container\n"
                "     --private-network     Disable network in container\n"
                "     --read-only           Mount the root directory read-only\n"
+               "  -s --service             Run this container as a service not a machine\n"
                "     --capability=CAP      In addition to the default, retain specified\n"
                "                           capability\n"
                "     --drop-capability=CAP Drop the specified capability from the default set\n"
@@ -182,6 +184,7 @@ static int parse_argv(int argc, char *argv[]) {
                 { "setenv",          required_argument, NULL, ARG_SETENV          },
                 { "label",           required_argument, NULL, 'Z'                 },
                 { "filelabel",       required_argument, NULL, 'L'                 },
+                { "service",         no_argument,       NULL, 's'                 },
                 {}
         };
 
@@ -190,7 +193,7 @@ static int parse_argv(int argc, char *argv[]) {
         assert(argc >= 0);
         assert(argv);
 
-        while ((c = getopt_long(argc, argv, "+hD:u:bL:M:jS:Z:", options, NULL)) >= 0) {
+        while ((c = getopt_long(argc, argv, "+hD:u:bL:M:jsS:Z:", options, NULL)) >= 0) {
 
                 switch (c) {
 
@@ -269,6 +272,10 @@ static int parse_argv(int argc, char *argv[]) {
 
                         break;
 
+                case 's':
+                        arg_service = true;
+                        break;
+
                 case ARG_READ_ONLY:
                         arg_read_only = true;
                         break;
@@ -1561,9 +1568,11 @@ int main(int argc, char *argv[]) {
                 fdset_free(fds);
                 fds = NULL;
 
-                r = register_machine(pid);
-                if (r < 0)
-                        goto finish;
+                if (! arg_service) {
+                        r = register_machine(pid);
+                        if (r < 0)
+                                goto finish;
+                }
 
                 eventfd_write(sync_fd, 1);
                 close_nointr_nofail(sync_fd);
-- 
1.8.5.3



More information about the systemd-devel mailing list