]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/tools/lxc_freeze.c
Merge pull request #3235 from xinhua9569/master
[mirror_lxc.git] / src / lxc / tools / lxc_freeze.c
CommitLineData
cc73685d 1/* SPDX-License-Identifier: LGPL-2.1+ */
49ac7514 2
d38dd64a
CB
3#ifndef _GNU_SOURCE
4#define _GNU_SOURCE 1
5#endif
49ac7514 6#include <libgen.h>
5e97c3fc 7#include <stdio.h>
49ac7514 8#include <string.h>
5e97c3fc 9#include <sys/types.h>
d38dd64a 10#include <unistd.h>
5e97c3fc 11
92b0b5ba 12#include <lxc/lxccontainer.h>
00b3c2e2 13
4237c6ca 14#include "arguments.h"
d38dd64a 15#include "config.h"
a599e9c0 16#include "log.h"
17
18lxc_log_define(lxc_freeze, lxc);
5e97c3fc 19
4237c6ca
MN
20static const struct option my_longopts[] = {
21 LXC_COMMON_OPTIONS
22};
23
24static struct lxc_arguments my_args = {
fe8c37ef 25 .progname = "lxc-freeze",
26 .help = "\
4237c6ca
MN
27--name=NAME\n\
28\n\
29lxc-freeze freezes a container with the identifier NAME\n\
30\n\
31Options :\n\
50b737a3
WB
32 -n, --name=NAME NAME of the container\n\
33 --rcfile=FILE Load configuration file FILE\n",
fe8c37ef 34 .options = my_longopts,
35 .parser = NULL,
36 .checker = NULL,
37 .log_priority = "ERROR",
38 .log_file = "none",
4237c6ca 39};
5e97c3fc 40
41int main(int argc, char *argv[])
42{
92b0b5ba 43 struct lxc_container *c;
73b910a3 44 struct lxc_log log;
92b0b5ba 45
c8f55b0e 46 if (lxc_arguments_parse(&my_args, argc, argv))
b52b0595 47 exit(EXIT_FAILURE);
5e97c3fc 48
fe8c37ef 49 log.name = my_args.name;
50 log.file = my_args.log_file;
51 log.level = my_args.log_priority;
52 log.prefix = my_args.progname;
53 log.quiet = my_args.quiet;
54 log.lxcpath = my_args.lxcpath[0];
f6d79ec1 55
fe8c37ef 56 if (lxc_log_init(&log))
57 exit(EXIT_FAILURE);
5e97c3fc 58
92b0b5ba
SH
59 c = lxc_container_new(my_args.name, my_args.lxcpath[0]);
60 if (!c) {
a599e9c0 61 ERROR("No such container: %s:%s", my_args.lxcpath[0], my_args.name);
b52b0595 62 exit(EXIT_FAILURE);
92b0b5ba 63 }
5e97c3fc 64
50b737a3
WB
65 if (my_args.rcfile) {
66 c->clear_config(c);
a599e9c0 67
50b737a3 68 if (!c->load_config(c, my_args.rcfile)) {
a599e9c0 69 ERROR("Failed to load rcfile");
50b737a3 70 lxc_container_put(c);
b52b0595 71 exit(EXIT_FAILURE);
50b737a3 72 }
a599e9c0 73
6118210e
WB
74 c->configfile = strdup(my_args.rcfile);
75 if (!c->configfile) {
a599e9c0 76 ERROR("Out of memory setting new config filename");
6118210e 77 lxc_container_put(c);
b52b0595 78 exit(EXIT_FAILURE);
6118210e 79 }
50b737a3
WB
80 }
81
f5abd74d 82 if (!c->may_control(c)) {
a599e9c0 83 ERROR("Insufficent privileges to control %s:%s", my_args.lxcpath[0], my_args.name);
f3e52710 84 lxc_container_put(c);
b52b0595 85 exit(EXIT_FAILURE);
f5abd74d
SG
86 }
87
92b0b5ba 88 if (!c->freeze(c)) {
a599e9c0 89 ERROR("Failed to freeze %s:%s", my_args.lxcpath[0], my_args.name);
92b0b5ba 90 lxc_container_put(c);
b52b0595 91 exit(EXIT_FAILURE);
92b0b5ba
SH
92 }
93
94 lxc_container_put(c);
95
b52b0595 96 exit(EXIT_SUCCESS);
92b0b5ba 97}