-
Notifications
You must be signed in to change notification settings - Fork 245
/
VM.js
8579 lines (7628 loc) · 281 KB
/
VM.js
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
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at http://smartos.org/CDDL
*
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file.
*
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*
* Copyright (c) 2012, Joyent, Inc. All rights reserved.
*
* Experimental functions, expect these interfaces to be unstable and
* potentially go away entirely:
*
* install(uuid, callback)
* receive(target, options, callback)
* send(uuid, where, options, callback)
* getSysinfo(args, callback)
* validate(brand, action, payload, callback)
* waitForZoneState(payload, state, options, callback)
*
* Exported functions:
*
* console(uuid, callback)
* create(properties, callback)
* delete(uuid, callback)
* flatten(vmobj, key)
* info(uuid, types, callback)
* load([zonename|uuid], callback)
* lookup(match, callback)
* reboot(uuid, options={[force=true]}, callback)
* start(uuid, extra, callback)
* stop(uuid, options={[force=true]}, callback)
* sysrq(uuid, req=[nmi|screenshot], options={}, callback)
* update(uuid, properties, callback)
*
* Exported variables:
*
* logname - you can set this to a string [a-zA-Z_] to use as log name
* logger - you can set this to a node-bunyan log stream to capture the logs
* logger - a function which takes (priority, message, <Error Obj>)
* INFO_TYPES - list of supported types for the info command
* SYSRQ_TYPES - list of supported requests for sysrq
*/
// Ensure we're using the platform's node
require('/usr/node/node_modules/platform_node_version').assert();
var assert = require('assert');
var async = require('/usr/node/node_modules/async');
var bunyan = require('/usr/node/node_modules/bunyan');
var cp = require('child_process');
var dladm = require('/usr/vm/node_modules/dladm');
var exec = cp.exec;
var execFile = cp.execFile;
var expat = require('/usr/node/node_modules/node-expat');
var fs = require('fs');
var http = require('http');
var net = require('net');
var path = require('path');
var Qmp = require('/usr/vm/node_modules/qmp').Qmp;
var spawn = cp.spawn;
var sprintf = require('/usr/node/node_modules/sprintf').sprintf;
var system = require('/usr/node/node_modules/system');
var tty = require('tty');
var util = require('util');
var log_to_file = false;
// keep the last 50 messages just in case we end up wanting them.
var ringbuffer = new bunyan.RingBuffer({ limit: 50 });
/*
* zone states from libzonecfg/common/zonecfg_impl.h
*
* #define ZONE_STATE_STR_CONFIGURED "configured"
* #define ZONE_STATE_STR_INCOMPLETE "incomplete"
* #define ZONE_STATE_STR_INSTALLED "installed"
* #define ZONE_STATE_STR_READY "ready"
* #define ZONE_STATE_STR_MOUNTED "mounted"
* #define ZONE_STATE_STR_RUNNING "running"
* #define ZONE_STATE_STR_SHUTTING_DOWN "shutting_down"
* #define ZONE_STATE_STR_DOWN "down"
*
*/
exports.FLATTENABLE_ARRAY_HASH_KEYS = [
'disks',
'nics'
];
exports.FLATTENABLE_HASH_KEYS = [
'customer_metadata',
'internal_metadata',
'tags'
];
var DEFAULT_MDATA_TIMEOUT = 300;
var PROVISION_TIMEOUT = 300;
var STOP_TIMEOUT = 60;
var VM = this;
VM.log = null;
// can be (re)set by loader before we start.
exports.logger = null;
exports.loglevel = 'debug';
// OpenOnErrorFileStream is a bunyan stream that only creates the file when
// there's an error or higher level message. We use this for actions that
// shouldn't log in the normal case but where we do want logs when something
// breaks. Thanks to Trent++ for most of this code.
function OpenOnErrorFileStream(filename) {
this.path = filename;
this.write = this.constructor.prototype.write1;
this.end = this.constructor.prototype.end1;
}
OpenOnErrorFileStream.prototype.end1 = function () {
// in initial mode we're not writing anything, so nothing to flush
return;
};
// used until first ERROR or higher, then opens file and ensures future writes
// go to .write2()
OpenOnErrorFileStream.prototype.write1 = function (rec) {
var r;
if (rec.level >= bunyan.ERROR || log_to_file) {
this.stream = fs.createWriteStream(this.path,
{flags: 'a', encoding: 'utf8'});
this.end = this.stream.end;
this.write = this.constructor.prototype.write2;
// dump out logs from ringbuffer too since there was an error so we can
// figure out what's going on.
for (r in ringbuffer.records) {
r = ringbuffer.records[r];
if (r != rec) {
this.write(r);
}
}
this.write(rec);
}
};
// used when writing to file
OpenOnErrorFileStream.prototype.write2 = function (rec) {
var str = JSON.stringify(rec, bunyan.safeCycles()) + '\n';
this.stream.write(str);
};
// This function should be called by any exported function from this module.
// 'action' should be the name of whatever we're trying to do and side_effects
// should be true if this action is expected to change the state or modify VMs
// on the system: eg. create, start, stop, delete and false when the action
// should not cause changes to the system, eg.: load, lookup, info, console, &c.
function ensureLogging(action, side_effects)
{
side_effects = !!side_effects; // make it boolean (undef === false)
var filename;
var logname;
var streams = [];
// This is here in case an app calls a lookup first and then a create. The
// logger will get created in no-sideeffects mode for the lookup but when
// the create is called this will force the switch to writing.
if (side_effects) {
log_to_file = true;
}
if (VM.log) {
// We're already logging, don't break things.
return;
}
if (VM.hasOwnProperty('logname')) {
logname = VM.logname.replace(/[^a-zA-Z\_]/g, '');
}
if (!logname || logname.length < 1) {
logname = 'VM';
}
if (VM.hasOwnProperty('logger') && VM.logger) {
// Use concat, in case someone's sneaky and makes more than one logger.
// We don't officially support that yet though.
streams = streams.concat(VM.logger);
}
filename = '/var/log/vm/' + Date.now(0) + '-'
+ sprintf('%06d', process.pid) + '-' + logname + '.log';
// Add the ringbuffer which we'll dump if we switch from not writing to
// writing, and so that they'll show up in dumps.
streams.push({
level: 'trace',
type: 'raw',
stream: ringbuffer
});
if (side_effects) {
streams.push({
level: VM.loglevel,
path: filename
});
} else {
streams.push({
type: 'raw',
stream: new OpenOnErrorFileStream(filename),
level: VM.loglevel
});
}
VM.log = bunyan.createLogger({
name: logname,
streams: streams,
serializers: bunyan.stdSerializers
});
}
function ltrim(str, chars)
{
chars = chars || '\\s';
str = str || '';
return str.replace(new RegExp('^[' + chars + ']+', 'g'), '');
}
function rtrim(str, chars)
{
chars = chars || '\\s';
str = str || '';
return str.replace(new RegExp('[' + chars + ']+$', 'g'), '');
}
function trim(str, chars)
{
return ltrim(rtrim(str, chars), chars);
}
function isUUID(str) {
var re = /^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/;
if (str.length === 36 && str.match(re)) {
return true;
} else {
return false;
}
}
function fixBoolean(str)
{
if (str === 'true') {
return true;
} else if (str === 'false') {
return false;
} else {
return str;
}
}
function fixBooleanLoose(str)
{
if (str === 'true' || str === '1' || str === 1) {
return true;
} else if (str === 'false' || str === '0' || str === 0) {
return false;
} else {
return str;
}
}
// IMPORTANT:
//
// Some of these properties get translated below into backward compatible
// names.
//
var UPDATABLE_NIC_PROPS = [
'primary',
'nic_tag',
'vrrp_vrid',
'vrrp_primary_ip',
'blocked_outgoing_ports',
'mac',
'gateway',
'ip',
'model',
'netmask',
'dhcp_server',
'allow_dhcp_spoofing',
'allow_ip_spoofing',
'allow_mac_spoofing',
'allow_restricted_traffic',
'allow_unfiltered_promisc',
'vlan_id'
];
var UPDATABLE_DISK_PROPS = [
'boot',
'model'
];
// Note: this doesn't include 'state' because of 'stopping' which is a virtual
// state and therefore lookups would be wrong (because they'd search on real
// state).
var QUICK_LOOKUP = [
'zoneid',
'zonename',
'zonepath',
'uuid',
'brand',
'ip_type'
];
exports.DISK_MODELS = [
'virtio',
'ide',
'scsi'
];
exports.VGA_TYPES = [
'cirrus',
'std',
'vmware',
'qxl',
'xenfb'
];
exports.INFO_TYPES = [
'all',
'block',
'blockstats',
'chardev',
'cpus',
'kvm',
'pci',
'spice',
'status',
'version',
'vnc'
];
exports.SYSRQ_TYPES = [
'nmi',
'screenshot'
];
exports.COMPRESSION_TYPES = [
'on',
'off',
'lzjb',
'gzip',
'gzip-1',
'gzip-2',
'gzip-3',
'gzip-4',
'gzip-5',
'gzip-6',
'gzip-7',
'gzip-8',
'gzip-9',
'zle'
];
exports.KVM_MEM_OVERHEAD = 1024;
exports.KVM_MIN_MEM_OVERHEAD = 256;
var XML_PROPERTIES = {
'zone': {
'name': 'zonename',
'zonepath': 'zonepath',
'autoboot': 'autoboot',
'brand': 'brand',
'limitpriv': 'limit_priv',
'fs-allowed': 'fs_allowed'
},
'zone.attr': {
'alias': 'alias',
'billing-id': 'billing_id',
'boot': 'boot',
'cpu-type': 'cpu_type',
'create-timestamp': 'create_timestamp',
'dataset-uuid': 'image_uuid',
'default-gateway': 'default_gateway',
'dns-domain': 'dns_domain',
'do-not-inventory': 'do_not_inventory',
'failed': 'failed',
'hostname': 'hostname',
'never-booted': 'never_booted',
'owner-uuid': 'owner_uuid',
'package-name': 'package_name',
'package-version': 'package_version',
'qemu-extra-opts': 'qemu_extra_opts',
'qemu-opts': 'qemu_opts',
'ram': 'ram',
'resolvers': 'resolvers',
'spice-opts': 'spice_opts',
'spice-password': 'spice_password',
'spice-port': 'spice_port',
'tmpfs': 'tmpfs',
'transition': 'transition',
'vcpus': 'vcpus',
'vga': 'vga',
'virtio-txtimer': 'virtio_txtimer',
'virtio-txburst': 'virtio_txburst',
'vm-autoboot': 'vm_autoboot',
'vnc-password': 'vnc_password',
'vnc-port': 'vnc_port'
},
'zone.rctl.zone.cpu-shares.rctl-value': {
'limit': 'cpu_shares'
},
'zone.rctl.zone.cpu-cap.rctl-value': {
'limit': 'cpu_cap'
},
'zone.rctl.zone.zfs-io-priority.rctl-value': {
'limit': 'zfs_io_priority'
},
'zone.rctl.zone.max-lwps.rctl-value': {
'limit': 'max_lwps'
},
'zone.rctl.zone.max-physical-memory.rctl-value': {
'limit': 'max_physical_memory'
},
'zone.rctl.zone.max-locked-memory.rctl-value': {
'limit': 'max_locked_memory'
},
'zone.rctl.zone.max-swap.rctl-value': {
'limit': 'max_swap'
},
'nic': {
'ip': 'ip',
'mac-addr': 'mac',
'physical': 'interface',
'vlan-id': 'vlan_id',
'global-nic': 'nic_tag',
'dhcp_server': 'dhcp_server',
'allow_dhcp_spoofing': 'allow_dhcp_spoofing',
'allow_ip_spoofing': 'allow_ip_spoofing',
'allow_mac_spoofing': 'allow_mac_spoofing',
'allow_restricted_traffic': 'allow_restricted_traffic',
'allow_unfiltered_promisc': 'allow_unfiltered_promisc',
'netmask': 'netmask',
'model': 'model',
'gateway': 'gateway',
'primary': 'primary',
'vrrp_vrid': 'vrrp_vrid',
'vrrp_primary_ip': 'vrrp_primary_ip',
'blocked-outgoing-ports': 'blocked_outgoing_ports'
},
'filesystem': {
'special': 'source',
'directory': 'target',
'type': 'type',
'raw': 'raw'
},
'disk': {
'boot': 'boot',
'image-size': 'image_size',
'image-name': 'image_name',
'image-uuid': 'image_uuid',
'match': 'path',
'media': 'media',
'model': 'model',
'size': 'size'
}
};
/*
* This allows one to define a function that will be run over the values from
* the zonecfg at the point where we transform that data into a VM object.
*
*/
var XML_PROPERTY_TRANSFORMS = {
'alias': unbase64,
'autoboot': fixBoolean,
'cpu_cap': numberify,
'cpu_shares': numberify,
'disks': {
'boot': fixBoolean,
'image_size': numberify,
'size': numberify
},
'do_not_inventory': fixBoolean,
'max_locked_memory': unmangleMem,
'max_lwps': numberify,
'max_physical_memory': unmangleMem,
'max_swap': unmangleMem,
'never_booted': fixBoolean,
'nics': {
'dhcp_server': fixBoolean,
'allow_dhcp_spoofing': fixBoolean,
'allow_ip_spoofing': fixBoolean,
'allow_mac_spoofing': fixBoolean,
'allow_restricted_traffic': fixBoolean,
'allow_unfiltered_promisc': fixBoolean,
'primary': fixBooleanLoose,
'vrrp_vrid': numberify,
'vlan_id': numberify
},
'qemu_extra_opts': unbase64,
'qemu_opts': unbase64,
'ram': numberify,
'resolvers': separateCommas,
'spice_password': unbase64,
'spice_port': numberify,
'spice_opts': unbase64,
'tmpfs': numberify,
'vcpus': numberify,
'virtio_txburst': numberify,
'virtio_txtimer': numberify,
'vnc_password': unbase64,
'vnc_port': numberify,
'zfs_io_priority': numberify,
'zoneid': numberify
};
/*
* This defines all of the possible properties that could be in a create/update
* payload and their types. Each of the entries are required to have at least
* a 'type' property which is one of:
*
* object-array -- an array of objects
* boolean -- true or false
* flat-object -- an object that has only string properties
* integer -- integers only
* list -- Either comma separated or array list of strings
* string -- Simple string
* uuid -- A standard 00000000-0000-0000-0000-000000000000 type uuid
* zpool -- The name of an existing zpool
*
*/
var PAYLOAD_PROPERTIES = {
'add_disks': {'type': 'object-array', 'check_as': 'disks'},
'add_nics': {'type': 'object-array', 'check_as': 'nics'},
'alias': {'type': 'string'},
'autoboot': {'type': 'boolean'},
'billing_id': {'type': 'string'},
'boot': {'type': 'string'},
'brand': {'type': 'string'},
'cpu_cap': {'type': 'integer'},
'cpu_shares': {'type': 'integer'},
'cpu_type': {'type': 'string'},
'create_only': {'type': 'boolean'},
'create_timestamp': {'type': 'string'},
'customer_metadata': {'type': 'flat-object'},
'dataset_uuid': {'type': 'uuid'},
'delegate_dataset': {'type': 'boolean'},
'disks': {'type': 'object-array'},
'disks.*.block_size': {'type': 'integer'},
'disks.*.boot': {'type': 'boolean'},
'disks.*.compression': {'type': 'string'},
'disks.*.image_name': {'type': 'string'},
'disks.*.image_size': {'type': 'integer'},
'disks.*.image_uuid': {'type': 'uuid'},
'disks.*.size': {'type': 'integer'},
'disks.*.media': {'type': 'string'},
'disks.*.model': {'type': 'string'},
'disks.*.nocreate': {'type': 'boolean'},
'disks.*.path': {'type': 'string'},
'disks.*.zpool': {'type': 'zpool'},
'disk_driver': {'type': 'string'},
'do_not_inventory': {'type': 'boolean'},
'dns_domain': {'type': 'string'},
'filesystems': {'type': 'object-array'},
'filesystems.*.type': {'type': 'string'},
'filesystems.*.source': {'type': 'string'},
'filesystems.*.target': {'type': 'string'},
'filesystems.*.raw': {'type': 'string'},
'filesystems.*.options': {'type': 'list'},
'fs_allowed': {'type': 'list'},
'hostname': {'type': 'string'},
'image_uuid': {'type': 'uuid'},
'internal_metadata': {'type': 'flat-object'},
'limit_priv': {'type': 'list'},
'max_locked_memory': {'type': 'integer'},
'max_lwps': {'type': 'integer'},
'max_physical_memory': {'type': 'integer'},
'max_swap': {'type': 'integer'},
'mdata_exec_timeout': {'type': 'integer'},
'nics': {'type': 'object-array'},
'nics.*.allow_dhcp_spoofing': {'type': 'boolean'},
'nics.*.allow_ip_spoofing': {'type': 'boolean'},
'nics.*.allow_mac_spoofing': {'type': 'boolean'},
'nics.*.allow_restricted_traffic': {'type': 'boolean'},
'nics.*.allow_unfiltered_promisc': {'type': 'boolean'},
'nics.*.blocked_outgoing_ports': {'type': 'list'},
'nics.*.dhcp_server': {'type': 'boolean'},
'nics.*.gateway': {'type': 'string'},
'nics.*.interface': {'type': 'string'},
'nics.*.ip': {'type': 'string'},
'nics.*.mac': {'type': 'string'},
'nics.*.model': {'type': 'string'},
'nics.*.netmask': {'type': 'string'},
'nics.*.nic_tag': {'type': 'string'},
'nics.*.primary': {'type': 'boolean'},
'nics.*.vrrp_vrid': {'type': 'integer-8bit'},
'nics.*.vrrp_primary_ip': {'type': 'string'},
'nics.*.vlan_id': {'type': 'integer'},
'nic_driver': {'type': 'string'},
'nowait': {'type': 'boolean'},
'owner_uuid': {'type': 'string'},
'package_name': {'type': 'string'},
'package_version': {'type': 'string'},
'qemu_opts': {'type': 'string'},
'qemu_extra_opts': {'type': 'string'},
'quota': {'type': 'integer'},
'ram': {'type': 'integer'},
'remove_customer_metadata': {'type': 'list'},
'remove_disks': {'type': 'list'},
'remove_internal_metadata': {'type': 'list'},
'remove_nics': {'type': 'list'},
'remove_tags': {'type': 'list'},
'resolvers': {'type': 'list'},
'set_tags': {'type': 'flat-object'},
'set_customer_metadata': {'type': 'flat-object'},
'set_internal_metadata': {'type': 'flat-object'},
'spice_opts': {'type': 'string'},
'spice_password': {'type': 'string'},
'spice_port': {'type': 'integer'},
'tags': {'type': 'flat-object'},
'tmpfs': {'type': 'integer'},
'transition': {'type': 'flat-object'},
'update_disks': {'type': 'object-array', 'check_as': 'disks'},
'update_nics': {'type': 'object-array', 'check_as': 'nics'},
'uuid': {'type': 'uuid'},
'vcpus': {'type': 'integer'},
'vga': {'type': 'string'},
'virtio_txburst': {'type': 'integer'},
'virtio_txtimer': {'type': 'integer'},
'vnc_password': {'type': 'string'},
'vnc_port': {'type': 'integer'},
'zfs_data_compression': {'type': 'string'},
'zfs_data_recsize': {'type': 'integer'},
'zfs_io_priority': {'type': 'integer'},
'zfs_root_compression': {'type': 'string'},
'zfs_root_recsize': {'type': 'integer'},
'zone_dataset_uuid': {'type': 'uuid'},
'zonename': {'type': 'string'},
'zfs_storage_pool_name': {'type': 'zpool'},
'zpool': {'type': 'zpool'}
};
// shared between 'joyent' and 'joyent-minimal'
var joyent_allowed = {
'add_nics': ['update'],
'alias': ['create', 'receive', 'update'],
'autoboot': ['create', 'receive', 'update'],
'billing_id': ['create', 'receive', 'update'],
'brand': ['create', 'receive'],
'cpu_cap': ['create', 'receive', 'update'],
'cpu_shares': ['create', 'receive', 'update'],
'create_only': ['receive'],
'create_timestamp': ['receive'],
'customer_metadata': ['create', 'receive'],
'dataset_uuid': ['create', 'receive'],
'delegate_dataset': ['create', 'receive'],
'do_not_inventory': ['create', 'receive', 'update'],
'dns_domain': ['create', 'receive'],
'filesystems': ['create', 'receive'],
'filesystems.*.type': ['add'],
'filesystems.*.source': ['add'],
'filesystems.*.target': ['add'],
'filesystems.*.raw': ['add'],
'filesystems.*.options': ['add'],
'fs_allowed': ['create', 'receive', 'update'],
'hostname': ['create', 'receive', 'update'],
'image_uuid': ['create', 'receive'],
'internal_metadata': ['create', 'receive'],
'limit_priv': ['create', 'receive', 'update'],
'max_locked_memory': ['create', 'receive', 'update'],
'max_lwps': ['create', 'receive', 'update'],
'max_physical_memory': ['create', 'receive', 'update'],
'max_swap': ['create', 'receive', 'update'],
'mdata_exec_timeout': ['create'],
'nics': ['create', 'receive'],
'nics.*.allow_dhcp_spoofing': ['add', 'update'],
'nics.*.allow_ip_spoofing': ['add', 'update'],
'nics.*.allow_mac_spoofing': ['add', 'update'],
'nics.*.allow_restricted_traffic': ['add', 'update'],
'nics.*.blocked_outgoing_ports': ['add', 'update'],
'nics.*.dhcp_server': ['add', 'update'],
'nics.*.gateway': ['add', 'update'],
'nics.*.interface': ['add', 'update'],
'nics.*.ip': ['add', 'update'],
'nics.*.mac': ['add', 'update'],
'nics.*.netmask': ['add', 'update'],
'nics.*.nic_tag': ['add', 'update'],
'nics.*.vrrp_vrid': ['add', 'update'],
'nics.*.vrrp_primary_ip': ['add', 'update'],
'nics.*.primary': ['add', 'update'],
'nics.*.vlan_id': ['add', 'update'],
'nowait': ['create', 'receive'],
'owner_uuid': ['create', 'receive', 'update'],
'package_name': ['create', 'receive', 'update'],
'package_version': ['create', 'receive', 'update'],
'quota': ['create', 'receive', 'update'],
'ram': ['create', 'receive', 'update'],
'remove_customer_metadata': ['update'],
'remove_internal_metadata': ['update'],
'remove_nics': ['update'],
'remove_tags': ['update'],
'resolvers': ['create', 'receive', 'update'],
'set_customer_metadata': ['update'],
'set_internal_metadata': ['update'],
'set_tags': ['update'],
'tags': ['create', 'receive'],
'tmpfs': ['create', 'receive', 'update'],
'transition': ['receive'],
'update_nics': ['update'],
'uuid': ['create', 'receive'],
'zfs_data_compression': ['create', 'receive', 'update'],
'zfs_data_recsize': ['create', 'receive', 'update'],
'zfs_io_priority': ['create', 'receive', 'update'],
'zfs_root_compression': ['create', 'receive', 'update'],
'zfs_root_recsize': ['create', 'receive', 'update'],
'zfs_storage_pool_name': ['create', 'receive'],
'zonename': ['create', 'receive'],
'zpool': ['create', 'receive']
};
/*
* This defines all of the properties allowed, required and features that a
* brand has. For each of the allowed/required properties you have a list of
* actions for which this is allowed/required. For properties that are lists
* of objects, you can specify the action as 'add' or 'update' for when you're
* adding or updating one of those objects.
*
* Features can currently be one of:
*
* 'check_avail_ram' -- (boolean) whether we should check system ram on create
* 'cleanup_dataset' -- (boolean) whether to remove trash before booting
* 'default_memory_overhead' -- (integer) memory above 'ram' that's added
* 'limit_priv': (list) list of priviledges for this zone (if not 'default')
* 'min_memory_overhead' -- (integer) minimum delta between ram + max_physical
* 'model_required' -- (boolean) whether a .model is required on nics and disks
* 'pid_file' -- (pathname) file containing the PID for zones with one process
* 'runtime_info' -- (boolean) whether this zone supports the 'info' command
* 'serial_console' -- (boolean) whether this zone uses serial console
* 'type' -- the type of the VM (OS or KVM), all brands should include this
* 'update_mdata_exec_timeout' (boolean) whether to update mdata:exec timeout
* 'update_rctls' (boolean) whether we can update rctls 'live' for this zone
* 'use_tmpfs' -- (boolean) whether this type of zone uses tmpfs
* 'use_vm_autoboot' -- (boolean) use vm-autoboot instead of autoboot
* 'use_vmadmd' -- (boolean) use vmadmd for some actions instead of direct
* 'var_svc_provisioning' -- (boolean) whether brand uses /var/svc/provisioning
* 'wait_for_hwsetup' -- (boolean) use QMP and provision_success when hwsetup
* 'write_zone_netfiles' -- (boolean) write out files like /etc/hostname.net0
* 'zlogin_console' -- (boolean) use zlogin -C for console (vs. serial_console)
* 'zoneinit' -- (boolean) this brand's setup may be controlled by zoneinit
*
* All of the keys:
*
* allowed_properties
* required_properties
* features
*
* should be defined for each brand. Even if empty.
*/
var BRAND_OPTIONS = {
'joyent': {
'allowed_properties': joyent_allowed,
'required_properties': {
'brand': ['create', 'receive'],
'image_uuid': ['create', 'receive']
}, 'features': {
'cleanup_dataset': true,
'type': 'OS',
'update_mdata_exec_timeout': true,
'update_rctls': true,
'use_tmpfs': true,
'write_zone_netfiles': true,
'zlogin_console': true,
'zoneinit': true
}
}, 'joyent-minimal': {
'allowed_properties': joyent_allowed,
'required_properties': {
'brand': ['create', 'receive'],
'image_uuid': ['create', 'receive']
}, 'features': {
'cleanup_dataset': true,
'type': 'OS',
'update_mdata_exec_timeout': true,
'update_rctls': true,
'use_tmpfs': true,
'var_svc_provisioning': true,
'write_zone_netfiles': true,
'zlogin_console': true
}
}, 'kvm': {
'allowed_properties': {
'add_disks': ['update'],
'add_nics': ['update'],
'alias': ['create', 'receive', 'update'],
'autoboot': ['create', 'receive', 'update'],
'billing_id': ['create', 'receive', 'update'],
'boot': ['create', 'receive', 'update'],
'brand': ['create', 'receive'],
'cpu_cap': ['create', 'receive', 'update'],
'cpu_shares': ['create', 'receive', 'update'],
'cpu_type': ['create', 'receive', 'update'],
'create_only': ['receive'],
'create_timestamp': ['receive'],
'customer_metadata': ['create', 'receive'],
'disks': ['create', 'receive'],
'disks.*.block_size': ['add'],
'disks.*.boot': ['add', 'update'],
'disks.*.compression': ['add', 'update'],
'disks.*.image_name': ['add', 'update'],
'disks.*.image_size': ['add'],
'disks.*.image_uuid': ['add'],
'disks.*.size': ['add'],
'disks.*.media': ['add', 'update'],
'disks.*.model': ['add', 'update'],
'disks.*.nocreate': ['add'],
'disks.*.path': ['add', 'update'],
'disks.*.zpool': ['add'],
'disk_driver': ['create', 'receive', 'update'],
'do_not_inventory': ['create', 'receive', 'update'],
'hostname': ['create', 'receive', 'update'],
'image_uuid': ['create', 'receive'],
'internal_metadata': ['create', 'receive'],
'limit_priv': ['create', 'receive', 'update'],
'max_locked_memory': ['create', 'receive', 'update'],
'max_lwps': ['create', 'receive', 'update'],
'max_physical_memory': ['create', 'receive', 'update'],
'max_swap': ['create', 'receive', 'update'],
'nics': ['create', 'receive'],
'nics.*.allow_dhcp_spoofing': ['add', 'update'],
'nics.*.allow_ip_spoofing': ['add', 'update'],
'nics.*.allow_mac_spoofing': ['add', 'update'],
'nics.*.allow_restricted_traffic': ['add', 'update'],
'nics.*.allow_unfiltered_promisc': ['add', 'update'],
'nics.*.blocked_outgoing_ports': ['add', 'update'],
'nics.*.dhcp_server': ['add', 'update'],
'nics.*.gateway': ['add', 'update'],
'nics.*.interface': ['add', 'update'],
'nics.*.ip': ['add', 'update'],
'nics.*.mac': ['add', 'update'],
'nics.*.model': ['add'],
'nics.*.netmask': ['add', 'update'],
'nics.*.nic_tag': ['add', 'update'],
'nics.*.primary': ['add', 'update'],
'nics.*.vlan_id': ['add', 'update'],
'nic_driver': ['create', 'receive', 'update'],
'owner_uuid': ['create', 'receive', 'update'],
'package_name': ['create', 'receive', 'update'],
'package_version': ['create', 'receive', 'update'],
'qemu_opts': ['create', 'receive', 'update'],
'qemu_extra_opts': ['create', 'receive', 'update'],
'quota': ['create', 'receive', 'update'],
'ram': ['create', 'receive', 'update'],
'remove_customer_metadata': ['update'],
'remove_disks': ['update'],
'remove_internal_metadata': ['update'],
'remove_nics': ['update'],
'remove_tags': ['update'],
'resolvers': ['create', 'receive', 'update'],
'set_customer_metadata': ['update'],
'set_internal_metadata': ['update'],
'set_tags': ['update'],
'spice_opts': ['create', 'receive', 'update'],
'spice_password': ['create', 'receive', 'update'],
'spice_port': ['create', 'receive', 'update'],
'transition': ['receive'],
'update_disks': ['update'],
'update_nics': ['update'],
'uuid': ['create', 'receive'],
'vcpus': ['create', 'receive', 'update'],
'vga': ['create', 'receive', 'update'],
'virtio_txburst': ['create', 'receive', 'update'],
'virtio_txtimer': ['create', 'receive', 'update'],
'vnc_password': ['create', 'receive', 'update'],
'vnc_port': ['create', 'receive', 'update'],
'zfs_io_priority': ['create', 'receive', 'update'],
'zfs_root_compression': ['create', 'receive', 'update'],
'zfs_root_recsize': ['create', 'receive', 'update'],
'zone_dataset_uuid': ['create', 'receive'],
'zpool': ['create', 'receive']
}, 'required_properties': {
'brand': ['create', 'receive']
}, 'features': {
'check_avail_ram': true,
'default_memory_overhead': VM.KVM_MEM_OVERHEAD,
'limit_priv': ['default', '-file_link_any', '-net_access',
'-proc_fork', '-proc_info', '-proc_session'],
'min_memory_overhead': VM.KVM_MIN_MEM_OVERHEAD,
'model_required': true,
'pid_file': '/tmp/vm.pid',
'runtime_info': true,
'serial_console': true,
'type': 'KVM',
'use_vm_autoboot': true,
'use_vmadmd': true,
'var_svc_provisioning': true,
'wait_for_hwsetup': true
}
}
};
var VIRTIO_TXTIMER_DEFAULT = 200000;
var VIRTIO_TXBURST_DEFAULT = 128;
function getZpools(callback)
{
var args = ['list', '-H', '-p', '-o', 'name'];
var cmd = '/usr/sbin/zpool';
var idx;
var raw = [];
var zpools = [];
VM.log.debug(cmd + ' ' + args.join(' '));
execFile(cmd, args, function (error, stdout, stderr) {
if (error) {
VM.log.error('Unable to get list of zpools');
callback(error, {'stdout': stdout, 'stderr': stderr});
} else {
// strip out any empty values (last one).
raw = stdout.split('\n');
for (idx in raw) {
if (raw[idx].length > 0) {
zpools.push(raw[idx]);
}
}
callback(null, zpools);
}
});
}
function validateProperty(brand, prop, value, action, data, errors)
{
var allowed;
var k;
if (!data.hasOwnProperty('zpools')) {
data.zpools = [];
}
if (BRAND_OPTIONS[brand].hasOwnProperty('allowed_properties')) {
allowed = BRAND_OPTIONS[brand].allowed_properties;
} else {
allowed = {};
}
if (!errors.hasOwnProperty('bad_values')) {
errors.bad_values = [];
}
if (!errors.hasOwnProperty('bad_properties')) {
errors.bad_properties = [];
}
if (!allowed.hasOwnProperty(prop)) {
// thie BRAND_OPTIONS doesn't have this property at all
if (errors.bad_properties.indexOf(prop) === -1) {
errors.bad_properties.push(prop);
}
} else if (!Array.isArray(allowed[prop])
|| allowed[prop].indexOf(action) === -1) {
// here we've ether got no actions allowed for this value,
// or just not this one
if (errors.bad_properties.indexOf(prop) === -1) {
errors.bad_properties.push(prop);
}
}
if (PAYLOAD_PROPERTIES.hasOwnProperty(prop)) {
switch (PAYLOAD_PROPERTIES[prop].type) {
case 'uuid':
if (typeof (value) === 'string' && !isUUID(value)
&& errors.bad_values.indexOf(prop) === -1) {
errors.bad_values.push(prop);
}
break;
case 'boolean':
if (value === 1 || value === '1') {
VM.log.warn('DEPRECATED: payload uses 1 instead of '
+ 'true for ' + prop + ', use "true" instead.');
} else if (typeof (fixBoolean(value)) !== 'boolean'
&& errors.bad_values.indexOf(prop) === -1) {
errors.bad_values.push(prop);
}
break;
case 'string':