-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconf.c
1857 lines (1614 loc) · 39.9 KB
/
conf.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
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
/*-
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
*
* Copyright (c) 2021 Yuichiro Naito
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/param.h>
#include <ctype.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include "bmd.h"
#include "bmd_plugin.h"
#include "conf.h"
#include "log.h"
#define CMP(a, b) ((a) < (b) ? -1 : ((a) == (b) ? 0 : 1))
#define CMP_RETURN(a, b) \
if ((a) != (b)) \
return (a) < (b) ? -1 : 1
struct id_entry {
SLIST_ENTRY(id_entry) next;
unsigned int id;
char name[0];
};
/*
List of identfiers.
*/
static SLIST_HEAD(, id_entry) id_list = SLIST_HEAD_INITIALIZER();
static int compare_variable_key(struct conf_var *, struct conf_var *);
RB_GENERATE_STATIC(vartree, conf_var, entry, compare_variable_key);
struct vartree *global_vars = NULL;
#define generate_list_getter(type, member) \
struct type *get_##type(struct vm_conf *conf) \
{ \
return STAILQ_FIRST(&conf->member); \
} \
struct type *next_##type(struct type *p) \
{ \
return STAILQ_NEXT(p, next); \
}
#define generate_member_getter(rtype, type, member) \
rtype get_##type##_##member(struct type *p) \
{ \
return p->member; \
}
#define generate_member_bool(type, member) \
bool is_##type##_##member(struct type *p) \
{ \
return p->member; \
}
#define generate_clear_list(type, member) \
void clear_##type(struct vm_conf *vc) \
{ \
struct type *p, *pn; \
STAILQ_FOREACH_SAFE(p, &vc->member, next, pn) \
free_##type(p); \
STAILQ_INIT(&vc->member); \
}
#define generate_getter(rtype, member) \
rtype get_##member(struct vm_conf *c) \
{ \
return c->member; \
}
#define generate_string_accessor(member) \
char *get_##member(struct vm_conf *c) \
{ \
return c->member; \
} \
int set_##member(struct vm_conf *c, const char *v) \
{ \
if (c == NULL) \
return -1; \
return set_string(&c->member, v); \
}
#define generate_number_accessor(type, member) \
type get_##member(struct vm_conf *c) \
{ \
return c->member; \
} \
int set_##member(struct vm_conf *c, type v) \
{ \
if (c == NULL) \
return -1; \
c->member = v; \
return 0; \
}
#define generate_bool_accessor(member) \
bool is_##member(struct vm_conf *c) \
{ \
return c->member; \
} \
int set_##member(struct vm_conf *c, bool v) \
{ \
if (c == NULL) \
return -1; \
c->member = v; \
return 0; \
}
#define generate_vm_accessor(type, member) \
type get_##member(struct vm *vm) \
{ \
return vm->member; \
} \
void set_##member(struct vm *vm, type v) \
{ \
vm->member = v; \
}
void
free_id_list(void)
{
struct id_entry *e, *t;
SLIST_FOREACH_SAFE(e, &id_list, next, t)
free(e);
SLIST_INIT(&id_list);
}
static int
assign_id(const char *name, unsigned int *id)
{
static unsigned int lastid = 0;
struct id_entry *e;
SLIST_FOREACH(e, &id_list, next)
if (strcmp(e->name, name) == 0) {
*id = e->id;
return 0;
}
if ((e = malloc(sizeof(*e) + strlen(name) + 1)) == NULL)
return -1;
strcpy(e->name, name);
*id = e->id = lastid++;
SLIST_INSERT_HEAD(&id_list, e, next);
return 0;
}
void
free_passthru_conf(struct passthru_conf *c)
{
if (c == NULL)
return;
free(c->devid);
free(c);
}
void
free_disk_conf(struct disk_conf *c)
{
if (c == NULL)
return;
free(c->type);
free(c->path);
free(c);
}
void
free_iso_conf(struct iso_conf *c)
{
if (c == NULL)
return;
free(c->type);
free(c->path);
free(c);
}
void
free_net_conf(struct net_conf *c)
{
if (c == NULL)
return;
free(c->type);
free(c->bridge);
free(c->tap);
free(c->vale);
free(c->vale_port);
free(c->mac);
free(c);
}
void
free_sharefs_conf(struct sharefs_conf *c)
{
if (c == NULL)
return;
free(c->name);
free(c->path);
free(c);
}
void
free_fbuf(struct fbuf *f)
{
if (f == NULL)
return;
free(f->ipaddr);
free(f->vgaconf);
free(f->password);
free(f);
}
generate_clear_list(passthru_conf, passthrues);
generate_clear_list(disk_conf, disks);
generate_clear_list(iso_conf, isoes);
generate_clear_list(net_conf, nets);
generate_clear_list(sharefs_conf, sharefss);
generate_clear_list(bhyveload_env, bhyveload_envs);
generate_clear_list(bhyve_env, bhyve_envs);
generate_clear_list(cpu_pin, cpu_pins);
static void
free_var(struct conf_var *c)
{
if (c == NULL)
return;
free(c->key);
free(c->val);
free(c);
}
void
free_vartree(struct vartree *vt)
{
struct conf_var *v, *vn;
RB_FOREACH_SAFE(v, vartree, vt, vn) {
RB_REMOVE(vartree, vt, v);
free_var(v);
}
free(vt);
}
void
free_vm_conf(struct vm_conf *vc)
{
char **com;
if (vc == NULL)
return;
free_vartree(vc->vars.local);
free(vc->name);
free(vc->memory);
ARRAY_FOREACH(com, vc->com)
free(*com);
free(vc->loader);
free(vc->loadcmd);
free(vc->installcmd);
free(vc->backend);
free(vc->debug_port);
free(vc->err_logfile);
free(vc->grub_run_partition);
free_fbuf(vc->fbuf);
clear_passthru_conf(vc);
clear_disk_conf(vc);
clear_iso_conf(vc);
clear_net_conf(vc);
clear_sharefs_conf(vc);
free(vc->keymap);
free(vc->bhyveload_loader);
clear_bhyveload_env(vc);
clear_bhyve_env(vc);
clear_cpu_pin(vc);
free(vc->tpm_dev);
free(vc->tpm_type);
free(vc->tpm_version);
free(vc);
}
int
add_passthru_conf(struct vm_conf *conf, const char *devid)
{
struct passthru_conf *p;
char *d;
if (conf == NULL)
return 0;
p = malloc(sizeof(struct passthru_conf));
d = strdup(devid);
if (p == NULL || d == NULL)
goto err;
p->devid = d;
STAILQ_INSERT_TAIL(&conf->passthrues, p, next);
conf->npassthrues++;
set_wired_memory(conf, true);
return 0;
err:
free(d);
free(p);
return -1;
}
generate_list_getter(passthru_conf, passthrues);
generate_member_getter(char *, passthru_conf, devid);
int
add_disk_conf(struct vm_conf *conf, const char *type, const char *path,
bool nocache, bool direct, bool readonly, bool nodelete)
{
struct disk_conf *t;
char *y, *p;
if (conf == NULL)
return 0;
t = malloc(sizeof(struct disk_conf));
y = strdup(type);
p = strdup(path);
if (t == NULL || y == NULL || p == NULL)
goto err;
t->type = y;
t->path = p;
t->nocache = nocache;
t->direct = direct;
t->readonly = readonly;
t->nodelete = nodelete;
STAILQ_INSERT_TAIL(&conf->disks, t, next);
conf->ndisks++;
return 0;
err:
free(p);
free(y);
free(t);
return -1;
}
generate_list_getter(disk_conf, disks);
generate_member_getter(char *, disk_conf, type);
generate_member_getter(char *, disk_conf, path);
generate_member_bool(disk_conf, nocache);
generate_member_bool(disk_conf, direct);
generate_member_bool(disk_conf, readonly);
generate_member_bool(disk_conf, nodelete);
int
add_iso_conf(struct vm_conf *conf, const char *type, const char *path)
{
struct iso_conf *t;
char *y, *p;
if (conf == NULL)
return 0;
t = malloc(sizeof(struct iso_conf));
y = strdup(type);
p = strdup(path);
if (t == NULL)
goto err;
t->type = y;
t->path = p;
STAILQ_INSERT_TAIL(&conf->isoes, t, next);
conf->nisoes++;
return 0;
err:
free(p);
free(y);
free(t);
return -1;
}
generate_list_getter(iso_conf, isoes);
generate_member_getter(char *, iso_conf, type);
generate_member_getter(char *, iso_conf, path);
int
add_net_conf(struct vm_conf *conf, const char *type, const char *eaddr,
const char *bridge)
{
bool is_vale;
struct net_conf *t;
char *y, *b, *e = NULL;
if (conf == NULL)
return 0;
is_vale = (strncmp(bridge, "vale", 4) == 0);
t = calloc(1, sizeof(struct net_conf));
y = strdup(type);
b = strdup(bridge);
if (t == NULL || y == NULL || b == NULL)
goto err;
if (eaddr && strlen(eaddr) > 10) {
if ((e = strdup(eaddr)) == NULL)
goto err;
t->mac = e;
}
if (is_vale) {
if (asprintf(&t->vale_port, "vm%dp%d", conf->id, conf->nnets) <
0)
goto err;
t->vale = b;
} else
t->bridge = b;
t->type = y;
t->tap = NULL;
STAILQ_INSERT_TAIL(&conf->nets, t, next);
conf->nnets++;
return 0;
err:
free(b);
free(y);
free(e);
free(t);
return -1;
}
generate_list_getter(net_conf, nets);
generate_member_getter(char *, net_conf, type);
generate_member_getter(char *, net_conf, bridge);
generate_member_getter(char *, net_conf, mac);
generate_member_getter(char *, net_conf, tap);
generate_member_getter(char *, net_conf, vale);
generate_member_getter(char *, net_conf, vale_port);
int
add_sharefs_conf(struct vm_conf *conf, const char *name, const char *path,
bool ro)
{
struct sharefs_conf *t;
char *n, *p;
t = calloc(1, sizeof(struct sharefs_conf));
n = strdup(name);
p = strdup(path);
if (t == NULL || n == NULL || p == NULL)
goto err;
t->name = n;
t->path = p;
t->readonly = ro;
STAILQ_INSERT_TAIL(&conf->sharefss, t, next);
conf->nsharefs++;
return 0;
err:
free(t);
free(n);
free(p);
return -1;
}
generate_list_getter(sharefs_conf, sharefss);
generate_member_getter(char *, sharefs_conf, name);
generate_member_getter(char *, sharefs_conf, path);
generate_member_bool(sharefs_conf, readonly);
int
add_bhyveload_env(struct vm_conf *conf, const char *env)
{
struct bhyveload_env *be;
if (conf == NULL)
return 0;
if (env == NULL || (be = malloc(sizeof(*be) + strlen(env) + 1)) == NULL)
return -1;
strcpy(be->env, env);
STAILQ_INSERT_TAIL(&conf->bhyveload_envs, be, next);
conf->nbhyveload_envs++;
return 0;
}
generate_list_getter(bhyveload_env, bhyveload_envs);
generate_member_getter(char *, bhyveload_env, env);
int
add_bhyve_env(struct vm_conf *conf, const char *env)
{
struct bhyve_env *be;
if (conf == NULL)
return 0;
if (env == NULL || (be = malloc(sizeof(*be) + strlen(env) + 1)) == NULL)
return -1;
strcpy(be->env, env);
STAILQ_INSERT_TAIL(&conf->bhyve_envs, be, next);
conf->nbhyve_envs++;
return 0;
}
generate_list_getter(bhyve_env, bhyve_envs);
generate_member_getter(char *, bhyve_env, env);
int
add_cpu_pin(struct vm_conf *conf, int vcpu, int hostcpu)
{
struct cpu_pin *pin;
if (conf == NULL)
return 0;
if ((pin = malloc(sizeof(struct cpu_pin))) == NULL)
return -1;
pin->vcpu = vcpu;
pin->hostcpu = hostcpu;
STAILQ_INSERT_TAIL(&conf->cpu_pins, pin, next);
conf->ncpu_pins++;
return 0;
}
generate_list_getter(cpu_pin, cpu_pins);
generate_member_getter(int, cpu_pin, vcpu);
generate_member_getter(int, cpu_pin, hostcpu);
struct net_conf *
copy_net_conf(const struct net_conf *nc)
{
struct net_conf *ret;
char *y, *b, *t, *v, *m, *vp;
#define DUPLICATE_STRING(str) (str) ? strdup(str) : NULL
#define CHECK_DUP_ERR(src, dst) (src != NULL && dst == NULL)
ret = malloc(sizeof(struct net_conf));
y = strdup(nc->type);
b = DUPLICATE_STRING(nc->bridge);
t = DUPLICATE_STRING(nc->tap);
m = DUPLICATE_STRING(nc->mac);
v = DUPLICATE_STRING(nc->vale);
vp = DUPLICATE_STRING(nc->vale_port);
if (ret == NULL || y == NULL || CHECK_DUP_ERR(nc->bridge, b) ||
CHECK_DUP_ERR(nc->tap, t) || CHECK_DUP_ERR(nc->mac, m) ||
CHECK_DUP_ERR(nc->vale, v) || CHECK_DUP_ERR(nc->vale_port, vp))
goto err;
#undef CHECK_DUP_ERR
#undef DUPLICATE_STRING
ret->type = y;
ret->bridge = b;
ret->tap = t;
ret->mac = m;
ret->vale = v;
ret->vale_port = vp;
STAILQ_NEXT(ret, next) = NULL;
return ret;
err:
free(t);
free(b);
free(y);
free(m);
free(v);
free(vp);
free(ret);
return NULL;
}
int
set_string(char **var, const char *value)
{
char *new;
if ((new = strdup(value)) == NULL)
return -1;
free(*var);
*var = new;
return 0;
}
generate_getter(unsigned int, id);
generate_string_accessor(name);
generate_string_accessor(loadcmd);
generate_string_accessor(installcmd);
generate_string_accessor(err_logfile);
generate_string_accessor(loader);
generate_string_accessor(bhyveload_loader);
generate_number_accessor(int, loader_timeout);
generate_number_accessor(int, stop_timeout);
generate_string_accessor(grub_run_partition);
generate_string_accessor(debug_port);
generate_string_accessor(memory);
int
set_ncpu(struct vm_conf *conf, int ncpu)
{
if (conf == NULL)
return 0;
conf->ncpu = ncpu;
conf->ncpu_sockets = ncpu;
conf->ncpu_cores = 1;
conf->ncpu_threads = 1;
return 0;
}
int
set_cpu_topology(struct vm_conf *conf, int ncpu[3])
{
if (conf == NULL)
return 0;
conf->ncpu = ncpu[0] * ncpu[1] * ncpu[2];
conf->ncpu_sockets = ncpu[0];
conf->ncpu_cores = ncpu[1];
conf->ncpu_threads = ncpu[2];
return 0;
}
generate_getter(int, ncpu);
generate_getter(int, ncpu_sockets);
generate_getter(int, ncpu_cores);
generate_getter(int, ncpu_threads);
generate_number_accessor(uid_t, owner);
generate_number_accessor(gid_t, group);
generate_number_accessor(enum BOOT, boot);
generate_number_accessor(enum HOSTBRIDGE_TYPE, hostbridge);
generate_string_accessor(backend);
generate_string_accessor(keymap);
generate_number_accessor(int, boot_delay);
generate_bool_accessor(reboot_on_change);
generate_bool_accessor(single_user);
generate_bool_accessor(install);
generate_bool_accessor(virt_random);
generate_bool_accessor(x2apic);
int
set_fbuf_enable(struct fbuf *fb, bool enable)
{
if (fb == NULL)
return 0;
fb->enable = enable;
return 0;
}
bool
is_fbuf_enable(struct vm_conf *conf)
{
return conf->fbuf->enable;
}
int
set_fbuf_ipaddr(struct fbuf *fb, const char *ipaddr)
{
int ret;
if (fb == NULL)
return 0;
ret = set_string(&fb->ipaddr, ipaddr);
if (ret == 0 && fb->enable < 0)
fb->enable = true;
return ret;
}
char *
get_fbuf_ipaddr(struct vm_conf *conf)
{
return conf->fbuf->ipaddr;
}
int
set_fbuf_port(struct fbuf *fb, int port)
{
if (fb == NULL)
return 0;
fb->port = port;
if (fb->enable < 0)
fb->enable = true;
return 0;
}
int
get_fbuf_port(struct vm_conf *conf)
{
return conf->fbuf->port;
}
int
set_fbuf_res(struct fbuf *fb, int width, int height)
{
if (fb == NULL)
return 0;
fb->width = width;
fb->height = height;
if (fb->enable < 0)
fb->enable = true;
return 0;
}
void
get_fbuf_res(struct vm_conf *conf, int *width, int *height)
{
if (width)
*width = conf->fbuf->width;
if (height)
*height = conf->fbuf->height;
}
int
set_fbuf_vgaconf(struct fbuf *fb, const char *vga)
{
int ret;
if (fb == NULL)
return 0;
ret = set_string(&fb->vgaconf, vga);
if (ret == 0 && fb->enable < 0)
fb->enable = 1;
return ret;
}
char *
get_fbuf_vgaconf(struct vm_conf *conf)
{
return conf->fbuf->vgaconf;
}
int
set_fbuf_wait(struct fbuf *fb, int wait)
{
fb->wait = wait;
return 0;
}
int
get_fbuf_wait(struct vm_conf *conf)
{
return conf->fbuf->wait;
}
int
set_fbuf_password(struct fbuf *fb, const char *pass)
{
int ret;
if (fb == NULL)
return 0;
ret = set_string(&fb->password, pass);
if (ret == 0 && fb->enable < 0)
fb->enable = true;
return ret;
}
char *
get_fbuf_password(struct vm_conf *conf)
{
return conf->fbuf->password;
}
generate_bool_accessor(mouse);
generate_bool_accessor(wired_memory);
generate_bool_accessor(utctime);
generate_string_accessor(tpm_dev);
generate_string_accessor(tpm_type);
generate_string_accessor(tpm_version);
generate_vm_accessor(int, infd);
generate_vm_accessor(int, outfd);
generate_vm_accessor(int, errfd);
generate_vm_accessor(int, logfd);
generate_vm_accessor(enum STATE, state);
char *
get_assigned_comport(struct vm *vm)
{
return vm->assigned_com[0];
}
int
set_com(struct vm_conf *c, unsigned int i, const char *v)
{
if (c == NULL)
return -1;
return set_string(&c->com[i], v);
}
char *
get_comport(struct vm_conf *conf)
{
return conf->com[0];
}
char *
get_com(struct vm_conf *conf, unsigned int i)
{
if (i >= nitems(conf->com))
return NULL;
return conf->com[i];
}
char *
get_assigned_com(struct vm *vm, unsigned int i)
{
if (i >= nitems(vm->assigned_com))
return NULL;
return vm->assigned_com[i];
}
void
set_pid(struct vm *vm, pid_t pid)
{
vm->pid = pid;
}
int
set_bootrom(struct vm *vm, const char *rom)
{
return set_string(&vm->bootrom, rom);
}
const char *
get_mapfile(struct vm *vm)
{
return vm->mapfile;
}
int
set_mapfile(struct vm *vm, const char *mapfile)
{
return set_string(&vm->mapfile, mapfile);
}
const char *
get_varsdir(void)
{
return gl_conf->vars_dir;
}
const char *
get_varsfile(struct vm *vm)
{
return vm->varsfile;
}
int
set_varsfile(struct vm *vm, const char *varsfile)
{
return set_string(&vm->varsfile, varsfile);
}
void
free_mapfile(struct vm *vm)
{
free(vm->mapfile);
vm->mapfile = NULL;
}
struct vm_conf *
vm_get_conf(struct vm *vm)
{
return vm->conf;
}
struct net_conf *
get_taps(struct vm *vm)
{
return STAILQ_FIRST(&vm->taps);
}
struct fbuf *
create_fbuf(void)
{
struct fbuf *ret;
char *addr, *vga;
ret = calloc(1, sizeof(typeof(*ret)));
addr = strdup("0.0.0.0");
vga = strdup("io");
if (ret == NULL || addr == NULL || vga == NULL)
goto err;
ret->enable = -1;
ret->ipaddr = addr;
ret->vgaconf = vga;
ret->port = 5900;
ret->width = 1024;
ret->height = 768;
return ret;
err:
free(ret);
free(addr);
free(vga);
return NULL;
}
struct vm_conf *
create_vm_conf(const char *vm_name)
{
char *name, *backend, idnum[12];
struct vm_conf *ret;
struct fbuf *fbuf;
unsigned int id;
struct vartree *local;
ret = calloc(1, sizeof(typeof(*ret)));
fbuf = create_fbuf();
name = strdup(vm_name);
backend = strdup("bhyve");
local = malloc(sizeof(*local));
if (ret == NULL || fbuf == NULL || name == NULL || backend == NULL ||
local == NULL)
goto err;
RB_INIT(local);
ret->vars.local = local;
ret->vars.args = NULL;
if (set_var0(local, "NAME", name) < 0)
ERR("failed to set \"NAME\" variable! (%s)\n", strerror(errno));
if (assign_id(name, &id) == 0) {
snprintf(idnum, sizeof(idnum), "%u", id);
if (set_var0(local, "ID", idnum) < 0)
ERR("failed to set \"ID\" variable! (%s)\n",
strerror(errno));
ret->id = id;
} else {
ERR("failed to allocate \"ID\" number! (%s)\n",
strerror(errno));
free_vartree(local);
local = NULL;
goto err;
}
ret->ncpu = 1;
ret->ncpu_sockets = 1;
ret->ncpu_cores = 1;
ret->ncpu_threads = 1;
ret->hostbridge = INTEL;
ret->fbuf = fbuf;
ret->name = name;
ret->loader_timeout = 15;
ret->stop_timeout = 300;
ret->utctime = true;
ret->backend = backend;
ret->group = -1;
STAILQ_INIT(&ret->disks);
STAILQ_INIT(&ret->isoes);
STAILQ_INIT(&ret->nets);
STAILQ_INIT(&ret->sharefss);
STAILQ_INIT(&ret->bhyveload_envs);
STAILQ_INIT(&ret->bhyve_envs);
STAILQ_INIT(&ret->cpu_pins);
return ret;
err:
ERR("failed to create VM config! (%s)\n", strerror(errno));
free(local);
free(ret);
free(backend);
free(fbuf);
free(name);
return NULL;
}
int
finalize_vm_conf(struct vm_conf *conf)
{
if (conf == NULL || conf->fbuf == NULL)
return -1;
if (conf->fbuf->enable < 0)
conf->fbuf->enable = false;
return 0;
}