]>
Commit | Line | Data |
---|---|---|
cbba9b5b DC |
1 | package PVE::Diskmanage; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | use PVE::ProcFSTools; | |
6 | use Data::Dumper; | |
7 | use Cwd qw(abs_path); | |
8 | ||
9 | use PVE::Tools qw(extract_param run_command file_get_contents file_read_firstline dir_glob_regex dir_glob_foreach trim); | |
10 | ||
11 | my $SMARTCTL = "/usr/sbin/smartctl"; | |
12 | my $ZPOOL = "/sbin/zpool"; | |
13 | my $SGDISK = "/sbin/sgdisk"; | |
14 | my $PVS = "/sbin/pvs"; | |
15 | my $UDEVADM = "/bin/udevadm"; | |
16 | ||
17 | sub verify_blockdev_path { | |
18 | my ($rel_path) = @_; | |
19 | ||
20 | die "missing path" if !$rel_path; | |
21 | my $path = abs_path($rel_path); | |
22 | die "failed to get absolute path to $rel_path\n" if !$path; | |
23 | ||
24 | die "got unusual device path '$path'\n" if $path !~ m|^/dev/(.*)$|; | |
25 | ||
26 | $path = "/dev/$1"; # untaint | |
27 | ||
28 | assert_blockdev($path); | |
29 | ||
30 | return $path; | |
31 | } | |
32 | ||
33 | sub assert_blockdev { | |
34 | my ($dev, $noerr) = @_; | |
35 | ||
36 | if ($dev !~ m|^/dev/| || !(-b $dev)) { | |
37 | return undef if $noerr; | |
38 | die "not a valid block device\n"; | |
39 | } | |
40 | ||
41 | return 1; | |
42 | } | |
43 | ||
44 | sub init_disk { | |
45 | my ($disk, $uuid) = @_; | |
46 | ||
47 | assert_blockdev($disk); | |
48 | ||
49 | # we should already have checked if it is in use in the api call | |
50 | # but we check again for safety | |
51 | die "disk $disk is already in use\n" if disk_is_used($disk); | |
52 | ||
53 | my $id = $uuid || 'R'; | |
54 | run_command([$SGDISK, $disk, '-U', $id]); | |
55 | return 1; | |
56 | } | |
57 | ||
58 | sub disk_is_used { | |
59 | my ($disk) = @_; | |
60 | ||
61 | my $dev = $disk; | |
62 | $dev =~ s|^/dev/||; | |
63 | ||
7a98a62d | 64 | my $disklist = get_disks($dev, 1); |
cbba9b5b DC |
65 | |
66 | die "'$disk' is not a valid local disk\n" if !defined($disklist->{$dev}); | |
67 | return 1 if $disklist->{$dev}->{used}; | |
68 | ||
69 | return 0; | |
70 | } | |
71 | ||
72 | sub get_smart_data { | |
dd902da7 | 73 | my ($disk, $healthonly) = @_; |
cbba9b5b DC |
74 | |
75 | assert_blockdev($disk); | |
76 | my $smartdata = {}; | |
dc1311cb | 77 | my $type; |
cbba9b5b | 78 | |
9018a4e6 | 79 | my $returncode = 0; |
c9bd3d22 FG |
80 | |
81 | $disk =~ s/n\d+$// | |
82 | if $disk =~ m!^/dev/nvme\d+n\d+$!; | |
83 | ||
dd902da7 DC |
84 | my $cmd = [$SMARTCTL, '-H']; |
85 | push @$cmd, '-A', '-f', 'brief' if !$healthonly; | |
86 | push @$cmd, $disk; | |
87 | ||
cbba9b5b | 88 | eval { |
dd902da7 | 89 | $returncode = run_command($cmd, noerr => 1, outfunc => sub{ |
cbba9b5b DC |
90 | my ($line) = @_; |
91 | ||
1c999553 FG |
92 | # ATA SMART attributes, e.g.: |
93 | # ID# ATTRIBUTE_NAME FLAGS VALUE WORST THRESH FAIL RAW_VALUE | |
94 | # 1 Raw_Read_Error_Rate POSR-K 100 100 000 - 0 | |
dc1311cb FG |
95 | # |
96 | # SAS and NVME disks, e.g.: | |
97 | # Data Units Written: 5,584,952 [2.85 TB] | |
98 | # Accumulated start-stop cycles: 34 | |
99 | ||
bd54091c | 100 | 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+(.*)$/) { |
cbba9b5b | 101 | my $entry = {}; |
bd54091c DC |
102 | |
103 | ||
1c999553 FG |
104 | $entry->{name} = $2 if defined $2; |
105 | $entry->{flags} = $3 if defined $3; | |
cbba9b5b | 106 | # the +0 makes a number out of the strings |
1c999553 FG |
107 | $entry->{value} = $4+0 if defined $4; |
108 | $entry->{worst} = $5+0 if defined $5; | |
bd54091c DC |
109 | # some disks report the default threshold as --- instead of 000 |
110 | if (defined($6) && $6 eq '---') { | |
111 | $entry->{threshold} = 0; | |
112 | } else { | |
113 | $entry->{threshold} = $6+0 if defined $6; | |
114 | } | |
1c999553 FG |
115 | $entry->{fail} = $7 if defined $7; |
116 | $entry->{raw} = $8 if defined $8; | |
117 | $entry->{id} = $1 if defined $1; | |
cbba9b5b | 118 | push @{$smartdata->{attributes}}, $entry; |
5db2d529 | 119 | } elsif ($line =~ m/(?:Health Status|self\-assessment test result): (.*)$/ ) { |
cbba9b5b DC |
120 | $smartdata->{health} = $1; |
121 | } elsif ($line =~ m/Vendor Specific SMART Attributes with Thresholds:/) { | |
dc1311cb FG |
122 | $type = 'ata'; |
123 | delete $smartdata->{text}; | |
124 | } elsif ($line =~ m/=== START OF (READ )?SMART DATA SECTION ===/) { | |
125 | $type = 'text'; | |
126 | } elsif (defined($type) && $type eq 'text') { | |
127 | $smartdata->{text} = '' if !defined $smartdata->{text}; | |
128 | $smartdata->{text} .= "$line\n"; | |
dd902da7 DC |
129 | } elsif ($line =~ m/SMART Disabled/) { |
130 | $smartdata->{health} = "SMART Disabled"; | |
cbba9b5b DC |
131 | } |
132 | }); | |
133 | }; | |
9018a4e6 DC |
134 | my $err = $@; |
135 | ||
136 | # bit 0 and 1 mark an severe smartctl error | |
137 | # all others are for disk status, so ignore them | |
138 | # see smartctl(8) | |
139 | if ((defined($returncode) && ($returncode & 0b00000011)) || $err) { | |
140 | die "Error getting S.M.A.R.T. data: Exit code: $returncode\n"; | |
141 | } | |
dc1311cb FG |
142 | |
143 | $smartdata->{type} = $type; | |
144 | ||
cbba9b5b DC |
145 | return $smartdata; |
146 | } | |
147 | ||
cbba9b5b DC |
148 | sub get_zfs_devices { |
149 | my $list = {}; | |
150 | ||
151 | # use zpool and parttype uuid, | |
152 | # because log and cache do not have | |
153 | # zfs type uuid | |
154 | eval { | |
155 | run_command([$ZPOOL, 'list', '-HPLv'], outfunc => sub { | |
156 | my ($line) = @_; | |
157 | ||
158 | if ($line =~ m|^\t([^\t]+)\t|) { | |
159 | $list->{$1} = 1; | |
160 | } | |
161 | }); | |
162 | }; | |
163 | ||
164 | # only warn here, | |
165 | # because maybe zfs tools are not installed | |
166 | warn "$@\n" if $@; | |
167 | ||
168 | my $applezfsuuid = "6a898cc3-1dd2-11b2-99a6-080020736631"; | |
169 | my $bsdzfsuuid = "516e7cba-6ecf-11d6-8ff8-00022d09712b"; | |
170 | ||
171 | dir_glob_foreach('/dev/disk/by-parttypeuuid', "($applezfsuuid|$bsdzfsuuid)\..+", sub { | |
172 | my ($entry) = @_; | |
173 | my $real_dev = abs_path("/dev/disk/by-parttypeuuid/$entry"); | |
174 | $list->{$real_dev} = 1; | |
175 | }); | |
176 | ||
177 | return $list; | |
178 | } | |
179 | ||
180 | sub get_lvm_devices { | |
181 | my $list = {}; | |
182 | eval { | |
183 | run_command([$PVS, '--noheadings', '--readonly', '-o', 'pv_name'], outfunc => sub{ | |
184 | my ($line) = @_; | |
185 | $line = trim($line); | |
186 | if ($line =~ m|^/dev/|) { | |
187 | $list->{$line} = 1; | |
188 | } | |
189 | }); | |
190 | }; | |
191 | ||
192 | # if something goes wrong, we do not want | |
193 | # to give up, but indicate an error has occured | |
194 | warn "$@\n" if $@; | |
195 | ||
196 | my $lvmuuid = "e6d6d379-f507-44c2-a23c-238f2a3df928"; | |
197 | ||
198 | dir_glob_foreach('/dev/disk/by-parttypeuuid', "$lvmuuid\..+", sub { | |
199 | my ($entry) = @_; | |
200 | my $real_dev = abs_path("/dev/disk/by-parttypeuuid/$entry"); | |
201 | $list->{$real_dev} = 1; | |
202 | }); | |
203 | ||
204 | return $list; | |
205 | } | |
206 | ||
207 | sub get_ceph_journals { | |
208 | my $journalhash = {}; | |
209 | ||
210 | my $journal_uuid = '45b0969e-9b03-4f30-b4c6-b4b80ceff106'; | |
211 | ||
212 | dir_glob_foreach('/dev/disk/by-parttypeuuid', "$journal_uuid\..+", sub { | |
213 | my ($entry) = @_; | |
214 | my $real_dev = abs_path("/dev/disk/by-parttypeuuid/$entry"); | |
215 | $journalhash->{$real_dev} = 1; | |
216 | }); | |
217 | ||
218 | return $journalhash; | |
219 | } | |
220 | ||
221 | sub get_udev_info { | |
222 | my ($dev) = @_; | |
223 | ||
224 | my $info = ""; | |
225 | my $data = {}; | |
226 | eval { | |
532e89e7 | 227 | run_command([$UDEVADM, 'info', '-p', $dev, '--query', 'all'], outfunc => sub { |
cbba9b5b DC |
228 | my ($line) = @_; |
229 | $info .= "$line\n"; | |
230 | }); | |
231 | }; | |
232 | warn $@ if $@; | |
233 | return undef if !$info; | |
234 | ||
235 | return undef if $info !~ m/^E: DEVTYPE=disk$/m; | |
236 | return undef if $info =~ m/^E: ID_CDROM/m; | |
237 | ||
238 | # we use this, because some disks are not simply in /dev | |
239 | # e.g. /dev/cciss/c0d0 | |
240 | if ($info =~ m/^E: DEVNAME=(\S+)$/m) { | |
241 | $data->{devpath} = $1; | |
242 | } | |
243 | return if !defined($data->{devpath}); | |
244 | ||
245 | $data->{serial} = 'unknown'; | |
246 | if ($info =~ m/^E: ID_SERIAL_SHORT=(\S+)$/m) { | |
247 | $data->{serial} = $1; | |
248 | } | |
249 | ||
250 | $data->{gpt} = 0; | |
251 | if ($info =~ m/^E: ID_PART_TABLE_TYPE=gpt$/m) { | |
252 | $data->{gpt} = 1; | |
253 | } | |
254 | ||
255 | # detect SSD | |
256 | $data->{rpm} = -1; | |
257 | if ($info =~ m/^E: ID_ATA_ROTATION_RATE_RPM=(\d+)$/m) { | |
258 | $data->{rpm} = $1; | |
259 | } | |
260 | ||
261 | if ($info =~ m/^E: ID_BUS=usb$/m) { | |
262 | $data->{usb} = 1; | |
263 | } | |
264 | ||
865bdbd9 DC |
265 | if ($info =~ m/^E: ID_MODEL=(.+)$/m) { |
266 | $data->{model} = $1; | |
267 | } | |
268 | ||
cbba9b5b DC |
269 | $data->{wwn} = 'unknown'; |
270 | if ($info =~ m/^E: ID_WWN=(.*)$/m) { | |
271 | $data->{wwn} = $1; | |
272 | } | |
273 | ||
274 | return $data; | |
275 | } | |
276 | ||
277 | sub get_sysdir_info { | |
278 | my ($sysdir) = @_; | |
279 | ||
461a9fd8 DC |
280 | return undef if ! -d "$sysdir/device"; |
281 | ||
cbba9b5b DC |
282 | my $data = {}; |
283 | ||
284 | my $size = file_read_firstline("$sysdir/size"); | |
285 | return undef if !$size; | |
286 | ||
287 | # linux always considers sectors to be 512 bytes, | |
288 | # independently of real block size | |
289 | $data->{size} = $size * 512; | |
290 | ||
291 | # dir/queue/rotational should be 1 for hdd, 0 for ssd | |
571b6f26 | 292 | $data->{rotational} = file_read_firstline("$sysdir/queue/rotational") // -1; |
cbba9b5b DC |
293 | |
294 | $data->{vendor} = file_read_firstline("$sysdir/device/vendor") || 'unknown'; | |
295 | $data->{model} = file_read_firstline("$sysdir/device/model") || 'unknown'; | |
296 | ||
297 | return $data; | |
298 | } | |
299 | ||
6965a670 DC |
300 | sub get_wear_leveling_info { |
301 | my ($attributes, $model) = @_; | |
302 | ||
303 | my $wearout; | |
304 | ||
305 | my $vendormap = { | |
306 | 'kingston' => 231, | |
307 | 'samsung' => 177, | |
308 | 'intel' => 233, | |
309 | 'sandisk' => 233, | |
310 | 'default' => 233, | |
311 | }; | |
312 | ||
313 | # find target attr id | |
314 | ||
315 | my $attrid; | |
316 | ||
d57fb43d | 317 | foreach my $vendor (keys %$vendormap) { |
6965a670 DC |
318 | if ($model =~ m/$vendor/i) { |
319 | $attrid = $vendormap->{$vendor}; | |
320 | # found the attribute | |
321 | last; | |
322 | } | |
323 | } | |
324 | ||
325 | if (!$attrid) { | |
326 | $attrid = $vendormap->{default}; | |
327 | } | |
328 | ||
329 | foreach my $attr (@$attributes) { | |
330 | next if $attr->{id} != $attrid; | |
331 | $wearout = $attr->{value}; | |
332 | last; | |
333 | } | |
334 | ||
335 | return $wearout; | |
336 | } | |
337 | ||
10a48db5 DC |
338 | sub dir_is_empty { |
339 | my ($dir) = @_; | |
340 | ||
341 | my $dh = IO::Dir->new ($dir); | |
342 | return 1 if !$dh; | |
343 | ||
344 | while (defined(my $tmp = $dh->read)) { | |
345 | next if $tmp eq '.' || $tmp eq '..'; | |
346 | $dh->close; | |
347 | return 0; | |
348 | } | |
349 | $dh->close; | |
350 | return 1; | |
351 | } | |
352 | ||
cbba9b5b | 353 | sub get_disks { |
7a98a62d | 354 | my ($disk, $nosmart) = @_; |
cbba9b5b DC |
355 | my $disklist = {}; |
356 | ||
357 | my $mounted = {}; | |
358 | ||
359 | my $mounts = PVE::ProcFSTools::parse_proc_mounts(); | |
360 | ||
361 | foreach my $mount (@$mounts) { | |
362 | next if $mount->[0] !~ m|^/dev/|; | |
363 | $mounted->{abs_path($mount->[0])} = $mount->[1]; | |
364 | }; | |
365 | ||
366 | my $dev_is_mounted = sub { | |
367 | my ($dev) = @_; | |
368 | return $mounted->{$dev}; | |
369 | }; | |
370 | ||
cbba9b5b DC |
371 | my $journalhash = get_ceph_journals(); |
372 | ||
373 | my $zfslist = get_zfs_devices(); | |
374 | ||
375 | my $lvmlist = get_lvm_devices(); | |
376 | ||
377 | dir_glob_foreach('/sys/block', '.*', sub { | |
378 | my ($dev) = @_; | |
379 | return if defined($disk) && $disk ne $dev; | |
380 | # whitelisting following devices | |
381 | # hdX: ide block device | |
382 | # sdX: sd block device | |
383 | # vdX: virtual block device | |
384 | # xvdX: xen virtual block device | |
385 | # nvmeXnY: nvme devices | |
38ddd4ce | 386 | # cciss!cXnY: cciss devices |
cbba9b5b DC |
387 | return if $dev !~ m/^(h|s|x?v)d[a-z]+$/ && |
388 | $dev !~ m/^nvme\d+n\d+$/ && | |
38ddd4ce | 389 | $dev !~ m/^cciss\!c\d+d\d+$/; |
cbba9b5b | 390 | |
532e89e7 | 391 | my $data = get_udev_info("/sys/block/$dev"); |
cbba9b5b DC |
392 | return if !defined($data); |
393 | my $devpath = $data->{devpath}; | |
394 | ||
395 | my $sysdir = "/sys/block/$dev"; | |
396 | ||
cbba9b5b | 397 | # we do not want iscsi devices |
461a9fd8 | 398 | return if -l $sysdir && readlink($sysdir) =~ m|host[^/]*/session[^/]*|; |
cbba9b5b DC |
399 | |
400 | my $sysdata = get_sysdir_info($sysdir); | |
401 | return if !defined($sysdata); | |
402 | ||
403 | my $type = 'unknown'; | |
404 | ||
405 | if ($sysdata->{rotational} == 0) { | |
406 | $type = 'ssd'; | |
407 | $data->{rpm} = 0; | |
408 | } elsif ($sysdata->{rotational} == 1) { | |
409 | if ($data->{rpm} != -1) { | |
410 | $type = 'hdd'; | |
411 | } elsif ($data->{usb}) { | |
412 | $type = 'usb'; | |
413 | $data->{rpm} = 0; | |
414 | } | |
415 | } | |
416 | ||
acd3d916 | 417 | my $health = 'UNKNOWN'; |
6965a670 | 418 | my $wearout = 'N/A'; |
7a98a62d FG |
419 | |
420 | if (!$nosmart) { | |
421 | eval { | |
dd902da7 DC |
422 | my $smartdata = get_smart_data($devpath, ($type ne 'ssd')); |
423 | $health = $smartdata->{health} if $smartdata->{health}; | |
424 | ||
7a98a62d FG |
425 | if ($type eq 'ssd') { |
426 | # if we have an ssd we try to get the wearout indicator | |
865bdbd9 | 427 | my $wearval = get_wear_leveling_info($smartdata->{attributes}, $data->{model} || $sysdir->{model}); |
6965a670 | 428 | $wearout = $wearval if $wearval; |
acd3d916 | 429 | } |
7a98a62d FG |
430 | }; |
431 | } | |
cbba9b5b DC |
432 | |
433 | my $used; | |
434 | ||
435 | $used = 'LVM' if $lvmlist->{$devpath}; | |
436 | ||
437 | $used = 'mounted' if &$dev_is_mounted($devpath); | |
438 | ||
439 | $used = 'ZFS' if $zfslist->{$devpath}; | |
440 | ||
441 | $disklist->{$dev} = { | |
442 | vendor => $sysdata->{vendor}, | |
865bdbd9 | 443 | model => $data->{model} || $sysdata->{model}, |
cbba9b5b DC |
444 | size => $sysdata->{size}, |
445 | serial => $data->{serial}, | |
446 | gpt => $data->{gpt}, | |
447 | rpm => $data->{rpm}, | |
448 | type => $type, | |
449 | wwn => $data->{wwn}, | |
450 | health => $health, | |
451 | devpath => $devpath, | |
452 | wearout => $wearout, | |
453 | }; | |
454 | ||
455 | my $osdid = -1; | |
456 | ||
457 | my $journal_count = 0; | |
458 | ||
459 | my $found_partitions; | |
460 | my $found_lvm; | |
461 | my $found_mountpoints; | |
462 | my $found_zfs; | |
463 | my $found_dm; | |
464 | my $partpath = $devpath; | |
465 | ||
466 | # remove part after last / to | |
467 | # get the base path for the partitions | |
468 | # e.g. from /dev/cciss/c0d0 get /dev/cciss | |
469 | $partpath =~ s/\/[^\/]+$//; | |
470 | ||
471 | dir_glob_foreach("$sysdir", "$dev.+", sub { | |
472 | my ($part) = @_; | |
473 | ||
474 | $found_partitions = 1; | |
475 | ||
476 | if (my $mp = &$dev_is_mounted("$partpath/$part")) { | |
477 | $found_mountpoints = 1; | |
478 | if ($mp =~ m|^/var/lib/ceph/osd/ceph-(\d+)$|) { | |
479 | $osdid = $1; | |
480 | } | |
481 | } | |
482 | ||
483 | if ($lvmlist->{"$partpath/$part"}) { | |
484 | $found_lvm = 1; | |
485 | } | |
486 | ||
487 | if ($zfslist->{"$partpath/$part"}) { | |
488 | $found_zfs = 1; | |
489 | } | |
490 | ||
491 | $journal_count++ if $journalhash->{"$partpath/$part"}; | |
492 | ||
10a48db5 | 493 | if (!dir_is_empty("$sysdir/$part/holders") && !$found_lvm) { |
cbba9b5b DC |
494 | $found_dm = 1; |
495 | } | |
496 | }); | |
497 | ||
498 | $used = 'mounted' if $found_mountpoints && !$used; | |
499 | $used = 'LVM' if $found_lvm && !$used; | |
500 | $used = 'ZFS' if $found_zfs && !$used; | |
501 | $used = 'Device Mapper' if $found_dm && !$used; | |
502 | $used = 'partitions' if $found_partitions && !$used; | |
503 | ||
504 | # multipath, software raid, etc. | |
505 | # this check comes in last, to show more specific info | |
506 | # if we have it | |
10a48db5 | 507 | $used = 'Device Mapper' if !$used && !dir_is_empty("$sysdir/holders"); |
cbba9b5b DC |
508 | |
509 | $disklist->{$dev}->{used} = $used if $used; | |
510 | $disklist->{$dev}->{osdid} = $osdid; | |
511 | $disklist->{$dev}->{journals} = $journal_count; | |
512 | }); | |
513 | ||
514 | return $disklist; | |
515 | ||
516 | } | |
517 | ||
518 | 1; |