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