]> git.proxmox.com Git - pve-storage.git/blame - PVE/Storage/DirPlugin.pm
Fix #1012: dir storage: add is_mountpoint option
[pve-storage.git] / PVE / Storage / DirPlugin.pm
CommitLineData
1dc01b9f
DM
1package PVE::Storage::DirPlugin;
2
3use strict;
4use warnings;
d547f26c 5use Cwd;
1dc01b9f
DM
6use File::Path;
7use PVE::Storage::Plugin;
8use PVE::JSONSchema qw(get_standard_option);
9
10use base qw(PVE::Storage::Plugin);
11
12# Configuration
13
14sub type {
15 return 'dir';
16}
17
18sub plugindata {
19 return {
20 content => [ { images => 1, rootdir => 1, vztmpl => 1, iso => 1, backup => 1, none => 1 },
21 { images => 1, rootdir => 1 }],
35533c68 22 format => [ { raw => 1, qcow2 => 1, vmdk => 1, subvol => 1 } , 'raw' ],
1dc01b9f
DM
23 };
24}
25
26sub 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 =>
39 "Assume the directory is an externally managed mountpoint. " .
40 "If nothing is mounted the storage will be considered offline.",
41 type => 'boolean',
42 default => 'no',
43 },
1dc01b9f
DM
44 };
45}
46
47sub options {
48 return {
49 path => { fixed => 1 },
50 nodes => { optional => 1 },
51 shared => { optional => 1 },
52 disable => { optional => 1 },
53 maxfiles => { optional => 1 },
54 content => { optional => 1 },
55 format => { optional => 1 },
b521247b 56 mkdir => { optional => 1 },
d547f26c 57 is_mountpoint => { optional => 1 },
1dc01b9f
DM
58 };
59}
60
61# Storage implementation
d547f26c
WB
62#
63
64# NOTE: should ProcFSTools::is_mounted accept an optional cache like this?
65sub path_is_mounted {
66 my ($mountpoint, $mountdata) = @_;
67
68 $mountpoint = Cwd::realpath($mountpoint); # symlinks
69 return 0 if !defined($mountpoint); # path does not exist
70
71 $mountdata = PVE::ProcFSTools::parse_proc_mounts() if !$mountdata;
72 return 1 if grep { $_->[1] eq $mountpoint } @$mountdata;
73 return undef;
74}
75
76sub status {
77 my ($class, $storeid, $scfg, $cache) = @_;
78
79 $cache->{mountdata} = PVE::ProcFSTools::parse_proc_mounts()
80 if !$cache->{mountdata};
81
82 my $path = $scfg->{path};
83
84 return undef if !path_is_mounted($path, $cache->{mountdata});
85
86 return $class->SUPER::status($storeid, $scfg, $cache);
87}
88
1dc01b9f
DM
89
90sub activate_storage {
91 my ($class, $storeid, $scfg, $cache) = @_;
92
d547f26c 93 my $path = $scfg->{path};
b521247b 94 if (!defined($scfg->{mkdir}) || $scfg->{mkdir}) {
b521247b
WB
95 mkpath $path;
96 }
1dc01b9f 97
d547f26c
WB
98 if ($scfg->{is_mountpoint} && !path_is_mounted($path, $cache->{mountdata})) {
99 die "unable to activate storage '$storeid' - " .
100 "directory is expected to be a mount point but is not mounted: '$path'\n";
101 }
102
1dc01b9f
DM
103 $class->SUPER::activate_storage($storeid, $scfg, $cache);
104}
105
5c95e484
WB
106sub check_config {
107 my ($self, $sectionId, $config, $create, $skipSchemaCheck) = @_;
108 my $opts = PVE::SectionConfig::check_config($self, $sectionId, $config, $create, $skipSchemaCheck);
109 return $opts if !$create;
110 if ($opts->{path} !~ m@^/[-/a-zA-Z0-9_.]+$@) {
111 die "illegal path for directory storage: $opts->{path}\n";
112 }
113 return $opts;
114}
1dc01b9f
DM
115
1161;