]>
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);
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 &$check_vmid_usage(); # final check after locking
279 PVE
::Cluster
::check_cfs_quorum
();
281 PVE
::LXCCreate
::create_rootfs
($storage_cfg, $storage, $param->{disk
}, $vmid, $conf,
282 $archive, $password, $restore);
285 my $realcmd = sub { PVE
::LXC
::lock_container
($vmid, 1, $code); };
287 &$check_vmid_usage(); # first check before locking
289 return $rpcenv->fork_worker($restore ?
'vzrestore' : 'vzcreate',
290 $vmid, $authuser, $realcmd);
294 my $vm_config_perm_list = [
302 __PACKAGE__-
>register_method({
304 path
=> '{vmid}/config',
308 description
=> "Set container options.",
310 check
=> ['perm', '/vms/{vmid}', $vm_config_perm_list, any
=> 1],
313 additionalProperties
=> 0,
314 properties
=> PVE
::LXC
::json_config_properties
(
316 node
=> get_standard_option
('pve-node'),
317 vmid
=> get_standard_option
('pve-vmid'),
319 type
=> 'string', format
=> 'pve-configid-list',
320 description
=> "A list of settings you want to delete.",
325 description
=> 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.',
331 returns
=> { type
=> 'null'},
335 my $rpcenv = PVE
::RPCEnvironment
::get
();
337 my $authuser = $rpcenv->get_user();
339 my $node = extract_param
($param, 'node');
341 my $vmid = extract_param
($param, 'vmid');
343 my $digest = extract_param
($param, 'digest');
345 die "no options specified\n" if !scalar(keys %$param);
347 my $delete_str = extract_param
($param, 'delete');
348 my @delete = PVE
::Tools
::split_list
($delete_str);
350 &$check_ct_modify_config_perm($rpcenv, $authuser, $vmid, undef, [@delete]);
352 foreach my $opt (@delete) {
353 raise_param_exc
({ delete => "you can't use '-$opt' and " .
354 "-delete $opt' at the same time" })
355 if defined($param->{$opt});
357 if (!PVE
::LXC
::option_exists
($opt)) {
358 raise_param_exc
({ delete => "unknown option '$opt'" });
362 &$check_ct_modify_config_perm($rpcenv, $authuser, $vmid, undef, [keys %$param]);
366 my $conf = PVE
::LXC
::load_config
($vmid);
367 PVE
::LXC
::check_lock
($conf);
369 PVE
::Tools
::assert_if_modified
($digest, $conf->{digest
});
371 my $running = PVE
::LXC
::check_running
($vmid);
373 PVE
::LXC
::update_lxc_config
($vmid, $conf, $running, $param, \
@delete);
375 PVE
::LXC
::write_config
($vmid, $conf);
378 PVE
::LXC
::lock_container
($vmid, undef, $code);
383 __PACKAGE__-
>register_method ({
384 subclass
=> "PVE::API2::Firewall::CT",
385 path
=> '{vmid}/firewall',
388 __PACKAGE__-
>register_method({
393 description
=> "Directory index",
398 additionalProperties
=> 0,
400 node
=> get_standard_option
('pve-node'),
401 vmid
=> get_standard_option
('pve-vmid'),
409 subdir
=> { type
=> 'string' },
412 links
=> [ { rel
=> 'child', href
=> "{subdir}" } ],
418 my $conf = PVE
::LXC
::load_config
($param->{vmid
});
421 { subdir
=> 'config' },
422 { subdir
=> 'status' },
423 { subdir
=> 'vncproxy' },
424 { subdir
=> 'vncwebsocket' },
425 { subdir
=> 'spiceproxy' },
426 { subdir
=> 'migrate' },
427 # { subdir => 'initlog' },
429 { subdir
=> 'rrddata' },
430 { subdir
=> 'firewall' },
431 { subdir
=> 'snapshot' },
437 __PACKAGE__-
>register_method({
439 path
=> '{vmid}/rrd',
441 protected
=> 1, # fixme: can we avoid that?
443 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
445 description
=> "Read VM RRD statistics (returns PNG)",
447 additionalProperties
=> 0,
449 node
=> get_standard_option
('pve-node'),
450 vmid
=> get_standard_option
('pve-vmid'),
452 description
=> "Specify the time frame you are interested in.",
454 enum
=> [ 'hour', 'day', 'week', 'month', 'year' ],
457 description
=> "The list of datasources you want to display.",
458 type
=> 'string', format
=> 'pve-configid-list',
461 description
=> "The RRD consolidation function",
463 enum
=> [ 'AVERAGE', 'MAX' ],
471 filename
=> { type
=> 'string' },
477 return PVE
::Cluster
::create_rrd_graph
(
478 "pve2-vm/$param->{vmid}", $param->{timeframe
},
479 $param->{ds
}, $param->{cf
});
483 __PACKAGE__-
>register_method({
485 path
=> '{vmid}/rrddata',
487 protected
=> 1, # fixme: can we avoid that?
489 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
491 description
=> "Read VM RRD statistics",
493 additionalProperties
=> 0,
495 node
=> get_standard_option
('pve-node'),
496 vmid
=> get_standard_option
('pve-vmid'),
498 description
=> "Specify the time frame you are interested in.",
500 enum
=> [ 'hour', 'day', 'week', 'month', 'year' ],
503 description
=> "The RRD consolidation function",
505 enum
=> [ 'AVERAGE', 'MAX' ],
520 return PVE
::Cluster
::create_rrd_data
(
521 "pve2-vm/$param->{vmid}", $param->{timeframe
}, $param->{cf
});
525 __PACKAGE__-
>register_method({
527 path
=> '{vmid}/config',
530 description
=> "Get container configuration.",
532 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
535 additionalProperties
=> 0,
537 node
=> get_standard_option
('pve-node'),
538 vmid
=> get_standard_option
('pve-vmid'),
546 description
=> 'SHA1 digest of configuration file. This can be used to prevent concurrent modifications.',
553 my $lxc_conf = PVE
::LXC
::load_config
($param->{vmid
});
555 # NOTE: we only return selected/converted values
557 my $conf = PVE
::LXC
::lxc_conf_to_pve
($param->{vmid
}, $lxc_conf);
559 my $stcfg = PVE
::Cluster
::cfs_read_file
("storage.cfg");
561 my ($sid, undef, $path) = &$get_container_storage($stcfg, $param->{vmid
}, $lxc_conf);
562 $conf->{storage
} = $sid || $path;
567 __PACKAGE__-
>register_method({
568 name
=> 'destroy_vm',
573 description
=> "Destroy the container (also delete all uses files).",
575 check
=> [ 'perm', '/vms/{vmid}', ['VM.Allocate']],
578 additionalProperties
=> 0,
580 node
=> get_standard_option
('pve-node'),
581 vmid
=> get_standard_option
('pve-vmid'),
590 my $rpcenv = PVE
::RPCEnvironment
::get
();
592 my $authuser = $rpcenv->get_user();
594 my $vmid = $param->{vmid
};
596 # test if container exists
597 my $conf = PVE
::LXC
::load_config
($vmid);
599 my $storage_cfg = cfs_read_file
("storage.cfg");
602 # reload config after lock
603 $conf = PVE
::LXC
::load_config
($vmid);
604 PVE
::LXC
::check_lock
($conf);
606 PVE
::LXC
::destory_lxc_container
($storage_cfg, $vmid, $conf);
607 PVE
::AccessControl
::remove_vm_from_pool
($vmid);
610 my $realcmd = sub { PVE
::LXC
::lock_container
($vmid, 1, $code); };
612 return $rpcenv->fork_worker('vzdestroy', $vmid, $authuser, $realcmd);
617 __PACKAGE__-
>register_method ({
619 path
=> '{vmid}/vncproxy',
623 check
=> ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
625 description
=> "Creates a TCP VNC proxy connections.",
627 additionalProperties
=> 0,
629 node
=> get_standard_option
('pve-node'),
630 vmid
=> get_standard_option
('pve-vmid'),
634 description
=> "use websocket instead of standard VNC.",
639 additionalProperties
=> 0,
641 user
=> { type
=> 'string' },
642 ticket
=> { type
=> 'string' },
643 cert
=> { type
=> 'string' },
644 port
=> { type
=> 'integer' },
645 upid
=> { type
=> 'string' },
651 my $rpcenv = PVE
::RPCEnvironment
::get
();
653 my $authuser = $rpcenv->get_user();
655 my $vmid = $param->{vmid
};
656 my $node = $param->{node
};
658 my $authpath = "/vms/$vmid";
660 my $ticket = PVE
::AccessControl
::assemble_vnc_ticket
($authuser, $authpath);
662 $sslcert = PVE
::Tools
::file_get_contents
("/etc/pve/pve-root-ca.pem", 8192)
665 my ($remip, $family);
667 if ($node ne PVE
::INotify
::nodename
()) {
668 ($remip, $family) = PVE
::Cluster
::remote_node_ip
($node);
670 $family = PVE
::Tools
::get_host_address_family
($node);
673 my $port = PVE
::Tools
::next_vnc_port
($family);
675 # NOTE: vncterm VNC traffic is already TLS encrypted,
676 # so we select the fastest chipher here (or 'none'?)
677 my $remcmd = $remip ?
678 ['/usr/bin/ssh', '-t', $remip] : [];
680 my $shcmd = [ '/usr/bin/dtach', '-A',
681 "/var/run/dtach/vzctlconsole$vmid",
683 'lxc-console', '-n', $vmid ];
688 syslog
('info', "starting lxc vnc proxy $upid\n");
692 my $cmd = ['/usr/bin/vncterm', '-rfbport', $port,
693 '-timeout', $timeout, '-authpath', $authpath,
694 '-perm', 'VM.Console'];
696 if ($param->{websocket
}) {
697 $ENV{PVE_VNC_TICKET
} = $ticket; # pass ticket to vncterm
698 push @$cmd, '-notls', '-listen', 'localhost';
701 push @$cmd, '-c', @$remcmd, @$shcmd;
708 my $upid = $rpcenv->fork_worker('vncproxy', $vmid, $authuser, $realcmd);
710 PVE
::Tools
::wait_for_vnc_port
($port);
721 __PACKAGE__-
>register_method({
722 name
=> 'vncwebsocket',
723 path
=> '{vmid}/vncwebsocket',
726 description
=> "You also need to pass a valid ticket (vncticket).",
727 check
=> ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
729 description
=> "Opens a weksocket for VNC traffic.",
731 additionalProperties
=> 0,
733 node
=> get_standard_option
('pve-node'),
734 vmid
=> get_standard_option
('pve-vmid'),
736 description
=> "Ticket from previous call to vncproxy.",
741 description
=> "Port number returned by previous vncproxy call.",
751 port
=> { type
=> 'string' },
757 my $rpcenv = PVE
::RPCEnvironment
::get
();
759 my $authuser = $rpcenv->get_user();
761 my $authpath = "/vms/$param->{vmid}";
763 PVE
::AccessControl
::verify_vnc_ticket
($param->{vncticket
}, $authuser, $authpath);
765 my $port = $param->{port
};
767 return { port
=> $port };
770 __PACKAGE__-
>register_method ({
771 name
=> 'spiceproxy',
772 path
=> '{vmid}/spiceproxy',
777 check
=> ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
779 description
=> "Returns a SPICE configuration to connect to the CT.",
781 additionalProperties
=> 0,
783 node
=> get_standard_option
('pve-node'),
784 vmid
=> get_standard_option
('pve-vmid'),
785 proxy
=> get_standard_option
('spice-proxy', { optional
=> 1 }),
788 returns
=> get_standard_option
('remote-viewer-config'),
792 my $vmid = $param->{vmid
};
793 my $node = $param->{node
};
794 my $proxy = $param->{proxy
};
796 my $authpath = "/vms/$vmid";
797 my $permissions = 'VM.Console';
799 my $shcmd = ['/usr/bin/dtach', '-A',
800 "/var/run/dtach/vzctlconsole$vmid",
802 'lxc-console', '-n', $vmid];
804 my $title = "CT $vmid";
806 return PVE
::API2Tools
::run_spiceterm
($authpath, $permissions, $vmid, $node, $proxy, $title, $shcmd);
809 __PACKAGE__-
>register_method({
811 path
=> '{vmid}/status',
814 description
=> "Directory index",
819 additionalProperties
=> 0,
821 node
=> get_standard_option
('pve-node'),
822 vmid
=> get_standard_option
('pve-vmid'),
830 subdir
=> { type
=> 'string' },
833 links
=> [ { rel
=> 'child', href
=> "{subdir}" } ],
839 my $conf = PVE
::LXC
::load_config
($param->{vmid
});
842 { subdir
=> 'current' },
843 { subdir
=> 'start' },
844 { subdir
=> 'stop' },
845 { subdir
=> 'shutdown' },
846 { subdir
=> 'migrate' },
852 __PACKAGE__-
>register_method({
854 path
=> '{vmid}/status/current',
857 protected
=> 1, # openvz /proc entries are only readable by root
858 description
=> "Get virtual machine status.",
860 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
863 additionalProperties
=> 0,
865 node
=> get_standard_option
('pve-node'),
866 vmid
=> get_standard_option
('pve-vmid'),
869 returns
=> { type
=> 'object' },
874 my $conf = PVE
::LXC
::load_config
($param->{vmid
});
876 my $vmstatus = PVE
::LXC
::vmstatus
($param->{vmid
});
877 my $status = $vmstatus->{$param->{vmid
}};
879 $status->{ha
} = PVE
::HA
::Config
::vm_is_ha_managed
($param->{vmid
}) ?
1 : 0;
884 __PACKAGE__-
>register_method({
886 path
=> '{vmid}/status/start',
890 description
=> "Start the container.",
892 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
895 additionalProperties
=> 0,
897 node
=> get_standard_option
('pve-node'),
898 vmid
=> get_standard_option
('pve-vmid'),
907 my $rpcenv = PVE
::RPCEnvironment
::get
();
909 my $authuser = $rpcenv->get_user();
911 my $node = extract_param
($param, 'node');
913 my $vmid = extract_param
($param, 'vmid');
915 die "CT $vmid already running\n" if PVE
::LXC
::check_running
($vmid);
917 if (PVE
::HA
::Config
::vm_is_ha_managed
($vmid) && $rpcenv->{type
} ne 'ha') {
922 my $service = "ct:$vmid";
924 my $cmd = ['ha-manager', 'enable', $service];
926 print "Executing HA start for CT $vmid\n";
928 PVE
::Tools
::run_command
($cmd);
933 return $rpcenv->fork_worker('hastart', $vmid, $authuser, $hacmd);
940 syslog
('info', "starting CT $vmid: $upid\n");
942 my $conf = PVE
::LXC
::load_config
($vmid);
943 my $stcfg = cfs_read_file
("storage.cfg");
944 if (my $sid = &$get_container_storage($stcfg, $vmid, $conf)) {
945 PVE
::Storage
::activate_storage
($stcfg, $sid);
948 my $cmd = ['lxc-start', '-n', $vmid];
955 return $rpcenv->fork_worker('vzstart', $vmid, $authuser, $realcmd);
959 __PACKAGE__-
>register_method({
961 path
=> '{vmid}/status/stop',
965 description
=> "Stop the container.",
967 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
970 additionalProperties
=> 0,
972 node
=> get_standard_option
('pve-node'),
973 vmid
=> get_standard_option
('pve-vmid'),
982 my $rpcenv = PVE
::RPCEnvironment
::get
();
984 my $authuser = $rpcenv->get_user();
986 my $node = extract_param
($param, 'node');
988 my $vmid = extract_param
($param, 'vmid');
990 die "CT $vmid not running\n" if !PVE
::LXC
::check_running
($vmid);
992 if (PVE
::HA
::Config
::vm_is_ha_managed
($vmid) && $rpcenv->{type
} ne 'ha') {
997 my $service = "ct:$vmid";
999 my $cmd = ['ha-manager', 'disable', $service];
1001 print "Executing HA stop for CT $vmid\n";
1003 PVE
::Tools
::run_command
($cmd);
1008 return $rpcenv->fork_worker('hastop', $vmid, $authuser, $hacmd);
1015 syslog
('info', "stoping CT $vmid: $upid\n");
1017 my $cmd = ['lxc-stop', '-n', $vmid, '--kill'];
1024 return $rpcenv->fork_worker('vzstop', $vmid, $authuser, $realcmd);
1028 __PACKAGE__-
>register_method({
1029 name
=> 'vm_shutdown',
1030 path
=> '{vmid}/status/shutdown',
1034 description
=> "Shutdown the container.",
1036 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1039 additionalProperties
=> 0,
1041 node
=> get_standard_option
('pve-node'),
1042 vmid
=> get_standard_option
('pve-vmid'),
1044 description
=> "Wait maximal timeout seconds.",
1051 description
=> "Make sure the Container stops.",
1064 my $rpcenv = PVE
::RPCEnvironment
::get
();
1066 my $authuser = $rpcenv->get_user();
1068 my $node = extract_param
($param, 'node');
1070 my $vmid = extract_param
($param, 'vmid');
1072 my $timeout = extract_param
($param, 'timeout');
1074 die "CT $vmid not running\n" if !PVE
::LXC
::check_running
($vmid);
1079 syslog
('info', "shutdown CT $vmid: $upid\n");
1081 my $cmd = ['lxc-stop', '-n', $vmid];
1083 $timeout = 60 if !defined($timeout);
1085 push @$cmd, '--timeout', $timeout;
1087 eval { run_command
($cmd, timeout
=> $timeout+5); };
1091 die $err if !$param->{forceStop
};
1093 warn "shutdown failed - forcing stop now\n";
1095 push @$cmd, '--kill';
1101 my $upid = $rpcenv->fork_worker('vzshutdown', $vmid, $authuser, $realcmd);
1106 __PACKAGE__-
>register_method({
1107 name
=> 'vm_suspend',
1108 path
=> '{vmid}/status/suspend',
1112 description
=> "Suspend the container.",
1114 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1117 additionalProperties
=> 0,
1119 node
=> get_standard_option
('pve-node'),
1120 vmid
=> get_standard_option
('pve-vmid'),
1129 my $rpcenv = PVE
::RPCEnvironment
::get
();
1131 my $authuser = $rpcenv->get_user();
1133 my $node = extract_param
($param, 'node');
1135 my $vmid = extract_param
($param, 'vmid');
1137 die "CT $vmid not running\n" if !PVE
::LXC
::check_running
($vmid);
1142 syslog
('info', "suspend CT $vmid: $upid\n");
1144 my $cmd = ['lxc-checkpoint', '-n', $vmid, '-s', '-D', '/var/liv/vz/dump'];
1151 my $upid = $rpcenv->fork_worker('vzsuspend', $vmid, $authuser, $realcmd);
1156 __PACKAGE__-
>register_method({
1157 name
=> 'vm_resume',
1158 path
=> '{vmid}/status/resume',
1162 description
=> "Resume the container.",
1164 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1167 additionalProperties
=> 0,
1169 node
=> get_standard_option
('pve-node'),
1170 vmid
=> get_standard_option
('pve-vmid'),
1179 my $rpcenv = PVE
::RPCEnvironment
::get
();
1181 my $authuser = $rpcenv->get_user();
1183 my $node = extract_param
($param, 'node');
1185 my $vmid = extract_param
($param, 'vmid');
1187 die "CT $vmid already running\n" if PVE
::LXC
::check_running
($vmid);
1192 syslog
('info', "resume CT $vmid: $upid\n");
1194 my $cmd = ['lxc-checkpoint', '-n', $vmid, '-r', '--foreground',
1195 '-D', '/var/liv/vz/dump'];
1202 my $upid = $rpcenv->fork_worker('vzresume', $vmid, $authuser, $realcmd);
1207 __PACKAGE__-
>register_method({
1208 name
=> 'migrate_vm',
1209 path
=> '{vmid}/migrate',
1213 description
=> "Migrate the container to another node. Creates a new migration task.",
1215 check
=> ['perm', '/vms/{vmid}', [ 'VM.Migrate' ]],
1218 additionalProperties
=> 0,
1220 node
=> get_standard_option
('pve-node'),
1221 vmid
=> get_standard_option
('pve-vmid'),
1222 target
=> get_standard_option
('pve-node', { description
=> "Target node." }),
1225 description
=> "Use online/live migration.",
1232 description
=> "the task ID.",
1237 my $rpcenv = PVE
::RPCEnvironment
::get
();
1239 my $authuser = $rpcenv->get_user();
1241 my $target = extract_param
($param, 'target');
1243 my $localnode = PVE
::INotify
::nodename
();
1244 raise_param_exc
({ target
=> "target is local node."}) if $target eq $localnode;
1246 PVE
::Cluster
::check_cfs_quorum
();
1248 PVE
::Cluster
::check_node_exists
($target);
1250 my $targetip = PVE
::Cluster
::remote_node_ip
($target);
1252 my $vmid = extract_param
($param, 'vmid');
1255 PVE
::LXC
::load_config
($vmid);
1257 # try to detect errors early
1258 if (PVE
::LXC
::check_running
($vmid)) {
1259 die "cant migrate running container without --online\n"
1260 if !$param->{online
};
1263 if (PVE
::HA
::Config
::vm_is_ha_managed
($vmid) && $rpcenv->{type
} ne 'ha') {
1268 my $service = "ct:$vmid";
1270 my $cmd = ['ha-manager', 'migrate', $service, $target];
1272 print "Executing HA migrate for CT $vmid to node $target\n";
1274 PVE
::Tools
::run_command
($cmd);
1279 return $rpcenv->fork_worker('hamigrate', $vmid, $authuser, $hacmd);
1286 # fixme: implement lxc container migration
1287 die "lxc container migration not implemented\n";
1292 return $rpcenv->fork_worker('vzmigrate', $vmid, $authuser, $realcmd);
1296 __PACKAGE__-
>register_method({
1298 path
=> '{vmid}/snapshot',
1302 description
=> "Snapshot a container.",
1304 check
=> ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
1307 additionalProperties
=> 0,
1309 node
=> get_standard_option
('pve-node'),
1310 vmid
=> get_standard_option
('pve-vmid'),
1311 snapname
=> get_standard_option
('pve-lxc-snapshot-name'),
1315 description
=> "Save the vmstate",
1320 description
=> "A textual description or comment.",
1326 description
=> "the task ID.",
1331 my $rpcenv = PVE
::RPCEnvironment
::get
();
1333 my $authuser = $rpcenv->get_user();
1335 my $node = extract_param
($param, 'node');
1337 my $vmid = extract_param
($param, 'vmid');
1339 my $snapname = extract_param
($param, 'snapname');
1341 die "unable to use snapshot name 'current' (reserved name)\n"
1342 if $snapname eq 'current';
1345 PVE
::Cluster
::log_msg
('info', $authuser, "snapshot container $vmid: $snapname");
1346 PVE
::LXC
::snapshot_create
($vmid, $snapname, $param->{description
});
1349 return $rpcenv->fork_worker('pctsnapshot', $vmid, $authuser, $realcmd);
1352 __PACKAGE__-
>register_method({
1353 name
=> 'delsnapshot',
1354 path
=> '{vmid}/snapshot/{snapname}',
1358 description
=> "Delete a LXC snapshot.",
1360 check
=> ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
1363 additionalProperties
=> 0,
1365 node
=> get_standard_option
('pve-node'),
1366 vmid
=> get_standard_option
('pve-vmid'),
1367 snapname
=> get_standard_option
('pve-lxc-snapshot-name'),
1371 description
=> "For removal from config file, even if removing disk snapshots fails.",
1377 description
=> "the task ID.",
1382 my $rpcenv = PVE
::RPCEnvironment
::get
();
1384 my $authuser = $rpcenv->get_user();
1386 my $node = extract_param
($param, 'node');
1388 my $vmid = extract_param
($param, 'vmid');
1390 my $snapname = extract_param
($param, 'snapname');
1393 PVE
::Cluster
::log_msg
('info', $authuser, "delete snapshot VM $vmid: $snapname");
1394 PVE
::LXC
::snapshot_delete
($vmid, $snapname, $param->{force
});
1397 return $rpcenv->fork_worker('lxcdelsnapshot', $vmid, $authuser, $realcmd);
1400 __PACKAGE__-
>register_method({
1402 path
=> '{vmid}/snapshot/{snapname}/rollback',
1406 description
=> "Rollback LXC state to specified snapshot.",
1408 check
=> ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
1411 additionalProperties
=> 0,
1413 node
=> get_standard_option
('pve-node'),
1414 vmid
=> get_standard_option
('pve-vmid'),
1415 snapname
=> get_standard_option
('pve-lxc-snapshot-name'),
1420 description
=> "the task ID.",
1425 my $rpcenv = PVE
::RPCEnvironment
::get
();
1427 my $authuser = $rpcenv->get_user();
1429 my $node = extract_param
($param, 'node');
1431 my $vmid = extract_param
($param, 'vmid');
1433 my $snapname = extract_param
($param, 'snapname');
1436 PVE
::Cluster
::log_msg
('info', $authuser, "rollback snapshot LXC $vmid: $snapname");
1437 PVE
::LXC
::snapshot_rollback
($vmid, $snapname);
1440 return $rpcenv->fork_worker('lxcrollback', $vmid, $authuser, $realcmd);
1443 __PACKAGE__-
>register_method({
1444 name
=> 'snapshot_list',
1445 path
=> '{vmid}/snapshot',
1447 description
=> "List all snapshots.",
1449 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
1452 protected
=> 1, # lxc pid files are only readable by root
1454 additionalProperties
=> 0,
1456 vmid
=> get_standard_option
('pve-vmid'),
1457 node
=> get_standard_option
('pve-node'),
1466 links
=> [ { rel
=> 'child', href
=> "{name}" } ],
1471 my $vmid = $param->{vmid
};
1473 my $conf = PVE
::LXC
::load_config
($vmid);
1474 my $snaphash = $conf->{snapshots
} || {};
1478 foreach my $name (keys %$snaphash) {
1479 my $d = $snaphash->{$name};
1482 snaptime
=> $d->{'pve.snaptime'} || 0,
1483 description
=> $d->{'pve.snapcomment'} || '',
1485 $item->{parent
} = $d->{'pve.parent'} if $d->{'pve.parent'};
1486 $item->{snapstate
} = $d->{'pve.snapstate'} if $d->{'pve.snapstate'};
1490 my $running = PVE
::LXC
::check_running
($vmid) ?
1 : 0;
1491 my $current = { name
=> 'current', digest
=> $conf->{digest
}, running
=> $running };
1492 $current->{parent
} = $conf->{'pve.parent'} if $conf->{'pve.parent'};
1494 push @$res, $current;
1499 __PACKAGE__-
>register_method({
1500 name
=> 'snapshot_cmd_idx',
1501 path
=> '{vmid}/snapshot/{snapname}',
1508 additionalProperties
=> 0,
1510 vmid
=> get_standard_option
('pve-vmid'),
1511 node
=> get_standard_option
('pve-node'),
1512 snapname
=> get_standard_option
('pve-lxc-snapshot-name'),
1521 links
=> [ { rel
=> 'child', href
=> "{cmd}" } ],
1528 push @$res, { cmd
=> 'rollback' };
1529 push @$res, { cmd
=> 'config' };
1534 __PACKAGE__-
>register_method({
1535 name
=> 'update_snapshot_config',
1536 path
=> '{vmid}/snapshot/{snapname}/config',
1540 description
=> "Update snapshot metadata.",
1542 check
=> ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
1545 additionalProperties
=> 0,
1547 node
=> get_standard_option
('pve-node'),
1548 vmid
=> get_standard_option
('pve-vmid'),
1549 snapname
=> get_standard_option
('pve-lxc-snapshot-name'),
1553 description
=> "A textual description or comment.",
1557 returns
=> { type
=> 'null' },
1561 my $rpcenv = PVE
::RPCEnvironment
::get
();
1563 my $authuser = $rpcenv->get_user();
1565 my $vmid = extract_param
($param, 'vmid');
1567 my $snapname = extract_param
($param, 'snapname');
1569 return undef if !defined($param->{description
});
1571 my $updatefn = sub {
1573 my $conf = PVE
::LXC
::load_config
($vmid);
1574 PVE
::LXC
::check_lock
($conf);
1576 my $snap = $conf->{snapshots
}->{$snapname};
1578 die "snapshot '$snapname' does not exist\n" if !defined($snap);
1580 $snap->{'pve.snapcomment'} = $param->{description
} if defined($param->{description
});
1582 PVE
::LXC
::write_config
($vmid, $conf, 1);
1585 PVE
::LXC
::lock_container
($vmid, 10, $updatefn);
1590 __PACKAGE__-
>register_method({
1591 name
=> 'get_snapshot_config',
1592 path
=> '{vmid}/snapshot/{snapname}/config',
1595 description
=> "Get snapshot configuration",
1597 check
=> ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
1600 additionalProperties
=> 0,
1602 node
=> get_standard_option
('pve-node'),
1603 vmid
=> get_standard_option
('pve-vmid'),
1604 snapname
=> get_standard_option
('pve-lxc-snapshot-name'),
1607 returns
=> { type
=> "object" },
1611 my $rpcenv = PVE
::RPCEnvironment
::get
();
1613 my $authuser = $rpcenv->get_user();
1615 my $vmid = extract_param
($param, 'vmid');
1617 my $snapname = extract_param
($param, 'snapname');
1619 my $lxc_conf = PVE
::LXC
::load_config
($vmid);
1621 my $snap = $lxc_conf->{snapshots
}->{$snapname};
1623 die "snapshot '$snapname' does not exist\n" if !defined($snap);
1625 my $conf = PVE
::LXC
::lxc_conf_to_pve
($param->{vmid
}, $snap);
1630 __PACKAGE__-
>register_method({
1631 name
=> 'vm_feature',
1632 path
=> '{vmid}/feature',
1636 description
=> "Check if feature for virtual machine is available.",
1638 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
1641 additionalProperties
=> 0,
1643 node
=> get_standard_option
('pve-node'),
1644 vmid
=> get_standard_option
('pve-vmid'),
1646 description
=> "Feature to check.",
1648 enum
=> [ 'snapshot' ],
1650 snapname
=> get_standard_option
('pve-lxc-snapshot-name', {
1658 hasFeature
=> { type
=> 'boolean' },
1661 #items => { type => 'string' },
1668 my $node = extract_param
($param, 'node');
1670 my $vmid = extract_param
($param, 'vmid');
1672 my $snapname = extract_param
($param, 'snapname');
1674 my $feature = extract_param
($param, 'feature');
1676 my $conf = PVE
::LXC
::load_config
($vmid);
1679 my $snap = $conf->{snapshots
}->{$snapname};
1680 die "snapshot '$snapname' does not exist\n" if !defined($snap);
1683 my $storecfg = PVE
::Storage
::config
();
1684 #Maybe include later
1685 #my $nodelist = PVE::LXC::shared_nodes($conf, $storecfg);
1686 my $hasFeature = PVE
::LXC
::has_feature
($feature, $conf, $storecfg, $snapname);
1689 hasFeature
=> $hasFeature,
1690 #nodes => [ keys %$nodelist ],