]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/DirPlugin.pm
Fix #1012: dir storage: add is_mountpoint option
[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 directory is an externally managed mountpoint. " .
40 "If nothing is mounted the storage will be considered offline.",
41 type => 'boolean',
42 default => 'no',
43 },
44 };
45 }
46
47 sub 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 },
56 mkdir => { optional => 1 },
57 is_mountpoint => { optional => 1 },
58 };
59 }
60
61 # Storage implementation
62 #
63
64 # NOTE: should ProcFSTools::is_mounted accept an optional cache like this?
65 sub 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
76 sub 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
89
90 sub activate_storage {
91 my ($class, $storeid, $scfg, $cache) = @_;
92
93 my $path = $scfg->{path};
94 if (!defined($scfg->{mkdir}) || $scfg->{mkdir}) {
95 mkpath $path;
96 }
97
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
103 $class->SUPER::activate_storage($storeid, $scfg, $cache);
104 }
105
106 sub 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 }
115
116 1;