]>
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 = ['unshare', '-m', '--',
167 'sh', '-c', "mount --make-rslave / && exec lxc-start -n $vmid"];
174 return $rpcenv->fork_worker('vzstart', $vmid, $authuser, $realcmd);
178 __PACKAGE__-
>register_method({
184 description
=> "Stop the container.",
186 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
189 additionalProperties
=> 0,
191 node
=> get_standard_option
('pve-node'),
192 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid_running
}),
201 my $rpcenv = PVE
::RPCEnvironment
::get
();
203 my $authuser = $rpcenv->get_user();
205 my $node = extract_param
($param, 'node');
207 my $vmid = extract_param
($param, 'vmid');
209 die "CT $vmid not running\n" if !PVE
::LXC
::check_running
($vmid);
211 if (PVE
::HA
::Config
::vm_is_ha_managed
($vmid) && $rpcenv->{type
} ne 'ha') {
216 my $service = "ct:$vmid";
218 my $cmd = ['ha-manager', 'disable', $service];
220 print "Executing HA stop for CT $vmid\n";
222 PVE
::Tools
::run_command
($cmd);
227 return $rpcenv->fork_worker('hastop', $vmid, $authuser, $hacmd);
234 syslog
('info', "stopping CT $vmid: $upid\n");
236 my $cmd = ['lxc-stop', '-n', $vmid, '--kill'];
243 return $rpcenv->fork_worker('vzstop', $vmid, $authuser, $realcmd);
247 __PACKAGE__-
>register_method({
248 name
=> 'vm_shutdown',
253 description
=> "Shutdown the container.",
255 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
258 additionalProperties
=> 0,
260 node
=> get_standard_option
('pve-node'),
261 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid_running
}),
263 description
=> "Wait maximal timeout seconds.",
270 description
=> "Make sure the Container stops.",
283 my $rpcenv = PVE
::RPCEnvironment
::get
();
285 my $authuser = $rpcenv->get_user();
287 my $node = extract_param
($param, 'node');
289 my $vmid = extract_param
($param, 'vmid');
291 my $timeout = extract_param
($param, 'timeout');
293 die "CT $vmid not running\n" if !PVE
::LXC
::check_running
($vmid);
298 syslog
('info', "shutdown CT $vmid: $upid\n");
300 my $cmd = ['lxc-stop', '-n', $vmid];
302 $timeout = 60 if !defined($timeout);
304 my $conf = PVE
::LXC
::load_config
($vmid);
306 my $storage_cfg = PVE
::Storage
::config
();
308 push @$cmd, '--timeout', $timeout;
310 eval { run_command
($cmd, timeout
=> $timeout+5); };
312 if ($err && $param->{forceStop
}) {
314 warn "shutdown failed - forcing stop now\n";
316 my $cmd = ['lxc-stop', '-n', $vmid, '--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);