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