]> git.proxmox.com Git - pve-storage.git/blame - PVE/Storage/CephFSPlugin.pm
bump version to 5.0-33
[pve-storage.git] / PVE / Storage / CephFSPlugin.pm
CommitLineData
e34ce144
AA
1package PVE::Storage::CephFSPlugin;
2
3use strict;
4use warnings;
5402cea5 5
e34ce144
AA
6use IO::File;
7use Net::IP;
8use File::Path;
5402cea5 9
e34ce144
AA
10use PVE::Tools qw(run_command);
11use PVE::ProcFSTools;
12use PVE::Storage::Plugin;
13use PVE::JSONSchema qw(get_standard_option);
14use PVE::Storage::CephTools;
15
16use base qw(PVE::Storage::Plugin);
17
18sub 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
42sub 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);
5402cea5 64 } else {
e34ce144
AA
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 }
54e0b003
TL
69 # tell systemd that we're network dependent, else it umounts us to late on
70 # shutdown, when we couldn't connect to the active MDS and thus unmount
71 # hangs and delays shutdown/reboot (man systemd.mount)
72 push @$cmd, '-o', '_netdev';
e34ce144
AA
73
74 if ($scfg->{options}) {
75 push @$cmd, '-o', $scfg->{options};
76 }
77
78 run_command($cmd, errmsg => "mount error");
79}
80
81# Configuration
82
83sub type {
84 return 'cephfs';
85}
86
87sub plugindata {
88 return {
89 content => [ { vztmpl => 1, iso => 1, backup => 1},
90 { backup => 1 }],
91 };
92}
93
94sub properties {
95 return {
96 fuse => {
97 description => "Mount CephFS through FUSE.",
98 type => 'boolean',
99 },
100 subdir => {
101 description => "Subdir to mount.",
102 type => 'string', format => 'pve-storage-path',
103 },
104 };
105}
106
107sub options {
108 return {
109 path => { fixed => 1 },
110 monhost => { optional => 1},
111 nodes => { optional => 1 },
112 subdir => { optional => 1 },
113 disable => { optional => 1 },
114 options => { optional => 1 },
115 username => { optional => 1 },
116 content => { optional => 1 },
117 format => { optional => 1 },
118 mkdir => { optional => 1 },
119 fuse => { optional => 1 },
120 bwlimit => { optional => 1 },
121 };
122}
123
124sub check_config {
125 my ($class, $sectionId, $config, $create, $skipSchemaCheck) = @_;
126
127 $config->{path} = "/mnt/pve/$sectionId" if $create && !$config->{path};
128
129 return $class->SUPER::check_config($sectionId, $config, $create, $skipSchemaCheck);
130}
131
132# Storage implementation
133
134sub on_add_hook {
135 my ($class, $storeid, $scfg, %param) = @_;
136
137 return if defined($scfg->{monhost}); # nothing to do if not pve managed ceph
138
139 PVE::Storage::CephTools::ceph_create_keyfile($scfg->{type}, $storeid);
140}
141
142sub on_delete_hook {
143 my ($class, $storeid, $scfg) = @_;
144
145 return if defined($scfg->{monhost}); # nothing to do if not pve managed ceph
146
147 PVE::Storage::CephTools::ceph_remove_keyfile($scfg->{type}, $storeid);
e34ce144
AA
148}
149
150sub status {
151 my ($class, $storeid, $scfg, $cache) = @_;
152
5402cea5 153 $cache->{mountdata} //= PVE::ProcFSTools::parse_proc_mounts();
e34ce144
AA
154
155 return undef if !cephfs_is_mounted($scfg, $storeid, $cache->{mountdata});
156
157 return $class->SUPER::status($storeid, $scfg, $cache);
158}
159
160sub activate_storage {
161 my ($class, $storeid, $scfg, $cache) = @_;
162
5402cea5 163 $cache->{mountdata} //= PVE::ProcFSTools::parse_proc_mounts();
e34ce144 164
5402cea5 165 # NOTE: mkpath may hang if storage is mounted but not reachable
e34ce144 166 if (!cephfs_is_mounted($scfg, $storeid, $cache->{mountdata})) {
5402cea5 167 my $path = $scfg->{path};
e34ce144
AA
168
169 mkpath $path if !(defined($scfg->{mkdir}) && !$scfg->{mkdir});
170
171 die "unable to activate storage '$storeid' - " .
172 "directory '$path' does not exist\n" if ! -d $path;
173
174 cephfs_mount($scfg, $storeid);
175 }
176
177 $class->SUPER::activate_storage($storeid, $scfg, $cache);
178}
179
180sub deactivate_storage {
181 my ($class, $storeid, $scfg, $cache) = @_;
182
5402cea5 183 $cache->{mountdata} //= PVE::ProcFSTools::parse_proc_mounts();
e34ce144
AA
184
185 my $path = $scfg->{path};
186
187 if (cephfs_is_mounted($scfg, $storeid, $cache->{mountdata})) {
5402cea5 188 run_command(['/bin/umount', $path], errmsg => 'umount error');
e34ce144
AA
189 }
190}
191
1921;