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 $check_ct_modify_config_perm = sub {
24 my ($rpcenv, $authuser, $vmid, $pool, $key_list) = @_;
26 return 1 if $authuser ne 'root@pam';
28 foreach my $opt (@$key_list) {
30 if ($opt eq 'cpus' || $opt eq 'cpuunits' || $opt eq 'cpulimit') {
31 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.CPU']);
32 } elsif ($opt eq 'disk') {
33 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Disk']);
34 } elsif ($opt eq 'memory' || $opt eq 'swap') {
35 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Memory']);
36 } elsif ($opt =~ m/^net\d+$/ || $opt eq 'nameserver' ||
37 $opt eq 'searchdomain' || $opt eq 'hostname') {
38 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Network']);
40 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Options']);
47 my $alloc_rootfs = sub {
48 my ($storage_conf, $storage, $disk_size_gb, $vmid) = @_;
52 my $size = 4*1024*1024; # defaults to 4G
54 $size = int($disk_size_gb*1024) * 1024 if defined($disk_size_gb);
57 my $scfg = PVE
::Storage
::storage_config
($storage_conf, $storage);
58 # fixme: use better naming ct-$vmid-disk-X.raw?
60 if ($scfg->{type
} eq 'dir' || $scfg->{type
} eq 'nfs') {
62 $volid = PVE
::Storage
::vdisk_alloc
($storage_conf, $storage, $vmid, 'raw',
65 $volid = PVE
::Storage
::vdisk_alloc
($storage_conf, $storage, $vmid, 'subvol',
68 } elsif ($scfg->{type
} eq 'zfspool') {
70 $volid = PVE
::Storage
::vdisk_alloc
($storage_conf, $storage, $vmid, 'subvol',
72 } elsif ($scfg->{type
} eq 'drbd') {
74 $volid = PVE
::Storage
::vdisk_alloc
($storage_conf, $storage, $vmid, 'raw', undef, $size);
77 die "unable to create containers on storage type '$scfg->{type}'\n";
82 eval { PVE
::Storage
::vdisk_free
($storage_conf, $volid) if $volid; };
90 PVE
::JSONSchema
::register_standard_option
('pve-lxc-snapshot-name', {
91 description
=> "The name of the snapshot.",
92 type
=> 'string', format
=> 'pve-configid',
96 __PACKAGE__-
>register_method({
100 description
=> "LXC container index (per node).",
102 description
=> "Only list CTs where you have VM.Audit permissons on /vms/<vmid>.",
106 protected
=> 1, # /proc files are only readable by root
108 additionalProperties
=> 0,
110 node
=> get_standard_option
('pve-node'),
119 links
=> [ { rel
=> 'child', href
=> "{vmid}" } ],
124 my $rpcenv = PVE
::RPCEnvironment
::get
();
125 my $authuser = $rpcenv->get_user();
127 my $vmstatus = PVE
::LXC
::vmstatus
();
130 foreach my $vmid (keys %$vmstatus) {
131 next if !$rpcenv->check($authuser, "/vms/$vmid", [ 'VM.Audit' ], 1);
133 my $data = $vmstatus->{$vmid};
134 $data->{vmid
} = $vmid;
142 __PACKAGE__-
>register_method({
146 description
=> "Create or restore a container.",
148 user
=> 'all', # check inside
149 description
=> "You need 'VM.Allocate' permissions on /vms/{vmid} or on the VM pool /pool/{pool}. " .
150 "For restore, it is enough if the user has 'VM.Backup' permission and the VM already exists. " .
151 "You also need 'Datastore.AllocateSpace' permissions on the storage.",
156 additionalProperties
=> 0,
157 properties
=> PVE
::LXC
::json_config_properties_no_rootfs
({
158 node
=> get_standard_option
('pve-node'),
159 vmid
=> get_standard_option
('pve-vmid'),
161 description
=> "The OS template or backup file.",
168 description
=> "Sets root password inside container.",
171 storage
=> get_standard_option
('pve-storage-id', {
172 description
=> "Target storage.",
179 description
=> "Amount of disk space for the VM in GB. A zero indicates no limits.",
186 description
=> "Allow to overwrite existing container.",
191 description
=> "Mark this as restore task.",
195 type
=> 'string', format
=> 'pve-poolid',
196 description
=> "Add the VM to the specified pool.",
206 my $rpcenv = PVE
::RPCEnvironment
::get
();
208 my $authuser = $rpcenv->get_user();
210 my $node = extract_param
($param, 'node');
212 my $vmid = extract_param
($param, 'vmid');
214 my $basecfg_fn = PVE
::LXC
::config_file
($vmid);
216 my $same_container_exists = -f
$basecfg_fn;
218 my $restore = extract_param
($param, 'restore');
221 # fixme: limit allowed parameters
225 my $force = extract_param
($param, 'force');
227 if (!($same_container_exists && $restore && $force)) {
228 PVE
::Cluster
::check_vmid_unused
($vmid);
231 my $password = extract_param
($param, 'password');
233 my $disksize = extract_param
($param, 'size');
235 my $storage = extract_param
($param, 'storage') // 'local';
237 my $storage_cfg = cfs_read_file
("storage.cfg");
239 my $scfg = PVE
::Storage
::storage_check_node
($storage_cfg, $storage, $node);
241 raise_param_exc
({ storage
=> "storage '$storage' does not support container root directories"})
242 if !($scfg->{content
}->{images
} || $scfg->{content
}->{rootdir
});
244 my $pool = extract_param
($param, 'pool');
246 if (defined($pool)) {
247 $rpcenv->check_pool_exist($pool);
248 $rpcenv->check_perm_modify($authuser, "/pool/$pool");
251 $rpcenv->check($authuser, "/storage/$storage", ['Datastore.AllocateSpace']);
253 if ($rpcenv->check($authuser, "/vms/$vmid", ['VM.Allocate'], 1)) {
255 } elsif ($pool && $rpcenv->check($authuser, "/pool/$pool", ['VM.Allocate'], 1)) {
257 } elsif ($restore && $force && $same_container_exists &&
258 $rpcenv->check($authuser, "/vms/$vmid", ['VM.Backup'], 1)) {
259 # OK: user has VM.Backup permissions, and want to restore an existing VM
264 &$check_ct_modify_config_perm($rpcenv, $authuser, $vmid, $pool, [ keys %$param]);
266 PVE
::Storage
::activate_storage
($storage_cfg, $storage);
268 my $ostemplate = extract_param
($param, 'ostemplate');
272 if ($ostemplate eq '-') {
273 die "pipe requires cli environment\n"
274 if $rpcenv->{type
} ne 'cli';
275 die "pipe can only be used with restore tasks\n"
277 die "pipe requires --size parameter\n"
278 if !defined($disksize);
281 $rpcenv->check_volume_access($authuser, $storage_cfg, $vmid, $ostemplate);
282 $archive = PVE
::Storage
::abs_filesystem_path
($storage_cfg, $ostemplate);
287 PVE
::LXC
::update_pct_config
($vmid, $conf, 0, $param);
289 my $check_vmid_usage = sub {
291 die "cant overwrite running container\n"
292 if PVE
::LXC
::check_running
($vmid);
294 PVE
::Cluster
::check_vmid_unused
($vmid);
299 &$check_vmid_usage(); # final check after locking
301 PVE
::Cluster
::check_cfs_quorum
();
306 if (!defined($disksize)) {
308 (undef, $disksize) = PVE
::LXCCreate
::recover_config
($archive);
309 die "unable to detect disk size - please specify with --size\n"
315 $volid = &$alloc_rootfs($storage_cfg, $storage, $disksize, $vmid);
317 PVE
::LXCCreate
::create_rootfs
($storage_cfg, $storage, $volid, $vmid, $conf,
318 $archive, $password, $restore);
320 $conf->{rootfs
} = "$volid,size=$disksize";
323 $conf->{hostname
} ||= "CT$vmid";
324 $conf->{memory
} ||= 512;
325 $conf->{swap
} //= 512;
327 PVE
::LXC
::create_config
($vmid, $conf);
330 eval { PVE
::Storage
::vdisk_free
($storage_cfg, $volid) if $volid; };
332 PVE
::LXC
::destroy_config
($vmid);
335 PVE
::AccessControl
::add_vm_to_pool
($vmid, $pool) if $pool;
338 my $realcmd = sub { PVE
::LXC
::lock_container
($vmid, 1, $code); };
340 &$check_vmid_usage(); # first check before locking
342 return $rpcenv->fork_worker($restore ?
'vzrestore' : 'vzcreate',
343 $vmid, $authuser, $realcmd);
347 my $vm_config_perm_list = [
355 __PACKAGE__-
>register_method({
357 path
=> '{vmid}/config',
361 description
=> "Set container options.",
363 check
=> ['perm', '/vms/{vmid}', $vm_config_perm_list, any
=> 1],
366 additionalProperties
=> 0,
367 properties
=> PVE
::LXC
::json_config_properties
(
369 node
=> get_standard_option
('pve-node'),
370 vmid
=> get_standard_option
('pve-vmid'),
372 type
=> 'string', format
=> 'pve-configid-list',
373 description
=> "A list of settings you want to delete.",
378 description
=> 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.',
384 returns
=> { type
=> 'null'},
388 my $rpcenv = PVE
::RPCEnvironment
::get
();
390 my $authuser = $rpcenv->get_user();
392 my $node = extract_param
($param, 'node');
394 my $vmid = extract_param
($param, 'vmid');
396 my $digest = extract_param
($param, 'digest');
398 die "no options specified\n" if !scalar(keys %$param);
400 my $delete_str = extract_param
($param, 'delete');
401 my @delete = PVE
::Tools
::split_list
($delete_str);
403 &$check_ct_modify_config_perm($rpcenv, $authuser, $vmid, undef, [@delete]);
405 foreach my $opt (@delete) {
406 raise_param_exc
({ delete => "you can't use '-$opt' and " .
407 "-delete $opt' at the same time" })
408 if defined($param->{$opt});
410 if (!PVE
::LXC
::option_exists
($opt)) {
411 raise_param_exc
({ delete => "unknown option '$opt'" });
415 &$check_ct_modify_config_perm($rpcenv, $authuser, $vmid, undef, [keys %$param]);
417 my $storage_cfg = cfs_read_file
("storage.cfg");
421 my $conf = PVE
::LXC
::load_config
($vmid);
422 PVE
::LXC
::check_lock
($conf);
424 PVE
::Tools
::assert_if_modified
($digest, $conf->{digest
});
426 my $running = PVE
::LXC
::check_running
($vmid);
428 PVE
::LXC
::update_pct_config
($vmid, $conf, $running, $param, \
@delete);
430 PVE
::LXC
::write_config
($vmid, $conf);
431 PVE
::LXC
::update_lxc_config
($storage_cfg, $vmid, $conf);
434 PVE
::LXC
::lock_container
($vmid, undef, $code);
439 __PACKAGE__-
>register_method ({
440 subclass
=> "PVE::API2::Firewall::CT",
441 path
=> '{vmid}/firewall',
444 __PACKAGE__-
>register_method({
449 description
=> "Directory index",
454 additionalProperties
=> 0,
456 node
=> get_standard_option
('pve-node'),
457 vmid
=> get_standard_option
('pve-vmid'),
465 subdir
=> { type
=> 'string' },
468 links
=> [ { rel
=> 'child', href
=> "{subdir}" } ],
474 my $conf = PVE
::LXC
::load_config
($param->{vmid
});
477 { subdir
=> 'config' },
478 { subdir
=> 'status' },
479 { subdir
=> 'vncproxy' },
480 { subdir
=> 'vncwebsocket' },
481 { subdir
=> 'spiceproxy' },
482 { subdir
=> 'migrate' },
483 # { subdir => 'initlog' },
485 { subdir
=> 'rrddata' },
486 { subdir
=> 'firewall' },
487 { subdir
=> 'snapshot' },
493 __PACKAGE__-
>register_method({
495 path
=> '{vmid}/rrd',
497 protected
=> 1, # fixme: can we avoid that?
499 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
501 description
=> "Read VM RRD statistics (returns PNG)",
503 additionalProperties
=> 0,
505 node
=> get_standard_option
('pve-node'),
506 vmid
=> get_standard_option
('pve-vmid'),
508 description
=> "Specify the time frame you are interested in.",
510 enum
=> [ 'hour', 'day', 'week', 'month', 'year' ],
513 description
=> "The list of datasources you want to display.",
514 type
=> 'string', format
=> 'pve-configid-list',
517 description
=> "The RRD consolidation function",
519 enum
=> [ 'AVERAGE', 'MAX' ],
527 filename
=> { type
=> 'string' },
533 return PVE
::Cluster
::create_rrd_graph
(
534 "pve2-vm/$param->{vmid}", $param->{timeframe
},
535 $param->{ds
}, $param->{cf
});
539 __PACKAGE__-
>register_method({
541 path
=> '{vmid}/rrddata',
543 protected
=> 1, # fixme: can we avoid that?
545 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
547 description
=> "Read VM RRD statistics",
549 additionalProperties
=> 0,
551 node
=> get_standard_option
('pve-node'),
552 vmid
=> get_standard_option
('pve-vmid'),
554 description
=> "Specify the time frame you are interested in.",
556 enum
=> [ 'hour', 'day', 'week', 'month', 'year' ],
559 description
=> "The RRD consolidation function",
561 enum
=> [ 'AVERAGE', 'MAX' ],
576 return PVE
::Cluster
::create_rrd_data
(
577 "pve2-vm/$param->{vmid}", $param->{timeframe
}, $param->{cf
});
581 __PACKAGE__-
>register_method({
583 path
=> '{vmid}/config',
586 description
=> "Get container configuration.",
588 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
591 additionalProperties
=> 0,
593 node
=> get_standard_option
('pve-node'),
594 vmid
=> get_standard_option
('pve-vmid'),
602 description
=> 'SHA1 digest of configuration file. This can be used to prevent concurrent modifications.',
609 my $conf = PVE
::LXC
::load_config
($param->{vmid
});
611 delete $conf->{snapshots
};
617 __PACKAGE__-
>register_method({
618 name
=> 'destroy_vm',
623 description
=> "Destroy the container (also delete all uses files).",
625 check
=> [ 'perm', '/vms/{vmid}', ['VM.Allocate']],
628 additionalProperties
=> 0,
630 node
=> get_standard_option
('pve-node'),
631 vmid
=> get_standard_option
('pve-vmid'),
640 my $rpcenv = PVE
::RPCEnvironment
::get
();
642 my $authuser = $rpcenv->get_user();
644 my $vmid = $param->{vmid
};
646 # test if container exists
647 my $conf = PVE
::LXC
::load_config
($vmid);
649 my $storage_cfg = cfs_read_file
("storage.cfg");
652 # reload config after lock
653 $conf = PVE
::LXC
::load_config
($vmid);
654 PVE
::LXC
::check_lock
($conf);
656 PVE
::LXC
::destroy_lxc_container
($storage_cfg, $vmid, $conf);
657 PVE
::AccessControl
::remove_vm_from_pool
($vmid);
660 my $realcmd = sub { PVE
::LXC
::lock_container
($vmid, 1, $code); };
662 return $rpcenv->fork_worker('vzdestroy', $vmid, $authuser, $realcmd);
667 __PACKAGE__-
>register_method ({
669 path
=> '{vmid}/vncproxy',
673 check
=> ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
675 description
=> "Creates a TCP VNC proxy connections.",
677 additionalProperties
=> 0,
679 node
=> get_standard_option
('pve-node'),
680 vmid
=> get_standard_option
('pve-vmid'),
684 description
=> "use websocket instead of standard VNC.",
689 additionalProperties
=> 0,
691 user
=> { type
=> 'string' },
692 ticket
=> { type
=> 'string' },
693 cert
=> { type
=> 'string' },
694 port
=> { type
=> 'integer' },
695 upid
=> { type
=> 'string' },
701 my $rpcenv = PVE
::RPCEnvironment
::get
();
703 my $authuser = $rpcenv->get_user();
705 my $vmid = $param->{vmid
};
706 my $node = $param->{node
};
708 my $authpath = "/vms/$vmid";
710 my $ticket = PVE
::AccessControl
::assemble_vnc_ticket
($authuser, $authpath);
712 $sslcert = PVE
::Tools
::file_get_contents
("/etc/pve/pve-root-ca.pem", 8192)
715 my ($remip, $family);
717 if ($node ne PVE
::INotify
::nodename
()) {
718 ($remip, $family) = PVE
::Cluster
::remote_node_ip
($node);
720 $family = PVE
::Tools
::get_host_address_family
($node);
723 my $port = PVE
::Tools
::next_vnc_port
($family);
725 # NOTE: vncterm VNC traffic is already TLS encrypted,
726 # so we select the fastest chipher here (or 'none'?)
727 my $remcmd = $remip ?
728 ['/usr/bin/ssh', '-t', $remip] : [];
730 my $shcmd = [ '/usr/bin/dtach', '-A',
731 "/var/run/dtach/vzctlconsole$vmid",
733 'lxc-console', '-n', $vmid ];
738 syslog
('info', "starting lxc vnc proxy $upid\n");
742 my $cmd = ['/usr/bin/vncterm', '-rfbport', $port,
743 '-timeout', $timeout, '-authpath', $authpath,
744 '-perm', 'VM.Console'];
746 if ($param->{websocket
}) {
747 $ENV{PVE_VNC_TICKET
} = $ticket; # pass ticket to vncterm
748 push @$cmd, '-notls', '-listen', 'localhost';
751 push @$cmd, '-c', @$remcmd, @$shcmd;
758 my $upid = $rpcenv->fork_worker('vncproxy', $vmid, $authuser, $realcmd);
760 PVE
::Tools
::wait_for_vnc_port
($port);
771 __PACKAGE__-
>register_method({
772 name
=> 'vncwebsocket',
773 path
=> '{vmid}/vncwebsocket',
776 description
=> "You also need to pass a valid ticket (vncticket).",
777 check
=> ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
779 description
=> "Opens a weksocket for VNC traffic.",
781 additionalProperties
=> 0,
783 node
=> get_standard_option
('pve-node'),
784 vmid
=> get_standard_option
('pve-vmid'),
786 description
=> "Ticket from previous call to vncproxy.",
791 description
=> "Port number returned by previous vncproxy call.",
801 port
=> { type
=> 'string' },
807 my $rpcenv = PVE
::RPCEnvironment
::get
();
809 my $authuser = $rpcenv->get_user();
811 my $authpath = "/vms/$param->{vmid}";
813 PVE
::AccessControl
::verify_vnc_ticket
($param->{vncticket
}, $authuser, $authpath);
815 my $port = $param->{port
};
817 return { port
=> $port };
820 __PACKAGE__-
>register_method ({
821 name
=> 'spiceproxy',
822 path
=> '{vmid}/spiceproxy',
827 check
=> ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
829 description
=> "Returns a SPICE configuration to connect to the CT.",
831 additionalProperties
=> 0,
833 node
=> get_standard_option
('pve-node'),
834 vmid
=> get_standard_option
('pve-vmid'),
835 proxy
=> get_standard_option
('spice-proxy', { optional
=> 1 }),
838 returns
=> get_standard_option
('remote-viewer-config'),
842 my $vmid = $param->{vmid
};
843 my $node = $param->{node
};
844 my $proxy = $param->{proxy
};
846 my $authpath = "/vms/$vmid";
847 my $permissions = 'VM.Console';
849 my $shcmd = ['/usr/bin/dtach', '-A',
850 "/var/run/dtach/vzctlconsole$vmid",
852 'lxc-console', '-n', $vmid];
854 my $title = "CT $vmid";
856 return PVE
::API2Tools
::run_spiceterm
($authpath, $permissions, $vmid, $node, $proxy, $title, $shcmd);
859 __PACKAGE__-
>register_method({
861 path
=> '{vmid}/status',
864 description
=> "Directory index",
869 additionalProperties
=> 0,
871 node
=> get_standard_option
('pve-node'),
872 vmid
=> get_standard_option
('pve-vmid'),
880 subdir
=> { type
=> 'string' },
883 links
=> [ { rel
=> 'child', href
=> "{subdir}" } ],
889 my $conf = PVE
::LXC
::load_config
($param->{vmid
});
892 { subdir
=> 'current' },
893 { subdir
=> 'start' },
894 { subdir
=> 'stop' },
895 { subdir
=> 'shutdown' },
896 { subdir
=> 'migrate' },
902 __PACKAGE__-
>register_method({
904 path
=> '{vmid}/status/current',
907 protected
=> 1, # openvz /proc entries are only readable by root
908 description
=> "Get virtual machine status.",
910 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
913 additionalProperties
=> 0,
915 node
=> get_standard_option
('pve-node'),
916 vmid
=> get_standard_option
('pve-vmid'),
919 returns
=> { type
=> 'object' },
924 my $conf = PVE
::LXC
::load_config
($param->{vmid
});
926 my $vmstatus = PVE
::LXC
::vmstatus
($param->{vmid
});
927 my $status = $vmstatus->{$param->{vmid
}};
929 $status->{ha
} = PVE
::HA
::Config
::vm_is_ha_managed
($param->{vmid
}) ?
1 : 0;
934 __PACKAGE__-
>register_method({
936 path
=> '{vmid}/status/start',
940 description
=> "Start the container.",
942 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
945 additionalProperties
=> 0,
947 node
=> get_standard_option
('pve-node'),
948 vmid
=> get_standard_option
('pve-vmid'),
957 my $rpcenv = PVE
::RPCEnvironment
::get
();
959 my $authuser = $rpcenv->get_user();
961 my $node = extract_param
($param, 'node');
963 my $vmid = extract_param
($param, 'vmid');
965 die "CT $vmid already running\n" if PVE
::LXC
::check_running
($vmid);
967 if (PVE
::HA
::Config
::vm_is_ha_managed
($vmid) && $rpcenv->{type
} ne 'ha') {
972 my $service = "ct:$vmid";
974 my $cmd = ['ha-manager', 'enable', $service];
976 print "Executing HA start for CT $vmid\n";
978 PVE
::Tools
::run_command
($cmd);
983 return $rpcenv->fork_worker('hastart', $vmid, $authuser, $hacmd);
990 syslog
('info', "starting CT $vmid: $upid\n");
992 my $conf = PVE
::LXC
::load_config
($vmid);
993 my $storage_cfg = cfs_read_file
("storage.cfg");
995 my $rootinfo = PVE
::LXC
::parse_ct_mountpoint
($conf->{rootfs
});
996 PVE
::Storage
::activate_volumes
($storage_cfg, [$rootinfo->{volume
}]);
998 PVE
::LXC
::update_lxc_config
($storage_cfg, $vmid, $conf);
1000 my $cmd = ['lxc-start', '-n', $vmid];
1007 return $rpcenv->fork_worker('vzstart', $vmid, $authuser, $realcmd);
1011 __PACKAGE__-
>register_method({
1013 path
=> '{vmid}/status/stop',
1017 description
=> "Stop the container.",
1019 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1022 additionalProperties
=> 0,
1024 node
=> get_standard_option
('pve-node'),
1025 vmid
=> get_standard_option
('pve-vmid'),
1034 my $rpcenv = PVE
::RPCEnvironment
::get
();
1036 my $authuser = $rpcenv->get_user();
1038 my $node = extract_param
($param, 'node');
1040 my $vmid = extract_param
($param, 'vmid');
1042 die "CT $vmid not running\n" if !PVE
::LXC
::check_running
($vmid);
1044 if (PVE
::HA
::Config
::vm_is_ha_managed
($vmid) && $rpcenv->{type
} ne 'ha') {
1049 my $service = "ct:$vmid";
1051 my $cmd = ['ha-manager', 'disable', $service];
1053 print "Executing HA stop for CT $vmid\n";
1055 PVE
::Tools
::run_command
($cmd);
1060 return $rpcenv->fork_worker('hastop', $vmid, $authuser, $hacmd);
1067 syslog
('info', "stoping CT $vmid: $upid\n");
1069 my $conf = PVE
::LXC
::load_config
($vmid);
1071 my $storage_cfg = PVE
::Storage
::config
();
1073 my $cmd = ['lxc-stop', '-n', $vmid, '--kill'];
1077 PVE
::LXC
::vm_stop_cleanup
($storage_cfg, $vmid, $conf);
1082 return $rpcenv->fork_worker('vzstop', $vmid, $authuser, $realcmd);
1086 __PACKAGE__-
>register_method({
1087 name
=> 'vm_shutdown',
1088 path
=> '{vmid}/status/shutdown',
1092 description
=> "Shutdown the container.",
1094 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1097 additionalProperties
=> 0,
1099 node
=> get_standard_option
('pve-node'),
1100 vmid
=> get_standard_option
('pve-vmid'),
1102 description
=> "Wait maximal timeout seconds.",
1109 description
=> "Make sure the Container stops.",
1122 my $rpcenv = PVE
::RPCEnvironment
::get
();
1124 my $authuser = $rpcenv->get_user();
1126 my $node = extract_param
($param, 'node');
1128 my $vmid = extract_param
($param, 'vmid');
1130 my $timeout = extract_param
($param, 'timeout');
1132 die "CT $vmid not running\n" if !PVE
::LXC
::check_running
($vmid);
1137 syslog
('info', "shutdown CT $vmid: $upid\n");
1139 my $cmd = ['lxc-stop', '-n', $vmid];
1141 $timeout = 60 if !defined($timeout);
1143 my $conf = PVE
::LXC
::load_config
($vmid);
1145 my $storage_cfg = PVE
::Storage
::config
();
1147 push @$cmd, '--timeout', $timeout;
1149 eval { run_command
($cmd, timeout
=> $timeout+5); };
1151 if ($err && $param->{forceStop
}) {
1153 warn "shutdown failed - forcing stop now\n";
1155 push @$cmd, '--kill';
1160 PVE
::LXC
::vm_stop_cleanup
($storage_cfg, $vmid, $conf);
1167 my $upid = $rpcenv->fork_worker('vzshutdown', $vmid, $authuser, $realcmd);
1172 __PACKAGE__-
>register_method({
1173 name
=> 'vm_suspend',
1174 path
=> '{vmid}/status/suspend',
1178 description
=> "Suspend the container.",
1180 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1183 additionalProperties
=> 0,
1185 node
=> get_standard_option
('pve-node'),
1186 vmid
=> get_standard_option
('pve-vmid'),
1195 my $rpcenv = PVE
::RPCEnvironment
::get
();
1197 my $authuser = $rpcenv->get_user();
1199 my $node = extract_param
($param, 'node');
1201 my $vmid = extract_param
($param, 'vmid');
1203 die "CT $vmid not running\n" if !PVE
::LXC
::check_running
($vmid);
1208 syslog
('info', "suspend CT $vmid: $upid\n");
1210 my $cmd = ['lxc-checkpoint', '-n', $vmid, '-s', '-D', '/var/liv/vz/dump'];
1217 my $upid = $rpcenv->fork_worker('vzsuspend', $vmid, $authuser, $realcmd);
1222 __PACKAGE__-
>register_method({
1223 name
=> 'vm_resume',
1224 path
=> '{vmid}/status/resume',
1228 description
=> "Resume the container.",
1230 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1233 additionalProperties
=> 0,
1235 node
=> get_standard_option
('pve-node'),
1236 vmid
=> get_standard_option
('pve-vmid'),
1245 my $rpcenv = PVE
::RPCEnvironment
::get
();
1247 my $authuser = $rpcenv->get_user();
1249 my $node = extract_param
($param, 'node');
1251 my $vmid = extract_param
($param, 'vmid');
1253 die "CT $vmid already running\n" if PVE
::LXC
::check_running
($vmid);
1258 syslog
('info', "resume CT $vmid: $upid\n");
1260 my $cmd = ['lxc-checkpoint', '-n', $vmid, '-r', '--foreground',
1261 '-D', '/var/liv/vz/dump'];
1268 my $upid = $rpcenv->fork_worker('vzresume', $vmid, $authuser, $realcmd);
1273 __PACKAGE__-
>register_method({
1274 name
=> 'migrate_vm',
1275 path
=> '{vmid}/migrate',
1279 description
=> "Migrate the container to another node. Creates a new migration task.",
1281 check
=> ['perm', '/vms/{vmid}', [ 'VM.Migrate' ]],
1284 additionalProperties
=> 0,
1286 node
=> get_standard_option
('pve-node'),
1287 vmid
=> get_standard_option
('pve-vmid'),
1288 target
=> get_standard_option
('pve-node', { description
=> "Target node." }),
1291 description
=> "Use online/live migration.",
1298 description
=> "the task ID.",
1303 my $rpcenv = PVE
::RPCEnvironment
::get
();
1305 my $authuser = $rpcenv->get_user();
1307 my $target = extract_param
($param, 'target');
1309 my $localnode = PVE
::INotify
::nodename
();
1310 raise_param_exc
({ target
=> "target is local node."}) if $target eq $localnode;
1312 PVE
::Cluster
::check_cfs_quorum
();
1314 PVE
::Cluster
::check_node_exists
($target);
1316 my $targetip = PVE
::Cluster
::remote_node_ip
($target);
1318 my $vmid = extract_param
($param, 'vmid');
1321 PVE
::LXC
::load_config
($vmid);
1323 # try to detect errors early
1324 if (PVE
::LXC
::check_running
($vmid)) {
1325 die "cant migrate running container without --online\n"
1326 if !$param->{online
};
1329 if (PVE
::HA
::Config
::vm_is_ha_managed
($vmid) && $rpcenv->{type
} ne 'ha') {
1334 my $service = "ct:$vmid";
1336 my $cmd = ['ha-manager', 'migrate', $service, $target];
1338 print "Executing HA migrate for CT $vmid to node $target\n";
1340 PVE
::Tools
::run_command
($cmd);
1345 return $rpcenv->fork_worker('hamigrate', $vmid, $authuser, $hacmd);
1352 # fixme: implement lxc container migration
1353 die "lxc container migration not implemented\n";
1358 return $rpcenv->fork_worker('vzmigrate', $vmid, $authuser, $realcmd);
1362 __PACKAGE__-
>register_method({
1364 path
=> '{vmid}/snapshot',
1368 description
=> "Snapshot a container.",
1370 check
=> ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
1373 additionalProperties
=> 0,
1375 node
=> get_standard_option
('pve-node'),
1376 vmid
=> get_standard_option
('pve-vmid'),
1377 snapname
=> get_standard_option
('pve-lxc-snapshot-name'),
1380 # type => 'boolean',
1381 # description => "Save the vmstate",
1386 description
=> "A textual description or comment.",
1392 description
=> "the task ID.",
1397 my $rpcenv = PVE
::RPCEnvironment
::get
();
1399 my $authuser = $rpcenv->get_user();
1401 my $node = extract_param
($param, 'node');
1403 my $vmid = extract_param
($param, 'vmid');
1405 my $snapname = extract_param
($param, 'snapname');
1407 die "unable to use snapshot name 'current' (reserved name)\n"
1408 if $snapname eq 'current';
1411 PVE
::Cluster
::log_msg
('info', $authuser, "snapshot container $vmid: $snapname");
1412 PVE
::LXC
::snapshot_create
($vmid, $snapname, $param->{description
});
1415 return $rpcenv->fork_worker('pctsnapshot', $vmid, $authuser, $realcmd);
1418 __PACKAGE__-
>register_method({
1419 name
=> 'delsnapshot',
1420 path
=> '{vmid}/snapshot/{snapname}',
1424 description
=> "Delete a LXC snapshot.",
1426 check
=> ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
1429 additionalProperties
=> 0,
1431 node
=> get_standard_option
('pve-node'),
1432 vmid
=> get_standard_option
('pve-vmid'),
1433 snapname
=> get_standard_option
('pve-lxc-snapshot-name'),
1437 description
=> "For removal from config file, even if removing disk snapshots fails.",
1443 description
=> "the task ID.",
1448 my $rpcenv = PVE
::RPCEnvironment
::get
();
1450 my $authuser = $rpcenv->get_user();
1452 my $node = extract_param
($param, 'node');
1454 my $vmid = extract_param
($param, 'vmid');
1456 my $snapname = extract_param
($param, 'snapname');
1459 PVE
::Cluster
::log_msg
('info', $authuser, "delete snapshot VM $vmid: $snapname");
1460 PVE
::LXC
::snapshot_delete
($vmid, $snapname, $param->{force
});
1463 return $rpcenv->fork_worker('lxcdelsnapshot', $vmid, $authuser, $realcmd);
1466 __PACKAGE__-
>register_method({
1468 path
=> '{vmid}/snapshot/{snapname}/rollback',
1472 description
=> "Rollback LXC state to specified snapshot.",
1474 check
=> ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
1477 additionalProperties
=> 0,
1479 node
=> get_standard_option
('pve-node'),
1480 vmid
=> get_standard_option
('pve-vmid'),
1481 snapname
=> get_standard_option
('pve-lxc-snapshot-name'),
1486 description
=> "the task ID.",
1491 my $rpcenv = PVE
::RPCEnvironment
::get
();
1493 my $authuser = $rpcenv->get_user();
1495 my $node = extract_param
($param, 'node');
1497 my $vmid = extract_param
($param, 'vmid');
1499 my $snapname = extract_param
($param, 'snapname');
1502 PVE
::Cluster
::log_msg
('info', $authuser, "rollback snapshot LXC $vmid: $snapname");
1503 PVE
::LXC
::snapshot_rollback
($vmid, $snapname);
1506 return $rpcenv->fork_worker('lxcrollback', $vmid, $authuser, $realcmd);
1509 __PACKAGE__-
>register_method({
1510 name
=> 'snapshot_list',
1511 path
=> '{vmid}/snapshot',
1513 description
=> "List all snapshots.",
1515 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
1518 protected
=> 1, # lxc pid files are only readable by root
1520 additionalProperties
=> 0,
1522 vmid
=> get_standard_option
('pve-vmid'),
1523 node
=> get_standard_option
('pve-node'),
1532 links
=> [ { rel
=> 'child', href
=> "{name}" } ],
1537 my $vmid = $param->{vmid
};
1539 my $conf = PVE
::LXC
::load_config
($vmid);
1540 my $snaphash = $conf->{snapshots
} || {};
1544 foreach my $name (keys %$snaphash) {
1545 my $d = $snaphash->{$name};
1548 snaptime
=> $d->{snaptime
} || 0,
1549 description
=> $d->{description
} || '',
1551 $item->{parent
} = $d->{parent
} if defined($d->{parent
});
1552 $item->{snapstate
} = $d->{snapstate
} if $d->{snapstate
};
1556 my $running = PVE
::LXC
::check_running
($vmid) ?
1 : 0;
1557 my $current = { name
=> 'current', digest
=> $conf->{digest
}, running
=> $running };
1558 $current->{parent
} = $conf->{parent
} if defined($conf->{parent
});
1560 push @$res, $current;
1565 __PACKAGE__-
>register_method({
1566 name
=> 'snapshot_cmd_idx',
1567 path
=> '{vmid}/snapshot/{snapname}',
1574 additionalProperties
=> 0,
1576 vmid
=> get_standard_option
('pve-vmid'),
1577 node
=> get_standard_option
('pve-node'),
1578 snapname
=> get_standard_option
('pve-lxc-snapshot-name'),
1587 links
=> [ { rel
=> 'child', href
=> "{cmd}" } ],
1594 push @$res, { cmd
=> 'rollback' };
1595 push @$res, { cmd
=> 'config' };
1600 __PACKAGE__-
>register_method({
1601 name
=> 'update_snapshot_config',
1602 path
=> '{vmid}/snapshot/{snapname}/config',
1606 description
=> "Update snapshot metadata.",
1608 check
=> ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
1611 additionalProperties
=> 0,
1613 node
=> get_standard_option
('pve-node'),
1614 vmid
=> get_standard_option
('pve-vmid'),
1615 snapname
=> get_standard_option
('pve-lxc-snapshot-name'),
1619 description
=> "A textual description or comment.",
1623 returns
=> { type
=> 'null' },
1627 my $rpcenv = PVE
::RPCEnvironment
::get
();
1629 my $authuser = $rpcenv->get_user();
1631 my $vmid = extract_param
($param, 'vmid');
1633 my $snapname = extract_param
($param, 'snapname');
1635 return undef if !defined($param->{description
});
1637 my $updatefn = sub {
1639 my $conf = PVE
::LXC
::load_config
($vmid);
1640 PVE
::LXC
::check_lock
($conf);
1642 my $snap = $conf->{snapshots
}->{$snapname};
1644 die "snapshot '$snapname' does not exist\n" if !defined($snap);
1646 $snap->{description
} = $param->{description
} if defined($param->{description
});
1648 PVE
::LXC
::write_config
($vmid, $conf, 1);
1651 PVE
::LXC
::lock_container
($vmid, 10, $updatefn);
1656 __PACKAGE__-
>register_method({
1657 name
=> 'get_snapshot_config',
1658 path
=> '{vmid}/snapshot/{snapname}/config',
1661 description
=> "Get snapshot configuration",
1663 check
=> ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
1666 additionalProperties
=> 0,
1668 node
=> get_standard_option
('pve-node'),
1669 vmid
=> get_standard_option
('pve-vmid'),
1670 snapname
=> get_standard_option
('pve-lxc-snapshot-name'),
1673 returns
=> { type
=> "object" },
1677 my $rpcenv = PVE
::RPCEnvironment
::get
();
1679 my $authuser = $rpcenv->get_user();
1681 my $vmid = extract_param
($param, 'vmid');
1683 my $snapname = extract_param
($param, 'snapname');
1685 my $conf = PVE
::LXC
::load_config
($vmid);
1687 my $snap = $conf->{snapshots
}->{$snapname};
1689 die "snapshot '$snapname' does not exist\n" if !defined($snap);
1691 delete $snap->{lxc
};
1696 __PACKAGE__-
>register_method({
1697 name
=> 'vm_feature',
1698 path
=> '{vmid}/feature',
1702 description
=> "Check if feature for virtual machine is available.",
1704 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
1707 additionalProperties
=> 0,
1709 node
=> get_standard_option
('pve-node'),
1710 vmid
=> get_standard_option
('pve-vmid'),
1712 description
=> "Feature to check.",
1714 enum
=> [ 'snapshot' ],
1716 snapname
=> get_standard_option
('pve-lxc-snapshot-name', {
1724 hasFeature
=> { type
=> 'boolean' },
1727 #items => { type => 'string' },
1734 my $node = extract_param
($param, 'node');
1736 my $vmid = extract_param
($param, 'vmid');
1738 my $snapname = extract_param
($param, 'snapname');
1740 my $feature = extract_param
($param, 'feature');
1742 my $conf = PVE
::LXC
::load_config
($vmid);
1745 my $snap = $conf->{snapshots
}->{$snapname};
1746 die "snapshot '$snapname' does not exist\n" if !defined($snap);
1749 my $storage_cfg = PVE
::Storage
::config
();
1750 #Maybe include later
1751 #my $nodelist = PVE::LXC::shared_nodes($conf, $storage_cfg);
1752 my $hasFeature = PVE
::LXC
::has_feature
($feature, $conf, $storage_cfg, $snapname);
1755 hasFeature
=> $hasFeature,
1756 #nodes => [ keys %$nodelist ],