]> git.proxmox.com Git - pve-storage.git/blame - PVE/Storage/DirPlugin.pm
DirPlugin: update_volume_attribute: don't use update_volume_notes
[pve-storage.git] / PVE / Storage / DirPlugin.pm
CommitLineData
1dc01b9f
DM
1package PVE::Storage::DirPlugin;
2
3use strict;
4use warnings;
ddb32630 5
d547f26c 6use Cwd;
43f8112f 7use Encode qw(decode encode);
1dc01b9f 8use File::Path;
56897a92 9use IO::File;
ddb32630
FE
10use POSIX;
11
1dc01b9f
DM
12use PVE::Storage::Plugin;
13use PVE::JSONSchema qw(get_standard_option);
14
15use base qw(PVE::Storage::Plugin);
16
17# Configuration
18
19sub type {
20 return 'dir';
21}
22
23sub plugindata {
24 return {
d1eb35ea 25 content => [ { images => 1, rootdir => 1, vztmpl => 1, iso => 1, backup => 1, snippets => 1, none => 1 },
1dc01b9f 26 { images => 1, rootdir => 1 }],
35533c68 27 format => [ { raw => 1, qcow2 => 1, vmdk => 1, subvol => 1 } , 'raw' ],
1dc01b9f 28 };
c2fc9fbe 29}
1dc01b9f
DM
30
31sub properties {
32 return {
33 path => {
34 description => "File system path.",
35 type => 'string', format => 'pve-storage-path',
36 },
b521247b
WB
37 mkdir => {
38 description => "Create the directory if it doesn't exist.",
39 type => 'boolean',
40 default => 'yes',
41 },
d547f26c
WB
42 is_mountpoint => {
43 description =>
de8eff4d
WB
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',
d547f26c
WB
48 default => 'no',
49 },
9edb99a5 50 bwlimit => get_standard_option('bwlimit'),
1dc01b9f
DM
51 };
52}
53
54sub options {
55 return {
56 path => { fixed => 1 },
3353698f 57 nodes => { optional => 1 },
1dc01b9f
DM
58 shared => { optional => 1 },
59 disable => { optional => 1 },
3353698f
FE
60 maxfiles => { optional => 1 },
61 'prune-backups' => { optional => 1 },
8009417d 62 'max-protected-backups' => { optional => 1 },
1dc01b9f
DM
63 content => { optional => 1 },
64 format => { optional => 1 },
b521247b 65 mkdir => { optional => 1 },
d547f26c 66 is_mountpoint => { optional => 1 },
9edb99a5 67 bwlimit => { optional => 1 },
95ff5dbd 68 preallocation => { optional => 1 },
1dc01b9f
DM
69 };
70}
71
72# Storage implementation
d547f26c
WB
73#
74
75# NOTE: should ProcFSTools::is_mounted accept an optional cache like this?
76sub 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
de8eff4d
WB
87sub 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
91d49d1d
DC
97# FIXME move into 'get_volume_attribute' when removing 'get_volume_notes'
98my $get_volume_notes_impl = sub {
e9991d26 99 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
20471dfd 100
e0aa2070
FE
101 my ($vtype) = $class->parse_volname($volname);
102 return if $vtype ne 'backup';
103
e9991d26
DC
104 my $path = $class->filesystem_path($scfg, $volname);
105 $path .= $class->SUPER::NOTES_EXT;
106
43f8112f
DC
107 if (-f $path) {
108 my $data = PVE::Tools::file_get_contents($path);
109 return eval { decode('UTF-8', $data, 1) } // $data;
110 }
e9991d26 111
20471dfd 112 return '';
91d49d1d 113};
e9991d26 114
f1de8281 115# FIXME remove on the next APIAGE reset.
91d49d1d
DC
116# Deprecated, use get_volume_attribute instead.
117sub 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'
123my $update_volume_notes_impl = sub {
e9991d26 124 my ($class, $scfg, $storeid, $volname, $notes, $timeout) = @_;
e9991d26 125
20471dfd
TL
126 my ($vtype) = $class->parse_volname($volname);
127 die "only backups can have notes\n" if $vtype ne 'backup';
e9991d26 128
20471dfd 129 my $path = $class->filesystem_path($scfg, $volname);
e9991d26
DC
130 $path .= $class->SUPER::NOTES_EXT;
131
f244e2aa 132 if (defined($notes) && $notes ne '') {
43f8112f
DC
133 my $encoded = encode('UTF-8', $notes);
134 PVE::Tools::file_set_contents($path, $encoded);
f244e2aa 135 } else {
ddb32630 136 unlink $path or $! == ENOENT or die "could not delete notes - $!\n";
f244e2aa 137 }
20471dfd 138 return;
91d49d1d
DC
139};
140
141# FIXME remove on the next APIAGE reset.
142# Deprecated, use update_volume_attribute instead.
143sub update_volume_notes {
144 my ($class, $scfg, $storeid, $volname, $notes, $timeout) = @_;
145 return $update_volume_notes_impl->($class, $scfg, $storeid, $volname, $notes, $timeout);
e9991d26
DC
146}
147
f1de8281
FE
148sub get_volume_attribute {
149 my ($class, $scfg, $storeid, $volname, $attribute) = @_;
150
151 if ($attribute eq 'notes') {
91d49d1d 152 return $get_volume_notes_impl->($class, $scfg, $storeid, $volname);
f1de8281
FE
153 }
154
56897a92
FE
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
f1de8281
FE
163 return;
164}
165
166sub update_volume_attribute {
167 my ($class, $scfg, $storeid, $volname, $attribute, $value) = @_;
168
169 if ($attribute eq 'notes') {
91d49d1d 170 return $update_volume_notes_impl->($class, $scfg, $storeid, $volname, $value);
f1de8281
FE
171 }
172
56897a92
FE
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
f1de8281
FE
194 die "attribute '$attribute' is not supported for storage type '$scfg->{type}'\n";
195}
196
d547f26c
WB
197sub status {
198 my ($class, $storeid, $scfg, $cache) = @_;
199
de8eff4d 200 if (defined(my $mp = parse_is_mountpoint($scfg))) {
8b5ccc06
FG
201 $cache->{mountdata} = PVE::ProcFSTools::parse_proc_mounts()
202 if !$cache->{mountdata};
d547f26c 203
de8eff4d 204 return undef if !path_is_mounted($mp, $cache->{mountdata});
8b5ccc06 205 }
d547f26c
WB
206
207 return $class->SUPER::status($storeid, $scfg, $cache);
208}
209
1dc01b9f
DM
210
211sub activate_storage {
212 my ($class, $storeid, $scfg, $cache) = @_;
213
d547f26c 214 my $path = $scfg->{path};
b521247b 215 if (!defined($scfg->{mkdir}) || $scfg->{mkdir}) {
b521247b
WB
216 mkpath $path;
217 }
1dc01b9f 218
de8eff4d
WB
219 my $mp = parse_is_mountpoint($scfg);
220 if (defined($mp) && !path_is_mounted($mp, $cache->{mountdata})) {
d547f26c 221 die "unable to activate storage '$storeid' - " .
de8eff4d 222 "directory is expected to be a mount point but is not mounted: '$mp'\n";
d547f26c
WB
223 }
224
c2fc9fbe 225 $class->SUPER::activate_storage($storeid, $scfg, $cache);
1dc01b9f
DM
226}
227
5c95e484
WB
228sub 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}
1dc01b9f
DM
237
2381;