]>
Commit | Line | Data |
---|---|---|
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 | use Fcntl ':mode'; | |
9 | use JSON; | |
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"; | |
17 | my $LVS = "/sbin/lvs"; | |
18 | my $UDEVADM = "/bin/udevadm"; | |
19 | my $LSBLK = "/bin/lsblk"; | |
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 | ||
68 | my $disklist = get_disks($dev, 1); | |
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 { | |
77 | my ($disk, $healthonly) = @_; | |
78 | ||
79 | assert_blockdev($disk); | |
80 | my $smartdata = {}; | |
81 | my $type; | |
82 | ||
83 | my $returncode = 0; | |
84 | ||
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 | } | |
90 | ||
91 | my $cmd = [$SMARTCTL, '-H']; | |
92 | push @$cmd, '-A', '-f', 'brief' if !$healthonly; | |
93 | push @$cmd, $disk; | |
94 | ||
95 | eval { | |
96 | $returncode = run_command($cmd, noerr => 1, outfunc => sub{ | |
97 | my ($line) = @_; | |
98 | ||
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 | |
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 | ||
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+(.*)$/) { | |
108 | my $entry = {}; | |
109 | ||
110 | ||
111 | $entry->{name} = $2 if defined $2; | |
112 | $entry->{flags} = $3 if defined $3; | |
113 | # the +0 makes a number out of the strings | |
114 | $entry->{value} = $4+0 if defined $4; | |
115 | $entry->{worst} = $5+0 if defined $5; | |
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 | } | |
122 | $entry->{fail} = $7 if defined $7; | |
123 | $entry->{raw} = $8 if defined $8; | |
124 | $entry->{id} = $1 if defined $1; | |
125 | push @{$smartdata->{attributes}}, $entry; | |
126 | } elsif ($line =~ m/(?:Health Status|self\-assessment test result): (.*)$/ ) { | |
127 | $smartdata->{health} = $1; | |
128 | } elsif ($line =~ m/Vendor Specific SMART Attributes with Thresholds:/) { | |
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"; | |
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 | } | |
140 | } elsif ($line =~ m/SMART Disabled/) { | |
141 | $smartdata->{health} = "SMART Disabled"; | |
142 | } | |
143 | }); | |
144 | }; | |
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 | } | |
153 | ||
154 | $smartdata->{type} = $type; | |
155 | ||
156 | return $smartdata; | |
157 | } | |
158 | ||
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 | ||
198 | sub get_zfs_devices { | |
199 | my ($parttype_map) = @_; | |
200 | my $res = {}; | |
201 | ||
202 | return {} if ! -x $ZPOOL; | |
203 | ||
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|) { | |
212 | $res->{$1} = 1; | |
213 | } | |
214 | }); | |
215 | }; | |
216 | ||
217 | # only warn here, | |
218 | # because maybe zfs tools are not installed | |
219 | warn "$@\n" if $@; | |
220 | ||
221 | my $uuids = { | |
222 | "6a898cc3-1dd2-11b2-99a6-080020736631" => 1, # apple | |
223 | "516e7cba-6ecf-11d6-8ff8-00022d09712b" => 1, # bsd | |
224 | }; | |
225 | ||
226 | ||
227 | $res = $get_devices_by_partuuid->($parttype_map, $uuids, $res); | |
228 | ||
229 | return $res; | |
230 | } | |
231 | ||
232 | sub get_lvm_devices { | |
233 | my ($parttype_map) = @_; | |
234 | my $res = {}; | |
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/|) { | |
240 | $res->{$line} = 1; | |
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 | ||
249 | my $uuids = { | |
250 | "e6d6d379-f507-44c2-a23c-238f2a3df928" => 1, | |
251 | }; | |
252 | ||
253 | $res = $get_devices_by_partuuid->($parttype_map, $uuids, $res); | |
254 | ||
255 | return $res; | |
256 | } | |
257 | ||
258 | sub get_ceph_journals { | |
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); | |
270 | ||
271 | return $res; | |
272 | } | |
273 | ||
274 | # reads the lv_tags and matches them with the devices | |
275 | sub get_ceph_volume_infos { | |
276 | my $result = {}; | |
277 | ||
278 | my $cmd = [ $LVS, '-S', 'lv_name=~^osd-', '-o', 'devices,lv_name,lv_tags', | |
279 | '--noheadings', '--readonly', '--separator', ';' ]; | |
280 | ||
281 | run_command($cmd, outfunc => sub { | |
282 | my $line = shift; | |
283 | $line =~ s/(?:^\s+)|(?:\s+$)//g; # trim whitespaces | |
284 | ||
285 | my $fields = [ split(';', $line) ]; | |
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; | |
291 | # $result autovivification is wanted, to not creating empty hashes | |
292 | if (($type eq 'block' || $type eq 'data') && $fields->[2] =~ m/ceph.osd_id=([^,]+)/) { | |
293 | $result->{$dev}->{osdid} = $1; | |
294 | $result->{$dev}->{bluestore} = ($type eq 'block'); | |
295 | if ($fields->[2] =~ m/ceph\.encrypted=1/) { | |
296 | $result->{$dev}->{encrypted} = 1; | |
297 | } | |
298 | } else { | |
299 | # undef++ becomes '1' (see `perldoc perlop`: Auto-increment) | |
300 | $result->{$dev}->{$type}++; | |
301 | } | |
302 | } | |
303 | }); | |
304 | ||
305 | return $result; | |
306 | } | |
307 | ||
308 | sub get_udev_info { | |
309 | my ($dev) = @_; | |
310 | ||
311 | my $info = ""; | |
312 | my $data = {}; | |
313 | eval { | |
314 | run_command([$UDEVADM, 'info', '-p', $dev, '--query', 'all'], outfunc => sub { | |
315 | my ($line) = @_; | |
316 | $info .= "$line\n"; | |
317 | }); | |
318 | }; | |
319 | warn $@ if $@; | |
320 | return undef if !$info; | |
321 | ||
322 | return undef if $info !~ m/^E: DEVTYPE=disk$/m; | |
323 | return undef if $info =~ m/^E: ID_CDROM/m; | |
324 | ||
325 | # we use this, because some disks are not simply in /dev | |
326 | # e.g. /dev/cciss/c0d0 | |
327 | if ($info =~ m/^E: DEVNAME=(\S+)$/m) { | |
328 | $data->{devpath} = $1; | |
329 | } | |
330 | return if !defined($data->{devpath}); | |
331 | ||
332 | $data->{serial} = 'unknown'; | |
333 | if ($info =~ m/^E: ID_SERIAL_SHORT=(\S+)$/m) { | |
334 | $data->{serial} = $1; | |
335 | } | |
336 | ||
337 | $data->{gpt} = 0; | |
338 | if ($info =~ m/^E: ID_PART_TABLE_TYPE=gpt$/m) { | |
339 | $data->{gpt} = 1; | |
340 | } | |
341 | ||
342 | # detect SSD | |
343 | $data->{rpm} = -1; | |
344 | if ($info =~ m/^E: ID_ATA_ROTATION_RATE_RPM=(\d+)$/m) { | |
345 | $data->{rpm} = $1; | |
346 | } | |
347 | ||
348 | if ($info =~ m/^E: ID_BUS=usb$/m) { | |
349 | $data->{usb} = 1; | |
350 | } | |
351 | ||
352 | if ($info =~ m/^E: ID_MODEL=(.+)$/m) { | |
353 | $data->{model} = $1; | |
354 | } | |
355 | ||
356 | $data->{wwn} = 'unknown'; | |
357 | if ($info =~ m/^E: ID_WWN=(.*)$/m) { | |
358 | $data->{wwn} = $1; | |
359 | } | |
360 | ||
361 | return $data; | |
362 | } | |
363 | ||
364 | sub get_sysdir_info { | |
365 | my ($sysdir) = @_; | |
366 | ||
367 | return undef if ! -d "$sysdir/device"; | |
368 | ||
369 | my $data = {}; | |
370 | ||
371 | my $size = file_read_firstline("$sysdir/size"); | |
372 | return undef if !$size; | |
373 | ||
374 | # linux always considers sectors to be 512 bytes, | |
375 | # independently of real block size | |
376 | $data->{size} = $size * 512; | |
377 | ||
378 | # dir/queue/rotational should be 1 for hdd, 0 for ssd | |
379 | $data->{rotational} = file_read_firstline("$sysdir/queue/rotational") // -1; | |
380 | ||
381 | $data->{vendor} = file_read_firstline("$sysdir/device/vendor") || 'unknown'; | |
382 | $data->{model} = file_read_firstline("$sysdir/device/model") || 'unknown'; | |
383 | ||
384 | if (defined(my $device = readlink("$sysdir/device"))) { | |
385 | # strip directory and untaint: | |
386 | ($data->{device}) = $device =~ m!([^/]+)$!; | |
387 | } | |
388 | ||
389 | return $data; | |
390 | } | |
391 | ||
392 | sub get_wear_leveling_info { | |
393 | my ($smartdata, $model) = @_; | |
394 | my $attributes = $smartdata->{attributes}; | |
395 | ||
396 | if (defined($smartdata->{wearout})) { | |
397 | return $smartdata->{wearout}; | |
398 | } | |
399 | ||
400 | my $wearout; | |
401 | ||
402 | my $vendormap = { | |
403 | 'kingston' => 231, | |
404 | 'samsung' => 177, | |
405 | 'intel' => 233, | |
406 | 'sandisk' => 233, | |
407 | 'crucial' => 202, | |
408 | 'default' => 233, | |
409 | }; | |
410 | ||
411 | # find target attr id | |
412 | ||
413 | my $attrid; | |
414 | ||
415 | foreach my $vendor (keys %$vendormap) { | |
416 | if ($model =~ m/$vendor/i) { | |
417 | $attrid = $vendormap->{$vendor}; | |
418 | # found the attribute | |
419 | last; | |
420 | } | |
421 | } | |
422 | ||
423 | if (!$attrid) { | |
424 | $attrid = $vendormap->{default}; | |
425 | } | |
426 | ||
427 | foreach my $attr (@$attributes) { | |
428 | next if $attr->{id} != $attrid; | |
429 | $wearout = $attr->{value}; | |
430 | last; | |
431 | } | |
432 | ||
433 | return $wearout; | |
434 | } | |
435 | ||
436 | sub dir_is_empty { | |
437 | my ($dir) = @_; | |
438 | ||
439 | my $dh = IO::Dir->new ($dir); | |
440 | return 1 if !$dh; | |
441 | ||
442 | while (defined(my $tmp = $dh->read)) { | |
443 | next if $tmp eq '.' || $tmp eq '..'; | |
444 | $dh->close; | |
445 | return 0; | |
446 | } | |
447 | $dh->close; | |
448 | return 1; | |
449 | } | |
450 | ||
451 | sub is_iscsi { | |
452 | my ($sysdir) = @_; | |
453 | ||
454 | if (-l $sysdir && readlink($sysdir) =~ m|host[^/]*/session[^/]*|) { | |
455 | return 1; | |
456 | } | |
457 | ||
458 | return 0; | |
459 | } | |
460 | ||
461 | sub get_disks { | |
462 | my ($disks, $nosmart) = @_; | |
463 | my $disklist = {}; | |
464 | ||
465 | my $mounted = {}; | |
466 | ||
467 | my $mounts = PVE::ProcFSTools::parse_proc_mounts(); | |
468 | ||
469 | foreach my $mount (@$mounts) { | |
470 | next if $mount->[0] !~ m|^/dev/|; | |
471 | $mounted->{abs_path($mount->[0])} = $mount->[1]; | |
472 | }; | |
473 | ||
474 | my $dev_is_mounted = sub { | |
475 | my ($dev) = @_; | |
476 | return $mounted->{$dev}; | |
477 | }; | |
478 | ||
479 | my $parttype_map = get_parttype_info(); | |
480 | ||
481 | my $journalhash = get_ceph_journals($parttype_map); | |
482 | my $ceph_volume_infos = get_ceph_volume_infos(); | |
483 | ||
484 | my $zfshash = get_zfs_devices($parttype_map); | |
485 | ||
486 | my $lvmhash = get_lvm_devices($parttype_map); | |
487 | ||
488 | my $disk_regex = ".*"; | |
489 | if (defined($disks)) { | |
490 | if (!ref($disks)) { | |
491 | $disks = [ $disks ]; | |
492 | } elsif (ref($disks) ne 'ARRAY') { | |
493 | die "disks is not a string or array reference\n"; | |
494 | } | |
495 | # we get cciss/c0d0 but need cciss!c0d0 | |
496 | map { s|cciss/|cciss!| } @$disks; | |
497 | ||
498 | $disk_regex = "(?:" . join('|', @$disks) . ")"; | |
499 | } | |
500 | ||
501 | dir_glob_foreach('/sys/block', $disk_regex, sub { | |
502 | my ($dev) = @_; | |
503 | # whitelisting following devices | |
504 | # hdX: ide block device | |
505 | # sdX: sd block device | |
506 | # vdX: virtual block device | |
507 | # xvdX: xen virtual block device | |
508 | # nvmeXnY: nvme devices | |
509 | # cciss!cXnY: cciss devices | |
510 | return if $dev !~ m/^(h|s|x?v)d[a-z]+$/ && | |
511 | $dev !~ m/^nvme\d+n\d+$/ && | |
512 | $dev !~ m/^cciss\!c\d+d\d+$/; | |
513 | ||
514 | my $data = get_udev_info("/sys/block/$dev"); | |
515 | return if !defined($data); | |
516 | my $devpath = $data->{devpath}; | |
517 | ||
518 | my $sysdir = "/sys/block/$dev"; | |
519 | ||
520 | # we do not want iscsi devices | |
521 | return if is_iscsi($sysdir); | |
522 | ||
523 | my $sysdata = get_sysdir_info($sysdir); | |
524 | return if !defined($sysdata); | |
525 | ||
526 | my $type = 'unknown'; | |
527 | ||
528 | if ($sysdata->{rotational} == 0) { | |
529 | $type = 'ssd'; | |
530 | $data->{rpm} = 0; | |
531 | } elsif ($sysdata->{rotational} == 1) { | |
532 | if ($data->{rpm} != -1) { | |
533 | $type = 'hdd'; | |
534 | } elsif ($data->{usb}) { | |
535 | $type = 'usb'; | |
536 | $data->{rpm} = 0; | |
537 | } | |
538 | } | |
539 | ||
540 | my $health = 'UNKNOWN'; | |
541 | my $wearout = 'N/A'; | |
542 | ||
543 | if (!$nosmart) { | |
544 | eval { | |
545 | my $smartdata = get_smart_data($devpath, ($type ne 'ssd')); | |
546 | $health = $smartdata->{health} if $smartdata->{health}; | |
547 | ||
548 | if ($type eq 'ssd') { | |
549 | # if we have an ssd we try to get the wearout indicator | |
550 | my $wearval = get_wear_leveling_info($smartdata, $data->{model} || $sysdata->{model}); | |
551 | $wearout = $wearval if defined($wearval); | |
552 | } | |
553 | }; | |
554 | } | |
555 | ||
556 | my $used; | |
557 | ||
558 | $used = 'LVM' if $lvmhash->{$devpath}; | |
559 | ||
560 | $used = 'mounted' if &$dev_is_mounted($devpath); | |
561 | ||
562 | $used = 'ZFS' if $zfshash->{$devpath}; | |
563 | ||
564 | # we replaced cciss/ with cciss! above | |
565 | # but in the result we need cciss/ again | |
566 | # because the caller might want to check the | |
567 | # result again with the original parameter | |
568 | if ($dev =~ m|^cciss!|) { | |
569 | $dev =~ s|^cciss!|cciss/|; | |
570 | } | |
571 | ||
572 | $disklist->{$dev} = { | |
573 | vendor => $sysdata->{vendor}, | |
574 | model => $data->{model} || $sysdata->{model}, | |
575 | size => $sysdata->{size}, | |
576 | serial => $data->{serial}, | |
577 | gpt => $data->{gpt}, | |
578 | rpm => $data->{rpm}, | |
579 | type => $type, | |
580 | wwn => $data->{wwn}, | |
581 | health => $health, | |
582 | devpath => $devpath, | |
583 | wearout => $wearout, | |
584 | }; | |
585 | ||
586 | my $osdid = -1; | |
587 | my $bluestore = 0; | |
588 | my $osdencrypted = 0; | |
589 | ||
590 | my $journal_count = 0; | |
591 | my $db_count = 0; | |
592 | my $wal_count = 0; | |
593 | ||
594 | my $found_partitions; | |
595 | my $found_lvm; | |
596 | my $found_mountpoints; | |
597 | my $found_zfs; | |
598 | my $found_dm; | |
599 | my $partpath = $devpath; | |
600 | ||
601 | # remove part after last / to | |
602 | # get the base path for the partitions | |
603 | # e.g. from /dev/cciss/c0d0 get /dev/cciss | |
604 | $partpath =~ s/\/[^\/]+$//; | |
605 | ||
606 | dir_glob_foreach("$sysdir", "$dev.+", sub { | |
607 | my ($part) = @_; | |
608 | ||
609 | $found_partitions = 1; | |
610 | ||
611 | if (my $mp = &$dev_is_mounted("$partpath/$part")) { | |
612 | $found_mountpoints = 1; | |
613 | if ($mp =~ m|^/var/lib/ceph/osd/ceph-(\d+)$|) { | |
614 | $osdid = $1; | |
615 | } | |
616 | } | |
617 | ||
618 | if ($lvmhash->{"$partpath/$part"}) { | |
619 | $found_lvm = 1; | |
620 | } | |
621 | ||
622 | if ($zfshash->{"$partpath/$part"}) { | |
623 | $found_zfs = 1; | |
624 | } | |
625 | ||
626 | if (my $journal_part = $journalhash->{"$partpath/$part"}) { | |
627 | $journal_count++ if $journal_part == 1; | |
628 | $db_count++ if $journal_part == 2; | |
629 | $wal_count++ if $journal_part == 3; | |
630 | $bluestore = 1 if $journal_part == 4; | |
631 | } | |
632 | ||
633 | if (!dir_is_empty("$sysdir/$part/holders") && !$found_lvm) { | |
634 | $found_dm = 1; | |
635 | } | |
636 | }); | |
637 | ||
638 | if (my $ceph_volume = $ceph_volume_infos->{$devpath}) { | |
639 | $journal_count += $ceph_volume->{journal} // 0; | |
640 | $db_count += $ceph_volume->{db} // 0; | |
641 | $wal_count += $ceph_volume->{wal} // 0; | |
642 | if (defined($ceph_volume->{osdid})) { | |
643 | $osdid = $ceph_volume->{osdid}; | |
644 | $bluestore = 1 if $ceph_volume->{bluestore}; | |
645 | $osdencrypted = 1 if $ceph_volume->{encrypted}; | |
646 | } | |
647 | } | |
648 | ||
649 | $used = 'mounted' if $found_mountpoints && !$used; | |
650 | $used = 'LVM' if $found_lvm && !$used; | |
651 | $used = 'ZFS' if $found_zfs && !$used; | |
652 | $used = 'Device Mapper' if $found_dm && !$used; | |
653 | $used = 'partitions' if $found_partitions && !$used; | |
654 | ||
655 | # multipath, software raid, etc. | |
656 | # this check comes in last, to show more specific info | |
657 | # if we have it | |
658 | $used = 'Device Mapper' if !$used && !dir_is_empty("$sysdir/holders"); | |
659 | ||
660 | $disklist->{$dev}->{used} = $used if $used; | |
661 | $disklist->{$dev}->{osdid} = $osdid; | |
662 | $disklist->{$dev}->{journals} = $journal_count if $journal_count; | |
663 | $disklist->{$dev}->{bluestore} = $bluestore if $osdid != -1; | |
664 | $disklist->{$dev}->{osdencrypted} = $osdencrypted if $osdid != -1; | |
665 | $disklist->{$dev}->{db} = $db_count if $db_count; | |
666 | $disklist->{$dev}->{wal} = $wal_count if $wal_count; | |
667 | }); | |
668 | ||
669 | return $disklist; | |
670 | ||
671 | } | |
672 | ||
673 | sub get_partnum { | |
674 | my ($part_path) = @_; | |
675 | ||
676 | my ($mode, $rdev) = (stat($part_path))[2,6]; | |
677 | ||
678 | next if !$mode || !S_ISBLK($mode) || !$rdev; | |
679 | my $major = PVE::Tools::dev_t_major($rdev); | |
680 | my $minor = PVE::Tools::dev_t_minor($rdev); | |
681 | my $partnum_path = "/sys/dev/block/$major:$minor/"; | |
682 | ||
683 | my $partnum; | |
684 | ||
685 | $partnum = file_read_firstline("${partnum_path}partition"); | |
686 | ||
687 | die "Partition does not exists\n" if !defined($partnum); | |
688 | ||
689 | #untaint and ensure it is a int | |
690 | if ($partnum =~ m/(\d+)/) { | |
691 | $partnum = $1; | |
692 | die "Partition number $partnum is invalid\n" if $partnum > 128; | |
693 | } else { | |
694 | die "Failed to get partition number\n"; | |
695 | } | |
696 | ||
697 | return $partnum; | |
698 | } | |
699 | ||
700 | sub get_blockdev { | |
701 | my ($part_path) = @_; | |
702 | ||
703 | my $dev = $1 if $part_path =~ m|^/dev/(.*)$|; | |
704 | my $link = readlink "/sys/class/block/$dev"; | |
705 | my $block_dev = $1 if $link =~ m|([^/]*)/$dev$|; | |
706 | ||
707 | die "Can't parse parent device\n" if !defined($block_dev); | |
708 | die "No valid block device\n" if index($dev, $block_dev) == -1; | |
709 | ||
710 | $block_dev = "/dev/$block_dev"; | |
711 | die "Block device does not exsists\n" if !(-b $block_dev); | |
712 | ||
713 | return $block_dev; | |
714 | } | |
715 | ||
716 | sub locked_disk_action { | |
717 | my ($sub) = @_; | |
718 | my $res = PVE::Tools::lock_file('/run/lock/pve-diskmanage.lck', undef, $sub); | |
719 | die $@ if $@; | |
720 | return $res; | |
721 | } | |
722 | ||
723 | sub assert_disk_unused { | |
724 | my ($dev) = @_; | |
725 | ||
726 | die "device '$dev' is already in use\n" if disk_is_used($dev); | |
727 | ||
728 | return undef; | |
729 | } | |
730 | ||
731 | sub append_partition { | |
732 | my ($dev, $size) = @_; | |
733 | ||
734 | my $devname = $dev; | |
735 | $devname =~ s|^/dev/||; | |
736 | ||
737 | my $newpartid = 1; | |
738 | dir_glob_foreach("/sys/block/$devname", qr/\Q$devname\E.*?(\d+)/, sub { | |
739 | my ($part, $partid) = @_; | |
740 | ||
741 | if ($partid >= $newpartid) { | |
742 | $newpartid = $partid + 1; | |
743 | } | |
744 | }); | |
745 | ||
746 | $size = PVE::Tools::convert_size($size, 'b' => 'mb'); | |
747 | ||
748 | run_command([ $SGDISK, '-n', "$newpartid:0:+${size}M", $dev ], | |
749 | errmsg => "error creating partition '$newpartid' on '$dev'"); | |
750 | ||
751 | my $partition; | |
752 | ||
753 | # loop again to detect the real partiton device which does not always follow | |
754 | # a strict $devname$partition scheme like /dev/nvme0n1 -> /dev/nvme0n1p1 | |
755 | dir_glob_foreach("/sys/block/$devname", qr/\Q$devname\E.*$newpartid/, sub { | |
756 | my ($part) = @_; | |
757 | ||
758 | $partition = "/dev/$part"; | |
759 | }); | |
760 | ||
761 | return $partition; | |
762 | } | |
763 | ||
764 | 1; |