]> git.proxmox.com Git - pve-storage.git/blob - PVE/Diskmanage.pm
add crucial smart attribute for wear leveling
[pve-storage.git] / PVE / Diskmanage.pm
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
64 my $disklist = get_disks($dev, 1);
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 {
73 my ($disk, $healthonly) = @_;
74
75 assert_blockdev($disk);
76 my $smartdata = {};
77 my $type;
78
79 my $returncode = 0;
80
81 $disk =~ s/n\d+$//
82 if $disk =~ m!^/dev/nvme\d+n\d+$!;
83
84 my $cmd = [$SMARTCTL, '-H'];
85 push @$cmd, '-A', '-f', 'brief' if !$healthonly;
86 push @$cmd, $disk;
87
88 eval {
89 $returncode = run_command($cmd, noerr => 1, outfunc => sub{
90 my ($line) = @_;
91
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
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
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+(.*)$/) {
101 my $entry = {};
102
103
104 $entry->{name} = $2 if defined $2;
105 $entry->{flags} = $3 if defined $3;
106 # the +0 makes a number out of the strings
107 $entry->{value} = $4+0 if defined $4;
108 $entry->{worst} = $5+0 if defined $5;
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 }
115 $entry->{fail} = $7 if defined $7;
116 $entry->{raw} = $8 if defined $8;
117 $entry->{id} = $1 if defined $1;
118 push @{$smartdata->{attributes}}, $entry;
119 } elsif ($line =~ m/(?:Health Status|self\-assessment test result): (.*)$/ ) {
120 $smartdata->{health} = $1;
121 } elsif ($line =~ m/Vendor Specific SMART Attributes with Thresholds:/) {
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";
129 } elsif ($line =~ m/SMART Disabled/) {
130 $smartdata->{health} = "SMART Disabled";
131 }
132 });
133 };
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 }
142
143 $smartdata->{type} = $type;
144
145 return $smartdata;
146 }
147
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 {
227 run_command([$UDEVADM, 'info', '-p', $dev, '--query', 'all'], outfunc => sub {
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
265 if ($info =~ m/^E: ID_MODEL=(.+)$/m) {
266 $data->{model} = $1;
267 }
268
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
280 return undef if ! -d "$sysdir/device";
281
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
292 $data->{rotational} = file_read_firstline("$sysdir/queue/rotational") // -1;
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
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 'crucial' => 202,
311 'default' => 233,
312 };
313
314 # find target attr id
315
316 my $attrid;
317
318 foreach my $vendor (keys %$vendormap) {
319 if ($model =~ m/$vendor/i) {
320 $attrid = $vendormap->{$vendor};
321 # found the attribute
322 last;
323 }
324 }
325
326 if (!$attrid) {
327 $attrid = $vendormap->{default};
328 }
329
330 foreach my $attr (@$attributes) {
331 next if $attr->{id} != $attrid;
332 $wearout = $attr->{value};
333 last;
334 }
335
336 return $wearout;
337 }
338
339 sub dir_is_empty {
340 my ($dir) = @_;
341
342 my $dh = IO::Dir->new ($dir);
343 return 1 if !$dh;
344
345 while (defined(my $tmp = $dh->read)) {
346 next if $tmp eq '.' || $tmp eq '..';
347 $dh->close;
348 return 0;
349 }
350 $dh->close;
351 return 1;
352 }
353
354 sub get_disks {
355 my ($disk, $nosmart) = @_;
356 my $disklist = {};
357
358 my $mounted = {};
359
360 my $mounts = PVE::ProcFSTools::parse_proc_mounts();
361
362 foreach my $mount (@$mounts) {
363 next if $mount->[0] !~ m|^/dev/|;
364 $mounted->{abs_path($mount->[0])} = $mount->[1];
365 };
366
367 my $dev_is_mounted = sub {
368 my ($dev) = @_;
369 return $mounted->{$dev};
370 };
371
372 my $journalhash = get_ceph_journals();
373
374 my $zfslist = get_zfs_devices();
375
376 my $lvmlist = get_lvm_devices();
377
378 dir_glob_foreach('/sys/block', '.*', sub {
379 my ($dev) = @_;
380 return if defined($disk) && $disk ne $dev;
381 # whitelisting following devices
382 # hdX: ide block device
383 # sdX: sd block device
384 # vdX: virtual block device
385 # xvdX: xen virtual block device
386 # nvmeXnY: nvme devices
387 # cciss!cXnY: cciss devices
388 return if $dev !~ m/^(h|s|x?v)d[a-z]+$/ &&
389 $dev !~ m/^nvme\d+n\d+$/ &&
390 $dev !~ m/^cciss\!c\d+d\d+$/;
391
392 my $data = get_udev_info("/sys/block/$dev");
393 return if !defined($data);
394 my $devpath = $data->{devpath};
395
396 my $sysdir = "/sys/block/$dev";
397
398 # we do not want iscsi devices
399 return if -l $sysdir && readlink($sysdir) =~ m|host[^/]*/session[^/]*|;
400
401 my $sysdata = get_sysdir_info($sysdir);
402 return if !defined($sysdata);
403
404 my $type = 'unknown';
405
406 if ($sysdata->{rotational} == 0) {
407 $type = 'ssd';
408 $data->{rpm} = 0;
409 } elsif ($sysdata->{rotational} == 1) {
410 if ($data->{rpm} != -1) {
411 $type = 'hdd';
412 } elsif ($data->{usb}) {
413 $type = 'usb';
414 $data->{rpm} = 0;
415 }
416 }
417
418 my $health = 'UNKNOWN';
419 my $wearout = 'N/A';
420
421 if (!$nosmart) {
422 eval {
423 my $smartdata = get_smart_data($devpath, ($type ne 'ssd'));
424 $health = $smartdata->{health} if $smartdata->{health};
425
426 if ($type eq 'ssd') {
427 # if we have an ssd we try to get the wearout indicator
428 my $wearval = get_wear_leveling_info($smartdata->{attributes}, $data->{model} || $sysdir->{model});
429 $wearout = $wearval if $wearval;
430 }
431 };
432 }
433
434 my $used;
435
436 $used = 'LVM' if $lvmlist->{$devpath};
437
438 $used = 'mounted' if &$dev_is_mounted($devpath);
439
440 $used = 'ZFS' if $zfslist->{$devpath};
441
442 $disklist->{$dev} = {
443 vendor => $sysdata->{vendor},
444 model => $data->{model} || $sysdata->{model},
445 size => $sysdata->{size},
446 serial => $data->{serial},
447 gpt => $data->{gpt},
448 rpm => $data->{rpm},
449 type => $type,
450 wwn => $data->{wwn},
451 health => $health,
452 devpath => $devpath,
453 wearout => $wearout,
454 };
455
456 my $osdid = -1;
457
458 my $journal_count = 0;
459
460 my $found_partitions;
461 my $found_lvm;
462 my $found_mountpoints;
463 my $found_zfs;
464 my $found_dm;
465 my $partpath = $devpath;
466
467 # remove part after last / to
468 # get the base path for the partitions
469 # e.g. from /dev/cciss/c0d0 get /dev/cciss
470 $partpath =~ s/\/[^\/]+$//;
471
472 dir_glob_foreach("$sysdir", "$dev.+", sub {
473 my ($part) = @_;
474
475 $found_partitions = 1;
476
477 if (my $mp = &$dev_is_mounted("$partpath/$part")) {
478 $found_mountpoints = 1;
479 if ($mp =~ m|^/var/lib/ceph/osd/ceph-(\d+)$|) {
480 $osdid = $1;
481 }
482 }
483
484 if ($lvmlist->{"$partpath/$part"}) {
485 $found_lvm = 1;
486 }
487
488 if ($zfslist->{"$partpath/$part"}) {
489 $found_zfs = 1;
490 }
491
492 $journal_count++ if $journalhash->{"$partpath/$part"};
493
494 if (!dir_is_empty("$sysdir/$part/holders") && !$found_lvm) {
495 $found_dm = 1;
496 }
497 });
498
499 $used = 'mounted' if $found_mountpoints && !$used;
500 $used = 'LVM' if $found_lvm && !$used;
501 $used = 'ZFS' if $found_zfs && !$used;
502 $used = 'Device Mapper' if $found_dm && !$used;
503 $used = 'partitions' if $found_partitions && !$used;
504
505 # multipath, software raid, etc.
506 # this check comes in last, to show more specific info
507 # if we have it
508 $used = 'Device Mapper' if !$used && !dir_is_empty("$sysdir/holders");
509
510 $disklist->{$dev}->{used} = $used if $used;
511 $disklist->{$dev}->{osdid} = $osdid;
512 $disklist->{$dev}->{journals} = $journal_count;
513 });
514
515 return $disklist;
516
517 }
518
519 1;