1 package PVE
::API2
::Qemu
;
7 use PVE
::Cluster qw
(cfs_read_file cfs_write_file
);;
9 use PVE
::Tools
qw(extract_param);
10 use PVE
::Exception
qw(raise raise_param_exc);
12 use PVE
::JSONSchema
qw(get_standard_option);
16 use PVE
::RPCEnvironment
;
17 use PVE
::AccessControl
;
20 use Data
::Dumper
; # fixme: remove
22 use base
qw(PVE::RESTHandler);
24 my $opt_force_description = "Force physical removal. Without this, we simple remove the disk from the config file and create an additional configuration entry called 'unused[n]', which contains the volume ID. Unlink of unused[n] always cause physical removal.";
26 my $resolve_cdrom_alias = sub {
29 if (my $value = $param->{cdrom
}) {
30 $value .= ",media=cdrom" if $value !~ m/media=/;
31 $param->{ide2
} = $value;
32 delete $param->{cdrom
};
37 my $check_storage_access = sub {
38 my ($rpcenv, $authuser, $storecfg, $vmid, $settings, $default_storage) = @_;
40 PVE
::QemuServer
::foreach_drive
($settings, sub {
41 my ($ds, $drive) = @_;
43 my $isCDROM = PVE
::QemuServer
::drive_is_cdrom
($drive);
45 my $volid = $drive->{file
};
47 if (!$volid || $volid eq 'none') {
49 } elsif ($isCDROM && ($volid eq 'cdrom')) {
50 $rpcenv->check($authuser, "/", ['Sys.Console']);
51 } elsif (!$isCDROM && ($volid =~ m/^(([^:\s]+):)?(\d+(\.\d+)?)$/)) {
52 my ($storeid, $size) = ($2 || $default_storage, $3);
53 die "no storage ID specified (and no default storage)\n" if !$storeid;
54 $rpcenv->check($authuser, "/storage/$storeid", ['Datastore.AllocateSpace']);
56 $rpcenv->check_volume_access($authuser, $storecfg, $vmid, $volid);
61 # Note: $pool is only needed when creating a VM, because pool permissions
62 # are automatically inherited if VM already exists inside a pool.
63 my $create_disks = sub {
64 my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $pool, $settings, $default_storage) = @_;
69 PVE
::QemuServer
::foreach_drive
($settings, sub {
72 my $volid = $disk->{file
};
74 if (!$volid || $volid eq 'none' || $volid eq 'cdrom') {
75 $res->{$ds} = $settings->{$ds};
76 } elsif ($volid =~ m/^(([^:\s]+):)?(\d+(\.\d+)?)$/) {
77 my ($storeid, $size) = ($2 || $default_storage, $3);
78 die "no storage ID specified (and no default storage)\n" if !$storeid;
79 my $defformat = PVE
::Storage
::storage_default_format
($storecfg, $storeid);
80 my $fmt = $disk->{format
} || $defformat;
81 my $volid = PVE
::Storage
::vdisk_alloc
($storecfg, $storeid, $vmid,
82 $fmt, undef, $size*1024*1024);
83 $disk->{file
} = $volid;
84 $disk->{size
} = $size*1024*1024*1024;
85 push @$vollist, $volid;
86 delete $disk->{format
}; # no longer needed
87 $res->{$ds} = PVE
::QemuServer
::print_drive
($vmid, $disk);
90 my $path = $rpcenv->check_volume_access($authuser, $storecfg, $vmid, $volid);
92 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($volid, 1);
94 my $foundvolid = undef;
97 PVE
::Storage
::activate_volumes
($storecfg, [ $volid ]);
98 my $dl = PVE
::Storage
::vdisk_list
($storecfg, $storeid, undef);
100 PVE
::Storage
::foreach_volid
($dl, sub {
102 if($volumeid eq $volid) {
109 die "image '$path' does not exists\n" if (!(-f
$path || -b
$path || $foundvolid));
111 my ($size) = PVE
::Storage
::volume_size_info
($storecfg, $volid, 1);
112 $disk->{size
} = $size;
113 $res->{$ds} = PVE
::QemuServer
::print_drive
($vmid, $disk);
117 # free allocated images on error
119 syslog
('err', "VM $vmid creating disks failed");
120 foreach my $volid (@$vollist) {
121 eval { PVE
::Storage
::vdisk_free
($storecfg, $volid); };
127 # modify vm config if everything went well
128 foreach my $ds (keys %$res) {
129 $conf->{$ds} = $res->{$ds};
135 my $check_vm_modify_config_perm = sub {
136 my ($rpcenv, $authuser, $vmid, $pool, $key_list) = @_;
138 return 1 if $authuser eq 'root@pam';
140 foreach my $opt (@$key_list) {
141 # disk checks need to be done somewhere else
142 next if PVE
::QemuServer
::valid_drivename
($opt);
144 if ($opt eq 'sockets' || $opt eq 'cores' ||
145 $opt eq 'cpu' || $opt eq 'smp' ||
146 $opt eq 'cpulimit' || $opt eq 'cpuunits') {
147 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.CPU']);
148 } elsif ($opt eq 'boot' || $opt eq 'bootdisk') {
149 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Disk']);
150 } elsif ($opt eq 'memory' || $opt eq 'balloon') {
151 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Memory']);
152 } elsif ($opt eq 'args' || $opt eq 'lock') {
153 die "only root can set '$opt' config\n";
154 } elsif ($opt eq 'cpu' || $opt eq 'kvm' || $opt eq 'acpi' ||
155 $opt eq 'vga' || $opt eq 'watchdog' || $opt eq 'tablet') {
156 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.HWType']);
157 } elsif ($opt =~ m/^net\d+$/) {
158 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Network']);
160 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Options']);
167 __PACKAGE__-
>register_method({
171 description
=> "Virtual machine index (per node).",
173 description
=> "Only list VMs where you have VM.Audit permissons on /vms/<vmid>.",
177 protected
=> 1, # qemu pid files are only readable by root
179 additionalProperties
=> 0,
181 node
=> get_standard_option
('pve-node'),
190 links
=> [ { rel
=> 'child', href
=> "{vmid}" } ],
195 my $rpcenv = PVE
::RPCEnvironment
::get
();
196 my $authuser = $rpcenv->get_user();
198 my $vmstatus = PVE
::QemuServer
::vmstatus
();
201 foreach my $vmid (keys %$vmstatus) {
202 next if !$rpcenv->check($authuser, "/vms/$vmid", [ 'VM.Audit' ], 1);
204 my $data = $vmstatus->{$vmid};
205 $data->{vmid
} = $vmid;
212 __PACKAGE__-
>register_method({
216 description
=> "Create or restore a virtual machine.",
218 description
=> "You need 'VM.Allocate' permissions on /vms/{vmid} or on the VM pool /pool/{pool}. If you create disks you need 'Datastore.AllocateSpace' on any used storage.",
220 [ 'perm', '/vms/{vmid}', ['VM.Allocate']],
221 [ 'perm', '/pool/{pool}', ['VM.Allocate'], require_param
=> 'pool'],
227 additionalProperties
=> 0,
228 properties
=> PVE
::QemuServer
::json_config_properties
(
230 node
=> get_standard_option
('pve-node'),
231 vmid
=> get_standard_option
('pve-vmid'),
233 description
=> "The backup file.",
238 storage
=> get_standard_option
('pve-storage-id', {
239 description
=> "Default storage.",
245 description
=> "Allow to overwrite existing VM.",
246 requires
=> 'archive',
251 description
=> "Assign a unique random ethernet address.",
252 requires
=> 'archive',
256 type
=> 'string', format
=> 'pve-poolid',
257 description
=> "Add the VM to the specified pool.",
267 my $rpcenv = PVE
::RPCEnvironment
::get
();
269 my $authuser = $rpcenv->get_user();
271 my $node = extract_param
($param, 'node');
273 my $vmid = extract_param
($param, 'vmid');
275 my $archive = extract_param
($param, 'archive');
277 my $storage = extract_param
($param, 'storage');
279 my $force = extract_param
($param, 'force');
281 my $unique = extract_param
($param, 'unique');
283 my $pool = extract_param
($param, 'pool');
285 my $filename = PVE
::QemuServer
::config_file
($vmid);
287 my $storecfg = PVE
::Storage
::config
();
289 PVE
::Cluster
::check_cfs_quorum
();
291 if (defined($pool)) {
292 $rpcenv->check_pool_exist($pool);
295 $rpcenv->check($authuser, "/storage/$storage", ['Datastore.AllocateSpace'])
296 if defined($storage);
299 &$resolve_cdrom_alias($param);
301 &$check_storage_access($rpcenv, $authuser, $storecfg, $vmid, $param, $storage);
303 &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, $pool, [ keys %$param]);
305 foreach my $opt (keys %$param) {
306 if (PVE
::QemuServer
::valid_drivename
($opt)) {
307 my $drive = PVE
::QemuServer
::parse_drive
($opt, $param->{$opt});
308 raise_param_exc
({ $opt => "unable to parse drive options" }) if !$drive;
310 PVE
::QemuServer
::cleanup_drive_path
($opt, $storecfg, $drive);
311 $param->{$opt} = PVE
::QemuServer
::print_drive
($vmid, $drive);
315 PVE
::QemuServer
::add_random_macs
($param);
317 my $keystr = join(' ', keys %$param);
318 raise_param_exc
({ archive
=> "option conflicts with other options ($keystr)"}) if $keystr;
320 if ($archive eq '-') {
321 die "pipe requires cli environment\n"
322 if $rpcenv->{type
} ne 'cli';
324 my $path = $rpcenv->check_volume_access($authuser, $storecfg, $vmid, $archive);
326 PVE
::Storage
::activate_volumes
($storecfg, [ $archive ])
327 if PVE
::Storage
::parse_volume_id
($archive, 1);
329 die "can't find archive file '$archive'\n" if !($path && -f
$path);
334 my $addVMtoPoolFn = sub {
335 my $usercfg = cfs_read_file
("user.cfg");
336 if (my $data = $usercfg->{pools
}->{$pool}) {
337 $data->{vms
}->{$vmid} = 1;
338 $usercfg->{vms
}->{$vmid} = $pool;
339 cfs_write_file
("user.cfg", $usercfg);
343 my $restorefn = sub {
346 die "unable to restore vm $vmid: config file already exists\n"
349 die "unable to restore vm $vmid: vm is running\n"
350 if PVE
::QemuServer
::check_running
($vmid);
352 # destroy existing data - keep empty config
353 PVE
::QemuServer
::destroy_vm
($storecfg, $vmid, 1);
357 PVE
::QemuServer
::restore_archive
($archive, $vmid, $authuser, {
360 unique
=> $unique });
362 PVE
::AccessControl
::lock_user_config
($addVMtoPoolFn, "can't add VM to pool") if $pool;
365 return $rpcenv->fork_worker('qmrestore', $vmid, $authuser, $realcmd);
371 die "unable to create vm $vmid: config file already exists\n"
382 $vollist = &$create_disks($rpcenv, $authuser, $conf, $storecfg, $vmid, $pool, $param, $storage);
384 # try to be smart about bootdisk
385 my @disks = PVE
::QemuServer
::disknames
();
387 foreach my $ds (reverse @disks) {
388 next if !$conf->{$ds};
389 my $disk = PVE
::QemuServer
::parse_drive
($ds, $conf->{$ds});
390 next if PVE
::QemuServer
::drive_is_cdrom
($disk);
394 if (!$conf->{bootdisk
} && $firstdisk) {
395 $conf->{bootdisk
} = $firstdisk;
398 PVE
::QemuServer
::update_config_nolock
($vmid, $conf);
404 foreach my $volid (@$vollist) {
405 eval { PVE
::Storage
::vdisk_free
($storecfg, $volid); };
408 die "create failed - $err";
411 PVE
::AccessControl
::lock_user_config
($addVMtoPoolFn, "can't add VM to pool") if $pool;
414 return $rpcenv->fork_worker('qmcreate', $vmid, $authuser, $realcmd);
417 return PVE
::QemuServer
::lock_config_full
($vmid, 1, $archive ?
$restorefn : $createfn);
420 __PACKAGE__-
>register_method({
425 description
=> "Directory index",
430 additionalProperties
=> 0,
432 node
=> get_standard_option
('pve-node'),
433 vmid
=> get_standard_option
('pve-vmid'),
441 subdir
=> { type
=> 'string' },
444 links
=> [ { rel
=> 'child', href
=> "{subdir}" } ],
450 { subdir
=> 'config' },
451 { subdir
=> 'status' },
452 { subdir
=> 'unlink' },
453 { subdir
=> 'vncproxy' },
454 { subdir
=> 'migrate' },
456 { subdir
=> 'rrddata' },
457 { subdir
=> 'monitor' },
463 __PACKAGE__-
>register_method({
465 path
=> '{vmid}/rrd',
467 protected
=> 1, # fixme: can we avoid that?
469 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
471 description
=> "Read VM RRD statistics (returns PNG)",
473 additionalProperties
=> 0,
475 node
=> get_standard_option
('pve-node'),
476 vmid
=> get_standard_option
('pve-vmid'),
478 description
=> "Specify the time frame you are interested in.",
480 enum
=> [ 'hour', 'day', 'week', 'month', 'year' ],
483 description
=> "The list of datasources you want to display.",
484 type
=> 'string', format
=> 'pve-configid-list',
487 description
=> "The RRD consolidation function",
489 enum
=> [ 'AVERAGE', 'MAX' ],
497 filename
=> { type
=> 'string' },
503 return PVE
::Cluster
::create_rrd_graph
(
504 "pve2-vm/$param->{vmid}", $param->{timeframe
},
505 $param->{ds
}, $param->{cf
});
509 __PACKAGE__-
>register_method({
511 path
=> '{vmid}/rrddata',
513 protected
=> 1, # fixme: can we avoid that?
515 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
517 description
=> "Read VM RRD statistics",
519 additionalProperties
=> 0,
521 node
=> get_standard_option
('pve-node'),
522 vmid
=> get_standard_option
('pve-vmid'),
524 description
=> "Specify the time frame you are interested in.",
526 enum
=> [ 'hour', 'day', 'week', 'month', 'year' ],
529 description
=> "The RRD consolidation function",
531 enum
=> [ 'AVERAGE', 'MAX' ],
546 return PVE
::Cluster
::create_rrd_data
(
547 "pve2-vm/$param->{vmid}", $param->{timeframe
}, $param->{cf
});
551 __PACKAGE__-
>register_method({
553 path
=> '{vmid}/config',
556 description
=> "Get virtual machine configuration.",
558 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
561 additionalProperties
=> 0,
563 node
=> get_standard_option
('pve-node'),
564 vmid
=> get_standard_option
('pve-vmid'),
572 description
=> 'SHA1 digest of configuration file. This can be used to prevent concurrent modifications.',
579 my $conf = PVE
::QemuServer
::load_config
($param->{vmid
});
584 my $vm_is_volid_owner = sub {
585 my ($storecfg, $vmid, $volid) =@_;
587 if ($volid !~ m
|^/|) {
589 eval { ($path, $owner) = PVE
::Storage
::path
($storecfg, $volid); };
590 if ($owner && ($owner == $vmid)) {
598 my $test_deallocate_drive = sub {
599 my ($storecfg, $vmid, $key, $drive, $force) = @_;
601 if (!PVE
::QemuServer
::drive_is_cdrom
($drive)) {
602 my $volid = $drive->{file
};
603 if (&$vm_is_volid_owner($storecfg, $vmid, $volid)) {
604 if ($force || $key =~ m/^unused/) {
605 my $sid = PVE
::Storage
::parse_volume_id
($volid);
614 my $delete_drive = sub {
615 my ($conf, $storecfg, $vmid, $key, $drive, $force) = @_;
617 if (!PVE
::QemuServer
::drive_is_cdrom
($drive)) {
618 my $volid = $drive->{file
};
619 if (&$vm_is_volid_owner($storecfg, $vmid, $volid)) {
620 if ($force || $key =~ m/^unused/) {
621 eval { PVE
::Storage
::vdisk_free
($storecfg, $volid); };
624 PVE
::QemuServer
::add_unused_volume
($conf, $volid, $vmid);
629 delete $conf->{$key};
632 my $vmconfig_delete_option = sub {
633 my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $force) = @_;
635 return if !defined($conf->{$opt});
637 my $isDisk = PVE
::QemuServer
::valid_drivename
($opt)|| ($opt =~ m/^unused/);
640 $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']);
642 my $drive = PVE
::QemuServer
::parse_drive
($opt, $conf->{$opt});
643 if (my $sid = &$test_deallocate_drive($storecfg, $vmid, $opt, $drive, $force)) {
644 $rpcenv->check($authuser, "/storage/$sid", ['Datastore.Allocate']);
648 die "error hot-unplug $opt" if !PVE
::QemuServer
::vm_deviceunplug
($vmid, $conf, $opt);
651 my $drive = PVE
::QemuServer
::parse_drive
($opt, $conf->{$opt});
652 &$delete_drive($conf, $storecfg, $vmid, $opt, $drive, $force);
654 delete $conf->{$opt};
657 PVE
::QemuServer
::update_config_nolock
($vmid, $conf, 1);
660 my $safe_int_ne = sub {
663 return 0 if !defined($a) && !defined($b);
664 return 1 if !defined($a);
665 return 1 if !defined($b);
670 my $vmconfig_update_disk = sub {
671 my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $value, $force) = @_;
673 my $drive = PVE
::QemuServer
::parse_drive
($opt, $value);
675 if (PVE
::QemuServer
::drive_is_cdrom
($drive)) { #cdrom
676 $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.CDROM']);
678 $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']);
683 if (my $old_drive = PVE
::QemuServer
::parse_drive
($opt, $conf->{$opt})) {
685 my $media = $drive->{media
} || 'disk';
686 my $oldmedia = $old_drive->{media
} || 'disk';
687 die "unable to change media type\n" if $media ne $oldmedia;
689 if (!PVE
::QemuServer
::drive_is_cdrom
($old_drive) &&
690 ($drive->{file
} ne $old_drive->{file
})) { # delete old disks
692 &$vmconfig_delete_option($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $force);
693 $conf = PVE
::QemuServer
::load_config
($vmid); # update/reload
696 if(&$safe_int_ne($drive->{bps
}, $old_drive->{bps
}) ||
697 &$safe_int_ne($drive->{bps_rd
}, $old_drive->{bps_rd
}) ||
698 &$safe_int_ne($drive->{bps_wr
}, $old_drive->{bps_wr
}) ||
699 &$safe_int_ne($drive->{iops
}, $old_drive->{iops
}) ||
700 &$safe_int_ne($drive->{iops_rd
}, $old_drive->{iops_rd
}) ||
701 &$safe_int_ne($drive->{iops_wr
}, $old_drive->{iops_wr
})) {
702 PVE
::QemuServer
::qemu_block_set_io_throttle
($vmid,"drive-$opt",$drive->{bps
}, $drive->{bps_rd
}, $drive->{bps_wr
}, $drive->{iops
}, $drive->{iops_rd
}, $drive->{iops_wr
}) if !PVE
::QemuServer
::drive_is_cdrom
($drive);
707 &$create_disks($rpcenv, $authuser, $conf, $storecfg, $vmid, undef, {$opt => $value});
708 PVE
::QemuServer
::update_config_nolock
($vmid, $conf, 1);
710 $conf = PVE
::QemuServer
::load_config
($vmid); # update/reload
711 $drive = PVE
::QemuServer
::parse_drive
($opt, $conf->{$opt});
713 if (PVE
::QemuServer
::drive_is_cdrom
($drive)) { # cdrom
715 if (PVE
::QemuServer
::check_running
($vmid)) {
716 if ($drive->{file
} eq 'none') {
717 PVE
::QemuServer
::vm_mon_cmd
($vmid, "eject",force
=> JSON
::true
,device
=> "drive-$opt");
719 my $path = PVE
::QemuServer
::get_iso_path
($storecfg, $vmid, $drive->{file
});
720 PVE
::QemuServer
::vm_mon_cmd
($vmid, "eject",force
=> JSON
::true
,device
=> "drive-$opt"); #force eject if locked
721 PVE
::QemuServer
::vm_mon_cmd
($vmid, "change",device
=> "drive-$opt",target
=> "$path") if $path;
725 } else { # hotplug new disks
727 die "error hotplug $opt" if !PVE
::QemuServer
::vm_deviceplug
($storecfg, $conf, $vmid, $opt, $drive);
731 my $vmconfig_update_net = sub {
732 my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $value) = @_;
735 #if online update, then unplug first
736 die "error hot-unplug $opt for update" if !PVE
::QemuServer
::vm_deviceunplug
($vmid, $conf, $opt);
739 $conf->{$opt} = $value;
740 PVE
::QemuServer
::update_config_nolock
($vmid, $conf, 1);
741 $conf = PVE
::QemuServer
::load_config
($vmid); # update/reload
743 my $net = PVE
::QemuServer
::parse_net
($conf->{$opt});
745 die "error hotplug $opt" if !PVE
::QemuServer
::vm_deviceplug
($storecfg, $conf, $vmid, $opt, $net);
748 my $vm_config_perm_list = [
758 __PACKAGE__-
>register_method({
760 path
=> '{vmid}/config',
764 description
=> "Set virtual machine options.",
766 check
=> ['perm', '/vms/{vmid}', $vm_config_perm_list, any
=> 1],
769 additionalProperties
=> 0,
770 properties
=> PVE
::QemuServer
::json_config_properties
(
772 node
=> get_standard_option
('pve-node'),
773 vmid
=> get_standard_option
('pve-vmid'),
774 skiplock
=> get_standard_option
('skiplock'),
776 type
=> 'string', format
=> 'pve-configid-list',
777 description
=> "A list of settings you want to delete.",
782 description
=> $opt_force_description,
784 requires
=> 'delete',
788 description
=> 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.',
794 returns
=> { type
=> 'null'},
798 my $rpcenv = PVE
::RPCEnvironment
::get
();
800 my $authuser = $rpcenv->get_user();
802 my $node = extract_param
($param, 'node');
804 my $vmid = extract_param
($param, 'vmid');
806 my $digest = extract_param
($param, 'digest');
808 my @paramarr = (); # used for log message
809 foreach my $key (keys %$param) {
810 push @paramarr, "-$key", $param->{$key};
813 my $skiplock = extract_param
($param, 'skiplock');
814 raise_param_exc
({ skiplock
=> "Only root may use this option." })
815 if $skiplock && $authuser ne 'root@pam';
817 my $delete_str = extract_param
($param, 'delete');
819 my $force = extract_param
($param, 'force');
821 die "no options specified\n" if !$delete_str && !scalar(keys %$param);
823 my $storecfg = PVE
::Storage
::config
();
825 &$resolve_cdrom_alias($param);
827 # now try to verify all parameters
830 foreach my $opt (PVE
::Tools
::split_list
($delete_str)) {
831 $opt = 'ide2' if $opt eq 'cdrom';
832 raise_param_exc
({ delete => "you can't use '-$opt' and " .
833 "-delete $opt' at the same time" })
834 if defined($param->{$opt});
836 if (!PVE
::QemuServer
::option_exists
($opt)) {
837 raise_param_exc
({ delete => "unknown option '$opt'" });
843 foreach my $opt (keys %$param) {
844 if (PVE
::QemuServer
::valid_drivename
($opt)) {
846 my $drive = PVE
::QemuServer
::parse_drive
($opt, $param->{$opt});
847 PVE
::QemuServer
::cleanup_drive_path
($opt, $storecfg, $drive);
848 $param->{$opt} = PVE
::QemuServer
::print_drive
($vmid, $drive);
849 } elsif ($opt =~ m/^net(\d+)$/) {
851 my $net = PVE
::QemuServer
::parse_net
($param->{$opt});
852 $param->{$opt} = PVE
::QemuServer
::print_net
($net);
856 &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, undef, [@delete]);
858 &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, undef, [keys %$param]);
860 &$check_storage_access($rpcenv, $authuser, $storecfg, $vmid, $param);
864 my $conf = PVE
::QemuServer
::load_config
($vmid);
866 die "checksum missmatch (file change by other user?)\n"
867 if $digest && $digest ne $conf->{digest
};
869 PVE
::QemuServer
::check_lock
($conf) if !$skiplock;
871 PVE
::Cluster
::log_msg
('info', $authuser, "update VM $vmid: " . join (' ', @paramarr));
873 foreach my $opt (@delete) { # delete
874 $conf = PVE
::QemuServer
::load_config
($vmid); # update/reload
875 &$vmconfig_delete_option($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $force);
878 foreach my $opt (keys %$param) { # add/change
880 $conf = PVE
::QemuServer
::load_config
($vmid); # update/reload
882 next if $conf->{$opt} && ($param->{$opt} eq $conf->{$opt}); # skip if nothing changed
884 if (PVE
::QemuServer
::valid_drivename
($opt)) {
886 &$vmconfig_update_disk($rpcenv, $authuser, $conf, $storecfg, $vmid,
887 $opt, $param->{$opt}, $force);
889 } elsif ($opt =~ m/^net(\d+)$/) { #nics
891 &$vmconfig_update_net($rpcenv, $authuser, $conf, $storecfg, $vmid,
892 $opt, $param->{$opt});
896 $conf->{$opt} = $param->{$opt};
897 PVE
::QemuServer
::update_config_nolock
($vmid, $conf, 1);
902 PVE
::QemuServer
::lock_config
($vmid, $updatefn);
908 __PACKAGE__-
>register_method({
909 name
=> 'destroy_vm',
914 description
=> "Destroy the vm (also delete all used/owned volumes).",
916 check
=> [ 'perm', '/vms/{vmid}', ['VM.Allocate']],
919 additionalProperties
=> 0,
921 node
=> get_standard_option
('pve-node'),
922 vmid
=> get_standard_option
('pve-vmid'),
923 skiplock
=> get_standard_option
('skiplock'),
932 my $rpcenv = PVE
::RPCEnvironment
::get
();
934 my $authuser = $rpcenv->get_user();
936 my $vmid = $param->{vmid
};
938 my $skiplock = $param->{skiplock
};
939 raise_param_exc
({ skiplock
=> "Only root may use this option." })
940 if $skiplock && $authuser ne 'root@pam';
943 my $conf = PVE
::QemuServer
::load_config
($vmid);
945 my $storecfg = PVE
::Storage
::config
();
947 my $delVMfromPoolFn = sub {
948 my $usercfg = cfs_read_file
("user.cfg");
949 if (my $pool = $usercfg->{vms
}->{$vmid}) {
950 if (my $data = $usercfg->{pools
}->{$pool}) {
951 delete $data->{vms
}->{$vmid};
952 delete $usercfg->{vms
}->{$vmid};
953 cfs_write_file
("user.cfg", $usercfg);
961 syslog
('info', "destroy VM $vmid: $upid\n");
963 PVE
::QemuServer
::vm_destroy
($storecfg, $vmid, $skiplock);
965 PVE
::AccessControl
::lock_user_config
($delVMfromPoolFn, "pool cleanup failed");
968 return $rpcenv->fork_worker('qmdestroy', $vmid, $authuser, $realcmd);
971 __PACKAGE__-
>register_method({
973 path
=> '{vmid}/unlink',
977 description
=> "Unlink/delete disk images.",
979 check
=> [ 'perm', '/vms/{vmid}', ['VM.Config.Disk']],
982 additionalProperties
=> 0,
984 node
=> get_standard_option
('pve-node'),
985 vmid
=> get_standard_option
('pve-vmid'),
987 type
=> 'string', format
=> 'pve-configid-list',
988 description
=> "A list of disk IDs you want to delete.",
992 description
=> $opt_force_description,
997 returns
=> { type
=> 'null'},
1001 $param->{delete} = extract_param
($param, 'idlist');
1003 __PACKAGE__-
>update_vm($param);
1010 __PACKAGE__-
>register_method({
1012 path
=> '{vmid}/vncproxy',
1016 check
=> ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
1018 description
=> "Creates a TCP VNC proxy connections.",
1020 additionalProperties
=> 0,
1022 node
=> get_standard_option
('pve-node'),
1023 vmid
=> get_standard_option
('pve-vmid'),
1027 additionalProperties
=> 0,
1029 user
=> { type
=> 'string' },
1030 ticket
=> { type
=> 'string' },
1031 cert
=> { type
=> 'string' },
1032 port
=> { type
=> 'integer' },
1033 upid
=> { type
=> 'string' },
1039 my $rpcenv = PVE
::RPCEnvironment
::get
();
1041 my $authuser = $rpcenv->get_user();
1043 my $vmid = $param->{vmid
};
1044 my $node = $param->{node
};
1046 my $authpath = "/vms/$vmid";
1048 my $ticket = PVE
::AccessControl
::assemble_vnc_ticket
($authuser, $authpath);
1050 $sslcert = PVE
::Tools
::file_get_contents
("/etc/pve/pve-root-ca.pem", 8192)
1053 my $port = PVE
::Tools
::next_vnc_port
();
1057 if ($node ne 'localhost' && $node ne PVE
::INotify
::nodename
()) {
1058 $remip = PVE
::Cluster
::remote_node_ip
($node);
1061 # NOTE: kvm VNC traffic is already TLS encrypted,
1062 # so we select the fastest chipher here (or 'none'?)
1063 my $remcmd = $remip ?
['/usr/bin/ssh', '-T', '-o', 'BatchMode=yes',
1064 '-c', 'blowfish-cbc', $remip] : [];
1071 syslog
('info', "starting vnc proxy $upid\n");
1073 my $qmcmd = [@$remcmd, "/usr/sbin/qm", 'vncproxy', $vmid];
1075 my $qmstr = join(' ', @$qmcmd);
1077 # also redirect stderr (else we get RFB protocol errors)
1078 my $cmd = ['/bin/nc', '-l', '-p', $port, '-w', $timeout, '-c', "$qmstr 2>/dev/null"];
1080 PVE
::Tools
::run_command
($cmd);
1085 my $upid = $rpcenv->fork_worker('vncproxy', $vmid, $authuser, $realcmd);
1096 __PACKAGE__-
>register_method({
1098 path
=> '{vmid}/status',
1101 description
=> "Directory index",
1106 additionalProperties
=> 0,
1108 node
=> get_standard_option
('pve-node'),
1109 vmid
=> get_standard_option
('pve-vmid'),
1117 subdir
=> { type
=> 'string' },
1120 links
=> [ { rel
=> 'child', href
=> "{subdir}" } ],
1126 my $conf = PVE
::QemuServer
::load_config
($param->{vmid
});
1129 { subdir
=> 'current' },
1130 { subdir
=> 'start' },
1131 { subdir
=> 'stop' },
1137 my $vm_is_ha_managed = sub {
1140 my $cc = PVE
::Cluster
::cfs_read_file
('cluster.conf');
1141 if (PVE
::Cluster
::cluster_conf_lookup_pvevm
($cc, 0, $vmid, 1)) {
1147 __PACKAGE__-
>register_method({
1148 name
=> 'vm_status',
1149 path
=> '{vmid}/status/current',
1152 protected
=> 1, # qemu pid files are only readable by root
1153 description
=> "Get virtual machine status.",
1155 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
1158 additionalProperties
=> 0,
1160 node
=> get_standard_option
('pve-node'),
1161 vmid
=> get_standard_option
('pve-vmid'),
1164 returns
=> { type
=> 'object' },
1169 my $conf = PVE
::QemuServer
::load_config
($param->{vmid
});
1171 my $vmstatus = PVE
::QemuServer
::vmstatus
($param->{vmid
}, 1);
1172 my $status = $vmstatus->{$param->{vmid
}};
1174 $status->{ha
} = &$vm_is_ha_managed($param->{vmid
});
1179 __PACKAGE__-
>register_method({
1181 path
=> '{vmid}/status/start',
1185 description
=> "Start virtual machine.",
1187 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1190 additionalProperties
=> 0,
1192 node
=> get_standard_option
('pve-node'),
1193 vmid
=> get_standard_option
('pve-vmid'),
1194 skiplock
=> get_standard_option
('skiplock'),
1195 stateuri
=> get_standard_option
('pve-qm-stateuri'),
1204 my $rpcenv = PVE
::RPCEnvironment
::get
();
1206 my $authuser = $rpcenv->get_user();
1208 my $node = extract_param
($param, 'node');
1210 my $vmid = extract_param
($param, 'vmid');
1212 my $stateuri = extract_param
($param, 'stateuri');
1213 raise_param_exc
({ stateuri
=> "Only root may use this option." })
1214 if $stateuri && $authuser ne 'root@pam';
1216 my $skiplock = extract_param
($param, 'skiplock');
1217 raise_param_exc
({ skiplock
=> "Only root may use this option." })
1218 if $skiplock && $authuser ne 'root@pam';
1220 my $storecfg = PVE
::Storage
::config
();
1222 if (&$vm_is_ha_managed($vmid) && !$stateuri &&
1223 $rpcenv->{type
} ne 'ha') {
1228 my $service = "pvevm:$vmid";
1230 my $cmd = ['clusvcadm', '-e', $service, '-m', $node];
1232 print "Executing HA start for VM $vmid\n";
1234 PVE
::Tools
::run_command
($cmd);
1239 return $rpcenv->fork_worker('hastart', $vmid, $authuser, $hacmd);
1246 syslog
('info', "start VM $vmid: $upid\n");
1248 PVE
::QemuServer
::vm_start
($storecfg, $vmid, $stateuri, $skiplock);
1253 return $rpcenv->fork_worker('qmstart', $vmid, $authuser, $realcmd);
1257 __PACKAGE__-
>register_method({
1259 path
=> '{vmid}/status/stop',
1263 description
=> "Stop virtual machine.",
1265 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1268 additionalProperties
=> 0,
1270 node
=> get_standard_option
('pve-node'),
1271 vmid
=> get_standard_option
('pve-vmid'),
1272 skiplock
=> get_standard_option
('skiplock'),
1274 description
=> "Wait maximal timeout seconds.",
1280 description
=> "Do not decativate storage volumes.",
1293 my $rpcenv = PVE
::RPCEnvironment
::get
();
1295 my $authuser = $rpcenv->get_user();
1297 my $node = extract_param
($param, 'node');
1299 my $vmid = extract_param
($param, 'vmid');
1301 my $skiplock = extract_param
($param, 'skiplock');
1302 raise_param_exc
({ skiplock
=> "Only root may use this option." })
1303 if $skiplock && $authuser ne 'root@pam';
1305 my $keepActive = extract_param
($param, 'keepActive');
1306 raise_param_exc
({ keepActive
=> "Only root may use this option." })
1307 if $keepActive && $authuser ne 'root@pam';
1309 my $storecfg = PVE
::Storage
::config
();
1311 if (&$vm_is_ha_managed($vmid) && $rpcenv->{type
} ne 'ha') {
1316 my $service = "pvevm:$vmid";
1318 my $cmd = ['clusvcadm', '-d', $service];
1320 print "Executing HA stop for VM $vmid\n";
1322 PVE
::Tools
::run_command
($cmd);
1327 return $rpcenv->fork_worker('hastop', $vmid, $authuser, $hacmd);
1333 syslog
('info', "stop VM $vmid: $upid\n");
1335 PVE
::QemuServer
::vm_stop
($storecfg, $vmid, $skiplock, 0,
1336 $param->{timeout
}, 0, 1, $keepActive);
1341 return $rpcenv->fork_worker('qmstop', $vmid, $authuser, $realcmd);
1345 __PACKAGE__-
>register_method({
1347 path
=> '{vmid}/status/reset',
1351 description
=> "Reset virtual machine.",
1353 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1356 additionalProperties
=> 0,
1358 node
=> get_standard_option
('pve-node'),
1359 vmid
=> get_standard_option
('pve-vmid'),
1360 skiplock
=> get_standard_option
('skiplock'),
1369 my $rpcenv = PVE
::RPCEnvironment
::get
();
1371 my $authuser = $rpcenv->get_user();
1373 my $node = extract_param
($param, 'node');
1375 my $vmid = extract_param
($param, 'vmid');
1377 my $skiplock = extract_param
($param, 'skiplock');
1378 raise_param_exc
({ skiplock
=> "Only root may use this option." })
1379 if $skiplock && $authuser ne 'root@pam';
1381 die "VM $vmid not running\n" if !PVE
::QemuServer
::check_running
($vmid);
1386 PVE
::QemuServer
::vm_reset
($vmid, $skiplock);
1391 return $rpcenv->fork_worker('qmreset', $vmid, $authuser, $realcmd);
1394 __PACKAGE__-
>register_method({
1395 name
=> 'vm_shutdown',
1396 path
=> '{vmid}/status/shutdown',
1400 description
=> "Shutdown virtual machine.",
1402 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1405 additionalProperties
=> 0,
1407 node
=> get_standard_option
('pve-node'),
1408 vmid
=> get_standard_option
('pve-vmid'),
1409 skiplock
=> get_standard_option
('skiplock'),
1411 description
=> "Wait maximal timeout seconds.",
1417 description
=> "Make sure the VM stops.",
1423 description
=> "Do not decativate storage volumes.",
1436 my $rpcenv = PVE
::RPCEnvironment
::get
();
1438 my $authuser = $rpcenv->get_user();
1440 my $node = extract_param
($param, 'node');
1442 my $vmid = extract_param
($param, 'vmid');
1444 my $skiplock = extract_param
($param, 'skiplock');
1445 raise_param_exc
({ skiplock
=> "Only root may use this option." })
1446 if $skiplock && $authuser ne 'root@pam';
1448 my $keepActive = extract_param
($param, 'keepActive');
1449 raise_param_exc
({ keepActive
=> "Only root may use this option." })
1450 if $keepActive && $authuser ne 'root@pam';
1452 my $storecfg = PVE
::Storage
::config
();
1457 syslog
('info', "shutdown VM $vmid: $upid\n");
1459 PVE
::QemuServer
::vm_stop
($storecfg, $vmid, $skiplock, 0, $param->{timeout
},
1460 1, $param->{forceStop
}, $keepActive);
1465 return $rpcenv->fork_worker('qmshutdown', $vmid, $authuser, $realcmd);
1468 __PACKAGE__-
>register_method({
1469 name
=> 'vm_suspend',
1470 path
=> '{vmid}/status/suspend',
1474 description
=> "Suspend virtual machine.",
1476 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1479 additionalProperties
=> 0,
1481 node
=> get_standard_option
('pve-node'),
1482 vmid
=> get_standard_option
('pve-vmid'),
1483 skiplock
=> get_standard_option
('skiplock'),
1492 my $rpcenv = PVE
::RPCEnvironment
::get
();
1494 my $authuser = $rpcenv->get_user();
1496 my $node = extract_param
($param, 'node');
1498 my $vmid = extract_param
($param, 'vmid');
1500 my $skiplock = extract_param
($param, 'skiplock');
1501 raise_param_exc
({ skiplock
=> "Only root may use this option." })
1502 if $skiplock && $authuser ne 'root@pam';
1504 die "VM $vmid not running\n" if !PVE
::QemuServer
::check_running
($vmid);
1509 syslog
('info', "suspend VM $vmid: $upid\n");
1511 PVE
::QemuServer
::vm_suspend
($vmid, $skiplock);
1516 return $rpcenv->fork_worker('qmsuspend', $vmid, $authuser, $realcmd);
1519 __PACKAGE__-
>register_method({
1520 name
=> 'vm_resume',
1521 path
=> '{vmid}/status/resume',
1525 description
=> "Resume virtual machine.",
1527 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1530 additionalProperties
=> 0,
1532 node
=> get_standard_option
('pve-node'),
1533 vmid
=> get_standard_option
('pve-vmid'),
1534 skiplock
=> get_standard_option
('skiplock'),
1543 my $rpcenv = PVE
::RPCEnvironment
::get
();
1545 my $authuser = $rpcenv->get_user();
1547 my $node = extract_param
($param, 'node');
1549 my $vmid = extract_param
($param, 'vmid');
1551 my $skiplock = extract_param
($param, 'skiplock');
1552 raise_param_exc
({ skiplock
=> "Only root may use this option." })
1553 if $skiplock && $authuser ne 'root@pam';
1555 die "VM $vmid not running\n" if !PVE
::QemuServer
::check_running
($vmid);
1560 syslog
('info', "resume VM $vmid: $upid\n");
1562 PVE
::QemuServer
::vm_resume
($vmid, $skiplock);
1567 return $rpcenv->fork_worker('qmresume', $vmid, $authuser, $realcmd);
1570 __PACKAGE__-
>register_method({
1571 name
=> 'vm_sendkey',
1572 path
=> '{vmid}/sendkey',
1576 description
=> "Send key event to virtual machine.",
1578 check
=> ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
1581 additionalProperties
=> 0,
1583 node
=> get_standard_option
('pve-node'),
1584 vmid
=> get_standard_option
('pve-vmid'),
1585 skiplock
=> get_standard_option
('skiplock'),
1587 description
=> "The key (qemu monitor encoding).",
1592 returns
=> { type
=> 'null'},
1596 my $rpcenv = PVE
::RPCEnvironment
::get
();
1598 my $authuser = $rpcenv->get_user();
1600 my $node = extract_param
($param, 'node');
1602 my $vmid = extract_param
($param, 'vmid');
1604 my $skiplock = extract_param
($param, 'skiplock');
1605 raise_param_exc
({ skiplock
=> "Only root may use this option." })
1606 if $skiplock && $authuser ne 'root@pam';
1608 PVE
::QemuServer
::vm_sendkey
($vmid, $skiplock, $param->{key
});
1613 __PACKAGE__-
>register_method({
1614 name
=> 'migrate_vm',
1615 path
=> '{vmid}/migrate',
1619 description
=> "Migrate virtual machine. Creates a new migration task.",
1621 check
=> ['perm', '/vms/{vmid}', [ 'VM.Migrate' ]],
1624 additionalProperties
=> 0,
1626 node
=> get_standard_option
('pve-node'),
1627 vmid
=> get_standard_option
('pve-vmid'),
1628 target
=> get_standard_option
('pve-node', { description
=> "Target node." }),
1631 description
=> "Use online/live migration.",
1636 description
=> "Allow to migrate VMs which use local devices. Only root may use this option.",
1643 description
=> "the task ID.",
1648 my $rpcenv = PVE
::RPCEnvironment
::get
();
1650 my $authuser = $rpcenv->get_user();
1652 my $target = extract_param
($param, 'target');
1654 my $localnode = PVE
::INotify
::nodename
();
1655 raise_param_exc
({ target
=> "target is local node."}) if $target eq $localnode;
1657 PVE
::Cluster
::check_cfs_quorum
();
1659 PVE
::Cluster
::check_node_exists
($target);
1661 my $targetip = PVE
::Cluster
::remote_node_ip
($target);
1663 my $vmid = extract_param
($param, 'vmid');
1665 raise_param_exc
({ force
=> "Only root may use this option." })
1666 if $param->{force
} && $authuser ne 'root@pam';
1669 my $conf = PVE
::QemuServer
::load_config
($vmid);
1671 # try to detect errors early
1673 PVE
::QemuServer
::check_lock
($conf);
1675 if (PVE
::QemuServer
::check_running
($vmid)) {
1676 die "cant migrate running VM without --online\n"
1677 if !$param->{online
};
1680 my $storecfg = PVE
::Storage
::config
();
1681 PVE
::QemuServer
::check_storage_availability
($storecfg, $conf, $target);
1683 if (&$vm_is_ha_managed($vmid) && $rpcenv->{type
} ne 'ha') {
1688 my $service = "pvevm:$vmid";
1690 my $cmd = ['clusvcadm', '-M', $service, '-m', $target];
1692 print "Executing HA migrate for VM $vmid to node $target\n";
1694 PVE
::Tools
::run_command
($cmd);
1699 return $rpcenv->fork_worker('hamigrate', $vmid, $authuser, $hacmd);
1706 PVE
::QemuMigrate-
>migrate($target, $targetip, $vmid, $param);
1709 return $rpcenv->fork_worker('qmigrate', $vmid, $authuser, $realcmd);
1714 __PACKAGE__-
>register_method({
1716 path
=> '{vmid}/monitor',
1720 description
=> "Execute Qemu monitor commands.",
1722 check
=> ['perm', '/vms/{vmid}', [ 'VM.Monitor' ]],
1725 additionalProperties
=> 0,
1727 node
=> get_standard_option
('pve-node'),
1728 vmid
=> get_standard_option
('pve-vmid'),
1731 description
=> "The monitor command.",
1735 returns
=> { type
=> 'string'},
1739 my $vmid = $param->{vmid
};
1741 my $conf = PVE
::QemuServer
::load_config
($vmid); # check if VM exists
1745 $res = PVE
::QemuServer
::vm_human_monitor_command
($vmid, $param->{command
});
1747 $res = "ERROR: $@" if $@;