]> git.proxmox.com Git - pve-storage.git/blame - PVE/Diskmanage.pm
bump version to 5.0-16
[pve-storage.git] / PVE / Diskmanage.pm
CommitLineData
cbba9b5b
DC
1package PVE::Diskmanage;
2
3use strict;
4use warnings;
5use PVE::ProcFSTools;
6use Data::Dumper;
7use Cwd qw(abs_path);
3196c387 8use Fcntl ':mode';
cbba9b5b
DC
9
10use PVE::Tools qw(extract_param run_command file_get_contents file_read_firstline dir_glob_regex dir_glob_foreach trim);
11
12my $SMARTCTL = "/usr/sbin/smartctl";
13my $ZPOOL = "/sbin/zpool";
14my $SGDISK = "/sbin/sgdisk";
15my $PVS = "/sbin/pvs";
16my $UDEVADM = "/bin/udevadm";
17
18sub 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
34sub 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
45sub 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
59sub disk_is_used {
60 my ($disk) = @_;
61
62 my $dev = $disk;
63 $dev =~ s|^/dev/||;
64
7a98a62d 65 my $disklist = get_disks($dev, 1);
cbba9b5b
DC
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
73sub get_smart_data {
dd902da7 74 my ($disk, $healthonly) = @_;
cbba9b5b
DC
75
76 assert_blockdev($disk);
77 my $smartdata = {};
dc1311cb 78 my $type;
cbba9b5b 79
9018a4e6 80 my $returncode = 0;
c9bd3d22
FG
81
82 $disk =~ s/n\d+$//
83 if $disk =~ m!^/dev/nvme\d+n\d+$!;
84
dd902da7
DC
85 my $cmd = [$SMARTCTL, '-H'];
86 push @$cmd, '-A', '-f', 'brief' if !$healthonly;
87 push @$cmd, $disk;
88
cbba9b5b 89 eval {
dd902da7 90 $returncode = run_command($cmd, noerr => 1, outfunc => sub{
cbba9b5b
DC
91 my ($line) = @_;
92
1c999553
FG
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
dc1311cb
FG
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
bd54091c 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+(.*)$/) {
cbba9b5b 102 my $entry = {};
bd54091c
DC
103
104
1c999553
FG
105 $entry->{name} = $2 if defined $2;
106 $entry->{flags} = $3 if defined $3;
cbba9b5b 107 # the +0 makes a number out of the strings
1c999553
FG
108 $entry->{value} = $4+0 if defined $4;
109 $entry->{worst} = $5+0 if defined $5;
bd54091c
DC
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 }
1c999553
FG
116 $entry->{fail} = $7 if defined $7;
117 $entry->{raw} = $8 if defined $8;
118 $entry->{id} = $1 if defined $1;
cbba9b5b 119 push @{$smartdata->{attributes}}, $entry;
5db2d529 120 } elsif ($line =~ m/(?:Health Status|self\-assessment test result): (.*)$/ ) {
cbba9b5b
DC
121 $smartdata->{health} = $1;
122 } elsif ($line =~ m/Vendor Specific SMART Attributes with Thresholds:/) {
dc1311cb
FG
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";
dd902da7
DC
130 } elsif ($line =~ m/SMART Disabled/) {
131 $smartdata->{health} = "SMART Disabled";
cbba9b5b
DC
132 }
133 });
134 };
9018a4e6
DC
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 }
dc1311cb
FG
143
144 $smartdata->{type} = $type;
145
cbba9b5b
DC
146 return $smartdata;
147}
148
cbba9b5b
DC
149sub 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
181sub 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
208sub get_ceph_journals {
209 my $journalhash = {};
210
211 my $journal_uuid = '45b0969e-9b03-4f30-b4c6-b4b80ceff106';
e2bd817c
DC
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';
cbba9b5b 215
e2bd817c
DC
216 dir_glob_foreach('/dev/disk/by-parttypeuuid', "($journal_uuid|$db_uuid|$wal_uuid|$block_uuid)\..+", sub {
217 my ($entry, $type) = @_;
cbba9b5b 218 my $real_dev = abs_path("/dev/disk/by-parttypeuuid/$entry");
e2bd817c
DC
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 }
cbba9b5b
DC
228 });
229
230 return $journalhash;
231}
232
233sub get_udev_info {
234 my ($dev) = @_;
235
236 my $info = "";
237 my $data = {};
238 eval {
532e89e7 239 run_command([$UDEVADM, 'info', '-p', $dev, '--query', 'all'], outfunc => sub {
cbba9b5b
DC
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
865bdbd9
DC
277 if ($info =~ m/^E: ID_MODEL=(.+)$/m) {
278 $data->{model} = $1;
279 }
280
cbba9b5b
DC
281 $data->{wwn} = 'unknown';
282 if ($info =~ m/^E: ID_WWN=(.*)$/m) {
283 $data->{wwn} = $1;
284 }
285
286 return $data;
287}
288
289sub get_sysdir_info {
290 my ($sysdir) = @_;
291
461a9fd8
DC
292 return undef if ! -d "$sysdir/device";
293
cbba9b5b
DC
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
571b6f26 304 $data->{rotational} = file_read_firstline("$sysdir/queue/rotational") // -1;
cbba9b5b
DC
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
6965a670
DC
312sub 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,
7b32e2a7 322 'crucial' => 202,
6965a670
DC
323 'default' => 233,
324 };
325
326 # find target attr id
327
328 my $attrid;
329
d57fb43d 330 foreach my $vendor (keys %$vendormap) {
6965a670
DC
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
10a48db5
DC
351sub 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
cbba9b5b 366sub get_disks {
7a98a62d 367 my ($disk, $nosmart) = @_;
cbba9b5b
DC
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
cbba9b5b
DC
384 my $journalhash = get_ceph_journals();
385
386 my $zfslist = get_zfs_devices();
387
388 my $lvmlist = get_lvm_devices();
389
1590fc13
DC
390 # we get cciss/c0d0 but need cciss!c0d0
391 if (defined($disk) && $disk =~ m|^cciss/|) {
392 $disk =~ s|cciss/|cciss!|;
393 }
394
cbba9b5b
DC
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
38ddd4ce 404 # cciss!cXnY: cciss devices
cbba9b5b
DC
405 return if $dev !~ m/^(h|s|x?v)d[a-z]+$/ &&
406 $dev !~ m/^nvme\d+n\d+$/ &&
38ddd4ce 407 $dev !~ m/^cciss\!c\d+d\d+$/;
cbba9b5b 408
532e89e7 409 my $data = get_udev_info("/sys/block/$dev");
cbba9b5b
DC
410 return if !defined($data);
411 my $devpath = $data->{devpath};
412
413 my $sysdir = "/sys/block/$dev";
414
cbba9b5b 415 # we do not want iscsi devices
461a9fd8 416 return if -l $sysdir && readlink($sysdir) =~ m|host[^/]*/session[^/]*|;
cbba9b5b
DC
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
acd3d916 435 my $health = 'UNKNOWN';
6965a670 436 my $wearout = 'N/A';
7a98a62d
FG
437
438 if (!$nosmart) {
439 eval {
dd902da7
DC
440 my $smartdata = get_smart_data($devpath, ($type ne 'ssd'));
441 $health = $smartdata->{health} if $smartdata->{health};
442
7a98a62d
FG
443 if ($type eq 'ssd') {
444 # if we have an ssd we try to get the wearout indicator
865bdbd9 445 my $wearval = get_wear_leveling_info($smartdata->{attributes}, $data->{model} || $sysdir->{model});
6965a670 446 $wearout = $wearval if $wearval;
acd3d916 447 }
7a98a62d
FG
448 };
449 }
cbba9b5b
DC
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 $disklist->{$dev} = {
460 vendor => $sysdata->{vendor},
865bdbd9 461 model => $data->{model} || $sysdata->{model},
cbba9b5b
DC
462 size => $sysdata->{size},
463 serial => $data->{serial},
464 gpt => $data->{gpt},
465 rpm => $data->{rpm},
466 type => $type,
467 wwn => $data->{wwn},
468 health => $health,
469 devpath => $devpath,
470 wearout => $wearout,
471 };
472
473 my $osdid = -1;
e2bd817c 474 my $bluestore = 0;
cbba9b5b
DC
475
476 my $journal_count = 0;
e2bd817c
DC
477 my $db_count = 0;
478 my $wal_count = 0;
cbba9b5b
DC
479
480 my $found_partitions;
481 my $found_lvm;
482 my $found_mountpoints;
483 my $found_zfs;
484 my $found_dm;
485 my $partpath = $devpath;
486
487 # remove part after last / to
488 # get the base path for the partitions
489 # e.g. from /dev/cciss/c0d0 get /dev/cciss
490 $partpath =~ s/\/[^\/]+$//;
491
492 dir_glob_foreach("$sysdir", "$dev.+", sub {
493 my ($part) = @_;
494
495 $found_partitions = 1;
496
497 if (my $mp = &$dev_is_mounted("$partpath/$part")) {
498 $found_mountpoints = 1;
499 if ($mp =~ m|^/var/lib/ceph/osd/ceph-(\d+)$|) {
500 $osdid = $1;
501 }
502 }
503
504 if ($lvmlist->{"$partpath/$part"}) {
505 $found_lvm = 1;
506 }
507
508 if ($zfslist->{"$partpath/$part"}) {
509 $found_zfs = 1;
510 }
511
e2bd817c
DC
512 if ($journalhash->{"$partpath/$part"}) {
513 $journal_count++ if $journalhash->{"$partpath/$part"} == 1;
514 $db_count++ if $journalhash->{"$partpath/$part"} == 2;
515 $wal_count++ if $journalhash->{"$partpath/$part"} == 3;
516 $bluestore = 1 if $journalhash->{"$partpath/$part"} == 4;
517 }
cbba9b5b 518
10a48db5 519 if (!dir_is_empty("$sysdir/$part/holders") && !$found_lvm) {
cbba9b5b
DC
520 $found_dm = 1;
521 }
522 });
523
524 $used = 'mounted' if $found_mountpoints && !$used;
525 $used = 'LVM' if $found_lvm && !$used;
526 $used = 'ZFS' if $found_zfs && !$used;
527 $used = 'Device Mapper' if $found_dm && !$used;
528 $used = 'partitions' if $found_partitions && !$used;
529
530 # multipath, software raid, etc.
531 # this check comes in last, to show more specific info
532 # if we have it
10a48db5 533 $used = 'Device Mapper' if !$used && !dir_is_empty("$sysdir/holders");
cbba9b5b
DC
534
535 $disklist->{$dev}->{used} = $used if $used;
536 $disklist->{$dev}->{osdid} = $osdid;
e2bd817c
DC
537 $disklist->{$dev}->{journals} = $journal_count if $journal_count;
538 $disklist->{$dev}->{bluestore} = $bluestore if $osdid != -1;
539 $disklist->{$dev}->{db} = $db_count if $db_count;
540 $disklist->{$dev}->{wal} = $wal_count if $wal_count;
cbba9b5b
DC
541 });
542
543 return $disklist;
544
545}
546
3196c387
WL
547sub get_partnum {
548 my ($part_path) = @_;
549
550 my ($mode, $rdev) = (stat($part_path))[2,6];
551
552 next if !$mode || !S_ISBLK($mode) || !$rdev;
553 my $major = int($rdev / 0x100);
554 my $minor = $rdev % 0x100;
555 my $partnum_path = "/sys/dev/block/$major:$minor/";
556
557 my $partnum;
558
559 $partnum = file_read_firstline("${partnum_path}partition");
560
561 die "Partition does not exists\n" if !defined($partnum);
562
563 #untaint and ensure it is a int
564 if ($partnum =~ m/(\d+)/) {
565 $partnum = $1;
566 die "Partition number $partnum is invalid\n" if $partnum > 128;
567 } else {
568 die "Failed to get partition number\n";
569 }
570
571 return $partnum;
572}
573
0d28307d
WL
574sub get_blockdev {
575 my ($part_path) = @_;
576
577 my $dev = $1 if $part_path =~ m|^/dev/(.*)$|;
578 my $link = readlink "/sys/class/block/$dev";
579 my $block_dev = $1 if $link =~ m|([^/]*)/$dev$|;
580
581 die "Can't parse parent device\n" if !defined($block_dev);
582 die "No valid block device\n" if index($dev, $block_dev) == -1;
583
584 $block_dev = "/dev/$block_dev";
585 die "Block device does not exsists\n" if !(-b $block_dev);
586
587 return $block_dev;
588}
589
cbba9b5b 5901;