Repository navigation
Expand file tree
/
Copy pathedit.php
More file actions
706 lines (695 loc) · 37 KB
/
Copy pathedit.php
File metadata and controls
706 lines (695 loc) · 37 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
<?php
require_once 'function.php';
// libvirt device modification flags
const AFFECT_LIVE = 1;
const AFFECT_CONFIG = 2;
const AFFECT_LIVE_CONFIG = 3;
// undefine: managed save image, snapshot metadata and UEFI variables file
const UNDEFINE_ALL_METADATA = 1 | 2 | 4;
$node = (string)($_GET['node'] ?? $_POST['node'] ?? '');
if (!machine_visible($con, $node)) {
flash_set('danger', t('Unknown machine: %s', $node));
redirect('index.php');
}
$edit_url = 'edit.php?node='.urlencode($node);
require_machine('operate', $node, 'node.php?node='.urlencode($node));
$res = libvirt_domain_lookup_by_name($con, $node);
$active = (bool)libvirt_domain_is_active($res);
$xml = domain_definition_xml($res);
$node_info = libvirt_node_get_info($con);
$max_memory_mb = (int)floor($node_info['memory'] / 1024);
$max_vcpus = (int)$node_info['cpus'];
$isos = storage_iso_list($con);
$pools = storage_active_pools($con);
// interface targets: libvirt networks, host bridges, direct (macvtap) interfaces
$networks = nic_targets_for($con, $connection);
// attaches a device: live and persistent for a running machine (falling back to
// the persistent definition only, e.g. when q35 has no free PCIe port), otherwise
// persistent; returns '' when attached live, the "after restart" note otherwise, false on error
function attach_device($con, $res, $xml, $active, $device) {
if (!$active) {
return libvirt_domain_define_xml($con, vm_xml_add_device($xml, $device)) ? '' : false;
}
if (libvirt_domain_attach_device($res, $device, AFFECT_LIVE_CONFIG)) {
return '';
}
return libvirt_domain_attach_device($res, $device, AFFECT_CONFIG)
? ' '.t('It could not be attached to the running machine and will be available after it is shut down and started again.')
: false;
}
// capacity of a disk in bytes (running machine: block info, otherwise the volume), null when unknown
function disk_capacity($con, $res, $active, $target, $path) {
if ($active) {
$block = @libvirt_domain_get_block_info($res, $target);
return is_array($block) ? (int)$block['capacity'] : null;
}
$vol = $path !== '' ? @libvirt_storagevolume_lookup_by_path($con, $path) : false;
$info = $vol ? @libvirt_storagevolume_get_info($vol) : false;
return is_array($info) ? (int)$info['capacity'] : null;
}
// redefines the machine with the new persistent XML or throws
function define_or_fail($con, $new_xml) {
if (!libvirt_domain_define_xml($con, $new_xml)) {
throw new RuntimeException(t('Error: %s', libvirt_get_last_error()));
}
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
csrf_require();
$action = (string)($_POST['action'] ?? '');
readonly_guard($edit_url, null);
// deleting: administrators, or the owner with the localadmin role; cloning creates a machine
if ($action === 'delete' && !machine_allowed('delete', $node)) {
require_permission('admin', $edit_url);
}
if ($action === 'clone') {
require_permission('create', $edit_url);
}
$after_restart = $active ? ' '.t('Takes effect after the machine is shut down and started again.') : '';
$details = '';
$message = '';
$back = $edit_url;
try {
if ($action === 'resources') {
$memory = (int)($_POST['memory'] ?? 0);
$current = (int)($_POST['current_memory'] ?? $memory);
$cpu = [
'mode' => (string)($_POST['cpu_mode'] ?? 'host-passthrough'),
'sockets' => (int)($_POST['sockets'] ?? 1),
'cores' => (int)($_POST['cores'] ?? 1),
'threads' => (int)($_POST['threads'] ?? 1),
];
$vcpus = $cpu['sockets'] * $cpu['cores'] * $cpu['threads'];
// vCPUs online now (hot-plug up to the topology maximum) and pinning to host CPUs
$current_vcpus = (int)($_POST['current_vcpus'] ?? $vcpus);
$cpuset = str_replace(' ', '', (string)($_POST['cpuset'] ?? ''));
if ($memory < 128 || $memory > $max_memory_mb) {
throw new RuntimeException(t('Memory must be between %d and %d MB.', 128, $max_memory_mb));
}
if ($current < 128 || $current > $memory) {
throw new RuntimeException(t('Current memory must be between %d MB and the maximum memory.', 128));
}
if (min($cpu['sockets'], $cpu['cores'], $cpu['threads']) < 1 || $vcpus > $max_vcpus) {
throw new RuntimeException(t('vCPUs (sockets x cores x threads) must be between %d and %d.', 1, $max_vcpus));
}
$old_max = vm_memory_mb($xml);
$old_cpu = vm_cpu_settings($xml);
// the current mode stays allowed (e.g. custom with a named model)
if (!isset(VM_CPU_MODES[$cpu['mode']]) && $cpu['mode'] !== $old_cpu['mode']) {
throw new RuntimeException(t('Unknown CPU mode.'));
}
if ($current_vcpus < 1 || $current_vcpus > $vcpus) {
throw new RuntimeException(t('Current vCPUs must be between 1 and the maximum (sockets x cores x threads).'));
}
if ($cpuset !== '' && !vm_cpuset_valid($cpuset, $max_vcpus)) {
throw new RuntimeException(t('Host CPUs: a list such as 0-3 or 2,3 within 0-%d.', $max_vcpus - 1));
}
$old_vcpu = vm_vcpu_settings($xml);
// quota: the machine with its new size instead of the current one
if (($quota = quota_check($con, ['vcpus' => $vcpus, 'memory_mb' => $memory, 'disk_gb' => 0], $node)) !== '') {
throw new RuntimeException($quota);
}
$old_vcpus = $old_cpu['sockets'] * $old_cpu['cores'] * $old_cpu['threads'];
define_or_fail($con, vm_xml_set_vcpu_options(vm_xml_set_resources($xml, $memory, $vcpus, $current, $cpu), $current_vcpus, $cpuset));
$details = sprintf('%d/%d MB, %d/%d vCPU (%dx%dx%d), %s, cpuset %s', $current, $memory, $current_vcpus, $vcpus,
$cpu['sockets'], $cpu['cores'], $cpu['threads'], $cpu['mode'], $cpuset ?: '-');
$message = t('Memory and vCPUs saved.');
// the balloon target of a running machine changes at once (up to the current maximum)
$only_balloon = $memory === $old_max && $cpu == $old_cpu && $vcpus === $old_vcpus;
if ($active && $current !== vm_memory_mb($xml, 'currentMemory') && $current <= $old_max) {
$message .= libvirt_domain_set_memory_flags($res, $current * 1024, AFFECT_LIVE)
? ' '.t('The running machine now has %d MB.', $current)
: ' '.t('Live change failed (%s).', libvirt_get_last_error());
}
// vCPU hot-plug and pinning of the running machine (the maximum stays until a restart)
$live_vcpus = $active && $vcpus === $old_vcpus;
if ($live_vcpus && $current_vcpus !== $old_vcpu['current']) {
[$status, $output] = domain_virsh($connection, ['setvcpus', $node, (string)$current_vcpus, '--live']);
$message .= ' '.($status === 0 ? t('The running machine now has %d vCPUs.', $current_vcpus) : t('Live change failed (%s).', trim($output)));
}
if ($active && $cpuset !== $old_vcpu['cpuset']) {
$pin = $cpuset !== '' ? $cpuset : '0-'.($max_vcpus - 1);
$failed = '';
for ($i = 0; $i < min($current_vcpus, $old_vcpu['current']); $i++) {
[$status, $output] = domain_virsh($connection, ['vcpupin', $node, (string)$i, $pin, '--live']);
$failed = $status === 0 ? $failed : trim($output);
}
$message .= ' '.($failed === '' ? t('vCPU pinning applied to the running machine.') : t('Live change failed (%s).', $failed));
}
$only_balloon = $only_balloon && $current_vcpus === $old_vcpu['current'] && $cpuset === $old_vcpu['cpuset'];
if ($active && !$only_balloon && !($live_vcpus && $memory === $old_max && $cpu == $old_cpu)) {
$message .= $after_restart;
}
}
elseif ($action === 'cdrom') {
$iso = (string)($_POST['iso'] ?? '');
if ($iso !== '' && !isset($isos[$iso])) {
throw new RuntimeException(t('Unknown ISO image.'));
}
[$new_xml, $device] = vm_xml_set_cdrom($xml, $iso);
define_or_fail($con, $new_xml);
$message = $iso !== '' ? t('ISO image inserted.') : t('CD/DVD ejected.');
if ($active && $device !== null) {
// change the medium in the running machine as well
if (!libvirt_domain_update_device($res, $device, AFFECT_LIVE)) {
$message .= ' '.t('Live change failed (%s).', libvirt_get_last_error()).$after_restart;
}
}
elseif ($active) {
$message = t('CD/DVD drive added.').$after_restart;
}
$details = $iso ?: 'eject';
}
elseif ($action === 'add_disk') {
$source = (string)($_POST['source'] ?? 'new');
$bus = (string)($_POST['bus'] ?? 'virtio');
$cache = (string)($_POST['cache'] ?? '');
if (!isset(DISK_BUSES[$bus])) {
throw new RuntimeException(t('Unknown disk bus.'));
}
if (!isset(DISK_CACHE_MODES[$cache])) {
throw new RuntimeException(t('Unknown cache mode.'));
}
$target = vm_next_target($xml, vm_disk_target_prefix($bus));
$vol = null;
$new_gb = $source === 'existing' ? 0 : (int)($_POST['size'] ?? 0);
if ($source === 'existing') {
$info = @libvirt_storagevolume_get_info(@libvirt_storagevolume_lookup_by_path($con, (string)($_POST['volume'] ?? '')));
$new_gb = is_array($info) ? (int)ceil($info['capacity'] / 1073741824) : 0;
}
if (($quota = quota_check($con, ['disk_gb' => $new_gb])) !== '') {
throw new RuntimeException($quota);
}
if ($source === 'existing') {
// an existing, unused volume (e.g. a disk image moved from another host)
$path = (string)($_POST['volume'] ?? '');
$free = storage_free_volumes($con);
if (!isset($free[$path])) {
throw new RuntimeException(t('Select a volume that is not used by a machine.'));
}
$device = vm_disk_device_xml($path, $target, $bus, $free[$path]['format'], $cache);
$details = $target.' '.$path;
$message = t('Disk %s attached (%s).', $target, basename($path));
}
else {
$size = (int)($_POST['size'] ?? 0);
$pool = (string)($_POST['pool'] ?? '');
if ($size < 1 || $size > 4096) {
throw new RuntimeException(t('Disk size must be between %d and %d GB.', 1, 4096));
}
if (!isset($pools[$pool])) {
throw new RuntimeException(t('Select an active storage pool.'));
}
$vol = storage_create_qcow2($con, $pool, $node.'-'.$target.'.qcow2', $size);
$device = vm_disk_device_xml((string)libvirt_storagevolume_get_path($vol), $target, $bus, 'qcow2', $cache);
$details = $target.', '.$size.' GB in '.$pool;
$message = t('Disk %s (%d GB) added.', $target, $size);
}
// SCSI disks need a virtio-scsi controller
if ($bus === 'scsi' && !vm_has_virtio_scsi($xml)) {
$controller = "<controller type='scsi' model='virtio-scsi'/>";
if (attach_device($con, $res, $xml, $active, $controller) === false) {
if ($vol) {
libvirt_storagevolume_delete($vol, 0);
}
throw new RuntimeException(t('Error: %s', libvirt_get_last_error()));
}
$xml = domain_definition_xml($res);
}
$note = attach_device($con, $res, $xml, $active, $device);
if ($note === false) {
$error = libvirt_get_last_error();
if ($vol) {
libvirt_storagevolume_delete($vol, 0);
}
throw new RuntimeException(t('Error: %s', $error));
}
$details .= ', '.$bus.($cache !== '' ? ', cache '.$cache : '').($note ? ', after restart' : '');
$message .= $note;
}
elseif ($action === 'add_nic') {
$network = nic_target_normalize((string)($_POST['network'] ?? ''));
if (!nic_target_valid($network, $networks)) {
throw new RuntimeException(t('Select a network.'));
}
$note = attach_device($con, $res, $xml, $active, vm_nic_device_xml($network));
if ($note === false) {
throw new RuntimeException(t('Error: %s', libvirt_get_last_error()));
}
$details = $network.($note ? ', after restart' : '');
$message = t('Network interface added.').$note;
}
elseif ($action === 'detach_disk') {
$target = (string)($_POST['target'] ?? '');
$disks = vm_disk_devices($xml);
if (!isset($disks[$target]) || $disks[$target]['device'] !== 'disk') {
throw new RuntimeException(t('Unknown disk.'));
}
$delete = !empty($_POST['delete_volume']);
if ($delete && ($active || !machine_allowed('delete', $node))) {
throw new RuntimeException(t('The disk file can be deleted only by a user allowed to delete the machine, with the machine shut down.'));
}
$path = $disks[$target]['path'];
if ($delete && in_array($path, storage_sources_in_use($con, $node), true)) {
throw new RuntimeException(t('The disk is used by another machine and cannot be deleted.'));
}
if ($active) {
// the guest releases the disk (hot-unplug), the definition changes at once
if (!libvirt_domain_detach_device($res, $disks[$target]['xml'], AFFECT_LIVE_CONFIG)) {
throw new RuntimeException(t('Error: %s', libvirt_get_last_error()));
}
}
else {
define_or_fail($con, vm_xml_remove_disk($xml, $target));
}
$message = t('Disk %s detached.', $target);
$details = $target.' '.$path;
if ($delete) {
$vol = libvirt_storagevolume_lookup_by_path($con, $path);
if ($vol && libvirt_storagevolume_delete($vol, 0)) {
$message .= ' '.t('The disk file was deleted.');
$details .= ', file deleted';
}
else {
$message .= ' '.t('Could not delete: %s', $path);
}
}
}
elseif ($action === 'resize_disk') {
$target = (string)($_POST['target'] ?? '');
$size_gb = (int)($_POST['size'] ?? 0);
$disks = vm_disk_devices($xml);
if (!isset($disks[$target]) || $disks[$target]['device'] !== 'disk') {
throw new RuntimeException(t('Unknown disk.'));
}
$bytes = $size_gb * 1073741824;
$current = disk_capacity($con, $res, $active, $target, $disks[$target]['path']);
if ($size_gb < 1 || $size_gb > 65536) {
throw new RuntimeException(t('Disk size must be between %d and %d GB.', 1, 65536));
}
if ($current !== null && $bytes <= $current) {
throw new RuntimeException(t('The new size must be larger than the current %s (shrinking would destroy data).', format_bytes($current)));
}
if (($quota = quota_check($con, ['disk_gb' => $size_gb - (int)ceil(($current ?? 0) / 1073741824)])) !== '') {
throw new RuntimeException($quota);
}
if ($active) {
// flag 1 = size in bytes; the guest sees the bigger disk at once
$ok = libvirt_domain_block_resize($res, $target, $bytes, 1);
}
else {
$vol = libvirt_storagevolume_lookup_by_path($con, $disks[$target]['path']);
$ok = $vol && libvirt_storagevolume_resize($vol, $bytes, 0) !== false;
}
if (!$ok) {
throw new RuntimeException(t('Error: %s', libvirt_get_last_error()));
}
$details = $target.' -> '.$size_gb.' GB';
$message = t('Disk %s resized to %d GB. Grow the partition and file system in the guest (e.g. growpart, resize2fs); cloud images do it at the next boot.', $target, $size_gb);
}
elseif ($action === 'detach_nic') {
$mac = strtolower((string)($_POST['mac'] ?? ''));
$nics = vm_nic_devices($xml);
if (!isset($nics[$mac])) {
throw new RuntimeException(t('Unknown network interface.'));
}
if ($active) {
if (!libvirt_domain_detach_device($res, $nics[$mac]['xml'], AFFECT_LIVE_CONFIG)) {
throw new RuntimeException(t('Error: %s', libvirt_get_last_error()));
}
}
else {
define_or_fail($con, vm_xml_remove_nic($xml, $mac));
}
$details = $mac;
$message = t('Network interface %s detached.', $mac);
}
elseif ($action === 'nic_change') {
$mac = strtolower((string)($_POST['mac'] ?? ''));
$network = nic_target_normalize((string)($_POST['network'] ?? ''));
$model = (string)($_POST['model'] ?? '');
$nics = vm_nic_devices($xml);
if (!isset($nics[$mac])) {
throw new RuntimeException(t('Unknown network interface.'));
}
if (!nic_target_valid($network, $networks)) {
throw new RuntimeException(t('Select a network.'));
}
if (!isset(NIC_MODELS[$model])) {
throw new RuntimeException(t('Unknown network card model.'));
}
$new_nic = vm_nic_xml_change($nics[$mac]['xml'], $network, $model);
$model_changed = $model !== $nics[$mac]['model'];
if ($active && !$model_changed && libvirt_domain_update_device($res, $new_nic, AFFECT_LIVE_CONFIG)) {
// switched in the running machine (the link stays, the new connection at once)
$message = t('Network interface %s connected to %s.', $mac, nic_target_parse($network)[1]);
}
elseif ($active && !$model_changed) {
// some changes (e.g. network -> macvtap) cannot be made live: the definition only
define_or_fail($con, vm_xml_replace_nic($xml, $mac, $new_nic));
$message = t('Network interface %s saved.', $mac).$after_restart;
}
else {
define_or_fail($con, vm_xml_replace_nic($xml, $mac, $new_nic));
$message = t('Network interface %s saved.', $mac).($model_changed ? $after_restart : '');
}
$details = $mac.': '.$network.', '.$model;
}
elseif ($action === 'hostdev_attach' || $action === 'hostdev_detach') {
if (!can('admin')) {
throw new RuntimeException(t('You do not have permission for this action.'));
}
$key = (string)($_POST['device'] ?? '');
$attached = vm_hostdevs($xml);
if ($action === 'hostdev_attach') {
$available = hostdev_usb_list($con) + hostdev_pci_list($con);
$device = isset($available[$key]) ? hostdev_xml($key) : null;
if ($device === null) {
throw new RuntimeException(t('Unknown host device.'));
}
if (isset($attached[$key])) {
throw new RuntimeException(t('The device is already attached.'));
}
// USB goes live into a running machine; PCI only into the definition (next start)
if (str_starts_with($key, 'usb:')) {
$note = attach_device($con, $res, $xml, $active, $device);
if ($note === false) {
throw new RuntimeException(t('Error: %s', libvirt_get_last_error()));
}
}
else {
define_or_fail($con, vm_xml_add_device($xml, $device));
$note = $after_restart;
}
$message = t('Host device %s attached.', $available[$key]['label']).$note;
}
else {
if (!isset($attached[$key])) {
throw new RuntimeException(t('Unknown host device.'));
}
if ($active) {
if (!libvirt_domain_detach_device($res, $attached[$key], AFFECT_LIVE_CONFIG)) {
throw new RuntimeException(t('Error: %s', libvirt_get_last_error()));
}
}
else {
define_or_fail($con, vm_xml_remove_hostdev($xml, $key));
}
$message = t('Host device %s detached.', $key);
}
$details = $key;
}
elseif ($action === 'disk_iotune') {
$target = (string)($_POST['target'] ?? '');
$mbs = (float)($_POST['mbs'] ?? 0);
$iops = (int)($_POST['iops'] ?? 0);
if (!isset(vm_disk_devices($xml)[$target]) || vm_disk_devices($xml)[$target]['device'] !== 'disk') {
throw new RuntimeException(t('Unknown disk.'));
}
if ($mbs < 0 || $mbs > 100000 || $iops < 0 || $iops > 10000000) {
throw new RuntimeException(t('Limits must be 0 (none) or positive numbers.'));
}
// --config always, --live as well for a running machine; 0 removes a limit
$args = ['blkdeviotune', $node, $target, '--total-bytes-sec', (string)(int)round($mbs * 1048576), '--total-iops-sec', (string)$iops, '--config'];
if ($active) {
$args[] = '--live';
}
[$status, $output] = domain_virsh($connection, $args);
if ($status !== 0) {
throw new RuntimeException(t('Error: %s', trim($output)));
}
$details = $target.': '.$mbs.' MB/s, '.$iops.' IOPS';
$message = t('I/O limits of %s saved.', $target);
}
elseif ($action === 'nic_bandwidth') {
$mac = strtolower((string)($_POST['mac'] ?? ''));
$in = (float)($_POST['in'] ?? 0);
$out = (float)($_POST['out'] ?? 0);
if (!isset(vm_nic_devices($xml)[$mac])) {
throw new RuntimeException(t('Unknown network interface.'));
}
if ($in < 0 || $in > 100000 || $out < 0 || $out > 100000) {
throw new RuntimeException(t('Limits must be 0 (none) or positive numbers.'));
}
// inbound = traffic to the machine (download), outbound = from it; KiB/s, 0 removes
$args = ['domiftune', $node, $mac, '--inbound', (string)vm_mbit_to_kib($in), '--outbound', (string)vm_mbit_to_kib($out), '--config'];
if ($active) {
$args[] = '--live';
}
[$status, $output] = domain_virsh($connection, $args);
if ($status !== 0) {
throw new RuntimeException(t('Error: %s', trim($output)));
}
$details = $mac.': in '.$in.' Mbit/s, out '.$out.' Mbit/s';
$message = t('Bandwidth limits of %s saved.', $mac);
}
elseif ($action === 'describe') {
$title = trim(preg_replace('/\s+/', ' ', (string)($_POST['title'] ?? '')));
$description = trim(str_replace("\r\n", "\n", (string)($_POST['description'] ?? '')));
if (mb_strlen($title) > 120 || mb_strlen($description) > 4000) {
throw new RuntimeException(t('Title: up to 120 characters, description: up to 4000.'));
}
// virsh desc changes the definition and, for a running machine, the live state
foreach (['--title' => $title, '' => $description] as $flag => $text) {
$args = array_values(array_filter(['desc', $node, '--config', $active ? '--live' : '', $flag, '--new-desc', $text], fn($a) => $a !== ''));
// an empty text must still be passed to clear the field
if ($text === '') {
$args[] = '';
}
[$status, $output] = domain_virsh($connection, $args);
if ($status !== 0) {
throw new RuntimeException(t('Error: %s', trim($output)));
}
}
$details = $title;
$message = t('Title and description saved.');
}
elseif ($action === 'firmware') {
$firmware = (string)($_POST['firmware'] ?? 'bios');
if ($active) {
throw new RuntimeException(t('Shut the machine down first.'));
}
if (!isset(DOMAIN_FIRMWARES[$firmware])) {
throw new RuntimeException(t('Select the firmware.'));
}
$current_fw = vm_firmware_settings($xml);
if ($firmware === 'efi-secure' && !$current_fw['q35']) {
throw new RuntimeException(t('Secure Boot needs the q35 machine type.'));
}
define_or_fail($con, vm_xml_set_firmware($xml, $firmware, !empty($_POST['tpm'])));
$details = $current_fw['firmware'].' -> '.$firmware.', tpm '.(empty($_POST['tpm']) ? 'off' : 'on');
$message = t('Firmware and TPM saved.');
}
elseif ($action === 'hardware') {
$video = (string)($_POST['video'] ?? 'virtio');
$sound = (string)($_POST['sound'] ?? '');
$watchdog = (string)($_POST['watchdog'] ?? '');
if (!isset(VM_VIDEO_MODELS[$video]) || !isset(VM_SOUND_MODELS[$sound]) || !isset(VM_WATCHDOG_ACTIONS[$watchdog])) {
throw new RuntimeException(t('Unknown device model.'));
}
define_or_fail($con, vm_xml_set_hardware($xml, $video, $sound, $watchdog, !empty($_POST['rng'])));
$details = 'video '.$video.', sound '.($sound ?: '-').', watchdog '.($watchdog ?: '-').', rng '.(empty($_POST['rng']) ? 'off' : 'on');
$message = t('Hardware saved.').$after_restart;
}
elseif ($action === 'vnc_password') {
$password = (string)($_POST['vnc_password'] ?? '');
// VNC passwords are limited to 8 characters by the protocol
if ($password !== '' && !preg_match('/^[\x21-\x7e]{1,8}$/', $password)) {
throw new RuntimeException(t('VNC password: 1-8 printable characters without spaces.'));
}
define_or_fail($con, vm_xml_set_vnc_password($xml, $password));
$details = $password === '' ? 'removed' : 'set';
$message = ($password === '' ? t('VNC password removed.') : t('VNC password set; the browser console uses it automatically.')).$after_restart;
}
elseif ($action === 'fs_add' || $action === 'fs_remove') {
if (!can('admin')) {
throw new RuntimeException(t('You do not have permission for this action.'));
}
$tag = (string)($_POST['tag'] ?? '');
if (!preg_match(VM_FS_TAG_PATTERN, $tag)) {
throw new RuntimeException(t('Mount tag: letters, digits, _ - (up to 36 characters).'));
}
if ($action === 'fs_add') {
$source = rtrim(trim((string)($_POST['source'] ?? '')), '/');
if ($source === '' || !preg_match(STORAGE_PATH_PATTERN, $source) || str_contains($source, '..')) {
throw new RuntimeException(t('Host directory: an absolute path such as /srv/share.'));
}
if (isset(vm_hardware($xml)['filesystems'][$tag])) {
throw new RuntimeException(t('The mount tag %s is already used.', $tag));
}
define_or_fail($con, vm_xml_add_filesystem($xml, $source, $tag));
$details = $source.' as '.$tag;
$message = t('Directory %s shared as %s (in the guest: mount -t virtiofs %s /mnt).', $source, $tag, $tag).$after_restart;
}
else {
define_or_fail($con, vm_xml_remove_filesystem($xml, $tag));
$details = $tag;
$message = t('Shared folder %s removed.', $tag).$after_restart;
}
}
elseif ($action === 'serial_console') {
$enable = !empty($_POST['enable']);
$port = $enable ? (vm_serial_port($xml) ?: console_serial_free_port($con)) : 0;
define_or_fail($con, vm_xml_set_serial($xml, $port));
$details = $enable ? 'tcp 127.0.0.1:'.$port : 'pty';
$message = ($enable ? t('Text console enabled.') : t('Text console disabled.')).$after_restart;
}
elseif ($action === 'boot') {
$order = array_values(array_filter(array_map('strval', (array)($_POST['boot'] ?? []))));
if (!$order) {
throw new RuntimeException(t('Select at least one boot device.'));
}
define_or_fail($con, vm_xml_set_boot_order($xml, $order));
$details = implode(', ', $order);
$message = t('Boot order saved.').$after_restart;
}
elseif ($action === 'rename') {
$name = trim((string)($_POST['name'] ?? ''));
if ($active) {
throw new RuntimeException(t('Shut the machine down first.'));
}
if (!preg_match(DOMAIN_NAME_PATTERN, $name)) {
throw new RuntimeException(t('Name: 1-64 characters, letters, digits, . _ - (must start with a letter or digit).'));
}
if (in_array($name, libvirt_list_domains($con) ?: [], true)) {
throw new RuntimeException(t('A machine with this name already exists.'));
}
[$status, $output] = domain_virsh($connection, ['domrename', $node, $name]);
if ($status !== 0) {
throw new RuntimeException(t('Error: %s', trim($output)));
}
domain_rename_references(connection_current_key(), $node, $name);
action_log('edit_rename', $node, true, $name);
flash_set('success', t('Machine renamed to %s. Disk files keep their names.', $name));
redirect('edit.php?node='.urlencode($name));
}
elseif ($action === 'clone') {
$name = trim((string)($_POST['name'] ?? ''));
if ($active) {
throw new RuntimeException(t('Shut the machine down first.'));
}
if (!preg_match(DOMAIN_NAME_PATTERN, $name)) {
throw new RuntimeException(t('Name: 1-64 characters, letters, digits, . _ - (must start with a letter or digit).'));
}
if (in_array($name, libvirt_list_domains($con) ?: [], true) || job_pending('clone', 'name', $name)) {
throw new RuntimeException(t('A machine with this name already exists.'));
}
$modes = vm_clone_disk_modes($xml, (array)($_POST['disk_mode'] ?? []));
if (($quota = quota_check($con, quota_clone_add($con, $res, $xml, $modes))) !== '') {
throw new RuntimeException($quota);
}
// a clone made by an own-machine role is its own
$id = job_create('clone', ['source' => $node, 'name' => $name, 'disks' => $modes] + owner_job_params());
action_log('clone_queued', $node, true, $name.' (job #'.$id.') '.json_encode($modes));
flash_set('success', t('Cloning to %s started in the background (job #%d).', $name, $id));
redirect('jobs.php');
}
elseif ($action === 'delete') {
if ($active) {
throw new RuntimeException(t('Shut the machine down first.'));
}
$selected = array_map('strval', (array)($_POST['disks'] ?? []));
$disk_paths = vm_disk_paths($xml) + vm_seed_paths($xml);
$shared = storage_sources_in_use($con, $node);
if (!libvirt_domain_undefine_flags($res, UNDEFINE_ALL_METADATA)) {
throw new RuntimeException(t('Error: %s', libvirt_get_last_error()));
}
machine_owner_set(connection_current_key(), $node, null);
$deleted = [];
$errors = [];
foreach ($disk_paths as $target => $path) {
if (!in_array($target, $selected, true) || in_array($path, $shared, true)) {
continue;
}
$vol = libvirt_storagevolume_lookup_by_path($con, $path);
if ($vol && libvirt_storagevolume_delete($vol, 0)) {
$deleted[] = $path;
}
else {
$errors[] = $path;
}
}
$details = 'deleted disks: '.($deleted ? implode(', ', $deleted) : '-');
action_log('delete', $node, !$errors, $details.($errors ? '; failed: '.implode(', ', $errors) : ''));
flash_set($errors ? 'warning' : 'success', t('Machine %s deleted.', $node)
.($deleted ? ' '.t('Deleted disks: %d.', count($deleted)) : '')
.($errors ? ' '.t('Could not delete: %s', implode(', ', $errors)) : ''));
redirect('index.php');
}
else {
throw new RuntimeException(t('Unknown action: %s', $action));
}
action_log('edit_'.$action, $node, true, $details);
flash_set('success', $message);
}
catch (RuntimeException | InvalidArgumentException $e) {
action_log('edit_'.$action, $node, false, $e->getMessage());
flash_set('danger', $e->getMessage());
}
redirect($back);
}
$doc = simplexml_load_string($xml);
$cdrom = $doc->xpath("/domain/devices/disk[@device='cdrom']")[0] ?? null;
$shared = storage_sources_in_use($con, $node);
$disks = [];
foreach (vm_disk_paths($xml) + vm_seed_paths($xml) as $target => $path) {
$disks[] = ['target' => $target, 'path' => $path, 'shared' => in_array($path, $shared, true)];
}
$smarty->assign('node', $node);
$smarty->assign('active', $active);
$smarty->assign('memory_mb', vm_memory_mb($xml));
$smarty->assign('current_memory_mb', vm_memory_mb($xml, 'currentMemory'));
$smarty->assign('cpu', vm_cpu_settings($xml));
$smarty->assign('vcpu', vm_vcpu_settings($xml));
$smarty->assign('cpu_modes', VM_CPU_MODES);
$smarty->assign('vcpus', (int)$doc->vcpu);
$smarty->assign('max_memory_mb', $max_memory_mb);
$smarty->assign('max_vcpus', $max_vcpus);
$smarty->assign('cdrom_iso', $cdrom ? (string)($cdrom->source['file'] ?? '') : null);
$smarty->assign('isos', $isos);
$smarty->assign('pools', $pools);
$smarty->assign('networks', $networks);
$smarty->assign('boot', vm_boot_order($xml));
$smarty->assign('disks', $disks);
// devices for the disk and network interface tables
$device_disks = [];
foreach (vm_disk_devices($xml) as $target => $d) {
if ($d['device'] !== 'disk') {
continue;
}
$capacity = disk_capacity($con, $res, $active, $target, $d['path']);
$iotune = vm_disk_iotune($xml, $target);
$device_disks[] = $d + ['target' => $target, 'file' => basename($d['path']),
'limit_mbs' => round($iotune['bytes'] / 1048576, 1), 'limit_iops' => $iotune['iops'], 'capacity' => $capacity !== null ? format_bytes($capacity) : '-',
'capacity_gb' => $capacity !== null ? (int)ceil($capacity / 1073741824) : 0,
'shared' => in_array($d['path'], $shared, true)];
}
$smarty->assign('device_disks', $device_disks);
$device_nics = vm_nic_devices($xml);
foreach ($device_nics as $mac => $nic) {
$bandwidth = vm_nic_bandwidth($xml, $mac);
$device_nics[$mac] += ['limit_in' => vm_kib_to_mbit($bandwidth['in']), 'limit_out' => vm_kib_to_mbit($bandwidth['out'])];
}
$smarty->assign('device_nics', $device_nics);
$smarty->assign('nic_models', NIC_MODELS);
$smarty->assign('disk_buses', DISK_BUSES);
$smarty->assign('disk_cache_modes', DISK_CACHE_MODES);
$smarty->assign('free_volumes', storage_free_volumes($con));
$smarty->assign('serial_port', vm_serial_port($xml));
$smarty->assign('hardware', vm_hardware($xml));
$smarty->assign('firmware', vm_firmware_settings($xml));
$xml_doc = simplexml_load_string($xml);
$smarty->assign('vm_title', $xml_doc ? trim((string)$xml_doc->title) : '');
$smarty->assign('vm_description', $xml_doc ? (string)$xml_doc->description : '');
$smarty->assign('firmwares', DOMAIN_FIRMWARES);
$smarty->assign('video_models', VM_VIDEO_MODELS);
$smarty->assign('sound_models', VM_SOUND_MODELS);
$smarty->assign('watchdog_actions', VM_WATCHDOG_ACTIONS);
$smarty->assign('can_delete', machine_allowed('delete', $node) && !$readonly);
// host devices: only administrators see and change them
if (can('admin')) {
$host_devices = hostdev_usb_list($con) + hostdev_pci_list($con);
$smarty->assign('host_devices', $host_devices);
$smarty->assign('vm_hostdevs', array_map(fn($key) => ['key' => $key, 'label' => $host_devices[$key]['label'] ?? t('(not present on the host)')],
array_keys(vm_hostdevs($xml))));
}
$smarty->display('edit.tpl');