]> git.proxmox.com Git - pve-manager.git/blob - PVE/API2/VZDump.pm
vzdump: getlock: return lock file handle and let the caller close it
[pve-manager.git] / PVE / API2 / VZDump.pm
1 package PVE::API2::VZDump;
2
3 use strict;
4 use warnings;
5 use PVE::Exception qw(raise_param_exc);
6 use PVE::Tools qw(extract_param);
7 use PVE::Cluster qw(cfs_register_file cfs_read_file);
8 use PVE::INotify;
9 use PVE::RPCEnvironment;
10 use PVE::AccessControl;
11 use PVE::JSONSchema qw(get_standard_option);
12 use PVE::Storage;
13 use PVE::VZDump;
14 use PVE::VZDump::Common;
15 use PVE::API2Tools;
16
17 use Data::Dumper; # fixme: remove
18
19
20 use base qw(PVE::RESTHandler);
21
22 __PACKAGE__->register_method ({
23 name => 'vzdump',
24 path => '',
25 method => 'POST',
26 description => "Create backup.",
27 permissions => {
28 description => "The user needs 'VM.Backup' permissions on any VM, and 'Datastore.AllocateSpace' on the backup storage. The 'maxfiles', 'prune-backups', 'tmpdir', 'dumpdir', 'script', 'bwlimit' and 'ionice' parameters are restricted to the 'root\@pam' user.",
29 user => 'all',
30 },
31 protected => 1,
32 proxyto => 'node',
33 parameters => {
34 additionalProperties => 0,
35 properties => PVE::VZDump::Common::json_config_properties({
36 stdout => {
37 type => 'boolean',
38 description => "Write tar to stdout, not to a file.",
39 optional => 1,
40 },
41 }),
42 },
43 returns => { type => 'string' },
44 code => sub {
45 my ($param) = @_;
46
47 my $rpcenv = PVE::RPCEnvironment::get();
48
49 my $user = $rpcenv->get_user();
50
51 my $nodename = PVE::INotify::nodename();
52
53 if ($rpcenv->{type} ne 'cli') {
54 raise_param_exc({ node => "option is only allowed on the command line interface."})
55 if $param->{node} && $param->{node} ne $nodename;
56
57 raise_param_exc({ stdout => "option is only allowed on the command line interface."})
58 if $param->{stdout};
59 }
60
61 foreach my $key (qw(maxfiles prune-backups tmpdir dumpdir script bwlimit ionice)) {
62 raise_param_exc({ $key => "Only root may set this option."})
63 if defined($param->{$key}) && ($user ne 'root@pam');
64 }
65
66 PVE::VZDump::verify_vzdump_parameters($param, 1);
67
68 # silent exit if we run on wrong node
69 return 'OK' if $param->{node} && $param->{node} ne $nodename;
70
71 my $cmdline = PVE::VZDump::Common::command_line($param);
72
73 my $vmids_per_node = PVE::VZDump::get_included_guests($param);
74
75 my $local_vmids = delete $vmids_per_node->{$nodename} // [];
76
77 # include IDs for deleted guests, and visibly fail later
78 my $orphaned_vmids = delete $vmids_per_node->{''} // [];
79 push @{$local_vmids}, @{$orphaned_vmids};
80
81 my $skiplist = [ map { @$_ } values $vmids_per_node->%* ];
82
83 if($param->{stop}){
84 PVE::VZDump::stop_running_backups();
85 return 'OK' if !scalar(@{$local_vmids});
86 }
87
88 # silent exit if specified VMs run on other nodes
89 return "OK" if !scalar(@{$local_vmids}) && !$param->{all};
90
91 PVE::VZDump::parse_mailto_exclude_path($param);
92
93 die "you can only backup a single VM with option --stdout\n"
94 if $param->{stdout} && scalar(@{$local_vmids}) != 1;
95
96 $rpcenv->check($user, "/storage/$param->{storage}", [ 'Datastore.AllocateSpace' ])
97 if $param->{storage};
98
99 my $worker = sub {
100 my $upid = shift;
101
102 $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = $SIG{HUP} = $SIG{PIPE} = sub {
103 die "interrupted by signal\n";
104 };
105
106 $param->{vmids} = $local_vmids;
107 my $vzdump = PVE::VZDump->new($cmdline, $param, $skiplist);
108
109 my $LOCK_FH = eval {
110 $vzdump->getlock($upid); # only one process allowed
111 };
112 if (my $err = $@) {
113 $vzdump->sendmail([], 0, $err);
114 exit(-1);
115 }
116
117 if (defined($param->{ionice})) {
118 if ($param->{ionice} > 7) {
119 PVE::VZDump::run_command(undef, "ionice -c3 -p $$");
120 } else {
121 PVE::VZDump::run_command(undef, "ionice -c2 -n$param->{ionice} -p $$");
122 }
123 }
124 $vzdump->exec_backup($rpcenv, $user);
125
126 close($LOCK_FH);
127 };
128
129 open STDOUT, '>/dev/null' if $param->{quiet} && !$param->{stdout};
130 open STDERR, '>/dev/null' if $param->{quiet};
131
132 if ($rpcenv->{type} eq 'cli') {
133 if ($param->{stdout}) {
134
135 open my $saved_stdout, ">&STDOUT"
136 || die "can't dup STDOUT: $!\n";
137
138 open STDOUT, '>&STDERR' ||
139 die "unable to redirect STDOUT: $!\n";
140
141 $param->{stdout} = $saved_stdout;
142 }
143 }
144
145 my $taskid;
146 $taskid = $local_vmids->[0] if scalar(@{$local_vmids}) == 1;
147
148 return $rpcenv->fork_worker('vzdump', $taskid, $user, $worker);
149 }});
150
151 __PACKAGE__->register_method ({
152 name => 'extractconfig',
153 path => 'extractconfig',
154 method => 'GET',
155 description => "Extract configuration from vzdump backup archive.",
156 permissions => {
157 description => "The user needs 'VM.Backup' permissions on the backed up guest ID, and 'Datastore.AllocateSpace' on the backup storage.",
158 user => 'all',
159 },
160 protected => 1,
161 proxyto => 'node',
162 parameters => {
163 additionalProperties => 0,
164 properties => {
165 node => get_standard_option('pve-node'),
166 volume => {
167 description => "Volume identifier",
168 type => 'string',
169 completion => \&PVE::Storage::complete_volume,
170 },
171 },
172 },
173 returns => { type => 'string' },
174 code => sub {
175 my ($param) = @_;
176
177 my $volume = extract_param($param, 'volume');
178
179 my $rpcenv = PVE::RPCEnvironment::get();
180 my $authuser = $rpcenv->get_user();
181
182 my $storage_cfg = PVE::Storage::config();
183 PVE::Storage::check_volume_access($rpcenv, $authuser, $storage_cfg, undef, $volume);
184
185 return PVE::Storage::extract_vzdump_config($storage_cfg, $volume);
186 }});
187
188 1;