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