[lxc-devel] [PATCH 2/4] lxc-destroy: Remove container with all snapshots

Christian Brauner christianvanbrauner at gmail.com
Fri Aug 14 18:30:46 UTC 2015


- This enables the user to destroy a container with all its snapshots without
  having to use lxc-snapshot first to destroy all snapshots. (The enum values
  DESTROY and SNAP from the previous commit are reused here again.)
- Some unification regarding the usage of exit() and return has been done.

Signed-off-by: Christian Brauner <christianvanbrauner at gmail.com>
---
 src/lxc/lxc_destroy.c | 74 ++++++++++++++++++++++++++++++++++++++-------------
 1 file changed, 56 insertions(+), 18 deletions(-)

diff --git a/src/lxc/lxc_destroy.c b/src/lxc/lxc_destroy.c
index 955fc23..32bd2b4 100644
--- a/src/lxc/lxc_destroy.c
+++ b/src/lxc/lxc_destroy.c
@@ -31,16 +31,11 @@
 
 lxc_log_define(lxc_destroy_ui, lxc);
 
-static int my_parser(struct lxc_arguments* args, int c, char* arg)
-{
-	switch (c) {
-	case 'f': args->force = 1; break;
-	}
-	return 0;
-}
+static int my_parser(struct lxc_arguments* args, int c, char* arg);
 
 static const struct option my_longopts[] = {
 	{"force", no_argument, 0, 'f'},
+	{"snapshots", no_argument, 0, 's'},
 	LXC_COMMON_OPTIONS
 };
 
@@ -52,61 +47,104 @@ static struct lxc_arguments my_args = {
 lxc-destroy destroys a container with the identifier NAME\n\
 \n\
 Options :\n\
-  -n, --name=NAME   NAME for name of the container\n\
+  -n, --name=NAME   NAME of the container\n\
+  -s, --snapshots   destroy including all snapshots\n\
   -f, --force       wait for the container to shut down\n",
 	.options  = my_longopts,
 	.parser   = my_parser,
 	.checker  = NULL,
+	.task     = DESTROY,
 };
 
+static int do_destroy(struct lxc_container *c);
+static int do_destroy_with_snapshots(struct lxc_container *c);
+
 int main(int argc, char *argv[])
 {
 	struct lxc_container *c;
+	int ret;
 
 	if (lxc_arguments_parse(&my_args, argc, argv))
-		exit(1);
+		exit(EXIT_FAILURE);
 
 	if (!my_args.log_file)
 		my_args.log_file = "none";
 
 	if (lxc_log_init(my_args.name, my_args.log_file, my_args.log_priority,
 			 my_args.progname, my_args.quiet, my_args.lxcpath[0]))
-		exit(1);
+		exit(EXIT_FAILURE);
 	lxc_log_options_no_override();
 
 	c = lxc_container_new(my_args.name, my_args.lxcpath[0]);
 	if (!c) {
 		fprintf(stderr, "System error loading container\n");
-		exit(1);
+		exit(EXIT_FAILURE);
 	}
 
 	if (!c->may_control(c)) {
 		fprintf(stderr, "Insufficent privileges to control %s\n", my_args.name);
 		lxc_container_put(c);
-		exit(1);
+		exit(EXIT_FAILURE);
 	}
 
 	if (!c->is_defined(c)) {
 		fprintf(stderr, "Container is not defined\n");
 		lxc_container_put(c);
-		exit(1);
+		exit(EXIT_FAILURE);
 	}
 
 	if (c->is_running(c)) {
 		if (!my_args.force) {
 			fprintf(stderr, "%s is running\n", my_args.name);
 			lxc_container_put(c);
-			exit(1);
+			exit(EXIT_FAILURE);
 		}
 		c->stop(c);
 	}
 
+	if (my_args.task == SNAP) {
+		ret = do_destroy_with_snapshots(c);
+	} else {
+		ret = do_destroy(c);
+	}
+
+	lxc_container_put(c);
+
+	if (ret == 0)
+		exit(EXIT_SUCCESS);
+	exit(EXIT_FAILURE);
+}
+
+static int my_parser(struct lxc_arguments *args, int c, char *arg)
+{
+	switch (c) {
+	case 'f': args->force = 1; break;
+	case 's': args->task = SNAP; break;
+	}
+	return 0;
+}
+
+static int do_destroy(struct lxc_container *c)
+{
 	if (!c->destroy(c)) {
 		fprintf(stderr, "Destroying %s failed\n", my_args.name);
-		lxc_container_put(c);
-		exit(1);
+		return -1;
 	}
 
-	lxc_container_put(c);
-	exit(0);
+	printf("Destroyed container %s\n", my_args.name);
+
+	return 0;
 }
+
+static int do_destroy_with_snapshots(struct lxc_container *c)
+{
+	if (!c->destroy_with_snapshots(c)) {
+		fprintf(stderr, "Destroying %s failed\n", my_args.name);
+		return -1;
+	}
+
+	printf("Destroyed container including snapshots %s\n", my_args.name);
+
+	return 0;
+}
+
-- 
2.5.0



More information about the lxc-devel mailing list