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