]>
Commit | Line | Data |
---|---|---|
cbba9b5b DC |
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 | ||
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 | ||
72 | sub 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 |
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 { | |
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 | ||
865bdbd9 DC |
258 | if ($info =~ m/^E: ID_MODEL=(.+)$/m) { |
259 | $data->{model} = $1; | |
260 | } | |
261 | ||
cbba9b5b DC |
262 | $data->{wwn} = 'unknown'; |
263 | if ($info =~ m/^E: ID_WWN=(.*)$/m) { | |
264 | $data->{wwn} = $1; | |
265 | } | |
266 | ||
267 | return $data; | |
268 | } | |
269 | ||
270 | sub get_sysdir_info { | |
271 | my ($sysdir) = @_; | |
272 | ||
461a9fd8 DC |
273 | return undef if ! -d "$sysdir/device"; |
274 | ||
cbba9b5b DC |
275 | my $data = {}; |
276 | ||
277 | my $size = file_read_firstline("$sysdir/size"); | |
278 | return undef if !$size; | |
279 | ||
280 | # linux always considers sectors to be 512 bytes, | |
281 | # independently of real block size | |
282 | $data->{size} = $size * 512; | |
283 | ||
284 | # dir/queue/rotational should be 1 for hdd, 0 for ssd | |
571b6f26 | 285 | $data->{rotational} = file_read_firstline("$sysdir/queue/rotational") // -1; |
cbba9b5b DC |
286 | |
287 | $data->{vendor} = file_read_firstline("$sysdir/device/vendor") || 'unknown'; | |
288 | $data->{model} = file_read_firstline("$sysdir/device/model") || 'unknown'; | |
289 | ||
290 | return $data; | |
291 | } | |
292 | ||
6965a670 DC |
293 | sub get_wear_leveling_info { |
294 | my ($attributes, $model) = @_; | |
295 | ||
296 | my $wearout; | |
297 | ||
298 | my $vendormap = { | |
299 | 'kingston' => 231, | |
300 | 'samsung' => 177, | |
301 | 'intel' => 233, | |
302 | 'sandisk' => 233, | |
303 | 'default' => 233, | |
304 | }; | |
305 | ||
306 | # find target attr id | |
307 | ||
308 | my $attrid; | |
309 | ||
d57fb43d | 310 | foreach my $vendor (keys %$vendormap) { |
6965a670 DC |
311 | if ($model =~ m/$vendor/i) { |
312 | $attrid = $vendormap->{$vendor}; | |
313 | # found the attribute | |
314 | last; | |
315 | } | |
316 | } | |
317 | ||
318 | if (!$attrid) { | |
319 | $attrid = $vendormap->{default}; | |
320 | } | |
321 | ||
322 | foreach my $attr (@$attributes) { | |
323 | next if $attr->{id} != $attrid; | |
324 | $wearout = $attr->{value}; | |
325 | last; | |
326 | } | |
327 | ||
328 | return $wearout; | |
329 | } | |
330 | ||
10a48db5 DC |
331 | sub dir_is_empty { |
332 | my ($dir) = @_; | |
333 | ||
334 | my $dh = IO::Dir->new ($dir); | |
335 | return 1 if !$dh; | |
336 | ||
337 | while (defined(my $tmp = $dh->read)) { | |
338 | next if $tmp eq '.' || $tmp eq '..'; | |
339 | $dh->close; | |
340 | return 0; | |
341 | } | |
342 | $dh->close; | |
343 | return 1; | |
344 | } | |
345 | ||
cbba9b5b | 346 | sub get_disks { |
7a98a62d | 347 | my ($disk, $nosmart) = @_; |
cbba9b5b DC |
348 | my $disklist = {}; |
349 | ||
350 | my $mounted = {}; | |
351 | ||
352 | my $mounts = PVE::ProcFSTools::parse_proc_mounts(); | |
353 | ||
354 | foreach my $mount (@$mounts) { | |
355 | next if $mount->[0] !~ m|^/dev/|; | |
356 | $mounted->{abs_path($mount->[0])} = $mount->[1]; | |
357 | }; | |
358 | ||
359 | my $dev_is_mounted = sub { | |
360 | my ($dev) = @_; | |
361 | return $mounted->{$dev}; | |
362 | }; | |
363 | ||
cbba9b5b DC |
364 | my $journalhash = get_ceph_journals(); |
365 | ||
366 | my $zfslist = get_zfs_devices(); | |
367 | ||
368 | my $lvmlist = get_lvm_devices(); | |
369 | ||
370 | dir_glob_foreach('/sys/block', '.*', sub { | |
371 | my ($dev) = @_; | |
372 | return if defined($disk) && $disk ne $dev; | |
373 | # whitelisting following devices | |
374 | # hdX: ide block device | |
375 | # sdX: sd block device | |
376 | # vdX: virtual block device | |
377 | # xvdX: xen virtual block device | |
378 | # nvmeXnY: nvme devices | |
38ddd4ce | 379 | # cciss!cXnY: cciss devices |
cbba9b5b DC |
380 | return if $dev !~ m/^(h|s|x?v)d[a-z]+$/ && |
381 | $dev !~ m/^nvme\d+n\d+$/ && | |
38ddd4ce | 382 | $dev !~ m/^cciss\!c\d+d\d+$/; |
cbba9b5b | 383 | |
532e89e7 | 384 | my $data = get_udev_info("/sys/block/$dev"); |
cbba9b5b DC |
385 | return if !defined($data); |
386 | my $devpath = $data->{devpath}; | |
387 | ||
388 | my $sysdir = "/sys/block/$dev"; | |
389 | ||
cbba9b5b | 390 | # we do not want iscsi devices |
461a9fd8 | 391 | return if -l $sysdir && readlink($sysdir) =~ m|host[^/]*/session[^/]*|; |
cbba9b5b DC |
392 | |
393 | my $sysdata = get_sysdir_info($sysdir); | |
394 | return if !defined($sysdata); | |
395 | ||
396 | my $type = 'unknown'; | |
397 | ||
398 | if ($sysdata->{rotational} == 0) { | |
399 | $type = 'ssd'; | |
400 | $data->{rpm} = 0; | |
401 | } elsif ($sysdata->{rotational} == 1) { | |
402 | if ($data->{rpm} != -1) { | |
403 | $type = 'hdd'; | |
404 | } elsif ($data->{usb}) { | |
405 | $type = 'usb'; | |
406 | $data->{rpm} = 0; | |
407 | } | |
408 | } | |
409 | ||
acd3d916 | 410 | my $health = 'UNKNOWN'; |
6965a670 | 411 | my $wearout = 'N/A'; |
7a98a62d FG |
412 | |
413 | if (!$nosmart) { | |
414 | eval { | |
dd902da7 DC |
415 | my $smartdata = get_smart_data($devpath, ($type ne 'ssd')); |
416 | $health = $smartdata->{health} if $smartdata->{health}; | |
417 | ||
7a98a62d FG |
418 | if ($type eq 'ssd') { |
419 | # if we have an ssd we try to get the wearout indicator | |
865bdbd9 | 420 | my $wearval = get_wear_leveling_info($smartdata->{attributes}, $data->{model} || $sysdir->{model}); |
6965a670 | 421 | $wearout = $wearval if $wearval; |
acd3d916 | 422 | } |
7a98a62d FG |
423 | }; |
424 | } | |
cbba9b5b DC |
425 | |
426 | my $used; | |
427 | ||
428 | $used = 'LVM' if $lvmlist->{$devpath}; | |
429 | ||
430 | $used = 'mounted' if &$dev_is_mounted($devpath); | |
431 | ||
432 | $used = 'ZFS' if $zfslist->{$devpath}; | |
433 | ||
434 | $disklist->{$dev} = { | |
435 | vendor => $sysdata->{vendor}, | |
865bdbd9 | 436 | model => $data->{model} || $sysdata->{model}, |
cbba9b5b DC |
437 | size => $sysdata->{size}, |
438 | serial => $data->{serial}, | |
439 | gpt => $data->{gpt}, | |
440 | rpm => $data->{rpm}, | |
441 | type => $type, | |
442 | wwn => $data->{wwn}, | |
443 | health => $health, | |
444 | devpath => $devpath, | |
445 | wearout => $wearout, | |
446 | }; | |
447 | ||
448 | my $osdid = -1; | |
449 | ||
450 | my $journal_count = 0; | |
451 | ||
452 | my $found_partitions; | |
453 | my $found_lvm; | |
454 | my $found_mountpoints; | |
455 | my $found_zfs; | |
456 | my $found_dm; | |
457 | my $partpath = $devpath; | |
458 | ||
459 | # remove part after last / to | |
460 | # get the base path for the partitions | |
461 | # e.g. from /dev/cciss/c0d0 get /dev/cciss | |
462 | $partpath =~ s/\/[^\/]+$//; | |
463 | ||
464 | dir_glob_foreach("$sysdir", "$dev.+", sub { | |
465 | my ($part) = @_; | |
466 | ||
467 | $found_partitions = 1; | |
468 | ||
469 | if (my $mp = &$dev_is_mounted("$partpath/$part")) { | |
470 | $found_mountpoints = 1; | |
471 | if ($mp =~ m|^/var/lib/ceph/osd/ceph-(\d+)$|) { | |
472 | $osdid = $1; | |
473 | } | |
474 | } | |
475 | ||
476 | if ($lvmlist->{"$partpath/$part"}) { | |
477 | $found_lvm = 1; | |
478 | } | |
479 | ||
480 | if ($zfslist->{"$partpath/$part"}) { | |
481 | $found_zfs = 1; | |
482 | } | |
483 | ||
484 | $journal_count++ if $journalhash->{"$partpath/$part"}; | |
485 | ||
10a48db5 | 486 | if (!dir_is_empty("$sysdir/$part/holders") && !$found_lvm) { |
cbba9b5b DC |
487 | $found_dm = 1; |
488 | } | |
489 | }); | |
490 | ||
491 | $used = 'mounted' if $found_mountpoints && !$used; | |
492 | $used = 'LVM' if $found_lvm && !$used; | |
493 | $used = 'ZFS' if $found_zfs && !$used; | |
494 | $used = 'Device Mapper' if $found_dm && !$used; | |
495 | $used = 'partitions' if $found_partitions && !$used; | |
496 | ||
497 | # multipath, software raid, etc. | |
498 | # this check comes in last, to show more specific info | |
499 | # if we have it | |
10a48db5 | 500 | $used = 'Device Mapper' if !$used && !dir_is_empty("$sysdir/holders"); |
cbba9b5b DC |
501 | |
502 | $disklist->{$dev}->{used} = $used if $used; | |
503 | $disklist->{$dev}->{osdid} = $osdid; | |
504 | $disklist->{$dev}->{journals} = $journal_count; | |
505 | }); | |
506 | ||
507 | return $disklist; | |
508 | ||
509 | } | |
510 | ||
511 | 1; |