]>
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 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) = @_;
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 update_volume_attribute instead.
118 sub update_volume_notes
{
119 my ($class, $scfg, $storeid, $volname, $notes, $timeout) = @_;
121 my ($vtype) = $class->parse_volname($volname);
122 die "only backups can have notes\n" if $vtype ne 'backup';
124 my $path = $class->filesystem_path($scfg, $volname);
125 $path .= $class->SUPER::NOTES_EXT
;
127 if (defined($notes) && $notes ne '') {
128 my $encoded = encode
('UTF-8', $notes);
129 PVE
::Tools
::file_set_contents
($path, $encoded);
131 unlink $path or $! == ENOENT
or die "could not delete notes - $!\n";
136 sub get_volume_attribute
{
137 my ($class, $scfg, $storeid, $volname, $attribute) = @_;
139 if ($attribute eq 'notes') {
140 return $class->get_volume_notes($scfg, $storeid, $volname);
143 my ($vtype) = $class->parse_volname($volname);
144 return if $vtype ne 'backup';
146 if ($attribute eq 'protected') {
147 my $path = $class->filesystem_path($scfg, $volname);
148 return -e PVE
::Storage
::protection_file_path
($path) ?
1 : 0;
154 sub update_volume_attribute
{
155 my ($class, $scfg, $storeid, $volname, $attribute, $value) = @_;
157 if ($attribute eq 'notes') {
158 return $class->update_volume_notes($scfg, $storeid, $volname, $value);
161 my ($vtype) = $class->parse_volname($volname);
162 die "only backups support attribute '$attribute'\n" if $vtype ne 'backup';
164 if ($attribute eq 'protected') {
165 my $path = $class->filesystem_path($scfg, $volname);
166 my $protection_path = PVE
::Storage
::protection_file_path
($path);
168 return if !((-e
$protection_path) xor $value); # protection status already correct
171 my $fh = IO
::File-
>new($protection_path, O_CREAT
, 0644)
172 or die "unable to create protection file '$protection_path' - $!\n";
175 unlink $protection_path or $! == ENOENT
176 or die "could not delete protection file '$protection_path' - $!\n";
182 die "attribute '$attribute' is not supported for storage type '$scfg->{type}'\n";
186 my ($class, $storeid, $scfg, $cache) = @_;
188 if (defined(my $mp = parse_is_mountpoint
($scfg))) {
189 $cache->{mountdata
} = PVE
::ProcFSTools
::parse_proc_mounts
()
190 if !$cache->{mountdata
};
192 return undef if !path_is_mounted
($mp, $cache->{mountdata
});
195 return $class->SUPER::status
($storeid, $scfg, $cache);
199 sub activate_storage
{
200 my ($class, $storeid, $scfg, $cache) = @_;
202 my $path = $scfg->{path
};
203 if (!defined($scfg->{mkdir}) || $scfg->{mkdir}) {
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";
213 $class->SUPER::activate_storage
($storeid, $scfg, $cache);
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";