]>
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 $param->{disk
} = $conf->{'pve.disksize'} if !$param->{disk
} && $restore;
283 PVE
::LXCCreate
::create_rootfs
($storage_cfg, $storage, $param->{disk
}, $vmid, $conf,
284 $archive, $password, $restore);
287 my $realcmd = sub { PVE
::LXC
::lock_container
($vmid, 1, $code); };
289 &$check_vmid_usage(); # first check before locking
291 return $rpcenv->fork_worker($restore ?
'vzrestore' : 'vzcreate',
292 $vmid, $authuser, $realcmd);
296 my $vm_config_perm_list = [
304 __PACKAGE__-
>register_method({
306 path
=> '{vmid}/config',
310 description
=> "Set container options.",
312 check
=> ['perm', '/vms/{vmid}', $vm_config_perm_list, any
=> 1],
315 additionalProperties
=> 0,
316 properties
=> PVE
::LXC
::json_config_properties
(
318 node
=> get_standard_option
('pve-node'),
319 vmid
=> get_standard_option
('pve-vmid'),
321 type
=> 'string', format
=> 'pve-configid-list',
322 description
=> "A list of settings you want to delete.",
327 description
=> 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.',
333 returns
=> { type
=> 'null'},
337 my $rpcenv = PVE
::RPCEnvironment
::get
();
339 my $authuser = $rpcenv->get_user();
341 my $node = extract_param
($param, 'node');
343 my $vmid = extract_param
($param, 'vmid');
345 my $digest = extract_param
($param, 'digest');
347 die "no options specified\n" if !scalar(keys %$param);
349 my $delete_str = extract_param
($param, 'delete');
350 my @delete = PVE
::Tools
::split_list
($delete_str);
352 &$check_ct_modify_config_perm($rpcenv, $authuser, $vmid, undef, [@delete]);
354 foreach my $opt (@delete) {
355 raise_param_exc
({ delete => "you can't use '-$opt' and " .
356 "-delete $opt' at the same time" })
357 if defined($param->{$opt});
359 if (!PVE
::LXC
::option_exists
($opt)) {
360 raise_param_exc
({ delete => "unknown option '$opt'" });
364 &$check_ct_modify_config_perm($rpcenv, $authuser, $vmid, undef, [keys %$param]);
368 my $conf = PVE
::LXC
::load_config
($vmid);
369 PVE
::LXC
::check_lock
($conf);
371 PVE
::Tools
::assert_if_modified
($digest, $conf->{digest
});
373 my $running = PVE
::LXC
::check_running
($vmid);
375 PVE
::LXC
::update_lxc_config
($vmid, $conf, $running, $param, \
@delete);
377 PVE
::LXC
::write_config
($vmid, $conf);
380 PVE
::LXC
::lock_container
($vmid, undef, $code);
385 __PACKAGE__-
>register_method ({
386 subclass
=> "PVE::API2::Firewall::CT",
387 path
=> '{vmid}/firewall',
390 __PACKAGE__-
>register_method({
395 description
=> "Directory index",
400 additionalProperties
=> 0,
402 node
=> get_standard_option
('pve-node'),
403 vmid
=> get_standard_option
('pve-vmid'),
411 subdir
=> { type
=> 'string' },
414 links
=> [ { rel
=> 'child', href
=> "{subdir}" } ],
420 my $conf = PVE
::LXC
::load_config
($param->{vmid
});
423 { subdir
=> 'config' },
424 { subdir
=> 'status' },
425 { subdir
=> 'vncproxy' },
426 { subdir
=> 'vncwebsocket' },
427 { subdir
=> 'spiceproxy' },
428 { subdir
=> 'migrate' },
429 # { subdir => 'initlog' },
431 { subdir
=> 'rrddata' },
432 { subdir
=> 'firewall' },
433 { subdir
=> 'snapshot' },
439 __PACKAGE__-
>register_method({
441 path
=> '{vmid}/rrd',
443 protected
=> 1, # fixme: can we avoid that?
445 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
447 description
=> "Read VM RRD statistics (returns PNG)",
449 additionalProperties
=> 0,
451 node
=> get_standard_option
('pve-node'),
452 vmid
=> get_standard_option
('pve-vmid'),
454 description
=> "Specify the time frame you are interested in.",
456 enum
=> [ 'hour', 'day', 'week', 'month', 'year' ],
459 description
=> "The list of datasources you want to display.",
460 type
=> 'string', format
=> 'pve-configid-list',
463 description
=> "The RRD consolidation function",
465 enum
=> [ 'AVERAGE', 'MAX' ],
473 filename
=> { type
=> 'string' },
479 return PVE
::Cluster
::create_rrd_graph
(
480 "pve2-vm/$param->{vmid}", $param->{timeframe
},
481 $param->{ds
}, $param->{cf
});
485 __PACKAGE__-
>register_method({
487 path
=> '{vmid}/rrddata',
489 protected
=> 1, # fixme: can we avoid that?
491 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
493 description
=> "Read VM RRD statistics",
495 additionalProperties
=> 0,
497 node
=> get_standard_option
('pve-node'),
498 vmid
=> get_standard_option
('pve-vmid'),
500 description
=> "Specify the time frame you are interested in.",
502 enum
=> [ 'hour', 'day', 'week', 'month', 'year' ],
505 description
=> "The RRD consolidation function",
507 enum
=> [ 'AVERAGE', 'MAX' ],
522 return PVE
::Cluster
::create_rrd_data
(
523 "pve2-vm/$param->{vmid}", $param->{timeframe
}, $param->{cf
});
527 __PACKAGE__-
>register_method({
529 path
=> '{vmid}/config',
532 description
=> "Get container configuration.",
534 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
537 additionalProperties
=> 0,
539 node
=> get_standard_option
('pve-node'),
540 vmid
=> get_standard_option
('pve-vmid'),
548 description
=> 'SHA1 digest of configuration file. This can be used to prevent concurrent modifications.',
555 my $lxc_conf = PVE
::LXC
::load_config
($param->{vmid
});
557 # NOTE: we only return selected/converted values
559 my $conf = PVE
::LXC
::lxc_conf_to_pve
($param->{vmid
}, $lxc_conf);
561 my $stcfg = PVE
::Cluster
::cfs_read_file
("storage.cfg");
563 my ($sid, undef, $path) = &$get_container_storage($stcfg, $param->{vmid
}, $lxc_conf);
564 $conf->{storage
} = $sid || $path;
569 __PACKAGE__-
>register_method({
570 name
=> 'destroy_vm',
575 description
=> "Destroy the container (also delete all uses files).",
577 check
=> [ 'perm', '/vms/{vmid}', ['VM.Allocate']],
580 additionalProperties
=> 0,
582 node
=> get_standard_option
('pve-node'),
583 vmid
=> get_standard_option
('pve-vmid'),
592 my $rpcenv = PVE
::RPCEnvironment
::get
();
594 my $authuser = $rpcenv->get_user();
596 my $vmid = $param->{vmid
};
598 # test if container exists
599 my $conf = PVE
::LXC
::load_config
($vmid);
601 my $storage_cfg = cfs_read_file
("storage.cfg");
604 # reload config after lock
605 $conf = PVE
::LXC
::load_config
($vmid);
606 PVE
::LXC
::check_lock
($conf);
608 PVE
::LXC
::destory_lxc_container
($storage_cfg, $vmid, $conf);
609 PVE
::AccessControl
::remove_vm_from_pool
($vmid);
612 my $realcmd = sub { PVE
::LXC
::lock_container
($vmid, 1, $code); };
614 return $rpcenv->fork_worker('vzdestroy', $vmid, $authuser, $realcmd);
619 __PACKAGE__-
>register_method ({
621 path
=> '{vmid}/vncproxy',
625 check
=> ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
627 description
=> "Creates a TCP VNC proxy connections.",
629 additionalProperties
=> 0,
631 node
=> get_standard_option
('pve-node'),
632 vmid
=> get_standard_option
('pve-vmid'),
636 description
=> "use websocket instead of standard VNC.",
641 additionalProperties
=> 0,
643 user
=> { type
=> 'string' },
644 ticket
=> { type
=> 'string' },
645 cert
=> { type
=> 'string' },
646 port
=> { type
=> 'integer' },
647 upid
=> { type
=> 'string' },
653 my $rpcenv = PVE
::RPCEnvironment
::get
();
655 my $authuser = $rpcenv->get_user();
657 my $vmid = $param->{vmid
};
658 my $node = $param->{node
};
660 my $authpath = "/vms/$vmid";
662 my $ticket = PVE
::AccessControl
::assemble_vnc_ticket
($authuser, $authpath);
664 $sslcert = PVE
::Tools
::file_get_contents
("/etc/pve/pve-root-ca.pem", 8192)
667 my ($remip, $family);
669 if ($node ne PVE
::INotify
::nodename
()) {
670 ($remip, $family) = PVE
::Cluster
::remote_node_ip
($node);
672 $family = PVE
::Tools
::get_host_address_family
($node);
675 my $port = PVE
::Tools
::next_vnc_port
($family);
677 # NOTE: vncterm VNC traffic is already TLS encrypted,
678 # so we select the fastest chipher here (or 'none'?)
679 my $remcmd = $remip ?
680 ['/usr/bin/ssh', '-t', $remip] : [];
682 my $shcmd = [ '/usr/bin/dtach', '-A',
683 "/var/run/dtach/vzctlconsole$vmid",
685 'lxc-console', '-n', $vmid ];
690 syslog
('info', "starting lxc vnc proxy $upid\n");
694 my $cmd = ['/usr/bin/vncterm', '-rfbport', $port,
695 '-timeout', $timeout, '-authpath', $authpath,
696 '-perm', 'VM.Console'];
698 if ($param->{websocket
}) {
699 $ENV{PVE_VNC_TICKET
} = $ticket; # pass ticket to vncterm
700 push @$cmd, '-notls', '-listen', 'localhost';
703 push @$cmd, '-c', @$remcmd, @$shcmd;
710 my $upid = $rpcenv->fork_worker('vncproxy', $vmid, $authuser, $realcmd);
712 PVE
::Tools
::wait_for_vnc_port
($port);
723 __PACKAGE__-
>register_method({
724 name
=> 'vncwebsocket',
725 path
=> '{vmid}/vncwebsocket',
728 description
=> "You also need to pass a valid ticket (vncticket).",
729 check
=> ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
731 description
=> "Opens a weksocket for VNC traffic.",
733 additionalProperties
=> 0,
735 node
=> get_standard_option
('pve-node'),
736 vmid
=> get_standard_option
('pve-vmid'),
738 description
=> "Ticket from previous call to vncproxy.",
743 description
=> "Port number returned by previous vncproxy call.",
753 port
=> { type
=> 'string' },
759 my $rpcenv = PVE
::RPCEnvironment
::get
();
761 my $authuser = $rpcenv->get_user();
763 my $authpath = "/vms/$param->{vmid}";
765 PVE
::AccessControl
::verify_vnc_ticket
($param->{vncticket
}, $authuser, $authpath);
767 my $port = $param->{port
};
769 return { port
=> $port };
772 __PACKAGE__-
>register_method ({
773 name
=> 'spiceproxy',
774 path
=> '{vmid}/spiceproxy',
779 check
=> ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
781 description
=> "Returns a SPICE configuration to connect to the CT.",
783 additionalProperties
=> 0,
785 node
=> get_standard_option
('pve-node'),
786 vmid
=> get_standard_option
('pve-vmid'),
787 proxy
=> get_standard_option
('spice-proxy', { optional
=> 1 }),
790 returns
=> get_standard_option
('remote-viewer-config'),
794 my $vmid = $param->{vmid
};
795 my $node = $param->{node
};
796 my $proxy = $param->{proxy
};
798 my $authpath = "/vms/$vmid";
799 my $permissions = 'VM.Console';
801 my $shcmd = ['/usr/bin/dtach', '-A',
802 "/var/run/dtach/vzctlconsole$vmid",
804 'lxc-console', '-n', $vmid];
806 my $title = "CT $vmid";
808 return PVE
::API2Tools
::run_spiceterm
($authpath, $permissions, $vmid, $node, $proxy, $title, $shcmd);
811 __PACKAGE__-
>register_method({
813 path
=> '{vmid}/status',
816 description
=> "Directory index",
821 additionalProperties
=> 0,
823 node
=> get_standard_option
('pve-node'),
824 vmid
=> get_standard_option
('pve-vmid'),
832 subdir
=> { type
=> 'string' },
835 links
=> [ { rel
=> 'child', href
=> "{subdir}" } ],
841 my $conf = PVE
::LXC
::load_config
($param->{vmid
});
844 { subdir
=> 'current' },
845 { subdir
=> 'start' },
846 { subdir
=> 'stop' },
847 { subdir
=> 'shutdown' },
848 { subdir
=> 'migrate' },
854 __PACKAGE__-
>register_method({
856 path
=> '{vmid}/status/current',
859 protected
=> 1, # openvz /proc entries are only readable by root
860 description
=> "Get virtual machine status.",
862 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
865 additionalProperties
=> 0,
867 node
=> get_standard_option
('pve-node'),
868 vmid
=> get_standard_option
('pve-vmid'),
871 returns
=> { type
=> 'object' },
876 my $conf = PVE
::LXC
::load_config
($param->{vmid
});
878 my $vmstatus = PVE
::LXC
::vmstatus
($param->{vmid
});
879 my $status = $vmstatus->{$param->{vmid
}};
881 $status->{ha
} = PVE
::HA
::Config
::vm_is_ha_managed
($param->{vmid
}) ?
1 : 0;
886 __PACKAGE__-
>register_method({
888 path
=> '{vmid}/status/start',
892 description
=> "Start the container.",
894 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
897 additionalProperties
=> 0,
899 node
=> get_standard_option
('pve-node'),
900 vmid
=> get_standard_option
('pve-vmid'),
909 my $rpcenv = PVE
::RPCEnvironment
::get
();
911 my $authuser = $rpcenv->get_user();
913 my $node = extract_param
($param, 'node');
915 my $vmid = extract_param
($param, 'vmid');
917 die "CT $vmid already running\n" if PVE
::LXC
::check_running
($vmid);
919 if (PVE
::HA
::Config
::vm_is_ha_managed
($vmid) && $rpcenv->{type
} ne 'ha') {
924 my $service = "ct:$vmid";
926 my $cmd = ['ha-manager', 'enable', $service];
928 print "Executing HA start for CT $vmid\n";
930 PVE
::Tools
::run_command
($cmd);
935 return $rpcenv->fork_worker('hastart', $vmid, $authuser, $hacmd);
942 syslog
('info', "starting CT $vmid: $upid\n");
944 my $conf = PVE
::LXC
::load_config
($vmid);
945 my $stcfg = cfs_read_file
("storage.cfg");
946 if (my $sid = &$get_container_storage($stcfg, $vmid, $conf)) {
947 PVE
::Storage
::activate_storage
($stcfg, $sid);
950 my $cmd = ['lxc-start', '-n', $vmid];
957 return $rpcenv->fork_worker('vzstart', $vmid, $authuser, $realcmd);
961 __PACKAGE__-
>register_method({
963 path
=> '{vmid}/status/stop',
967 description
=> "Stop the container.",
969 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
972 additionalProperties
=> 0,
974 node
=> get_standard_option
('pve-node'),
975 vmid
=> get_standard_option
('pve-vmid'),
984 my $rpcenv = PVE
::RPCEnvironment
::get
();
986 my $authuser = $rpcenv->get_user();
988 my $node = extract_param
($param, 'node');
990 my $vmid = extract_param
($param, 'vmid');
992 die "CT $vmid not running\n" if !PVE
::LXC
::check_running
($vmid);
994 if (PVE
::HA
::Config
::vm_is_ha_managed
($vmid) && $rpcenv->{type
} ne 'ha') {
999 my $service = "ct:$vmid";
1001 my $cmd = ['ha-manager', 'disable', $service];
1003 print "Executing HA stop for CT $vmid\n";
1005 PVE
::Tools
::run_command
($cmd);
1010 return $rpcenv->fork_worker('hastop', $vmid, $authuser, $hacmd);
1017 syslog
('info', "stoping CT $vmid: $upid\n");
1019 my $cmd = ['lxc-stop', '-n', $vmid, '--kill'];
1026 return $rpcenv->fork_worker('vzstop', $vmid, $authuser, $realcmd);
1030 __PACKAGE__-
>register_method({
1031 name
=> 'vm_shutdown',
1032 path
=> '{vmid}/status/shutdown',
1036 description
=> "Shutdown the container.",
1038 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1041 additionalProperties
=> 0,
1043 node
=> get_standard_option
('pve-node'),
1044 vmid
=> get_standard_option
('pve-vmid'),
1046 description
=> "Wait maximal timeout seconds.",
1053 description
=> "Make sure the Container stops.",
1066 my $rpcenv = PVE
::RPCEnvironment
::get
();
1068 my $authuser = $rpcenv->get_user();
1070 my $node = extract_param
($param, 'node');
1072 my $vmid = extract_param
($param, 'vmid');
1074 my $timeout = extract_param
($param, 'timeout');
1076 die "CT $vmid not running\n" if !PVE
::LXC
::check_running
($vmid);
1081 syslog
('info', "shutdown CT $vmid: $upid\n");
1083 my $cmd = ['lxc-stop', '-n', $vmid];
1085 $timeout = 60 if !defined($timeout);
1087 push @$cmd, '--timeout', $timeout;
1089 eval { run_command
($cmd, timeout
=> $timeout+5); };
1093 die $err if !$param->{forceStop
};
1095 warn "shutdown failed - forcing stop now\n";
1097 push @$cmd, '--kill';
1103 my $upid = $rpcenv->fork_worker('vzshutdown', $vmid, $authuser, $realcmd);
1108 __PACKAGE__-
>register_method({
1109 name
=> 'vm_suspend',
1110 path
=> '{vmid}/status/suspend',
1114 description
=> "Suspend the container.",
1116 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1119 additionalProperties
=> 0,
1121 node
=> get_standard_option
('pve-node'),
1122 vmid
=> get_standard_option
('pve-vmid'),
1131 my $rpcenv = PVE
::RPCEnvironment
::get
();
1133 my $authuser = $rpcenv->get_user();
1135 my $node = extract_param
($param, 'node');
1137 my $vmid = extract_param
($param, 'vmid');
1139 die "CT $vmid not running\n" if !PVE
::LXC
::check_running
($vmid);
1144 syslog
('info', "suspend CT $vmid: $upid\n");
1146 my $cmd = ['lxc-checkpoint', '-n', $vmid, '-s', '-D', '/var/liv/vz/dump'];
1153 my $upid = $rpcenv->fork_worker('vzsuspend', $vmid, $authuser, $realcmd);
1158 __PACKAGE__-
>register_method({
1159 name
=> 'vm_resume',
1160 path
=> '{vmid}/status/resume',
1164 description
=> "Resume the container.",
1166 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1169 additionalProperties
=> 0,
1171 node
=> get_standard_option
('pve-node'),
1172 vmid
=> get_standard_option
('pve-vmid'),
1181 my $rpcenv = PVE
::RPCEnvironment
::get
();
1183 my $authuser = $rpcenv->get_user();
1185 my $node = extract_param
($param, 'node');
1187 my $vmid = extract_param
($param, 'vmid');
1189 die "CT $vmid already running\n" if PVE
::LXC
::check_running
($vmid);
1194 syslog
('info', "resume CT $vmid: $upid\n");
1196 my $cmd = ['lxc-checkpoint', '-n', $vmid, '-r', '--foreground',
1197 '-D', '/var/liv/vz/dump'];
1204 my $upid = $rpcenv->fork_worker('vzresume', $vmid, $authuser, $realcmd);
1209 __PACKAGE__-
>register_method({
1210 name
=> 'migrate_vm',
1211 path
=> '{vmid}/migrate',
1215 description
=> "Migrate the container to another node. Creates a new migration task.",
1217 check
=> ['perm', '/vms/{vmid}', [ 'VM.Migrate' ]],
1220 additionalProperties
=> 0,
1222 node
=> get_standard_option
('pve-node'),
1223 vmid
=> get_standard_option
('pve-vmid'),
1224 target
=> get_standard_option
('pve-node', { description
=> "Target node." }),
1227 description
=> "Use online/live migration.",
1234 description
=> "the task ID.",
1239 my $rpcenv = PVE
::RPCEnvironment
::get
();
1241 my $authuser = $rpcenv->get_user();
1243 my $target = extract_param
($param, 'target');
1245 my $localnode = PVE
::INotify
::nodename
();
1246 raise_param_exc
({ target
=> "target is local node."}) if $target eq $localnode;
1248 PVE
::Cluster
::check_cfs_quorum
();
1250 PVE
::Cluster
::check_node_exists
($target);
1252 my $targetip = PVE
::Cluster
::remote_node_ip
($target);
1254 my $vmid = extract_param
($param, 'vmid');
1257 PVE
::LXC
::load_config
($vmid);
1259 # try to detect errors early
1260 if (PVE
::LXC
::check_running
($vmid)) {
1261 die "cant migrate running container without --online\n"
1262 if !$param->{online
};
1265 if (PVE
::HA
::Config
::vm_is_ha_managed
($vmid) && $rpcenv->{type
} ne 'ha') {
1270 my $service = "ct:$vmid";
1272 my $cmd = ['ha-manager', 'migrate', $service, $target];
1274 print "Executing HA migrate for CT $vmid to node $target\n";
1276 PVE
::Tools
::run_command
($cmd);
1281 return $rpcenv->fork_worker('hamigrate', $vmid, $authuser, $hacmd);
1288 # fixme: implement lxc container migration
1289 die "lxc container migration not implemented\n";
1294 return $rpcenv->fork_worker('vzmigrate', $vmid, $authuser, $realcmd);
1298 __PACKAGE__-
>register_method({
1300 path
=> '{vmid}/snapshot',
1304 description
=> "Snapshot a container.",
1306 check
=> ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
1309 additionalProperties
=> 0,
1311 node
=> get_standard_option
('pve-node'),
1312 vmid
=> get_standard_option
('pve-vmid'),
1313 snapname
=> get_standard_option
('pve-lxc-snapshot-name'),
1317 description
=> "Save the vmstate",
1322 description
=> "A textual description or comment.",
1328 description
=> "the task ID.",
1333 my $rpcenv = PVE
::RPCEnvironment
::get
();
1335 my $authuser = $rpcenv->get_user();
1337 my $node = extract_param
($param, 'node');
1339 my $vmid = extract_param
($param, 'vmid');
1341 my $snapname = extract_param
($param, 'snapname');
1343 die "unable to use snapshot name 'current' (reserved name)\n"
1344 if $snapname eq 'current';
1347 PVE
::Cluster
::log_msg
('info', $authuser, "snapshot container $vmid: $snapname");
1348 PVE
::LXC
::snapshot_create
($vmid, $snapname, $param->{description
});
1351 return $rpcenv->fork_worker('pctsnapshot', $vmid, $authuser, $realcmd);
1354 __PACKAGE__-
>register_method({
1355 name
=> 'delsnapshot',
1356 path
=> '{vmid}/snapshot/{snapname}',
1360 description
=> "Delete a LXC snapshot.",
1362 check
=> ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
1365 additionalProperties
=> 0,
1367 node
=> get_standard_option
('pve-node'),
1368 vmid
=> get_standard_option
('pve-vmid'),
1369 snapname
=> get_standard_option
('pve-lxc-snapshot-name'),
1373 description
=> "For removal from config file, even if removing disk snapshots fails.",
1379 description
=> "the task ID.",
1384 my $rpcenv = PVE
::RPCEnvironment
::get
();
1386 my $authuser = $rpcenv->get_user();
1388 my $node = extract_param
($param, 'node');
1390 my $vmid = extract_param
($param, 'vmid');
1392 my $snapname = extract_param
($param, 'snapname');
1395 PVE
::Cluster
::log_msg
('info', $authuser, "delete snapshot VM $vmid: $snapname");
1396 PVE
::LXC
::snapshot_delete
($vmid, $snapname, $param->{force
});
1399 return $rpcenv->fork_worker('lxcdelsnapshot', $vmid, $authuser, $realcmd);
1402 __PACKAGE__-
>register_method({
1404 path
=> '{vmid}/snapshot/{snapname}/rollback',
1408 description
=> "Rollback LXC state to specified snapshot.",
1410 check
=> ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
1413 additionalProperties
=> 0,
1415 node
=> get_standard_option
('pve-node'),
1416 vmid
=> get_standard_option
('pve-vmid'),
1417 snapname
=> get_standard_option
('pve-lxc-snapshot-name'),
1422 description
=> "the task ID.",
1427 my $rpcenv = PVE
::RPCEnvironment
::get
();
1429 my $authuser = $rpcenv->get_user();
1431 my $node = extract_param
($param, 'node');
1433 my $vmid = extract_param
($param, 'vmid');
1435 my $snapname = extract_param
($param, 'snapname');
1438 PVE
::Cluster
::log_msg
('info', $authuser, "rollback snapshot LXC $vmid: $snapname");
1439 PVE
::LXC
::snapshot_rollback
($vmid, $snapname);
1442 return $rpcenv->fork_worker('lxcrollback', $vmid, $authuser, $realcmd);
1445 __PACKAGE__-
>register_method({
1446 name
=> 'snapshot_list',
1447 path
=> '{vmid}/snapshot',
1449 description
=> "List all snapshots.",
1451 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
1454 protected
=> 1, # lxc pid files are only readable by root
1456 additionalProperties
=> 0,
1458 vmid
=> get_standard_option
('pve-vmid'),
1459 node
=> get_standard_option
('pve-node'),
1468 links
=> [ { rel
=> 'child', href
=> "{name}" } ],
1473 my $vmid = $param->{vmid
};
1475 my $conf = PVE
::LXC
::load_config
($vmid);
1476 my $snaphash = $conf->{snapshots
} || {};
1480 foreach my $name (keys %$snaphash) {
1481 my $d = $snaphash->{$name};
1484 snaptime
=> $d->{'pve.snaptime'} || 0,
1485 description
=> $d->{'pve.snapcomment'} || '',
1487 $item->{parent
} = $d->{'pve.parent'} if $d->{'pve.parent'};
1488 $item->{snapstate
} = $d->{'pve.snapstate'} if $d->{'pve.snapstate'};
1492 my $running = PVE
::LXC
::check_running
($vmid) ?
1 : 0;
1493 my $current = { name
=> 'current', digest
=> $conf->{digest
}, running
=> $running };
1494 $current->{parent
} = $conf->{'pve.parent'} if $conf->{'pve.parent'};
1496 push @$res, $current;
1501 __PACKAGE__-
>register_method({
1502 name
=> 'snapshot_cmd_idx',
1503 path
=> '{vmid}/snapshot/{snapname}',
1510 additionalProperties
=> 0,
1512 vmid
=> get_standard_option
('pve-vmid'),
1513 node
=> get_standard_option
('pve-node'),
1514 snapname
=> get_standard_option
('pve-lxc-snapshot-name'),
1523 links
=> [ { rel
=> 'child', href
=> "{cmd}" } ],
1530 push @$res, { cmd
=> 'rollback' };
1531 push @$res, { cmd
=> 'config' };
1536 __PACKAGE__-
>register_method({
1537 name
=> 'update_snapshot_config',
1538 path
=> '{vmid}/snapshot/{snapname}/config',
1542 description
=> "Update snapshot metadata.",
1544 check
=> ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
1547 additionalProperties
=> 0,
1549 node
=> get_standard_option
('pve-node'),
1550 vmid
=> get_standard_option
('pve-vmid'),
1551 snapname
=> get_standard_option
('pve-lxc-snapshot-name'),
1555 description
=> "A textual description or comment.",
1559 returns
=> { type
=> 'null' },
1563 my $rpcenv = PVE
::RPCEnvironment
::get
();
1565 my $authuser = $rpcenv->get_user();
1567 my $vmid = extract_param
($param, 'vmid');
1569 my $snapname = extract_param
($param, 'snapname');
1571 return undef if !defined($param->{description
});
1573 my $updatefn = sub {
1575 my $conf = PVE
::LXC
::load_config
($vmid);
1576 PVE
::LXC
::check_lock
($conf);
1578 my $snap = $conf->{snapshots
}->{$snapname};
1580 die "snapshot '$snapname' does not exist\n" if !defined($snap);
1582 $snap->{'pve.snapcomment'} = $param->{description
} if defined($param->{description
});
1584 PVE
::LXC
::write_config
($vmid, $conf, 1);
1587 PVE
::LXC
::lock_container
($vmid, 10, $updatefn);
1592 __PACKAGE__-
>register_method({
1593 name
=> 'get_snapshot_config',
1594 path
=> '{vmid}/snapshot/{snapname}/config',
1597 description
=> "Get snapshot configuration",
1599 check
=> ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
1602 additionalProperties
=> 0,
1604 node
=> get_standard_option
('pve-node'),
1605 vmid
=> get_standard_option
('pve-vmid'),
1606 snapname
=> get_standard_option
('pve-lxc-snapshot-name'),
1609 returns
=> { type
=> "object" },
1613 my $rpcenv = PVE
::RPCEnvironment
::get
();
1615 my $authuser = $rpcenv->get_user();
1617 my $vmid = extract_param
($param, 'vmid');
1619 my $snapname = extract_param
($param, 'snapname');
1621 my $lxc_conf = PVE
::LXC
::load_config
($vmid);
1623 my $snap = $lxc_conf->{snapshots
}->{$snapname};
1625 die "snapshot '$snapname' does not exist\n" if !defined($snap);
1627 my $conf = PVE
::LXC
::lxc_conf_to_pve
($param->{vmid
}, $snap);
1632 __PACKAGE__-
>register_method({
1633 name
=> 'vm_feature',
1634 path
=> '{vmid}/feature',
1638 description
=> "Check if feature for virtual machine is available.",
1640 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
1643 additionalProperties
=> 0,
1645 node
=> get_standard_option
('pve-node'),
1646 vmid
=> get_standard_option
('pve-vmid'),
1648 description
=> "Feature to check.",
1650 enum
=> [ 'snapshot' ],
1652 snapname
=> get_standard_option
('pve-lxc-snapshot-name', {
1660 hasFeature
=> { type
=> 'boolean' },
1663 #items => { type => 'string' },
1670 my $node = extract_param
($param, 'node');
1672 my $vmid = extract_param
($param, 'vmid');
1674 my $snapname = extract_param
($param, 'snapname');
1676 my $feature = extract_param
($param, 'feature');
1678 my $conf = PVE
::LXC
::load_config
($vmid);
1681 my $snap = $conf->{snapshots
}->{$snapname};
1682 die "snapshot '$snapname' does not exist\n" if !defined($snap);
1685 my $storecfg = PVE
::Storage
::config
();
1686 #Maybe include later
1687 #my $nodelist = PVE::LXC::shared_nodes($conf, $storecfg);
1688 my $hasFeature = PVE
::LXC
::has_feature
($feature, $conf, $storecfg, $snapname);
1691 hasFeature
=> $hasFeature,
1692 #nodes => [ keys %$nodelist ],