2
0
mirror of git://github.com/lxc/lxc synced 2025-09-03 00:29:34 +00:00

lxc-destroy: Remove container with all snapshots

- 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>
Acked-by: Serge E. Hallyn <serge.hallyn@ubuntu.com>
This commit is contained in:
Christian Brauner
2015-08-14 20:16:16 +02:00
committed by Serge Hallyn
parent 513f23df5c
commit f29bb5d52b

View File

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