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