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