]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/DirPlugin.pm
makefile: convert to use simple parenthesis
[pve-storage.git] / PVE / Storage / DirPlugin.pm
1 package PVE::Storage::DirPlugin;
2
3 use strict;
4 use warnings;
5
6 use Cwd;
7 use Encode qw(decode encode);
8 use File::Path;
9 use IO::File;
10 use POSIX;
11
12 use PVE::Storage::Plugin;
13 use PVE::JSONSchema qw(get_standard_option);
14
15 use base qw(PVE::Storage::Plugin);
16
17 # Configuration
18
19 sub type {
20 return 'dir';
21 }
22
23 sub plugindata {
24 return {
25 content => [ { images => 1, rootdir => 1, vztmpl => 1, iso => 1, backup => 1, snippets => 1, none => 1 },
26 { images => 1, rootdir => 1 }],
27 format => [ { raw => 1, qcow2 => 1, vmdk => 1, subvol => 1 } , 'raw' ],
28 };
29 }
30
31 sub properties {
32 return {
33 path => {
34 description => "File system path.",
35 type => 'string', format => 'pve-storage-path',
36 },
37 mkdir => {
38 description => "Create the directory if it doesn't exist.",
39 type => 'boolean',
40 default => 'yes',
41 },
42 is_mountpoint => {
43 description =>
44 "Assume the given path is an externally managed mountpoint " .
45 "and consider the storage offline if it is not mounted. ".
46 "Using a boolean (yes/no) value serves as a shortcut to using the target path in this field.",
47 type => 'string',
48 default => 'no',
49 },
50 bwlimit => get_standard_option('bwlimit'),
51 };
52 }
53
54 sub options {
55 return {
56 path => { fixed => 1 },
57 'content-dirs' => { optional => 1 },
58 nodes => { optional => 1 },
59 shared => { optional => 1 },
60 disable => { optional => 1 },
61 maxfiles => { optional => 1 },
62 'prune-backups' => { optional => 1 },
63 'max-protected-backups' => { optional => 1 },
64 content => { optional => 1 },
65 format => { optional => 1 },
66 mkdir => { optional => 1 },
67 is_mountpoint => { optional => 1 },
68 bwlimit => { optional => 1 },
69 preallocation => { optional => 1 },
70 };
71 }
72
73 # Storage implementation
74 #
75
76 # NOTE: should ProcFSTools::is_mounted accept an optional cache like this?
77 sub path_is_mounted {
78 my ($mountpoint, $mountdata) = @_;
79
80 $mountpoint = Cwd::realpath($mountpoint); # symlinks
81 return 0 if !defined($mountpoint); # path does not exist
82
83 $mountdata = PVE::ProcFSTools::parse_proc_mounts() if !$mountdata;
84 return 1 if grep { $_->[1] eq $mountpoint } @$mountdata;
85 return undef;
86 }
87
88 sub parse_is_mountpoint {
89 my ($scfg) = @_;
90 my $is_mp = $scfg->{is_mountpoint};
91 return undef if !defined $is_mp;
92 if (defined(my $bool = PVE::JSONSchema::parse_boolean($is_mp))) {
93 return $bool ? $scfg->{path} : undef;
94 }
95 return $is_mp; # contains a path
96 }
97
98 # FIXME move into 'get_volume_attribute' when removing 'get_volume_notes'
99 my $get_volume_notes_impl = sub {
100 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
101
102 my ($vtype) = $class->parse_volname($volname);
103 return if $vtype ne 'backup';
104
105 my $path = $class->filesystem_path($scfg, $volname);
106 $path .= $class->SUPER::NOTES_EXT;
107
108 if (-f $path) {
109 my $data = PVE::Tools::file_get_contents($path);
110 return eval { decode('UTF-8', $data, 1) } // $data;
111 }
112
113 return '';
114 };
115
116 # FIXME remove on the next APIAGE reset.
117 # Deprecated, use get_volume_attribute instead.
118 sub get_volume_notes {
119 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
120 return $get_volume_notes_impl->($class, $scfg, $storeid, $volname, $timeout);
121 }
122
123 # FIXME move into 'update_volume_attribute' when removing 'update_volume_notes'
124 my $update_volume_notes_impl = sub {
125 my ($class, $scfg, $storeid, $volname, $notes, $timeout) = @_;
126
127 my ($vtype) = $class->parse_volname($volname);
128 die "only backups can have notes\n" if $vtype ne 'backup';
129
130 my $path = $class->filesystem_path($scfg, $volname);
131 $path .= $class->SUPER::NOTES_EXT;
132
133 if (defined($notes) && $notes ne '') {
134 my $encoded = encode('UTF-8', $notes);
135 PVE::Tools::file_set_contents($path, $encoded);
136 } else {
137 unlink $path or $! == ENOENT or die "could not delete notes - $!\n";
138 }
139 return;
140 };
141
142 # FIXME remove on the next APIAGE reset.
143 # Deprecated, use update_volume_attribute instead.
144 sub update_volume_notes {
145 my ($class, $scfg, $storeid, $volname, $notes, $timeout) = @_;
146 return $update_volume_notes_impl->($class, $scfg, $storeid, $volname, $notes, $timeout);
147 }
148
149 sub get_volume_attribute {
150 my ($class, $scfg, $storeid, $volname, $attribute) = @_;
151
152 if ($attribute eq 'notes') {
153 return $get_volume_notes_impl->($class, $scfg, $storeid, $volname);
154 }
155
156 my ($vtype) = $class->parse_volname($volname);
157 return if $vtype ne 'backup';
158
159 if ($attribute eq 'protected') {
160 my $path = $class->filesystem_path($scfg, $volname);
161 return -e PVE::Storage::protection_file_path($path) ? 1 : 0;
162 }
163
164 return;
165 }
166
167 sub update_volume_attribute {
168 my ($class, $scfg, $storeid, $volname, $attribute, $value) = @_;
169
170 if ($attribute eq 'notes') {
171 return $update_volume_notes_impl->($class, $scfg, $storeid, $volname, $value);
172 }
173
174 my ($vtype) = $class->parse_volname($volname);
175 die "only backups support attribute '$attribute'\n" if $vtype ne 'backup';
176
177 if ($attribute eq 'protected') {
178 my $path = $class->filesystem_path($scfg, $volname);
179 my $protection_path = PVE::Storage::protection_file_path($path);
180
181 return if !((-e $protection_path) xor $value); # protection status already correct
182
183 if ($value) {
184 my $fh = IO::File->new($protection_path, O_CREAT, 0644)
185 or die "unable to create protection file '$protection_path' - $!\n";
186 close($fh);
187 } else {
188 unlink $protection_path or $! == ENOENT
189 or die "could not delete protection file '$protection_path' - $!\n";
190 }
191
192 return;
193 }
194
195 die "attribute '$attribute' is not supported for storage type '$scfg->{type}'\n";
196 }
197
198 sub status {
199 my ($class, $storeid, $scfg, $cache) = @_;
200
201 if (defined(my $mp = parse_is_mountpoint($scfg))) {
202 $cache->{mountdata} = PVE::ProcFSTools::parse_proc_mounts()
203 if !$cache->{mountdata};
204
205 return undef if !path_is_mounted($mp, $cache->{mountdata});
206 }
207
208 return $class->SUPER::status($storeid, $scfg, $cache);
209 }
210
211
212 sub activate_storage {
213 my ($class, $storeid, $scfg, $cache) = @_;
214
215 my $path = $scfg->{path};
216 if (!defined($scfg->{mkdir}) || $scfg->{mkdir}) {
217 mkpath $path;
218 }
219
220 my $mp = parse_is_mountpoint($scfg);
221 if (defined($mp) && !path_is_mounted($mp, $cache->{mountdata})) {
222 die "unable to activate storage '$storeid' - " .
223 "directory is expected to be a mount point but is not mounted: '$mp'\n";
224 }
225
226 $class->SUPER::activate_storage($storeid, $scfg, $cache);
227 }
228
229 sub check_config {
230 my ($self, $sectionId, $config, $create, $skipSchemaCheck) = @_;
231 my $opts = PVE::SectionConfig::check_config($self, $sectionId, $config, $create, $skipSchemaCheck);
232 return $opts if !$create;
233 if ($opts->{path} !~ m@^/[-/a-zA-Z0-9_.]+$@) {
234 die "illegal path for directory storage: $opts->{path}\n";
235 }
236 return $opts;
237 }
238
239 1;