]>
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 PVE
::Cluster
::check_cfs_quorum
();
134 if (PVE
::HA
::Config
::vm_is_ha_managed
($vmid) && $rpcenv->{type
} ne 'ha') {
139 my $service = "ct:$vmid";
141 my $cmd = ['ha-manager', 'enable', $service];
143 print "Executing HA start for CT $vmid\n";
145 PVE
::Tools
::run_command
($cmd);
150 return $rpcenv->fork_worker('hastart', $vmid, $authuser, $hacmd);
157 syslog
('info', "starting CT $vmid: $upid\n");
159 my $conf = PVE
::LXC
::load_config
($vmid);
161 die "you can't start a CT if it's a template\n"
162 if PVE
::LXC
::is_template
($conf);
164 my $storage_cfg = cfs_read_file
("storage.cfg");
166 PVE
::LXC
::update_lxc_config
($storage_cfg, $vmid, $conf);
168 my $cmd = ['lxc-start', '-n', $vmid];
175 return $rpcenv->fork_worker('vzstart', $vmid, $authuser, $realcmd);
179 __PACKAGE__-
>register_method({
185 description
=> "Stop the container.",
187 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
190 additionalProperties
=> 0,
192 node
=> get_standard_option
('pve-node'),
193 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid_running
}),
202 my $rpcenv = PVE
::RPCEnvironment
::get
();
204 my $authuser = $rpcenv->get_user();
206 my $node = extract_param
($param, 'node');
208 my $vmid = extract_param
($param, 'vmid');
210 die "CT $vmid not running\n" if !PVE
::LXC
::check_running
($vmid);
212 if (PVE
::HA
::Config
::vm_is_ha_managed
($vmid) && $rpcenv->{type
} ne 'ha') {
217 my $service = "ct:$vmid";
219 my $cmd = ['ha-manager', 'disable', $service];
221 print "Executing HA stop for CT $vmid\n";
223 PVE
::Tools
::run_command
($cmd);
228 return $rpcenv->fork_worker('hastop', $vmid, $authuser, $hacmd);
235 syslog
('info', "stopping CT $vmid: $upid\n");
237 my $cmd = ['lxc-stop', '-n', $vmid, '--kill'];
244 return $rpcenv->fork_worker('vzstop', $vmid, $authuser, $realcmd);
248 __PACKAGE__-
>register_method({
249 name
=> 'vm_shutdown',
254 description
=> "Shutdown the container.",
256 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
259 additionalProperties
=> 0,
261 node
=> get_standard_option
('pve-node'),
262 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid_running
}),
264 description
=> "Wait maximal timeout seconds.",
271 description
=> "Make sure the Container stops.",
284 my $rpcenv = PVE
::RPCEnvironment
::get
();
286 my $authuser = $rpcenv->get_user();
288 my $node = extract_param
($param, 'node');
290 my $vmid = extract_param
($param, 'vmid');
292 my $timeout = extract_param
($param, 'timeout');
294 die "CT $vmid not running\n" if !PVE
::LXC
::check_running
($vmid);
299 syslog
('info', "shutdown CT $vmid: $upid\n");
301 my $cmd = ['lxc-stop', '-n', $vmid];
303 $timeout = 60 if !defined($timeout);
305 my $conf = PVE
::LXC
::load_config
($vmid);
307 my $storage_cfg = PVE
::Storage
::config
();
309 push @$cmd, '--timeout', $timeout;
311 eval { run_command
($cmd, timeout
=> $timeout+5); };
313 if ($err && $param->{forceStop
}) {
315 warn "shutdown failed - forcing stop now\n";
317 my $cmd = ['lxc-stop', '-n', $vmid, '--kill'];
321 # make sure container is stopped
322 $cmd = ['lxc-wait', '-n', $vmid, '-t', 5, '-s', 'STOPPED'];
330 my $upid = $rpcenv->fork_worker('vzshutdown', $vmid, $authuser, $realcmd);
335 __PACKAGE__-
>register_method({
336 name
=> 'vm_suspend',
341 description
=> "Suspend the container.",
343 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
346 additionalProperties
=> 0,
348 node
=> get_standard_option
('pve-node'),
349 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid_running
}),
358 my $rpcenv = PVE
::RPCEnvironment
::get
();
360 my $authuser = $rpcenv->get_user();
362 my $node = extract_param
($param, 'node');
364 my $vmid = extract_param
($param, 'vmid');
366 die "CT $vmid not running\n" if !PVE
::LXC
::check_running
($vmid);
371 syslog
('info', "suspend CT $vmid: $upid\n");
373 my $cmd = ['lxc-checkpoint', '-n', $vmid, '-s', '-D', '/var/liv/vz/dump'];
380 my $upid = $rpcenv->fork_worker('vzsuspend', $vmid, $authuser, $realcmd);
385 __PACKAGE__-
>register_method({
391 description
=> "Resume the container.",
393 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
396 additionalProperties
=> 0,
398 node
=> get_standard_option
('pve-node'),
399 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid_stopped
}),
408 my $rpcenv = PVE
::RPCEnvironment
::get
();
410 my $authuser = $rpcenv->get_user();
412 my $node = extract_param
($param, 'node');
414 my $vmid = extract_param
($param, 'vmid');
416 die "CT $vmid already running\n" if PVE
::LXC
::check_running
($vmid);
421 syslog
('info', "resume CT $vmid: $upid\n");
423 my $cmd = ['lxc-checkpoint', '-n', $vmid, '-r', '--foreground',
424 '-D', '/var/liv/vz/dump'];
431 my $upid = $rpcenv->fork_worker('vzresume', $vmid, $authuser, $realcmd);