]> git.proxmox.com Git - pve-storage.git/blame - PVE/Diskmanage.pm
makefile: convert to use simple parenthesis
[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';
262ad7a9 10use File::Basename;
92ae59df 11use File::stat;
8cd6d7e8 12use JSON;
cbba9b5b
DC
13
14use PVE::Tools qw(extract_param run_command file_get_contents file_read_firstline dir_glob_regex dir_glob_foreach trim);
15
16my $SMARTCTL = "/usr/sbin/smartctl";
17my $ZPOOL = "/sbin/zpool";
18my $SGDISK = "/sbin/sgdisk";
19my $PVS = "/sbin/pvs";
19dcd1ad 20my $LVS = "/sbin/lvs";
8cd6d7e8 21my $LSBLK = "/bin/lsblk";
cbba9b5b 22
a64aedd3
FE
23my sub strip_dev :prototype($) {
24 my ($devpath) = @_;
25 $devpath =~ s|^/dev/||;
26 return $devpath;
27}
28
525b4a6e
FE
29sub check_bin {
30 my ($path) = @_;
525b4a6e
FE
31 return -x $path;
32}
33
cbba9b5b
DC
34sub verify_blockdev_path {
35 my ($rel_path) = @_;
36
37 die "missing path" if !$rel_path;
38 my $path = abs_path($rel_path);
39 die "failed to get absolute path to $rel_path\n" if !$path;
40
41 die "got unusual device path '$path'\n" if $path !~ m|^/dev/(.*)$|;
42
43 $path = "/dev/$1"; # untaint
44
45 assert_blockdev($path);
46
47 return $path;
48}
49
50sub assert_blockdev {
51 my ($dev, $noerr) = @_;
52
53 if ($dev !~ m|^/dev/| || !(-b $dev)) {
9aff3f3d 54 return if $noerr;
cbba9b5b
DC
55 die "not a valid block device\n";
56 }
57
58 return 1;
59}
60
61sub init_disk {
62 my ($disk, $uuid) = @_;
63
64 assert_blockdev($disk);
65
cc884f73
FE
66 # we should already have checked these in the api call, but we check again for safety
67 die "$disk is a partition\n" if is_partition($disk);
cbba9b5b
DC
68 die "disk $disk is already in use\n" if disk_is_used($disk);
69
70 my $id = $uuid || 'R';
71 run_command([$SGDISK, $disk, '-U', $id]);
72 return 1;
73}
74
75sub disk_is_used {
76 my ($disk) = @_;
77
78 my $dev = $disk;
79 $dev =~ s|^/dev/||;
80
a2c34371 81 my $disklist = get_disks($dev, 1, 1);
cbba9b5b
DC
82
83 die "'$disk' is not a valid local disk\n" if !defined($disklist->{$dev});
84 return 1 if $disklist->{$dev}->{used};
85
86 return 0;
87}
88
89sub get_smart_data {
dd902da7 90 my ($disk, $healthonly) = @_;
cbba9b5b
DC
91
92 assert_blockdev($disk);
93 my $smartdata = {};
dc1311cb 94 my $type;
cbba9b5b 95
dd902da7
DC
96 my $cmd = [$SMARTCTL, '-H'];
97 push @$cmd, '-A', '-f', 'brief' if !$healthonly;
98 push @$cmd, $disk;
99
9aff3f3d
TL
100 my $returncode = eval {
101 run_command($cmd, noerr => 1, outfunc => sub {
cbba9b5b
DC
102 my ($line) = @_;
103
1c999553
FG
104# ATA SMART attributes, e.g.:
105# ID# ATTRIBUTE_NAME FLAGS VALUE WORST THRESH FAIL RAW_VALUE
106# 1 Raw_Read_Error_Rate POSR-K 100 100 000 - 0
dc1311cb
FG
107#
108# SAS and NVME disks, e.g.:
109# Data Units Written: 5,584,952 [2.85 TB]
110# Accumulated start-stop cycles: 34
111
bd54091c 112 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 113 my $entry = {};
bd54091c 114
1c999553
FG
115 $entry->{name} = $2 if defined $2;
116 $entry->{flags} = $3 if defined $3;
cbba9b5b 117 # the +0 makes a number out of the strings
47d1125b 118 # FIXME: 'value' is depreacated by 'normalized'; remove with PVE 7.0
1c999553 119 $entry->{value} = $4+0 if defined $4;
4c86c711 120 $entry->{normalized} = $4+0 if defined $4;
1c999553 121 $entry->{worst} = $5+0 if defined $5;
bd54091c
DC
122 # some disks report the default threshold as --- instead of 000
123 if (defined($6) && $6 eq '---') {
124 $entry->{threshold} = 0;
125 } else {
126 $entry->{threshold} = $6+0 if defined $6;
127 }
1c999553
FG
128 $entry->{fail} = $7 if defined $7;
129 $entry->{raw} = $8 if defined $8;
130 $entry->{id} = $1 if defined $1;
cbba9b5b 131 push @{$smartdata->{attributes}}, $entry;
5db2d529 132 } elsif ($line =~ m/(?:Health Status|self\-assessment test result): (.*)$/ ) {
cbba9b5b
DC
133 $smartdata->{health} = $1;
134 } elsif ($line =~ m/Vendor Specific SMART Attributes with Thresholds:/) {
dc1311cb
FG
135 $type = 'ata';
136 delete $smartdata->{text};
137 } elsif ($line =~ m/=== START OF (READ )?SMART DATA SECTION ===/) {
138 $type = 'text';
139 } elsif (defined($type) && $type eq 'text') {
140 $smartdata->{text} = '' if !defined $smartdata->{text};
141 $smartdata->{text} .= "$line\n";
2c048efd
DC
142 # extract wearout from nvme/sas text, allow for decimal values
143 if ($line =~ m/Percentage Used(?: endurance indicator)?:\s*(\d+(?:\.\d+)?)\%/i) {
ea928fd4
DC
144 $smartdata->{wearout} = 100 - $1;
145 }
dd902da7
DC
146 } elsif ($line =~ m/SMART Disabled/) {
147 $smartdata->{health} = "SMART Disabled";
cbba9b5b 148 }
9aff3f3d 149 })
cbba9b5b 150 };
9018a4e6
DC
151 my $err = $@;
152
9aff3f3d 153 # bit 0 and 1 mark a fatal error, other bits are for disk status -> ignore (see man 8 smartctl)
9018a4e6
DC
154 if ((defined($returncode) && ($returncode & 0b00000011)) || $err) {
155 die "Error getting S.M.A.R.T. data: Exit code: $returncode\n";
156 }
dc1311cb
FG
157
158 $smartdata->{type} = $type;
159
cbba9b5b
DC
160 return $smartdata;
161}
162
9aff3f3d 163sub get_lsblk_info {
59c03cd9 164 my $cmd = [$LSBLK, '--json', '-o', 'path,parttype,fstype'];
8cd6d7e8 165 my $output = "";
9aff3f3d 166 eval { run_command($cmd, outfunc => sub { $output .= "$_[0]\n"; }) };
8cd6d7e8 167 warn "$@\n" if $@;
9aff3f3d 168 return {} if $output eq '';
8cd6d7e8 169
9aff3f3d 170 my $parsed = eval { decode_json($output) } // {};
8cd6d7e8
DC
171 warn "$@\n" if $@;
172 my $list = $parsed->{blockdevices} // [];
173
9aff3f3d
TL
174 return {
175 map {
176 $_->{path} => {
177 parttype => $_->{parttype},
178 fstype => $_->{fstype}
179 }
180 } @{$list}
181 };
8cd6d7e8
DC
182}
183
9aff3f3d 184my sub get_devices_by_partuuid {
b6bbc2ab 185 my ($lsblk_info, $uuids, $res) = @_;
8cd6d7e8
DC
186
187 $res = {} if !defined($res);
188
b6bbc2ab
FE
189 foreach my $dev (sort keys %{$lsblk_info}) {
190 my $uuid = $lsblk_info->{$dev}->{parttype};
191 next if !defined($uuid) || !defined($uuids->{$uuid});
192 $res->{$dev} = $uuids->{$uuid};
8cd6d7e8
DC
193 }
194
195 return $res;
9aff3f3d 196}
8cd6d7e8 197
cbba9b5b 198sub get_zfs_devices {
b6bbc2ab 199 my ($lsblk_info) = @_;
8cd6d7e8 200 my $res = {};
cbba9b5b 201
525b4a6e 202 return {} if !check_bin($ZPOOL);
4526dffa 203
9aff3f3d 204 # use zpool and parttype uuid, because log and cache do not have zfs type uuid
cbba9b5b
DC
205 eval {
206 run_command([$ZPOOL, 'list', '-HPLv'], outfunc => sub {
207 my ($line) = @_;
cbba9b5b 208 if ($line =~ m|^\t([^\t]+)\t|) {
8cd6d7e8 209 $res->{$1} = 1;
cbba9b5b
DC
210 }
211 });
212 };
213
9aff3f3d 214 # only warn here, because maybe zfs tools are not installed
cbba9b5b
DC
215 warn "$@\n" if $@;
216
8cd6d7e8
DC
217 my $uuids = {
218 "6a898cc3-1dd2-11b2-99a6-080020736631" => 1, # apple
219 "516e7cba-6ecf-11d6-8ff8-00022d09712b" => 1, # bsd
220 };
cbba9b5b 221
cbba9b5b 222
9aff3f3d 223 $res = get_devices_by_partuuid($lsblk_info, $uuids, $res);
8cd6d7e8
DC
224
225 return $res;
cbba9b5b
DC
226}
227
228sub get_lvm_devices {
b6bbc2ab 229 my ($lsblk_info) = @_;
8cd6d7e8 230 my $res = {};
cbba9b5b
DC
231 eval {
232 run_command([$PVS, '--noheadings', '--readonly', '-o', 'pv_name'], outfunc => sub{
233 my ($line) = @_;
234 $line = trim($line);
235 if ($line =~ m|^/dev/|) {
8cd6d7e8 236 $res->{$line} = 1;
cbba9b5b
DC
237 }
238 });
239 };
240
9aff3f3d 241 # if something goes wrong, we do not want to give up, but indicate an error has occurred
cbba9b5b
DC
242 warn "$@\n" if $@;
243
8cd6d7e8
DC
244 my $uuids = {
245 "e6d6d379-f507-44c2-a23c-238f2a3df928" => 1,
246 };
cbba9b5b 247
9aff3f3d 248 $res = get_devices_by_partuuid($lsblk_info, $uuids, $res);
cbba9b5b 249
8cd6d7e8 250 return $res;
cbba9b5b
DC
251}
252
253sub get_ceph_journals {
b6bbc2ab 254 my ($lsblk_info) = @_;
8cd6d7e8
DC
255 my $res = {};
256
257 my $uuids = {
258 '45b0969e-9b03-4f30-b4c6-b4b80ceff106' => 1, # journal
259 '30cd0809-c2b2-499c-8879-2d6b78529876' => 2, # db
260 '5ce17fce-4087-4169-b7ff-056cc58473f9' => 3, # wal
261 'cafecafe-9b03-4f30-b4c6-b4b80ceff106' => 4, # block
262 };
263
9aff3f3d 264 $res = get_devices_by_partuuid($lsblk_info, $uuids, $res);
cbba9b5b 265
8cd6d7e8 266 return $res;
cbba9b5b
DC
267}
268
19dcd1ad
DC
269# reads the lv_tags and matches them with the devices
270sub get_ceph_volume_infos {
271 my $result = {};
272
248f43f5
TL
273 my $cmd = [ $LVS, '-S', 'lv_name=~^osd-', '-o', 'devices,lv_name,lv_tags',
274 '--noheadings', '--readonly', '--separator', ';' ];
19dcd1ad
DC
275
276 run_command($cmd, outfunc => sub {
277 my $line = shift;
248f43f5
TL
278 $line =~ s/(?:^\s+)|(?:\s+$)//g; # trim whitespaces
279
280 my $fields = [ split(';', $line) ];
19dcd1ad
DC
281
282 # lvs syntax is /dev/sdX(Y) where Y is the start (which we do not need)
41f93ece 283 my ($dev) = $fields->[0] =~ m|^(/dev/[a-z]+[^(]*)|;
19dcd1ad
DC
284 if ($fields->[1] =~ m|^osd-([^-]+)-|) {
285 my $type = $1;
248f43f5 286 # $result autovivification is wanted, to not creating empty hashes
79f4a7bf 287 if (($type eq 'block' || $type eq 'data') && $fields->[2] =~ m/ceph.osd_id=([^,]+)/) {
19dcd1ad
DC
288 $result->{$dev}->{osdid} = $1;
289 $result->{$dev}->{bluestore} = ($type eq 'block');
bfb3d42d
DC
290 if ($fields->[2] =~ m/ceph\.encrypted=1/) {
291 $result->{$dev}->{encrypted} = 1;
292 }
19dcd1ad 293 } else {
248f43f5 294 # undef++ becomes '1' (see `perldoc perlop`: Auto-increment)
19dcd1ad
DC
295 $result->{$dev}->{$type}++;
296 }
297 }
298 });
299
300 return $result;
301}
302
cbba9b5b
DC
303sub get_udev_info {
304 my ($dev) = @_;
305
306 my $info = "";
307 my $data = {};
308 eval {
d3a5e309 309 run_command(['udevadm', 'info', '-p', $dev, '--query', 'all'], outfunc => sub {
cbba9b5b
DC
310 my ($line) = @_;
311 $info .= "$line\n";
312 });
313 };
314 warn $@ if $@;
9aff3f3d 315 return if !$info;
cbba9b5b 316
9aff3f3d
TL
317 return if $info !~ m/^E: DEVTYPE=(disk|partition)$/m;
318 return if $info =~ m/^E: ID_CDROM/m;
cbba9b5b 319
9aff3f3d 320 # we use this, because some disks are not simply in /dev e.g. /dev/cciss/c0d0
cbba9b5b
DC
321 if ($info =~ m/^E: DEVNAME=(\S+)$/m) {
322 $data->{devpath} = $1;
323 }
324 return if !defined($data->{devpath});
325
326 $data->{serial} = 'unknown';
9aff3f3d 327 $data->{serial} = $1 if $info =~ m/^E: ID_SERIAL_SHORT=(\S+)$/m;
cbba9b5b 328
9aff3f3d 329 $data->{gpt} = $info =~ m/^E: ID_PART_TABLE_TYPE=gpt$/m ? 1 : 0;
cbba9b5b 330
cbba9b5b 331 $data->{rpm} = -1;
9aff3f3d 332 $data->{rpm} = $1 if $info =~ m/^E: ID_ATA_ROTATION_RATE_RPM=(\d+)$/m; # detects SSD implicit
cbba9b5b 333
9aff3f3d 334 $data->{usb} = 1 if $info =~ m/^E: ID_BUS=usb$/m;
cbba9b5b 335
9aff3f3d 336 $data->{model} = $1 if $info =~ m/^E: ID_MODEL=(.+)$/m;
865bdbd9 337
cbba9b5b 338 $data->{wwn} = 'unknown';
9aff3f3d 339 $data->{wwn} = $1 if $info =~ m/^E: ID_WWN=(.*)$/m;
cbba9b5b 340
0f0d99a3
SI
341 if ($info =~ m/^E: DEVLINKS=(.+)$/m) {
342 my @devlinks = grep(m#^/dev/disk/by-id/(ata|scsi|nvme(?!-eui))#, split (/ /, $1));
343 $data->{by_id_link} = $devlinks[0] if defined($devlinks[0]);
344 }
345
cbba9b5b
DC
346 return $data;
347}
348
40be5c5c
FE
349sub get_sysdir_size {
350 my ($sysdir) = @_;
351
352 my $size = file_read_firstline("$sysdir/size");
353 return if !$size;
354
9aff3f3d 355 # linux always considers sectors to be 512 bytes, independently of real block size
40be5c5c
FE
356 return $size * 512;
357}
358
cbba9b5b
DC
359sub get_sysdir_info {
360 my ($sysdir) = @_;
361
9aff3f3d 362 return if ! -d "$sysdir/device";
461a9fd8 363
cbba9b5b
DC
364 my $data = {};
365
40be5c5c 366 $data->{size} = get_sysdir_size($sysdir) or return;
cbba9b5b
DC
367
368 # dir/queue/rotational should be 1 for hdd, 0 for ssd
571b6f26 369 $data->{rotational} = file_read_firstline("$sysdir/queue/rotational") // -1;
cbba9b5b
DC
370
371 $data->{vendor} = file_read_firstline("$sysdir/device/vendor") || 'unknown';
372 $data->{model} = file_read_firstline("$sysdir/device/model") || 'unknown';
373
374 return $data;
375}
376
6965a670 377sub get_wear_leveling_info {
dbad606d 378 my ($smartdata) = @_;
ea928fd4
DC
379 my $attributes = $smartdata->{attributes};
380
381 if (defined($smartdata->{wearout})) {
382 return $smartdata->{wearout};
383 }
6965a670
DC
384
385 my $wearout;
386
9aff3f3d
TL
387 # Common register names that represent percentage values of potential failure indicators used
388 # in drivedb.h of smartmontool's. Order matters, as some drives may have multiple definitions
dbad606d
JJS
389 my @wearoutregisters = (
390 "Media_Wearout_Indicator",
391 "SSD_Life_Left",
392 "Wear_Leveling_Count",
393 "Perc_Write\/Erase_Ct_BC",
394 "Perc_Rated_Life_Remain",
395 "Remaining_Lifetime_Perc",
396 "Percent_Lifetime_Remain",
397 "Lifetime_Left",
398 "PCT_Life_Remaining",
399 "Lifetime_Remaining",
400 "Percent_Life_Remaining",
401 "Percent_Lifetime_Used",
402 "Perc_Rated_Life_Used"
403 );
404
405 # Search for S.M.A.R.T. attributes for known register
406 foreach my $register (@wearoutregisters) {
407 last if defined $wearout;
408 foreach my $attr (@$attributes) {
409 next if $attr->{name} !~ m/$register/;
410 $wearout = $attr->{value};
411 last;
6965a670
DC
412 }
413 }
414
6965a670
DC
415 return $wearout;
416}
417
10a48db5
DC
418sub dir_is_empty {
419 my ($dir) = @_;
420
421 my $dh = IO::Dir->new ($dir);
422 return 1 if !$dh;
423
424 while (defined(my $tmp = $dh->read)) {
425 next if $tmp eq '.' || $tmp eq '..';
426 $dh->close;
427 return 0;
428 }
429 $dh->close;
430 return 1;
431}
432
eebcdb11
DC
433sub is_iscsi {
434 my ($sysdir) = @_;
435
436 if (-l $sysdir && readlink($sysdir) =~ m|host[^/]*/session[^/]*|) {
437 return 1;
438 }
439
440 return 0;
441}
442
4731eb11
TL
443my sub is_ssdlike {
444 my ($type) = @_;
445 return $type eq 'ssd' || $type eq 'nvme';
446}
447
7e14102a 448sub mounted_blockdevs {
cbba9b5b
DC
449 my $mounted = {};
450
451 my $mounts = PVE::ProcFSTools::parse_proc_mounts();
452
453 foreach my $mount (@$mounts) {
454 next if $mount->[0] !~ m|^/dev/|;
455 $mounted->{abs_path($mount->[0])} = $mount->[1];
456 };
457
7e14102a
FE
458 return $mounted;
459}
460
4de60025
AL
461# returns hashmap of abs mount path -> first part of /proc/mounts (what)
462sub mounted_paths {
463 my $mounted = {};
464
465 my $mounts = PVE::ProcFSTools::parse_proc_mounts();
466
467 foreach my $mount (@$mounts) {
468 $mounted->{abs_path($mount->[1])} = $mount->[0];
469 };
470
471 return $mounted;
472}
473
7e14102a
FE
474sub get_disks {
475 my ($disks, $nosmart, $include_partitions) = @_;
476 my $disklist = {};
477
478 my $mounted = mounted_blockdevs();
479
b6bbc2ab 480 my $lsblk_info = get_lsblk_info();
8cd6d7e8 481
b6bbc2ab 482 my $journalhash = get_ceph_journals($lsblk_info);
19dcd1ad 483 my $ceph_volume_infos = get_ceph_volume_infos();
cbba9b5b 484
b6bbc2ab 485 my $zfshash = get_zfs_devices($lsblk_info);
cbba9b5b 486
b6bbc2ab 487 my $lvmhash = get_lvm_devices($lsblk_info);
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
5045e0b7 497 $_ =~ s|cciss/|cciss!| for @$disks;
52a064af 498
a64aedd3
FE
499 if ($include_partitions) {
500 # Proper blockdevice is needed for the regex, use parent for partitions.
501 for my $disk ($disks->@*) {
502 next if !is_partition("/dev/$disk");
503 $disk = strip_dev(get_blockdev("/dev/$disk"));
504 }
505 }
506
52a064af 507 $disk_regex = "(?:" . join('|', @$disks) . ")";
1590fc13
DC
508 }
509
52a064af 510 dir_glob_foreach('/sys/block', $disk_regex, sub {
cbba9b5b 511 my ($dev) = @_;
cbba9b5b 512 # whitelisting following devices
9aff3f3d
TL
513 # - hdX ide block device
514 # - sdX scsi/sata block device
515 # - vdX virtIO block device
516 # - xvdX: xen virtual block device
517 # - nvmeXnY: nvme devices
518 # - cciss!cXnY cciss devices
cbba9b5b
DC
519 return if $dev !~ m/^(h|s|x?v)d[a-z]+$/ &&
520 $dev !~ m/^nvme\d+n\d+$/ &&
38ddd4ce 521 $dev !~ m/^cciss\!c\d+d\d+$/;
cbba9b5b 522
9aff3f3d 523 my $data = get_udev_info("/sys/block/$dev") // return;
cbba9b5b
DC
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
9aff3f3d 549 my ($health, $wearout) = ('UNKNOWN', 'N/A');
7a98a62d
FG
550 if (!$nosmart) {
551 eval {
4731eb11 552 my $smartdata = get_smart_data($devpath, !is_ssdlike($type));
dd902da7
DC
553 $health = $smartdata->{health} if $smartdata->{health};
554
9aff3f3d
TL
555 if (is_ssdlike($type)) { # if we have an ssd we try to get the wearout indicator
556 my $wear_level = get_wear_leveling_info($smartdata);
557 $wearout = $wear_level if defined($wear_level);
acd3d916 558 }
7a98a62d
FG
559 };
560 }
cbba9b5b 561
9aff3f3d
TL
562 # we replaced cciss/ with cciss! above, but in the result we need cciss/ again because the
563 # caller might want to check the result again with the original parameter
fc7c0e05
DC
564 if ($dev =~ m|^cciss!|) {
565 $dev =~ s|^cciss!|cciss/|;
566 }
567
cbba9b5b
DC
568 $disklist->{$dev} = {
569 vendor => $sysdata->{vendor},
865bdbd9 570 model => $data->{model} || $sysdata->{model},
cbba9b5b
DC
571 size => $sysdata->{size},
572 serial => $data->{serial},
573 gpt => $data->{gpt},
574 rpm => $data->{rpm},
575 type => $type,
576 wwn => $data->{wwn},
577 health => $health,
578 devpath => $devpath,
579 wearout => $wearout,
580 };
2949acd6 581 $disklist->{$dev}->{mounted} = 1 if exists $mounted->{$devpath};
cbba9b5b 582
0f0d99a3
SI
583 my $by_id_link = $data->{by_id_link};
584 $disklist->{$dev}->{by_id_link} = $by_id_link if defined($by_id_link);
585
9aff3f3d
TL
586 my ($osdid, $bluestore, $osdencrypted) = (-1, 0, 0);
587 my ($journal_count, $db_count, $wal_count) = (0, 0, 0);
cbba9b5b 588
cbba9b5b 589 my $partpath = $devpath;
9aff3f3d 590 # remove trailing part to get the partition base path, e.g. /dev/cciss/c0d0 -> /dev/cciss
cbba9b5b
DC
591 $partpath =~ s/\/[^\/]+$//;
592
01aa7d75
FE
593 my $determine_usage = sub {
594 my ($devpath, $sysdir, $is_partition) = @_;
595
596 return 'LVM' if $lvmhash->{$devpath};
597 return 'ZFS' if $zfshash->{$devpath};
598
599 my $info = $lsblk_info->{$devpath} // {};
d3857eeb 600
9aff3f3d
TL
601 if (defined(my $parttype = $info->{parttype})) {
602 return 'BIOS boot'if $parttype eq '21686148-6449-6e6f-744e-656564454649';
603 return 'EFI' if $parttype eq 'c12a7328-f81f-11d2-ba4b-00a0c93ec93b';
604 return 'ZFS reserved' if $parttype eq '6a945a3b-1dd2-11b2-99a6-080020736631';
d3857eeb
FE
605 }
606
9aff3f3d 607 return "$info->{fstype}" if defined($info->{fstype});
01aa7d75
FE
608 return 'mounted' if $mounted->{$devpath};
609
610 return if !$is_partition;
611
612 # for devices, this check is done explicitly later
613 return 'Device Mapper' if !dir_is_empty("$sysdir/holders");
614
ff91cfae 615 return; # unused partition
01aa7d75
FE
616 };
617
41f93ece
FE
618 my $collect_ceph_info = sub {
619 my ($devpath) = @_;
620
621 my $ceph_volume = $ceph_volume_infos->{$devpath} or return;
622 $journal_count += $ceph_volume->{journal} // 0;
623 $db_count += $ceph_volume->{db} // 0;
624 $wal_count += $ceph_volume->{wal} // 0;
625 if (defined($ceph_volume->{osdid})) {
626 $osdid = $ceph_volume->{osdid};
627 $bluestore = 1 if $ceph_volume->{bluestore};
628 $osdencrypted = 1 if $ceph_volume->{encrypted};
629 }
6a1919b1
FE
630
631 my $result = { %{$ceph_volume} };
9aff3f3d 632 $result->{journals} = delete $result->{journal} if $result->{journal};
6a1919b1 633 return $result;
41f93ece
FE
634 };
635
89c27ea8 636 my $partitions = {};
cbba9b5b
DC
637 dir_glob_foreach("$sysdir", "$dev.+", sub {
638 my ($part) = @_;
639
6a1919b1
FE
640 $partitions->{$part} = $collect_ceph_info->("$partpath/$part");
641 my $lvm_based_osd = defined($partitions->{$part});
642
89c27ea8 643 $partitions->{$part}->{devpath} = "$partpath/$part";
2949c537 644 $partitions->{$part}->{parent} = "$devpath";
2949acd6 645 $partitions->{$part}->{mounted} = 1 if exists $mounted->{"$partpath/$part"};
89c27ea8 646 $partitions->{$part}->{gpt} = $data->{gpt};
31ed94cc 647 $partitions->{$part}->{type} = 'partition';
9aff3f3d
TL
648 $partitions->{$part}->{size} = get_sysdir_size("$sysdir/$part") // 0;
649 $partitions->{$part}->{used} = $determine_usage->("$partpath/$part", "$sysdir/$part", 1);
6a1919b1 650 $partitions->{$part}->{osdid} //= -1;
41f93ece 651
9aff3f3d 652 # avoid counting twice (e.g. partition with the LVM for the DB OSD is in $journalhash)
41f93ece
FE
653 return if $lvm_based_osd;
654
655 # Legacy handling for non-LVM based OSDs
0cca5356 656 if (my $mp = $mounted->{"$partpath/$part"}) {
cbba9b5b
DC
657 if ($mp =~ m|^/var/lib/ceph/osd/ceph-(\d+)$|) {
658 $osdid = $1;
6a1919b1 659 $partitions->{$part}->{osdid} = $osdid;
cbba9b5b
DC
660 }
661 }
662
0180fa42
TL
663 if (my $journal_part = $journalhash->{"$partpath/$part"}) {
664 $journal_count++ if $journal_part == 1;
665 $db_count++ if $journal_part == 2;
666 $wal_count++ if $journal_part == 3;
667 $bluestore = 1 if $journal_part == 4;
6a1919b1
FE
668
669 $partitions->{$part}->{journals} = 1 if $journal_part == 1;
670 $partitions->{$part}->{db} = 1 if $journal_part == 2;
671 $partitions->{$part}->{wal} = 1 if $journal_part == 3;
672 $partitions->{$part}->{bluestore} = 1 if $journal_part == 4;
e2bd817c 673 }
cbba9b5b
DC
674 });
675
01aa7d75 676 my $used = $determine_usage->($devpath, $sysdir, 0);
2949c537
FE
677 if (!$include_partitions) {
678 foreach my $part (sort keys %{$partitions}) {
2949c537
FE
679 $used //= $partitions->{$part}->{used};
680 }
415dc398
FE
681 } else {
682 # fstype might be set even if there are partitions, but showing that is confusing
683 $used = 'partitions' if scalar(keys %{$partitions});
01aa7d75
FE
684 }
685 $used //= 'partitions' if scalar(keys %{$partitions});
cbba9b5b
DC
686 # multipath, software raid, etc.
687 # this check comes in last, to show more specific info
688 # if we have it
01aa7d75 689 $used //= 'Device Mapper' if !dir_is_empty("$sysdir/holders");
cbba9b5b
DC
690
691 $disklist->{$dev}->{used} = $used if $used;
41f93ece
FE
692
693 $collect_ceph_info->($devpath);
694
cbba9b5b 695 $disklist->{$dev}->{osdid} = $osdid;
e2bd817c
DC
696 $disklist->{$dev}->{journals} = $journal_count if $journal_count;
697 $disklist->{$dev}->{bluestore} = $bluestore if $osdid != -1;
bfb3d42d 698 $disklist->{$dev}->{osdencrypted} = $osdencrypted if $osdid != -1;
e2bd817c
DC
699 $disklist->{$dev}->{db} = $db_count if $db_count;
700 $disklist->{$dev}->{wal} = $wal_count if $wal_count;
2949c537
FE
701
702 if ($include_partitions) {
9aff3f3d 703 $disklist->{$_} = $partitions->{$_} for keys %{$partitions};
2949c537 704 }
cbba9b5b
DC
705 });
706
707 return $disklist;
cbba9b5b
DC
708}
709
3196c387
WL
710sub get_partnum {
711 my ($part_path) = @_;
712
92ae59df 713 my $st = stat($part_path);
3196c387 714
ceb7b1ed
FE
715 die "error detecting block device '$part_path'\n"
716 if !$st || !$st->mode || !S_ISBLK($st->mode) || !$st->rdev;
717
92ae59df
AA
718 my $major = PVE::Tools::dev_t_major($st->rdev);
719 my $minor = PVE::Tools::dev_t_minor($st->rdev);
3196c387
WL
720 my $partnum_path = "/sys/dev/block/$major:$minor/";
721
9aff3f3d 722 my $partnum = file_read_firstline("${partnum_path}partition");
481f6177 723 die "Partition does not exist\n" if !defined($partnum);
9aff3f3d
TL
724 die "Failed to get partition number\n" if $partnum !~ m/(\d+)/; # untaint
725 $partnum = $1;
726 die "Partition number $partnum is invalid\n" if $partnum > 128;
3196c387
WL
727
728 return $partnum;
729}
730
0d28307d
WL
731sub get_blockdev {
732 my ($part_path) = @_;
733
1207620c
TL
734 my ($dev, $block_dev);
735 if ($part_path =~ m|^/dev/(.*)$|) {
736 $dev = $1;
737 my $link = readlink "/sys/class/block/$dev";
738 $block_dev = $1 if $link =~ m|([^/]*)/$dev$|;
739 }
0d28307d
WL
740
741 die "Can't parse parent device\n" if !defined($block_dev);
742 die "No valid block device\n" if index($dev, $block_dev) == -1;
743
744 $block_dev = "/dev/$block_dev";
ffc31266 745 die "Block device does not exists\n" if !(-b $block_dev);
0d28307d
WL
746
747 return $block_dev;
748}
749
e8df8fb1
FE
750sub is_partition {
751 my ($dev_path) = @_;
752
753 return defined(eval { get_partnum($dev_path) });
754}
755
e39e8ee2
DC
756sub locked_disk_action {
757 my ($sub) = @_;
758 my $res = PVE::Tools::lock_file('/run/lock/pve-diskmanage.lck', undef, $sub);
759 die $@ if $@;
760 return $res;
761}
762
0370861c 763sub assert_disk_unused {
76c1e57b 764 my ($dev) = @_;
0370861c 765 die "device '$dev' is already in use\n" if disk_is_used($dev);
9aff3f3d 766 return;
76c1e57b
DC
767}
768
1dc3038d
DC
769sub append_partition {
770 my ($dev, $size) = @_;
771
772 my $devname = $dev;
773 $devname =~ s|^/dev/||;
774
775 my $newpartid = 1;
776 dir_glob_foreach("/sys/block/$devname", qr/\Q$devname\E.*?(\d+)/, sub {
777 my ($part, $partid) = @_;
778
779 if ($partid >= $newpartid) {
780 $newpartid = $partid + 1;
781 }
782 });
783
784 $size = PVE::Tools::convert_size($size, 'b' => 'mb');
785
786 run_command([ $SGDISK, '-n', "$newpartid:0:+${size}M", $dev ],
787 errmsg => "error creating partition '$newpartid' on '$dev'");
788
789 my $partition;
790
ffc31266 791 # loop again to detect the real partition device which does not always follow
1dc3038d
DC
792 # a strict $devname$partition scheme like /dev/nvme0n1 -> /dev/nvme0n1p1
793 dir_glob_foreach("/sys/block/$devname", qr/\Q$devname\E.*$newpartid/, sub {
794 my ($part) = @_;
795
796 $partition = "/dev/$part";
797 });
798
799 return $partition;
800}
801
cb057e21
FE
802# Check if a disk or any of its partitions has a holder.
803# Can also be called with a partition.
804# Expected to be called with a result of verify_blockdev_path().
805sub has_holder {
806 my ($devpath) = @_;
807
70dc7098 808 my $dev = strip_dev($devpath);
cb057e21 809
70dc7098 810 return $devpath if !dir_is_empty("/sys/class/block/${dev}/holders");
cb057e21
FE
811
812 my $found;
cb057e21
FE
813 dir_glob_foreach("/sys/block/${dev}", "${dev}.+", sub {
814 my ($part) = @_;
70dc7098 815 $found = "/dev/${part}" if !dir_is_empty("/sys/class/block/${part}/holders");
cb057e21
FE
816 });
817
818 return $found;
819}
820
3bf7f889
FE
821# Basic check if a disk or any of its partitions is mounted.
822# Can also be called with a partition.
823# Expected to be called with a result of verify_blockdev_path().
824sub is_mounted {
825 my ($devpath) = @_;
826
827 my $mounted = mounted_blockdevs();
828
829 return $devpath if $mounted->{$devpath};
830
70dc7098 831 my $dev = strip_dev($devpath);
3bf7f889
FE
832
833 my $found;
3bf7f889
FE
834 dir_glob_foreach("/sys/block/${dev}", "${dev}.+", sub {
835 my ($part) = @_;
3bf7f889
FE
836 my $partpath = "/dev/${part}";
837
838 $found = $partpath if $mounted->{$partpath};
839 });
840
841 return $found;
842}
843
e8df8fb1
FE
844# Currently only supports GPT-partitioned disks.
845sub change_parttype {
846 my ($partpath, $parttype) = @_;
847
848 my $err = "unable to change partition type for $partpath";
849
850 my $partnum = get_partnum($partpath);
851 my $blockdev = get_blockdev($partpath);
852 my $dev = strip_dev($blockdev);
853
854 my $info = get_disks($dev, 1);
855 die "$err - unable to get disk info for '$blockdev'\n" if !defined($info->{$dev});
856 die "$err - disk '$blockdev' is not GPT partitioned\n" if !$info->{$dev}->{gpt};
857
858 run_command(['sgdisk', "-t${partnum}:${parttype}", $blockdev], errmsg => $err);
859}
860
262ad7a9 861# Wipes all labels and the first 200 MiB of a disk/partition (or the whole if it is smaller).
bd46e59b 862# If called with a partition, also sets the partition type to 0x83 'Linux filesystem'.
262ad7a9
FE
863# Expected to be called with a result of verify_blockdev_path().
864sub wipe_blockdev {
865 my ($devpath) = @_;
866
262ad7a9
FE
867 my $devname = basename($devpath);
868 my $dev_size = PVE::Tools::file_get_contents("/sys/class/block/$devname/size");
869
870 ($dev_size) = $dev_size =~ m|(\d+)|; # untaint $dev_size
871 die "Couldn't get the size of the device $devname\n" if !defined($dev_size);
872
873 my $size = ($dev_size * 512 / 1024 / 1024);
874 my $count = ($size < 200) ? $size : 200;
875
839afff8
TL
876 my $to_wipe = [];
877 dir_glob_foreach("/sys/class/block/${devname}", "${devname}.+", sub {
878 my ($part) = @_;
879 push $to_wipe->@*, "/dev/${part}" if -b "/dev/${part}";
880 });
881
f7a95153 882 if (scalar($to_wipe->@*) > 0) {
d9381782 883 print "found child partitions to wipe: ". join(', ', $to_wipe->@*) ."\n";
839afff8
TL
884 }
885 push $to_wipe->@*, $devpath; # put actual device last
886
887 print "wiping block device ${devpath}\n";
262ad7a9 888
839afff8 889 run_command(['wipefs', '--all', $to_wipe->@*], errmsg => "error wiping '${devpath}'");
fa6d05ab
TL
890
891 run_command(
892 ['dd', 'if=/dev/zero', "of=${devpath}", 'bs=1M', 'conv=fdatasync', "count=${count}"],
893 errmsg => "error wiping '${devpath}'",
894 );
bd46e59b
FE
895
896 if (is_partition($devpath)) {
897 eval { change_parttype($devpath, '8300'); };
898 warn $@ if $@;
899 }
262ad7a9
FE
900}
901
26082b7d
FE
902# FIXME: Remove once we depend on systemd >= v249.
903# Work around udev bug https://github.com/systemd/systemd/issues/18525 ensuring database is updated.
904sub udevadm_trigger {
905 my @devs = @_;
906
907 return if scalar(@devs) == 0;
908
909 eval { run_command(['udevadm', 'trigger', @devs]); };
910 warn $@ if $@;
911}
912
cbba9b5b 9131;