-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
474 lines (395 loc) · 10.4 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/*
* Copyright (c) 2016-2017, Parallels International GmbH
* Copyright (c) 2017-2019 Virtuozzo International GmbH. All rights reserved.
*
* This file is part of OpenVZ. OpenVZ is free software; you can redistribute
* it and/or modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*
* Our contact details: Virtuozzo International GmbH, Vordergasse 59, 8200
* Schaffhausen, Switzerland.
*/
#include <limits.h>
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <string.h>
#include <sys/file.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <syslog.h>
#include <uuid/uuid.h>
#include <pthread.h>
#include <sys/stat.h>
#include <vz/vzevent.h>
#include <vzctl/libvzctl.h>
#include "parser.h"
#define COMPACT_CONF "/etc/vz/pcompact.conf"
#define COMPACT_STATE "/var/run/pcompact.pid"
#define COMPACT_LOG_FILE "/var/log/pcompact.log"
static struct {
int threshold;
int image_defrag_threshold;
int delta; /* how many data should be freed */
int log_level;
int dry;
int oneshot;
int quiet;
int defrag;
} config = {
.threshold = 20,
.image_defrag_threshold = 10,
.delta = 10,
.log_level = 0,
.dry = 0,
.oneshot = 0,
.quiet = 0,
.defrag = 1,
};
static volatile int stop = 0;
static volatile int keep_running = 1;
static dev_t compact_dev;
static void sigint_handler(int signo)
{
stop = 1;
keep_running = 0;
}
static void *vzevent_monitor(void *arg)
{
struct vzctl_state_evt *s;
int n;
vzevt_handle_t *evt = (vzevt_handle_t *) arg;
int fd = evt->sock;
while (keep_running) {
vzevt_t *e = NULL;
fd_set rfds;
FD_ZERO(&rfds);
FD_SET(fd, &rfds);
n = select(fd + 1, &rfds, NULL, NULL, NULL);
if (n < 0) {
if (errno == EINTR)
continue;
vzctl2_log(-1, errno, "vzevent_monitor: select(): %m");
break;
}
if (FD_ISSET(fd, &rfds) && vzevt_recv(evt, &e) == 1) {
s = (struct vzctl_state_evt *) e->buffer;
if (e->type == VZEVENT_VZCTL_EVENT_TYPE &&
s->state == VZCTL_ENV_UMOUNT &&
compact_dev == s->dev) {
vzctl2_log(0, 0, "Cancel compacting %s", s->ctid);
stop = 1;
}
vzevt_free(e);
}
}
return NULL;
}
static void log_start(const char *uuid, const char *task_id, const struct vzctl_compact_param *param)
{
char out[1024];
snprintf(out, sizeof(out), "{\"operation\":\"pcompactStart\", \"uuid\":\"%s\", "
"\"task_id\":\"%s\", \"config_dry\":%d, \"config_threhshold\":%d}",
uuid, task_id, param->dry, param->threshold);
syslog(LOG_INFO, "%s", out);
}
static void log_finish(const char *uuid, const char *task_id, int code)
{
char out[1024];
snprintf(out, sizeof(out), "{\"operation\":\"pcompactFinish\", \"uuid\":\"%s\", "
"\"task_id\":\"%s\", \"was_compacted\":1, \"result\":%d}",
uuid, task_id, code);
syslog(LOG_INFO, "%s", out);
}
static void log_cancel(const char *uuid, const char *task_id)
{
char out[1024];
snprintf(out, sizeof(out), "{\"operation\":\"pcompactFinish\", \"uuid\":\"%s\", "
"\"task_id\":\"%s\", \"was_compacted\":0}",
uuid, task_id);
syslog(LOG_INFO, "%s", out);
}
static int parse_config()
{
struct vzctl_config *conf;
const char *res;
int err;
conf = vzctl2_conf_open(COMPACT_CONF, 0, &err);
if (err)
return -1;
res = NULL;
vzctl2_conf_get_param(conf, "THRESHOLD", &res);
if (res)
config.threshold = atoi(res);
res = NULL;
vzctl2_conf_get_param(conf, "IMAGE_DEFRAG_THRESHOLD", &res);
if (res)
config.image_defrag_threshold = atoi(res);
res = NULL;
vzctl2_conf_get_param(conf, "DELTA", &res);
if (res)
config.delta = atoi(res);
res = NULL;
vzctl2_conf_get_param(conf, "DEFRAG", &res);
if (res)
config.defrag = (strcmp(res, "yes") == 0);
vzctl2_conf_close(conf);
return err;
}
int state_fd = -1;
static int open_state_file()
{
int fd, err;
char buf[20];
fd = open(COMPACT_STATE, O_CREAT | O_RDWR, 0644);
if (fd == -1) {
vzctl2_log(-1, errno, "Can't create a pid file: %s", COMPACT_STATE);
return -1;
}
/* Prevent to execute two instances simultaneously */
err = flock(fd, LOCK_EX | LOCK_NB);
if (err == -1) {
vzctl2_log(-1, errno, "Can't lock %s", COMPACT_STATE);
close(fd);
return err;
}
memset(buf, 0, sizeof(buf));
err = read(fd, buf, sizeof(buf) - 1);
if (err == -1) {
vzctl2_log(-1, errno, "Can't read %s", COMPACT_STATE);
close(fd);
}
state_fd = fd;
if (err == 0)
return INT_MAX;
return atoi(buf);
}
/* Save the current VPS number in the start file */
static int update_stat_file(const int state)
{
int err;
err = ftruncate(state_fd, 0);
if (err == -1) {
vzctl2_log(-1, errno, "Can't truncate the state file %s", COMPACT_STATE);
return -1;
}
lseek(state_fd, 0, SEEK_SET);
err = dprintf(state_fd, "%d", state);
if (err == -1) {
vzctl2_log(-1, errno, "Can't write the state file %s", COMPACT_STATE);
return err;
}
return 0;
}
/**
* Enumerate all VPSs and compact their disks.
* If this functions is interrupted, it will start from
* the next VPS than in a previous case.
* */
static int scan()
{
int i, err, pstate, vps;
struct vps_list vpses;
err = vps_get_list(&vpses);
if (err)
return 1;
pstate = open_state_file();
if (pstate < 0) {
closelog();
vps_list_free(&vpses);
return -1;
}
if (pstate > vpses.num)
pstate = 0;
else
pstate++; /* start from the next one */
openlog("pcompact", LOG_PID, LOG_INFO | LOG_USER);
for (i = 0; i < vpses.num && keep_running; i++) {
int ret = 0;
int flags = 0;
int isCompactEnabled = 0;
ctid_t ctID;
struct vzctl_env_handle *h = NULL;
char task_id[39] = "";
uuid_t task_uuid;
struct vzctl_compact_param param = {
.defrag = config.defrag,
.threshold = config.threshold,
.delta = config.delta,
.dry = config.dry,
.stop = (int *)&stop,
.compact_dev = &compact_dev,
};
vps = (i + pstate) % vpses.num;
vzctl2_log(0, 0, "Inspect %s", vpses.vpses[vps].uuid);
err = update_stat_file(vps);
if (err < 0)
goto out;
if (vzctl2_parse_ctid(vpses.vpses[vps].uuid, ctID)) {
vzctl2_log(-1, 0, "Error: Invalid CT ID %s. Skip it", vpses.vpses[vps].uuid);
continue;
}
h = vzctl2_env_open(ctID, flags, &ret);
if (ret || !h){
vzctl2_log(-1, 0, "Error [%d]: cannot open CT [%s]. Skip it", ret, ctID);
continue;
}
stop = 0;
vzctl2_env_get_autocompact(vzctl2_get_env_param(h), &isCompactEnabled);
if (isCompactEnabled == 0) {
//skip current CT
vzctl2_env_close(h);
continue;
}
uuid_generate(task_uuid);
uuid_unparse(task_uuid, task_id);
log_start(vpses.vpses[vps].uuid, task_id, ¶m);
ret = vzctl2_env_compact(h, ¶m, sizeof(param));
if (ret) {
vzctl2_log(-1, 0, "vzctl2_env_compact return error code: %d", ret);
log_cancel(vpses.vpses[vps].uuid, task_id);
} else
log_finish(vpses.vpses[vps].uuid, task_id, err);
vzctl2_env_close(h);
if (config.oneshot)
break;
}
out:
closelog();
vps_list_free(&vpses);
return 0;
}
static void usage(char **argv)
{
vzctl2_log(-1, 0, "Usage:\n"
"\t%s [-vnsq] [-t timeout[smh]] [-D]\n\n"
"Options:\n"
" -v\tincrease verbosity.\n"
"\tCould be used several times to increase verbosity higher\n"
" -n\tprint the actions that would be executed, but do not execute them\n"
" -s\tcompact only first not yet compacted disk\n"
" -t\twork only specified time. Suffixes for seconds, minutes and hours are allowed\n"
" -q\tdisable printing of non-error messages to the standard output (console).\n"
" -D\tdefragment file system only.\n",
argv[0]);
}
static int settimer(const char *opt)
{
timer_t timer;
struct itimerspec its = {};
char * endptr;
int val = strtoul(opt, &endptr, 0);
if (strlen(endptr) > 1) {
vzctl2_log(-1, 0, "Invalid argument - %s", opt);
return -1;
}
switch (*endptr)
{
case 's': case 'S': case 0:
break;
case 'm': case 'M':
val *= 60;
break;
case 'h': case 'H':
val *= 3600; break;
default:
vzctl2_log(-1, 0, "Invalid argument - %s", opt);
return -1;
};
its.it_value.tv_sec = val;
if (timer_create(CLOCK_MONOTONIC, NULL, &timer) == -1) {
vzctl2_log(-1, errno, "Can't set up a timer");
return 1;
}
if (timer_settime(timer, 0, &its, NULL) == -1) {
vzctl2_log(-1, errno, "Can't set up a timer");
return 1;
}
return 0;
}
int main(int argc, char **argv)
{
int err, opt;
struct sigaction sa = {
.sa_handler = sigint_handler,
.sa_flags = SA_RESTART,
};
vzctl2_lib_init();
vzctl2_init_log("pcompact");
err = parse_config();
if (err < 0) {
fprintf(stderr, "Can't parse config: %s\n", COMPACT_CONF);
return 1;
}
while ((opt = getopt(argc, argv, "nvst:qD")) != -1) {
switch (opt) {
case 'v':
config.log_level++;
break;
case 'n':
config.dry = 1;
break;
case 's':
config.oneshot = 1;
break;
case 't':
if (settimer(optarg))
return 1;
break;
case 'q':
config.quiet = 1;
break;
case 'D':
config.defrag = 2;
break;
default:
usage(argv);
exit(1);
}
}
vzctl2_set_log_file(COMPACT_LOG_FILE);
vzctl2_set_log_enable(1);
vzctl2_set_log_quiet(config.quiet);
vzctl2_set_log_level(config.log_level);
vzctl2_set_log_verbose(config.log_level);
sigemptyset(&sa.sa_mask);
if (sigaction(SIGINT, &sa, NULL) ||
sigaction(SIGALRM, &sa, NULL) ||
sigaction(SIGTERM, &sa, NULL)) {
vzctl2_log(-1, errno, "Can't set signal handler");
exit(1);
}
vzevt_handle_t *evt;
pthread_t evt_th;
if (vzevt_register(&evt)) {
syslog(LOG_ERR, "Unable to register vzevent handler");
return 1;
}
if (pthread_create(&evt_th, NULL, vzevent_monitor, evt)) {
syslog( LOG_ERR, "pthread_create: %m");
return 1;
}
scan();
keep_running = 0;
pthread_kill(evt_th, SIGTERM);
pthread_join(evt_th, NULL);
vzevt_unregister(evt);
vzctl2_lib_close();
return 0;
}