]>
Commit | Line | Data |
---|---|---|
1 | package PVE::Diskmanage; | |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
6 | use PVE::ProcFSTools; | |
7 | use Data::Dumper; | |
8 | use Cwd qw(abs_path); | |
9 | use Fcntl ':mode'; | |
10 | use File::stat; | |
11 | use JSON; | |
12 | ||
13 | use PVE::Tools qw(extract_param run_command file_get_contents file_read_firstline dir_glob_regex dir_glob_foreach trim); | |
14 | ||
15 | my $SMARTCTL = "/usr/sbin/smartctl"; | |
16 | my $ZPOOL = "/sbin/zpool"; | |
17 | my $SGDISK = "/sbin/sgdisk"; | |
18 | my $PVS = "/sbin/pvs"; | |
19 | my $LVS = "/sbin/lvs"; | |
20 | my $LSBLK = "/bin/lsblk"; | |
21 | ||
22 | sub verify_blockdev_path { | |
23 | my ($rel_path) = @_; | |
24 | ||
25 | die "missing path" if !$rel_path; | |
26 | my $path = abs_path($rel_path); | |
27 | die "failed to get absolute path to $rel_path\n" if !$path; | |
28 | ||
29 | die "got unusual device path '$path'\n" if $path !~ m|^/dev/(.*)$|; | |
30 | ||
31 | $path = "/dev/$1"; # untaint | |
32 | ||
33 | assert_blockdev($path); | |
34 | ||
35 | return $path; | |
36 | } | |
37 | ||
38 | sub assert_blockdev { | |
39 | my ($dev, $noerr) = @_; | |
40 | ||
41 | if ($dev !~ m|^/dev/| || !(-b $dev)) { | |
42 | return undef if $noerr; | |
43 | die "not a valid block device\n"; | |
44 | } | |
45 | ||
46 | return 1; | |
47 | } | |
48 | ||
49 | sub init_disk { | |
50 | my ($disk, $uuid) = @_; | |
51 | ||
52 | assert_blockdev($disk); | |
53 | ||
54 | # we should already have checked if it is in use in the api call | |
55 | # but we check again for safety | |
56 | die "disk $disk is already in use\n" if disk_is_used($disk); | |
57 | ||
58 | my $id = $uuid || 'R'; | |
59 | run_command([$SGDISK, $disk, '-U', $id]); | |
60 | return 1; | |
61 | } | |
62 | ||
63 | sub disk_is_used { | |
64 | my ($disk) = @_; | |
65 | ||
66 | my $dev = $disk; | |
67 | $dev =~ s|^/dev/||; | |
68 | ||
69 | my $disklist = get_disks($dev, 1); | |
70 | ||
71 | die "'$disk' is not a valid local disk\n" if !defined($disklist->{$dev}); | |
72 | return 1 if $disklist->{$dev}->{used}; | |
73 | ||
74 | return 0; | |
75 | } | |
76 | ||
77 | sub get_smart_data { | |
78 | my ($disk, $healthonly) = @_; | |
79 | ||
80 | assert_blockdev($disk); | |
81 | my $smartdata = {}; | |
82 | my $type; | |
83 | ||
84 | my $returncode = 0; | |
85 | ||
86 | if ($disk =~ m!^/dev/(nvme\d+n\d+)$!) { | |
87 | my $info = get_sysdir_info("/sys/block/$1"); | |
88 | $disk = "/dev/".($info->{device} | |
89 | or die "failed to get nvme controller device for $disk\n"); | |
90 | } | |
91 | ||
92 | my $cmd = [$SMARTCTL, '-H']; | |
93 | push @$cmd, '-A', '-f', 'brief' if !$healthonly; | |
94 | push @$cmd, $disk; | |
95 | ||
96 | eval { | |
97 | $returncode = run_command($cmd, noerr => 1, outfunc => sub{ | |
98 | my ($line) = @_; | |
99 | ||
100 | # ATA SMART attributes, e.g.: | |
101 | # ID# ATTRIBUTE_NAME FLAGS VALUE WORST THRESH FAIL RAW_VALUE | |
102 | # 1 Raw_Read_Error_Rate POSR-K 100 100 000 - 0 | |
103 | # | |
104 | # SAS and NVME disks, e.g.: | |
105 | # Data Units Written: 5,584,952 [2.85 TB] | |
106 | # Accumulated start-stop cycles: 34 | |
107 | ||
108 | if (defined($type) && $type eq 'ata' && $line =~ m/^([ \d]{2}\d)\s+(\S+)\s+(\S{6})\s+(\d+)\s+(\d+)\s+(\S+)\s+(\S+)\s+(.*)$/) { | |
109 | my $entry = {}; | |
110 | ||
111 | $entry->{name} = $2 if defined $2; | |
112 | $entry->{flags} = $3 if defined $3; | |
113 | # the +0 makes a number out of the strings | |
114 | $entry->{value} = $4+0 if defined $4; | |
115 | $entry->{worst} = $5+0 if defined $5; | |
116 | # some disks report the default threshold as --- instead of 000 | |
117 | if (defined($6) && $6 eq '---') { | |
118 | $entry->{threshold} = 0; | |
119 | } else { | |
120 | $entry->{threshold} = $6+0 if defined $6; | |
121 | } | |
122 | $entry->{fail} = $7 if defined $7; | |
123 | $entry->{raw} = $8 if defined $8; | |
124 | $entry->{id} = $1 if defined $1; | |
125 | push @{$smartdata->{attributes}}, $entry; | |
126 | } elsif ($line =~ m/(?:Health Status|self\-assessment test result): (.*)$/ ) { | |
127 | $smartdata->{health} = $1; | |
128 | } elsif ($line =~ m/Vendor Specific SMART Attributes with Thresholds:/) { | |
129 | $type = 'ata'; | |
130 | delete $smartdata->{text}; | |
131 | } elsif ($line =~ m/=== START OF (READ )?SMART DATA SECTION ===/) { | |
132 | $type = 'text'; | |
133 | } elsif (defined($type) && $type eq 'text') { | |
134 | $smartdata->{text} = '' if !defined $smartdata->{text}; | |
135 | $smartdata->{text} .= "$line\n"; | |
136 | # extract wearout from nvme/sas text, allow for decimal values | |
137 | if ($line =~ m/Percentage Used(?: endurance indicator)?:\s*(\d+(?:\.\d+)?)\%/i) { | |
138 | $smartdata->{wearout} = 100 - $1; | |
139 | } | |
140 | } elsif ($line =~ m/SMART Disabled/) { | |
141 | $smartdata->{health} = "SMART Disabled"; | |
142 | } | |
143 | }); | |
144 | }; | |
145 | my $err = $@; | |
146 | ||
147 | # bit 0 and 1 mark an severe smartctl error | |
148 | # all others are for disk status, so ignore them | |
149 | # see smartctl(8) | |
150 | if ((defined($returncode) && ($returncode & 0b00000011)) || $err) { | |
151 | die "Error getting S.M.A.R.T. data: Exit code: $returncode\n"; | |
152 | } | |
153 | ||
154 | $smartdata->{type} = $type; | |
155 | ||
156 | return $smartdata; | |
157 | } | |
158 | ||
159 | sub get_lsblk_info() { | |
160 | my $cmd = [$LSBLK, '--json', '-o', 'path,parttype,fstype']; | |
161 | my $output = ""; | |
162 | my $res = {}; | |
163 | eval { | |
164 | run_command($cmd, outfunc => sub { | |
165 | my ($line) = @_; | |
166 | $output .= "$line\n"; | |
167 | }); | |
168 | }; | |
169 | warn "$@\n" if $@; | |
170 | return $res if $output eq ''; | |
171 | ||
172 | my $parsed = eval { decode_json($output) }; | |
173 | warn "$@\n" if $@; | |
174 | my $list = $parsed->{blockdevices} // []; | |
175 | ||
176 | $res = { map { | |
177 | $_->{path} => { | |
178 | parttype => $_->{parttype}, | |
179 | fstype => $_->{fstype} | |
180 | } | |
181 | } @{$list} }; | |
182 | ||
183 | return $res; | |
184 | } | |
185 | ||
186 | my $get_devices_by_partuuid = sub { | |
187 | my ($lsblk_info, $uuids, $res) = @_; | |
188 | ||
189 | $res = {} if !defined($res); | |
190 | ||
191 | foreach my $dev (sort keys %{$lsblk_info}) { | |
192 | my $uuid = $lsblk_info->{$dev}->{parttype}; | |
193 | next if !defined($uuid) || !defined($uuids->{$uuid}); | |
194 | $res->{$dev} = $uuids->{$uuid}; | |
195 | } | |
196 | ||
197 | return $res; | |
198 | }; | |
199 | ||
200 | sub get_zfs_devices { | |
201 | my ($lsblk_info) = @_; | |
202 | my $res = {}; | |
203 | ||
204 | return {} if ! -x $ZPOOL; | |
205 | ||
206 | # use zpool and parttype uuid, | |
207 | # because log and cache do not have | |
208 | # zfs type uuid | |
209 | eval { | |
210 | run_command([$ZPOOL, 'list', '-HPLv'], outfunc => sub { | |
211 | my ($line) = @_; | |
212 | ||
213 | if ($line =~ m|^\t([^\t]+)\t|) { | |
214 | $res->{$1} = 1; | |
215 | } | |
216 | }); | |
217 | }; | |
218 | ||
219 | # only warn here, | |
220 | # because maybe zfs tools are not installed | |
221 | warn "$@\n" if $@; | |
222 | ||
223 | my $uuids = { | |
224 | "6a898cc3-1dd2-11b2-99a6-080020736631" => 1, # apple | |
225 | "516e7cba-6ecf-11d6-8ff8-00022d09712b" => 1, # bsd | |
226 | }; | |
227 | ||
228 | ||
229 | $res = $get_devices_by_partuuid->($lsblk_info, $uuids, $res); | |
230 | ||
231 | return $res; | |
232 | } | |
233 | ||
234 | sub get_lvm_devices { | |
235 | my ($lsblk_info) = @_; | |
236 | my $res = {}; | |
237 | eval { | |
238 | run_command([$PVS, '--noheadings', '--readonly', '-o', 'pv_name'], outfunc => sub{ | |
239 | my ($line) = @_; | |
240 | $line = trim($line); | |
241 | if ($line =~ m|^/dev/|) { | |
242 | $res->{$line} = 1; | |
243 | } | |
244 | }); | |
245 | }; | |
246 | ||
247 | # if something goes wrong, we do not want | |
248 | # to give up, but indicate an error has occured | |
249 | warn "$@\n" if $@; | |
250 | ||
251 | my $uuids = { | |
252 | "e6d6d379-f507-44c2-a23c-238f2a3df928" => 1, | |
253 | }; | |
254 | ||
255 | $res = $get_devices_by_partuuid->($lsblk_info, $uuids, $res); | |
256 | ||
257 | return $res; | |
258 | } | |
259 | ||
260 | sub get_ceph_journals { | |
261 | my ($lsblk_info) = @_; | |
262 | my $res = {}; | |
263 | ||
264 | my $uuids = { | |
265 | '45b0969e-9b03-4f30-b4c6-b4b80ceff106' => 1, # journal | |
266 | '30cd0809-c2b2-499c-8879-2d6b78529876' => 2, # db | |
267 | '5ce17fce-4087-4169-b7ff-056cc58473f9' => 3, # wal | |
268 | 'cafecafe-9b03-4f30-b4c6-b4b80ceff106' => 4, # block | |
269 | }; | |
270 | ||
271 | $res = $get_devices_by_partuuid->($lsblk_info, $uuids, $res); | |
272 | ||
273 | return $res; | |
274 | } | |
275 | ||
276 | # reads the lv_tags and matches them with the devices | |
277 | sub get_ceph_volume_infos { | |
278 | my $result = {}; | |
279 | ||
280 | my $cmd = [ $LVS, '-S', 'lv_name=~^osd-', '-o', 'devices,lv_name,lv_tags', | |
281 | '--noheadings', '--readonly', '--separator', ';' ]; | |
282 | ||
283 | run_command($cmd, outfunc => sub { | |
284 | my $line = shift; | |
285 | $line =~ s/(?:^\s+)|(?:\s+$)//g; # trim whitespaces | |
286 | ||
287 | my $fields = [ split(';', $line) ]; | |
288 | ||
289 | # lvs syntax is /dev/sdX(Y) where Y is the start (which we do not need) | |
290 | my ($dev) = $fields->[0] =~ m|^(/dev/[a-z]+[^(]*)|; | |
291 | if ($fields->[1] =~ m|^osd-([^-]+)-|) { | |
292 | my $type = $1; | |
293 | # $result autovivification is wanted, to not creating empty hashes | |
294 | if (($type eq 'block' || $type eq 'data') && $fields->[2] =~ m/ceph.osd_id=([^,]+)/) { | |
295 | $result->{$dev}->{osdid} = $1; | |
296 | $result->{$dev}->{bluestore} = ($type eq 'block'); | |
297 | if ($fields->[2] =~ m/ceph\.encrypted=1/) { | |
298 | $result->{$dev}->{encrypted} = 1; | |
299 | } | |
300 | } else { | |
301 | # undef++ becomes '1' (see `perldoc perlop`: Auto-increment) | |
302 | $result->{$dev}->{$type}++; | |
303 | } | |
304 | } | |
305 | }); | |
306 | ||
307 | return $result; | |
308 | } | |
309 | ||
310 | sub get_udev_info { | |
311 | my ($dev) = @_; | |
312 | ||
313 | my $info = ""; | |
314 | my $data = {}; | |
315 | eval { | |
316 | run_command(['udevadm', 'info', '-p', $dev, '--query', 'all'], outfunc => sub { | |
317 | my ($line) = @_; | |
318 | $info .= "$line\n"; | |
319 | }); | |
320 | }; | |
321 | warn $@ if $@; | |
322 | return undef if !$info; | |
323 | ||
324 | return undef if $info !~ m/^E: DEVTYPE=disk$/m; | |
325 | return undef if $info =~ m/^E: ID_CDROM/m; | |
326 | ||
327 | # we use this, because some disks are not simply in /dev | |
328 | # e.g. /dev/cciss/c0d0 | |
329 | if ($info =~ m/^E: DEVNAME=(\S+)$/m) { | |
330 | $data->{devpath} = $1; | |
331 | } | |
332 | return if !defined($data->{devpath}); | |
333 | ||
334 | $data->{serial} = 'unknown'; | |
335 | if ($info =~ m/^E: ID_SERIAL_SHORT=(\S+)$/m) { | |
336 | $data->{serial} = $1; | |
337 | } | |
338 | ||
339 | $data->{gpt} = 0; | |
340 | if ($info =~ m/^E: ID_PART_TABLE_TYPE=gpt$/m) { | |
341 | $data->{gpt} = 1; | |
342 | } | |
343 | ||
344 | # detect SSD | |
345 | $data->{rpm} = -1; | |
346 | if ($info =~ m/^E: ID_ATA_ROTATION_RATE_RPM=(\d+)$/m) { | |
347 | $data->{rpm} = $1; | |
348 | } | |
349 | ||
350 | if ($info =~ m/^E: ID_BUS=usb$/m) { | |
351 | $data->{usb} = 1; | |
352 | } | |
353 | ||
354 | if ($info =~ m/^E: ID_MODEL=(.+)$/m) { | |
355 | $data->{model} = $1; | |
356 | } | |
357 | ||
358 | $data->{wwn} = 'unknown'; | |
359 | if ($info =~ m/^E: ID_WWN=(.*)$/m) { | |
360 | $data->{wwn} = $1; | |
361 | } | |
362 | ||
363 | if ($info =~ m/^E: DEVLINKS=(.+)$/m) { | |
364 | my @devlinks = grep(m#^/dev/disk/by-id/(ata|scsi|nvme(?!-eui))#, split (/ /, $1)); | |
365 | $data->{by_id_link} = $devlinks[0] if defined($devlinks[0]); | |
366 | } | |
367 | ||
368 | return $data; | |
369 | } | |
370 | ||
371 | sub get_sysdir_size { | |
372 | my ($sysdir) = @_; | |
373 | ||
374 | my $size = file_read_firstline("$sysdir/size"); | |
375 | return if !$size; | |
376 | ||
377 | # linux always considers sectors to be 512 bytes, | |
378 | # independently of real block size | |
379 | return $size * 512; | |
380 | } | |
381 | ||
382 | sub get_sysdir_info { | |
383 | my ($sysdir) = @_; | |
384 | ||
385 | return undef if ! -d "$sysdir/device"; | |
386 | ||
387 | my $data = {}; | |
388 | ||
389 | $data->{size} = get_sysdir_size($sysdir) or return; | |
390 | ||
391 | # dir/queue/rotational should be 1 for hdd, 0 for ssd | |
392 | $data->{rotational} = file_read_firstline("$sysdir/queue/rotational") // -1; | |
393 | ||
394 | $data->{vendor} = file_read_firstline("$sysdir/device/vendor") || 'unknown'; | |
395 | $data->{model} = file_read_firstline("$sysdir/device/model") || 'unknown'; | |
396 | ||
397 | if (defined(my $device = readlink("$sysdir/device"))) { | |
398 | # strip directory and untaint: | |
399 | ($data->{device}) = $device =~ m!([^/]+)$!; | |
400 | } | |
401 | ||
402 | return $data; | |
403 | } | |
404 | ||
405 | sub get_wear_leveling_info { | |
406 | my ($smartdata) = @_; | |
407 | my $attributes = $smartdata->{attributes}; | |
408 | ||
409 | if (defined($smartdata->{wearout})) { | |
410 | return $smartdata->{wearout}; | |
411 | } | |
412 | ||
413 | my $wearout; | |
414 | ||
415 | # Common register names that represent percentage values of potential | |
416 | # failure indicators used in drivedb.h of smartmontool's. Order matters, | |
417 | # as some drives may have multiple definitions | |
418 | my @wearoutregisters = ( | |
419 | "Media_Wearout_Indicator", | |
420 | "SSD_Life_Left", | |
421 | "Wear_Leveling_Count", | |
422 | "Perc_Write\/Erase_Ct_BC", | |
423 | "Perc_Rated_Life_Remain", | |
424 | "Remaining_Lifetime_Perc", | |
425 | "Percent_Lifetime_Remain", | |
426 | "Lifetime_Left", | |
427 | "PCT_Life_Remaining", | |
428 | "Lifetime_Remaining", | |
429 | "Percent_Life_Remaining", | |
430 | "Percent_Lifetime_Used", | |
431 | "Perc_Rated_Life_Used" | |
432 | ); | |
433 | ||
434 | # Search for S.M.A.R.T. attributes for known register | |
435 | foreach my $register (@wearoutregisters) { | |
436 | last if defined $wearout; | |
437 | foreach my $attr (@$attributes) { | |
438 | next if $attr->{name} !~ m/$register/; | |
439 | $wearout = $attr->{value}; | |
440 | last; | |
441 | } | |
442 | } | |
443 | ||
444 | return $wearout; | |
445 | } | |
446 | ||
447 | sub dir_is_empty { | |
448 | my ($dir) = @_; | |
449 | ||
450 | my $dh = IO::Dir->new ($dir); | |
451 | return 1 if !$dh; | |
452 | ||
453 | while (defined(my $tmp = $dh->read)) { | |
454 | next if $tmp eq '.' || $tmp eq '..'; | |
455 | $dh->close; | |
456 | return 0; | |
457 | } | |
458 | $dh->close; | |
459 | return 1; | |
460 | } | |
461 | ||
462 | sub is_iscsi { | |
463 | my ($sysdir) = @_; | |
464 | ||
465 | if (-l $sysdir && readlink($sysdir) =~ m|host[^/]*/session[^/]*|) { | |
466 | return 1; | |
467 | } | |
468 | ||
469 | return 0; | |
470 | } | |
471 | ||
472 | my sub is_ssdlike { | |
473 | my ($type) = @_; | |
474 | return $type eq 'ssd' || $type eq 'nvme'; | |
475 | } | |
476 | ||
477 | sub get_disks { | |
478 | my ($disks, $nosmart) = @_; | |
479 | my $disklist = {}; | |
480 | ||
481 | my $mounted = {}; | |
482 | ||
483 | my $mounts = PVE::ProcFSTools::parse_proc_mounts(); | |
484 | ||
485 | foreach my $mount (@$mounts) { | |
486 | next if $mount->[0] !~ m|^/dev/|; | |
487 | $mounted->{abs_path($mount->[0])} = $mount->[1]; | |
488 | }; | |
489 | ||
490 | my $lsblk_info = get_lsblk_info(); | |
491 | ||
492 | my $journalhash = get_ceph_journals($lsblk_info); | |
493 | my $ceph_volume_infos = get_ceph_volume_infos(); | |
494 | ||
495 | my $zfshash = get_zfs_devices($lsblk_info); | |
496 | ||
497 | my $lvmhash = get_lvm_devices($lsblk_info); | |
498 | ||
499 | my $disk_regex = ".*"; | |
500 | if (defined($disks)) { | |
501 | if (!ref($disks)) { | |
502 | $disks = [ $disks ]; | |
503 | } elsif (ref($disks) ne 'ARRAY') { | |
504 | die "disks is not a string or array reference\n"; | |
505 | } | |
506 | # we get cciss/c0d0 but need cciss!c0d0 | |
507 | $_ =~ s|cciss/|cciss!| for @$disks; | |
508 | ||
509 | $disk_regex = "(?:" . join('|', @$disks) . ")"; | |
510 | } | |
511 | ||
512 | dir_glob_foreach('/sys/block', $disk_regex, sub { | |
513 | my ($dev) = @_; | |
514 | # whitelisting following devices | |
515 | # hdX: ide block device | |
516 | # sdX: sd block device | |
517 | # vdX: virtual block device | |
518 | # xvdX: xen virtual block device | |
519 | # nvmeXnY: nvme devices | |
520 | # cciss!cXnY: cciss devices | |
521 | return if $dev !~ m/^(h|s|x?v)d[a-z]+$/ && | |
522 | $dev !~ m/^nvme\d+n\d+$/ && | |
523 | $dev !~ m/^cciss\!c\d+d\d+$/; | |
524 | ||
525 | my $data = get_udev_info("/sys/block/$dev"); | |
526 | return if !defined($data); | |
527 | my $devpath = $data->{devpath}; | |
528 | ||
529 | my $sysdir = "/sys/block/$dev"; | |
530 | ||
531 | # we do not want iscsi devices | |
532 | return if is_iscsi($sysdir); | |
533 | ||
534 | my $sysdata = get_sysdir_info($sysdir); | |
535 | return if !defined($sysdata); | |
536 | ||
537 | my $type = 'unknown'; | |
538 | ||
539 | if ($sysdata->{rotational} == 0) { | |
540 | $type = 'ssd'; | |
541 | $type = 'nvme' if $dev =~ m/^nvme\d+n\d+$/; | |
542 | $data->{rpm} = 0; | |
543 | } elsif ($sysdata->{rotational} == 1) { | |
544 | if ($data->{rpm} != -1) { | |
545 | $type = 'hdd'; | |
546 | } elsif ($data->{usb}) { | |
547 | $type = 'usb'; | |
548 | $data->{rpm} = 0; | |
549 | } | |
550 | } | |
551 | ||
552 | my $health = 'UNKNOWN'; | |
553 | my $wearout = 'N/A'; | |
554 | ||
555 | if (!$nosmart) { | |
556 | eval { | |
557 | my $smartdata = get_smart_data($devpath, !is_ssdlike($type)); | |
558 | $health = $smartdata->{health} if $smartdata->{health}; | |
559 | ||
560 | if (is_ssdlike($type)) { | |
561 | # if we have an ssd we try to get the wearout indicator | |
562 | my $wearval = get_wear_leveling_info($smartdata); | |
563 | $wearout = $wearval if defined($wearval); | |
564 | } | |
565 | }; | |
566 | } | |
567 | ||
568 | # we replaced cciss/ with cciss! above | |
569 | # but in the result we need cciss/ again | |
570 | # because the caller might want to check the | |
571 | # result again with the original parameter | |
572 | if ($dev =~ m|^cciss!|) { | |
573 | $dev =~ s|^cciss!|cciss/|; | |
574 | } | |
575 | ||
576 | $disklist->{$dev} = { | |
577 | vendor => $sysdata->{vendor}, | |
578 | model => $data->{model} || $sysdata->{model}, | |
579 | size => $sysdata->{size}, | |
580 | serial => $data->{serial}, | |
581 | gpt => $data->{gpt}, | |
582 | rpm => $data->{rpm}, | |
583 | type => $type, | |
584 | wwn => $data->{wwn}, | |
585 | health => $health, | |
586 | devpath => $devpath, | |
587 | wearout => $wearout, | |
588 | }; | |
589 | ||
590 | my $by_id_link = $data->{by_id_link}; | |
591 | $disklist->{$dev}->{by_id_link} = $by_id_link if defined($by_id_link); | |
592 | ||
593 | my $osdid = -1; | |
594 | my $bluestore = 0; | |
595 | my $osdencrypted = 0; | |
596 | ||
597 | my $journal_count = 0; | |
598 | my $db_count = 0; | |
599 | my $wal_count = 0; | |
600 | ||
601 | my $partpath = $devpath; | |
602 | ||
603 | # remove part after last / to | |
604 | # get the base path for the partitions | |
605 | # e.g. from /dev/cciss/c0d0 get /dev/cciss | |
606 | $partpath =~ s/\/[^\/]+$//; | |
607 | ||
608 | my $determine_usage = sub { | |
609 | my ($devpath, $sysdir, $is_partition) = @_; | |
610 | ||
611 | return 'LVM' if $lvmhash->{$devpath}; | |
612 | return 'ZFS' if $zfshash->{$devpath}; | |
613 | ||
614 | my $info = $lsblk_info->{$devpath} // {}; | |
615 | ||
616 | my $parttype = $info->{parttype}; | |
617 | if (defined($parttype)) { | |
618 | return 'BIOS boot' | |
619 | if $parttype eq '21686148-6449-6e6f-744e-656564454649'; | |
620 | return 'EFI' | |
621 | if $parttype eq 'c12a7328-f81f-11d2-ba4b-00a0c93ec93b'; | |
622 | return 'ZFS reserved' | |
623 | if $parttype eq '6a945a3b-1dd2-11b2-99a6-080020736631'; | |
624 | } | |
625 | ||
626 | my $fstype = $info->{fstype}; | |
627 | if (defined($fstype)) { | |
628 | return "${fstype} (mounted)" if $mounted->{$devpath}; | |
629 | return "${fstype}"; | |
630 | } | |
631 | return 'mounted' if $mounted->{$devpath}; | |
632 | ||
633 | return if !$is_partition; | |
634 | ||
635 | # for devices, this check is done explicitly later | |
636 | return 'Device Mapper' if !dir_is_empty("$sysdir/holders"); | |
637 | ||
638 | return 'partition'; | |
639 | }; | |
640 | ||
641 | my $collect_ceph_info = sub { | |
642 | my ($devpath) = @_; | |
643 | ||
644 | my $ceph_volume = $ceph_volume_infos->{$devpath} or return; | |
645 | $journal_count += $ceph_volume->{journal} // 0; | |
646 | $db_count += $ceph_volume->{db} // 0; | |
647 | $wal_count += $ceph_volume->{wal} // 0; | |
648 | if (defined($ceph_volume->{osdid})) { | |
649 | $osdid = $ceph_volume->{osdid}; | |
650 | $bluestore = 1 if $ceph_volume->{bluestore}; | |
651 | $osdencrypted = 1 if $ceph_volume->{encrypted}; | |
652 | } | |
653 | ||
654 | my $result = { %{$ceph_volume} }; | |
655 | $result->{journals} = delete $result->{journal} | |
656 | if $result->{journal}; | |
657 | return $result; | |
658 | }; | |
659 | ||
660 | my $partitions = {}; | |
661 | ||
662 | dir_glob_foreach("$sysdir", "$dev.+", sub { | |
663 | my ($part) = @_; | |
664 | ||
665 | $partitions->{$part} = $collect_ceph_info->("$partpath/$part"); | |
666 | my $lvm_based_osd = defined($partitions->{$part}); | |
667 | ||
668 | $partitions->{$part}->{devpath} = "$partpath/$part"; | |
669 | $partitions->{$part}->{gpt} = $data->{gpt}; | |
670 | $partitions->{$part}->{size} = | |
671 | get_sysdir_size("$sysdir/$part") // 0; | |
672 | $partitions->{$part}->{used} = | |
673 | $determine_usage->("$partpath/$part", "$sysdir/$part", 1); | |
674 | $partitions->{$part}->{osdid} //= -1; | |
675 | ||
676 | # Avoid counting twice (e.g. partition on which the LVM for the | |
677 | # DB OSD resides is present in the $journalhash) | |
678 | return if $lvm_based_osd; | |
679 | ||
680 | # Legacy handling for non-LVM based OSDs | |
681 | ||
682 | if (my $mp = $mounted->{"$partpath/$part"}) { | |
683 | if ($mp =~ m|^/var/lib/ceph/osd/ceph-(\d+)$|) { | |
684 | $osdid = $1; | |
685 | $partitions->{$part}->{osdid} = $osdid; | |
686 | } | |
687 | } | |
688 | ||
689 | if (my $journal_part = $journalhash->{"$partpath/$part"}) { | |
690 | $journal_count++ if $journal_part == 1; | |
691 | $db_count++ if $journal_part == 2; | |
692 | $wal_count++ if $journal_part == 3; | |
693 | $bluestore = 1 if $journal_part == 4; | |
694 | ||
695 | $partitions->{$part}->{journals} = 1 if $journal_part == 1; | |
696 | $partitions->{$part}->{db} = 1 if $journal_part == 2; | |
697 | $partitions->{$part}->{wal} = 1 if $journal_part == 3; | |
698 | $partitions->{$part}->{bluestore} = 1 if $journal_part == 4; | |
699 | } | |
700 | }); | |
701 | ||
702 | my $used = $determine_usage->($devpath, $sysdir, 0); | |
703 | foreach my $part (sort keys %{$partitions}) { | |
704 | next if $partitions->{$part}->{used} eq 'partition'; | |
705 | $used //= $partitions->{$part}->{used}; | |
706 | } | |
707 | $used //= 'partitions' if scalar(keys %{$partitions}); | |
708 | # multipath, software raid, etc. | |
709 | # this check comes in last, to show more specific info | |
710 | # if we have it | |
711 | $used //= 'Device Mapper' if !dir_is_empty("$sysdir/holders"); | |
712 | ||
713 | $disklist->{$dev}->{used} = $used if $used; | |
714 | ||
715 | $collect_ceph_info->($devpath); | |
716 | ||
717 | $disklist->{$dev}->{osdid} = $osdid; | |
718 | $disklist->{$dev}->{journals} = $journal_count if $journal_count; | |
719 | $disklist->{$dev}->{bluestore} = $bluestore if $osdid != -1; | |
720 | $disklist->{$dev}->{osdencrypted} = $osdencrypted if $osdid != -1; | |
721 | $disklist->{$dev}->{db} = $db_count if $db_count; | |
722 | $disklist->{$dev}->{wal} = $wal_count if $wal_count; | |
723 | }); | |
724 | ||
725 | return $disklist; | |
726 | ||
727 | } | |
728 | ||
729 | sub get_partnum { | |
730 | my ($part_path) = @_; | |
731 | ||
732 | my $st = stat($part_path); | |
733 | ||
734 | next if !$st->mode || !S_ISBLK($st->mode) || !$st->rdev; | |
735 | my $major = PVE::Tools::dev_t_major($st->rdev); | |
736 | my $minor = PVE::Tools::dev_t_minor($st->rdev); | |
737 | my $partnum_path = "/sys/dev/block/$major:$minor/"; | |
738 | ||
739 | my $partnum; | |
740 | ||
741 | $partnum = file_read_firstline("${partnum_path}partition"); | |
742 | ||
743 | die "Partition does not exist\n" if !defined($partnum); | |
744 | ||
745 | #untaint and ensure it is a int | |
746 | if ($partnum =~ m/(\d+)/) { | |
747 | $partnum = $1; | |
748 | die "Partition number $partnum is invalid\n" if $partnum > 128; | |
749 | } else { | |
750 | die "Failed to get partition number\n"; | |
751 | } | |
752 | ||
753 | return $partnum; | |
754 | } | |
755 | ||
756 | sub get_blockdev { | |
757 | my ($part_path) = @_; | |
758 | ||
759 | my ($dev, $block_dev); | |
760 | if ($part_path =~ m|^/dev/(.*)$|) { | |
761 | $dev = $1; | |
762 | my $link = readlink "/sys/class/block/$dev"; | |
763 | $block_dev = $1 if $link =~ m|([^/]*)/$dev$|; | |
764 | } | |
765 | ||
766 | die "Can't parse parent device\n" if !defined($block_dev); | |
767 | die "No valid block device\n" if index($dev, $block_dev) == -1; | |
768 | ||
769 | $block_dev = "/dev/$block_dev"; | |
770 | die "Block device does not exsists\n" if !(-b $block_dev); | |
771 | ||
772 | return $block_dev; | |
773 | } | |
774 | ||
775 | sub locked_disk_action { | |
776 | my ($sub) = @_; | |
777 | my $res = PVE::Tools::lock_file('/run/lock/pve-diskmanage.lck', undef, $sub); | |
778 | die $@ if $@; | |
779 | return $res; | |
780 | } | |
781 | ||
782 | sub assert_disk_unused { | |
783 | my ($dev) = @_; | |
784 | ||
785 | die "device '$dev' is already in use\n" if disk_is_used($dev); | |
786 | ||
787 | return undef; | |
788 | } | |
789 | ||
790 | sub append_partition { | |
791 | my ($dev, $size) = @_; | |
792 | ||
793 | my $devname = $dev; | |
794 | $devname =~ s|^/dev/||; | |
795 | ||
796 | my $newpartid = 1; | |
797 | dir_glob_foreach("/sys/block/$devname", qr/\Q$devname\E.*?(\d+)/, sub { | |
798 | my ($part, $partid) = @_; | |
799 | ||
800 | if ($partid >= $newpartid) { | |
801 | $newpartid = $partid + 1; | |
802 | } | |
803 | }); | |
804 | ||
805 | $size = PVE::Tools::convert_size($size, 'b' => 'mb'); | |
806 | ||
807 | run_command([ $SGDISK, '-n', "$newpartid:0:+${size}M", $dev ], | |
808 | errmsg => "error creating partition '$newpartid' on '$dev'"); | |
809 | ||
810 | my $partition; | |
811 | ||
812 | # loop again to detect the real partiton device which does not always follow | |
813 | # a strict $devname$partition scheme like /dev/nvme0n1 -> /dev/nvme0n1p1 | |
814 | dir_glob_foreach("/sys/block/$devname", qr/\Q$devname\E.*$newpartid/, sub { | |
815 | my ($part) = @_; | |
816 | ||
817 | $partition = "/dev/$part"; | |
818 | }); | |
819 | ||
820 | return $partition; | |
821 | } | |
822 | ||
823 | 1; |