From 47010f352c943e5b6d52d6dc975d1ee261873cd6 Mon Sep 17 00:00:00 2001 From: Daniel Veillard Date: Thu, 10 Jul 2008 07:52:14 +0000 Subject: [PATCH] * src/openvz_conf.c src/openvz_conf.h src/openvz_driver.c: new patch from Evgeniy Sokolov adding OpenVZ autostart get and set support Daniel --- ChangeLog | 5 ++++ src/openvz_conf.c | 59 +++++++++++++++++++++++++++++++++++++++++++++ src/openvz_conf.h | 1 + src/openvz_driver.c | 52 +++++++++++++++++++++++++++++++++++++-- 4 files changed, 115 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index 3dda22c5b0..6d78ada4e5 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Thu Jul 10 09:50:39 CEST 2008 Daniel Veillard + + * src/openvz_conf.c src/openvz_conf.h src/openvz_driver.c: new + patch from Evgeniy Sokolov adding OpenVZ autostart get and set support + Wed Jul 9 13:53:25 CEST 2008 Daniel Veillard * src/openvz_conf.c src/openvz_conf.h src/openvz_driver.c: applied diff --git a/src/openvz_conf.c b/src/openvz_conf.c index 32d7258bc7..b8096be437 100644 --- a/src/openvz_conf.c +++ b/src/openvz_conf.c @@ -603,6 +603,63 @@ error: return NULL; } +/* +* Read parameter from container config +* sample: 133, "OSTEMPLATE", value, 1024 +* return: -1 - error +* 0 - don't found +* 1 - OK +*/ +int +openvzReadConfigParam(int vpsid ,const char * param, char *value, int maxlen) +{ + char conf_file[PATH_MAX] ; + char line[PATH_MAX] ; + int ret, found = 0; + char * conf_dir; + int fd ; + char * sf, * token; + char *saveptr = NULL; + + + conf_dir = openvzLocateConfDir(); + if (conf_dir == NULL) + return -1; + + if (snprintf(conf_file, PATH_MAX,"%s/%d.conf",conf_dir,vpsid) >= PATH_MAX) + return -1; + + VIR_FREE(conf_dir); + + value[0] = 0; + + fd = open(conf_file, O_RDONLY); + if (fd == -1) + return -1; + + while(1) { + ret = openvz_readline(fd, line, sizeof(line)); + if(ret <= 0) + break; + saveptr = NULL; + if (STREQLEN(line, param, strlen(param))) { + sf = line; + sf += strlen(param); + if (sf[0] == '=' && (token = strtok_r(sf,"\"\t=\n", &saveptr)) != NULL) { + strncpy(value, token, maxlen) ; + value[maxlen-1] = '\0'; + found = 1; + } + } + } + close(fd); + + if (ret == 0 && found) + ret = 1; + + return ret ; +} + static char *openvzLocateConfDir(void) { @@ -680,6 +737,8 @@ openvzGetVPSUUID(int vpsid, char *uuidstr) break; } } + close(fd); + return 0; } diff --git a/src/openvz_conf.h b/src/openvz_conf.h index 772782fcc2..9d6aa05784 100644 --- a/src/openvz_conf.h +++ b/src/openvz_conf.h @@ -112,6 +112,7 @@ openvzIsActiveVM(struct openvz_vm *vm) void error (virConnectPtr conn, virErrorNumber code, const char *fmt, ...); int openvz_readline(int fd, char *ptr, int maxlen); +int openvzReadConfigParam(int vpsid ,const char * param, char *value, int maxlen); struct openvz_vm *openvzFindVMByID(const struct openvz_driver *driver, int id); struct openvz_vm *openvzFindVMByUUID(const struct openvz_driver *driver, const unsigned char *uuid); diff --git a/src/openvz_driver.c b/src/openvz_driver.c index fcc9d8c129..27e4fdcd1b 100644 --- a/src/openvz_driver.c +++ b/src/openvz_driver.c @@ -547,6 +547,54 @@ bail_out5: return ret; } +static int +openvzDomainSetAutostart(virDomainPtr dom, int autostart) +{ + virConnectPtr conn= dom->conn; + struct openvz_driver *driver = (struct openvz_driver *) conn->privateData; + struct openvz_vm *vm = openvzFindVMByUUID(driver, dom->uuid); + const char *prog[] = { VZCTL, "set", vm->vmdef->name, + "--onboot", autostart ? "yes" : "no", + "--save", NULL }; + + if (!vm) { + error(conn, VIR_ERR_INVALID_DOMAIN, _("no domain with matching uuid")); + return -1; + } + + if (virRun(conn, (char **)prog, NULL) < 0) { + error(conn, VIR_ERR_INTERNAL_ERROR, _("Could not exec %s"), VZCTL); + return -1; + } + + return 0; +} + +static int +openvzDomainGetAutostart(virDomainPtr dom, int *autostart) +{ + virConnectPtr conn= dom->conn; + struct openvz_driver *driver = (struct openvz_driver *) conn->privateData; + struct openvz_vm *vm = openvzFindVMByUUID(driver, dom->uuid); + char value[1024]; + + if (!vm) { + error(conn, VIR_ERR_INVALID_DOMAIN, _("no domain with matching uuid")); + return -1; + } + + if (openvzReadConfigParam(vm->vpsid , "ONBOOT", value, sizeof(value)) < 0) { + error(conn, VIR_ERR_INTERNAL_ERROR, _("Cound not read container config")); + return -1; + } + + *autostart = 0; + if (STREQ(value,"yes")) + *autostart = 1; + + return 0; +} + static const char *openvzProbe(void) { #ifdef __linux__ @@ -748,8 +796,8 @@ static virDriver openvzDriver = { openvzDomainUndefine, /* domainUndefine */ NULL, /* domainAttachDevice */ NULL, /* domainDetachDevice */ - NULL, /* domainGetAutostart */ - NULL, /* domainSetAutostart */ + openvzDomainGetAutostart, /* domainGetAutostart */ + openvzDomainSetAutostart, /* domainSetAutostart */ NULL, /* domainGetSchedulerType */ NULL, /* domainGetSchedulerParameters */ NULL, /* domainSetSchedulerParameters */