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