]> git.proxmox.com Git - pve-storage.git/blame - PVE/Diskmanage.pm
drop absolute udevadm path
[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";
8cd6d7e8 20my $LSBLK = "/bin/lsblk";
cbba9b5b
DC
21
22sub verify_blockdev_path {
23 my ($rel_path) = @_;
24
25 die "missing path" if !$rel_path;
26 my $path = abs_path($rel_path);
27 die "failed to get absolute path to $rel_path\n" if !$path;
28
29 die "got unusual device path '$path'\n" if $path !~ m|^/dev/(.*)$|;
30
31 $path = "/dev/$1"; # untaint
32
33 assert_blockdev($path);
34
35 return $path;
36}
37
38sub assert_blockdev {
39 my ($dev, $noerr) = @_;
40
41 if ($dev !~ m|^/dev/| || !(-b $dev)) {
42 return undef if $noerr;
43 die "not a valid block device\n";
44 }
45
46 return 1;
47}
48
49sub init_disk {
50 my ($disk, $uuid) = @_;
51
52 assert_blockdev($disk);
53
54 # we should already have checked if it is in use in the api call
55 # but we check again for safety
56 die "disk $disk is already in use\n" if disk_is_used($disk);
57
58 my $id = $uuid || 'R';
59 run_command([$SGDISK, $disk, '-U', $id]);
60 return 1;
61}
62
63sub disk_is_used {
64 my ($disk) = @_;
65
66 my $dev = $disk;
67 $dev =~ s|^/dev/||;
68
7a98a62d 69 my $disklist = get_disks($dev, 1);
cbba9b5b
DC
70
71 die "'$disk' is not a valid local disk\n" if !defined($disklist->{$dev});
72 return 1 if $disklist->{$dev}->{used};
73
74 return 0;
75}
76
77sub get_smart_data {
dd902da7 78 my ($disk, $healthonly) = @_;
cbba9b5b
DC
79
80 assert_blockdev($disk);
81 my $smartdata = {};
dc1311cb 82 my $type;
cbba9b5b 83
9018a4e6 84 my $returncode = 0;
c9bd3d22 85
c3442aa5
WB
86 if ($disk =~ m!^/dev/(nvme\d+n\d+)$!) {
87 my $info = get_sysdir_info("/sys/block/$1");
88 $disk = "/dev/".($info->{device}
89 or die "failed to get nvme controller device for $disk\n");
90 }
c9bd3d22 91
dd902da7
DC
92 my $cmd = [$SMARTCTL, '-H'];
93 push @$cmd, '-A', '-f', 'brief' if !$healthonly;
94 push @$cmd, $disk;
95
cbba9b5b 96 eval {
dd902da7 97 $returncode = run_command($cmd, noerr => 1, outfunc => sub{
cbba9b5b
DC
98 my ($line) = @_;
99
1c999553
FG
100# ATA SMART attributes, e.g.:
101# ID# ATTRIBUTE_NAME FLAGS VALUE WORST THRESH FAIL RAW_VALUE
102# 1 Raw_Read_Error_Rate POSR-K 100 100 000 - 0
dc1311cb
FG
103#
104# SAS and NVME disks, e.g.:
105# Data Units Written: 5,584,952 [2.85 TB]
106# Accumulated start-stop cycles: 34
107
bd54091c 108 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 109 my $entry = {};
bd54091c 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";
2c048efd
DC
136 # extract wearout from nvme/sas text, allow for decimal values
137 if ($line =~ m/Percentage Used(?: endurance indicator)?:\s*(\d+(?:\.\d+)?)\%/i) {
ea928fd4
DC
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
159sub 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
186my $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 198sub 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
232sub 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
258sub 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
275sub 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');
bfb3d42d
DC
295 if ($fields->[2] =~ m/ceph\.encrypted=1/) {
296 $result->{$dev}->{encrypted} = 1;
297 }
19dcd1ad 298 } else {
248f43f5 299 # undef++ becomes '1' (see `perldoc perlop`: Auto-increment)
19dcd1ad
DC
300 $result->{$dev}->{$type}++;
301 }
302 }
303 });
304
305 return $result;
306}
307
cbba9b5b
DC
308sub get_udev_info {
309 my ($dev) = @_;
310
311 my $info = "";
312 my $data = {};
313 eval {
d3a5e309 314 run_command(['udevadm', 'info', '-p', $dev, '--query', 'all'], outfunc => sub {
cbba9b5b
DC
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
865bdbd9
DC
352 if ($info =~ m/^E: ID_MODEL=(.+)$/m) {
353 $data->{model} = $1;
354 }
355
cbba9b5b
DC
356 $data->{wwn} = 'unknown';
357 if ($info =~ m/^E: ID_WWN=(.*)$/m) {
358 $data->{wwn} = $1;
359 }
360
0f0d99a3
SI
361 if ($info =~ m/^E: DEVLINKS=(.+)$/m) {
362 my @devlinks = grep(m#^/dev/disk/by-id/(ata|scsi|nvme(?!-eui))#, split (/ /, $1));
363 $data->{by_id_link} = $devlinks[0] if defined($devlinks[0]);
364 }
365
cbba9b5b
DC
366 return $data;
367}
368
369sub get_sysdir_info {
370 my ($sysdir) = @_;
371
461a9fd8
DC
372 return undef if ! -d "$sysdir/device";
373
cbba9b5b
DC
374 my $data = {};
375
376 my $size = file_read_firstline("$sysdir/size");
377 return undef if !$size;
378
379 # linux always considers sectors to be 512 bytes,
380 # independently of real block size
381 $data->{size} = $size * 512;
382
383 # dir/queue/rotational should be 1 for hdd, 0 for ssd
571b6f26 384 $data->{rotational} = file_read_firstline("$sysdir/queue/rotational") // -1;
cbba9b5b
DC
385
386 $data->{vendor} = file_read_firstline("$sysdir/device/vendor") || 'unknown';
387 $data->{model} = file_read_firstline("$sysdir/device/model") || 'unknown';
388
c3442aa5
WB
389 if (defined(my $device = readlink("$sysdir/device"))) {
390 # strip directory and untaint:
391 ($data->{device}) = $device =~ m!([^/]+)$!;
392 }
393
cbba9b5b
DC
394 return $data;
395}
396
6965a670 397sub get_wear_leveling_info {
dbad606d 398 my ($smartdata) = @_;
ea928fd4
DC
399 my $attributes = $smartdata->{attributes};
400
401 if (defined($smartdata->{wearout})) {
402 return $smartdata->{wearout};
403 }
6965a670
DC
404
405 my $wearout;
406
dbad606d
JJS
407 # Common register names that represent percentage values of potential
408 # failure indicators used in drivedb.h of smartmontool's. Order matters,
409 # as some drives may have multiple definitions
410 my @wearoutregisters = (
411 "Media_Wearout_Indicator",
412 "SSD_Life_Left",
413 "Wear_Leveling_Count",
414 "Perc_Write\/Erase_Ct_BC",
415 "Perc_Rated_Life_Remain",
416 "Remaining_Lifetime_Perc",
417 "Percent_Lifetime_Remain",
418 "Lifetime_Left",
419 "PCT_Life_Remaining",
420 "Lifetime_Remaining",
421 "Percent_Life_Remaining",
422 "Percent_Lifetime_Used",
423 "Perc_Rated_Life_Used"
424 );
425
426 # Search for S.M.A.R.T. attributes for known register
427 foreach my $register (@wearoutregisters) {
428 last if defined $wearout;
429 foreach my $attr (@$attributes) {
430 next if $attr->{name} !~ m/$register/;
431 $wearout = $attr->{value};
432 last;
6965a670
DC
433 }
434 }
435
6965a670
DC
436 return $wearout;
437}
438
10a48db5
DC
439sub dir_is_empty {
440 my ($dir) = @_;
441
442 my $dh = IO::Dir->new ($dir);
443 return 1 if !$dh;
444
445 while (defined(my $tmp = $dh->read)) {
446 next if $tmp eq '.' || $tmp eq '..';
447 $dh->close;
448 return 0;
449 }
450 $dh->close;
451 return 1;
452}
453
eebcdb11
DC
454sub is_iscsi {
455 my ($sysdir) = @_;
456
457 if (-l $sysdir && readlink($sysdir) =~ m|host[^/]*/session[^/]*|) {
458 return 1;
459 }
460
461 return 0;
462}
463
4731eb11
TL
464my sub is_ssdlike {
465 my ($type) = @_;
466 return $type eq 'ssd' || $type eq 'nvme';
467}
468
cbba9b5b 469sub get_disks {
52a064af 470 my ($disks, $nosmart) = @_;
cbba9b5b
DC
471 my $disklist = {};
472
473 my $mounted = {};
474
475 my $mounts = PVE::ProcFSTools::parse_proc_mounts();
476
477 foreach my $mount (@$mounts) {
478 next if $mount->[0] !~ m|^/dev/|;
479 $mounted->{abs_path($mount->[0])} = $mount->[1];
480 };
481
482 my $dev_is_mounted = sub {
483 my ($dev) = @_;
484 return $mounted->{$dev};
485 };
486
8cd6d7e8
DC
487 my $parttype_map = get_parttype_info();
488
489 my $journalhash = get_ceph_journals($parttype_map);
19dcd1ad 490 my $ceph_volume_infos = get_ceph_volume_infos();
cbba9b5b 491
8cd6d7e8 492 my $zfshash = get_zfs_devices($parttype_map);
cbba9b5b 493
8cd6d7e8 494 my $lvmhash = get_lvm_devices($parttype_map);
cbba9b5b 495
52a064af
DC
496 my $disk_regex = ".*";
497 if (defined($disks)) {
498 if (!ref($disks)) {
499 $disks = [ $disks ];
500 } elsif (ref($disks) ne 'ARRAY') {
501 die "disks is not a string or array reference\n";
502 }
503 # we get cciss/c0d0 but need cciss!c0d0
5045e0b7 504 $_ =~ s|cciss/|cciss!| for @$disks;
52a064af
DC
505
506 $disk_regex = "(?:" . join('|', @$disks) . ")";
1590fc13
DC
507 }
508
52a064af 509 dir_glob_foreach('/sys/block', $disk_regex, sub {
cbba9b5b 510 my ($dev) = @_;
cbba9b5b
DC
511 # whitelisting following devices
512 # hdX: ide block device
513 # sdX: sd block device
514 # vdX: virtual block device
515 # xvdX: xen virtual block device
516 # nvmeXnY: nvme devices
38ddd4ce 517 # cciss!cXnY: cciss devices
cbba9b5b
DC
518 return if $dev !~ m/^(h|s|x?v)d[a-z]+$/ &&
519 $dev !~ m/^nvme\d+n\d+$/ &&
38ddd4ce 520 $dev !~ m/^cciss\!c\d+d\d+$/;
cbba9b5b 521
532e89e7 522 my $data = get_udev_info("/sys/block/$dev");
cbba9b5b
DC
523 return if !defined($data);
524 my $devpath = $data->{devpath};
525
526 my $sysdir = "/sys/block/$dev";
527
cbba9b5b 528 # we do not want iscsi devices
eebcdb11 529 return if is_iscsi($sysdir);
cbba9b5b
DC
530
531 my $sysdata = get_sysdir_info($sysdir);
532 return if !defined($sysdata);
533
534 my $type = 'unknown';
535
536 if ($sysdata->{rotational} == 0) {
537 $type = 'ssd';
4731eb11 538 $type = 'nvme' if $dev =~ m/^nvme\d+n\d+$/;
cbba9b5b
DC
539 $data->{rpm} = 0;
540 } elsif ($sysdata->{rotational} == 1) {
541 if ($data->{rpm} != -1) {
542 $type = 'hdd';
543 } elsif ($data->{usb}) {
544 $type = 'usb';
545 $data->{rpm} = 0;
546 }
547 }
548
acd3d916 549 my $health = 'UNKNOWN';
6965a670 550 my $wearout = 'N/A';
7a98a62d
FG
551
552 if (!$nosmart) {
553 eval {
4731eb11 554 my $smartdata = get_smart_data($devpath, !is_ssdlike($type));
dd902da7
DC
555 $health = $smartdata->{health} if $smartdata->{health};
556
4731eb11 557 if (is_ssdlike($type)) {
7a98a62d 558 # if we have an ssd we try to get the wearout indicator
dbad606d 559 my $wearval = get_wear_leveling_info($smartdata);
9250ddfe 560 $wearout = $wearval if defined($wearval);
acd3d916 561 }
7a98a62d
FG
562 };
563 }
cbba9b5b
DC
564
565 my $used;
566
8cd6d7e8 567 $used = 'LVM' if $lvmhash->{$devpath};
cbba9b5b
DC
568
569 $used = 'mounted' if &$dev_is_mounted($devpath);
570
8cd6d7e8 571 $used = 'ZFS' if $zfshash->{$devpath};
cbba9b5b 572
fc7c0e05
DC
573 # we replaced cciss/ with cciss! above
574 # but in the result we need cciss/ again
575 # because the caller might want to check the
576 # result again with the original parameter
577 if ($dev =~ m|^cciss!|) {
578 $dev =~ s|^cciss!|cciss/|;
579 }
580
cbba9b5b
DC
581 $disklist->{$dev} = {
582 vendor => $sysdata->{vendor},
865bdbd9 583 model => $data->{model} || $sysdata->{model},
cbba9b5b
DC
584 size => $sysdata->{size},
585 serial => $data->{serial},
586 gpt => $data->{gpt},
587 rpm => $data->{rpm},
588 type => $type,
589 wwn => $data->{wwn},
590 health => $health,
591 devpath => $devpath,
592 wearout => $wearout,
593 };
594
0f0d99a3
SI
595 my $by_id_link = $data->{by_id_link};
596 $disklist->{$dev}->{by_id_link} = $by_id_link if defined($by_id_link);
597
cbba9b5b 598 my $osdid = -1;
e2bd817c 599 my $bluestore = 0;
bfb3d42d 600 my $osdencrypted = 0;
cbba9b5b
DC
601
602 my $journal_count = 0;
e2bd817c
DC
603 my $db_count = 0;
604 my $wal_count = 0;
cbba9b5b
DC
605
606 my $found_partitions;
607 my $found_lvm;
608 my $found_mountpoints;
609 my $found_zfs;
610 my $found_dm;
611 my $partpath = $devpath;
612
613 # remove part after last / to
614 # get the base path for the partitions
615 # e.g. from /dev/cciss/c0d0 get /dev/cciss
616 $partpath =~ s/\/[^\/]+$//;
617
618 dir_glob_foreach("$sysdir", "$dev.+", sub {
619 my ($part) = @_;
620
621 $found_partitions = 1;
622
623 if (my $mp = &$dev_is_mounted("$partpath/$part")) {
624 $found_mountpoints = 1;
625 if ($mp =~ m|^/var/lib/ceph/osd/ceph-(\d+)$|) {
626 $osdid = $1;
627 }
628 }
629
8cd6d7e8 630 if ($lvmhash->{"$partpath/$part"}) {
cbba9b5b
DC
631 $found_lvm = 1;
632 }
633
8cd6d7e8 634 if ($zfshash->{"$partpath/$part"}) {
cbba9b5b
DC
635 $found_zfs = 1;
636 }
637
0180fa42
TL
638 if (my $journal_part = $journalhash->{"$partpath/$part"}) {
639 $journal_count++ if $journal_part == 1;
640 $db_count++ if $journal_part == 2;
641 $wal_count++ if $journal_part == 3;
642 $bluestore = 1 if $journal_part == 4;
e2bd817c 643 }
cbba9b5b 644
10a48db5 645 if (!dir_is_empty("$sysdir/$part/holders") && !$found_lvm) {
cbba9b5b
DC
646 $found_dm = 1;
647 }
648 });
649
0180fa42
TL
650 if (my $ceph_volume = $ceph_volume_infos->{$devpath}) {
651 $journal_count += $ceph_volume->{journal} // 0;
652 $db_count += $ceph_volume->{db} // 0;
653 $wal_count += $ceph_volume->{wal} // 0;
35ccd3d0 654 if (defined($ceph_volume->{osdid})) {
0180fa42
TL
655 $osdid = $ceph_volume->{osdid};
656 $bluestore = 1 if $ceph_volume->{bluestore};
bfb3d42d 657 $osdencrypted = 1 if $ceph_volume->{encrypted};
19dcd1ad
DC
658 }
659 }
660
cbba9b5b
DC
661 $used = 'mounted' if $found_mountpoints && !$used;
662 $used = 'LVM' if $found_lvm && !$used;
663 $used = 'ZFS' if $found_zfs && !$used;
664 $used = 'Device Mapper' if $found_dm && !$used;
665 $used = 'partitions' if $found_partitions && !$used;
666
667 # multipath, software raid, etc.
668 # this check comes in last, to show more specific info
669 # if we have it
10a48db5 670 $used = 'Device Mapper' if !$used && !dir_is_empty("$sysdir/holders");
cbba9b5b
DC
671
672 $disklist->{$dev}->{used} = $used if $used;
673 $disklist->{$dev}->{osdid} = $osdid;
e2bd817c
DC
674 $disklist->{$dev}->{journals} = $journal_count if $journal_count;
675 $disklist->{$dev}->{bluestore} = $bluestore if $osdid != -1;
bfb3d42d 676 $disklist->{$dev}->{osdencrypted} = $osdencrypted if $osdid != -1;
e2bd817c
DC
677 $disklist->{$dev}->{db} = $db_count if $db_count;
678 $disklist->{$dev}->{wal} = $wal_count if $wal_count;
cbba9b5b
DC
679 });
680
681 return $disklist;
682
683}
684
3196c387
WL
685sub get_partnum {
686 my ($part_path) = @_;
687
92ae59df 688 my $st = stat($part_path);
3196c387 689
92ae59df
AA
690 next if !$st->mode || !S_ISBLK($st->mode) || !$st->rdev;
691 my $major = PVE::Tools::dev_t_major($st->rdev);
692 my $minor = PVE::Tools::dev_t_minor($st->rdev);
3196c387
WL
693 my $partnum_path = "/sys/dev/block/$major:$minor/";
694
695 my $partnum;
696
697 $partnum = file_read_firstline("${partnum_path}partition");
698
481f6177 699 die "Partition does not exist\n" if !defined($partnum);
3196c387
WL
700
701 #untaint and ensure it is a int
702 if ($partnum =~ m/(\d+)/) {
703 $partnum = $1;
704 die "Partition number $partnum is invalid\n" if $partnum > 128;
705 } else {
706 die "Failed to get partition number\n";
707 }
708
709 return $partnum;
710}
711
0d28307d
WL
712sub get_blockdev {
713 my ($part_path) = @_;
714
1207620c
TL
715 my ($dev, $block_dev);
716 if ($part_path =~ m|^/dev/(.*)$|) {
717 $dev = $1;
718 my $link = readlink "/sys/class/block/$dev";
719 $block_dev = $1 if $link =~ m|([^/]*)/$dev$|;
720 }
0d28307d
WL
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;