]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/DirPlugin.pm
rbd plugin: free image: use actual command in error message
[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, snippets => 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 bwlimit => get_standard_option('bwlimit'),
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 'prune-backups' => { optional => 1 },
57 content => { optional => 1 },
58 format => { optional => 1 },
59 mkdir => { optional => 1 },
60 is_mountpoint => { optional => 1 },
61 bwlimit => { optional => 1 },
62 preallocation => { optional => 1 },
63 };
64 }
65
66 # Storage implementation
67 #
68
69 # NOTE: should ProcFSTools::is_mounted accept an optional cache like this?
70 sub path_is_mounted {
71 my ($mountpoint, $mountdata) = @_;
72
73 $mountpoint = Cwd::realpath($mountpoint); # symlinks
74 return 0 if !defined($mountpoint); # path does not exist
75
76 $mountdata = PVE::ProcFSTools::parse_proc_mounts() if !$mountdata;
77 return 1 if grep { $_->[1] eq $mountpoint } @$mountdata;
78 return undef;
79 }
80
81 sub parse_is_mountpoint {
82 my ($scfg) = @_;
83 my $is_mp = $scfg->{is_mountpoint};
84 return undef if !defined $is_mp;
85 if (defined(my $bool = PVE::JSONSchema::parse_boolean($is_mp))) {
86 return $bool ? $scfg->{path} : undef;
87 }
88 return $is_mp; # contains a path
89 }
90
91 sub get_volume_notes {
92 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
93
94 my $path = $class->filesystem_path($scfg, $volname);
95 $path .= $class->SUPER::NOTES_EXT;
96
97 return PVE::Tools::file_get_contents($path) if -f $path;
98
99 return '';
100 }
101
102 sub update_volume_notes {
103 my ($class, $scfg, $storeid, $volname, $notes, $timeout) = @_;
104
105 my ($vtype) = $class->parse_volname($volname);
106 die "only backups can have notes\n" if $vtype ne 'backup';
107
108 my $path = $class->filesystem_path($scfg, $volname);
109 $path .= $class->SUPER::NOTES_EXT;
110
111 if (defined($notes) && $notes ne '') {
112 PVE::Tools::file_set_contents($path, $notes);
113 } else {
114 unlink $path or die "could not delete notes - $!\n";
115 }
116 return;
117 }
118
119 sub status {
120 my ($class, $storeid, $scfg, $cache) = @_;
121
122 if (defined(my $mp = parse_is_mountpoint($scfg))) {
123 $cache->{mountdata} = PVE::ProcFSTools::parse_proc_mounts()
124 if !$cache->{mountdata};
125
126 return undef if !path_is_mounted($mp, $cache->{mountdata});
127 }
128
129 return $class->SUPER::status($storeid, $scfg, $cache);
130 }
131
132
133 sub activate_storage {
134 my ($class, $storeid, $scfg, $cache) = @_;
135
136 my $path = $scfg->{path};
137 if (!defined($scfg->{mkdir}) || $scfg->{mkdir}) {
138 mkpath $path;
139 }
140
141 my $mp = parse_is_mountpoint($scfg);
142 if (defined($mp) && !path_is_mounted($mp, $cache->{mountdata})) {
143 die "unable to activate storage '$storeid' - " .
144 "directory is expected to be a mount point but is not mounted: '$mp'\n";
145 }
146
147 $class->SUPER::activate_storage($storeid, $scfg, $cache);
148 }
149
150 sub check_config {
151 my ($self, $sectionId, $config, $create, $skipSchemaCheck) = @_;
152 my $opts = PVE::SectionConfig::check_config($self, $sectionId, $config, $create, $skipSchemaCheck);
153 return $opts if !$create;
154 if ($opts->{path} !~ m@^/[-/a-zA-Z0-9_.]+$@) {
155 die "illegal path for directory storage: $opts->{path}\n";
156 }
157 return $opts;
158 }
159
160 1;