]>
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 { | |
73 | my ($disk) = @_; | |
74 | ||
75 | assert_blockdev($disk); | |
76 | my $smartdata = {}; | |
77 | my $datastarted = 0; | |
78 | ||
9018a4e6 | 79 | my $returncode = 0; |
cbba9b5b | 80 | eval { |
0c486b09 | 81 | $returncode = run_command([$SMARTCTL, '-H', '-A', '-f', 'brief', $disk], noerr => 1, outfunc => sub{ |
cbba9b5b DC |
82 | my ($line) = @_; |
83 | ||
1c999553 FG |
84 | # ATA SMART attributes, e.g.: |
85 | # ID# ATTRIBUTE_NAME FLAGS VALUE WORST THRESH FAIL RAW_VALUE | |
86 | # 1 Raw_Read_Error_Rate POSR-K 100 100 000 - 0 | |
87 | if ($datastarted && $line =~ m/^([ \d]{2}\d)\s+(\S+)\s+(\S{6})\s+(\d+)\s+(\d+)\s+(\d+)\s+(\S+)\s+(.*)$/) { | |
cbba9b5b | 88 | my $entry = {}; |
1c999553 FG |
89 | $entry->{name} = $2 if defined $2; |
90 | $entry->{flags} = $3 if defined $3; | |
cbba9b5b | 91 | # the +0 makes a number out of the strings |
1c999553 FG |
92 | $entry->{value} = $4+0 if defined $4; |
93 | $entry->{worst} = $5+0 if defined $5; | |
94 | $entry->{threshold} = $6+0 if defined $6; | |
95 | $entry->{fail} = $7 if defined $7; | |
96 | $entry->{raw} = $8 if defined $8; | |
97 | $entry->{id} = $1 if defined $1; | |
cbba9b5b DC |
98 | push @{$smartdata->{attributes}}, $entry; |
99 | } elsif ($line =~ m/self\-assessment test result: (.*)$/) { | |
100 | $smartdata->{health} = $1; | |
101 | } elsif ($line =~ m/Vendor Specific SMART Attributes with Thresholds:/) { | |
102 | $datastarted = 1; | |
103 | } | |
104 | }); | |
105 | }; | |
9018a4e6 DC |
106 | my $err = $@; |
107 | ||
108 | # bit 0 and 1 mark an severe smartctl error | |
109 | # all others are for disk status, so ignore them | |
110 | # see smartctl(8) | |
111 | if ((defined($returncode) && ($returncode & 0b00000011)) || $err) { | |
112 | die "Error getting S.M.A.R.T. data: Exit code: $returncode\n"; | |
113 | } | |
cbba9b5b DC |
114 | return $smartdata; |
115 | } | |
116 | ||
117 | sub get_smart_health { | |
118 | my ($disk) = @_; | |
119 | ||
120 | return "NOT A DEVICE" if !assert_blockdev($disk, 1); | |
121 | ||
acd3d916 | 122 | my $message; |
cbba9b5b | 123 | |
acd3d916 FG |
124 | run_command([$SMARTCTL, '-H', $disk], noerr => 1, outfunc => sub { |
125 | my ($line) = @_; | |
cbba9b5b | 126 | |
acd3d916 FG |
127 | if ($line =~ m/test result: (.*)$/) { |
128 | $message = $1; | |
129 | } elsif ($line =~ m/open device: (.*) failed: (.*)$/) { | |
130 | $message = "FAILED TO OPEN"; | |
131 | } elsif ($line =~ m/^SMART Disabled/) { | |
132 | $message = "SMART DISABLED"; | |
133 | } | |
134 | }); | |
cbba9b5b DC |
135 | |
136 | return $message; | |
137 | } | |
138 | ||
139 | sub get_zfs_devices { | |
140 | my $list = {}; | |
141 | ||
142 | # use zpool and parttype uuid, | |
143 | # because log and cache do not have | |
144 | # zfs type uuid | |
145 | eval { | |
146 | run_command([$ZPOOL, 'list', '-HPLv'], outfunc => sub { | |
147 | my ($line) = @_; | |
148 | ||
149 | if ($line =~ m|^\t([^\t]+)\t|) { | |
150 | $list->{$1} = 1; | |
151 | } | |
152 | }); | |
153 | }; | |
154 | ||
155 | # only warn here, | |
156 | # because maybe zfs tools are not installed | |
157 | warn "$@\n" if $@; | |
158 | ||
159 | my $applezfsuuid = "6a898cc3-1dd2-11b2-99a6-080020736631"; | |
160 | my $bsdzfsuuid = "516e7cba-6ecf-11d6-8ff8-00022d09712b"; | |
161 | ||
162 | dir_glob_foreach('/dev/disk/by-parttypeuuid', "($applezfsuuid|$bsdzfsuuid)\..+", sub { | |
163 | my ($entry) = @_; | |
164 | my $real_dev = abs_path("/dev/disk/by-parttypeuuid/$entry"); | |
165 | $list->{$real_dev} = 1; | |
166 | }); | |
167 | ||
168 | return $list; | |
169 | } | |
170 | ||
171 | sub get_lvm_devices { | |
172 | my $list = {}; | |
173 | eval { | |
174 | run_command([$PVS, '--noheadings', '--readonly', '-o', 'pv_name'], outfunc => sub{ | |
175 | my ($line) = @_; | |
176 | $line = trim($line); | |
177 | if ($line =~ m|^/dev/|) { | |
178 | $list->{$line} = 1; | |
179 | } | |
180 | }); | |
181 | }; | |
182 | ||
183 | # if something goes wrong, we do not want | |
184 | # to give up, but indicate an error has occured | |
185 | warn "$@\n" if $@; | |
186 | ||
187 | my $lvmuuid = "e6d6d379-f507-44c2-a23c-238f2a3df928"; | |
188 | ||
189 | dir_glob_foreach('/dev/disk/by-parttypeuuid', "$lvmuuid\..+", sub { | |
190 | my ($entry) = @_; | |
191 | my $real_dev = abs_path("/dev/disk/by-parttypeuuid/$entry"); | |
192 | $list->{$real_dev} = 1; | |
193 | }); | |
194 | ||
195 | return $list; | |
196 | } | |
197 | ||
198 | sub get_ceph_journals { | |
199 | my $journalhash = {}; | |
200 | ||
201 | my $journal_uuid = '45b0969e-9b03-4f30-b4c6-b4b80ceff106'; | |
202 | ||
203 | dir_glob_foreach('/dev/disk/by-parttypeuuid', "$journal_uuid\..+", sub { | |
204 | my ($entry) = @_; | |
205 | my $real_dev = abs_path("/dev/disk/by-parttypeuuid/$entry"); | |
206 | $journalhash->{$real_dev} = 1; | |
207 | }); | |
208 | ||
209 | return $journalhash; | |
210 | } | |
211 | ||
212 | sub get_udev_info { | |
213 | my ($dev) = @_; | |
214 | ||
215 | my $info = ""; | |
216 | my $data = {}; | |
217 | eval { | |
218 | run_command([$UDEVADM, 'info', '-n', $dev, '--query', 'all'], outfunc => sub { | |
219 | my ($line) = @_; | |
220 | $info .= "$line\n"; | |
221 | }); | |
222 | }; | |
223 | warn $@ if $@; | |
224 | return undef if !$info; | |
225 | ||
226 | return undef if $info !~ m/^E: DEVTYPE=disk$/m; | |
227 | return undef if $info =~ m/^E: ID_CDROM/m; | |
228 | ||
229 | # we use this, because some disks are not simply in /dev | |
230 | # e.g. /dev/cciss/c0d0 | |
231 | if ($info =~ m/^E: DEVNAME=(\S+)$/m) { | |
232 | $data->{devpath} = $1; | |
233 | } | |
234 | return if !defined($data->{devpath}); | |
235 | ||
236 | $data->{serial} = 'unknown'; | |
237 | if ($info =~ m/^E: ID_SERIAL_SHORT=(\S+)$/m) { | |
238 | $data->{serial} = $1; | |
239 | } | |
240 | ||
241 | $data->{gpt} = 0; | |
242 | if ($info =~ m/^E: ID_PART_TABLE_TYPE=gpt$/m) { | |
243 | $data->{gpt} = 1; | |
244 | } | |
245 | ||
246 | # detect SSD | |
247 | $data->{rpm} = -1; | |
248 | if ($info =~ m/^E: ID_ATA_ROTATION_RATE_RPM=(\d+)$/m) { | |
249 | $data->{rpm} = $1; | |
250 | } | |
251 | ||
252 | if ($info =~ m/^E: ID_BUS=usb$/m) { | |
253 | $data->{usb} = 1; | |
254 | } | |
255 | ||
256 | $data->{wwn} = 'unknown'; | |
257 | if ($info =~ m/^E: ID_WWN=(.*)$/m) { | |
258 | $data->{wwn} = $1; | |
259 | } | |
260 | ||
261 | return $data; | |
262 | } | |
263 | ||
264 | sub get_sysdir_info { | |
265 | my ($sysdir) = @_; | |
266 | ||
267 | my $data = {}; | |
268 | ||
269 | my $size = file_read_firstline("$sysdir/size"); | |
270 | return undef if !$size; | |
271 | ||
272 | # linux always considers sectors to be 512 bytes, | |
273 | # independently of real block size | |
274 | $data->{size} = $size * 512; | |
275 | ||
276 | # dir/queue/rotational should be 1 for hdd, 0 for ssd | |
277 | $data->{rotational} = file_read_firstline("$sysdir/queue/rotational"); | |
278 | ||
279 | $data->{vendor} = file_read_firstline("$sysdir/device/vendor") || 'unknown'; | |
280 | $data->{model} = file_read_firstline("$sysdir/device/model") || 'unknown'; | |
281 | ||
282 | return $data; | |
283 | } | |
284 | ||
285 | sub get_disks { | |
7a98a62d | 286 | my ($disk, $nosmart) = @_; |
cbba9b5b DC |
287 | my $disklist = {}; |
288 | ||
289 | my $mounted = {}; | |
290 | ||
291 | my $mounts = PVE::ProcFSTools::parse_proc_mounts(); | |
292 | ||
293 | foreach my $mount (@$mounts) { | |
294 | next if $mount->[0] !~ m|^/dev/|; | |
295 | $mounted->{abs_path($mount->[0])} = $mount->[1]; | |
296 | }; | |
297 | ||
298 | my $dev_is_mounted = sub { | |
299 | my ($dev) = @_; | |
300 | return $mounted->{$dev}; | |
301 | }; | |
302 | ||
303 | my $dir_is_empty = sub { | |
304 | my ($dir) = @_; | |
305 | ||
306 | my $dh = IO::Dir->new ($dir); | |
307 | return 1 if !$dh; | |
308 | ||
309 | while (defined(my $tmp = $dh->read)) { | |
310 | next if $tmp eq '.' || $tmp eq '..'; | |
311 | $dh->close; | |
312 | return 0; | |
313 | } | |
314 | $dh->close; | |
315 | return 1; | |
316 | }; | |
317 | ||
318 | my $journalhash = get_ceph_journals(); | |
319 | ||
320 | my $zfslist = get_zfs_devices(); | |
321 | ||
322 | my $lvmlist = get_lvm_devices(); | |
323 | ||
324 | dir_glob_foreach('/sys/block', '.*', sub { | |
325 | my ($dev) = @_; | |
326 | return if defined($disk) && $disk ne $dev; | |
327 | # whitelisting following devices | |
328 | # hdX: ide block device | |
329 | # sdX: sd block device | |
330 | # vdX: virtual block device | |
331 | # xvdX: xen virtual block device | |
332 | # nvmeXnY: nvme devices | |
333 | # cXnY: cciss devices | |
334 | return if $dev !~ m/^(h|s|x?v)d[a-z]+$/ && | |
335 | $dev !~ m/^nvme\d+n\d+$/ && | |
336 | $dev !~ m/^c\d+d\d+$/; | |
337 | ||
338 | my $data = get_udev_info($dev); | |
339 | return if !defined($data); | |
340 | my $devpath = $data->{devpath}; | |
341 | ||
342 | my $sysdir = "/sys/block/$dev"; | |
343 | ||
344 | return if ! -d "$sysdir/device"; | |
345 | ||
346 | # we do not want iscsi devices | |
347 | return if readlink($sysdir) =~ m|host[^/]*/session[^/]*|; | |
348 | ||
349 | my $sysdata = get_sysdir_info($sysdir); | |
350 | return if !defined($sysdata); | |
351 | ||
352 | my $type = 'unknown'; | |
353 | ||
354 | if ($sysdata->{rotational} == 0) { | |
355 | $type = 'ssd'; | |
356 | $data->{rpm} = 0; | |
357 | } elsif ($sysdata->{rotational} == 1) { | |
358 | if ($data->{rpm} != -1) { | |
359 | $type = 'hdd'; | |
360 | } elsif ($data->{usb}) { | |
361 | $type = 'usb'; | |
362 | $data->{rpm} = 0; | |
363 | } | |
364 | } | |
365 | ||
acd3d916 | 366 | my $health = 'UNKNOWN'; |
cbba9b5b | 367 | my $wearout; |
7a98a62d FG |
368 | |
369 | if (!$nosmart) { | |
370 | eval { | |
371 | if ($type eq 'ssd') { | |
372 | # if we have an ssd we try to get the wearout indicator | |
373 | $wearout = 'N/A'; | |
374 | my $smartdata = get_smart_data($devpath); | |
375 | $health = $smartdata->{health}; | |
376 | foreach my $attr (@{$smartdata->{attributes}}) { | |
377 | # ID 233 is media wearout indicator on intel and sandisk | |
378 | # ID 177 is media wearout indicator on samsung | |
379 | next if ($attr->{id} != 233 && $attr->{id} != 177); | |
380 | next if ($attr->{name} !~ m/wear/i); | |
381 | $wearout = $attr->{value}; | |
382 | ||
383 | # prefer the 233 value | |
384 | last if ($attr->{id} == 233); | |
385 | } | |
386 | } else { | |
387 | # else we just get the health | |
388 | $health = get_smart_health($devpath); | |
acd3d916 | 389 | } |
7a98a62d FG |
390 | }; |
391 | } | |
cbba9b5b DC |
392 | |
393 | my $used; | |
394 | ||
395 | $used = 'LVM' if $lvmlist->{$devpath}; | |
396 | ||
397 | $used = 'mounted' if &$dev_is_mounted($devpath); | |
398 | ||
399 | $used = 'ZFS' if $zfslist->{$devpath}; | |
400 | ||
401 | $disklist->{$dev} = { | |
402 | vendor => $sysdata->{vendor}, | |
403 | model => $sysdata->{model}, | |
404 | size => $sysdata->{size}, | |
405 | serial => $data->{serial}, | |
406 | gpt => $data->{gpt}, | |
407 | rpm => $data->{rpm}, | |
408 | type => $type, | |
409 | wwn => $data->{wwn}, | |
410 | health => $health, | |
411 | devpath => $devpath, | |
412 | wearout => $wearout, | |
413 | }; | |
414 | ||
415 | my $osdid = -1; | |
416 | ||
417 | my $journal_count = 0; | |
418 | ||
419 | my $found_partitions; | |
420 | my $found_lvm; | |
421 | my $found_mountpoints; | |
422 | my $found_zfs; | |
423 | my $found_dm; | |
424 | my $partpath = $devpath; | |
425 | ||
426 | # remove part after last / to | |
427 | # get the base path for the partitions | |
428 | # e.g. from /dev/cciss/c0d0 get /dev/cciss | |
429 | $partpath =~ s/\/[^\/]+$//; | |
430 | ||
431 | dir_glob_foreach("$sysdir", "$dev.+", sub { | |
432 | my ($part) = @_; | |
433 | ||
434 | $found_partitions = 1; | |
435 | ||
436 | if (my $mp = &$dev_is_mounted("$partpath/$part")) { | |
437 | $found_mountpoints = 1; | |
438 | if ($mp =~ m|^/var/lib/ceph/osd/ceph-(\d+)$|) { | |
439 | $osdid = $1; | |
440 | } | |
441 | } | |
442 | ||
443 | if ($lvmlist->{"$partpath/$part"}) { | |
444 | $found_lvm = 1; | |
445 | } | |
446 | ||
447 | if ($zfslist->{"$partpath/$part"}) { | |
448 | $found_zfs = 1; | |
449 | } | |
450 | ||
451 | $journal_count++ if $journalhash->{"$partpath/$part"}; | |
452 | ||
453 | if (!&$dir_is_empty("$sysdir/$part/holders") && !$found_lvm) { | |
454 | $found_dm = 1; | |
455 | } | |
456 | }); | |
457 | ||
458 | $used = 'mounted' if $found_mountpoints && !$used; | |
459 | $used = 'LVM' if $found_lvm && !$used; | |
460 | $used = 'ZFS' if $found_zfs && !$used; | |
461 | $used = 'Device Mapper' if $found_dm && !$used; | |
462 | $used = 'partitions' if $found_partitions && !$used; | |
463 | ||
464 | # multipath, software raid, etc. | |
465 | # this check comes in last, to show more specific info | |
466 | # if we have it | |
467 | $used = 'Device Mapper' if !$used && !&$dir_is_empty("$sysdir/holders"); | |
468 | ||
469 | $disklist->{$dev}->{used} = $used if $used; | |
470 | $disklist->{$dev}->{osdid} = $osdid; | |
471 | $disklist->{$dev}->{journals} = $journal_count; | |
472 | }); | |
473 | ||
474 | return $disklist; | |
475 | ||
476 | } | |
477 | ||
478 | 1; |