]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/DirPlugin.pm
rbd: get path: allow fake override of fsid in scfg for some regression tests
[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 remove on the next APIAGE reset.
98 # Deprecated, use get_volume_attribute instead.
99 sub get_volume_notes {
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 update_volume_attribute instead.
118 sub update_volume_notes {
119 my ($class, $scfg, $storeid, $volname, $notes, $timeout) = @_;
120
121 my ($vtype) = $class->parse_volname($volname);
122 die "only backups can have notes\n" if $vtype ne 'backup';
123
124 my $path = $class->filesystem_path($scfg, $volname);
125 $path .= $class->SUPER::NOTES_EXT;
126
127 if (defined($notes) && $notes ne '') {
128 my $encoded = encode('UTF-8', $notes);
129 PVE::Tools::file_set_contents($path, $encoded);
130 } else {
131 unlink $path or $! == ENOENT or die "could not delete notes - $!\n";
132 }
133 return;
134 }
135
136 sub get_volume_attribute {
137 my ($class, $scfg, $storeid, $volname, $attribute) = @_;
138
139 if ($attribute eq 'notes') {
140 return $class->get_volume_notes($scfg, $storeid, $volname);
141 }
142
143 my ($vtype) = $class->parse_volname($volname);
144 return if $vtype ne 'backup';
145
146 if ($attribute eq 'protected') {
147 my $path = $class->filesystem_path($scfg, $volname);
148 return -e PVE::Storage::protection_file_path($path) ? 1 : 0;
149 }
150
151 return;
152 }
153
154 sub update_volume_attribute {
155 my ($class, $scfg, $storeid, $volname, $attribute, $value) = @_;
156
157 if ($attribute eq 'notes') {
158 return $class->update_volume_notes($scfg, $storeid, $volname, $value);
159 }
160
161 my ($vtype) = $class->parse_volname($volname);
162 die "only backups support attribute '$attribute'\n" if $vtype ne 'backup';
163
164 if ($attribute eq 'protected') {
165 my $path = $class->filesystem_path($scfg, $volname);
166 my $protection_path = PVE::Storage::protection_file_path($path);
167
168 return if !((-e $protection_path) xor $value); # protection status already correct
169
170 if ($value) {
171 my $fh = IO::File->new($protection_path, O_CREAT, 0644)
172 or die "unable to create protection file '$protection_path' - $!\n";
173 close($fh);
174 } else {
175 unlink $protection_path or $! == ENOENT
176 or die "could not delete protection file '$protection_path' - $!\n";
177 }
178
179 return;
180 }
181
182 die "attribute '$attribute' is not supported for storage type '$scfg->{type}'\n";
183 }
184
185 sub status {
186 my ($class, $storeid, $scfg, $cache) = @_;
187
188 if (defined(my $mp = parse_is_mountpoint($scfg))) {
189 $cache->{mountdata} = PVE::ProcFSTools::parse_proc_mounts()
190 if !$cache->{mountdata};
191
192 return undef if !path_is_mounted($mp, $cache->{mountdata});
193 }
194
195 return $class->SUPER::status($storeid, $scfg, $cache);
196 }
197
198
199 sub activate_storage {
200 my ($class, $storeid, $scfg, $cache) = @_;
201
202 my $path = $scfg->{path};
203 if (!defined($scfg->{mkdir}) || $scfg->{mkdir}) {
204 mkpath $path;
205 }
206
207 my $mp = parse_is_mountpoint($scfg);
208 if (defined($mp) && !path_is_mounted($mp, $cache->{mountdata})) {
209 die "unable to activate storage '$storeid' - " .
210 "directory is expected to be a mount point but is not mounted: '$mp'\n";
211 }
212
213 $class->SUPER::activate_storage($storeid, $scfg, $cache);
214 }
215
216 sub check_config {
217 my ($self, $sectionId, $config, $create, $skipSchemaCheck) = @_;
218 my $opts = PVE::SectionConfig::check_config($self, $sectionId, $config, $create, $skipSchemaCheck);
219 return $opts if !$create;
220 if ($opts->{path} !~ m@^/[-/a-zA-Z0-9_.]+$@) {
221 die "illegal path for directory storage: $opts->{path}\n";
222 }
223 return $opts;
224 }
225
226 1;