]> git.proxmox.com Git - pve-storage.git/blob - PVE/API2/Disks/Directory.pm
followup comment that we do not escape completely like systemd
[pve-storage.git] / PVE / API2 / Disks / Directory.pm
1 package PVE::API2::Disks::Directory;
2
3 use strict;
4 use warnings;
5
6 use PVE::Diskmanage;
7 use PVE::JSONSchema qw(get_standard_option);
8 use PVE::API2::Storage::Config;
9 use PVE::Tools qw(run_command trim file_set_contents file_get_contents dir_glob_foreach lock_file);
10
11 use PVE::RPCEnvironment;
12 use PVE::RESTHandler;
13
14 use base qw(PVE::RESTHandler);
15
16 my $SGDISK = '/sbin/sgdisk';
17 my $MKFS = '/sbin/mkfs';
18 my $BLKID = '/sbin/blkid';
19
20 my $read_ini = sub {
21 my ($filename) = @_;
22
23 my $content = file_get_contents($filename);
24 my @lines = split /\n/, $content;
25
26 my $result = {};
27 my $section;
28
29 foreach my $line (@lines) {
30 $line = trim($line);
31 if ($line =~ m/^\[([^\]]+)\]/) {
32 $section = $1;
33 if (!defined($result->{$section})) {
34 $result->{$section} = {};
35 }
36 } elsif ($line =~ m/^(.*?)=(.*)$/) {
37 my ($key, $val) = ($1, $2);
38 if (!$section) {
39 warn "key value pair found without section, skipping\n";
40 next;
41 }
42
43 if ($result->{$section}->{$key}) {
44 # make duplicate properties to arrays to keep the order
45 my $prop = $result->{$section}->{$key};
46 if (ref($prop) eq 'ARRAY') {
47 push @$prop, $val;
48 } else {
49 $result->{$section}->{$key} = [$prop, $val];
50 }
51 } else {
52 $result->{$section}->{$key} = $val;
53 }
54 }
55 # ignore everything else
56 }
57
58 return $result;
59 };
60
61 my $write_ini = sub {
62 my ($ini, $filename) = @_;
63
64 my $content = "";
65
66 foreach my $sname (sort keys %$ini) {
67 my $section = $ini->{$sname};
68
69 $content .= "[$sname]\n";
70
71 foreach my $pname (sort keys %$section) {
72 my $prop = $section->{$pname};
73
74 if (!ref($prop)) {
75 $content .= "$pname=$prop\n";
76 } elsif (ref($prop) eq 'ARRAY') {
77 foreach my $val (@$prop) {
78 $content .= "$pname=$val\n";
79 }
80 } else {
81 die "invalid property '$pname'\n";
82 }
83 }
84 $content .= "\n";
85 }
86
87 file_set_contents($filename, $content);
88 };
89
90 sub systemd_escape {
91 my ($val) = @_;
92
93 # NOTE: this is not complete, but enough for our needs. normally all
94 # characters which are not alpha-numerical, '.' or '_' would need escaping
95 $val =~ s/\-/\\x2d/g;
96
97 return $val;
98 }
99
100 sub systemd_unescape {
101 my ($val) = @_;
102
103 $val =~ s/\\x([a-fA-F0-9]{2})/chr(hex($1))/eg;
104
105 return $val;
106 }
107
108 __PACKAGE__->register_method ({
109 name => 'index',
110 path => '',
111 method => 'GET',
112 proxyto => 'node',
113 protected => 1,
114 permissions => {
115 check => ['perm', '/', ['Sys.Audit', 'Datastore.Audit'], any => 1],
116 },
117 description => "PVE Managed Directory storages.",
118 parameters => {
119 additionalProperties => 0,
120 properties => {
121 node => get_standard_option('pve-node'),
122 },
123 },
124 returns => {
125 type => 'array',
126 items => {
127 type => 'object',
128 properties => {
129 unitfile => {
130 type => 'string',
131 description => 'The path of the mount unit.',
132 },
133 path => {
134 type => 'string',
135 description => 'The mount path.',
136 },
137 device => {
138 type => 'string',
139 description => 'The mounted device.',
140 },
141 type => {
142 type => 'string',
143 description => 'The filesystem type.',
144 },
145 options => {
146 type => 'string',
147 description => 'The mount options.',
148 },
149 },
150 },
151 },
152 code => sub {
153 my ($param) = @_;
154
155 my $result = [];
156
157 dir_glob_foreach('/etc/systemd/system', '^mnt-pve-(.+)\.mount$', sub {
158 my ($filename, $storid) = @_;
159 $storid = systemd_unescape($storid);
160
161 my $unitfile = "/etc/systemd/system/$filename";
162 my $unit = $read_ini->($unitfile);
163
164 push @$result, {
165 unitfile => $unitfile,
166 path => "/mnt/pve/$storid",
167 device => $unit->{'Mount'}->{'What'},
168 type => $unit->{'Mount'}->{'Type'},
169 options => $unit->{'Mount'}->{'Options'},
170 };
171 });
172
173 return $result;
174 }});
175
176 __PACKAGE__->register_method ({
177 name => 'create',
178 path => '',
179 method => 'POST',
180 proxyto => 'node',
181 protected => 1,
182 permissions => {
183 check => ['perm', '/', ['Sys.Modify', 'Datastore.Allocate']],
184 },
185 description => "Create a Filesystem on an unused disk. Will be mounted under '/mnt/pve/NAME'.",
186 parameters => {
187 additionalProperties => 0,
188 properties => {
189 node => get_standard_option('pve-node'),
190 name => get_standard_option('pve-storage-id'),
191 device => {
192 type => 'string',
193 description => 'The block device you want to create the filesystem on.',
194 },
195 add_storage => {
196 description => "Configure storage using the directory.",
197 type => 'boolean',
198 optional => 1,
199 default => 0,
200 },
201 filesystem => {
202 description => "The desired filesystem.",
203 type => 'string',
204 enum => ['ext4', 'xfs'],
205 optional => 1,
206 default => 'ext4',
207 },
208 },
209 },
210 returns => { type => 'string' },
211 code => sub {
212 my ($param) = @_;
213
214 my $rpcenv = PVE::RPCEnvironment::get();
215 my $user = $rpcenv->get_user();
216
217 my $name = $param->{name};
218 my $dev = $param->{device};
219 my $node = $param->{node};
220 my $type = $param->{filesystem} // 'ext4';
221
222 $dev = PVE::Diskmanage::verify_blockdev_path($dev);
223 PVE::Diskmanage::assert_disk_unused($dev);
224 PVE::Storage::assert_sid_unused($name) if $param->{add_storage};
225
226 my $worker = sub {
227 my $path = "/mnt/pve/$name";
228 my $mountunitname = "mnt-pve-".systemd_escape($name).".mount";
229 my $mountunitpath = "/etc/systemd/system/$mountunitname";
230
231 PVE::Diskmanage::locked_disk_action(sub {
232 # create partition
233 my $cmd = [$SGDISK, '-n1', '-t1:8300', $dev];
234 print "# ", join(' ', @$cmd), "\n";
235 run_command($cmd);
236
237 my ($devname) = $dev =~ m|^/dev/(.*)$|;
238 my $part = "/dev/";
239 dir_glob_foreach("/sys/block/$devname", qr/\Q$devname\E.+/, sub {
240 my ($partition) = @_;
241 $part .= $partition;
242 });
243
244 # create filesystem
245 $cmd = [$MKFS, '-t', $type, $part];
246 print "# ", join(' ', @$cmd), "\n";
247 run_command($cmd);
248
249 # create systemd mount unit and enable & start it
250 my $ini = {
251 'Unit' => {
252 'Description' => "Mount storage '$name' under /mnt/pve",
253 },
254 'Install' => {
255 'WantedBy' => 'multi-user.target',
256 },
257 };
258
259 my $uuid_path;
260 my $uuid;
261
262 $cmd = [$BLKID, $part, '-o', 'export'];
263 print "# ", join(' ', @$cmd), "\n";
264 run_command($cmd, outfunc => sub {
265 my ($line) = @_;
266
267 if ($line =~ m/^UUID=(.*)$/) {
268 $uuid = $1;
269 $uuid_path = "/dev/disk/by-uuid/$uuid";
270 }
271 });
272
273 die "could not get UUID of device '$part'\n" if !$uuid;
274
275 $ini->{'Mount'} = {
276 'What' => $uuid_path,
277 'Where' => $path,
278 'Type' => $type,
279 'Options' => 'defaults',
280 };
281
282 $write_ini->($ini, $mountunitpath);
283
284 run_command(['systemctl', 'daemon-reload']);
285 run_command(['systemctl', 'enable', $mountunitname]);
286 run_command(['systemctl', 'start', $mountunitname]);
287
288 if ($param->{add_storage}) {
289 my $storage_params = {
290 type => 'dir',
291 storage => $name,
292 content => 'rootdir,images,iso,backup,vztmpl,snippets',
293 is_mountpoint => 1,
294 path => $path,
295 nodes => $node,
296 };
297
298 PVE::API2::Storage::Config->create($storage_params);
299 }
300 });
301 };
302
303 return $rpcenv->fork_worker('dircreate', $name, $user, $worker);
304 }});
305
306 1;