]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/DirPlugin.pm
DirPlugin: update_volume_attribute: don't use update_volume_notes
[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 nodes => { optional => 1 },
58 shared => { optional => 1 },
59 disable => { optional => 1 },
60 maxfiles => { optional => 1 },
61 'prune-backups' => { optional => 1 },
62 'max-protected-backups' => { optional => 1 },
63 content => { optional => 1 },
64 format => { optional => 1 },
65 mkdir => { optional => 1 },
66 is_mountpoint => { optional => 1 },
67 bwlimit => { optional => 1 },
68 preallocation => { optional => 1 },
69 };
70 }
71
72 # Storage implementation
73 #
74
75 # NOTE: should ProcFSTools::is_mounted accept an optional cache like this?
76 sub path_is_mounted {
77 my ($mountpoint, $mountdata) = @_;
78
79 $mountpoint = Cwd::realpath($mountpoint); # symlinks
80 return 0 if !defined($mountpoint); # path does not exist
81
82 $mountdata = PVE::ProcFSTools::parse_proc_mounts() if !$mountdata;
83 return 1 if grep { $_->[1] eq $mountpoint } @$mountdata;
84 return undef;
85 }
86
87 sub parse_is_mountpoint {
88 my ($scfg) = @_;
89 my $is_mp = $scfg->{is_mountpoint};
90 return undef if !defined $is_mp;
91 if (defined(my $bool = PVE::JSONSchema::parse_boolean($is_mp))) {
92 return $bool ? $scfg->{path} : undef;
93 }
94 return $is_mp; # contains a path
95 }
96
97 # FIXME move into 'get_volume_attribute' when removing 'get_volume_notes'
98 my $get_volume_notes_impl = sub {
99 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
100
101 my ($vtype) = $class->parse_volname($volname);
102 return if $vtype ne 'backup';
103
104 my $path = $class->filesystem_path($scfg, $volname);
105 $path .= $class->SUPER::NOTES_EXT;
106
107 if (-f $path) {
108 my $data = PVE::Tools::file_get_contents($path);
109 return eval { decode('UTF-8', $data, 1) } // $data;
110 }
111
112 return '';
113 };
114
115 # FIXME remove on the next APIAGE reset.
116 # Deprecated, use get_volume_attribute instead.
117 sub get_volume_notes {
118 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
119 return $get_volume_notes_impl->($class, $scfg, $storeid, $volname, $timeout);
120 }
121
122 # FIXME move into 'update_volume_attribute' when removing 'update_volume_notes'
123 my $update_volume_notes_impl = sub {
124 my ($class, $scfg, $storeid, $volname, $notes, $timeout) = @_;
125
126 my ($vtype) = $class->parse_volname($volname);
127 die "only backups can have notes\n" if $vtype ne 'backup';
128
129 my $path = $class->filesystem_path($scfg, $volname);
130 $path .= $class->SUPER::NOTES_EXT;
131
132 if (defined($notes) && $notes ne '') {
133 my $encoded = encode('UTF-8', $notes);
134 PVE::Tools::file_set_contents($path, $encoded);
135 } else {
136 unlink $path or $! == ENOENT or die "could not delete notes - $!\n";
137 }
138 return;
139 };
140
141 # FIXME remove on the next APIAGE reset.
142 # Deprecated, use update_volume_attribute instead.
143 sub update_volume_notes {
144 my ($class, $scfg, $storeid, $volname, $notes, $timeout) = @_;
145 return $update_volume_notes_impl->($class, $scfg, $storeid, $volname, $notes, $timeout);
146 }
147
148 sub get_volume_attribute {
149 my ($class, $scfg, $storeid, $volname, $attribute) = @_;
150
151 if ($attribute eq 'notes') {
152 return $get_volume_notes_impl->($class, $scfg, $storeid, $volname);
153 }
154
155 my ($vtype) = $class->parse_volname($volname);
156 return if $vtype ne 'backup';
157
158 if ($attribute eq 'protected') {
159 my $path = $class->filesystem_path($scfg, $volname);
160 return -e PVE::Storage::protection_file_path($path) ? 1 : 0;
161 }
162
163 return;
164 }
165
166 sub update_volume_attribute {
167 my ($class, $scfg, $storeid, $volname, $attribute, $value) = @_;
168
169 if ($attribute eq 'notes') {
170 return $update_volume_notes_impl->($class, $scfg, $storeid, $volname, $value);
171 }
172
173 my ($vtype) = $class->parse_volname($volname);
174 die "only backups support attribute '$attribute'\n" if $vtype ne 'backup';
175
176 if ($attribute eq 'protected') {
177 my $path = $class->filesystem_path($scfg, $volname);
178 my $protection_path = PVE::Storage::protection_file_path($path);
179
180 return if !((-e $protection_path) xor $value); # protection status already correct
181
182 if ($value) {
183 my $fh = IO::File->new($protection_path, O_CREAT, 0644)
184 or die "unable to create protection file '$protection_path' - $!\n";
185 close($fh);
186 } else {
187 unlink $protection_path or $! == ENOENT
188 or die "could not delete protection file '$protection_path' - $!\n";
189 }
190
191 return;
192 }
193
194 die "attribute '$attribute' is not supported for storage type '$scfg->{type}'\n";
195 }
196
197 sub status {
198 my ($class, $storeid, $scfg, $cache) = @_;
199
200 if (defined(my $mp = parse_is_mountpoint($scfg))) {
201 $cache->{mountdata} = PVE::ProcFSTools::parse_proc_mounts()
202 if !$cache->{mountdata};
203
204 return undef if !path_is_mounted($mp, $cache->{mountdata});
205 }
206
207 return $class->SUPER::status($storeid, $scfg, $cache);
208 }
209
210
211 sub activate_storage {
212 my ($class, $storeid, $scfg, $cache) = @_;
213
214 my $path = $scfg->{path};
215 if (!defined($scfg->{mkdir}) || $scfg->{mkdir}) {
216 mkpath $path;
217 }
218
219 my $mp = parse_is_mountpoint($scfg);
220 if (defined($mp) && !path_is_mounted($mp, $cache->{mountdata})) {
221 die "unable to activate storage '$storeid' - " .
222 "directory is expected to be a mount point but is not mounted: '$mp'\n";
223 }
224
225 $class->SUPER::activate_storage($storeid, $scfg, $cache);
226 }
227
228 sub check_config {
229 my ($self, $sectionId, $config, $create, $skipSchemaCheck) = @_;
230 my $opts = PVE::SectionConfig::check_config($self, $sectionId, $config, $create, $skipSchemaCheck);
231 return $opts if !$create;
232 if ($opts->{path} !~ m@^/[-/a-zA-Z0-9_.]+$@) {
233 die "illegal path for directory storage: $opts->{path}\n";
234 }
235 return $opts;
236 }
237
238 1;