]> git.proxmox.com Git - pve-storage.git/blame - PVE/Diskmanage.pm
PBSPlugin.pm: avoid ascii-art in proxmox-backup-client output
[pve-storage.git] / PVE / Diskmanage.pm
CommitLineData
cbba9b5b
DC
1package PVE::Diskmanage;
2
3use strict;
4use warnings;
5use PVE::ProcFSTools;
6use Data::Dumper;
7use Cwd qw(abs_path);
3196c387 8use Fcntl ':mode';
92ae59df 9use File::stat;
8cd6d7e8 10use JSON;
cbba9b5b
DC
11
12use PVE::Tools qw(extract_param run_command file_get_contents file_read_firstline dir_glob_regex dir_glob_foreach trim);
13
14my $SMARTCTL = "/usr/sbin/smartctl";
15my $ZPOOL = "/sbin/zpool";
16my $SGDISK = "/sbin/sgdisk";
17my $PVS = "/sbin/pvs";
19dcd1ad 18my $LVS = "/sbin/lvs";
cbba9b5b 19my $UDEVADM = "/bin/udevadm";
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
DC
110
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
362 return $data;
363}
364
365sub get_sysdir_info {
366 my ($sysdir) = @_;
367
461a9fd8
DC
368 return undef if ! -d "$sysdir/device";
369
cbba9b5b
DC
370 my $data = {};
371
372 my $size = file_read_firstline("$sysdir/size");
373 return undef if !$size;
374
375 # linux always considers sectors to be 512 bytes,
376 # independently of real block size
377 $data->{size} = $size * 512;
378
379 # dir/queue/rotational should be 1 for hdd, 0 for ssd
571b6f26 380 $data->{rotational} = file_read_firstline("$sysdir/queue/rotational") // -1;
cbba9b5b
DC
381
382 $data->{vendor} = file_read_firstline("$sysdir/device/vendor") || 'unknown';
383 $data->{model} = file_read_firstline("$sysdir/device/model") || 'unknown';
384
c3442aa5
WB
385 if (defined(my $device = readlink("$sysdir/device"))) {
386 # strip directory and untaint:
387 ($data->{device}) = $device =~ m!([^/]+)$!;
388 }
389
cbba9b5b
DC
390 return $data;
391}
392
6965a670 393sub get_wear_leveling_info {
ea928fd4
DC
394 my ($smartdata, $model) = @_;
395 my $attributes = $smartdata->{attributes};
396
397 if (defined($smartdata->{wearout})) {
398 return $smartdata->{wearout};
399 }
6965a670
DC
400
401 my $wearout;
402
403 my $vendormap = {
404 'kingston' => 231,
405 'samsung' => 177,
406 'intel' => 233,
407 'sandisk' => 233,
7b32e2a7 408 'crucial' => 202,
6965a670
DC
409 'default' => 233,
410 };
411
412 # find target attr id
413
414 my $attrid;
415
d57fb43d 416 foreach my $vendor (keys %$vendormap) {
6965a670
DC
417 if ($model =~ m/$vendor/i) {
418 $attrid = $vendormap->{$vendor};
419 # found the attribute
420 last;
421 }
422 }
423
424 if (!$attrid) {
425 $attrid = $vendormap->{default};
426 }
427
428 foreach my $attr (@$attributes) {
429 next if $attr->{id} != $attrid;
430 $wearout = $attr->{value};
431 last;
432 }
433
434 return $wearout;
435}
436
10a48db5
DC
437sub dir_is_empty {
438 my ($dir) = @_;
439
440 my $dh = IO::Dir->new ($dir);
441 return 1 if !$dh;
442
443 while (defined(my $tmp = $dh->read)) {
444 next if $tmp eq '.' || $tmp eq '..';
445 $dh->close;
446 return 0;
447 }
448 $dh->close;
449 return 1;
450}
451
eebcdb11
DC
452sub is_iscsi {
453 my ($sysdir) = @_;
454
455 if (-l $sysdir && readlink($sysdir) =~ m|host[^/]*/session[^/]*|) {
456 return 1;
457 }
458
459 return 0;
460}
461
cbba9b5b 462sub get_disks {
52a064af 463 my ($disks, $nosmart) = @_;
cbba9b5b
DC
464 my $disklist = {};
465
466 my $mounted = {};
467
468 my $mounts = PVE::ProcFSTools::parse_proc_mounts();
469
470 foreach my $mount (@$mounts) {
471 next if $mount->[0] !~ m|^/dev/|;
472 $mounted->{abs_path($mount->[0])} = $mount->[1];
473 };
474
475 my $dev_is_mounted = sub {
476 my ($dev) = @_;
477 return $mounted->{$dev};
478 };
479
8cd6d7e8
DC
480 my $parttype_map = get_parttype_info();
481
482 my $journalhash = get_ceph_journals($parttype_map);
19dcd1ad 483 my $ceph_volume_infos = get_ceph_volume_infos();
cbba9b5b 484
8cd6d7e8 485 my $zfshash = get_zfs_devices($parttype_map);
cbba9b5b 486
8cd6d7e8 487 my $lvmhash = get_lvm_devices($parttype_map);
cbba9b5b 488
52a064af
DC
489 my $disk_regex = ".*";
490 if (defined($disks)) {
491 if (!ref($disks)) {
492 $disks = [ $disks ];
493 } elsif (ref($disks) ne 'ARRAY') {
494 die "disks is not a string or array reference\n";
495 }
496 # we get cciss/c0d0 but need cciss!c0d0
497 map { s|cciss/|cciss!| } @$disks;
498
499 $disk_regex = "(?:" . join('|', @$disks) . ")";
1590fc13
DC
500 }
501
52a064af 502 dir_glob_foreach('/sys/block', $disk_regex, sub {
cbba9b5b 503 my ($dev) = @_;
cbba9b5b
DC
504 # whitelisting following devices
505 # hdX: ide block device
506 # sdX: sd block device
507 # vdX: virtual block device
508 # xvdX: xen virtual block device
509 # nvmeXnY: nvme devices
38ddd4ce 510 # cciss!cXnY: cciss devices
cbba9b5b
DC
511 return if $dev !~ m/^(h|s|x?v)d[a-z]+$/ &&
512 $dev !~ m/^nvme\d+n\d+$/ &&
38ddd4ce 513 $dev !~ m/^cciss\!c\d+d\d+$/;
cbba9b5b 514
532e89e7 515 my $data = get_udev_info("/sys/block/$dev");
cbba9b5b
DC
516 return if !defined($data);
517 my $devpath = $data->{devpath};
518
519 my $sysdir = "/sys/block/$dev";
520
cbba9b5b 521 # we do not want iscsi devices
eebcdb11 522 return if is_iscsi($sysdir);
cbba9b5b
DC
523
524 my $sysdata = get_sysdir_info($sysdir);
525 return if !defined($sysdata);
526
527 my $type = 'unknown';
528
529 if ($sysdata->{rotational} == 0) {
530 $type = 'ssd';
531 $data->{rpm} = 0;
532 } elsif ($sysdata->{rotational} == 1) {
533 if ($data->{rpm} != -1) {
534 $type = 'hdd';
535 } elsif ($data->{usb}) {
536 $type = 'usb';
537 $data->{rpm} = 0;
538 }
539 }
540
acd3d916 541 my $health = 'UNKNOWN';
6965a670 542 my $wearout = 'N/A';
7a98a62d
FG
543
544 if (!$nosmart) {
545 eval {
dd902da7
DC
546 my $smartdata = get_smart_data($devpath, ($type ne 'ssd'));
547 $health = $smartdata->{health} if $smartdata->{health};
548
7a98a62d
FG
549 if ($type eq 'ssd') {
550 # if we have an ssd we try to get the wearout indicator
c5fa45a9 551 my $wearval = get_wear_leveling_info($smartdata, $data->{model} || $sysdata->{model});
9250ddfe 552 $wearout = $wearval if defined($wearval);
acd3d916 553 }
7a98a62d
FG
554 };
555 }
cbba9b5b
DC
556
557 my $used;
558
8cd6d7e8 559 $used = 'LVM' if $lvmhash->{$devpath};
cbba9b5b
DC
560
561 $used = 'mounted' if &$dev_is_mounted($devpath);
562
8cd6d7e8 563 $used = 'ZFS' if $zfshash->{$devpath};
cbba9b5b 564
fc7c0e05
DC
565 # we replaced cciss/ with cciss! above
566 # but in the result we need cciss/ again
567 # because the caller might want to check the
568 # result again with the original parameter
569 if ($dev =~ m|^cciss!|) {
570 $dev =~ s|^cciss!|cciss/|;
571 }
572
cbba9b5b
DC
573 $disklist->{$dev} = {
574 vendor => $sysdata->{vendor},
865bdbd9 575 model => $data->{model} || $sysdata->{model},
cbba9b5b
DC
576 size => $sysdata->{size},
577 serial => $data->{serial},
578 gpt => $data->{gpt},
579 rpm => $data->{rpm},
580 type => $type,
581 wwn => $data->{wwn},
582 health => $health,
583 devpath => $devpath,
584 wearout => $wearout,
585 };
586
587 my $osdid = -1;
e2bd817c 588 my $bluestore = 0;
bfb3d42d 589 my $osdencrypted = 0;
cbba9b5b
DC
590
591 my $journal_count = 0;
e2bd817c
DC
592 my $db_count = 0;
593 my $wal_count = 0;
cbba9b5b
DC
594
595 my $found_partitions;
596 my $found_lvm;
597 my $found_mountpoints;
598 my $found_zfs;
599 my $found_dm;
600 my $partpath = $devpath;
601
602 # remove part after last / to
603 # get the base path for the partitions
604 # e.g. from /dev/cciss/c0d0 get /dev/cciss
605 $partpath =~ s/\/[^\/]+$//;
606
607 dir_glob_foreach("$sysdir", "$dev.+", sub {
608 my ($part) = @_;
609
610 $found_partitions = 1;
611
612 if (my $mp = &$dev_is_mounted("$partpath/$part")) {
613 $found_mountpoints = 1;
614 if ($mp =~ m|^/var/lib/ceph/osd/ceph-(\d+)$|) {
615 $osdid = $1;
616 }
617 }
618
8cd6d7e8 619 if ($lvmhash->{"$partpath/$part"}) {
cbba9b5b
DC
620 $found_lvm = 1;
621 }
622
8cd6d7e8 623 if ($zfshash->{"$partpath/$part"}) {
cbba9b5b
DC
624 $found_zfs = 1;
625 }
626
0180fa42
TL
627 if (my $journal_part = $journalhash->{"$partpath/$part"}) {
628 $journal_count++ if $journal_part == 1;
629 $db_count++ if $journal_part == 2;
630 $wal_count++ if $journal_part == 3;
631 $bluestore = 1 if $journal_part == 4;
e2bd817c 632 }
cbba9b5b 633
10a48db5 634 if (!dir_is_empty("$sysdir/$part/holders") && !$found_lvm) {
cbba9b5b
DC
635 $found_dm = 1;
636 }
637 });
638
0180fa42
TL
639 if (my $ceph_volume = $ceph_volume_infos->{$devpath}) {
640 $journal_count += $ceph_volume->{journal} // 0;
641 $db_count += $ceph_volume->{db} // 0;
642 $wal_count += $ceph_volume->{wal} // 0;
35ccd3d0 643 if (defined($ceph_volume->{osdid})) {
0180fa42
TL
644 $osdid = $ceph_volume->{osdid};
645 $bluestore = 1 if $ceph_volume->{bluestore};
bfb3d42d 646 $osdencrypted = 1 if $ceph_volume->{encrypted};
19dcd1ad
DC
647 }
648 }
649
cbba9b5b
DC
650 $used = 'mounted' if $found_mountpoints && !$used;
651 $used = 'LVM' if $found_lvm && !$used;
652 $used = 'ZFS' if $found_zfs && !$used;
653 $used = 'Device Mapper' if $found_dm && !$used;
654 $used = 'partitions' if $found_partitions && !$used;
655
656 # multipath, software raid, etc.
657 # this check comes in last, to show more specific info
658 # if we have it
10a48db5 659 $used = 'Device Mapper' if !$used && !dir_is_empty("$sysdir/holders");
cbba9b5b
DC
660
661 $disklist->{$dev}->{used} = $used if $used;
662 $disklist->{$dev}->{osdid} = $osdid;
e2bd817c
DC
663 $disklist->{$dev}->{journals} = $journal_count if $journal_count;
664 $disklist->{$dev}->{bluestore} = $bluestore if $osdid != -1;
bfb3d42d 665 $disklist->{$dev}->{osdencrypted} = $osdencrypted if $osdid != -1;
e2bd817c
DC
666 $disklist->{$dev}->{db} = $db_count if $db_count;
667 $disklist->{$dev}->{wal} = $wal_count if $wal_count;
cbba9b5b
DC
668 });
669
670 return $disklist;
671
672}
673
3196c387
WL
674sub get_partnum {
675 my ($part_path) = @_;
676
92ae59df 677 my $st = stat($part_path);
3196c387 678
92ae59df
AA
679 next if !$st->mode || !S_ISBLK($st->mode) || !$st->rdev;
680 my $major = PVE::Tools::dev_t_major($st->rdev);
681 my $minor = PVE::Tools::dev_t_minor($st->rdev);
3196c387
WL
682 my $partnum_path = "/sys/dev/block/$major:$minor/";
683
684 my $partnum;
685
686 $partnum = file_read_firstline("${partnum_path}partition");
687
481f6177 688 die "Partition does not exist\n" if !defined($partnum);
3196c387
WL
689
690 #untaint and ensure it is a int
691 if ($partnum =~ m/(\d+)/) {
692 $partnum = $1;
693 die "Partition number $partnum is invalid\n" if $partnum > 128;
694 } else {
695 die "Failed to get partition number\n";
696 }
697
698 return $partnum;
699}
700
0d28307d
WL
701sub get_blockdev {
702 my ($part_path) = @_;
703
704 my $dev = $1 if $part_path =~ m|^/dev/(.*)$|;
705 my $link = readlink "/sys/class/block/$dev";
706 my $block_dev = $1 if $link =~ m|([^/]*)/$dev$|;
707
708 die "Can't parse parent device\n" if !defined($block_dev);
709 die "No valid block device\n" if index($dev, $block_dev) == -1;
710
711 $block_dev = "/dev/$block_dev";
712 die "Block device does not exsists\n" if !(-b $block_dev);
713
714 return $block_dev;
715}
716
e39e8ee2
DC
717sub locked_disk_action {
718 my ($sub) = @_;
719 my $res = PVE::Tools::lock_file('/run/lock/pve-diskmanage.lck', undef, $sub);
720 die $@ if $@;
721 return $res;
722}
723
0370861c 724sub assert_disk_unused {
76c1e57b
DC
725 my ($dev) = @_;
726
0370861c 727 die "device '$dev' is already in use\n" if disk_is_used($dev);
76c1e57b
DC
728
729 return undef;
730}
731
1dc3038d
DC
732sub append_partition {
733 my ($dev, $size) = @_;
734
735 my $devname = $dev;
736 $devname =~ s|^/dev/||;
737
738 my $newpartid = 1;
739 dir_glob_foreach("/sys/block/$devname", qr/\Q$devname\E.*?(\d+)/, sub {
740 my ($part, $partid) = @_;
741
742 if ($partid >= $newpartid) {
743 $newpartid = $partid + 1;
744 }
745 });
746
747 $size = PVE::Tools::convert_size($size, 'b' => 'mb');
748
749 run_command([ $SGDISK, '-n', "$newpartid:0:+${size}M", $dev ],
750 errmsg => "error creating partition '$newpartid' on '$dev'");
751
752 my $partition;
753
754 # loop again to detect the real partiton device which does not always follow
755 # a strict $devname$partition scheme like /dev/nvme0n1 -> /dev/nvme0n1p1
756 dir_glob_foreach("/sys/block/$devname", qr/\Q$devname\E.*$newpartid/, sub {
757 my ($part) = @_;
758
759 $partition = "/dev/$part";
760 });
761
762 return $partition;
763}
764
cbba9b5b 7651;