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