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