]>
git.proxmox.com Git - pve-container.git/blob - src/PVE/API2/LXC/Status.pm
1 package PVE
::API2
::LXC
::Status
;
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
;
15 use PVE
::RPCEnvironment
;
19 use PVE
::JSONSchema
qw(get_standard_option);
20 use base
qw(PVE::RESTHandler);
22 use Data
::Dumper
; # fixme: remove
24 __PACKAGE__-
>register_method({
29 description
=> "Directory index",
34 additionalProperties
=> 0,
36 node
=> get_standard_option
('pve-node'),
37 vmid
=> get_standard_option
('pve-vmid'),
45 subdir
=> { type
=> 'string' },
48 links
=> [ { rel
=> 'child', href
=> "{subdir}" } ],
54 my $conf = PVE
::LXC
::load_config
($param->{vmid
});
57 { subdir
=> 'current' },
58 { subdir
=> 'start' },
60 { subdir
=> 'shutdown' },
61 { subdir
=> 'migrate' },
67 __PACKAGE__-
>register_method({
72 protected
=> 1, # openvz /proc entries are only readable by root
73 description
=> "Get virtual machine status.",
75 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
78 additionalProperties
=> 0,
80 node
=> get_standard_option
('pve-node'),
81 vmid
=> get_standard_option
('pve-vmid'),
84 returns
=> { type
=> 'object' },
89 my $conf = PVE
::LXC
::load_config
($param->{vmid
});
91 my $vmstatus = PVE
::LXC
::vmstatus
($param->{vmid
});
92 my $status = $vmstatus->{$param->{vmid
}};
94 $status->{ha
} = PVE
::HA
::Config
::vm_is_ha_managed
($param->{vmid
}) ?
1 : 0;
99 __PACKAGE__-
>register_method({
105 description
=> "Start the container.",
107 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
110 additionalProperties
=> 0,
112 node
=> get_standard_option
('pve-node'),
113 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid_stopped
}),
122 my $rpcenv = PVE
::RPCEnvironment
::get
();
124 my $authuser = $rpcenv->get_user();
126 my $node = extract_param
($param, 'node');
128 my $vmid = extract_param
($param, 'vmid');
130 die "CT $vmid already running\n" if PVE
::LXC
::check_running
($vmid);
132 if (PVE
::HA
::Config
::vm_is_ha_managed
($vmid) && $rpcenv->{type
} ne 'ha') {
137 my $service = "ct:$vmid";
139 my $cmd = ['ha-manager', 'enable', $service];
141 print "Executing HA start for CT $vmid\n";
143 PVE
::Tools
::run_command
($cmd);
148 return $rpcenv->fork_worker('hastart', $vmid, $authuser, $hacmd);
155 syslog
('info', "starting CT $vmid: $upid\n");
157 my $conf = PVE
::LXC
::load_config
($vmid);
159 die "you can't start a CT if it's a template\n"
160 if PVE
::LXC
::is_template
($conf);
162 my $storage_cfg = cfs_read_file
("storage.cfg");
164 PVE
::LXC
::update_lxc_config
($storage_cfg, $vmid, $conf);
166 my $cmd = ['lxc-start', '-n', $vmid];
173 return $rpcenv->fork_worker('vzstart', $vmid, $authuser, $realcmd);
177 __PACKAGE__-
>register_method({
183 description
=> "Stop the container.",
185 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
188 additionalProperties
=> 0,
190 node
=> get_standard_option
('pve-node'),
191 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid_running
}),
200 my $rpcenv = PVE
::RPCEnvironment
::get
();
202 my $authuser = $rpcenv->get_user();
204 my $node = extract_param
($param, 'node');
206 my $vmid = extract_param
($param, 'vmid');
208 die "CT $vmid not running\n" if !PVE
::LXC
::check_running
($vmid);
210 if (PVE
::HA
::Config
::vm_is_ha_managed
($vmid) && $rpcenv->{type
} ne 'ha') {
215 my $service = "ct:$vmid";
217 my $cmd = ['ha-manager', 'disable', $service];
219 print "Executing HA stop for CT $vmid\n";
221 PVE
::Tools
::run_command
($cmd);
226 return $rpcenv->fork_worker('hastop', $vmid, $authuser, $hacmd);
233 syslog
('info', "stopping CT $vmid: $upid\n");
235 my $cmd = ['lxc-stop', '-n', $vmid, '--kill'];
242 return $rpcenv->fork_worker('vzstop', $vmid, $authuser, $realcmd);
246 __PACKAGE__-
>register_method({
247 name
=> 'vm_shutdown',
252 description
=> "Shutdown the container.",
254 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
257 additionalProperties
=> 0,
259 node
=> get_standard_option
('pve-node'),
260 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid_running
}),
262 description
=> "Wait maximal timeout seconds.",
269 description
=> "Make sure the Container stops.",
282 my $rpcenv = PVE
::RPCEnvironment
::get
();
284 my $authuser = $rpcenv->get_user();
286 my $node = extract_param
($param, 'node');
288 my $vmid = extract_param
($param, 'vmid');
290 my $timeout = extract_param
($param, 'timeout');
292 die "CT $vmid not running\n" if !PVE
::LXC
::check_running
($vmid);
297 syslog
('info', "shutdown CT $vmid: $upid\n");
299 my $cmd = ['lxc-stop', '-n', $vmid];
301 $timeout = 60 if !defined($timeout);
303 my $conf = PVE
::LXC
::load_config
($vmid);
305 my $storage_cfg = PVE
::Storage
::config
();
307 push @$cmd, '--timeout', $timeout;
309 eval { run_command
($cmd, timeout
=> $timeout+5); };
311 if ($err && $param->{forceStop
}) {
313 warn "shutdown failed - forcing stop now\n";
315 push @$cmd, '--kill';
325 my $upid = $rpcenv->fork_worker('vzshutdown', $vmid, $authuser, $realcmd);
330 __PACKAGE__-
>register_method({
331 name
=> 'vm_suspend',
336 description
=> "Suspend the container.",
338 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
341 additionalProperties
=> 0,
343 node
=> get_standard_option
('pve-node'),
344 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid_running
}),
353 my $rpcenv = PVE
::RPCEnvironment
::get
();
355 my $authuser = $rpcenv->get_user();
357 my $node = extract_param
($param, 'node');
359 my $vmid = extract_param
($param, 'vmid');
361 die "CT $vmid not running\n" if !PVE
::LXC
::check_running
($vmid);
366 syslog
('info', "suspend CT $vmid: $upid\n");
368 my $cmd = ['lxc-checkpoint', '-n', $vmid, '-s', '-D', '/var/liv/vz/dump'];
375 my $upid = $rpcenv->fork_worker('vzsuspend', $vmid, $authuser, $realcmd);
380 __PACKAGE__-
>register_method({
386 description
=> "Resume the container.",
388 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
391 additionalProperties
=> 0,
393 node
=> get_standard_option
('pve-node'),
394 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid_stopped
}),
403 my $rpcenv = PVE
::RPCEnvironment
::get
();
405 my $authuser = $rpcenv->get_user();
407 my $node = extract_param
($param, 'node');
409 my $vmid = extract_param
($param, 'vmid');
411 die "CT $vmid already running\n" if PVE
::LXC
::check_running
($vmid);
416 syslog
('info', "resume CT $vmid: $upid\n");
418 my $cmd = ['lxc-checkpoint', '-n', $vmid, '-r', '--foreground',
419 '-D', '/var/liv/vz/dump'];
426 my $upid = $rpcenv->fork_worker('vzresume', $vmid, $authuser, $realcmd);