]> git.proxmox.com Git - pve-storage.git/blob - PVE/Diskmanage.pm
correctly parse cciss devices in 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, 1);
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, $healthonly) = @_;
74
75 assert_blockdev($disk);
76 my $smartdata = {};
77 my $type;
78
79 my $returncode = 0;
80
81 $disk =~ s/n\d+$//
82 if $disk =~ m!^/dev/nvme\d+n\d+$!;
83
84 my $cmd = [$SMARTCTL, '-H'];
85 push @$cmd, '-A', '-f', 'brief' if !$healthonly;
86 push @$cmd, $disk;
87
88 eval {
89 $returncode = run_command($cmd, noerr => 1, outfunc => sub{
90 my ($line) = @_;
91
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
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+(.*)$/) {
101 my $entry = {};
102 $entry->{name} = $2 if defined $2;
103 $entry->{flags} = $3 if defined $3;
104 # the +0 makes a number out of the strings
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;
111 push @{$smartdata->{attributes}}, $entry;
112 } elsif ($line =~ m/(?:Health Status|self\-assessment test result): (.*)$/ ) {
113 $smartdata->{health} = $1;
114 } elsif ($line =~ m/Vendor Specific SMART Attributes with Thresholds:/) {
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";
122 } elsif ($line =~ m/SMART Disabled/) {
123 $smartdata->{health} = "SMART Disabled";
124 }
125 });
126 };
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 }
135
136 $smartdata->{type} = $type;
137
138 return $smartdata;
139 }
140
141 sub 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
173 sub 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
200 sub 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
214 sub get_udev_info {
215 my ($dev) = @_;
216
217 my $info = "";
218 my $data = {};
219 eval {
220 run_command([$UDEVADM, 'info', '-n', $dev, '--query', 'all'], outfunc => sub {
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
266 sub get_sysdir_info {
267 my ($sysdir) = @_;
268
269 my $data = {};
270
271 my $size = file_read_firstline("$sysdir/size");
272 return undef if !$size;
273
274 # linux always considers sectors to be 512 bytes,
275 # independently of real block size
276 $data->{size} = $size * 512;
277
278 # dir/queue/rotational should be 1 for hdd, 0 for ssd
279 $data->{rotational} = file_read_firstline("$sysdir/queue/rotational");
280
281 $data->{vendor} = file_read_firstline("$sysdir/device/vendor") || 'unknown';
282 $data->{model} = file_read_firstline("$sysdir/device/model") || 'unknown';
283
284 return $data;
285 }
286
287 sub get_wear_leveling_info {
288 my ($attributes, $model) = @_;
289
290 my $wearout;
291
292 my $vendormap = {
293 'kingston' => 231,
294 'samsung' => 177,
295 'intel' => 233,
296 'sandisk' => 233,
297 'default' => 233,
298 };
299
300 # find target attr id
301
302 my $attrid;
303
304 foreach my $vendor (keys $vendormap) {
305 if ($model =~ m/$vendor/i) {
306 $attrid = $vendormap->{$vendor};
307 # found the attribute
308 last;
309 }
310 }
311
312 if (!$attrid) {
313 $attrid = $vendormap->{default};
314 }
315
316 foreach my $attr (@$attributes) {
317 next if $attr->{id} != $attrid;
318 $wearout = $attr->{value};
319 last;
320 }
321
322 return $wearout;
323 }
324
325 sub get_disks {
326 my ($disk, $nosmart) = @_;
327 my $disklist = {};
328
329 my $mounted = {};
330
331 my $mounts = PVE::ProcFSTools::parse_proc_mounts();
332
333 foreach my $mount (@$mounts) {
334 next if $mount->[0] !~ m|^/dev/|;
335 $mounted->{abs_path($mount->[0])} = $mount->[1];
336 };
337
338 my $dev_is_mounted = sub {
339 my ($dev) = @_;
340 return $mounted->{$dev};
341 };
342
343 my $dir_is_empty = sub {
344 my ($dir) = @_;
345
346 my $dh = IO::Dir->new ($dir);
347 return 1 if !$dh;
348
349 while (defined(my $tmp = $dh->read)) {
350 next if $tmp eq '.' || $tmp eq '..';
351 $dh->close;
352 return 0;
353 }
354 $dh->close;
355 return 1;
356 };
357
358 my $journalhash = get_ceph_journals();
359
360 my $zfslist = get_zfs_devices();
361
362 my $lvmlist = get_lvm_devices();
363
364 dir_glob_foreach('/sys/block', '.*', sub {
365 my ($dev) = @_;
366 return if defined($disk) && $disk ne $dev;
367 # whitelisting following devices
368 # hdX: ide block device
369 # sdX: sd block device
370 # vdX: virtual block device
371 # xvdX: xen virtual block device
372 # nvmeXnY: nvme devices
373 # cciss!cXnY: cciss devices
374 return if $dev !~ m/^(h|s|x?v)d[a-z]+$/ &&
375 $dev !~ m/^nvme\d+n\d+$/ &&
376 $dev !~ m/^cciss\!c\d+d\d+$/;
377
378 my $data = get_udev_info($dev);
379 return if !defined($data);
380 my $devpath = $data->{devpath};
381
382 my $sysdir = "/sys/block/$dev";
383
384 return if ! -d "$sysdir/device";
385
386 # we do not want iscsi devices
387 return if readlink($sysdir) =~ m|host[^/]*/session[^/]*|;
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
406 my $health = 'UNKNOWN';
407 my $wearout = 'N/A';
408
409 if (!$nosmart) {
410 eval {
411 my $smartdata = get_smart_data($devpath, ($type ne 'ssd'));
412 $health = $smartdata->{health} if $smartdata->{health};
413
414 if ($type eq 'ssd') {
415 # if we have an ssd we try to get the wearout indicator
416 my $wearval = get_wear_leveling_info($smartdata->{attributes}, $sysdata->{model});
417 $wearout = $wearval if $wearval;
418 }
419 };
420 }
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
507 1;