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