Skip to contentSkip to conversation

lxc-unshare: accept multiple -s options

PATCH In review Serge E. Hallyn 2 participants 5 messages May 13, 2010 – May 18, 2010 3 messages not in the archive

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 lxc-unshare: accept multiple -s options

(also remove -f from usage as it is not actually supported)

---
 src/lxc/lxc_unshare.c |   28 ++++++++++++++++++++++++++--
 1 files changed, 26 insertions(+), 2 deletions(-)
src/lxc/lxc_unshare.c +26 -2
index 10654f7..8db1cb7 100644
@@ -48,7 +48,7 @@ void usage(char *cmd)
 	fprintf(stderr, "\t -s flags: Ored list of flags to unshare:\n" \
 			"\t           MOUNT, PID, UTSNAME, IPC, USER, NETWORK\n");
 	fprintf(stderr, "\t -u <id> : new id to be set if -s USER is specified\n");
-	fprintf(stderr, "\t if -f or -s PID is specified, <command> is mandatory)\n");
+	fprintf(stderr, "\t if -s PID is specified, <command> is mandatory)\n");
 	_exit(1);
 }
 
@@ -154,6 +154,29 @@ static int do_start(void *arg)
 	return 1;
 }
 
+void extend_namespaces(char **n, char *optarg)
+{
+	char *namespaces = *n;
+	int cont = 0, newlen = strlen(optarg) + 1; /* +1 for trailing \0 */
+
+	if (namespaces) {
+		cont = 1;
+		newlen += strlen(namespaces) + 1; /* +1 for '|' */
+	}
+
+	namespaces = realloc(namespaces, newlen);
+	if (!namespaces) {
+		perror("realloc");
+		exit(1);
+	}
+	if (cont)
+		sprintf(namespaces+strlen(namespaces), "|%s", optarg);
+	else
+		sprintf(namespaces, "%s", optarg);
+	namespaces[newlen-1] = '\0';
+	*n = namespaces;
+}
+
 int main(int argc, char *argv[])
 {
 	int opt, status;
@@ -174,7 +197,8 @@ int main(int argc, char *argv[])
 	while ((opt = getopt(argc, argv, "s:u:")) != -1) {
 		switch (opt) {
 		case 's':
-			namespaces = optarg;
+			extend_namespaces(&namespaces, optarg);
+			printf("namespaces is %s\n", namespaces);
 			break;
 		case 'u':
 			uid = lookup_user(optarg);
Signature · 1 line
1.6.3.3

A message that is not in the archive (sent off-list or not yet received): 20100513193412.GA15433-r/[email protected]

#2 lore raw

PATCH 2/2 lxc-unshare: make CLONE_NEWPID imply CLONE_NEWNS

I would like to also automatically have /proc remounted, but that would require digging deeper into lxc_clone.

Mind you perhaps having NEWPID imply NEWNS should be done there, at src/lxc/namespace.c:lxc_clone anyway. I'm starting here... Won't be offended if it's rejected on those grounds :)

---
 src/lxc/lxc_unshare.c |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
src/lxc/lxc_unshare.c +4 -0
index 8db1cb7..8531b59 100644
@@ -49,6 +49,7 @@ void usage(char *cmd)
 			"\t           MOUNT, PID, UTSNAME, IPC, USER, NETWORK\n");
 	fprintf(stderr, "\t -u <id> : new id to be set if -s USER is specified\n");
 	fprintf(stderr, "\t if -s PID is specified, <command> is mandatory)\n");
+	fprintf(stderr, "\t If -s PID is specified, then -s MOUNT is implied\n");
 	_exit(1);
 }
 
@@ -213,6 +214,9 @@ int main(int argc, char *argv[])
  	if (ret)
 		usage(argv[0]);
 
+	if (flags & CLONE_NEWPID)
+		flags |= CLONE_NEWNS;
+
 	if (!(flags & CLONE_NEWUSER) && uid != -1) {
 		ERROR("-u <uid> needs -s USER option");
 		return 1;
Signature · 1 line
1.6.3.3

A message that is not in the archive (sent off-list or not yet received): 20100513193447.GA15830-r/[email protected]

#4 lore raw

Re: [PATCH lxc 2/2] lxc-unshare: make CLONE_NEWPID imply CLONE_NEWNS

Quoted 2 lines · Serge E. Hallyn

I would like to also automatically have /proc remounted, but that would require digging deeper into lxc_clone.

You should not make that automatically, especially in lxc-clone because this function is just for cloning a process in a new namespace, nothing more. We may want to access /proc after cloning, for example to reach /proc/<pid>/ns/*. The automatic mount, should be done in your child reaper (like lxc-init), otherwise let the container init to run the services and mount /proc.

In the case of lxc_unshare, you can add a new option to remount /proc when there is the pidns or the mountns options.

Otherwise, adding the NEWNS with the NEWPID makes sense for me.

Don't forget lxc_unshare is a simple tool, it is not supposed to replace lxc-start/lxc-execute, at least it should do a bit more than the "unshare" command.

Quoted 31 lines

Mind you perhaps having NEWPID imply NEWNS should be done there, at src/lxc/namespace.c:lxc_clone anyway. I'm starting here... Won't be offended if it's rejected on those grounds :)

---
  src/lxc/lxc_unshare.c |    4 ++++
  1 files changed, 4 insertions(+), 0 deletions(-)
src/lxc/lxc_unshare.c +4 -0
index 8db1cb7..8531b59 100644
@@ -49,6 +49,7 @@ void usage(char *cmd)
  			"\t           MOUNT, PID, UTSNAME, IPC, USER, NETWORK\n");
  	fprintf(stderr, "\t -u<id>  : new id to be set if -s USER is specified\n");
  	fprintf(stderr, "\t if -s PID is specified,<command>  is mandatory)\n");
+	fprintf(stderr, "\t If -s PID is specified, then -s MOUNT is implied\n");
  	_exit(1);
  }
 
@@ -213,6 +214,9 @@ int main(int argc, char *argv[])
   	if (ret)
  		usage(argv[0]);
 
+	if (flags&  CLONE_NEWPID)
+		flags |= CLONE_NEWNS;
+
  	if (!(flags&  CLONE_NEWUSER)&&  uid != -1) {
  		ERROR("-u<uid>  needs -s USER option");
  		return 1;
#5 lore raw
In reply to a message not in the archive
Quoted 51 lines · Serge E. Hallyn

(also remove -f from usage as it is not actually supported)

---
  src/lxc/lxc_unshare.c |   28 ++++++++++++++++++++++++++--
  1 files changed, 26 insertions(+), 2 deletions(-)
src/lxc/lxc_unshare.c +24 -1
index 10654f7..8db1cb7 100644
@@ -48,7 +48,7 @@ void usage(char *cmd)
  	fprintf(stderr, "\t -s flags: Ored list of flags to unshare:\n" \
  			"\t           MOUNT, PID, UTSNAME, IPC, USER, NETWORK\n");
  	fprintf(stderr, "\t -u<id>  : new id to be set if -s USER is specified\n");
-	fprintf(stderr, "\t if -f or -s PID is specified,<command>  is mandatory)\n");
+	fprintf(stderr, "\t if -s PID is specified,<command>  is mandatory)\n");
  	_exit(1);
  }
 
@@ -154,6 +154,29 @@ static int do_start(void *arg)
  	return 1;
  }
 
+void extend_namespaces(char **n, char *optarg)
+{
+	char *namespaces = *n;
+	int cont = 0, newlen = strlen(optarg) + 1; /* +1 for trailing \0 */
+
+	if (namespaces) {
+		cont = 1;
+		newlen += strlen(namespaces) + 1; /* +1 for '|' */
+	}
+
+	namespaces = realloc(namespaces, newlen);
+	if (!namespaces) {
+		perror("realloc");
+		exit(1);
+	}
+	if (cont)
+		sprintf(namespaces+strlen(namespaces), "|%s", optarg);
+	else
+		sprintf(namespaces, "%s", optarg);
+	namespaces[newlen-1] = '\0';
+	*n = namespaces;
+}
+
  int main(int argc, char *argv[])
  {
  	int opt, status;

src/lxc/lxc_unshare.c int main(int argc, char *argv[]) · line 204

Quoted 9 lines
src/lxc/lxc_unshare.c +2 -1
@@ -174,7 +197,8 @@ int main(int argc, char *argv[])
  	while ((opt = getopt(argc, argv, "s:u:")) != -1) {
  		switch (opt) {
  		case 's':
-			namespaces = optarg;
+			extend_namespaces(&namespaces, optarg);
+			printf("namespaces is %s\n", namespaces);
  			break;
  		case 'u':
  			uid = lookup_user(optarg);

Hi Serge,

Sorry for the delay.

Is it possible to just kill the "Ored" option format and have only multiple "-s" options ? That will simplificate the code a lot.

Thanks
-- Daniel

A message that is not in the archive (sent off-list or not yet received): 4BF2B71A.8020906-NmTC/[email protected]

#7 lore raw
Quoted 64 lines · Daniel Lezcano
Quoted 51 lines · Serge E. Hallyn

(also remove -f from usage as it is not actually supported)

---
 src/lxc/lxc_unshare.c |   28 ++++++++++++++++++++++++++--
 1 files changed, 26 insertions(+), 2 deletions(-)
src/lxc/lxc_unshare.c +24 -1
index 10654f7..8db1cb7 100644
@@ -48,7 +48,7 @@ void usage(char *cmd)
 	fprintf(stderr, "\t -s flags: Ored list of flags to unshare:\n" \
 			"\t           MOUNT, PID, UTSNAME, IPC, USER, NETWORK\n");
 	fprintf(stderr, "\t -u<id>  : new id to be set if -s USER is specified\n");
-	fprintf(stderr, "\t if -f or -s PID is specified,<command>  is mandatory)\n");
+	fprintf(stderr, "\t if -s PID is specified,<command>  is mandatory)\n");
 	_exit(1);
 }
 
@@ -154,6 +154,29 @@ static int do_start(void *arg)
 	return 1;
 }
 
+void extend_namespaces(char **n, char *optarg)
+{
+	char *namespaces = *n;
+	int cont = 0, newlen = strlen(optarg) + 1; /* +1 for trailing \0 */
+
+	if (namespaces) {
+		cont = 1;
+		newlen += strlen(namespaces) + 1; /* +1 for '|' */
+	}
+
+	namespaces = realloc(namespaces, newlen);
+	if (!namespaces) {
+		perror("realloc");
+		exit(1);
+	}
+	if (cont)
+		sprintf(namespaces+strlen(namespaces), "|%s", optarg);
+	else
+		sprintf(namespaces, "%s", optarg);
+	namespaces[newlen-1] = '\0';
+	*n = namespaces;
+}
+
 int main(int argc, char *argv[])
 {
 	int opt, status;

src/lxc/lxc_unshare.c int main(int argc, char *argv[]) · line 204

Quoted 9 lines
src/lxc/lxc_unshare.c +2 -1
@@ -174,7 +197,8 @@ int main(int argc, char *argv[])
 	while ((opt = getopt(argc, argv, "s:u:")) != -1) {
 		switch (opt) {
 		case 's':
-			namespaces = optarg;
+			extend_namespaces(&namespaces, optarg);
+			printf("namespaces is %s\n", namespaces);
 			break;
 		case 'u':
 			uid = lookup_user(optarg);

Hi Serge,

Quoted 4 lines

Sorry for the delay.

Is it possible to just kill the "Ored" option format and have only multiple "-s" options ? That will simplificate the code a lot.

Agreed I think that'd be better

-serge