]>
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 }, | |
52 | nodes => { optional => 1 }, | |
53 | shared => { optional => 1 }, | |
54 | disable => { optional => 1 }, | |
55 | maxfiles => { optional => 1 }, | |
56 | content => { optional => 1 }, | |
57 | format => { optional => 1 }, | |
b521247b | 58 | mkdir => { optional => 1 }, |
d547f26c | 59 | is_mountpoint => { optional => 1 }, |
9edb99a5 | 60 | bwlimit => { optional => 1 }, |
1dc01b9f DM |
61 | }; |
62 | } | |
63 | ||
64 | # Storage implementation | |
d547f26c WB |
65 | # |
66 | ||
67 | # NOTE: should ProcFSTools::is_mounted accept an optional cache like this? | |
68 | sub path_is_mounted { | |
69 | my ($mountpoint, $mountdata) = @_; | |
70 | ||
71 | $mountpoint = Cwd::realpath($mountpoint); # symlinks | |
72 | return 0 if !defined($mountpoint); # path does not exist | |
73 | ||
74 | $mountdata = PVE::ProcFSTools::parse_proc_mounts() if !$mountdata; | |
75 | return 1 if grep { $_->[1] eq $mountpoint } @$mountdata; | |
76 | return undef; | |
77 | } | |
78 | ||
de8eff4d WB |
79 | sub parse_is_mountpoint { |
80 | my ($scfg) = @_; | |
81 | my $is_mp = $scfg->{is_mountpoint}; | |
82 | return undef if !defined $is_mp; | |
83 | if (defined(my $bool = PVE::JSONSchema::parse_boolean($is_mp))) { | |
84 | return $bool ? $scfg->{path} : undef; | |
85 | } | |
86 | return $is_mp; # contains a path | |
87 | } | |
88 | ||
d547f26c WB |
89 | sub status { |
90 | my ($class, $storeid, $scfg, $cache) = @_; | |
91 | ||
de8eff4d | 92 | if (defined(my $mp = parse_is_mountpoint($scfg))) { |
8b5ccc06 FG |
93 | $cache->{mountdata} = PVE::ProcFSTools::parse_proc_mounts() |
94 | if !$cache->{mountdata}; | |
d547f26c | 95 | |
de8eff4d | 96 | return undef if !path_is_mounted($mp, $cache->{mountdata}); |
8b5ccc06 | 97 | } |
d547f26c WB |
98 | |
99 | return $class->SUPER::status($storeid, $scfg, $cache); | |
100 | } | |
101 | ||
1dc01b9f DM |
102 | |
103 | sub activate_storage { | |
104 | my ($class, $storeid, $scfg, $cache) = @_; | |
105 | ||
d547f26c | 106 | my $path = $scfg->{path}; |
b521247b | 107 | if (!defined($scfg->{mkdir}) || $scfg->{mkdir}) { |
b521247b WB |
108 | mkpath $path; |
109 | } | |
1dc01b9f | 110 | |
de8eff4d WB |
111 | my $mp = parse_is_mountpoint($scfg); |
112 | if (defined($mp) && !path_is_mounted($mp, $cache->{mountdata})) { | |
d547f26c | 113 | die "unable to activate storage '$storeid' - " . |
de8eff4d | 114 | "directory is expected to be a mount point but is not mounted: '$mp'\n"; |
d547f26c WB |
115 | } |
116 | ||
c2fc9fbe | 117 | $class->SUPER::activate_storage($storeid, $scfg, $cache); |
1dc01b9f DM |
118 | } |
119 | ||
5c95e484 WB |
120 | sub check_config { |
121 | my ($self, $sectionId, $config, $create, $skipSchemaCheck) = @_; | |
122 | my $opts = PVE::SectionConfig::check_config($self, $sectionId, $config, $create, $skipSchemaCheck); | |
123 | return $opts if !$create; | |
124 | if ($opts->{path} !~ m@^/[-/a-zA-Z0-9_.]+$@) { | |
125 | die "illegal path for directory storage: $opts->{path}\n"; | |
126 | } | |
127 | return $opts; | |
128 | } | |
1dc01b9f DM |
129 | |
130 | 1; |