]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/CephFSPlugin.pm
Don't remove and recreate lun when changing a volume
[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::CephConfig;
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::CephConfig::ceph_connect_option($scfg, $storeid);
22 my $configfile = $cmd_option->{ceph_conf};
23
24 my $subdir = $scfg->{subdir} // '/';
25 my $mountpoint = $scfg->{path};
26
27 $mountdata = PVE::ProcFSTools::parse_proc_mounts() if !$mountdata;
28 return $mountpoint if grep {
29 $_->[2] =~ m#^ceph|fuse\.ceph-fuse# &&
30 $_->[0] =~ m#\Q:$subdir\E$|^ceph-fuse$# &&
31 $_->[1] eq $mountpoint
32 } @$mountdata;
33
34 warn "A filesystem is already mounted on $mountpoint\n"
35 if grep { $_->[1] eq $mountpoint } @$mountdata;
36
37 return undef;
38 }
39
40 sub cephfs_mount {
41 my ($scfg, $storeid) = @_;
42
43 my $cmd;
44 my $mountpoint = $scfg->{path};
45 my $subdir = $scfg->{subdir} // '/';
46
47 my $cmd_option = PVE::CephConfig::ceph_connect_option($scfg, $storeid);
48 my $configfile = $cmd_option->{ceph_conf};
49 my $secretfile = $cmd_option->{keyring};
50 my $server = $cmd_option->{mon_host} // PVE::CephConfig::get_monaddr_list($configfile);
51
52 # fuse -> client-enforced quotas (kernel doesn't), updates w/ ceph-fuse pkg
53 # kernel -> better performance, less frequent updates
54 if ($scfg->{fuse}) {
55 # FIXME: ceph-fuse client complains about missing ceph.conf or keyring if
56 # not provided on its default locations but still connects. Fix upstream??
57 $cmd = ['/usr/bin/ceph-fuse', '-n', "client.$cmd_option->{userid}", '-m', $server];
58 push @$cmd, '--keyfile', $secretfile if defined($secretfile);
59 push @$cmd, '-r', $subdir if !($subdir =~ m|^/$|);
60 push @$cmd, $mountpoint;
61 push @$cmd, '--conf', $configfile if defined($configfile);
62 } else {
63 my $source = "$server:$subdir";
64 $cmd = ['/bin/mount', '-t', 'ceph', $source, $mountpoint, '-o', "name=$cmd_option->{userid}"];
65 push @$cmd, '-o', "secretfile=$secretfile" if defined($secretfile);
66
67 # tell systemd that we're network dependent, else it umounts us to late
68 # on shutdown, when we couldn't connect to the active MDS and thus
69 # unmount hangs and delays shutdown/reboot (man systemd.mount).
70 push @$cmd, '-o', '_netdev';
71 }
72
73 if ($scfg->{options}) {
74 push @$cmd, '-o', $scfg->{options};
75 }
76
77 run_command($cmd, errmsg => "mount error");
78 }
79
80 # Configuration
81
82 sub type {
83 return 'cephfs';
84 }
85
86 sub plugindata {
87 return {
88 content => [ { vztmpl => 1, iso => 1, backup => 1, snippets => 1},
89 { backup => 1 }],
90 };
91 }
92
93 sub properties {
94 return {
95 fuse => {
96 description => "Mount CephFS through FUSE.",
97 type => 'boolean',
98 },
99 subdir => {
100 description => "Subdir to mount.",
101 type => 'string', format => 'pve-storage-path',
102 },
103 };
104 }
105
106 sub options {
107 return {
108 path => { fixed => 1 },
109 monhost => { optional => 1},
110 nodes => { optional => 1 },
111 subdir => { optional => 1 },
112 disable => { optional => 1 },
113 options => { optional => 1 },
114 username => { optional => 1 },
115 content => { optional => 1 },
116 format => { optional => 1 },
117 mkdir => { optional => 1 },
118 fuse => { optional => 1 },
119 bwlimit => { optional => 1 },
120 maxfiles => { optional => 1 },
121 };
122 }
123
124 sub 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
134 sub 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::CephConfig::ceph_create_keyfile($scfg->{type}, $storeid);
140 }
141
142 sub 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::CephConfig::ceph_remove_keyfile($scfg->{type}, $storeid);
148 }
149
150 sub status {
151 my ($class, $storeid, $scfg, $cache) = @_;
152
153 $cache->{mountdata} //= PVE::ProcFSTools::parse_proc_mounts();
154
155 return undef if !cephfs_is_mounted($scfg, $storeid, $cache->{mountdata});
156
157 return $class->SUPER::status($storeid, $scfg, $cache);
158 }
159
160 sub activate_storage {
161 my ($class, $storeid, $scfg, $cache) = @_;
162
163 $cache->{mountdata} //= PVE::ProcFSTools::parse_proc_mounts();
164
165 # NOTE: mkpath may hang if storage is mounted but not reachable
166 if (!cephfs_is_mounted($scfg, $storeid, $cache->{mountdata})) {
167 my $path = $scfg->{path};
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
180 sub deactivate_storage {
181 my ($class, $storeid, $scfg, $cache) = @_;
182
183 $cache->{mountdata} //= PVE::ProcFSTools::parse_proc_mounts();
184
185 my $path = $scfg->{path};
186
187 if (cephfs_is_mounted($scfg, $storeid, $cache->{mountdata})) {
188 run_command(['/bin/umount', $path], errmsg => 'umount error');
189 }
190 }
191
192 1;