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