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