]> git.proxmox.com Git - pve-storage.git/blame - PVE/Diskmanage.pm
move directory test into get_sysdir_info
[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);
8
9use PVE::Tools qw(extract_param run_command file_get_contents file_read_firstline dir_glob_regex dir_glob_foreach trim);
10
11my $SMARTCTL = "/usr/sbin/smartctl";
12my $ZPOOL = "/sbin/zpool";
13my $SGDISK = "/sbin/sgdisk";
14my $PVS = "/sbin/pvs";
15my $UDEVADM = "/bin/udevadm";
16
17sub 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
33sub 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
44sub 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
58sub disk_is_used {
59 my ($disk) = @_;
60
61 my $dev = $disk;
62 $dev =~ s|^/dev/||;
63
7a98a62d 64 my $disklist = get_disks($dev, 1);
cbba9b5b
DC
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
72sub get_smart_data {
dd902da7 73 my ($disk, $healthonly) = @_;
cbba9b5b
DC
74
75 assert_blockdev($disk);
76 my $smartdata = {};
dc1311cb 77 my $type;
cbba9b5b 78
9018a4e6 79 my $returncode = 0;
c9bd3d22
FG
80
81 $disk =~ s/n\d+$//
82 if $disk =~ m!^/dev/nvme\d+n\d+$!;
83
dd902da7
DC
84 my $cmd = [$SMARTCTL, '-H'];
85 push @$cmd, '-A', '-f', 'brief' if !$healthonly;
86 push @$cmd, $disk;
87
cbba9b5b 88 eval {
dd902da7 89 $returncode = run_command($cmd, noerr => 1, outfunc => sub{
cbba9b5b
DC
90 my ($line) = @_;
91
1c999553
FG
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
dc1311cb
FG
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+(.*)$/) {
cbba9b5b 101 my $entry = {};
1c999553
FG
102 $entry->{name} = $2 if defined $2;
103 $entry->{flags} = $3 if defined $3;
cbba9b5b 104 # the +0 makes a number out of the strings
1c999553
FG
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;
cbba9b5b 111 push @{$smartdata->{attributes}}, $entry;
5db2d529 112 } elsif ($line =~ m/(?:Health Status|self\-assessment test result): (.*)$/ ) {
cbba9b5b
DC
113 $smartdata->{health} = $1;
114 } elsif ($line =~ m/Vendor Specific SMART Attributes with Thresholds:/) {
dc1311cb
FG
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";
dd902da7
DC
122 } elsif ($line =~ m/SMART Disabled/) {
123 $smartdata->{health} = "SMART Disabled";
cbba9b5b
DC
124 }
125 });
126 };
9018a4e6
DC
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 }
dc1311cb
FG
135
136 $smartdata->{type} = $type;
137
cbba9b5b
DC
138 return $smartdata;
139}
140
cbba9b5b
DC
141sub 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
173sub 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
200sub 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
214sub get_udev_info {
215 my ($dev) = @_;
216
217 my $info = "";
218 my $data = {};
219 eval {
532e89e7 220 run_command([$UDEVADM, 'info', '-p', $dev, '--query', 'all'], outfunc => sub {
cbba9b5b
DC
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
266sub get_sysdir_info {
267 my ($sysdir) = @_;
268
461a9fd8
DC
269 return undef if ! -d "$sysdir/device";
270
cbba9b5b
DC
271 my $data = {};
272
273 my $size = file_read_firstline("$sysdir/size");
274 return undef if !$size;
275
276 # linux always considers sectors to be 512 bytes,
277 # independently of real block size
278 $data->{size} = $size * 512;
279
280 # dir/queue/rotational should be 1 for hdd, 0 for ssd
281 $data->{rotational} = file_read_firstline("$sysdir/queue/rotational");
282
283 $data->{vendor} = file_read_firstline("$sysdir/device/vendor") || 'unknown';
284 $data->{model} = file_read_firstline("$sysdir/device/model") || 'unknown';
285
286 return $data;
287}
288
6965a670
DC
289sub get_wear_leveling_info {
290 my ($attributes, $model) = @_;
291
292 my $wearout;
293
294 my $vendormap = {
295 'kingston' => 231,
296 'samsung' => 177,
297 'intel' => 233,
298 'sandisk' => 233,
299 'default' => 233,
300 };
301
302 # find target attr id
303
304 my $attrid;
305
d57fb43d 306 foreach my $vendor (keys %$vendormap) {
6965a670
DC
307 if ($model =~ m/$vendor/i) {
308 $attrid = $vendormap->{$vendor};
309 # found the attribute
310 last;
311 }
312 }
313
314 if (!$attrid) {
315 $attrid = $vendormap->{default};
316 }
317
318 foreach my $attr (@$attributes) {
319 next if $attr->{id} != $attrid;
320 $wearout = $attr->{value};
321 last;
322 }
323
324 return $wearout;
325}
326
cbba9b5b 327sub get_disks {
7a98a62d 328 my ($disk, $nosmart) = @_;
cbba9b5b
DC
329 my $disklist = {};
330
331 my $mounted = {};
332
333 my $mounts = PVE::ProcFSTools::parse_proc_mounts();
334
335 foreach my $mount (@$mounts) {
336 next if $mount->[0] !~ m|^/dev/|;
337 $mounted->{abs_path($mount->[0])} = $mount->[1];
338 };
339
340 my $dev_is_mounted = sub {
341 my ($dev) = @_;
342 return $mounted->{$dev};
343 };
344
345 my $dir_is_empty = sub {
346 my ($dir) = @_;
347
348 my $dh = IO::Dir->new ($dir);
349 return 1 if !$dh;
350
351 while (defined(my $tmp = $dh->read)) {
352 next if $tmp eq '.' || $tmp eq '..';
353 $dh->close;
354 return 0;
355 }
356 $dh->close;
357 return 1;
358 };
359
360 my $journalhash = get_ceph_journals();
361
362 my $zfslist = get_zfs_devices();
363
364 my $lvmlist = get_lvm_devices();
365
366 dir_glob_foreach('/sys/block', '.*', sub {
367 my ($dev) = @_;
368 return if defined($disk) && $disk ne $dev;
369 # whitelisting following devices
370 # hdX: ide block device
371 # sdX: sd block device
372 # vdX: virtual block device
373 # xvdX: xen virtual block device
374 # nvmeXnY: nvme devices
38ddd4ce 375 # cciss!cXnY: cciss devices
cbba9b5b
DC
376 return if $dev !~ m/^(h|s|x?v)d[a-z]+$/ &&
377 $dev !~ m/^nvme\d+n\d+$/ &&
38ddd4ce 378 $dev !~ m/^cciss\!c\d+d\d+$/;
cbba9b5b 379
532e89e7 380 my $data = get_udev_info("/sys/block/$dev");
cbba9b5b
DC
381 return if !defined($data);
382 my $devpath = $data->{devpath};
383
384 my $sysdir = "/sys/block/$dev";
385
cbba9b5b 386 # we do not want iscsi devices
461a9fd8 387 return if -l $sysdir && readlink($sysdir) =~ m|host[^/]*/session[^/]*|;
cbba9b5b
DC
388
389 my $sysdata = get_sysdir_info($sysdir);
390 return if !defined($sysdata);
391
392 my $type = 'unknown';
393
394 if ($sysdata->{rotational} == 0) {
395 $type = 'ssd';
396 $data->{rpm} = 0;
397 } elsif ($sysdata->{rotational} == 1) {
398 if ($data->{rpm} != -1) {
399 $type = 'hdd';
400 } elsif ($data->{usb}) {
401 $type = 'usb';
402 $data->{rpm} = 0;
403 }
404 }
405
acd3d916 406 my $health = 'UNKNOWN';
6965a670 407 my $wearout = 'N/A';
7a98a62d
FG
408
409 if (!$nosmart) {
410 eval {
dd902da7
DC
411 my $smartdata = get_smart_data($devpath, ($type ne 'ssd'));
412 $health = $smartdata->{health} if $smartdata->{health};
413
7a98a62d
FG
414 if ($type eq 'ssd') {
415 # if we have an ssd we try to get the wearout indicator
6965a670
DC
416 my $wearval = get_wear_leveling_info($smartdata->{attributes}, $sysdata->{model});
417 $wearout = $wearval if $wearval;
acd3d916 418 }
7a98a62d
FG
419 };
420 }
cbba9b5b
DC
421
422 my $used;
423
424 $used = 'LVM' if $lvmlist->{$devpath};
425
426 $used = 'mounted' if &$dev_is_mounted($devpath);
427
428 $used = 'ZFS' if $zfslist->{$devpath};
429
430 $disklist->{$dev} = {
431 vendor => $sysdata->{vendor},
432 model => $sysdata->{model},
433 size => $sysdata->{size},
434 serial => $data->{serial},
435 gpt => $data->{gpt},
436 rpm => $data->{rpm},
437 type => $type,
438 wwn => $data->{wwn},
439 health => $health,
440 devpath => $devpath,
441 wearout => $wearout,
442 };
443
444 my $osdid = -1;
445
446 my $journal_count = 0;
447
448 my $found_partitions;
449 my $found_lvm;
450 my $found_mountpoints;
451 my $found_zfs;
452 my $found_dm;
453 my $partpath = $devpath;
454
455 # remove part after last / to
456 # get the base path for the partitions
457 # e.g. from /dev/cciss/c0d0 get /dev/cciss
458 $partpath =~ s/\/[^\/]+$//;
459
460 dir_glob_foreach("$sysdir", "$dev.+", sub {
461 my ($part) = @_;
462
463 $found_partitions = 1;
464
465 if (my $mp = &$dev_is_mounted("$partpath/$part")) {
466 $found_mountpoints = 1;
467 if ($mp =~ m|^/var/lib/ceph/osd/ceph-(\d+)$|) {
468 $osdid = $1;
469 }
470 }
471
472 if ($lvmlist->{"$partpath/$part"}) {
473 $found_lvm = 1;
474 }
475
476 if ($zfslist->{"$partpath/$part"}) {
477 $found_zfs = 1;
478 }
479
480 $journal_count++ if $journalhash->{"$partpath/$part"};
481
482 if (!&$dir_is_empty("$sysdir/$part/holders") && !$found_lvm) {
483 $found_dm = 1;
484 }
485 });
486
487 $used = 'mounted' if $found_mountpoints && !$used;
488 $used = 'LVM' if $found_lvm && !$used;
489 $used = 'ZFS' if $found_zfs && !$used;
490 $used = 'Device Mapper' if $found_dm && !$used;
491 $used = 'partitions' if $found_partitions && !$used;
492
493 # multipath, software raid, etc.
494 # this check comes in last, to show more specific info
495 # if we have it
496 $used = 'Device Mapper' if !$used && !&$dir_is_empty("$sysdir/holders");
497
498 $disklist->{$dev}->{used} = $used if $used;
499 $disklist->{$dev}->{osdid} = $osdid;
500 $disklist->{$dev}->{journals} = $journal_count;
501 });
502
503 return $disklist;
504
505}
506
5071;