]>
git.proxmox.com Git - pve-container.git/blob - src/PVE/API2/LXC.pm
1 package PVE
::API2
::LXC
;
7 use PVE
::Tools
qw(extract_param run_command);
8 use PVE
::Exception
qw(raise raise_param_exc);
10 use PVE
::Cluster
qw(cfs_read_file);
11 use PVE
::AccessControl
;
14 use PVE
::RPCEnvironment
;
18 use PVE
::JSONSchema
qw(get_standard_option);
19 use base
qw(PVE::RESTHandler);
21 use Data
::Dumper
; # fixme: remove
23 my $get_container_storage = sub {
24 my ($stcfg, $vmid, $lxc_conf) = @_;
26 if (my $volid = $lxc_conf->{'pve.volid'}) {
27 my ($sid, $volname) = PVE
::Storage
::parse_volume_id
($volid);
28 return wantarray ?
($sid, $volname) : $sid;
30 my $path = $lxc_conf->{'lxc.rootfs'};
31 my ($vtype, $volid) = PVE
::Storage
::path_to_volume_id
($stcfg, $path);
32 my ($sid, $volname) = PVE
::Storage
::parse_volume_id
($volid, 1) if $volid;
33 return wantarray ?
($sid, $volname, $path) : $sid;
37 my $check_ct_modify_config_perm = sub {
38 my ($rpcenv, $authuser, $vmid, $pool, $key_list) = @_;
40 return 1 if $authuser ne 'root@pam';
42 foreach my $opt (@$key_list) {
44 if ($opt eq 'cpus' || $opt eq 'cpuunits' || $opt eq 'cpulimit') {
45 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.CPU']);
46 } elsif ($opt eq 'disk') {
47 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Disk']);
48 } elsif ($opt eq 'memory' || $opt eq 'swap') {
49 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Memory']);
50 } elsif ($opt =~ m/^net\d+$/ || $opt eq 'nameserver' ||
51 $opt eq 'searchdomain' || $opt eq 'hostname') {
52 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Network']);
54 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Options']);
61 PVE
::JSONSchema
::register_standard_option
('pve-lxc-snapshot-name', {
62 description
=> "The name of the snapshot.",
63 type
=> 'string', format
=> 'pve-configid',
67 __PACKAGE__-
>register_method({
71 description
=> "LXC container index (per node).",
73 description
=> "Only list CTs where you have VM.Audit permissons on /vms/<vmid>.",
77 protected
=> 1, # /proc files are only readable by root
79 additionalProperties
=> 0,
81 node
=> get_standard_option
('pve-node'),
90 links
=> [ { rel
=> 'child', href
=> "{vmid}" } ],
95 my $rpcenv = PVE
::RPCEnvironment
::get
();
96 my $authuser = $rpcenv->get_user();
98 my $vmstatus = PVE
::LXC
::vmstatus
();
101 foreach my $vmid (keys %$vmstatus) {
102 next if !$rpcenv->check($authuser, "/vms/$vmid", [ 'VM.Audit' ], 1);
104 my $data = $vmstatus->{$vmid};
105 $data->{vmid
} = $vmid;
113 __PACKAGE__-
>register_method({
117 description
=> "Create or restore a container.",
119 user
=> 'all', # check inside
120 description
=> "You need 'VM.Allocate' permissions on /vms/{vmid} or on the VM pool /pool/{pool}. " .
121 "For restore, it is enough if the user has 'VM.Backup' permission and the VM already exists. " .
122 "You also need 'Datastore.AllocateSpace' permissions on the storage.",
127 additionalProperties
=> 0,
128 properties
=> PVE
::LXC
::json_config_properties
({
129 node
=> get_standard_option
('pve-node'),
130 vmid
=> get_standard_option
('pve-vmid'),
132 description
=> "The OS template or backup file.",
139 description
=> "Sets root password inside container.",
142 storage
=> get_standard_option
('pve-storage-id', {
143 description
=> "Target storage.",
150 description
=> "Allow to overwrite existing container.",
155 description
=> "Mark this as restore task.",
159 type
=> 'string', format
=> 'pve-poolid',
160 description
=> "Add the VM to the specified pool.",
170 my $rpcenv = PVE
::RPCEnvironment
::get
();
172 my $authuser = $rpcenv->get_user();
174 my $node = extract_param
($param, 'node');
176 my $vmid = extract_param
($param, 'vmid');
178 my $basecfg_fn = PVE
::LXC
::config_file
($vmid);
180 my $same_container_exists = -f
$basecfg_fn;
182 my $restore = extract_param
($param, 'restore');
185 # fixme: limit allowed parameters
189 my $force = extract_param
($param, 'force');
191 if (!($same_container_exists && $restore && $force)) {
192 PVE
::Cluster
::check_vmid_unused
($vmid);
195 my $password = extract_param
($param, 'password');
197 my $storage = extract_param
($param, 'storage') || 'local';
199 my $pool = extract_param
($param, 'pool');
201 my $storage_cfg = cfs_read_file
("storage.cfg");
203 my $scfg = PVE
::Storage
::storage_check_node
($storage_cfg, $storage, $node);
205 raise_param_exc
({ storage
=> "storage '$storage' does not support container root directories"})
206 if !$scfg->{content
}->{rootdir
};
208 if (defined($pool)) {
209 $rpcenv->check_pool_exist($pool);
210 $rpcenv->check_perm_modify($authuser, "/pool/$pool");
213 if ($rpcenv->check($authuser, "/vms/$vmid", ['VM.Allocate'], 1)) {
215 } elsif ($pool && $rpcenv->check($authuser, "/pool/$pool", ['VM.Allocate'], 1)) {
217 } elsif ($restore && $force && $same_container_exists &&
218 $rpcenv->check($authuser, "/vms/$vmid", ['VM.Backup'], 1)) {
219 # OK: user has VM.Backup permissions, and want to restore an existing VM
224 &$check_ct_modify_config_perm($rpcenv, $authuser, $vmid, $pool, [ keys %$param]);
226 PVE
::Storage
::activate_storage
($storage_cfg, $storage);
228 my $ostemplate = extract_param
($param, 'ostemplate');
232 if ($ostemplate eq '-') {
233 die "pipe requires cli environment\n"
234 if $rpcenv->{type
} ne 'cli';
235 die "pipe can only be used with restore tasks\n"
239 $rpcenv->check_volume_access($authuser, $storage_cfg, $vmid, $ostemplate);
240 $archive = PVE
::Storage
::abs_filesystem_path
($storage_cfg, $ostemplate);
246 $conf = PVE
::LXCCreate
::recover_config
($archive, $conf);
247 PVE
::LXC
::lxc_config_change_vmid
($conf, $vmid);
250 $param->{hostname
} ||= "CT$vmid" if !defined($conf->{'lxc.utsname'});
251 $param->{memory
} ||= 512 if !defined($conf->{'lxc.cgroup.memory.limit_in_bytes'});
252 $param->{swap
} //= 512 if !defined($conf->{'lxc.cgroup.memory.memsw.limit_in_bytes'});
254 PVE
::LXC
::update_lxc_config
($vmid, $conf, 0, $param);
256 # assigng default names, so that we can configure network with LXCSetup
257 foreach my $k (keys %$conf) {
258 next if $k !~ m/^net(\d+)$/;
260 $conf->{$k}->{name
} ||= "eth$ind";
263 # use user namespace ?
264 # disable for now, because kernel 3.10.0 does not support it
265 #$conf->{'lxc.id_map'} = ["u 0 100000 65536", "g 0 100000 65536"];
267 my $check_vmid_usage = sub {
269 die "cant overwrite running container\n"
270 if PVE
::LXC
::check_running
($vmid);
272 PVE
::Cluster
::check_vmid_unused
($vmid);
277 if ($restore && ($ostemplate =~ m/openvz/) ) {
278 print "###########################################################\n";
279 print "Restore from OpenVZ please check the config and add network\n";
280 print "###########################################################\n";
283 &$check_vmid_usage(); # final check after locking
285 PVE
::Cluster
::check_cfs_quorum
();
287 PVE
::LXCCreate
::create_rootfs
($storage_cfg, $storage, $param->{disk
}, $vmid, $conf,
288 $archive, $password, $restore);
291 my $realcmd = sub { PVE
::LXC
::lock_container
($vmid, 1, $code); };
293 &$check_vmid_usage(); # first check before locking
295 return $rpcenv->fork_worker($restore ?
'vzrestore' : 'vzcreate',
296 $vmid, $authuser, $realcmd);
300 my $vm_config_perm_list = [
308 __PACKAGE__-
>register_method({
310 path
=> '{vmid}/config',
314 description
=> "Set container options.",
316 check
=> ['perm', '/vms/{vmid}', $vm_config_perm_list, any
=> 1],
319 additionalProperties
=> 0,
320 properties
=> PVE
::LXC
::json_config_properties
(
322 node
=> get_standard_option
('pve-node'),
323 vmid
=> get_standard_option
('pve-vmid'),
325 type
=> 'string', format
=> 'pve-configid-list',
326 description
=> "A list of settings you want to delete.",
331 description
=> 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.',
337 returns
=> { type
=> 'null'},
341 my $rpcenv = PVE
::RPCEnvironment
::get
();
343 my $authuser = $rpcenv->get_user();
345 my $node = extract_param
($param, 'node');
347 my $vmid = extract_param
($param, 'vmid');
349 my $digest = extract_param
($param, 'digest');
351 die "no options specified\n" if !scalar(keys %$param);
353 my $delete_str = extract_param
($param, 'delete');
354 my @delete = PVE
::Tools
::split_list
($delete_str);
356 &$check_ct_modify_config_perm($rpcenv, $authuser, $vmid, undef, [@delete]);
358 foreach my $opt (@delete) {
359 raise_param_exc
({ delete => "you can't use '-$opt' and " .
360 "-delete $opt' at the same time" })
361 if defined($param->{$opt});
363 if (!PVE
::LXC
::option_exists
($opt)) {
364 raise_param_exc
({ delete => "unknown option '$opt'" });
368 &$check_ct_modify_config_perm($rpcenv, $authuser, $vmid, undef, [keys %$param]);
372 my $conf = PVE
::LXC
::load_config
($vmid);
373 PVE
::LXC
::check_lock
($conf);
375 PVE
::Tools
::assert_if_modified
($digest, $conf->{digest
});
377 my $running = PVE
::LXC
::check_running
($vmid);
379 PVE
::LXC
::update_lxc_config
($vmid, $conf, $running, $param, \
@delete);
381 PVE
::LXC
::write_config
($vmid, $conf);
384 PVE
::LXC
::lock_container
($vmid, undef, $code);
389 __PACKAGE__-
>register_method ({
390 subclass
=> "PVE::API2::Firewall::CT",
391 path
=> '{vmid}/firewall',
394 __PACKAGE__-
>register_method({
399 description
=> "Directory index",
404 additionalProperties
=> 0,
406 node
=> get_standard_option
('pve-node'),
407 vmid
=> get_standard_option
('pve-vmid'),
415 subdir
=> { type
=> 'string' },
418 links
=> [ { rel
=> 'child', href
=> "{subdir}" } ],
424 my $conf = PVE
::LXC
::load_config
($param->{vmid
});
427 { subdir
=> 'config' },
428 { subdir
=> 'status' },
429 { subdir
=> 'vncproxy' },
430 { subdir
=> 'vncwebsocket' },
431 { subdir
=> 'spiceproxy' },
432 { subdir
=> 'migrate' },
433 # { subdir => 'initlog' },
435 { subdir
=> 'rrddata' },
436 { subdir
=> 'firewall' },
437 { subdir
=> 'snapshot' },
443 __PACKAGE__-
>register_method({
445 path
=> '{vmid}/rrd',
447 protected
=> 1, # fixme: can we avoid that?
449 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
451 description
=> "Read VM RRD statistics (returns PNG)",
453 additionalProperties
=> 0,
455 node
=> get_standard_option
('pve-node'),
456 vmid
=> get_standard_option
('pve-vmid'),
458 description
=> "Specify the time frame you are interested in.",
460 enum
=> [ 'hour', 'day', 'week', 'month', 'year' ],
463 description
=> "The list of datasources you want to display.",
464 type
=> 'string', format
=> 'pve-configid-list',
467 description
=> "The RRD consolidation function",
469 enum
=> [ 'AVERAGE', 'MAX' ],
477 filename
=> { type
=> 'string' },
483 return PVE
::Cluster
::create_rrd_graph
(
484 "pve2-vm/$param->{vmid}", $param->{timeframe
},
485 $param->{ds
}, $param->{cf
});
489 __PACKAGE__-
>register_method({
491 path
=> '{vmid}/rrddata',
493 protected
=> 1, # fixme: can we avoid that?
495 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
497 description
=> "Read VM RRD statistics",
499 additionalProperties
=> 0,
501 node
=> get_standard_option
('pve-node'),
502 vmid
=> get_standard_option
('pve-vmid'),
504 description
=> "Specify the time frame you are interested in.",
506 enum
=> [ 'hour', 'day', 'week', 'month', 'year' ],
509 description
=> "The RRD consolidation function",
511 enum
=> [ 'AVERAGE', 'MAX' ],
526 return PVE
::Cluster
::create_rrd_data
(
527 "pve2-vm/$param->{vmid}", $param->{timeframe
}, $param->{cf
});
531 __PACKAGE__-
>register_method({
533 path
=> '{vmid}/config',
536 description
=> "Get container configuration.",
538 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
541 additionalProperties
=> 0,
543 node
=> get_standard_option
('pve-node'),
544 vmid
=> get_standard_option
('pve-vmid'),
552 description
=> 'SHA1 digest of configuration file. This can be used to prevent concurrent modifications.',
559 my $lxc_conf = PVE
::LXC
::load_config
($param->{vmid
});
561 # NOTE: we only return selected/converted values
563 my $conf = PVE
::LXC
::lxc_conf_to_pve
($param->{vmid
}, $lxc_conf);
565 my $stcfg = PVE
::Cluster
::cfs_read_file
("storage.cfg");
567 my ($sid, undef, $path) = &$get_container_storage($stcfg, $param->{vmid
}, $lxc_conf);
568 $conf->{storage
} = $sid || $path;
573 __PACKAGE__-
>register_method({
574 name
=> 'destroy_vm',
579 description
=> "Destroy the container (also delete all uses files).",
581 check
=> [ 'perm', '/vms/{vmid}', ['VM.Allocate']],
584 additionalProperties
=> 0,
586 node
=> get_standard_option
('pve-node'),
587 vmid
=> get_standard_option
('pve-vmid'),
596 my $rpcenv = PVE
::RPCEnvironment
::get
();
598 my $authuser = $rpcenv->get_user();
600 my $vmid = $param->{vmid
};
602 # test if container exists
603 my $conf = PVE
::LXC
::load_config
($vmid);
605 my $storage_cfg = cfs_read_file
("storage.cfg");
608 # reload config after lock
609 $conf = PVE
::LXC
::load_config
($vmid);
610 PVE
::LXC
::check_lock
($conf);
612 PVE
::LXC
::destory_lxc_container
($storage_cfg, $vmid, $conf);
613 PVE
::AccessControl
::remove_vm_from_pool
($vmid);
616 my $realcmd = sub { PVE
::LXC
::lock_container
($vmid, 1, $code); };
618 return $rpcenv->fork_worker('vzdestroy', $vmid, $authuser, $realcmd);
623 __PACKAGE__-
>register_method ({
625 path
=> '{vmid}/vncproxy',
629 check
=> ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
631 description
=> "Creates a TCP VNC proxy connections.",
633 additionalProperties
=> 0,
635 node
=> get_standard_option
('pve-node'),
636 vmid
=> get_standard_option
('pve-vmid'),
640 description
=> "use websocket instead of standard VNC.",
645 additionalProperties
=> 0,
647 user
=> { type
=> 'string' },
648 ticket
=> { type
=> 'string' },
649 cert
=> { type
=> 'string' },
650 port
=> { type
=> 'integer' },
651 upid
=> { type
=> 'string' },
657 my $rpcenv = PVE
::RPCEnvironment
::get
();
659 my $authuser = $rpcenv->get_user();
661 my $vmid = $param->{vmid
};
662 my $node = $param->{node
};
664 my $authpath = "/vms/$vmid";
666 my $ticket = PVE
::AccessControl
::assemble_vnc_ticket
($authuser, $authpath);
668 $sslcert = PVE
::Tools
::file_get_contents
("/etc/pve/pve-root-ca.pem", 8192)
671 my ($remip, $family);
673 if ($node ne PVE
::INotify
::nodename
()) {
674 ($remip, $family) = PVE
::Cluster
::remote_node_ip
($node);
676 $family = PVE
::Tools
::get_host_address_family
($node);
679 my $port = PVE
::Tools
::next_vnc_port
($family);
681 # NOTE: vncterm VNC traffic is already TLS encrypted,
682 # so we select the fastest chipher here (or 'none'?)
683 my $remcmd = $remip ?
684 ['/usr/bin/ssh', '-t', $remip] : [];
686 my $shcmd = [ '/usr/bin/dtach', '-A',
687 "/var/run/dtach/vzctlconsole$vmid",
689 'lxc-console', '-n', $vmid ];
694 syslog
('info', "starting lxc vnc proxy $upid\n");
698 my $cmd = ['/usr/bin/vncterm', '-rfbport', $port,
699 '-timeout', $timeout, '-authpath', $authpath,
700 '-perm', 'VM.Console'];
702 if ($param->{websocket
}) {
703 $ENV{PVE_VNC_TICKET
} = $ticket; # pass ticket to vncterm
704 push @$cmd, '-notls', '-listen', 'localhost';
707 push @$cmd, '-c', @$remcmd, @$shcmd;
714 my $upid = $rpcenv->fork_worker('vncproxy', $vmid, $authuser, $realcmd);
716 PVE
::Tools
::wait_for_vnc_port
($port);
727 __PACKAGE__-
>register_method({
728 name
=> 'vncwebsocket',
729 path
=> '{vmid}/vncwebsocket',
732 description
=> "You also need to pass a valid ticket (vncticket).",
733 check
=> ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
735 description
=> "Opens a weksocket for VNC traffic.",
737 additionalProperties
=> 0,
739 node
=> get_standard_option
('pve-node'),
740 vmid
=> get_standard_option
('pve-vmid'),
742 description
=> "Ticket from previous call to vncproxy.",
747 description
=> "Port number returned by previous vncproxy call.",
757 port
=> { type
=> 'string' },
763 my $rpcenv = PVE
::RPCEnvironment
::get
();
765 my $authuser = $rpcenv->get_user();
767 my $authpath = "/vms/$param->{vmid}";
769 PVE
::AccessControl
::verify_vnc_ticket
($param->{vncticket
}, $authuser, $authpath);
771 my $port = $param->{port
};
773 return { port
=> $port };
776 __PACKAGE__-
>register_method ({
777 name
=> 'spiceproxy',
778 path
=> '{vmid}/spiceproxy',
783 check
=> ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
785 description
=> "Returns a SPICE configuration to connect to the CT.",
787 additionalProperties
=> 0,
789 node
=> get_standard_option
('pve-node'),
790 vmid
=> get_standard_option
('pve-vmid'),
791 proxy
=> get_standard_option
('spice-proxy', { optional
=> 1 }),
794 returns
=> get_standard_option
('remote-viewer-config'),
798 my $vmid = $param->{vmid
};
799 my $node = $param->{node
};
800 my $proxy = $param->{proxy
};
802 my $authpath = "/vms/$vmid";
803 my $permissions = 'VM.Console';
805 my $shcmd = ['/usr/bin/dtach', '-A',
806 "/var/run/dtach/vzctlconsole$vmid",
808 'lxc-console', '-n', $vmid];
810 my $title = "CT $vmid";
812 return PVE
::API2Tools
::run_spiceterm
($authpath, $permissions, $vmid, $node, $proxy, $title, $shcmd);
815 __PACKAGE__-
>register_method({
817 path
=> '{vmid}/status',
820 description
=> "Directory index",
825 additionalProperties
=> 0,
827 node
=> get_standard_option
('pve-node'),
828 vmid
=> get_standard_option
('pve-vmid'),
836 subdir
=> { type
=> 'string' },
839 links
=> [ { rel
=> 'child', href
=> "{subdir}" } ],
845 my $conf = PVE
::LXC
::load_config
($param->{vmid
});
848 { subdir
=> 'current' },
849 { subdir
=> 'start' },
850 { subdir
=> 'stop' },
851 { subdir
=> 'shutdown' },
852 { subdir
=> 'migrate' },
858 __PACKAGE__-
>register_method({
860 path
=> '{vmid}/status/current',
863 protected
=> 1, # openvz /proc entries are only readable by root
864 description
=> "Get virtual machine status.",
866 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
869 additionalProperties
=> 0,
871 node
=> get_standard_option
('pve-node'),
872 vmid
=> get_standard_option
('pve-vmid'),
875 returns
=> { type
=> 'object' },
880 my $conf = PVE
::LXC
::load_config
($param->{vmid
});
882 my $vmstatus = PVE
::LXC
::vmstatus
($param->{vmid
});
883 my $status = $vmstatus->{$param->{vmid
}};
885 $status->{ha
} = PVE
::HA
::Config
::vm_is_ha_managed
($param->{vmid
}) ?
1 : 0;
890 __PACKAGE__-
>register_method({
892 path
=> '{vmid}/status/start',
896 description
=> "Start the container.",
898 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
901 additionalProperties
=> 0,
903 node
=> get_standard_option
('pve-node'),
904 vmid
=> get_standard_option
('pve-vmid'),
913 my $rpcenv = PVE
::RPCEnvironment
::get
();
915 my $authuser = $rpcenv->get_user();
917 my $node = extract_param
($param, 'node');
919 my $vmid = extract_param
($param, 'vmid');
921 die "CT $vmid already running\n" if PVE
::LXC
::check_running
($vmid);
923 if (PVE
::HA
::Config
::vm_is_ha_managed
($vmid) && $rpcenv->{type
} ne 'ha') {
928 my $service = "ct:$vmid";
930 my $cmd = ['ha-manager', 'enable', $service];
932 print "Executing HA start for CT $vmid\n";
934 PVE
::Tools
::run_command
($cmd);
939 return $rpcenv->fork_worker('hastart', $vmid, $authuser, $hacmd);
946 syslog
('info', "starting CT $vmid: $upid\n");
948 my $conf = PVE
::LXC
::load_config
($vmid);
949 my $stcfg = cfs_read_file
("storage.cfg");
950 if (my $sid = &$get_container_storage($stcfg, $vmid, $conf)) {
951 PVE
::Storage
::activate_storage
($stcfg, $sid);
954 my $cmd = ['lxc-start', '-n', $vmid];
961 return $rpcenv->fork_worker('vzstart', $vmid, $authuser, $realcmd);
965 __PACKAGE__-
>register_method({
967 path
=> '{vmid}/status/stop',
971 description
=> "Stop the container.",
973 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
976 additionalProperties
=> 0,
978 node
=> get_standard_option
('pve-node'),
979 vmid
=> get_standard_option
('pve-vmid'),
988 my $rpcenv = PVE
::RPCEnvironment
::get
();
990 my $authuser = $rpcenv->get_user();
992 my $node = extract_param
($param, 'node');
994 my $vmid = extract_param
($param, 'vmid');
996 die "CT $vmid not running\n" if !PVE
::LXC
::check_running
($vmid);
998 if (PVE
::HA
::Config
::vm_is_ha_managed
($vmid) && $rpcenv->{type
} ne 'ha') {
1003 my $service = "ct:$vmid";
1005 my $cmd = ['ha-manager', 'disable', $service];
1007 print "Executing HA stop for CT $vmid\n";
1009 PVE
::Tools
::run_command
($cmd);
1014 return $rpcenv->fork_worker('hastop', $vmid, $authuser, $hacmd);
1021 syslog
('info', "stoping CT $vmid: $upid\n");
1023 my $cmd = ['lxc-stop', '-n', $vmid, '--kill'];
1030 return $rpcenv->fork_worker('vzstop', $vmid, $authuser, $realcmd);
1034 __PACKAGE__-
>register_method({
1035 name
=> 'vm_shutdown',
1036 path
=> '{vmid}/status/shutdown',
1040 description
=> "Shutdown the container.",
1042 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1045 additionalProperties
=> 0,
1047 node
=> get_standard_option
('pve-node'),
1048 vmid
=> get_standard_option
('pve-vmid'),
1050 description
=> "Wait maximal timeout seconds.",
1057 description
=> "Make sure the Container stops.",
1070 my $rpcenv = PVE
::RPCEnvironment
::get
();
1072 my $authuser = $rpcenv->get_user();
1074 my $node = extract_param
($param, 'node');
1076 my $vmid = extract_param
($param, 'vmid');
1078 my $timeout = extract_param
($param, 'timeout');
1080 die "CT $vmid not running\n" if !PVE
::LXC
::check_running
($vmid);
1085 syslog
('info', "shutdown CT $vmid: $upid\n");
1087 my $cmd = ['lxc-stop', '-n', $vmid];
1089 $timeout = 60 if !defined($timeout);
1091 push @$cmd, '--timeout', $timeout;
1093 eval { run_command
($cmd, timeout
=> $timeout+5); };
1097 die $err if !$param->{forceStop
};
1099 warn "shutdown failed - forcing stop now\n";
1101 push @$cmd, '--kill';
1107 my $upid = $rpcenv->fork_worker('vzshutdown', $vmid, $authuser, $realcmd);
1112 __PACKAGE__-
>register_method({
1113 name
=> 'vm_suspend',
1114 path
=> '{vmid}/status/suspend',
1118 description
=> "Suspend the container.",
1120 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1123 additionalProperties
=> 0,
1125 node
=> get_standard_option
('pve-node'),
1126 vmid
=> get_standard_option
('pve-vmid'),
1135 my $rpcenv = PVE
::RPCEnvironment
::get
();
1137 my $authuser = $rpcenv->get_user();
1139 my $node = extract_param
($param, 'node');
1141 my $vmid = extract_param
($param, 'vmid');
1143 die "CT $vmid not running\n" if !PVE
::LXC
::check_running
($vmid);
1148 syslog
('info', "suspend CT $vmid: $upid\n");
1150 my $cmd = ['lxc-checkpoint', '-n', $vmid, '-s', '-D', '/var/liv/vz/dump'];
1157 my $upid = $rpcenv->fork_worker('vzsuspend', $vmid, $authuser, $realcmd);
1162 __PACKAGE__-
>register_method({
1163 name
=> 'vm_resume',
1164 path
=> '{vmid}/status/resume',
1168 description
=> "Resume the container.",
1170 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1173 additionalProperties
=> 0,
1175 node
=> get_standard_option
('pve-node'),
1176 vmid
=> get_standard_option
('pve-vmid'),
1185 my $rpcenv = PVE
::RPCEnvironment
::get
();
1187 my $authuser = $rpcenv->get_user();
1189 my $node = extract_param
($param, 'node');
1191 my $vmid = extract_param
($param, 'vmid');
1193 die "CT $vmid already running\n" if PVE
::LXC
::check_running
($vmid);
1198 syslog
('info', "resume CT $vmid: $upid\n");
1200 my $cmd = ['lxc-checkpoint', '-n', $vmid, '-r', '--foreground',
1201 '-D', '/var/liv/vz/dump'];
1208 my $upid = $rpcenv->fork_worker('vzresume', $vmid, $authuser, $realcmd);
1213 __PACKAGE__-
>register_method({
1214 name
=> 'migrate_vm',
1215 path
=> '{vmid}/migrate',
1219 description
=> "Migrate the container to another node. Creates a new migration task.",
1221 check
=> ['perm', '/vms/{vmid}', [ 'VM.Migrate' ]],
1224 additionalProperties
=> 0,
1226 node
=> get_standard_option
('pve-node'),
1227 vmid
=> get_standard_option
('pve-vmid'),
1228 target
=> get_standard_option
('pve-node', { description
=> "Target node." }),
1231 description
=> "Use online/live migration.",
1238 description
=> "the task ID.",
1243 my $rpcenv = PVE
::RPCEnvironment
::get
();
1245 my $authuser = $rpcenv->get_user();
1247 my $target = extract_param
($param, 'target');
1249 my $localnode = PVE
::INotify
::nodename
();
1250 raise_param_exc
({ target
=> "target is local node."}) if $target eq $localnode;
1252 PVE
::Cluster
::check_cfs_quorum
();
1254 PVE
::Cluster
::check_node_exists
($target);
1256 my $targetip = PVE
::Cluster
::remote_node_ip
($target);
1258 my $vmid = extract_param
($param, 'vmid');
1261 PVE
::LXC
::load_config
($vmid);
1263 # try to detect errors early
1264 if (PVE
::LXC
::check_running
($vmid)) {
1265 die "cant migrate running container without --online\n"
1266 if !$param->{online
};
1269 if (PVE
::HA
::Config
::vm_is_ha_managed
($vmid) && $rpcenv->{type
} ne 'ha') {
1274 my $service = "ct:$vmid";
1276 my $cmd = ['ha-manager', 'migrate', $service, $target];
1278 print "Executing HA migrate for CT $vmid to node $target\n";
1280 PVE
::Tools
::run_command
($cmd);
1285 return $rpcenv->fork_worker('hamigrate', $vmid, $authuser, $hacmd);
1292 # fixme: implement lxc container migration
1293 die "lxc container migration not implemented\n";
1298 return $rpcenv->fork_worker('vzmigrate', $vmid, $authuser, $realcmd);
1302 __PACKAGE__-
>register_method({
1304 path
=> '{vmid}/snapshot',
1308 description
=> "Snapshot a container.",
1310 check
=> ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
1313 additionalProperties
=> 0,
1315 node
=> get_standard_option
('pve-node'),
1316 vmid
=> get_standard_option
('pve-vmid'),
1317 snapname
=> get_standard_option
('pve-lxc-snapshot-name'),
1321 description
=> "Save the vmstate",
1326 description
=> "A textual description or comment.",
1332 description
=> "the task ID.",
1337 my $rpcenv = PVE
::RPCEnvironment
::get
();
1339 my $authuser = $rpcenv->get_user();
1341 my $node = extract_param
($param, 'node');
1343 my $vmid = extract_param
($param, 'vmid');
1345 my $snapname = extract_param
($param, 'snapname');
1347 die "unable to use snapshot name 'current' (reserved name)\n"
1348 if $snapname eq 'current';
1351 PVE
::Cluster
::log_msg
('info', $authuser, "snapshot container $vmid: $snapname");
1352 PVE
::LXC
::snapshot_create
($vmid, $snapname, $param->{description
});
1355 return $rpcenv->fork_worker('pctsnapshot', $vmid, $authuser, $realcmd);
1358 __PACKAGE__-
>register_method({
1359 name
=> 'delsnapshot',
1360 path
=> '{vmid}/snapshot/{snapname}',
1364 description
=> "Delete a LXC snapshot.",
1366 check
=> ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
1369 additionalProperties
=> 0,
1371 node
=> get_standard_option
('pve-node'),
1372 vmid
=> get_standard_option
('pve-vmid'),
1373 snapname
=> get_standard_option
('pve-lxc-snapshot-name'),
1377 description
=> "For removal from config file, even if removing disk snapshots fails.",
1383 description
=> "the task ID.",
1388 my $rpcenv = PVE
::RPCEnvironment
::get
();
1390 my $authuser = $rpcenv->get_user();
1392 my $node = extract_param
($param, 'node');
1394 my $vmid = extract_param
($param, 'vmid');
1396 my $snapname = extract_param
($param, 'snapname');
1399 PVE
::Cluster
::log_msg
('info', $authuser, "delete snapshot VM $vmid: $snapname");
1400 PVE
::LXC
::snapshot_delete
($vmid, $snapname, $param->{force
});
1403 return $rpcenv->fork_worker('lxcdelsnapshot', $vmid, $authuser, $realcmd);
1406 __PACKAGE__-
>register_method({
1408 path
=> '{vmid}/snapshot/{snapname}/rollback',
1412 description
=> "Rollback LXC state to specified snapshot.",
1414 check
=> ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
1417 additionalProperties
=> 0,
1419 node
=> get_standard_option
('pve-node'),
1420 vmid
=> get_standard_option
('pve-vmid'),
1421 snapname
=> get_standard_option
('pve-lxc-snapshot-name'),
1426 description
=> "the task ID.",
1431 my $rpcenv = PVE
::RPCEnvironment
::get
();
1433 my $authuser = $rpcenv->get_user();
1435 my $node = extract_param
($param, 'node');
1437 my $vmid = extract_param
($param, 'vmid');
1439 my $snapname = extract_param
($param, 'snapname');
1442 PVE
::Cluster
::log_msg
('info', $authuser, "rollback snapshot LXC $vmid: $snapname");
1443 PVE
::LXC
::snapshot_rollback
($vmid, $snapname);
1446 return $rpcenv->fork_worker('lxcrollback', $vmid, $authuser, $realcmd);
1449 __PACKAGE__-
>register_method({
1450 name
=> 'snapshot_list',
1451 path
=> '{vmid}/snapshot',
1453 description
=> "List all snapshots.",
1455 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
1458 protected
=> 1, # lxc pid files are only readable by root
1460 additionalProperties
=> 0,
1462 vmid
=> get_standard_option
('pve-vmid'),
1463 node
=> get_standard_option
('pve-node'),
1472 links
=> [ { rel
=> 'child', href
=> "{name}" } ],
1477 my $vmid = $param->{vmid
};
1479 my $conf = PVE
::LXC
::load_config
($vmid);
1480 my $snaphash = $conf->{snapshots
} || {};
1484 foreach my $name (keys %$snaphash) {
1485 my $d = $snaphash->{$name};
1488 snaptime
=> $d->{'pve.snaptime'} || 0,
1489 description
=> $d->{'pve.snapcomment'} || '',
1491 $item->{parent
} = $d->{'pve.parent'} if $d->{'pve.parent'};
1492 $item->{snapstate
} = $d->{'pve.snapstate'} if $d->{'pve.snapstate'};
1496 my $running = PVE
::LXC
::check_running
($vmid) ?
1 : 0;
1497 my $current = { name
=> 'current', digest
=> $conf->{digest
}, running
=> $running };
1498 $current->{parent
} = $conf->{'pve.parent'} if $conf->{'pve.parent'};
1500 push @$res, $current;
1505 __PACKAGE__-
>register_method({
1506 name
=> 'snapshot_cmd_idx',
1507 path
=> '{vmid}/snapshot/{snapname}',
1514 additionalProperties
=> 0,
1516 vmid
=> get_standard_option
('pve-vmid'),
1517 node
=> get_standard_option
('pve-node'),
1518 snapname
=> get_standard_option
('pve-lxc-snapshot-name'),
1527 links
=> [ { rel
=> 'child', href
=> "{cmd}" } ],
1534 push @$res, { cmd
=> 'rollback' };
1535 push @$res, { cmd
=> 'config' };
1540 __PACKAGE__-
>register_method({
1541 name
=> 'update_snapshot_config',
1542 path
=> '{vmid}/snapshot/{snapname}/config',
1546 description
=> "Update snapshot metadata.",
1548 check
=> ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
1551 additionalProperties
=> 0,
1553 node
=> get_standard_option
('pve-node'),
1554 vmid
=> get_standard_option
('pve-vmid'),
1555 snapname
=> get_standard_option
('pve-lxc-snapshot-name'),
1559 description
=> "A textual description or comment.",
1563 returns
=> { type
=> 'null' },
1567 my $rpcenv = PVE
::RPCEnvironment
::get
();
1569 my $authuser = $rpcenv->get_user();
1571 my $vmid = extract_param
($param, 'vmid');
1573 my $snapname = extract_param
($param, 'snapname');
1575 return undef if !defined($param->{description
});
1577 my $updatefn = sub {
1579 my $conf = PVE
::LXC
::load_config
($vmid);
1580 PVE
::LXC
::check_lock
($conf);
1582 my $snap = $conf->{snapshots
}->{$snapname};
1584 die "snapshot '$snapname' does not exist\n" if !defined($snap);
1586 $snap->{'pve.snapcomment'} = $param->{description
} if defined($param->{description
});
1588 PVE
::LXC
::write_config
($vmid, $conf, 1);
1591 PVE
::LXC
::lock_container
($vmid, 10, $updatefn);
1596 __PACKAGE__-
>register_method({
1597 name
=> 'get_snapshot_config',
1598 path
=> '{vmid}/snapshot/{snapname}/config',
1601 description
=> "Get snapshot configuration",
1603 check
=> ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
1606 additionalProperties
=> 0,
1608 node
=> get_standard_option
('pve-node'),
1609 vmid
=> get_standard_option
('pve-vmid'),
1610 snapname
=> get_standard_option
('pve-lxc-snapshot-name'),
1613 returns
=> { type
=> "object" },
1617 my $rpcenv = PVE
::RPCEnvironment
::get
();
1619 my $authuser = $rpcenv->get_user();
1621 my $vmid = extract_param
($param, 'vmid');
1623 my $snapname = extract_param
($param, 'snapname');
1625 my $lxc_conf = PVE
::LXC
::load_config
($vmid);
1627 my $snap = $lxc_conf->{snapshots
}->{$snapname};
1629 die "snapshot '$snapname' does not exist\n" if !defined($snap);
1631 my $conf = PVE
::LXC
::lxc_conf_to_pve
($param->{vmid
}, $snap);
1636 __PACKAGE__-
>register_method({
1637 name
=> 'vm_feature',
1638 path
=> '{vmid}/feature',
1642 description
=> "Check if feature for virtual machine is available.",
1644 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
1647 additionalProperties
=> 0,
1649 node
=> get_standard_option
('pve-node'),
1650 vmid
=> get_standard_option
('pve-vmid'),
1652 description
=> "Feature to check.",
1654 enum
=> [ 'snapshot' ],
1656 snapname
=> get_standard_option
('pve-lxc-snapshot-name', {
1664 hasFeature
=> { type
=> 'boolean' },
1667 #items => { type => 'string' },
1674 my $node = extract_param
($param, 'node');
1676 my $vmid = extract_param
($param, 'vmid');
1678 my $snapname = extract_param
($param, 'snapname');
1680 my $feature = extract_param
($param, 'feature');
1682 my $conf = PVE
::LXC
::load_config
($vmid);
1685 my $snap = $conf->{snapshots
}->{$snapname};
1686 die "snapshot '$snapname' does not exist\n" if !defined($snap);
1689 my $storecfg = PVE
::Storage
::config
();
1690 #Maybe include later
1691 #my $nodelist = PVE::LXC::shared_nodes($conf, $storecfg);
1692 my $hasFeature = PVE
::LXC
::has_feature
($feature, $conf, $storecfg, $snapname);
1695 hasFeature
=> $hasFeature,
1696 #nodes => [ keys %$nodelist ],