]> git.proxmox.com Git - pve-storage.git/blame - PVE/Storage/DirPlugin.pm
add generalized functions to manage volume attributes
[pve-storage.git] / PVE / Storage / DirPlugin.pm
CommitLineData
1dc01b9f
DM
1package PVE::Storage::DirPlugin;
2
3use strict;
4use warnings;
ddb32630 5
d547f26c 6use Cwd;
1dc01b9f 7use File::Path;
ddb32630
FE
8use POSIX;
9
1dc01b9f
DM
10use PVE::Storage::Plugin;
11use PVE::JSONSchema qw(get_standard_option);
12
13use base qw(PVE::Storage::Plugin);
14
15# Configuration
16
17sub type {
18 return 'dir';
19}
20
21sub plugindata {
22 return {
d1eb35ea 23 content => [ { images => 1, rootdir => 1, vztmpl => 1, iso => 1, backup => 1, snippets => 1, none => 1 },
1dc01b9f 24 { images => 1, rootdir => 1 }],
35533c68 25 format => [ { raw => 1, qcow2 => 1, vmdk => 1, subvol => 1 } , 'raw' ],
1dc01b9f 26 };
c2fc9fbe 27}
1dc01b9f
DM
28
29sub properties {
30 return {
31 path => {
32 description => "File system path.",
33 type => 'string', format => 'pve-storage-path',
34 },
b521247b
WB
35 mkdir => {
36 description => "Create the directory if it doesn't exist.",
37 type => 'boolean',
38 default => 'yes',
39 },
d547f26c
WB
40 is_mountpoint => {
41 description =>
de8eff4d
WB
42 "Assume the given path is an externally managed mountpoint " .
43 "and consider the storage offline if it is not mounted. ".
44 "Using a boolean (yes/no) value serves as a shortcut to using the target path in this field.",
45 type => 'string',
d547f26c
WB
46 default => 'no',
47 },
9edb99a5 48 bwlimit => get_standard_option('bwlimit'),
1dc01b9f
DM
49 };
50}
51
52sub options {
53 return {
54 path => { fixed => 1 },
3353698f 55 nodes => { optional => 1 },
1dc01b9f
DM
56 shared => { optional => 1 },
57 disable => { optional => 1 },
3353698f
FE
58 maxfiles => { optional => 1 },
59 'prune-backups' => { optional => 1 },
1dc01b9f
DM
60 content => { optional => 1 },
61 format => { optional => 1 },
b521247b 62 mkdir => { optional => 1 },
d547f26c 63 is_mountpoint => { optional => 1 },
9edb99a5 64 bwlimit => { optional => 1 },
95ff5dbd 65 preallocation => { optional => 1 },
1dc01b9f
DM
66 };
67}
68
69# Storage implementation
d547f26c
WB
70#
71
72# NOTE: should ProcFSTools::is_mounted accept an optional cache like this?
73sub path_is_mounted {
74 my ($mountpoint, $mountdata) = @_;
75
76 $mountpoint = Cwd::realpath($mountpoint); # symlinks
77 return 0 if !defined($mountpoint); # path does not exist
78
79 $mountdata = PVE::ProcFSTools::parse_proc_mounts() if !$mountdata;
80 return 1 if grep { $_->[1] eq $mountpoint } @$mountdata;
81 return undef;
82}
83
de8eff4d
WB
84sub parse_is_mountpoint {
85 my ($scfg) = @_;
86 my $is_mp = $scfg->{is_mountpoint};
87 return undef if !defined $is_mp;
88 if (defined(my $bool = PVE::JSONSchema::parse_boolean($is_mp))) {
89 return $bool ? $scfg->{path} : undef;
90 }
91 return $is_mp; # contains a path
92}
93
f1de8281
FE
94# FIXME remove on the next APIAGE reset.
95# Deprecated, use get_volume_attribute instead.
e9991d26
DC
96sub get_volume_notes {
97 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
20471dfd 98
e0aa2070
FE
99 my ($vtype) = $class->parse_volname($volname);
100 return if $vtype ne 'backup';
101
e9991d26
DC
102 my $path = $class->filesystem_path($scfg, $volname);
103 $path .= $class->SUPER::NOTES_EXT;
104
20471dfd 105 return PVE::Tools::file_get_contents($path) if -f $path;
e9991d26 106
20471dfd 107 return '';
e9991d26
DC
108}
109
f1de8281
FE
110# FIXME remove on the next APIAGE reset.
111# Deprecated, use update_volume_attribute instead.
e9991d26
DC
112sub update_volume_notes {
113 my ($class, $scfg, $storeid, $volname, $notes, $timeout) = @_;
e9991d26 114
20471dfd
TL
115 my ($vtype) = $class->parse_volname($volname);
116 die "only backups can have notes\n" if $vtype ne 'backup';
e9991d26 117
20471dfd 118 my $path = $class->filesystem_path($scfg, $volname);
e9991d26
DC
119 $path .= $class->SUPER::NOTES_EXT;
120
f244e2aa
TL
121 if (defined($notes) && $notes ne '') {
122 PVE::Tools::file_set_contents($path, $notes);
123 } else {
ddb32630 124 unlink $path or $! == ENOENT or die "could not delete notes - $!\n";
f244e2aa 125 }
20471dfd 126 return;
e9991d26
DC
127}
128
f1de8281
FE
129sub get_volume_attribute {
130 my ($class, $scfg, $storeid, $volname, $attribute) = @_;
131
132 if ($attribute eq 'notes') {
133 return $class->get_volume_notes($scfg, $storeid, $volname);
134 }
135
136 return;
137}
138
139sub update_volume_attribute {
140 my ($class, $scfg, $storeid, $volname, $attribute, $value) = @_;
141
142 if ($attribute eq 'notes') {
143 return $class->update_volume_notes($scfg, $storeid, $volname, $value);
144 }
145
146 die "attribute '$attribute' is not supported for storage type '$scfg->{type}'\n";
147}
148
d547f26c
WB
149sub status {
150 my ($class, $storeid, $scfg, $cache) = @_;
151
de8eff4d 152 if (defined(my $mp = parse_is_mountpoint($scfg))) {
8b5ccc06
FG
153 $cache->{mountdata} = PVE::ProcFSTools::parse_proc_mounts()
154 if !$cache->{mountdata};
d547f26c 155
de8eff4d 156 return undef if !path_is_mounted($mp, $cache->{mountdata});
8b5ccc06 157 }
d547f26c
WB
158
159 return $class->SUPER::status($storeid, $scfg, $cache);
160}
161
1dc01b9f
DM
162
163sub activate_storage {
164 my ($class, $storeid, $scfg, $cache) = @_;
165
d547f26c 166 my $path = $scfg->{path};
b521247b 167 if (!defined($scfg->{mkdir}) || $scfg->{mkdir}) {
b521247b
WB
168 mkpath $path;
169 }
1dc01b9f 170
de8eff4d
WB
171 my $mp = parse_is_mountpoint($scfg);
172 if (defined($mp) && !path_is_mounted($mp, $cache->{mountdata})) {
d547f26c 173 die "unable to activate storage '$storeid' - " .
de8eff4d 174 "directory is expected to be a mount point but is not mounted: '$mp'\n";
d547f26c
WB
175 }
176
c2fc9fbe 177 $class->SUPER::activate_storage($storeid, $scfg, $cache);
1dc01b9f
DM
178}
179
5c95e484
WB
180sub check_config {
181 my ($self, $sectionId, $config, $create, $skipSchemaCheck) = @_;
182 my $opts = PVE::SectionConfig::check_config($self, $sectionId, $config, $create, $skipSchemaCheck);
183 return $opts if !$create;
184 if ($opts->{path} !~ m@^/[-/a-zA-Z0-9_.]+$@) {
185 die "illegal path for directory storage: $opts->{path}\n";
186 }
187 return $opts;
188}
1dc01b9f
DM
189
1901;