-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathenv.c
More file actions
3798 lines (3071 loc) · 86 KB
/
env.c
File metadata and controls
3798 lines (3071 loc) · 86 KB
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 1999-2017, Parallels International GmbH
* Copyright (c) 2017-2019 Virtuozzo International GmbH. All rights reserved.
*
* This file is part of OpenVZ libraries. OpenVZ is free software; you can
* redistribute it and/or modify it under the terms of the GNU Lesser General
* Public License as published by the Free Software Foundation; either version
* 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see
* <http://www.gnu.org/licenses/> or write to Free Software Foundation,
* 51 Franklin Street, Fifth Floor Boston, MA 02110, USA.
*
* Our contact details: Virtuozzo International GmbH, Vordergasse 59, 8200
* Schaffhausen, Switzerland.
*
*/
#define _GNU_SOURCE
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <signal.h>
#include <fcntl.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/resource.h>
#include <sys/sysmacros.h>
#include <string.h>
#include <assert.h>
#include <sys/ioctl.h>
#include <sys/mount.h>
#include <linux/vzcalluser.h>
#include <time.h>
#include <grp.h>
#include <sys/utsname.h>
#include <mntent.h>
#include <uuid/uuid.h>
#include <ext2fs/ext2_fs.h>
#include <grp.h>
#include "env.h"
#include "cgroup.h"
#include "env_config.h"
#include "env_configure.h"
#include "vzerror.h"
#include "vztypes.h"
#include "config.h"
#include "util.h"
#include "exec.h"
#include "net.h"
#include "meminfo.h"
#include "dev.h"
#include "readelf.h"
#include "vz.h"
#include "vzctl_param.h"
#include "veth.h"
#include "ub.h"
#include "dist.h"
#include "vztypes.h"
#include "lock.h"
#include "image.h"
#include "disk.h"
#include "tc.h"
#include "env_ops.h"
#include "ha.h"
#include "wrap.h"
#include "destroy.h"
#include "create.h"
#define ENVRETRY 3
#define LINUX_REBOOT_MAGIC1 0xfee1dead
#define LINUX_REBOOT_MAGIC2 672274793
#define LINUX_REBOOT_CMD_POWER_OFF 0x4321FEDC
const char *vzctl2_get_version()
{
return PACKAGE_VERSION;
}
int real_env_stop(int stop_mode)
{
logger(10, 0, "* stop mode %d", stop_mode);
/* Disable fsync. The fsync will be done by umount() */
configure_sysctl("/proc/sys/fs/fsync-enable", "0");
configure_sysctl("/sys/fs/cgroup/systemd/release_agent", "");
configure_sysctl("/sys/fs/cgroup/systemd/notify_on_release", "0");
switch (stop_mode) {
case M_HALT: {
char *argv[] = {"halt", NULL};
char *argv_init[] = {"init", "0", NULL};
execvep(argv[0], argv, NULL);
execvep(argv_init[0], argv_init, NULL);
break;
}
case M_REBOOT: {
char *argv[] = {"reboot", NULL};
execvep(argv[0], argv, NULL);
break;
}
case M_KILL:
return syscall(__NR_reboot, LINUX_REBOOT_MAGIC1,
LINUX_REBOOT_MAGIC2,
LINUX_REBOOT_CMD_POWER_OFF, NULL);
}
return -1;
}
static int run_start_script(struct vzctl_env_handle *h)
{
char buf[STR_SIZE];
char *arg[2];
char *env[2];
char s_veid[STR_SIZE];
arg[0] = get_script_path(VZCTL_START, buf, sizeof(buf));
arg[1] = NULL;
snprintf(s_veid, sizeof(s_veid), "VEID=%s", EID(h));
env[0] = s_veid;
env[1] = NULL;
return vzctl2_wrap_exec_script(arg, env, 0);
}
int run_stop_script(struct vzctl_env_handle *h)
{
char script[STR_SIZE];
char buf[STR_SIZE];
char *env[6] = {};
int ret, i = 0;
const char *bandwidth = NULL;
char *arg[] = {get_script_path(VZCTL_STOP, script, sizeof(script)), NULL};
snprintf(buf, sizeof(buf), "VEID=%s", EID(h));
ret = xstrdup(&env[i++], buf);
if (ret)
return ret;
if (h->env_param->vz->tc->traffic_shaping == VZCTL_PARAM_ON) {
ret = xstrdup(&env[i++], "TRAFFIC_SHAPING=yes");
if (ret)
goto err;
/* BANDWIDTH is needed for tc class removal */
vzctl2_env_get_param(h, "BANDWIDTH", &bandwidth);
if (bandwidth != NULL) {
snprintf(buf, sizeof(buf), "BANDWIDTH=%s", bandwidth);
ret = xstrdup(&env[i++], buf);
if (ret)
goto err;
}
}
struct vzctl_veth_param *veth = h->env_param->veth;
if (!list_empty(&veth->dev_list)) {
char *pn;
char *pm;
struct vzctl_veth_dev *it;
int len = sizeof("VETH=");
int len1 = sizeof("HMAC=");
list_for_each(it, &veth->dev_list, list) {
len += strlen(it->dev_name) + 1;
len1 += strlen(it->mac) + 1;
}
pn = malloc(len);
pm = malloc(len1);
if (pn == NULL || pm == NULL) {
free(pn);
free(pm);
env[i] = NULL;
ret = VZCTL_E_NOMEM;
goto err;
}
env[i++] = pn;
pn += sprintf(pn, "VETH=");
env[i++] = pm;
pm += sprintf(pm, "HMAC=");
list_for_each(it, &veth->dev_list, list) {
pn += sprintf(pn, "%s ", it->dev_name);
pm += sprintf(pm, "%s ", it->mac);
}
}
env[i] = NULL;
ret = vzctl2_wrap_exec_script(arg, env, 0);
err:
free_ar_str(env);
return ret;
}
int is_env_run(struct vzctl_env_handle *h)
{
return get_env_ops()->is_env_run(h);
}
int wait_env_state(struct vzctl_env_handle *h, int state, unsigned int timeout)
{
int i, rc;
for (i = 0; i < timeout * 2; i++) {
rc = is_env_run(h);
switch (state) {
case VZCTL_ENV_STARTED:
if (rc == 1)
return 0;
break;
case VZCTL_ENV_STOPPED:
if (rc == 0)
return 0;
break;
}
usleep(500000);
}
return vzctl_err(-1, 0, "Wait CT state %s timed out",
state == VZCTL_ENV_STARTED ? "started" : "stopped");
}
static int do_env_stop(struct vzctl_env_handle *h, int stop_mode)
{
/* Unregister before real stop to avoid races with vzevend */
vzctl2_unregister_running_state(h->env_param->fs->ve_private);
if (get_env_ops()->env_stop(h, stop_mode))
return vzctl_err(VZCTL_E_ENV_STOP, 0,
"Unable to stop the Container:"
" operation timed out");
return 0;
}
static int do_env_post_stop(struct vzctl_env_handle *h, int flags)
{
int ret = 0;
if (!(flags & VZCTL_SKIP_UMOUNT))
ret = vzctl2_env_umount(h, flags);
run_stop_script(h);
return ret;
}
int vzctl_env_stop(struct vzctl_env_handle *h, stop_mode_e stop_mode, int flags)
{
int ret;
struct vzctl_env_status env_status = {};
vzctl2_get_env_status_info(h, &env_status, ENV_STATUS_RUNNING);
if (!(env_status.mask & ENV_STATUS_RUNNING)) {
if (flags & VZCTL_FORCE)
goto force;
return vzctl_err(0, 0, "Container is not running");
} else if (flags & VZCTL_FORCE)
return 0;
logger(0, 0, "Stopping the Container ...");
if (env_status.mask & (ENV_STATUS_CPT_SUSPENDED | ENV_STATUS_CPT_UNDUMPED)) {
struct vzctl_cpt_param cpt_param = {};
logger(0, 0, "The Container is in the %s state",
(env_status.mask & ENV_STATUS_CPT_SUSPENDED) ? "suspended" : "undumped");
ret = vzctl2_cpt_cmd(h,
(env_status.mask & ENV_STATUS_CPT_SUSPENDED) ? VZCTL_CMD_CHKPNT : VZCTL_CMD_RESTORE,
VZCTL_CMD_RESUME, &cpt_param, flags);
if (ret)
return vzctl_err(VZCTL_E_ENV_STOP, 0, "Unable to stop the Container");
}
if (!(flags & VZCTL_SKIP_ACTION_SCRIPT)) {
char buf[PATH_MAX];
get_action_script_path(h, VZCTL_STOP_PREFIX, buf, sizeof(buf));
if (stat_file(buf) &&
vzctl2_wrap_env_exec_script(h, NULL, NULL, buf, 0, EXEC_LOG_OUTPUT))
{
return vzctl_err(VZCTL_E_ACTIONSCRIPT, 0,
"Error executing stop script %s", buf);
}
}
ret = do_env_stop(h, stop_mode);
if (ret)
return ret;
logger(0, 0, "Container was stopped");
force:
return do_env_post_stop(h, flags);
}
int vzctl2_env_stop(struct vzctl_env_handle *h, stop_mode_e stop_mode, int flags)
{
if (vzctl2_get_flags() & VZCTL_FLAG_DONT_USE_WRAP)
return vzctl_env_stop(h, stop_mode, flags);
return vzctl_wrap_env_stop(h, stop_mode, flags);
}
int vzctl2_env_pause(struct vzctl_env_handle *h, int flags)
{
int ret;
struct vzctl_cpt_param cpt = {};
struct vzctl_env_status env_status = {};
vzctl2_get_env_status_info(h, &env_status, ENV_STATUS_RUNNING);
if (!(env_status.mask & ENV_STATUS_RUNNING))
return vzctl_err(VZCTL_E_ENV_NOT_RUN, 0,
"Container is not running");
if (env_status.mask & ENV_STATUS_CPT_SUSPENDED)
return vzctl_err(VZCTL_E_ENV_RUN, 0,
"Container is already paused");
logger(0, 0, "Pause the Container ...");
ret = vzctl2_cpt_cmd(h, 0, VZCTL_CMD_SUSPEND, &cpt, flags);
if (ret)
return vzctl_err(ret, 0, "Unable to pause the Container");
logger(0, 0, "The Container has been successfully paused");
vzctl2_send_state_evt(EID(h), VZCTL_ENV_SUSPENDED);
return 0;
}
#define K_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
static int get_virt_osrelease(struct vzctl_env_handle *h)
{
int ret;
int min_a, min_b, min_c;
int max_a, max_b, max_c;
int cur_a, cur_b, cur_c;
char tm_osrelease[STR_SIZE];
char osrelease[STR_SIZE];
struct utsname utsbuf;
const char *tail;
const char *ostmpl = h->env_param->tmpl->ostmpl;
if (h->env_param->tmpl->osrelease != NULL || ostmpl == NULL)
return 0;
if (uname(&utsbuf) != 0)
return vzctl_err(-1, errno, "uname() failed");
ret = vztmpl_get_osrelease(ostmpl, tm_osrelease, sizeof(tm_osrelease));
if (ret)
return ret;
/* Osrelease is not provided */
if (tm_osrelease[0] == '\0')
return 0;
logger(2, 0, "Template %s osrelease: %s", ostmpl, tm_osrelease);
ret = sscanf(utsbuf.release, "%d.%d.%d",
&cur_a, &cur_b, &cur_c);
if (ret != 3)
return vzctl_err(-1, 0, "Unable to parse node release: %s",
utsbuf.release);
ret = sscanf(tm_osrelease, "%d.%d.%d:%d.%d.%d",
&min_a, &min_b, &min_c,
&max_a, &max_b, &max_c);
if (ret != 3 && ret != 6)
return vzctl_err(-1, 0, "Incorrect osrelease syntax: %s", ostmpl);
if (K_VERSION(cur_a, cur_b, cur_c) < K_VERSION(min_a, min_b, min_c)) {
cur_a = min_a; cur_b = min_b; cur_c = min_c;
}
if (ret == 6 && (K_VERSION(cur_a, cur_b, cur_c) > K_VERSION(max_a, max_b, max_c))) {
cur_a = max_a; cur_b = max_b; cur_c = max_c;
}
/* Make kernel version Vz specific like A.B.C-028stab070.1 */
tail = strchr(utsbuf.release, '-');
snprintf(osrelease, sizeof(osrelease), "%d.%d.%d%s",
cur_a, cur_b, cur_c, tail ? tail : "");
return xstrdup(&h->env_param->tmpl->osrelease, osrelease);
}
static void fix_ub(struct vzctl_ub_param *ub)
{
unsigned long min_ipt;
if (ub->numiptent == NULL) {
ub->numiptent = malloc(sizeof(struct vzctl_2UL_res));
if (ub->numiptent)
ub->numiptent->b = ub->numiptent->l = DEF_NUMIPTENT;
}
min_ipt = min_ul(ub->numiptent->b, ub->numiptent->l);
if (min_ipt < MIN_NUMIPTENT) {
logger(0, 0, "Warning: NUMIPTENT %lu:%lu is less"
" than minimally allowable value, set to %d:%d",
ub->numiptent->b, ub->numiptent->l,
MIN_NUMIPTENT, MIN_NUMIPTENT);
ub->numiptent->b = ub->numiptent->l = MIN_NUMIPTENT;
}
if (ub->numproc == NULL) {
ub->numproc = malloc(sizeof(struct vzctl_2UL_res));
if (ub->numproc)
ub->numproc->b = ub->numproc->l = 131072;
}
}
static void fix_cpu_param(struct vzctl_cpu_param *cpu)
{
assert(cpu);
if (cpu->units == NULL && cpu->weight == NULL) {
cpu->units = malloc(sizeof(unsigned long));
*cpu->units = VZCTL_CPUUNITS_DEFAULT;
}
}
static void fix_param(struct vzctl_env_param *env)
{
fix_ub(env->res->ub);
fix_cpu_param(env->cpu);
/* enable bridge by default #PSBM-50520 */
if (!(env->features->known & VE_FEATURE_BRIDGE)) {
env->features->known |= VE_FEATURE_BRIDGE;
env->features->mask |= VE_FEATURE_BRIDGE;
}
}
#define INITTAB_FILE "/etc/inittab"
#define INITTAB_VZID "vz:"
#define INITTAB_ACTION INITTAB_VZID "12345:once:touch " VZFIFO_FILE
#define EVENTS_DIR "/etc/event.d/"
#define EVENTS_FILE EVENTS_DIR "call_on_default_rc"
#define EVENTS_SCRIPT \
"# This task runs if default runlevel is reached\n" \
"start on stopped rc2\n" \
"start on stopped rc3\n" \
"start on stopped rc4\n" \
"start on stopped rc5\n" \
"exec touch " VZFIFO_FILE "\n"
#define EVENTS_DIR_UBUNTU "/etc/init/"
#define EVENTS_FILE_UBUNTU EVENTS_DIR_UBUNTU "call_on_default_rc.conf"
#define EVENTS_SCRIPT_UBUNTU \
"# tell vzctl that start was successfull\n" \
"#\n" \
"# This task causes to tell vzctl that start was successfull\n" \
"\n" \
"description \"tell vzctl that start was successfull\"\n" \
"\n" \
"start on stopped rc RUNLEVEL=[2345]\n" \
"\n" \
"task\n" \
"\n" \
"exec touch " VZFIFO_FILE
#define MAX_WAIT_TIMEOUT 60 * 60
#define SYSTEMD_BIN "systemd"
#define SBIN_INIT "/sbin/init"
static int add_inittab_entry(const char *entry, const char *id)
{
FILE *rfp = NULL;
int wfd =1, len, err = -1, found = 0;
struct stat st;
char buf[PATH_MAX];
if (stat(INITTAB_FILE, &st))
return vzctl_err(-1, errno, "Can't stat "INITTAB_FILE);
if ((rfp = fopen(INITTAB_FILE, "r")) == NULL)
return vzctl_err(-1, errno, "Unable to open " INITTAB_FILE);
wfd = open(INITTAB_FILE ".tmp", O_WRONLY|O_TRUNC|O_CREAT, st.st_mode);
if (wfd == -1) {
logger(-1, errno, "Unable to open " INITTAB_FILE ".tmp");
goto err;
}
set_fattr(wfd, &st);
while (!feof(rfp)) {
if (fgets(buf, sizeof(buf), rfp) == NULL) {
if (ferror(rfp))
goto err;
break;
}
if (!strcmp(buf, entry)) {
found = 1;
break;
}
if (id != NULL && !strncmp(buf, id, strlen(id)))
continue;
len = strlen(buf);
if (write(wfd, buf, len) == -1) {
logger(-1, errno, "Unable to write to " INITTAB_FILE);
goto err;
}
}
if (!found) {
if (write(wfd, entry, strlen(entry)) == -1 ||
write(wfd, "\n", 1) == -1)
{
logger(-1, errno, "Unable to write to " INITTAB_FILE);
goto err;
}
if (rename(INITTAB_FILE ".tmp", INITTAB_FILE)) {
logger(-1, errno, "Unable to rename " INITTAB_FILE);
goto err;
}
}
err = 0;
err:
if (wfd != -1)
close(wfd);
if (rfp != NULL)
fclose(rfp);
unlink(INITTAB_FILE ".tmp");
return err;
}
static int create_file(const char* filename, const char *data, size_t size)
{
int wfd;
wfd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, 0644);
if (wfd == -1) {
fprintf(stderr, "Unable to create %s %s\n",
filename, strerror(errno));
return -1;
}
if (write(wfd, data, size) == -1) {
fprintf(stderr, "Unable to write to %s %s\n",
filename, strerror(errno));
close(wfd);
return -1;
}
close(wfd);
return 0;
}
static int replace_reach_runlevel_mark(void)
{
int ret, err, is_upstart = 0;
struct stat st;
unlink(VZFIFO_FILE);
if (mkfifo(VZFIFO_FILE, 0644)) {
fprintf(stderr, "Unable to create " VZFIFO_FILE " %s\n",
strerror(errno));
return -1;
}
/* Create upstart specific script */
if (!stat(EVENTS_DIR_UBUNTU, &st)) {
is_upstart = 1;
ret = create_file(EVENTS_FILE_UBUNTU,
EVENTS_SCRIPT_UBUNTU, sizeof(EVENTS_SCRIPT_UBUNTU) - 1);
if (ret)
return -1;
} else if (!stat(EVENTS_DIR, &st)) {
is_upstart = 1;
ret = create_file(EVENTS_FILE,
EVENTS_SCRIPT, sizeof(EVENTS_SCRIPT) - 1);
if (ret)
return -1;
}
if (stat(INITTAB_FILE, &st)) {
if (is_upstart || is_systemd())
return 0;
fprintf(stderr, "Warning: unable to stat " INITTAB_FILE " %s\n",
strerror(errno));
return -1;
}
err = add_inittab_entry(INITTAB_ACTION, INITTAB_VZID);
return err;
}
static int check_requires(struct vzctl_env_param *env, int flags)
{
int ret;
unsigned long mask;
if ((ret = check_var(env->fs->ve_private, "VE_PRIVATE is not set")))
return ret;
if ((ret = check_var(env->fs->ve_root, "VE_ROOT is not set")))
return ret;
if ((ret = check_res_requires(env)))
return ret;
if ((mask = vzctl2_check_tech(env->features->tech))) {
char buf[512];
tech2str(mask, buf, sizeof(buf));
return vzctl_err(VZCTL_E_UNSUP_TECH, 0, "Unable to start Container"
" unsupported technologie(s) required: %s", buf);
}
if (env->misc->start_disabled == VZCTL_PARAM_ON &&
!(flags & VZCTL_FORCE))
return vzctl_err(VZCTL_E_ENV_START_DISABLED, 0,
"Container start disabled");
if (env->misc->ve_type == VZCTL_ENV_TYPE_TEMPLATE)
return vzctl_err(VZCTL_E_ENV_START_DISABLED, 0,
"Container is template"
" therefore cannot be started");
if (stat_file(env->fs->ve_private) != 1)
return vzctl_err(VZCTL_E_NO_PRVT, 0,
"Container private area %s does not exist",
env->fs->ve_private);
if (env->fs->layout >= VZCTL_LAYOUT_5 &&
find_root_disk(env->disk) == NULL)
return vzctl_err(VZCTL_E_INVAL, 0,
"Container root disk is not configured");
return ret;
}
static void restore_mtab(void)
{
struct stat st;
if (stat("/etc/mtab", &st) == 0 && S_ISLNK(st.st_mode))
return;
if (unlink("/etc/mtab") && errno != ENOENT)
logger(-1, errno, "failed to unlink /etc/mtab");
if (symlink("/proc/mounts", "/etc/mtab"))
logger(-1, errno, "symlink(/etc/mtab, /proc/mounts");
}
static struct devnode {
int major;
int minor;
const char *name;
mode_t mode;
const char *group;
} _g_devs[] = {
{2, 0x0, "/dev/ptyp0", S_IFCHR|0600},
{2, 0x1, "/dev/ptyp1", S_IFCHR|0600},
{2, 0x2, "/dev/ptyp2", S_IFCHR|0600},
{2, 0x3, "/dev/ptyp3", S_IFCHR|0600},
{2, 0x4, "/dev/ptyp4", S_IFCHR|0600},
{2, 0x5, "/dev/ptyp5", S_IFCHR|0600},
{2, 0x6, "/dev/ptyp6", S_IFCHR|0600},
{2, 0x7, "/dev/ptyp7", S_IFCHR|0600},
{2, 0x8, "/dev/ptyp8", S_IFCHR|0600},
{2, 0x9, "/dev/ptyp9", S_IFCHR|0600},
{2, 0xa, "/dev/ptypa", S_IFCHR|0600},
{2, 0xb, "/dev/ptypb", S_IFCHR|0600},
{3, 0x0, "/dev/ttyp0", S_IFCHR|0600},
{3, 0x1, "/dev/ttyp1", S_IFCHR|0600},
{3, 0x2, "/dev/ttyp2", S_IFCHR|0600},
{3, 0x3, "/dev/ttyp3", S_IFCHR|0600},
{3, 0x4, "/dev/ttyp4", S_IFCHR|0600},
{3, 0x5, "/dev/ttyp5", S_IFCHR|0600},
{3, 0x6, "/dev/ttyp6", S_IFCHR|0600},
{3, 0x7, "/dev/ttyp7", S_IFCHR|0600},
{3, 0x8, "/dev/ttyp8", S_IFCHR|0600},
{3, 0x9, "/dev/ttyp9", S_IFCHR|0600},
{3, 0xa, "/dev/ttypa", S_IFCHR|0600},
{3, 0xb, "/dev/ttypb", S_IFCHR|0600},
{5, 0x2, "/dev/ptmx", S_IFCHR|0666},
{5, 0x0, "/dev/tty", S_IFCHR|0666},
{5, 0x1, "/dev/console", S_IFCHR|0600},
{4, 0x0, "/dev/tty0", S_IFCHR|0620, "tty"},
{4, 0x1, "/dev/tty1", S_IFCHR|0620, "tty"},
{4, 0x2, "/dev/tty2", S_IFCHR|0620, "tty"},
{4, 0x3, "/dev/tty3", S_IFCHR|0620, "tty"},
{4, 0x4, "/dev/tty4", S_IFCHR|0620, "tty"},
{4, 0x5, "/dev/tty5", S_IFCHR|0620, "tty"},
{4, 0x6, "/dev/tty6", S_IFCHR|0620, "tty"},
{4, 0x7, "/dev/tty7", S_IFCHR|0620, "tty"},
{4, 0x8, "/dev/tty8", S_IFCHR|0620, "tty"},
{4, 0x9, "/dev/tty9", S_IFCHR|0620, "tty"},
{4, 0xa, "/dev/tty10", S_IFCHR|0620, "tty"},
{4, 0xb, "/dev/tty11", S_IFCHR|0620, "tty"},
{4, 0xc, "/dev/tty12", S_IFCHR|0620, "tty"},
{1, 0x3, "/dev/null", S_IFCHR|0666},
{1, 0x5, "/dev/zero", S_IFCHR|0666},
{1, 0x7, "/dev/full", S_IFCHR|0666},
{1, 0x8, "/dev/random", S_IFCHR|0666},
{1, 0x9, "/dev/urandom", S_IFCHR|0666},
{10, 235, "/dev/autofs", S_IFCHR|0600},
{1, 11, "/dev/kmsg", S_IFCHR|0644},
};
static int setup_devtmpfs()
{
int i, ret = 0;
logger(10, 0, "Setup devtmpfs");
if (mount("none", "/dev", "devtmpfs", MS_NOSUID|MS_STRICTATIME, "mode=755"))
return vzctl_err(-1, errno, "Failed to mount devtmpfs");
mode_t m = umask(000);
for (i = 0; i < sizeof(_g_devs)/sizeof(_g_devs[0]); i++) {
dev_t dev = makedev(_g_devs[i].major, _g_devs[i].minor);
if (mknod(_g_devs[i].name, _g_devs[i].mode, dev) &&
errno != EEXIST)
{
ret = vzctl_err(-1, errno, "Failed to create %s",
_g_devs[i].name);
break;
}
if (_g_devs[i].group) {
struct group *g = getgrnam(_g_devs[i].group);
if (g) {
if (chown(_g_devs[i].name, 0, g->gr_gid))
vzctl_err(-1, errno, "Failed to chown(%s, 0, %d)",
_g_devs[i].name, g->gr_gid);
}
}
}
umask(m);
return ret;
}
static void set_def_rlimits()
{
int i;
struct rlimit rl[] = {
[RLIMIT_CPU] = {RLIM_INFINITY, RLIM_INFINITY},
[RLIMIT_FSIZE] = {RLIM_INFINITY, RLIM_INFINITY},
[RLIMIT_DATA] = {RLIM_INFINITY, RLIM_INFINITY},
[RLIMIT_STACK] = {8*1024*1024, RLIM_INFINITY},
[RLIMIT_CORE] = {0, RLIM_INFINITY},
[RLIMIT_RSS] = {RLIM_INFINITY, RLIM_INFINITY},
[RLIMIT_NPROC] = {62987, 62987},
[RLIMIT_NOFILE] = {1024, 4096},
[RLIMIT_MEMLOCK] = {65536, 65536},
[RLIMIT_AS] = {RLIM_INFINITY, RLIM_INFINITY},
[RLIMIT_LOCKS] = {RLIM_INFINITY, RLIM_INFINITY},
};
for (i = 0; i < sizeof(rl)/ sizeof(rl[0]); i++)
{
if (setrlimit(i, &rl[i]))
vzctl_err(-1, errno, "Failed setrlimit(%d)", i);
}
}
int pre_setup_env(const struct start_param *param)
{
struct vzctl_env_param *env = param->h->env_param;
int fd;
int ret;
int errcode = 0;
/* Clear supplementary group IDs */
if (setgroups(0, NULL))
return vzctl_err(VZCTL_E_SYSTEM, errno, "setgroups");
errcode = set_personality32();
if (errcode)
return errcode;
/* Create /fastboot to skip run fsck */
fd = creat("/fastboot", 0644);
if (fd != -1)
close(fd);
const char *hn = env->misc->hostname ?: "localhost.localdomain";
if (sethostname(hn, strlen(hn)))
return vzctl_err(VZCTL_E_SYSTEM, errno, "Failed to set hostname %s", hn);
if (access("/proc", F_OK))
mkdir("/proc", 0555);
if (mount("proc", "/proc", "proc", 0, 0))
return vzctl_err(VZCTL_E_SYSTEM, errno, "Failed to mount /proc");
if (setup_devtmpfs())
return VZCTL_E_SYSTEM;
if (stat_file("/sys"))
mount("sysfs", "/sys", "sysfs", 0, 0);
if (env->features->mask & VE_FEATURE_NFSD) {
mount("nfsd", "/proc/fs/nfsd", "nfsd", 0, 0);
make_dir("/var/lib/nfs/rpc_pipefs", 1);
mount("sunrpc", "/var/lib/nfs/rpc_pipefs", "rpc_pipefs", 0, 0);
}
clean_static_dev("ploop");
create_root_dev(NULL);
unlink("/reboot");
unlink(VZFIFO_FILE);
if (env->fs->layout >= VZCTL_LAYOUT_5)
restore_mtab();
if (env->dq->ugidlimit != NULL && *env->dq->ugidlimit != 0) {
ret = setup_env_quota(get_user_quota_mode(env->dq));
if (ret)
return ret;
}
if (env->fs->layout >= VZCTL_LAYOUT_5 && env->disk != NULL &&
!is_secondary_disk_present(env->disk))
{
env_fin_configure_disk(env->disk);
}
if (env->opts->wait == VZCTL_PARAM_ON &&
replace_reach_runlevel_mark())
return VZCTL_E_WAIT;
if (env_configure_udev_rules())
return VZCTL_E_SYSTEM;
configure_sysctl("/proc/sys/net/ipv6/conf/all/forwarding", "0");
logger(10, 0, "* Report env_created");
/* report that environment is created. */
if (write(param->h->ctx->status_p[1], &errcode, sizeof(errcode)) == -1)
vzctl_err(-1, errno, "Failed write(param->status_p[1])");
logger(10, 0, "* Wait parent");
/* Now we wait until Container setup will be done
* If no error, then start init, otherwise exit.
*/
if (TEMP_FAILURE_RETRY(read(param->h->ctx->wait_p[0], &errcode, sizeof(errcode))) == 0) {
logger(0, 0, "Cancel init execution");
return -1;
}
logger(10, 0, "* Setup done");
if ((fd = open("/dev/null", O_WRONLY)) != -1) {
dup2(fd, 0);
dup2(fd, 1);
dup2(fd, 2);
close(fd);
}
if (param->pseudosuper_fd != -1) {
ret = cg_disable_pseudosuper(param->pseudosuper_fd);
if (ret) {
close(param->pseudosuper_fd);
return ret;
}
}
return close_fds(0, param->h->ctx->err_p[1], -1);
}
char **makeenv(char **env, list_head_t *head)
{
struct vzctl_str_param *it;
char **ar;
int i;
for (i = 0; env != NULL && env[i] != NULL; i++);
list_for_each(it, head, list) { i++; }
ar = calloc(1, (i + 1) * sizeof(char *));
if (ar == NULL) {
logger(-1, ENOMEM, "makeenv");
return NULL;
}
for (i = 0; env != NULL && env[i] != NULL; i++) {
if (xstrdup(&ar[i], env[i]))
goto err;
}
list_for_each(it, head, list) {
if (xstrdup(&ar[i++], it->str))
goto err;
}
ar[i] = NULL;
return ar;
err:
free_ar_str(ar);
free(ar);
logger(-1, ENOMEM, "makeenv");
return NULL;
}
int exec_init(const struct start_param *param)
{
char cid[STR_SIZE];
if (is_systemd())
char *argv[] = {"init", NULL};
else
char *argv[] = {"init", "-z", " ", NULL};
char *envp[] = {"HOME=/", "TERM=linux", cid, NULL};
char **env;
int errcode = 0;
logger(1, 0, "Starting init");
if (stat_file("/sbin/init") == 0 &&
stat_file("/bin/init") == 0)
errcode = VZCTL_E_BAD_TMPL;
if (write(param->h->ctx->err_p[1], &errcode, sizeof(errcode)) == -1)
logger(-1, errno, "exec_init: write(param->h->ctx->err_p[1]");
snprintf(cid, sizeof(cid), "container="SYSTEMD_CTID_FMT, EID(param->h));
env = makeenv(envp, ¶m->h->env_param->misc->ve_env);
if (env == NULL)
return VZCTL_E_NOMEM;
setsid();
set_def_rlimits();
execve("/sbin/init", argv, env);
execve("/bin/init", argv, env);
free_ar_str(env);
free(env);
return VZCTL_E_BAD_TMPL;
}
int read_p(int fd)
{
int rc, errcode;
rc = TEMP_FAILURE_RETRY(read(fd, &errcode, sizeof(errcode)));
if (rc == -1)
return vzctl_err(VZCTL_E_SYSTEM, errno, "Read from pipe failed");
else if (rc == 0)
return vzctl_err(VZCTL_E_SYSTEM, 0, "Error pipe unexpectedly closed");
else if (errcode != 0)
return errcode;
return 0;
}
static int drop_dump_state(struct vzctl_env_handle *h)
{
char fname[PATH_MAX];
vzctl2_get_dump_file(h, fname, sizeof(fname));
return destroydir(fname);
}
/** Start and configure Container. */
int vzctl_env_start(struct vzctl_env_handle *h, int flags)
{
int ret;
struct vzctl_env_param *env = h->env_param;
struct vzctl_env_status env_status = {};
char *cidata_mnt = NULL;
struct start_param param = {
.h = h,
.pseudosuper_fd = -1,
};
/* FIXME: */
if (flags & VZCTL_WAIT)
env->opts->wait = VZCTL_PARAM_ON;
vzctl2_get_env_status_info(h, &env_status, ENV_STATUS_RUNNING);
if (env_status.mask & ENV_STATUS_RUNNING) {
if (!(env_status.mask & ENV_STATUS_CPT_SUSPENDED))
return vzctl_err(VZCTL_E_ENV_RUN, 0,
"Container is already running");
logger(0, 0, "Unpause the Container");
struct vzctl_cpt_param cpt = {};
ret = vzctl2_cpt_cmd(h, 0, VZCTL_CMD_RESUME, &cpt, flags);
if (ret) {
logger(-1, 0, "Unable to unpause the Container");
}
else {
logger(0, 0, "The Container has been successfully unpaused");
vzctl2_send_state_evt(EID(h), VZCTL_ENV_STARTED);
}
return ret;
}
logger(0, 0, "Starting Container ...");