]>
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 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 },
72 # Storage implementation
75 # NOTE: should ProcFSTools::is_mounted accept an optional cache like this?
77 my ($mountpoint, $mountdata) = @_;
79 $mountpoint = Cwd
::realpath
($mountpoint); # symlinks
80 return 0 if !defined($mountpoint); # path does not exist
82 $mountdata = PVE
::ProcFSTools
::parse_proc_mounts
() if !$mountdata;
83 return 1 if grep { $_->[1] eq $mountpoint } @$mountdata;
87 sub parse_is_mountpoint
{
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;
94 return $is_mp; # contains a path
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) = @_;
101 my ($vtype) = $class->parse_volname($volname);
102 return if $vtype ne 'backup';
104 my $path = $class->filesystem_path($scfg, $volname);
105 $path .= $class->SUPER::NOTES_EXT
;
108 my $data = PVE
::Tools
::file_get_contents
($path);
109 return eval { decode
('UTF-8', $data, 1) } // $data;
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);
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) = @_;
126 my ($vtype) = $class->parse_volname($volname);
127 die "only backups can have notes\n" if $vtype ne 'backup';
129 my $path = $class->filesystem_path($scfg, $volname);
130 $path .= $class->SUPER::NOTES_EXT
;
132 if (defined($notes) && $notes ne '') {
133 my $encoded = encode
('UTF-8', $notes);
134 PVE
::Tools
::file_set_contents
($path, $encoded);
136 unlink $path or $! == ENOENT
or die "could not delete notes - $!\n";
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);
148 sub get_volume_attribute
{
149 my ($class, $scfg, $storeid, $volname, $attribute) = @_;
151 if ($attribute eq 'notes') {
152 return $get_volume_notes_impl->($class, $scfg, $storeid, $volname);
155 my ($vtype) = $class->parse_volname($volname);
156 return if $vtype ne 'backup';
158 if ($attribute eq 'protected') {
159 my $path = $class->filesystem_path($scfg, $volname);
160 return -e PVE
::Storage
::protection_file_path
($path) ?
1 : 0;
166 sub update_volume_attribute
{
167 my ($class, $scfg, $storeid, $volname, $attribute, $value) = @_;
169 if ($attribute eq 'notes') {
170 return $update_volume_notes_impl->($class, $scfg, $storeid, $volname, $value);
173 my ($vtype) = $class->parse_volname($volname);
174 die "only backups support attribute '$attribute'\n" if $vtype ne 'backup';
176 if ($attribute eq 'protected') {
177 my $path = $class->filesystem_path($scfg, $volname);
178 my $protection_path = PVE
::Storage
::protection_file_path
($path);
180 return if !((-e
$protection_path) xor $value); # protection status already correct
183 my $fh = IO
::File-
>new($protection_path, O_CREAT
, 0644)
184 or die "unable to create protection file '$protection_path' - $!\n";
187 unlink $protection_path or $! == ENOENT
188 or die "could not delete protection file '$protection_path' - $!\n";
194 die "attribute '$attribute' is not supported for storage type '$scfg->{type}'\n";
198 my ($class, $storeid, $scfg, $cache) = @_;
200 if (defined(my $mp = parse_is_mountpoint
($scfg))) {
201 $cache->{mountdata
} = PVE
::ProcFSTools
::parse_proc_mounts
()
202 if !$cache->{mountdata
};
204 return undef if !path_is_mounted
($mp, $cache->{mountdata
});
207 return $class->SUPER::status
($storeid, $scfg, $cache);
211 sub activate_storage
{
212 my ($class, $storeid, $scfg, $cache) = @_;
214 my $path = $scfg->{path
};
215 if (!defined($scfg->{mkdir}) || $scfg->{mkdir}) {
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";
225 $class->SUPER::activate_storage
($storeid, $scfg, $cache);
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";