]>
git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/DirPlugin.pm
1 package PVE
::Storage
::DirPlugin
;
7 use Encode
qw(decode encode);
12 use PVE
::Storage
::Plugin
;
13 use PVE
::JSONSchema
qw(get_standard_option);
15 use base
qw(PVE::Storage::Plugin);
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' ],
34 description
=> "File system path.",
35 type
=> 'string', format
=> 'pve-storage-path',
38 description
=> "Create the directory if it doesn't exist.",
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.",
50 bwlimit
=> get_standard_option
('bwlimit'),
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 },
73 # Storage implementation
76 # NOTE: should ProcFSTools::is_mounted accept an optional cache like this?
78 my ($mountpoint, $mountdata) = @_;
80 $mountpoint = Cwd
::realpath
($mountpoint); # symlinks
81 return 0 if !defined($mountpoint); # path does not exist
83 $mountdata = PVE
::ProcFSTools
::parse_proc_mounts
() if !$mountdata;
84 return 1 if grep { $_->[1] eq $mountpoint } @$mountdata;
88 sub parse_is_mountpoint
{
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;
95 return $is_mp; # contains a path
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) = @_;
102 my ($vtype) = $class->parse_volname($volname);
103 return if $vtype ne 'backup';
105 my $path = $class->filesystem_path($scfg, $volname);
106 $path .= $class->SUPER::NOTES_EXT
;
109 my $data = PVE
::Tools
::file_get_contents
($path);
110 return eval { decode
('UTF-8', $data, 1) } // $data;
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);
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) = @_;
127 my ($vtype) = $class->parse_volname($volname);
128 die "only backups can have notes\n" if $vtype ne 'backup';
130 my $path = $class->filesystem_path($scfg, $volname);
131 $path .= $class->SUPER::NOTES_EXT
;
133 if (defined($notes) && $notes ne '') {
134 my $encoded = encode
('UTF-8', $notes);
135 PVE
::Tools
::file_set_contents
($path, $encoded);
137 unlink $path or $! == ENOENT
or die "could not delete notes - $!\n";
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);
149 sub get_volume_attribute
{
150 my ($class, $scfg, $storeid, $volname, $attribute) = @_;
152 if ($attribute eq 'notes') {
153 return $get_volume_notes_impl->($class, $scfg, $storeid, $volname);
156 my ($vtype) = $class->parse_volname($volname);
157 return if $vtype ne 'backup';
159 if ($attribute eq 'protected') {
160 my $path = $class->filesystem_path($scfg, $volname);
161 return -e PVE
::Storage
::protection_file_path
($path) ?
1 : 0;
167 sub update_volume_attribute
{
168 my ($class, $scfg, $storeid, $volname, $attribute, $value) = @_;
170 if ($attribute eq 'notes') {
171 return $update_volume_notes_impl->($class, $scfg, $storeid, $volname, $value);
174 my ($vtype) = $class->parse_volname($volname);
175 die "only backups support attribute '$attribute'\n" if $vtype ne 'backup';
177 if ($attribute eq 'protected') {
178 my $path = $class->filesystem_path($scfg, $volname);
179 my $protection_path = PVE
::Storage
::protection_file_path
($path);
181 return if !((-e
$protection_path) xor $value); # protection status already correct
184 my $fh = IO
::File-
>new($protection_path, O_CREAT
, 0644)
185 or die "unable to create protection file '$protection_path' - $!\n";
188 unlink $protection_path or $! == ENOENT
189 or die "could not delete protection file '$protection_path' - $!\n";
195 die "attribute '$attribute' is not supported for storage type '$scfg->{type}'\n";
199 my ($class, $storeid, $scfg, $cache) = @_;
201 if (defined(my $mp = parse_is_mountpoint
($scfg))) {
202 $cache->{mountdata
} = PVE
::ProcFSTools
::parse_proc_mounts
()
203 if !$cache->{mountdata
};
205 return undef if !path_is_mounted
($mp, $cache->{mountdata
});
208 return $class->SUPER::status
($storeid, $scfg, $cache);
212 sub activate_storage
{
213 my ($class, $storeid, $scfg, $cache) = @_;
215 my $path = $scfg->{path
};
216 if (!defined($scfg->{mkdir}) || $scfg->{mkdir}) {
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";
226 $class->SUPER::activate_storage
($storeid, $scfg, $cache);
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";