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