]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/CephFSPlugin.pm
cephfs plugin: followup with some code cleanups
[pve-storage.git] / PVE / Storage / CephFSPlugin.pm
1 package PVE::Storage::CephFSPlugin;
2
3 use strict;
4 use warnings;
5
6 use IO::File;
7 use Net::IP;
8 use File::Path;
9
10 use PVE::Tools qw(run_command);
11 use PVE::ProcFSTools;
12 use PVE::Storage::Plugin;
13 use PVE::JSONSchema qw(get_standard_option);
14 use PVE::Storage::CephTools;
15
16 use base qw(PVE::Storage::Plugin);
17
18 sub cephfs_is_mounted {
19 my ($scfg, $storeid, $mountdata) = @_;
20
21 my $cmd_option = PVE::Storage::CephTools::ceph_connect_option($scfg, $storeid);
22 my $configfile = $cmd_option->{ceph_conf};
23 my $server = $cmd_option->{mon_host} // PVE::Storage::CephTools::get_monaddr_list($configfile);
24
25 my $subdir = $scfg->{subdir} // '/';
26 my $mountpoint = $scfg->{path};
27 my $source = "$server:$subdir";
28
29 $mountdata = PVE::ProcFSTools::parse_proc_mounts() if !$mountdata;
30 return $mountpoint if grep {
31 $_->[2] =~ m#^ceph|fuse\.ceph-fuse# &&
32 $_->[0] =~ m#^\Q$source\E|ceph-fuse$# &&
33 $_->[1] eq $mountpoint
34 } @$mountdata;
35
36 warn "A filesystem is already mounted on $mountpoint\n"
37 if grep { $_->[1] eq $mountpoint } @$mountdata;
38
39 return undef;
40 }
41
42 sub cephfs_mount {
43 my ($scfg, $storeid) = @_;
44
45 my $cmd;
46 my $mountpoint = $scfg->{path};
47 my $subdir = $scfg->{subdir} // '/';
48
49 my $cmd_option = PVE::Storage::CephTools::ceph_connect_option($scfg, $storeid);
50 my $configfile = $cmd_option->{ceph_conf};
51 my $secretfile = $cmd_option->{keyring};
52 my $server = $cmd_option->{mon_host} // PVE::Storage::CephTools::get_monaddr_list($configfile);
53
54 # fuse -> client-enforced quotas (kernel doesn't), updates w/ ceph-fuse pkg
55 # kernel -> better performance, less frequent updates
56 if ($scfg->{fuse}) {
57 # FIXME: ceph-fuse client complains about missing ceph.conf or keyring if
58 # not provided on its default locations but still connects. Fix upstream??
59 $cmd = ['/usr/bin/ceph-fuse', '-n', "client.$cmd_option->{userid}", '-m', $server];
60 push @$cmd, '--keyfile', $secretfile if defined($secretfile);
61 push @$cmd, '-r', $subdir if !($subdir =~ m|^/$|);
62 push @$cmd, $mountpoint;
63 push @$cmd, '--conf', $configfile if defined($configfile);
64 } else {
65 my $source = "$server:$subdir";
66 $cmd = ['/bin/mount', '-t', 'ceph', $source, $mountpoint, '-o', "name=$cmd_option->{userid}"];
67 push @$cmd, '-o', "secretfile=$secretfile" if defined($secretfile);
68 }
69
70 if ($scfg->{options}) {
71 push @$cmd, '-o', $scfg->{options};
72 }
73
74 run_command($cmd, errmsg => "mount error");
75 }
76
77 # Configuration
78
79 sub type {
80 return 'cephfs';
81 }
82
83 sub plugindata {
84 return {
85 content => [ { vztmpl => 1, iso => 1, backup => 1},
86 { backup => 1 }],
87 };
88 }
89
90 sub properties {
91 return {
92 fuse => {
93 description => "Mount CephFS through FUSE.",
94 type => 'boolean',
95 },
96 subdir => {
97 description => "Subdir to mount.",
98 type => 'string', format => 'pve-storage-path',
99 },
100 };
101 }
102
103 sub options {
104 return {
105 path => { fixed => 1 },
106 monhost => { optional => 1},
107 nodes => { optional => 1 },
108 subdir => { optional => 1 },
109 disable => { optional => 1 },
110 options => { optional => 1 },
111 username => { optional => 1 },
112 content => { optional => 1 },
113 format => { optional => 1 },
114 mkdir => { optional => 1 },
115 fuse => { optional => 1 },
116 bwlimit => { optional => 1 },
117 };
118 }
119
120 sub check_config {
121 my ($class, $sectionId, $config, $create, $skipSchemaCheck) = @_;
122
123 $config->{path} = "/mnt/pve/$sectionId" if $create && !$config->{path};
124
125 return $class->SUPER::check_config($sectionId, $config, $create, $skipSchemaCheck);
126 }
127
128 # Storage implementation
129
130 sub on_add_hook {
131 my ($class, $storeid, $scfg, %param) = @_;
132
133 return if defined($scfg->{monhost}); # nothing to do if not pve managed ceph
134
135 PVE::Storage::CephTools::ceph_create_keyfile($scfg->{type}, $storeid);
136 }
137
138 sub on_delete_hook {
139 my ($class, $storeid, $scfg) = @_;
140
141 return if defined($scfg->{monhost}); # nothing to do if not pve managed ceph
142
143 PVE::Storage::CephTools::ceph_remove_keyfile($scfg->{type}, $storeid);
144 }
145
146 sub status {
147 my ($class, $storeid, $scfg, $cache) = @_;
148
149 $cache->{mountdata} //= PVE::ProcFSTools::parse_proc_mounts();
150
151 return undef if !cephfs_is_mounted($scfg, $storeid, $cache->{mountdata});
152
153 return $class->SUPER::status($storeid, $scfg, $cache);
154 }
155
156 sub activate_storage {
157 my ($class, $storeid, $scfg, $cache) = @_;
158
159 $cache->{mountdata} //= PVE::ProcFSTools::parse_proc_mounts();
160
161 # NOTE: mkpath may hang if storage is mounted but not reachable
162 if (!cephfs_is_mounted($scfg, $storeid, $cache->{mountdata})) {
163 my $path = $scfg->{path};
164
165 mkpath $path if !(defined($scfg->{mkdir}) && !$scfg->{mkdir});
166
167 die "unable to activate storage '$storeid' - " .
168 "directory '$path' does not exist\n" if ! -d $path;
169
170 cephfs_mount($scfg, $storeid);
171 }
172
173 $class->SUPER::activate_storage($storeid, $scfg, $cache);
174 }
175
176 sub deactivate_storage {
177 my ($class, $storeid, $scfg, $cache) = @_;
178
179 $cache->{mountdata} //= PVE::ProcFSTools::parse_proc_mounts();
180
181 my $path = $scfg->{path};
182
183 if (cephfs_is_mounted($scfg, $storeid, $cache->{mountdata})) {
184 run_command(['/bin/umount', $path], errmsg => 'umount error');
185 }
186 }
187
188 1;