int VEObj::operateVE(const char * func, const char * action, const char ** options, int quiet) { int rc, i; string_list argv; if (action && !quiet) logger(LOG_INFO, "%s CT %s ...", action, ctid()); string_list_init(&argv); string_list_add(&argv, BIN_VZCTL); if (quiet) string_list_add(&argv, "--quiet"); string_list_add(&argv, "--skiplock"); string_list_add(&argv, "--skipowner"); string_list_add(&argv, "--ignore-ha-cluster"); string_list_add(&argv, (char *)func); string_list_add(&argv, ctid()); if (options) { for (i = 0; options[i]; i++) string_list_add(&argv, (char *)options[i]); } rc = vzml_execve(&argv, NULL, -1, -1, quiet); string_list_clean(&argv); if (action && (rc == 0) && !quiet) logger(LOG_INFO, "done"); return rc; }
int VEObj::unregister() { // do not remove VEID on force unregister // vzmigrate will itself to unregister CT on HA cluster int flags = VZ_REG_FORCE | VZ_UNREG_PRESERVE | VZ_REG_SKIP_HA_CLUSTER; logger(LOG_DEBUG, "vzctl2_env_unregister(%s, %s, %d)", priv, ctid(), VZ_REG_FORCE); if (vzctl2_env_unregister((char *)priv, ctid(), flags) == -1) return putErr(MIG_ERR_VZCTL, "vzctl2_env_unregister(%s, %s, %d) error: %s", priv, ctid(), VZ_REG_FORCE, vzctl2_get_last_error()); return 0; }
string VEObj::confPath() const { std::ostringstream os; os << VE_CONF_DIR << ctid() << ".conf"; return os.str(); }
void c_basis::dispatch(bool bmthread) { m_timer = create_net_timer(this); //30定时器 m_timer->_schedule(1000*1000*60); m_pipe = create_net_pipe(this); m_pipe->_create(); if ((!bmthread || libevent::get_instance()->threads() == 1) && libevent::get_instance()->thread_cb()) { mw_thread_cb* cb = libevent::get_instance()->thread_cb(); cb->thread_cb(); } libevent::get_instance()->push_basis(this); if (bmthread) { libevent::get_instance()->set_thread_pipe(m_pipe); } event_base_dispatch(m_base); s_log()->err("event_dispatch exit(0x%x)\r\n", ctid()); }
int VEObj::createDevmap() { const char *opt[] = { "--create-devmap", NULL }; if (operateVE("suspend", "Create devmap", opt, 1)) return putErr(MIG_ERR_VZCTL, MIG_MSG_CREATE_DEVMAP, ctid()); return 0; }
void VEObj::unlock() { if (!islocked()) return; vzctl2_env_unlock_prvt(ctid(), lock_fd, priv); lock_fd = -1; }
int VEObj::lock() { if (isOptSet(OPT_SKIP_LOCKVE)) return 0; logger(LOG_ERR, "locking %s", ctid()); lock_fd = vzctl2_env_lock_prvt(ctid(), priv, get_lock_status()); if (lock_fd == -2) return putErr(MIG_ERR_LOCK, MIG_MSG_LOCK, ctid(), "CT locked"); else if (lock_fd < 0) return putErr(MIG_ERR_LOCK, MIG_MSG_LOCK, ctid(), vzctl2_get_last_error()); return 0; }
/* to destroy VE */ int VEObj::destroy() { int rc; vzctl_env_status_t ve_status; /* get VE status */ if (vzctl2_get_env_status(ctid(), &ve_status, ENV_STATUS_ALL)) putErr(MIG_ERR_VZCTL, "Cannot get status for CT %s", ctid()); if (ve_status.mask & ENV_STATUS_RUNNING) { if ((rc = operateVE("stop", NULL, NULL, 0))) return rc; } else if (ve_status.mask & ENV_STATUS_MOUNTED) { if ((rc = operateVE("umount", NULL, NULL, 0))) return rc; } return operateVE("destroy", NULL, NULL, 1); }
int VEObj::registration() { // will rewrite old owner on force registration // vzmigrate will itself to register CT on HA cluster int flags = VZ_REG_RENEW | VZ_REG_FORCE | VZ_REG_SKIP_HA_CLUSTER; struct vzctl_reg_param reg; memset(®, 0, sizeof(struct vzctl_reg_param)); SET_CTID(reg.ctid, ctid()); logger(LOG_DEBUG, "vzctl2_env_register(%s, %s, %d)", priv, ctid(), flags); if (vzctl2_env_register((char *)priv, ®, flags) == -1) return putErr(MIG_ERR_VZCTL, "vzctl2_env_register(%s, %s, %d) error: %s", priv, ctid(), flags, vzctl2_get_last_error()); return 0; }
/* get real VE config file path */ string VEObj::confRealPath() const { std::ostringstream os; if (layout < VZCTL_LAYOUT_4) os << VE_CONF_DIR << ctid() << ".conf"; else os << priv << "/ve.conf"; return os.str(); }
CVZContainer::operator QString() const { QString res = ctid() + " "; if (status() == Running) { res += QString::number(nproc()); res += " running"; } else { res += "-"; res += " stopped"; } return res; }
/* check that this name does not used by other VE */ int VEObj::checkName(const char *name) { ctid_t tmpCtid; int rc; if (name == NULL) return 0; rc = vzctl2_get_envid_by_name(name, tmpCtid); /* it may be our name */ if ((rc == 0) && (CMP_CTID(tmpCtid, ctid()) != 0)) return putErr(MIG_ERR_NAME_CONFLICT, MIG_MSG_NAME_CONFLICT, name, tmpCtid); return 0; }
int VEObj::init_existed() { int rc; vzctl_env_status_t env_status; if (vzctl2_get_env_status(ctid(), &env_status, ENV_STATUS_ALL)) return putErr(MIG_ERR_SYSTEM, MIG_MSG_NOSTATUS); if (!(env_status.mask & ENV_STATUS_EXISTS)) return putErr(MIG_ERR_NOEXIST, MIG_MSG_NOEXIST, ctid()); /* TODO: move to upper level */ if (isOptSet(OPT_COPY) && (env_status.mask & ENV_STATUS_SUSPENDED)) return putErr(MIG_ERR_SUSPEND, MIG_MSG_CLONE_FORBIDDEN_FOR_SUSPENDED); if ((rc = ve_data_load(m_ctid, &ve_data))) return rc; root = ve_data.root; priv = ve_data.priv; /* get VELAYOUT version */ layout = vzctl2_env_layout_version((char *)priv); if (layout >= VZCTL_LAYOUT_5) { init_disks(ve_data); } /* get veformat */ if ((veformat = vzctl2_get_veformat(priv)) == -1) { return putErr(MIG_ERR_VZCTL, "vzctl2_get_veformat(%s): %s", priv, vzctl2_get_last_error()); } return 0; }
/* vpsd is PVA process, executes in CT context via 'vzctl exec' * will kill */ int VEObj::stopVpsd() { char path[PATH_MAX + 1]; FILE *fp; pid_t pid; char buf[PATH_MAX + 1]; char *p; const char * args[] = { buf, NULL }; int tries; if (!isrun()) return 0; snprintf(path, sizeof(path), "%s/var/run/vpsd.pid", root); if ((fp = fopen(path, "r")) == NULL) return 0; if (fgets(buf, sizeof(buf), fp) == NULL) { fclose(fp); return 0; } fclose(fp); if ((p = strchr(buf, '\n'))) *p = '\0'; pid = atol(buf); snprintf(path, sizeof(path), "%s/proc/%d", root, pid); if (access(path, F_OK)) return 0; snprintf(buf, sizeof(buf), "kill -TERM %d", pid); for (tries = 0; tries < 10; ++tries) { operateVE("exec", "Exec", args, 0); sleep(1); if (access(path, F_OK)) return 0; } return putErr(MIG_ERR_SYSTEM, "Can't stop vpsd in CT %s", ctid()); }
bool CVZContainer::operator<(const CVZContainer &ct) const { return (ctid() < ct.ctid()); }
void c_basis::on_process(void* data, int size, mw_net_pipe* pipe) { if (size > 0 && ((char*)data)[0] == 'Q') { s_log()->info("loopbreak(0x%x)\r\n", ctid()); loopbreak(); return; } //1.优先处理重先绑定的 while (true) { CRawTcpConnection* con = pop_rebind(); if (con == NULL) break; con->on_rebind(); } //判断是否有连接关闭 while (true) { int32 fd = get_close(); if (fd == 0) break; MSocket::iterator pos = m_mSocket.find(fd); if (pos != m_mSocket.end()) { pos->second->on_close(EVUTIL_SOCKET_ERROR()); } } //判断是否有新连接到来 accept_fd fd; while ( libevent::get_instance()->g_fd(fd) ) { CRawTcpConnection* pcon = new CRawTcpConnection(fd.fd, fd.remote_ip); fd.sink->on_accept(pcon, fd.port); m_mSocket.insert(make_pair(fd.fd, pcon)); } //判断是否有新的连接 connect_fd cd; while ( libevent::get_instance()->g_cd(cd) ) { cd.sink->asyn_connect(cd.addr, cd.port); } //信息本线程所有连接信号 while (true) { int fd = pop_fd(); if (fd == 0) break; MSocket::iterator pos = m_mSocket.find(fd); if (pos != m_mSocket.end()) { CRawTcpConnection* con = pos->second; pos->second->on_signal(); } } }
void VEObj::setPrivate(const char *p) { free((void*)priv); priv = subst_CTID(ctid(), p); }
void VEObj::setRoot(const char *p) { free((void*)root); root = subst_CTID(ctid(), p); }
std::string VEObj::getPrivateConf() { return subst_VEID_back(ctid(), priv); }
std::string VEObj::getRootConf() { return subst_VEID_back(ctid(), root); }