]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/DirPlugin.pm
dir: allow a path as is_mountpoint value
[pve-storage.git] / PVE / Storage / DirPlugin.pm
1 package PVE::Storage::DirPlugin;
2
3 use strict;
4 use warnings;
5 use Cwd;
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 {
20 content => [ { images => 1, rootdir => 1, vztmpl => 1, iso => 1, backup => 1, none => 1 },
21 { images => 1, rootdir => 1 }],
22 format => [ { raw => 1, qcow2 => 1, vmdk => 1, subvol => 1 } , 'raw' ],
23 };
24 }
25
26 sub properties {
27 return {
28 path => {
29 description => "File system path.",
30 type => 'string', format => 'pve-storage-path',
31 },
32 mkdir => {
33 description => "Create the directory if it doesn't exist.",
34 type => 'boolean',
35 default => 'yes',
36 },
37 is_mountpoint => {
38 description =>
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',
43 default => 'no',
44 },
45 };
46 }
47
48 sub options {
49 return {
50 path => { fixed => 1 },
51 nodes => { optional => 1 },
52 shared => { optional => 1 },
53 disable => { optional => 1 },
54 maxfiles => { optional => 1 },
55 content => { optional => 1 },
56 format => { optional => 1 },
57 mkdir => { optional => 1 },
58 is_mountpoint => { optional => 1 },
59 };
60 }
61
62 # Storage implementation
63 #
64
65 # NOTE: should ProcFSTools::is_mounted accept an optional cache like this?
66 sub path_is_mounted {
67 my ($mountpoint, $mountdata) = @_;
68
69 $mountpoint = Cwd::realpath($mountpoint); # symlinks
70 return 0 if !defined($mountpoint); # path does not exist
71
72 $mountdata = PVE::ProcFSTools::parse_proc_mounts() if !$mountdata;
73 return 1 if grep { $_->[1] eq $mountpoint } @$mountdata;
74 return undef;
75 }
76
77 sub parse_is_mountpoint {
78 my ($scfg) = @_;
79 my $is_mp = $scfg->{is_mountpoint};
80 return undef if !defined $is_mp;
81 if (defined(my $bool = PVE::JSONSchema::parse_boolean($is_mp))) {
82 return $bool ? $scfg->{path} : undef;
83 }
84 return $is_mp; # contains a path
85 }
86
87 sub status {
88 my ($class, $storeid, $scfg, $cache) = @_;
89
90 if (defined(my $mp = parse_is_mountpoint($scfg))) {
91 $cache->{mountdata} = PVE::ProcFSTools::parse_proc_mounts()
92 if !$cache->{mountdata};
93
94 return undef if !path_is_mounted($mp, $cache->{mountdata});
95 }
96
97 return $class->SUPER::status($storeid, $scfg, $cache);
98 }
99
100
101 sub activate_storage {
102 my ($class, $storeid, $scfg, $cache) = @_;
103
104 my $path = $scfg->{path};
105 if (!defined($scfg->{mkdir}) || $scfg->{mkdir}) {
106 mkpath $path;
107 }
108
109 my $mp = parse_is_mountpoint($scfg);
110 if (defined($mp) && !path_is_mounted($mp, $cache->{mountdata})) {
111 die "unable to activate storage '$storeid' - " .
112 "directory is expected to be a mount point but is not mounted: '$mp'\n";
113 }
114
115 $class->SUPER::activate_storage($storeid, $scfg, $cache);
116 }
117
118 sub check_config {
119 my ($self, $sectionId, $config, $create, $skipSchemaCheck) = @_;
120 my $opts = PVE::SectionConfig::check_config($self, $sectionId, $config, $create, $skipSchemaCheck);
121 return $opts if !$create;
122 if ($opts->{path} !~ m@^/[-/a-zA-Z0-9_.]+$@) {
123 die "illegal path for directory storage: $opts->{path}\n";
124 }
125 return $opts;
126 }
127
128 1;