]>
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
;
18 use PVE
::HA
::Env
::PVE2
;
20 use PVE
::JSONSchema
qw(get_standard_option);
21 use base
qw(PVE::RESTHandler);
23 use Data
::Dumper
; # fixme: remove
25 __PACKAGE__-
>register_method({
30 description
=> "Directory index",
35 additionalProperties
=> 0,
37 node
=> get_standard_option
('pve-node'),
38 vmid
=> get_standard_option
('pve-vmid'),
46 subdir
=> { type
=> 'string' },
49 links
=> [ { rel
=> 'child', href
=> "{subdir}" } ],
55 my $conf = PVE
::LXC
::load_config
($param->{vmid
});
58 { subdir
=> 'current' },
59 { subdir
=> 'start' },
61 { subdir
=> 'shutdown' },
62 { subdir
=> 'migrate' },
68 __PACKAGE__-
>register_method({
73 protected
=> 1, # openvz /proc entries are only readable by root
74 description
=> "Get virtual machine status.",
76 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
79 additionalProperties
=> 0,
81 node
=> get_standard_option
('pve-node'),
82 vmid
=> get_standard_option
('pve-vmid'),
85 returns
=> { type
=> 'object' },
90 my $conf = PVE
::LXC
::load_config
($param->{vmid
});
92 my $vmstatus = PVE
::LXC
::vmstatus
($param->{vmid
});
93 my $status = $vmstatus->{$param->{vmid
}};
95 $status->{ha
} = PVE
::HA
::Config
::vm_is_ha_managed
($param->{vmid
}) ?
1 : 0;
100 __PACKAGE__-
>register_method({
106 description
=> "Start the container.",
108 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
111 additionalProperties
=> 0,
113 node
=> get_standard_option
('pve-node'),
114 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid_stopped
}),
115 skiplock
=> get_standard_option
('skiplock'),
124 my $rpcenv = PVE
::RPCEnvironment
::get
();
126 my $authuser = $rpcenv->get_user();
128 my $node = extract_param
($param, 'node');
130 my $vmid = extract_param
($param, 'vmid');
132 my $skiplock = extract_param
($param, 'skiplock');
133 raise_param_exc
({ skiplock
=> "Only root may use this option." })
134 if $skiplock && $authuser ne 'root@pam';
136 die "CT $vmid already running\n" if PVE
::LXC
::check_running
($vmid);
138 PVE
::Cluster
::check_cfs_quorum
();
140 if (PVE
::HA
::Config
::vm_is_ha_managed
($vmid) && $rpcenv->{type
} ne 'ha') {
145 my $service = "ct:$vmid";
147 my $cmd = ['ha-manager', 'enable', $service];
149 print "Executing HA start for CT $vmid\n";
151 PVE
::Tools
::run_command
($cmd);
156 return $rpcenv->fork_worker('hastart', $vmid, $authuser, $hacmd);
164 syslog
('info', "starting CT $vmid: $upid\n");
166 my $conf = PVE
::LXC
::load_config
($vmid);
168 die "you can't start a CT if it's a template\n"
169 if PVE
::LXC
::is_template
($conf);
171 PVE
::LXC
::check_lock
($conf) if !$skiplock;
173 my $storage_cfg = cfs_read_file
("storage.cfg");
175 PVE
::LXC
::update_lxc_config
($storage_cfg, $vmid, $conf);
177 local $ENV{PVE_SKIPLOCK
}=1 if $skiplock;
179 my $cmd = ['lxc-start', '-n', $vmid];
186 return $rpcenv->fork_worker('vzstart', $vmid, $authuser, $realcmd);
189 return PVE
::LXC
::lock_config
($vmid, $lockcmd);
193 __PACKAGE__-
>register_method({
199 description
=> "Stop the container.",
201 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
204 additionalProperties
=> 0,
206 node
=> get_standard_option
('pve-node'),
207 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid_running
}),
208 skiplock
=> get_standard_option
('skiplock'),
217 my $rpcenv = PVE
::RPCEnvironment
::get
();
219 my $authuser = $rpcenv->get_user();
221 my $node = extract_param
($param, 'node');
223 my $vmid = extract_param
($param, 'vmid');
225 my $skiplock = extract_param
($param, 'skiplock');
226 raise_param_exc
({ skiplock
=> "Only root may use this option." })
227 if $skiplock && $authuser ne 'root@pam';
229 die "CT $vmid not running\n" if !PVE
::LXC
::check_running
($vmid);
231 if (PVE
::HA
::Config
::vm_is_ha_managed
($vmid) && $rpcenv->{type
} ne 'ha') {
236 my $service = "ct:$vmid";
238 my $cmd = ['ha-manager', 'disable', $service];
240 print "Executing HA stop for CT $vmid\n";
242 PVE
::Tools
::run_command
($cmd);
247 return $rpcenv->fork_worker('hastop', $vmid, $authuser, $hacmd);
255 syslog
('info', "stopping CT $vmid: $upid\n");
257 my $conf = PVE
::LXC
::load_config
($vmid);
259 PVE
::LXC
::check_lock
($conf) if !$skiplock;
261 my $cmd = ['lxc-stop', '-n', $vmid, '--kill'];
268 return $rpcenv->fork_worker('vzstop', $vmid, $authuser, $realcmd);
271 return PVE
::LXC
::lock_config
($vmid, $lockcmd);
275 __PACKAGE__-
>register_method({
276 name
=> 'vm_shutdown',
281 description
=> "Shutdown the container.",
283 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
286 additionalProperties
=> 0,
288 node
=> get_standard_option
('pve-node'),
289 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid_running
}),
291 description
=> "Wait maximal timeout seconds.",
298 description
=> "Make sure the Container stops.",
311 my $rpcenv = PVE
::RPCEnvironment
::get
();
313 my $authuser = $rpcenv->get_user();
315 my $node = extract_param
($param, 'node');
317 my $vmid = extract_param
($param, 'vmid');
319 my $timeout = extract_param
($param, 'timeout');
321 die "CT $vmid not running\n" if !PVE
::LXC
::check_running
($vmid);
327 syslog
('info', "shutdown CT $vmid: $upid\n");
329 my $cmd = ['lxc-stop', '-n', $vmid];
331 $timeout = 60 if !defined($timeout);
333 my $conf = PVE
::LXC
::load_config
($vmid);
335 PVE
::LXC
::check_lock
($conf);
337 my $storage_cfg = PVE
::Storage
::config
();
339 push @$cmd, '--timeout', $timeout;
341 eval { run_command
($cmd, timeout
=> $timeout+5); };
343 if ($err && $param->{forceStop
}) {
345 warn "shutdown failed - forcing stop now\n";
347 my $cmd = ['lxc-stop', '-n', $vmid, '--kill'];
351 # make sure container is stopped
352 $cmd = ['lxc-wait', '-n', $vmid, '-t', 5, '-s', 'STOPPED'];
361 return $rpcenv->fork_worker('vzshutdown', $vmid, $authuser, $realcmd);
364 return PVE
::LXC
::lock_config
($vmid, $lockcmd);
367 __PACKAGE__-
>register_method({
368 name
=> 'vm_suspend',
373 description
=> "Suspend the container.",
375 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
378 additionalProperties
=> 0,
380 node
=> get_standard_option
('pve-node'),
381 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid_running
}),
390 my $rpcenv = PVE
::RPCEnvironment
::get
();
392 my $authuser = $rpcenv->get_user();
394 my $node = extract_param
($param, 'node');
396 my $vmid = extract_param
($param, 'vmid');
398 die "CT $vmid not running\n" if !PVE
::LXC
::check_running
($vmid);
404 syslog
('info', "suspend CT $vmid: $upid\n");
406 my $conf = PVE
::LXC
::load_config
($vmid);
408 PVE
::LXC
::check_lock
($conf);
410 my $cmd = ['lxc-checkpoint', '-n', $vmid, '-s', '-D', '/var/lib/vz/dump'];
417 return $rpcenv->fork_worker('vzsuspend', $vmid, $authuser, $realcmd);
420 return PVE
::LXC
::lock_config
($vmid, $lockcmd);
423 __PACKAGE__-
>register_method({
429 description
=> "Resume the container.",
431 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
434 additionalProperties
=> 0,
436 node
=> get_standard_option
('pve-node'),
437 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid_stopped
}),
446 my $rpcenv = PVE
::RPCEnvironment
::get
();
448 my $authuser = $rpcenv->get_user();
450 my $node = extract_param
($param, 'node');
452 my $vmid = extract_param
($param, 'vmid');
454 die "CT $vmid already running\n" if PVE
::LXC
::check_running
($vmid);
459 syslog
('info', "resume CT $vmid: $upid\n");
461 my $cmd = ['lxc-checkpoint', '-n', $vmid, '-r', '--foreground',
462 '-D', '/var/lib/vz/dump'];
469 my $upid = $rpcenv->fork_worker('vzresume', $vmid, $authuser, $realcmd);