]>
Commit | Line | Data |
---|---|---|
e34ce144 AA |
1 | package PVE::Storage::CephFSPlugin; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5402cea5 | 5 | |
e34ce144 AA |
6 | use IO::File; |
7 | use Net::IP; | |
8 | use File::Path; | |
5402cea5 | 9 | |
e34ce144 AA |
10 | use PVE::Tools qw(run_command); |
11 | use PVE::ProcFSTools; | |
12 | use PVE::Storage::Plugin; | |
13 | use PVE::JSONSchema qw(get_standard_option); | |
4050fcc1 | 14 | use PVE::CephConfig; |
e34ce144 AA |
15 | |
16 | use base qw(PVE::Storage::Plugin); | |
17 | ||
18 | sub cephfs_is_mounted { | |
19 | my ($scfg, $storeid, $mountdata) = @_; | |
20 | ||
4050fcc1 | 21 | my $cmd_option = PVE::CephConfig::ceph_connect_option($scfg, $storeid); |
e34ce144 | 22 | my $configfile = $cmd_option->{ceph_conf}; |
4050fcc1 | 23 | my $server = $cmd_option->{mon_host} // PVE::CephConfig::get_monaddr_list($configfile); |
e34ce144 AA |
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 | ||
4050fcc1 | 49 | my $cmd_option = PVE::CephConfig::ceph_connect_option($scfg, $storeid); |
e34ce144 AA |
50 | my $configfile = $cmd_option->{ceph_conf}; |
51 | my $secretfile = $cmd_option->{keyring}; | |
4050fcc1 | 52 | my $server = $cmd_option->{mon_host} // PVE::CephConfig::get_monaddr_list($configfile); |
e34ce144 AA |
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 | ||
83 | sub type { | |
84 | return 'cephfs'; | |
85 | } | |
86 | ||
87 | sub plugindata { | |
88 | return { | |
d1eb35ea | 89 | content => [ { vztmpl => 1, iso => 1, backup => 1, snippets => 1}, |
e34ce144 AA |
90 | { backup => 1 }], |
91 | }; | |
92 | } | |
93 | ||
94 | sub 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 | ||
107 | sub 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 }, | |
d35a0b4b | 121 | maxfiles => { optional => 1 }, |
e34ce144 AA |
122 | }; |
123 | } | |
124 | ||
125 | sub check_config { | |
126 | my ($class, $sectionId, $config, $create, $skipSchemaCheck) = @_; | |
127 | ||
128 | $config->{path} = "/mnt/pve/$sectionId" if $create && !$config->{path}; | |
129 | ||
130 | return $class->SUPER::check_config($sectionId, $config, $create, $skipSchemaCheck); | |
131 | } | |
132 | ||
133 | # Storage implementation | |
134 | ||
135 | sub on_add_hook { | |
136 | my ($class, $storeid, $scfg, %param) = @_; | |
137 | ||
138 | return if defined($scfg->{monhost}); # nothing to do if not pve managed ceph | |
139 | ||
4050fcc1 | 140 | PVE::CephConfig::ceph_create_keyfile($scfg->{type}, $storeid); |
e34ce144 AA |
141 | } |
142 | ||
143 | sub on_delete_hook { | |
144 | my ($class, $storeid, $scfg) = @_; | |
145 | ||
146 | return if defined($scfg->{monhost}); # nothing to do if not pve managed ceph | |
147 | ||
4050fcc1 | 148 | PVE::CephConfig::ceph_remove_keyfile($scfg->{type}, $storeid); |
e34ce144 AA |
149 | } |
150 | ||
151 | sub status { | |
152 | my ($class, $storeid, $scfg, $cache) = @_; | |
153 | ||
5402cea5 | 154 | $cache->{mountdata} //= PVE::ProcFSTools::parse_proc_mounts(); |
e34ce144 AA |
155 | |
156 | return undef if !cephfs_is_mounted($scfg, $storeid, $cache->{mountdata}); | |
157 | ||
158 | return $class->SUPER::status($storeid, $scfg, $cache); | |
159 | } | |
160 | ||
161 | sub activate_storage { | |
162 | my ($class, $storeid, $scfg, $cache) = @_; | |
163 | ||
5402cea5 | 164 | $cache->{mountdata} //= PVE::ProcFSTools::parse_proc_mounts(); |
e34ce144 | 165 | |
5402cea5 | 166 | # NOTE: mkpath may hang if storage is mounted but not reachable |
e34ce144 | 167 | if (!cephfs_is_mounted($scfg, $storeid, $cache->{mountdata})) { |
5402cea5 | 168 | my $path = $scfg->{path}; |
e34ce144 AA |
169 | |
170 | mkpath $path if !(defined($scfg->{mkdir}) && !$scfg->{mkdir}); | |
171 | ||
172 | die "unable to activate storage '$storeid' - " . | |
173 | "directory '$path' does not exist\n" if ! -d $path; | |
174 | ||
175 | cephfs_mount($scfg, $storeid); | |
176 | } | |
177 | ||
178 | $class->SUPER::activate_storage($storeid, $scfg, $cache); | |
179 | } | |
180 | ||
181 | sub deactivate_storage { | |
182 | my ($class, $storeid, $scfg, $cache) = @_; | |
183 | ||
5402cea5 | 184 | $cache->{mountdata} //= PVE::ProcFSTools::parse_proc_mounts(); |
e34ce144 AA |
185 | |
186 | my $path = $scfg->{path}; | |
187 | ||
188 | if (cephfs_is_mounted($scfg, $storeid, $cache->{mountdata})) { | |
5402cea5 | 189 | run_command(['/bin/umount', $path], errmsg => 'umount error'); |
e34ce144 AA |
190 | } |
191 | } | |
192 | ||
193 | 1; |