mirror of
https://git.proxmox.com/git/mirror_lxc
synced 2025-08-04 01:22:14 +00:00
If no configuration file is specified or the container was not created before.
The lxc-execute command will automatically create a new container and destroy it when it dies. If a configuration file is specified and the container does not exists, the container is created with the configuration file and destroyed when it dies.
This commit is contained in:
parent
5e30dbead0
commit
7b40d70f6b
@ -71,7 +71,7 @@ lxc_start_LDADD = liblxc.la
|
||||
lxc_stop_SOURCES = lxc_stop.c
|
||||
lxc_stop_LDADD = liblxc.la
|
||||
|
||||
lxc_execute_SOURCES = lxc_execute.c
|
||||
lxc_execute_SOURCES = lxc_execute.c lxc_config.c lxc_config.h
|
||||
lxc_execute_LDADD = liblxc.la
|
||||
|
||||
lxc_monitor_SOURCES = lxc_monitor.c
|
||||
|
@ -21,32 +21,44 @@
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <libgen.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/param.h>
|
||||
|
||||
#include <lxc/lxc.h>
|
||||
#include <lxc/lxc_config.h>
|
||||
|
||||
void usage(char *cmd)
|
||||
{
|
||||
fprintf(stderr, "%s <command>\n", basename(cmd));
|
||||
fprintf(stderr, "\t -n <name> : name of the container\n");
|
||||
fprintf(stderr, "\t -n <name> : name of the container\n");
|
||||
fprintf(stderr, "\t [-f <confile>] : path of the configuration file\n");
|
||||
_exit(1);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
char opt;
|
||||
char *name = NULL;
|
||||
char *name = NULL, *file = NULL;
|
||||
char **args;
|
||||
char path[MAXPATHLEN];
|
||||
int nbargs = 0;
|
||||
int autodestroy = 0;
|
||||
int ret = 1;
|
||||
struct lxc_conf lxc_conf;
|
||||
|
||||
while ((opt = getopt(argc, argv, "n:")) != -1) {
|
||||
while ((opt = getopt(argc, argv, "f:n:")) != -1) {
|
||||
switch (opt) {
|
||||
case 'n':
|
||||
name = optarg;
|
||||
break;
|
||||
case 'f':
|
||||
file = optarg;
|
||||
break;
|
||||
}
|
||||
|
||||
nbargs++;
|
||||
@ -57,12 +69,44 @@ int main(int argc, char *argv[])
|
||||
|
||||
args = &argv[optind];
|
||||
argc -= nbargs;
|
||||
|
||||
if (lxc_execute(name, argc, args, NULL, NULL)) {
|
||||
fprintf(stderr, "failed to start '%s'\n", name);
|
||||
return 1;
|
||||
|
||||
if (lxc_config_init(&lxc_conf)) {
|
||||
fprintf(stderr, "failed to initialize the configuration\n");
|
||||
goto out;
|
||||
}
|
||||
|
||||
return 0;
|
||||
if (file) {
|
||||
|
||||
if (lxc_config_read(file, &lxc_conf)) {
|
||||
fprintf(stderr, "invalid configuration file\n");
|
||||
goto out;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (access(path, R_OK)) {
|
||||
if (lxc_create(name, &lxc_conf)) {
|
||||
fprintf(stderr, "failed to create the container '%s'\n", name);
|
||||
goto out;
|
||||
}
|
||||
autodestroy = 1;
|
||||
}
|
||||
|
||||
if (lxc_execute(name, argc, args, NULL, NULL)) {
|
||||
fprintf(stderr, "failed to execute '%s'\n", name);
|
||||
goto out;
|
||||
}
|
||||
|
||||
ret = 0;
|
||||
|
||||
out:
|
||||
if (autodestroy) {
|
||||
if (lxc_destroy(name)) {
|
||||
fprintf(stderr, "failed to destroy '%s'\n", name);
|
||||
ret = 1;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user