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 push @$vollist, $volid;
85 delete $disk->{format
}; # no longer needed
86 $res->{$ds} = PVE
::QemuServer
::print_drive
($vmid, $disk);
89 my $path = $rpcenv->check_volume_access($authuser, $storecfg, $vmid, $volid);
91 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($volid, 1);
93 my $foundvolid = undef;
96 PVE
::Storage
::activate_volumes
($storecfg, [ $volid ]);
97 my $dl = PVE
::Storage
::vdisk_list
($storecfg, $storeid, undef);
99 PVE
::Storage
::foreach_volid
($dl, sub {
101 if($volumeid eq $volid) {
108 die "image '$path' does not exists\n" if (!(-f
$path || -b
$path || $foundvolid));
109 $res->{$ds} = $settings->{$ds};
113 # free allocated images on error
115 syslog
('err', "VM $vmid creating disks failed");
116 foreach my $volid (@$vollist) {
117 eval { PVE
::Storage
::vdisk_free
($storecfg, $volid); };
123 # modify vm config if everything went well
124 foreach my $ds (keys %$res) {
125 $conf->{$ds} = $res->{$ds};
131 my $check_vm_modify_config_perm = sub {
132 my ($rpcenv, $authuser, $vmid, $pool, $key_list) = @_;
134 return 1 if $authuser eq 'root@pam';
136 foreach my $opt (@$key_list) {
137 # disk checks need to be done somewhere else
138 next if PVE
::QemuServer
::valid_drivename
($opt);
140 if ($opt eq 'sockets' || $opt eq 'cores' ||
141 $opt eq 'cpu' || $opt eq 'smp' ||
142 $opt eq 'cpulimit' || $opt eq 'cpuunits') {
143 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.CPU']);
144 } elsif ($opt eq 'boot' || $opt eq 'bootdisk') {
145 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Disk']);
146 } elsif ($opt eq 'memory' || $opt eq 'balloon') {
147 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Memory']);
148 } elsif ($opt eq 'args' || $opt eq 'lock') {
149 die "only root can set '$opt' config\n";
150 } elsif ($opt eq 'cpu' || $opt eq 'kvm' || $opt eq 'acpi' ||
151 $opt eq 'vga' || $opt eq 'watchdog' || $opt eq 'tablet') {
152 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.HWType']);
153 } elsif ($opt =~ m/^net\d+$/) {
154 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Network']);
156 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Options']);
163 __PACKAGE__-
>register_method({
167 description
=> "Virtual machine index (per node).",
169 description
=> "Only list VMs where you have VM.Audit permissons on /vms/<vmid>.",
173 protected
=> 1, # qemu pid files are only readable by root
175 additionalProperties
=> 0,
177 node
=> get_standard_option
('pve-node'),
186 links
=> [ { rel
=> 'child', href
=> "{vmid}" } ],
191 my $rpcenv = PVE
::RPCEnvironment
::get
();
192 my $authuser = $rpcenv->get_user();
194 my $vmstatus = PVE
::QemuServer
::vmstatus
();
197 foreach my $vmid (keys %$vmstatus) {
198 next if !$rpcenv->check($authuser, "/vms/$vmid", [ 'VM.Audit' ], 1);
200 my $data = $vmstatus->{$vmid};
201 $data->{vmid
} = $vmid;
208 __PACKAGE__-
>register_method({
212 description
=> "Create or restore a virtual machine.",
214 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.",
216 [ 'perm', '/vms/{vmid}', ['VM.Allocate']],
217 [ 'perm', '/pool/{pool}', ['VM.Allocate'], require_param
=> 'pool'],
223 additionalProperties
=> 0,
224 properties
=> PVE
::QemuServer
::json_config_properties
(
226 node
=> get_standard_option
('pve-node'),
227 vmid
=> get_standard_option
('pve-vmid'),
229 description
=> "The backup file.",
234 storage
=> get_standard_option
('pve-storage-id', {
235 description
=> "Default storage.",
241 description
=> "Allow to overwrite existing VM.",
242 requires
=> 'archive',
247 description
=> "Assign a unique random ethernet address.",
248 requires
=> 'archive',
252 type
=> 'string', format
=> 'pve-poolid',
253 description
=> "Add the VM to the specified pool.",
263 my $rpcenv = PVE
::RPCEnvironment
::get
();
265 my $authuser = $rpcenv->get_user();
267 my $node = extract_param
($param, 'node');
269 my $vmid = extract_param
($param, 'vmid');
271 my $archive = extract_param
($param, 'archive');
273 my $storage = extract_param
($param, 'storage');
275 my $force = extract_param
($param, 'force');
277 my $unique = extract_param
($param, 'unique');
279 my $pool = extract_param
($param, 'pool');
281 my $filename = PVE
::QemuServer
::config_file
($vmid);
283 my $storecfg = PVE
::Storage
::config
();
285 PVE
::Cluster
::check_cfs_quorum
();
287 if (defined($pool)) {
288 $rpcenv->check_pool_exist($pool);
291 $rpcenv->check($authuser, "/storage/$storage", ['Datastore.AllocateSpace'])
292 if defined($storage);
295 &$resolve_cdrom_alias($param);
297 &$check_storage_access($rpcenv, $authuser, $storecfg, $vmid, $param, $storage);
299 &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, $pool, [ keys %$param]);
301 foreach my $opt (keys %$param) {
302 if (PVE
::QemuServer
::valid_drivename
($opt)) {
303 my $drive = PVE
::QemuServer
::parse_drive
($opt, $param->{$opt});
304 raise_param_exc
({ $opt => "unable to parse drive options" }) if !$drive;
306 PVE
::QemuServer
::cleanup_drive_path
($opt, $storecfg, $drive);
307 $param->{$opt} = PVE
::QemuServer
::print_drive
($vmid, $drive);
311 PVE
::QemuServer
::add_random_macs
($param);
313 my $keystr = join(' ', keys %$param);
314 raise_param_exc
({ archive
=> "option conflicts with other options ($keystr)"}) if $keystr;
316 if ($archive eq '-') {
317 die "pipe requires cli environment\n"
318 if $rpcenv->{type
} ne 'cli';
320 my $path = $rpcenv->check_volume_access($authuser, $storecfg, $vmid, $archive);
322 PVE
::Storage
::activate_volumes
($storecfg, [ $archive ])
323 if PVE
::Storage
::parse_volume_id
($archive, 1);
325 die "can't find archive file '$archive'\n" if !($path && -f
$path);
330 my $addVMtoPoolFn = sub {
331 my $usercfg = cfs_read_file
("user.cfg");
332 if (my $data = $usercfg->{pools
}->{$pool}) {
333 $data->{vms
}->{$vmid} = 1;
334 $usercfg->{vms
}->{$vmid} = $pool;
335 cfs_write_file
("user.cfg", $usercfg);
339 my $restorefn = sub {
342 die "unable to restore vm $vmid: config file already exists\n"
345 die "unable to restore vm $vmid: vm is running\n"
346 if PVE
::QemuServer
::check_running
($vmid);
348 # destroy existing data - keep empty config
349 PVE
::QemuServer
::destroy_vm
($storecfg, $vmid, 1);
353 PVE
::QemuServer
::restore_archive
($archive, $vmid, $authuser, {
356 unique
=> $unique });
358 PVE
::AccessControl
::lock_user_config
($addVMtoPoolFn, "can't add VM to pool") if $pool;
361 return $rpcenv->fork_worker('qmrestore', $vmid, $authuser, $realcmd);
367 die "unable to create vm $vmid: config file already exists\n"
378 $vollist = &$create_disks($rpcenv, $authuser, $conf, $storecfg, $vmid, $pool, $param, $storage);
380 # try to be smart about bootdisk
381 my @disks = PVE
::QemuServer
::disknames
();
383 foreach my $ds (reverse @disks) {
384 next if !$conf->{$ds};
385 my $disk = PVE
::QemuServer
::parse_drive
($ds, $conf->{$ds});
386 next if PVE
::QemuServer
::drive_is_cdrom
($disk);
390 if (!$conf->{bootdisk
} && $firstdisk) {
391 $conf->{bootdisk
} = $firstdisk;
394 PVE
::QemuServer
::update_config_nolock
($vmid, $conf);
400 foreach my $volid (@$vollist) {
401 eval { PVE
::Storage
::vdisk_free
($storecfg, $volid); };
404 die "create failed - $err";
407 PVE
::AccessControl
::lock_user_config
($addVMtoPoolFn, "can't add VM to pool") if $pool;
410 return $rpcenv->fork_worker('qmcreate', $vmid, $authuser, $realcmd);
413 return PVE
::QemuServer
::lock_config_full
($vmid, 1, $archive ?
$restorefn : $createfn);
416 __PACKAGE__-
>register_method({
421 description
=> "Directory index",
426 additionalProperties
=> 0,
428 node
=> get_standard_option
('pve-node'),
429 vmid
=> get_standard_option
('pve-vmid'),
437 subdir
=> { type
=> 'string' },
440 links
=> [ { rel
=> 'child', href
=> "{subdir}" } ],
446 { subdir
=> 'config' },
447 { subdir
=> 'status' },
448 { subdir
=> 'unlink' },
449 { subdir
=> 'vncproxy' },
450 { subdir
=> 'migrate' },
452 { subdir
=> 'rrddata' },
453 { subdir
=> 'monitor' },
459 __PACKAGE__-
>register_method({
461 path
=> '{vmid}/rrd',
463 protected
=> 1, # fixme: can we avoid that?
465 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
467 description
=> "Read VM RRD statistics (returns PNG)",
469 additionalProperties
=> 0,
471 node
=> get_standard_option
('pve-node'),
472 vmid
=> get_standard_option
('pve-vmid'),
474 description
=> "Specify the time frame you are interested in.",
476 enum
=> [ 'hour', 'day', 'week', 'month', 'year' ],
479 description
=> "The list of datasources you want to display.",
480 type
=> 'string', format
=> 'pve-configid-list',
483 description
=> "The RRD consolidation function",
485 enum
=> [ 'AVERAGE', 'MAX' ],
493 filename
=> { type
=> 'string' },
499 return PVE
::Cluster
::create_rrd_graph
(
500 "pve2-vm/$param->{vmid}", $param->{timeframe
},
501 $param->{ds
}, $param->{cf
});
505 __PACKAGE__-
>register_method({
507 path
=> '{vmid}/rrddata',
509 protected
=> 1, # fixme: can we avoid that?
511 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
513 description
=> "Read VM RRD statistics",
515 additionalProperties
=> 0,
517 node
=> get_standard_option
('pve-node'),
518 vmid
=> get_standard_option
('pve-vmid'),
520 description
=> "Specify the time frame you are interested in.",
522 enum
=> [ 'hour', 'day', 'week', 'month', 'year' ],
525 description
=> "The RRD consolidation function",
527 enum
=> [ 'AVERAGE', 'MAX' ],
542 return PVE
::Cluster
::create_rrd_data
(
543 "pve2-vm/$param->{vmid}", $param->{timeframe
}, $param->{cf
});
547 __PACKAGE__-
>register_method({
549 path
=> '{vmid}/config',
552 description
=> "Get virtual machine configuration.",
554 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
557 additionalProperties
=> 0,
559 node
=> get_standard_option
('pve-node'),
560 vmid
=> get_standard_option
('pve-vmid'),
568 description
=> 'SHA1 digest of configuration file. This can be used to prevent concurrent modifications.',
575 my $conf = PVE
::QemuServer
::load_config
($param->{vmid
});
580 my $vm_is_volid_owner = sub {
581 my ($storecfg, $vmid, $volid) =@_;
583 if ($volid !~ m
|^/|) {
585 eval { ($path, $owner) = PVE
::Storage
::path
($storecfg, $volid); };
586 if ($owner && ($owner == $vmid)) {
594 my $test_deallocate_drive = sub {
595 my ($storecfg, $vmid, $key, $drive, $force) = @_;
597 if (!PVE
::QemuServer
::drive_is_cdrom
($drive)) {
598 my $volid = $drive->{file
};
599 if (&$vm_is_volid_owner($storecfg, $vmid, $volid)) {
600 if ($force || $key =~ m/^unused/) {
601 my $sid = PVE
::Storage
::parse_volume_id
($volid);
610 my $delete_drive = sub {
611 my ($conf, $storecfg, $vmid, $key, $drive, $force) = @_;
613 if (!PVE
::QemuServer
::drive_is_cdrom
($drive)) {
614 my $volid = $drive->{file
};
615 if (&$vm_is_volid_owner($storecfg, $vmid, $volid)) {
616 if ($force || $key =~ m/^unused/) {
617 eval { PVE
::Storage
::vdisk_free
($storecfg, $volid); };
620 PVE
::QemuServer
::add_unused_volume
($conf, $volid, $vmid);
625 delete $conf->{$key};
628 my $vmconfig_delete_option = sub {
629 my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $force) = @_;
631 return if !defined($conf->{$opt});
633 my $isDisk = PVE
::QemuServer
::valid_drivename
($opt)|| ($opt =~ m/^unused/);
636 $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']);
638 my $drive = PVE
::QemuServer
::parse_drive
($opt, $conf->{$opt});
639 if (my $sid = &$test_deallocate_drive($storecfg, $vmid, $opt, $drive, $force)) {
640 $rpcenv->check($authuser, "/storage/$sid", ['Datastore.Allocate']);
644 die "error hot-unplug $opt" if !PVE
::QemuServer
::vm_deviceunplug
($vmid, $conf, $opt);
647 my $drive = PVE
::QemuServer
::parse_drive
($opt, $conf->{$opt});
648 &$delete_drive($conf, $storecfg, $vmid, $opt, $drive, $force);
650 delete $conf->{$opt};
653 PVE
::QemuServer
::update_config_nolock
($vmid, $conf, 1);
656 my $vmconfig_update_disk = sub {
657 my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $value, $force) = @_;
659 my $drive = PVE
::QemuServer
::parse_drive
($opt, $value);
661 if (PVE
::QemuServer
::drive_is_cdrom
($drive)) { #cdrom
662 $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.CDROM']);
664 $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']);
669 if (my $old_drive = PVE
::QemuServer
::parse_drive
($opt, $conf->{$opt})) {
671 my $media = $drive->{media
} || 'disk';
672 my $oldmedia = $old_drive->{media
} || 'disk';
673 die "unable to change media type\n" if $media ne $oldmedia;
675 if (!PVE
::QemuServer
::drive_is_cdrom
($old_drive) &&
676 ($drive->{file
} ne $old_drive->{file
})) { # delete old disks
678 &$vmconfig_delete_option($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $force);
679 $conf = PVE
::QemuServer
::load_config
($vmid); # update/reload
682 if($drive->{bps
} != $old_drive->{bps
} ||
683 $drive->{bps_rd
} != $old_drive->{bps_rd
} ||
684 $drive->{bps_wr
} != $old_drive->{bps_wr
} ||
685 $drive->{iops
} != $old_drive->{iops
} ||
686 $drive->{iops_rd
} != $old_drive->{iops_rd
} ||
687 $drive->{iops_wr
} != $old_drive->{iops_wr
} ) {
688 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);
693 &$create_disks($rpcenv, $authuser, $conf, $storecfg, $vmid, undef, {$opt => $value});
694 PVE
::QemuServer
::update_config_nolock
($vmid, $conf, 1);
696 $conf = PVE
::QemuServer
::load_config
($vmid); # update/reload
697 $drive = PVE
::QemuServer
::parse_drive
($opt, $conf->{$opt});
699 if (PVE
::QemuServer
::drive_is_cdrom
($drive)) { # cdrom
701 if (PVE
::QemuServer
::check_running
($vmid)) {
702 if ($drive->{file
} eq 'none') {
703 PVE
::QemuServer
::vm_mon_cmd
($vmid, "eject",force
=> JSON
::true
,device
=> "drive-$opt");
705 my $path = PVE
::QemuServer
::get_iso_path
($storecfg, $vmid, $drive->{file
});
706 PVE
::QemuServer
::vm_mon_cmd
($vmid, "eject",force
=> JSON
::true
,device
=> "drive-$opt"); #force eject if locked
707 PVE
::QemuServer
::vm_mon_cmd
($vmid, "change",device
=> "drive-$opt",target
=> "$path") if $path;
711 } else { # hotplug new disks
713 die "error hotplug $opt" if !PVE
::QemuServer
::vm_deviceplug
($storecfg, $conf, $vmid, $opt, $drive);
717 my $vmconfig_update_net = sub {
718 my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $value) = @_;
721 #if online update, then unplug first
722 die "error hot-unplug $opt for update" if !PVE
::QemuServer
::vm_deviceunplug
($vmid, $conf, $opt);
725 $conf->{$opt} = $value;
726 PVE
::QemuServer
::update_config_nolock
($vmid, $conf, 1);
727 $conf = PVE
::QemuServer
::load_config
($vmid); # update/reload
729 my $net = PVE
::QemuServer
::parse_net
($conf->{$opt});
731 die "error hotplug $opt" if !PVE
::QemuServer
::vm_deviceplug
($storecfg, $conf, $vmid, $opt, $net);
734 my $vm_config_perm_list = [
744 __PACKAGE__-
>register_method({
746 path
=> '{vmid}/config',
750 description
=> "Set virtual machine options.",
752 check
=> ['perm', '/vms/{vmid}', $vm_config_perm_list, any
=> 1],
755 additionalProperties
=> 0,
756 properties
=> PVE
::QemuServer
::json_config_properties
(
758 node
=> get_standard_option
('pve-node'),
759 vmid
=> get_standard_option
('pve-vmid'),
760 skiplock
=> get_standard_option
('skiplock'),
762 type
=> 'string', format
=> 'pve-configid-list',
763 description
=> "A list of settings you want to delete.",
768 description
=> $opt_force_description,
770 requires
=> 'delete',
774 description
=> 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.',
780 returns
=> { type
=> 'null'},
784 my $rpcenv = PVE
::RPCEnvironment
::get
();
786 my $authuser = $rpcenv->get_user();
788 my $node = extract_param
($param, 'node');
790 my $vmid = extract_param
($param, 'vmid');
792 my $digest = extract_param
($param, 'digest');
794 my @paramarr = (); # used for log message
795 foreach my $key (keys %$param) {
796 push @paramarr, "-$key", $param->{$key};
799 my $skiplock = extract_param
($param, 'skiplock');
800 raise_param_exc
({ skiplock
=> "Only root may use this option." })
801 if $skiplock && $authuser ne 'root@pam';
803 my $delete_str = extract_param
($param, 'delete');
805 my $force = extract_param
($param, 'force');
807 die "no options specified\n" if !$delete_str && !scalar(keys %$param);
809 my $storecfg = PVE
::Storage
::config
();
811 &$resolve_cdrom_alias($param);
813 # now try to verify all parameters
816 foreach my $opt (PVE
::Tools
::split_list
($delete_str)) {
817 $opt = 'ide2' if $opt eq 'cdrom';
818 raise_param_exc
({ delete => "you can't use '-$opt' and " .
819 "-delete $opt' at the same time" })
820 if defined($param->{$opt});
822 if (!PVE
::QemuServer
::option_exists
($opt)) {
823 raise_param_exc
({ delete => "unknown option '$opt'" });
829 foreach my $opt (keys %$param) {
830 if (PVE
::QemuServer
::valid_drivename
($opt)) {
832 my $drive = PVE
::QemuServer
::parse_drive
($opt, $param->{$opt});
833 PVE
::QemuServer
::cleanup_drive_path
($opt, $storecfg, $drive);
834 $param->{$opt} = PVE
::QemuServer
::print_drive
($vmid, $drive);
835 } elsif ($opt =~ m/^net(\d+)$/) {
837 my $net = PVE
::QemuServer
::parse_net
($param->{$opt});
838 $param->{$opt} = PVE
::QemuServer
::print_net
($net);
842 &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, undef, [@delete]);
844 &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, undef, [keys %$param]);
846 &$check_storage_access($rpcenv, $authuser, $storecfg, $vmid, $param);
850 my $conf = PVE
::QemuServer
::load_config
($vmid);
852 die "checksum missmatch (file change by other user?)\n"
853 if $digest && $digest ne $conf->{digest
};
855 PVE
::QemuServer
::check_lock
($conf) if !$skiplock;
857 PVE
::Cluster
::log_msg
('info', $authuser, "update VM $vmid: " . join (' ', @paramarr));
859 foreach my $opt (@delete) { # delete
860 $conf = PVE
::QemuServer
::load_config
($vmid); # update/reload
861 &$vmconfig_delete_option($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $force);
864 foreach my $opt (keys %$param) { # add/change
866 $conf = PVE
::QemuServer
::load_config
($vmid); # update/reload
868 next if $conf->{$opt} && ($param->{$opt} eq $conf->{$opt}); # skip if nothing changed
870 if (PVE
::QemuServer
::valid_drivename
($opt)) {
872 &$vmconfig_update_disk($rpcenv, $authuser, $conf, $storecfg, $vmid,
873 $opt, $param->{$opt}, $force);
875 } elsif ($opt =~ m/^net(\d+)$/) { #nics
877 &$vmconfig_update_net($rpcenv, $authuser, $conf, $storecfg, $vmid,
878 $opt, $param->{$opt});
882 $conf->{$opt} = $param->{$opt};
883 PVE
::QemuServer
::update_config_nolock
($vmid, $conf, 1);
888 PVE
::QemuServer
::lock_config
($vmid, $updatefn);
894 __PACKAGE__-
>register_method({
895 name
=> 'destroy_vm',
900 description
=> "Destroy the vm (also delete all used/owned volumes).",
902 check
=> [ 'perm', '/vms/{vmid}', ['VM.Allocate']],
905 additionalProperties
=> 0,
907 node
=> get_standard_option
('pve-node'),
908 vmid
=> get_standard_option
('pve-vmid'),
909 skiplock
=> get_standard_option
('skiplock'),
918 my $rpcenv = PVE
::RPCEnvironment
::get
();
920 my $authuser = $rpcenv->get_user();
922 my $vmid = $param->{vmid
};
924 my $skiplock = $param->{skiplock
};
925 raise_param_exc
({ skiplock
=> "Only root may use this option." })
926 if $skiplock && $authuser ne 'root@pam';
929 my $conf = PVE
::QemuServer
::load_config
($vmid);
931 my $storecfg = PVE
::Storage
::config
();
933 my $delVMfromPoolFn = sub {
934 my $usercfg = cfs_read_file
("user.cfg");
935 if (my $pool = $usercfg->{vms
}->{$vmid}) {
936 if (my $data = $usercfg->{pools
}->{$pool}) {
937 delete $data->{vms
}->{$vmid};
938 delete $usercfg->{vms
}->{$vmid};
939 cfs_write_file
("user.cfg", $usercfg);
947 syslog
('info', "destroy VM $vmid: $upid\n");
949 PVE
::QemuServer
::vm_destroy
($storecfg, $vmid, $skiplock);
951 PVE
::AccessControl
::lock_user_config
($delVMfromPoolFn, "pool cleanup failed");
954 return $rpcenv->fork_worker('qmdestroy', $vmid, $authuser, $realcmd);
957 __PACKAGE__-
>register_method({
959 path
=> '{vmid}/unlink',
963 description
=> "Unlink/delete disk images.",
965 check
=> [ 'perm', '/vms/{vmid}', ['VM.Config.Disk']],
968 additionalProperties
=> 0,
970 node
=> get_standard_option
('pve-node'),
971 vmid
=> get_standard_option
('pve-vmid'),
973 type
=> 'string', format
=> 'pve-configid-list',
974 description
=> "A list of disk IDs you want to delete.",
978 description
=> $opt_force_description,
983 returns
=> { type
=> 'null'},
987 $param->{delete} = extract_param
($param, 'idlist');
989 __PACKAGE__-
>update_vm($param);
996 __PACKAGE__-
>register_method({
998 path
=> '{vmid}/vncproxy',
1002 check
=> ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
1004 description
=> "Creates a TCP VNC proxy connections.",
1006 additionalProperties
=> 0,
1008 node
=> get_standard_option
('pve-node'),
1009 vmid
=> get_standard_option
('pve-vmid'),
1013 additionalProperties
=> 0,
1015 user
=> { type
=> 'string' },
1016 ticket
=> { type
=> 'string' },
1017 cert
=> { type
=> 'string' },
1018 port
=> { type
=> 'integer' },
1019 upid
=> { type
=> 'string' },
1025 my $rpcenv = PVE
::RPCEnvironment
::get
();
1027 my $authuser = $rpcenv->get_user();
1029 my $vmid = $param->{vmid
};
1030 my $node = $param->{node
};
1032 my $authpath = "/vms/$vmid";
1034 my $ticket = PVE
::AccessControl
::assemble_vnc_ticket
($authuser, $authpath);
1036 $sslcert = PVE
::Tools
::file_get_contents
("/etc/pve/pve-root-ca.pem", 8192)
1039 my $port = PVE
::Tools
::next_vnc_port
();
1043 if ($node ne 'localhost' && $node ne PVE
::INotify
::nodename
()) {
1044 $remip = PVE
::Cluster
::remote_node_ip
($node);
1047 # NOTE: kvm VNC traffic is already TLS encrypted,
1048 # so we select the fastest chipher here (or 'none'?)
1049 my $remcmd = $remip ?
['/usr/bin/ssh', '-T', '-o', 'BatchMode=yes',
1050 '-c', 'blowfish-cbc', $remip] : [];
1057 syslog
('info', "starting vnc proxy $upid\n");
1059 my $qmcmd = [@$remcmd, "/usr/sbin/qm", 'vncproxy', $vmid];
1061 my $qmstr = join(' ', @$qmcmd);
1063 # also redirect stderr (else we get RFB protocol errors)
1064 my $cmd = ['/bin/nc', '-l', '-p', $port, '-w', $timeout, '-c', "$qmstr 2>/dev/null"];
1066 PVE
::Tools
::run_command
($cmd);
1071 my $upid = $rpcenv->fork_worker('vncproxy', $vmid, $authuser, $realcmd);
1082 __PACKAGE__-
>register_method({
1084 path
=> '{vmid}/status',
1087 description
=> "Directory index",
1092 additionalProperties
=> 0,
1094 node
=> get_standard_option
('pve-node'),
1095 vmid
=> get_standard_option
('pve-vmid'),
1103 subdir
=> { type
=> 'string' },
1106 links
=> [ { rel
=> 'child', href
=> "{subdir}" } ],
1112 my $conf = PVE
::QemuServer
::load_config
($param->{vmid
});
1115 { subdir
=> 'current' },
1116 { subdir
=> 'start' },
1117 { subdir
=> 'stop' },
1123 my $vm_is_ha_managed = sub {
1126 my $cc = PVE
::Cluster
::cfs_read_file
('cluster.conf');
1127 if (PVE
::Cluster
::cluster_conf_lookup_pvevm
($cc, 0, $vmid, 1)) {
1133 __PACKAGE__-
>register_method({
1134 name
=> 'vm_status',
1135 path
=> '{vmid}/status/current',
1138 protected
=> 1, # qemu pid files are only readable by root
1139 description
=> "Get virtual machine status.",
1141 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
1144 additionalProperties
=> 0,
1146 node
=> get_standard_option
('pve-node'),
1147 vmid
=> get_standard_option
('pve-vmid'),
1150 returns
=> { type
=> 'object' },
1155 my $conf = PVE
::QemuServer
::load_config
($param->{vmid
});
1157 my $vmstatus = PVE
::QemuServer
::vmstatus
($param->{vmid
}, 1);
1158 my $status = $vmstatus->{$param->{vmid
}};
1160 $status->{ha
} = &$vm_is_ha_managed($param->{vmid
});
1165 __PACKAGE__-
>register_method({
1167 path
=> '{vmid}/status/start',
1171 description
=> "Start virtual machine.",
1173 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1176 additionalProperties
=> 0,
1178 node
=> get_standard_option
('pve-node'),
1179 vmid
=> get_standard_option
('pve-vmid'),
1180 skiplock
=> get_standard_option
('skiplock'),
1181 stateuri
=> get_standard_option
('pve-qm-stateuri'),
1190 my $rpcenv = PVE
::RPCEnvironment
::get
();
1192 my $authuser = $rpcenv->get_user();
1194 my $node = extract_param
($param, 'node');
1196 my $vmid = extract_param
($param, 'vmid');
1198 my $stateuri = extract_param
($param, 'stateuri');
1199 raise_param_exc
({ stateuri
=> "Only root may use this option." })
1200 if $stateuri && $authuser ne 'root@pam';
1202 my $skiplock = extract_param
($param, 'skiplock');
1203 raise_param_exc
({ skiplock
=> "Only root may use this option." })
1204 if $skiplock && $authuser ne 'root@pam';
1206 my $storecfg = PVE
::Storage
::config
();
1208 if (&$vm_is_ha_managed($vmid) && !$stateuri &&
1209 $rpcenv->{type
} ne 'ha') {
1214 my $service = "pvevm:$vmid";
1216 my $cmd = ['clusvcadm', '-e', $service, '-m', $node];
1218 print "Executing HA start for VM $vmid\n";
1220 PVE
::Tools
::run_command
($cmd);
1225 return $rpcenv->fork_worker('hastart', $vmid, $authuser, $hacmd);
1232 syslog
('info', "start VM $vmid: $upid\n");
1234 PVE
::QemuServer
::vm_start
($storecfg, $vmid, $stateuri, $skiplock);
1239 return $rpcenv->fork_worker('qmstart', $vmid, $authuser, $realcmd);
1243 __PACKAGE__-
>register_method({
1245 path
=> '{vmid}/status/stop',
1249 description
=> "Stop virtual machine.",
1251 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1254 additionalProperties
=> 0,
1256 node
=> get_standard_option
('pve-node'),
1257 vmid
=> get_standard_option
('pve-vmid'),
1258 skiplock
=> get_standard_option
('skiplock'),
1260 description
=> "Wait maximal timeout seconds.",
1266 description
=> "Do not decativate storage volumes.",
1279 my $rpcenv = PVE
::RPCEnvironment
::get
();
1281 my $authuser = $rpcenv->get_user();
1283 my $node = extract_param
($param, 'node');
1285 my $vmid = extract_param
($param, 'vmid');
1287 my $skiplock = extract_param
($param, 'skiplock');
1288 raise_param_exc
({ skiplock
=> "Only root may use this option." })
1289 if $skiplock && $authuser ne 'root@pam';
1291 my $keepActive = extract_param
($param, 'keepActive');
1292 raise_param_exc
({ keepActive
=> "Only root may use this option." })
1293 if $keepActive && $authuser ne 'root@pam';
1295 my $storecfg = PVE
::Storage
::config
();
1297 if (&$vm_is_ha_managed($vmid) && $rpcenv->{type
} ne 'ha') {
1302 my $service = "pvevm:$vmid";
1304 my $cmd = ['clusvcadm', '-d', $service];
1306 print "Executing HA stop for VM $vmid\n";
1308 PVE
::Tools
::run_command
($cmd);
1313 return $rpcenv->fork_worker('hastop', $vmid, $authuser, $hacmd);
1319 syslog
('info', "stop VM $vmid: $upid\n");
1321 PVE
::QemuServer
::vm_stop
($storecfg, $vmid, $skiplock, 0,
1322 $param->{timeout
}, 0, 1, $keepActive);
1327 return $rpcenv->fork_worker('qmstop', $vmid, $authuser, $realcmd);
1331 __PACKAGE__-
>register_method({
1333 path
=> '{vmid}/status/reset',
1337 description
=> "Reset virtual machine.",
1339 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1342 additionalProperties
=> 0,
1344 node
=> get_standard_option
('pve-node'),
1345 vmid
=> get_standard_option
('pve-vmid'),
1346 skiplock
=> get_standard_option
('skiplock'),
1355 my $rpcenv = PVE
::RPCEnvironment
::get
();
1357 my $authuser = $rpcenv->get_user();
1359 my $node = extract_param
($param, 'node');
1361 my $vmid = extract_param
($param, 'vmid');
1363 my $skiplock = extract_param
($param, 'skiplock');
1364 raise_param_exc
({ skiplock
=> "Only root may use this option." })
1365 if $skiplock && $authuser ne 'root@pam';
1367 die "VM $vmid not running\n" if !PVE
::QemuServer
::check_running
($vmid);
1372 PVE
::QemuServer
::vm_reset
($vmid, $skiplock);
1377 return $rpcenv->fork_worker('qmreset', $vmid, $authuser, $realcmd);
1380 __PACKAGE__-
>register_method({
1381 name
=> 'vm_shutdown',
1382 path
=> '{vmid}/status/shutdown',
1386 description
=> "Shutdown virtual machine.",
1388 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1391 additionalProperties
=> 0,
1393 node
=> get_standard_option
('pve-node'),
1394 vmid
=> get_standard_option
('pve-vmid'),
1395 skiplock
=> get_standard_option
('skiplock'),
1397 description
=> "Wait maximal timeout seconds.",
1403 description
=> "Make sure the VM stops.",
1409 description
=> "Do not decativate storage volumes.",
1422 my $rpcenv = PVE
::RPCEnvironment
::get
();
1424 my $authuser = $rpcenv->get_user();
1426 my $node = extract_param
($param, 'node');
1428 my $vmid = extract_param
($param, 'vmid');
1430 my $skiplock = extract_param
($param, 'skiplock');
1431 raise_param_exc
({ skiplock
=> "Only root may use this option." })
1432 if $skiplock && $authuser ne 'root@pam';
1434 my $keepActive = extract_param
($param, 'keepActive');
1435 raise_param_exc
({ keepActive
=> "Only root may use this option." })
1436 if $keepActive && $authuser ne 'root@pam';
1438 my $storecfg = PVE
::Storage
::config
();
1443 syslog
('info', "shutdown VM $vmid: $upid\n");
1445 PVE
::QemuServer
::vm_stop
($storecfg, $vmid, $skiplock, 0, $param->{timeout
},
1446 1, $param->{forceStop
}, $keepActive);
1451 return $rpcenv->fork_worker('qmshutdown', $vmid, $authuser, $realcmd);
1454 __PACKAGE__-
>register_method({
1455 name
=> 'vm_suspend',
1456 path
=> '{vmid}/status/suspend',
1460 description
=> "Suspend virtual machine.",
1462 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1465 additionalProperties
=> 0,
1467 node
=> get_standard_option
('pve-node'),
1468 vmid
=> get_standard_option
('pve-vmid'),
1469 skiplock
=> get_standard_option
('skiplock'),
1478 my $rpcenv = PVE
::RPCEnvironment
::get
();
1480 my $authuser = $rpcenv->get_user();
1482 my $node = extract_param
($param, 'node');
1484 my $vmid = extract_param
($param, 'vmid');
1486 my $skiplock = extract_param
($param, 'skiplock');
1487 raise_param_exc
({ skiplock
=> "Only root may use this option." })
1488 if $skiplock && $authuser ne 'root@pam';
1490 die "VM $vmid not running\n" if !PVE
::QemuServer
::check_running
($vmid);
1495 syslog
('info', "suspend VM $vmid: $upid\n");
1497 PVE
::QemuServer
::vm_suspend
($vmid, $skiplock);
1502 return $rpcenv->fork_worker('qmsuspend', $vmid, $authuser, $realcmd);
1505 __PACKAGE__-
>register_method({
1506 name
=> 'vm_resume',
1507 path
=> '{vmid}/status/resume',
1511 description
=> "Resume virtual machine.",
1513 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1516 additionalProperties
=> 0,
1518 node
=> get_standard_option
('pve-node'),
1519 vmid
=> get_standard_option
('pve-vmid'),
1520 skiplock
=> get_standard_option
('skiplock'),
1529 my $rpcenv = PVE
::RPCEnvironment
::get
();
1531 my $authuser = $rpcenv->get_user();
1533 my $node = extract_param
($param, 'node');
1535 my $vmid = extract_param
($param, 'vmid');
1537 my $skiplock = extract_param
($param, 'skiplock');
1538 raise_param_exc
({ skiplock
=> "Only root may use this option." })
1539 if $skiplock && $authuser ne 'root@pam';
1541 die "VM $vmid not running\n" if !PVE
::QemuServer
::check_running
($vmid);
1546 syslog
('info', "resume VM $vmid: $upid\n");
1548 PVE
::QemuServer
::vm_resume
($vmid, $skiplock);
1553 return $rpcenv->fork_worker('qmresume', $vmid, $authuser, $realcmd);
1556 __PACKAGE__-
>register_method({
1557 name
=> 'vm_sendkey',
1558 path
=> '{vmid}/sendkey',
1562 description
=> "Send key event to virtual machine.",
1564 check
=> ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
1567 additionalProperties
=> 0,
1569 node
=> get_standard_option
('pve-node'),
1570 vmid
=> get_standard_option
('pve-vmid'),
1571 skiplock
=> get_standard_option
('skiplock'),
1573 description
=> "The key (qemu monitor encoding).",
1578 returns
=> { type
=> 'null'},
1582 my $rpcenv = PVE
::RPCEnvironment
::get
();
1584 my $authuser = $rpcenv->get_user();
1586 my $node = extract_param
($param, 'node');
1588 my $vmid = extract_param
($param, 'vmid');
1590 my $skiplock = extract_param
($param, 'skiplock');
1591 raise_param_exc
({ skiplock
=> "Only root may use this option." })
1592 if $skiplock && $authuser ne 'root@pam';
1594 PVE
::QemuServer
::vm_sendkey
($vmid, $skiplock, $param->{key
});
1599 __PACKAGE__-
>register_method({
1600 name
=> 'migrate_vm',
1601 path
=> '{vmid}/migrate',
1605 description
=> "Migrate virtual machine. Creates a new migration task.",
1607 check
=> ['perm', '/vms/{vmid}', [ 'VM.Migrate' ]],
1610 additionalProperties
=> 0,
1612 node
=> get_standard_option
('pve-node'),
1613 vmid
=> get_standard_option
('pve-vmid'),
1614 target
=> get_standard_option
('pve-node', { description
=> "Target node." }),
1617 description
=> "Use online/live migration.",
1622 description
=> "Allow to migrate VMs which use local devices. Only root may use this option.",
1629 description
=> "the task ID.",
1634 my $rpcenv = PVE
::RPCEnvironment
::get
();
1636 my $authuser = $rpcenv->get_user();
1638 my $target = extract_param
($param, 'target');
1640 my $localnode = PVE
::INotify
::nodename
();
1641 raise_param_exc
({ target
=> "target is local node."}) if $target eq $localnode;
1643 PVE
::Cluster
::check_cfs_quorum
();
1645 PVE
::Cluster
::check_node_exists
($target);
1647 my $targetip = PVE
::Cluster
::remote_node_ip
($target);
1649 my $vmid = extract_param
($param, 'vmid');
1651 raise_param_exc
({ force
=> "Only root may use this option." })
1652 if $param->{force
} && $authuser ne 'root@pam';
1655 my $conf = PVE
::QemuServer
::load_config
($vmid);
1657 # try to detect errors early
1659 PVE
::QemuServer
::check_lock
($conf);
1661 if (PVE
::QemuServer
::check_running
($vmid)) {
1662 die "cant migrate running VM without --online\n"
1663 if !$param->{online
};
1666 my $storecfg = PVE
::Storage
::config
();
1667 PVE
::QemuServer
::check_storage_availability
($storecfg, $conf, $target);
1669 if (&$vm_is_ha_managed($vmid) && $rpcenv->{type
} ne 'ha') {
1674 my $service = "pvevm:$vmid";
1676 my $cmd = ['clusvcadm', '-M', $service, '-m', $target];
1678 print "Executing HA migrate for VM $vmid to node $target\n";
1680 PVE
::Tools
::run_command
($cmd);
1685 return $rpcenv->fork_worker('hamigrate', $vmid, $authuser, $hacmd);
1692 PVE
::QemuMigrate-
>migrate($target, $targetip, $vmid, $param);
1695 return $rpcenv->fork_worker('qmigrate', $vmid, $authuser, $realcmd);
1700 __PACKAGE__-
>register_method({
1702 path
=> '{vmid}/monitor',
1706 description
=> "Execute Qemu monitor commands.",
1708 check
=> ['perm', '/vms/{vmid}', [ 'VM.Monitor' ]],
1711 additionalProperties
=> 0,
1713 node
=> get_standard_option
('pve-node'),
1714 vmid
=> get_standard_option
('pve-vmid'),
1717 description
=> "The monitor command.",
1721 returns
=> { type
=> 'string'},
1725 my $vmid = $param->{vmid
};
1727 my $conf = PVE
::QemuServer
::load_config
($vmid); # check if VM exists
1731 $res = PVE
::QemuServer
::vm_human_monitor_command
($vmid, $param->{command
});
1733 $res = "ERROR: $@" if $@;