]> git.proxmox.com Git - mirror_lxc.git/commitdiff
lxc-destroy: Remove container with all snapshots
authorChristian Brauner <christianvanbrauner@gmail.com>
Sat, 8 Aug 2015 12:51:11 +0000 (14:51 +0200)
committerChristian Brauner <christianvanbrauner@gmail.com>
Sat, 8 Aug 2015 12:59:59 +0000 (14:59 +0200)
- 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@gmail.com>
src/lxc/lxc_destroy.c

index ffae907c735ba4624b9a17b25ac5fa95f2e539e6..32bd2b42df8db185bf8c7e24e0591f8ee068d8bd 100644 (file)
 
 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
 };
 
@@ -53,60 +48,103 @@ lxc-destroy destroys a container with the identifier NAME\n\
 \n\
 Options :\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;
+}
+