]>
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); | |
3196c387 | 8 | use Fcntl ':mode'; |
8cd6d7e8 | 9 | use JSON; |
cbba9b5b DC |
10 | |
11 | use PVE::Tools qw(extract_param run_command file_get_contents file_read_firstline dir_glob_regex dir_glob_foreach trim); | |
12 | ||
13 | my $SMARTCTL = "/usr/sbin/smartctl"; | |
14 | my $ZPOOL = "/sbin/zpool"; | |
15 | my $SGDISK = "/sbin/sgdisk"; | |
16 | my $PVS = "/sbin/pvs"; | |
19dcd1ad | 17 | my $LVS = "/sbin/lvs"; |
cbba9b5b | 18 | my $UDEVADM = "/bin/udevadm"; |
8cd6d7e8 | 19 | my $LSBLK = "/bin/lsblk"; |
cbba9b5b DC |
20 | |
21 | sub verify_blockdev_path { | |
22 | my ($rel_path) = @_; | |
23 | ||
24 | die "missing path" if !$rel_path; | |
25 | my $path = abs_path($rel_path); | |
26 | die "failed to get absolute path to $rel_path\n" if !$path; | |
27 | ||
28 | die "got unusual device path '$path'\n" if $path !~ m|^/dev/(.*)$|; | |
29 | ||
30 | $path = "/dev/$1"; # untaint | |
31 | ||
32 | assert_blockdev($path); | |
33 | ||
34 | return $path; | |
35 | } | |
36 | ||
37 | sub assert_blockdev { | |
38 | my ($dev, $noerr) = @_; | |
39 | ||
40 | if ($dev !~ m|^/dev/| || !(-b $dev)) { | |
41 | return undef if $noerr; | |
42 | die "not a valid block device\n"; | |
43 | } | |
44 | ||
45 | return 1; | |
46 | } | |
47 | ||
48 | sub init_disk { | |
49 | my ($disk, $uuid) = @_; | |
50 | ||
51 | assert_blockdev($disk); | |
52 | ||
53 | # we should already have checked if it is in use in the api call | |
54 | # but we check again for safety | |
55 | die "disk $disk is already in use\n" if disk_is_used($disk); | |
56 | ||
57 | my $id = $uuid || 'R'; | |
58 | run_command([$SGDISK, $disk, '-U', $id]); | |
59 | return 1; | |
60 | } | |
61 | ||
62 | sub disk_is_used { | |
63 | my ($disk) = @_; | |
64 | ||
65 | my $dev = $disk; | |
66 | $dev =~ s|^/dev/||; | |
67 | ||
7a98a62d | 68 | my $disklist = get_disks($dev, 1); |
cbba9b5b DC |
69 | |
70 | die "'$disk' is not a valid local disk\n" if !defined($disklist->{$dev}); | |
71 | return 1 if $disklist->{$dev}->{used}; | |
72 | ||
73 | return 0; | |
74 | } | |
75 | ||
76 | sub get_smart_data { | |
dd902da7 | 77 | my ($disk, $healthonly) = @_; |
cbba9b5b DC |
78 | |
79 | assert_blockdev($disk); | |
80 | my $smartdata = {}; | |
dc1311cb | 81 | my $type; |
cbba9b5b | 82 | |
9018a4e6 | 83 | my $returncode = 0; |
c9bd3d22 | 84 | |
c3442aa5 WB |
85 | if ($disk =~ m!^/dev/(nvme\d+n\d+)$!) { |
86 | my $info = get_sysdir_info("/sys/block/$1"); | |
87 | $disk = "/dev/".($info->{device} | |
88 | or die "failed to get nvme controller device for $disk\n"); | |
89 | } | |
c9bd3d22 | 90 | |
dd902da7 DC |
91 | my $cmd = [$SMARTCTL, '-H']; |
92 | push @$cmd, '-A', '-f', 'brief' if !$healthonly; | |
93 | push @$cmd, $disk; | |
94 | ||
cbba9b5b | 95 | eval { |
dd902da7 | 96 | $returncode = run_command($cmd, noerr => 1, outfunc => sub{ |
cbba9b5b DC |
97 | my ($line) = @_; |
98 | ||
1c999553 FG |
99 | # ATA SMART attributes, e.g.: |
100 | # ID# ATTRIBUTE_NAME FLAGS VALUE WORST THRESH FAIL RAW_VALUE | |
101 | # 1 Raw_Read_Error_Rate POSR-K 100 100 000 - 0 | |
dc1311cb FG |
102 | # |
103 | # SAS and NVME disks, e.g.: | |
104 | # Data Units Written: 5,584,952 [2.85 TB] | |
105 | # Accumulated start-stop cycles: 34 | |
106 | ||
bd54091c | 107 | if (defined($type) && $type eq 'ata' && $line =~ m/^([ \d]{2}\d)\s+(\S+)\s+(\S{6})\s+(\d+)\s+(\d+)\s+(\S+)\s+(\S+)\s+(.*)$/) { |
cbba9b5b | 108 | my $entry = {}; |
bd54091c DC |
109 | |
110 | ||
1c999553 FG |
111 | $entry->{name} = $2 if defined $2; |
112 | $entry->{flags} = $3 if defined $3; | |
cbba9b5b | 113 | # the +0 makes a number out of the strings |
1c999553 FG |
114 | $entry->{value} = $4+0 if defined $4; |
115 | $entry->{worst} = $5+0 if defined $5; | |
bd54091c DC |
116 | # some disks report the default threshold as --- instead of 000 |
117 | if (defined($6) && $6 eq '---') { | |
118 | $entry->{threshold} = 0; | |
119 | } else { | |
120 | $entry->{threshold} = $6+0 if defined $6; | |
121 | } | |
1c999553 FG |
122 | $entry->{fail} = $7 if defined $7; |
123 | $entry->{raw} = $8 if defined $8; | |
124 | $entry->{id} = $1 if defined $1; | |
cbba9b5b | 125 | push @{$smartdata->{attributes}}, $entry; |
5db2d529 | 126 | } elsif ($line =~ m/(?:Health Status|self\-assessment test result): (.*)$/ ) { |
cbba9b5b DC |
127 | $smartdata->{health} = $1; |
128 | } elsif ($line =~ m/Vendor Specific SMART Attributes with Thresholds:/) { | |
dc1311cb FG |
129 | $type = 'ata'; |
130 | delete $smartdata->{text}; | |
131 | } elsif ($line =~ m/=== START OF (READ )?SMART DATA SECTION ===/) { | |
132 | $type = 'text'; | |
133 | } elsif (defined($type) && $type eq 'text') { | |
134 | $smartdata->{text} = '' if !defined $smartdata->{text}; | |
135 | $smartdata->{text} .= "$line\n"; | |
ea928fd4 DC |
136 | # extract wearout from nvme text, allow for decimal values |
137 | if ($line =~ m/Percentage Used:.*(\d+(?:\.\d+)?)\%/i) { | |
138 | $smartdata->{wearout} = 100 - $1; | |
139 | } | |
dd902da7 DC |
140 | } elsif ($line =~ m/SMART Disabled/) { |
141 | $smartdata->{health} = "SMART Disabled"; | |
cbba9b5b DC |
142 | } |
143 | }); | |
144 | }; | |
9018a4e6 DC |
145 | my $err = $@; |
146 | ||
147 | # bit 0 and 1 mark an severe smartctl error | |
148 | # all others are for disk status, so ignore them | |
149 | # see smartctl(8) | |
150 | if ((defined($returncode) && ($returncode & 0b00000011)) || $err) { | |
151 | die "Error getting S.M.A.R.T. data: Exit code: $returncode\n"; | |
152 | } | |
dc1311cb FG |
153 | |
154 | $smartdata->{type} = $type; | |
155 | ||
cbba9b5b DC |
156 | return $smartdata; |
157 | } | |
158 | ||
8cd6d7e8 DC |
159 | sub get_parttype_info() { |
160 | my $cmd = [$LSBLK, '--json', '-o', 'path,parttype']; | |
161 | my $output = ""; | |
162 | my $res = {}; | |
163 | eval { | |
164 | run_command($cmd, outfunc => sub { | |
165 | my ($line) = @_; | |
166 | $output .= "$line\n"; | |
167 | }); | |
168 | }; | |
169 | warn "$@\n" if $@; | |
170 | return $res if $output eq ''; | |
171 | ||
172 | my $parsed = eval { decode_json($output) }; | |
173 | warn "$@\n" if $@; | |
174 | my $list = $parsed->{blockdevices} // []; | |
175 | ||
176 | foreach my $dev (@$list) { | |
177 | next if !($dev->{parttype}); | |
178 | my $type = $dev->{parttype}; | |
179 | $res->{$type} = [] if !defined($res->{$type}); | |
180 | push @{$res->{$type}}, $dev->{path}; | |
181 | } | |
182 | ||
183 | return $res; | |
184 | } | |
185 | ||
186 | my $get_devices_by_partuuid = sub { | |
187 | my ($parttype_map, $uuids, $res) = @_; | |
188 | ||
189 | $res = {} if !defined($res); | |
190 | ||
191 | foreach my $uuid (sort keys %$uuids) { | |
192 | map { $res->{$_} = $uuids->{$uuid} } @{$parttype_map->{$uuid}}; | |
193 | } | |
194 | ||
195 | return $res; | |
196 | }; | |
197 | ||
cbba9b5b | 198 | sub get_zfs_devices { |
8cd6d7e8 DC |
199 | my ($parttype_map) = @_; |
200 | my $res = {}; | |
cbba9b5b | 201 | |
501562d4 | 202 | return {} if ! -x $ZPOOL; |
4526dffa | 203 | |
cbba9b5b DC |
204 | # use zpool and parttype uuid, |
205 | # because log and cache do not have | |
206 | # zfs type uuid | |
207 | eval { | |
208 | run_command([$ZPOOL, 'list', '-HPLv'], outfunc => sub { | |
209 | my ($line) = @_; | |
210 | ||
211 | if ($line =~ m|^\t([^\t]+)\t|) { | |
8cd6d7e8 | 212 | $res->{$1} = 1; |
cbba9b5b DC |
213 | } |
214 | }); | |
215 | }; | |
216 | ||
217 | # only warn here, | |
218 | # because maybe zfs tools are not installed | |
219 | warn "$@\n" if $@; | |
220 | ||
8cd6d7e8 DC |
221 | my $uuids = { |
222 | "6a898cc3-1dd2-11b2-99a6-080020736631" => 1, # apple | |
223 | "516e7cba-6ecf-11d6-8ff8-00022d09712b" => 1, # bsd | |
224 | }; | |
cbba9b5b | 225 | |
cbba9b5b | 226 | |
8cd6d7e8 DC |
227 | $res = $get_devices_by_partuuid->($parttype_map, $uuids, $res); |
228 | ||
229 | return $res; | |
cbba9b5b DC |
230 | } |
231 | ||
232 | sub get_lvm_devices { | |
8cd6d7e8 DC |
233 | my ($parttype_map) = @_; |
234 | my $res = {}; | |
cbba9b5b DC |
235 | eval { |
236 | run_command([$PVS, '--noheadings', '--readonly', '-o', 'pv_name'], outfunc => sub{ | |
237 | my ($line) = @_; | |
238 | $line = trim($line); | |
239 | if ($line =~ m|^/dev/|) { | |
8cd6d7e8 | 240 | $res->{$line} = 1; |
cbba9b5b DC |
241 | } |
242 | }); | |
243 | }; | |
244 | ||
245 | # if something goes wrong, we do not want | |
246 | # to give up, but indicate an error has occured | |
247 | warn "$@\n" if $@; | |
248 | ||
8cd6d7e8 DC |
249 | my $uuids = { |
250 | "e6d6d379-f507-44c2-a23c-238f2a3df928" => 1, | |
251 | }; | |
cbba9b5b | 252 | |
8cd6d7e8 | 253 | $res = $get_devices_by_partuuid->($parttype_map, $uuids, $res); |
cbba9b5b | 254 | |
8cd6d7e8 | 255 | return $res; |
cbba9b5b DC |
256 | } |
257 | ||
258 | sub get_ceph_journals { | |
8cd6d7e8 DC |
259 | my ($parttype_map) = @_; |
260 | my $res = {}; | |
261 | ||
262 | my $uuids = { | |
263 | '45b0969e-9b03-4f30-b4c6-b4b80ceff106' => 1, # journal | |
264 | '30cd0809-c2b2-499c-8879-2d6b78529876' => 2, # db | |
265 | '5ce17fce-4087-4169-b7ff-056cc58473f9' => 3, # wal | |
266 | 'cafecafe-9b03-4f30-b4c6-b4b80ceff106' => 4, # block | |
267 | }; | |
268 | ||
269 | $res = $get_devices_by_partuuid->($parttype_map, $uuids, $res); | |
cbba9b5b | 270 | |
8cd6d7e8 | 271 | return $res; |
cbba9b5b DC |
272 | } |
273 | ||
19dcd1ad DC |
274 | # reads the lv_tags and matches them with the devices |
275 | sub get_ceph_volume_infos { | |
276 | my $result = {}; | |
277 | ||
248f43f5 TL |
278 | my $cmd = [ $LVS, '-S', 'lv_name=~^osd-', '-o', 'devices,lv_name,lv_tags', |
279 | '--noheadings', '--readonly', '--separator', ';' ]; | |
19dcd1ad DC |
280 | |
281 | run_command($cmd, outfunc => sub { | |
282 | my $line = shift; | |
248f43f5 TL |
283 | $line =~ s/(?:^\s+)|(?:\s+$)//g; # trim whitespaces |
284 | ||
285 | my $fields = [ split(';', $line) ]; | |
19dcd1ad DC |
286 | |
287 | # lvs syntax is /dev/sdX(Y) where Y is the start (which we do not need) | |
288 | my ($dev) = $fields->[0] =~ m|^(/dev/[a-z]+)|; | |
289 | if ($fields->[1] =~ m|^osd-([^-]+)-|) { | |
290 | my $type = $1; | |
248f43f5 | 291 | # $result autovivification is wanted, to not creating empty hashes |
79f4a7bf | 292 | if (($type eq 'block' || $type eq 'data') && $fields->[2] =~ m/ceph.osd_id=([^,]+)/) { |
19dcd1ad DC |
293 | $result->{$dev}->{osdid} = $1; |
294 | $result->{$dev}->{bluestore} = ($type eq 'block'); | |
295 | } else { | |
248f43f5 | 296 | # undef++ becomes '1' (see `perldoc perlop`: Auto-increment) |
19dcd1ad DC |
297 | $result->{$dev}->{$type}++; |
298 | } | |
299 | } | |
300 | }); | |
301 | ||
302 | return $result; | |
303 | } | |
304 | ||
cbba9b5b DC |
305 | sub get_udev_info { |
306 | my ($dev) = @_; | |
307 | ||
308 | my $info = ""; | |
309 | my $data = {}; | |
310 | eval { | |
532e89e7 | 311 | run_command([$UDEVADM, 'info', '-p', $dev, '--query', 'all'], outfunc => sub { |
cbba9b5b DC |
312 | my ($line) = @_; |
313 | $info .= "$line\n"; | |
314 | }); | |
315 | }; | |
316 | warn $@ if $@; | |
317 | return undef if !$info; | |
318 | ||
319 | return undef if $info !~ m/^E: DEVTYPE=disk$/m; | |
320 | return undef if $info =~ m/^E: ID_CDROM/m; | |
321 | ||
322 | # we use this, because some disks are not simply in /dev | |
323 | # e.g. /dev/cciss/c0d0 | |
324 | if ($info =~ m/^E: DEVNAME=(\S+)$/m) { | |
325 | $data->{devpath} = $1; | |
326 | } | |
327 | return if !defined($data->{devpath}); | |
328 | ||
329 | $data->{serial} = 'unknown'; | |
330 | if ($info =~ m/^E: ID_SERIAL_SHORT=(\S+)$/m) { | |
331 | $data->{serial} = $1; | |
332 | } | |
333 | ||
334 | $data->{gpt} = 0; | |
335 | if ($info =~ m/^E: ID_PART_TABLE_TYPE=gpt$/m) { | |
336 | $data->{gpt} = 1; | |
337 | } | |
338 | ||
339 | # detect SSD | |
340 | $data->{rpm} = -1; | |
341 | if ($info =~ m/^E: ID_ATA_ROTATION_RATE_RPM=(\d+)$/m) { | |
342 | $data->{rpm} = $1; | |
343 | } | |
344 | ||
345 | if ($info =~ m/^E: ID_BUS=usb$/m) { | |
346 | $data->{usb} = 1; | |
347 | } | |
348 | ||
865bdbd9 DC |
349 | if ($info =~ m/^E: ID_MODEL=(.+)$/m) { |
350 | $data->{model} = $1; | |
351 | } | |
352 | ||
cbba9b5b DC |
353 | $data->{wwn} = 'unknown'; |
354 | if ($info =~ m/^E: ID_WWN=(.*)$/m) { | |
355 | $data->{wwn} = $1; | |
356 | } | |
357 | ||
358 | return $data; | |
359 | } | |
360 | ||
361 | sub get_sysdir_info { | |
362 | my ($sysdir) = @_; | |
363 | ||
461a9fd8 DC |
364 | return undef if ! -d "$sysdir/device"; |
365 | ||
cbba9b5b DC |
366 | my $data = {}; |
367 | ||
368 | my $size = file_read_firstline("$sysdir/size"); | |
369 | return undef if !$size; | |
370 | ||
371 | # linux always considers sectors to be 512 bytes, | |
372 | # independently of real block size | |
373 | $data->{size} = $size * 512; | |
374 | ||
375 | # dir/queue/rotational should be 1 for hdd, 0 for ssd | |
571b6f26 | 376 | $data->{rotational} = file_read_firstline("$sysdir/queue/rotational") // -1; |
cbba9b5b DC |
377 | |
378 | $data->{vendor} = file_read_firstline("$sysdir/device/vendor") || 'unknown'; | |
379 | $data->{model} = file_read_firstline("$sysdir/device/model") || 'unknown'; | |
380 | ||
c3442aa5 WB |
381 | if (defined(my $device = readlink("$sysdir/device"))) { |
382 | # strip directory and untaint: | |
383 | ($data->{device}) = $device =~ m!([^/]+)$!; | |
384 | } | |
385 | ||
cbba9b5b DC |
386 | return $data; |
387 | } | |
388 | ||
6965a670 | 389 | sub get_wear_leveling_info { |
ea928fd4 DC |
390 | my ($smartdata, $model) = @_; |
391 | my $attributes = $smartdata->{attributes}; | |
392 | ||
393 | if (defined($smartdata->{wearout})) { | |
394 | return $smartdata->{wearout}; | |
395 | } | |
6965a670 DC |
396 | |
397 | my $wearout; | |
398 | ||
399 | my $vendormap = { | |
400 | 'kingston' => 231, | |
401 | 'samsung' => 177, | |
402 | 'intel' => 233, | |
403 | 'sandisk' => 233, | |
7b32e2a7 | 404 | 'crucial' => 202, |
6965a670 DC |
405 | 'default' => 233, |
406 | }; | |
407 | ||
408 | # find target attr id | |
409 | ||
410 | my $attrid; | |
411 | ||
d57fb43d | 412 | foreach my $vendor (keys %$vendormap) { |
6965a670 DC |
413 | if ($model =~ m/$vendor/i) { |
414 | $attrid = $vendormap->{$vendor}; | |
415 | # found the attribute | |
416 | last; | |
417 | } | |
418 | } | |
419 | ||
420 | if (!$attrid) { | |
421 | $attrid = $vendormap->{default}; | |
422 | } | |
423 | ||
424 | foreach my $attr (@$attributes) { | |
425 | next if $attr->{id} != $attrid; | |
426 | $wearout = $attr->{value}; | |
427 | last; | |
428 | } | |
429 | ||
430 | return $wearout; | |
431 | } | |
432 | ||
10a48db5 DC |
433 | sub dir_is_empty { |
434 | my ($dir) = @_; | |
435 | ||
436 | my $dh = IO::Dir->new ($dir); | |
437 | return 1 if !$dh; | |
438 | ||
439 | while (defined(my $tmp = $dh->read)) { | |
440 | next if $tmp eq '.' || $tmp eq '..'; | |
441 | $dh->close; | |
442 | return 0; | |
443 | } | |
444 | $dh->close; | |
445 | return 1; | |
446 | } | |
447 | ||
eebcdb11 DC |
448 | sub is_iscsi { |
449 | my ($sysdir) = @_; | |
450 | ||
451 | if (-l $sysdir && readlink($sysdir) =~ m|host[^/]*/session[^/]*|) { | |
452 | return 1; | |
453 | } | |
454 | ||
455 | return 0; | |
456 | } | |
457 | ||
cbba9b5b | 458 | sub get_disks { |
52a064af | 459 | my ($disks, $nosmart) = @_; |
cbba9b5b DC |
460 | my $disklist = {}; |
461 | ||
462 | my $mounted = {}; | |
463 | ||
464 | my $mounts = PVE::ProcFSTools::parse_proc_mounts(); | |
465 | ||
466 | foreach my $mount (@$mounts) { | |
467 | next if $mount->[0] !~ m|^/dev/|; | |
468 | $mounted->{abs_path($mount->[0])} = $mount->[1]; | |
469 | }; | |
470 | ||
471 | my $dev_is_mounted = sub { | |
472 | my ($dev) = @_; | |
473 | return $mounted->{$dev}; | |
474 | }; | |
475 | ||
8cd6d7e8 DC |
476 | my $parttype_map = get_parttype_info(); |
477 | ||
478 | my $journalhash = get_ceph_journals($parttype_map); | |
19dcd1ad | 479 | my $ceph_volume_infos = get_ceph_volume_infos(); |
cbba9b5b | 480 | |
8cd6d7e8 | 481 | my $zfshash = get_zfs_devices($parttype_map); |
cbba9b5b | 482 | |
8cd6d7e8 | 483 | my $lvmhash = get_lvm_devices($parttype_map); |
cbba9b5b | 484 | |
52a064af DC |
485 | my $disk_regex = ".*"; |
486 | if (defined($disks)) { | |
487 | if (!ref($disks)) { | |
488 | $disks = [ $disks ]; | |
489 | } elsif (ref($disks) ne 'ARRAY') { | |
490 | die "disks is not a string or array reference\n"; | |
491 | } | |
492 | # we get cciss/c0d0 but need cciss!c0d0 | |
493 | map { s|cciss/|cciss!| } @$disks; | |
494 | ||
495 | $disk_regex = "(?:" . join('|', @$disks) . ")"; | |
1590fc13 DC |
496 | } |
497 | ||
52a064af | 498 | dir_glob_foreach('/sys/block', $disk_regex, sub { |
cbba9b5b | 499 | my ($dev) = @_; |
cbba9b5b DC |
500 | # whitelisting following devices |
501 | # hdX: ide block device | |
502 | # sdX: sd block device | |
503 | # vdX: virtual block device | |
504 | # xvdX: xen virtual block device | |
505 | # nvmeXnY: nvme devices | |
38ddd4ce | 506 | # cciss!cXnY: cciss devices |
cbba9b5b DC |
507 | return if $dev !~ m/^(h|s|x?v)d[a-z]+$/ && |
508 | $dev !~ m/^nvme\d+n\d+$/ && | |
38ddd4ce | 509 | $dev !~ m/^cciss\!c\d+d\d+$/; |
cbba9b5b | 510 | |
532e89e7 | 511 | my $data = get_udev_info("/sys/block/$dev"); |
cbba9b5b DC |
512 | return if !defined($data); |
513 | my $devpath = $data->{devpath}; | |
514 | ||
515 | my $sysdir = "/sys/block/$dev"; | |
516 | ||
cbba9b5b | 517 | # we do not want iscsi devices |
eebcdb11 | 518 | return if is_iscsi($sysdir); |
cbba9b5b DC |
519 | |
520 | my $sysdata = get_sysdir_info($sysdir); | |
521 | return if !defined($sysdata); | |
522 | ||
523 | my $type = 'unknown'; | |
524 | ||
525 | if ($sysdata->{rotational} == 0) { | |
526 | $type = 'ssd'; | |
527 | $data->{rpm} = 0; | |
528 | } elsif ($sysdata->{rotational} == 1) { | |
529 | if ($data->{rpm} != -1) { | |
530 | $type = 'hdd'; | |
531 | } elsif ($data->{usb}) { | |
532 | $type = 'usb'; | |
533 | $data->{rpm} = 0; | |
534 | } | |
535 | } | |
536 | ||
acd3d916 | 537 | my $health = 'UNKNOWN'; |
6965a670 | 538 | my $wearout = 'N/A'; |
7a98a62d FG |
539 | |
540 | if (!$nosmart) { | |
541 | eval { | |
dd902da7 DC |
542 | my $smartdata = get_smart_data($devpath, ($type ne 'ssd')); |
543 | $health = $smartdata->{health} if $smartdata->{health}; | |
544 | ||
7a98a62d FG |
545 | if ($type eq 'ssd') { |
546 | # if we have an ssd we try to get the wearout indicator | |
c5fa45a9 | 547 | my $wearval = get_wear_leveling_info($smartdata, $data->{model} || $sysdata->{model}); |
9250ddfe | 548 | $wearout = $wearval if defined($wearval); |
acd3d916 | 549 | } |
7a98a62d FG |
550 | }; |
551 | } | |
cbba9b5b DC |
552 | |
553 | my $used; | |
554 | ||
8cd6d7e8 | 555 | $used = 'LVM' if $lvmhash->{$devpath}; |
cbba9b5b DC |
556 | |
557 | $used = 'mounted' if &$dev_is_mounted($devpath); | |
558 | ||
8cd6d7e8 | 559 | $used = 'ZFS' if $zfshash->{$devpath}; |
cbba9b5b | 560 | |
fc7c0e05 DC |
561 | # we replaced cciss/ with cciss! above |
562 | # but in the result we need cciss/ again | |
563 | # because the caller might want to check the | |
564 | # result again with the original parameter | |
565 | if ($dev =~ m|^cciss!|) { | |
566 | $dev =~ s|^cciss!|cciss/|; | |
567 | } | |
568 | ||
cbba9b5b DC |
569 | $disklist->{$dev} = { |
570 | vendor => $sysdata->{vendor}, | |
865bdbd9 | 571 | model => $data->{model} || $sysdata->{model}, |
cbba9b5b DC |
572 | size => $sysdata->{size}, |
573 | serial => $data->{serial}, | |
574 | gpt => $data->{gpt}, | |
575 | rpm => $data->{rpm}, | |
576 | type => $type, | |
577 | wwn => $data->{wwn}, | |
578 | health => $health, | |
579 | devpath => $devpath, | |
580 | wearout => $wearout, | |
581 | }; | |
582 | ||
583 | my $osdid = -1; | |
e2bd817c | 584 | my $bluestore = 0; |
cbba9b5b DC |
585 | |
586 | my $journal_count = 0; | |
e2bd817c DC |
587 | my $db_count = 0; |
588 | my $wal_count = 0; | |
cbba9b5b DC |
589 | |
590 | my $found_partitions; | |
591 | my $found_lvm; | |
592 | my $found_mountpoints; | |
593 | my $found_zfs; | |
594 | my $found_dm; | |
595 | my $partpath = $devpath; | |
596 | ||
597 | # remove part after last / to | |
598 | # get the base path for the partitions | |
599 | # e.g. from /dev/cciss/c0d0 get /dev/cciss | |
600 | $partpath =~ s/\/[^\/]+$//; | |
601 | ||
602 | dir_glob_foreach("$sysdir", "$dev.+", sub { | |
603 | my ($part) = @_; | |
604 | ||
605 | $found_partitions = 1; | |
606 | ||
607 | if (my $mp = &$dev_is_mounted("$partpath/$part")) { | |
608 | $found_mountpoints = 1; | |
609 | if ($mp =~ m|^/var/lib/ceph/osd/ceph-(\d+)$|) { | |
610 | $osdid = $1; | |
611 | } | |
612 | } | |
613 | ||
8cd6d7e8 | 614 | if ($lvmhash->{"$partpath/$part"}) { |
cbba9b5b DC |
615 | $found_lvm = 1; |
616 | } | |
617 | ||
8cd6d7e8 | 618 | if ($zfshash->{"$partpath/$part"}) { |
cbba9b5b DC |
619 | $found_zfs = 1; |
620 | } | |
621 | ||
0180fa42 TL |
622 | if (my $journal_part = $journalhash->{"$partpath/$part"}) { |
623 | $journal_count++ if $journal_part == 1; | |
624 | $db_count++ if $journal_part == 2; | |
625 | $wal_count++ if $journal_part == 3; | |
626 | $bluestore = 1 if $journal_part == 4; | |
e2bd817c | 627 | } |
cbba9b5b | 628 | |
10a48db5 | 629 | if (!dir_is_empty("$sysdir/$part/holders") && !$found_lvm) { |
cbba9b5b DC |
630 | $found_dm = 1; |
631 | } | |
632 | }); | |
633 | ||
0180fa42 TL |
634 | if (my $ceph_volume = $ceph_volume_infos->{$devpath}) { |
635 | $journal_count += $ceph_volume->{journal} // 0; | |
636 | $db_count += $ceph_volume->{db} // 0; | |
637 | $wal_count += $ceph_volume->{wal} // 0; | |
35ccd3d0 | 638 | if (defined($ceph_volume->{osdid})) { |
0180fa42 TL |
639 | $osdid = $ceph_volume->{osdid}; |
640 | $bluestore = 1 if $ceph_volume->{bluestore}; | |
19dcd1ad DC |
641 | } |
642 | } | |
643 | ||
cbba9b5b DC |
644 | $used = 'mounted' if $found_mountpoints && !$used; |
645 | $used = 'LVM' if $found_lvm && !$used; | |
646 | $used = 'ZFS' if $found_zfs && !$used; | |
647 | $used = 'Device Mapper' if $found_dm && !$used; | |
648 | $used = 'partitions' if $found_partitions && !$used; | |
649 | ||
650 | # multipath, software raid, etc. | |
651 | # this check comes in last, to show more specific info | |
652 | # if we have it | |
10a48db5 | 653 | $used = 'Device Mapper' if !$used && !dir_is_empty("$sysdir/holders"); |
cbba9b5b DC |
654 | |
655 | $disklist->{$dev}->{used} = $used if $used; | |
656 | $disklist->{$dev}->{osdid} = $osdid; | |
e2bd817c DC |
657 | $disklist->{$dev}->{journals} = $journal_count if $journal_count; |
658 | $disklist->{$dev}->{bluestore} = $bluestore if $osdid != -1; | |
659 | $disklist->{$dev}->{db} = $db_count if $db_count; | |
660 | $disklist->{$dev}->{wal} = $wal_count if $wal_count; | |
cbba9b5b DC |
661 | }); |
662 | ||
663 | return $disklist; | |
664 | ||
665 | } | |
666 | ||
3196c387 WL |
667 | sub get_partnum { |
668 | my ($part_path) = @_; | |
669 | ||
670 | my ($mode, $rdev) = (stat($part_path))[2,6]; | |
671 | ||
672 | next if !$mode || !S_ISBLK($mode) || !$rdev; | |
f2e5018e WB |
673 | my $major = PVE::Tools::dev_t_major($rdev); |
674 | my $minor = PVE::Tools::dev_t_minor($rdev); | |
3196c387 WL |
675 | my $partnum_path = "/sys/dev/block/$major:$minor/"; |
676 | ||
677 | my $partnum; | |
678 | ||
679 | $partnum = file_read_firstline("${partnum_path}partition"); | |
680 | ||
681 | die "Partition does not exists\n" if !defined($partnum); | |
682 | ||
683 | #untaint and ensure it is a int | |
684 | if ($partnum =~ m/(\d+)/) { | |
685 | $partnum = $1; | |
686 | die "Partition number $partnum is invalid\n" if $partnum > 128; | |
687 | } else { | |
688 | die "Failed to get partition number\n"; | |
689 | } | |
690 | ||
691 | return $partnum; | |
692 | } | |
693 | ||
0d28307d WL |
694 | sub get_blockdev { |
695 | my ($part_path) = @_; | |
696 | ||
697 | my $dev = $1 if $part_path =~ m|^/dev/(.*)$|; | |
698 | my $link = readlink "/sys/class/block/$dev"; | |
699 | my $block_dev = $1 if $link =~ m|([^/]*)/$dev$|; | |
700 | ||
701 | die "Can't parse parent device\n" if !defined($block_dev); | |
702 | die "No valid block device\n" if index($dev, $block_dev) == -1; | |
703 | ||
704 | $block_dev = "/dev/$block_dev"; | |
705 | die "Block device does not exsists\n" if !(-b $block_dev); | |
706 | ||
707 | return $block_dev; | |
708 | } | |
709 | ||
e39e8ee2 DC |
710 | sub locked_disk_action { |
711 | my ($sub) = @_; | |
712 | my $res = PVE::Tools::lock_file('/run/lock/pve-diskmanage.lck', undef, $sub); | |
713 | die $@ if $@; | |
714 | return $res; | |
715 | } | |
716 | ||
0370861c | 717 | sub assert_disk_unused { |
76c1e57b DC |
718 | my ($dev) = @_; |
719 | ||
0370861c | 720 | die "device '$dev' is already in use\n" if disk_is_used($dev); |
76c1e57b DC |
721 | |
722 | return undef; | |
723 | } | |
724 | ||
1dc3038d DC |
725 | sub append_partition { |
726 | my ($dev, $size) = @_; | |
727 | ||
728 | my $devname = $dev; | |
729 | $devname =~ s|^/dev/||; | |
730 | ||
731 | my $newpartid = 1; | |
732 | dir_glob_foreach("/sys/block/$devname", qr/\Q$devname\E.*?(\d+)/, sub { | |
733 | my ($part, $partid) = @_; | |
734 | ||
735 | if ($partid >= $newpartid) { | |
736 | $newpartid = $partid + 1; | |
737 | } | |
738 | }); | |
739 | ||
740 | $size = PVE::Tools::convert_size($size, 'b' => 'mb'); | |
741 | ||
742 | run_command([ $SGDISK, '-n', "$newpartid:0:+${size}M", $dev ], | |
743 | errmsg => "error creating partition '$newpartid' on '$dev'"); | |
744 | ||
745 | my $partition; | |
746 | ||
747 | # loop again to detect the real partiton device which does not always follow | |
748 | # a strict $devname$partition scheme like /dev/nvme0n1 -> /dev/nvme0n1p1 | |
749 | dir_glob_foreach("/sys/block/$devname", qr/\Q$devname\E.*$newpartid/, sub { | |
750 | my ($part) = @_; | |
751 | ||
752 | $partition = "/dev/$part"; | |
753 | }); | |
754 | ||
755 | return $partition; | |
756 | } | |
757 | ||
cbba9b5b | 758 | 1; |