Skip to content

Commit 9cff3d0

Browse files
Vineeth Pillaibryteise
authored andcommitted
ch_monitor: Reload the pid of VM on reconnect
We need to get the pid of the running vm when we reconnect after a libvirtd restart. Refactor the common code in virCHMonitorOpen and virCHMonitorNew as well. Signed-off-by: Vineeth Pillai <[email protected]>
1 parent 769910b commit 9cff3d0

File tree

2 files changed

+57
-48
lines changed

2 files changed

+57
-48
lines changed

src/ch/ch_monitor.c

Lines changed: 54 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -977,10 +977,53 @@ static void virCHMonitorStopEventLoop(virCHMonitorPtr mon)
977977
g_atomic_int_set(&mon->event_loop_stop, 1);
978978
}
979979

980+
static int
981+
virCHMonitorPostInitialize(virCHMonitorPtr mon, virDomainObjPtr vm,
982+
virCHDriverPtr driver)
983+
{
984+
g_autoptr(virCHDriverConfig) cfg = virCHDriverGetConfig(driver);
985+
virCHDomainObjPrivatePtr priv = vm->privateData;
986+
int pings = 0;
987+
int rv;
988+
989+
if ((rv = virPidFileReadPath(priv->pidfile, &vm->pid)) < 0) {
990+
virReportSystemError(-rv,
991+
_("Domain %s didn't show up"),
992+
vm->def->name);
993+
return -1;
994+
}
995+
VIR_DEBUG("cloud-hypervisor vm=%p name=%s running with pid=%lld",
996+
vm, vm->def->name, (long long)vm->pid);
997+
998+
mon->pid = vm->pid;
999+
1000+
/* get a curl handle */
1001+
mon->handle = curl_easy_init();
1002+
1003+
/* Try pinging the VMM to make sure it is ready */
1004+
while (virCHMonitorPingVMM(mon)) {
1005+
if (++pings < MONITOR_TMP_FAIL_RETRIES) {
1006+
g_usleep(100 * 1000);
1007+
continue;
1008+
}
1009+
VIR_WARN("Failed to ping VMM after %d retries", pings);
1010+
return -1;
1011+
}
1012+
1013+
if (virDomainObjSave(vm, driver->xmlopt, cfg->stateDir) < 0)
1014+
return -1;
1015+
1016+
if (virCHMonitorStartEventLoop(mon) < 0)
1017+
return -1;
1018+
1019+
return 0;
1020+
}
1021+
9801022
virCHMonitorPtr
9811023
virCHMonitorOpen(virDomainObjPtr vm, virCHDriverPtr driver)
9821024
{
9831025
g_autoptr(virCHDriverConfig) cfg = virCHDriverGetConfig(driver);
1026+
virCHDomainObjPrivatePtr priv = vm->privateData;
9841027
virCHMonitorPtr mon = NULL;
9851028

9861029
/* Hold an extra reference because we can't allow 'vm' to be
@@ -1000,7 +1043,14 @@ virCHMonitorOpen(virDomainObjPtr vm, virCHDriverPtr driver)
10001043
goto error;
10011044

10021045
mon->socketpath = g_strdup_printf("%s/%s-socket", cfg->stateDir, vm->def->name);
1003-
mon->handle = curl_easy_init();
1046+
/* XXX If we ever gonna change pid file pattern, come up with
1047+
* some intelligence here to deal with old paths. */
1048+
if (!(priv->pidfile = virPidFileBuildPath(cfg->stateDir, vm->def->name)))
1049+
goto error;
1050+
1051+
if (virCHMonitorPostInitialize(mon, vm, driver) < 0)
1052+
goto error;
1053+
10041054
virObjectRef(mon);
10051055
mon->vm = virObjectRef(vm);
10061056

@@ -1018,8 +1068,6 @@ virCHMonitorNew(virDomainObjPtr vm, virCHDriverPtr driver)
10181068
virCHMonitorPtr mon = NULL;
10191069
virCommandPtr cmd = NULL;
10201070
int monitor_fds[2];
1021-
int pings = 0;
1022-
int rv;
10231071
int i;
10241072

10251073
if (virCHMonitorInitialize() < 0)
@@ -1063,29 +1111,13 @@ virCHMonitorNew(virDomainObjPtr vm, virCHDriverPtr driver)
10631111
virCommandDaemonize(cmd);
10641112
virCommandRequireHandshake(cmd);
10651113

1066-
rv = virCommandRun(cmd, NULL);
1067-
1068-
/* wait for cloud-hypervisor process to show up */
1069-
if (rv == 0) {
1070-
if ((rv = virPidFileReadPath(priv->pidfile, &vm->pid)) < 0) {
1071-
virReportSystemError(-rv,
1072-
_("Domain %s didn't show up"),
1073-
vm->def->name);
1074-
goto cleanup;
1075-
}
1076-
VIR_DEBUG("cloud-hypervisor vm=%p name=%s running with pid=%lld",
1077-
vm, vm->def->name, (long long)vm->pid);
1078-
} else {
1114+
if (virCommandRun(cmd, NULL) != 0) {
10791115
VIR_DEBUG("cloud-hypervisor vm=%p name=%s failed to spawn",
10801116
vm, vm->def->name);
10811117
goto cleanup;
10821118
}
1083-
mon->pid = vm->pid;
1084-
1085-
VIR_DEBUG("Writing early domain status to disk");
1086-
if (virDomainObjSave(vm, driver->xmlopt, cfg->stateDir) < 0)
1087-
goto cleanup;
10881119

1120+
/* wait for cloud-hypervisor process to show up */
10891121
VIR_DEBUG("Waiting for handshake from child");
10901122
if (virCommandHandshakeWait(cmd) < 0) {
10911123
/* Read errors from child that occurred between fork and exec. */
@@ -1097,25 +1129,8 @@ virCHMonitorNew(virDomainObjPtr vm, virCHDriverPtr driver)
10971129
if (virCommandHandshakeNotify(cmd) < 0)
10981130
goto cleanup;
10991131

1100-
/* get a curl handle */
1101-
mon->handle = curl_easy_init();
1102-
1103-
/* Try pinging the VMM to make sure it is ready */
1104-
while (virCHMonitorPingVMM(mon)) {
1105-
if (++pings < MONITOR_TMP_FAIL_RETRIES) {
1106-
g_usleep(100 * 1000);
1107-
continue;
1108-
}
1109-
VIR_WARN("Failed to ping VMM after %d retries", pings);
1110-
goto cleanup;
1111-
}
1112-
1113-
if (virDomainObjSave(vm, driver->xmlopt, cfg->stateDir) < 0)
1114-
goto cleanup;
1115-
1116-
if (virCHMonitorStartEventLoop(mon) < 0) {
1132+
if (virCHMonitorPostInitialize(mon, vm, driver) < 0)
11171133
goto cleanup;
1118-
}
11191134

11201135
/* now has its own reference */
11211136
virObjectRef(mon);

src/ch/ch_process.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -779,21 +779,15 @@ virCHProcessReconnect(void *opaque)
779779
goto error;
780780
jobStarted = true;
781781

782-
/* XXX If we ever gonna change pid file pattern, come up with
783-
* some intelligence here to deal with old paths. */
784-
if (!(priv->pidfile = virPidFileBuildPath(cfg->stateDir, obj->def->name)))
785-
goto error;
786-
787-
obj->def->id = obj->pid;
788-
virReportError(VIR_ERR_INTERNAL_ERROR, "%s-%d",
789-
"pid is", obj->pid);
790-
791782
if (chHostdevUpdateActiveDomainDevices(driver, obj->def) < 0)
792783
goto error;
793784

794785
if (virCHConnectMonitor(driver, obj) < 0)
795786
goto error;
796787

788+
obj->def->id = obj->pid;
789+
VIR_DEBUG("Domain Object def->id = %d", obj->def->id);
790+
797791
priv->machineName = virCHDomainGetMachineName(obj);
798792
if (!priv->machineName)
799793
goto error;

0 commit comments

Comments
 (0)