]> git.proxmox.com Git - pve-storage.git/blob - PVE/Diskmanage.pm
fix #2777 create zpools with stable dev paths
[pve-storage.git] / PVE / Diskmanage.pm
1 package PVE::Diskmanage;
2
3 use strict;
4 use warnings;
5 use PVE::ProcFSTools;
6 use Data::Dumper;
7 use Cwd qw(abs_path);
8 use Fcntl ':mode';
9 use File::stat;
10 use JSON;
11
12 use PVE::Tools qw(extract_param run_command file_get_contents file_read_firstline dir_glob_regex dir_glob_foreach trim);
13
14 my $SMARTCTL = "/usr/sbin/smartctl";
15 my $ZPOOL = "/sbin/zpool";
16 my $SGDISK = "/sbin/sgdisk";
17 my $PVS = "/sbin/pvs";
18 my $LVS = "/sbin/lvs";
19 my $UDEVADM = "/bin/udevadm";
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
112 $entry->{name} = $2 if defined $2;
113 $entry->{flags} = $3 if defined $3;
114 # the +0 makes a number out of the strings
115 $entry->{value} = $4+0 if defined $4;
116 $entry->{worst} = $5+0 if defined $5;
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 }
123 $entry->{fail} = $7 if defined $7;
124 $entry->{raw} = $8 if defined $8;
125 $entry->{id} = $1 if defined $1;
126 push @{$smartdata->{attributes}}, $entry;
127 } elsif ($line =~ m/(?:Health Status|self\-assessment test result): (.*)$/ ) {
128 $smartdata->{health} = $1;
129 } elsif ($line =~ m/Vendor Specific SMART Attributes with Thresholds:/) {
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";
137 # extract wearout from nvme text, allow for decimal values
138 if ($line =~ m/Percentage Used:\s*(\d+(?:\.\d+)?)\%/i) {
139 $smartdata->{wearout} = 100 - $1;
140 }
141 } elsif ($line =~ m/SMART Disabled/) {
142 $smartdata->{health} = "SMART Disabled";
143 }
144 });
145 };
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 }
154
155 $smartdata->{type} = $type;
156
157 return $smartdata;
158 }
159
160 sub 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
187 my $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
199 sub get_zfs_devices {
200 my ($parttype_map) = @_;
201 my $res = {};
202
203 return {} if ! -x $ZPOOL;
204
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|) {
213 $res->{$1} = 1;
214 }
215 });
216 };
217
218 # only warn here,
219 # because maybe zfs tools are not installed
220 warn "$@\n" if $@;
221
222 my $uuids = {
223 "6a898cc3-1dd2-11b2-99a6-080020736631" => 1, # apple
224 "516e7cba-6ecf-11d6-8ff8-00022d09712b" => 1, # bsd
225 };
226
227
228 $res = $get_devices_by_partuuid->($parttype_map, $uuids, $res);
229
230 return $res;
231 }
232
233 sub get_lvm_devices {
234 my ($parttype_map) = @_;
235 my $res = {};
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/|) {
241 $res->{$line} = 1;
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
250 my $uuids = {
251 "e6d6d379-f507-44c2-a23c-238f2a3df928" => 1,
252 };
253
254 $res = $get_devices_by_partuuid->($parttype_map, $uuids, $res);
255
256 return $res;
257 }
258
259 sub get_ceph_journals {
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);
271
272 return $res;
273 }
274
275 # reads the lv_tags and matches them with the devices
276 sub get_ceph_volume_infos {
277 my $result = {};
278
279 my $cmd = [ $LVS, '-S', 'lv_name=~^osd-', '-o', 'devices,lv_name,lv_tags',
280 '--noheadings', '--readonly', '--separator', ';' ];
281
282 run_command($cmd, outfunc => sub {
283 my $line = shift;
284 $line =~ s/(?:^\s+)|(?:\s+$)//g; # trim whitespaces
285
286 my $fields = [ split(';', $line) ];
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;
292 # $result autovivification is wanted, to not creating empty hashes
293 if (($type eq 'block' || $type eq 'data') && $fields->[2] =~ m/ceph.osd_id=([^,]+)/) {
294 $result->{$dev}->{osdid} = $1;
295 $result->{$dev}->{bluestore} = ($type eq 'block');
296 if ($fields->[2] =~ m/ceph\.encrypted=1/) {
297 $result->{$dev}->{encrypted} = 1;
298 }
299 } else {
300 # undef++ becomes '1' (see `perldoc perlop`: Auto-increment)
301 $result->{$dev}->{$type}++;
302 }
303 }
304 });
305
306 return $result;
307 }
308
309 sub get_udev_info {
310 my ($dev) = @_;
311
312 my $info = "";
313 my $data = {};
314 eval {
315 run_command([$UDEVADM, 'info', '-p', $dev, '--query', 'all'], outfunc => sub {
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
353 if ($info =~ m/^E: ID_MODEL=(.+)$/m) {
354 $data->{model} = $1;
355 }
356
357 $data->{wwn} = 'unknown';
358 if ($info =~ m/^E: ID_WWN=(.*)$/m) {
359 $data->{wwn} = $1;
360 }
361
362 if ($info =~ m/^E: DEVLINKS=(.+)$/m) {
363 my @devlinks = grep(m#^/dev/disk/by-id/(ata|scsi|nvme(?!-eui))#, split (/ /, $1));
364 $data->{by_id_link} = $devlinks[0] if defined($devlinks[0]);
365 }
366
367 return $data;
368 }
369
370 sub get_sysdir_info {
371 my ($sysdir) = @_;
372
373 return undef if ! -d "$sysdir/device";
374
375 my $data = {};
376
377 my $size = file_read_firstline("$sysdir/size");
378 return undef if !$size;
379
380 # linux always considers sectors to be 512 bytes,
381 # independently of real block size
382 $data->{size} = $size * 512;
383
384 # dir/queue/rotational should be 1 for hdd, 0 for ssd
385 $data->{rotational} = file_read_firstline("$sysdir/queue/rotational") // -1;
386
387 $data->{vendor} = file_read_firstline("$sysdir/device/vendor") || 'unknown';
388 $data->{model} = file_read_firstline("$sysdir/device/model") || 'unknown';
389
390 if (defined(my $device = readlink("$sysdir/device"))) {
391 # strip directory and untaint:
392 ($data->{device}) = $device =~ m!([^/]+)$!;
393 }
394
395 return $data;
396 }
397
398 sub get_wear_leveling_info {
399 my ($smartdata, $model) = @_;
400 my $attributes = $smartdata->{attributes};
401
402 if (defined($smartdata->{wearout})) {
403 return $smartdata->{wearout};
404 }
405
406 my $wearout;
407
408 my $vendormap = {
409 'kingston' => 231,
410 'samsung' => 177,
411 'intel' => 233,
412 'sandisk' => 233,
413 'crucial' => 202,
414 'default' => 233,
415 };
416
417 # find target attr id
418
419 my $attrid;
420
421 foreach my $vendor (keys %$vendormap) {
422 if ($model =~ m/$vendor/i) {
423 $attrid = $vendormap->{$vendor};
424 # found the attribute
425 last;
426 }
427 }
428
429 if (!$attrid) {
430 $attrid = $vendormap->{default};
431 }
432
433 foreach my $attr (@$attributes) {
434 next if $attr->{id} != $attrid;
435 $wearout = $attr->{value};
436 last;
437 }
438
439 return $wearout;
440 }
441
442 sub dir_is_empty {
443 my ($dir) = @_;
444
445 my $dh = IO::Dir->new ($dir);
446 return 1 if !$dh;
447
448 while (defined(my $tmp = $dh->read)) {
449 next if $tmp eq '.' || $tmp eq '..';
450 $dh->close;
451 return 0;
452 }
453 $dh->close;
454 return 1;
455 }
456
457 sub is_iscsi {
458 my ($sysdir) = @_;
459
460 if (-l $sysdir && readlink($sysdir) =~ m|host[^/]*/session[^/]*|) {
461 return 1;
462 }
463
464 return 0;
465 }
466
467 sub get_disks {
468 my ($disks, $nosmart) = @_;
469 my $disklist = {};
470
471 my $mounted = {};
472
473 my $mounts = PVE::ProcFSTools::parse_proc_mounts();
474
475 foreach my $mount (@$mounts) {
476 next if $mount->[0] !~ m|^/dev/|;
477 $mounted->{abs_path($mount->[0])} = $mount->[1];
478 };
479
480 my $dev_is_mounted = sub {
481 my ($dev) = @_;
482 return $mounted->{$dev};
483 };
484
485 my $parttype_map = get_parttype_info();
486
487 my $journalhash = get_ceph_journals($parttype_map);
488 my $ceph_volume_infos = get_ceph_volume_infos();
489
490 my $zfshash = get_zfs_devices($parttype_map);
491
492 my $lvmhash = get_lvm_devices($parttype_map);
493
494 my $disk_regex = ".*";
495 if (defined($disks)) {
496 if (!ref($disks)) {
497 $disks = [ $disks ];
498 } elsif (ref($disks) ne 'ARRAY') {
499 die "disks is not a string or array reference\n";
500 }
501 # we get cciss/c0d0 but need cciss!c0d0
502 map { s|cciss/|cciss!| } @$disks;
503
504 $disk_regex = "(?:" . join('|', @$disks) . ")";
505 }
506
507 dir_glob_foreach('/sys/block', $disk_regex, sub {
508 my ($dev) = @_;
509 # whitelisting following devices
510 # hdX: ide block device
511 # sdX: sd block device
512 # vdX: virtual block device
513 # xvdX: xen virtual block device
514 # nvmeXnY: nvme devices
515 # cciss!cXnY: cciss devices
516 return if $dev !~ m/^(h|s|x?v)d[a-z]+$/ &&
517 $dev !~ m/^nvme\d+n\d+$/ &&
518 $dev !~ m/^cciss\!c\d+d\d+$/;
519
520 my $data = get_udev_info("/sys/block/$dev");
521 return if !defined($data);
522 my $devpath = $data->{devpath};
523
524 my $sysdir = "/sys/block/$dev";
525
526 # we do not want iscsi devices
527 return if is_iscsi($sysdir);
528
529 my $sysdata = get_sysdir_info($sysdir);
530 return if !defined($sysdata);
531
532 my $type = 'unknown';
533
534 if ($sysdata->{rotational} == 0) {
535 $type = 'ssd';
536 $data->{rpm} = 0;
537 } elsif ($sysdata->{rotational} == 1) {
538 if ($data->{rpm} != -1) {
539 $type = 'hdd';
540 } elsif ($data->{usb}) {
541 $type = 'usb';
542 $data->{rpm} = 0;
543 }
544 }
545
546 my $health = 'UNKNOWN';
547 my $wearout = 'N/A';
548
549 if (!$nosmart) {
550 eval {
551 my $smartdata = get_smart_data($devpath, ($type ne 'ssd'));
552 $health = $smartdata->{health} if $smartdata->{health};
553
554 if ($type eq 'ssd') {
555 # if we have an ssd we try to get the wearout indicator
556 my $wearval = get_wear_leveling_info($smartdata, $data->{model} || $sysdata->{model});
557 $wearout = $wearval if defined($wearval);
558 }
559 };
560 }
561
562 my $used;
563
564 $used = 'LVM' if $lvmhash->{$devpath};
565
566 $used = 'mounted' if &$dev_is_mounted($devpath);
567
568 $used = 'ZFS' if $zfshash->{$devpath};
569
570 # we replaced cciss/ with cciss! above
571 # but in the result we need cciss/ again
572 # because the caller might want to check the
573 # result again with the original parameter
574 if ($dev =~ m|^cciss!|) {
575 $dev =~ s|^cciss!|cciss/|;
576 }
577
578 $disklist->{$dev} = {
579 vendor => $sysdata->{vendor},
580 model => $data->{model} || $sysdata->{model},
581 size => $sysdata->{size},
582 serial => $data->{serial},
583 gpt => $data->{gpt},
584 rpm => $data->{rpm},
585 type => $type,
586 wwn => $data->{wwn},
587 health => $health,
588 devpath => $devpath,
589 wearout => $wearout,
590 };
591
592 my $by_id_link = $data->{by_id_link};
593 $disklist->{$dev}->{by_id_link} = $by_id_link if defined($by_id_link);
594
595 my $osdid = -1;
596 my $bluestore = 0;
597 my $osdencrypted = 0;
598
599 my $journal_count = 0;
600 my $db_count = 0;
601 my $wal_count = 0;
602
603 my $found_partitions;
604 my $found_lvm;
605 my $found_mountpoints;
606 my $found_zfs;
607 my $found_dm;
608 my $partpath = $devpath;
609
610 # remove part after last / to
611 # get the base path for the partitions
612 # e.g. from /dev/cciss/c0d0 get /dev/cciss
613 $partpath =~ s/\/[^\/]+$//;
614
615 dir_glob_foreach("$sysdir", "$dev.+", sub {
616 my ($part) = @_;
617
618 $found_partitions = 1;
619
620 if (my $mp = &$dev_is_mounted("$partpath/$part")) {
621 $found_mountpoints = 1;
622 if ($mp =~ m|^/var/lib/ceph/osd/ceph-(\d+)$|) {
623 $osdid = $1;
624 }
625 }
626
627 if ($lvmhash->{"$partpath/$part"}) {
628 $found_lvm = 1;
629 }
630
631 if ($zfshash->{"$partpath/$part"}) {
632 $found_zfs = 1;
633 }
634
635 if (my $journal_part = $journalhash->{"$partpath/$part"}) {
636 $journal_count++ if $journal_part == 1;
637 $db_count++ if $journal_part == 2;
638 $wal_count++ if $journal_part == 3;
639 $bluestore = 1 if $journal_part == 4;
640 }
641
642 if (!dir_is_empty("$sysdir/$part/holders") && !$found_lvm) {
643 $found_dm = 1;
644 }
645 });
646
647 if (my $ceph_volume = $ceph_volume_infos->{$devpath}) {
648 $journal_count += $ceph_volume->{journal} // 0;
649 $db_count += $ceph_volume->{db} // 0;
650 $wal_count += $ceph_volume->{wal} // 0;
651 if (defined($ceph_volume->{osdid})) {
652 $osdid = $ceph_volume->{osdid};
653 $bluestore = 1 if $ceph_volume->{bluestore};
654 $osdencrypted = 1 if $ceph_volume->{encrypted};
655 }
656 }
657
658 $used = 'mounted' if $found_mountpoints && !$used;
659 $used = 'LVM' if $found_lvm && !$used;
660 $used = 'ZFS' if $found_zfs && !$used;
661 $used = 'Device Mapper' if $found_dm && !$used;
662 $used = 'partitions' if $found_partitions && !$used;
663
664 # multipath, software raid, etc.
665 # this check comes in last, to show more specific info
666 # if we have it
667 $used = 'Device Mapper' if !$used && !dir_is_empty("$sysdir/holders");
668
669 $disklist->{$dev}->{used} = $used if $used;
670 $disklist->{$dev}->{osdid} = $osdid;
671 $disklist->{$dev}->{journals} = $journal_count if $journal_count;
672 $disklist->{$dev}->{bluestore} = $bluestore if $osdid != -1;
673 $disklist->{$dev}->{osdencrypted} = $osdencrypted if $osdid != -1;
674 $disklist->{$dev}->{db} = $db_count if $db_count;
675 $disklist->{$dev}->{wal} = $wal_count if $wal_count;
676 });
677
678 return $disklist;
679
680 }
681
682 sub get_partnum {
683 my ($part_path) = @_;
684
685 my $st = stat($part_path);
686
687 next if !$st->mode || !S_ISBLK($st->mode) || !$st->rdev;
688 my $major = PVE::Tools::dev_t_major($st->rdev);
689 my $minor = PVE::Tools::dev_t_minor($st->rdev);
690 my $partnum_path = "/sys/dev/block/$major:$minor/";
691
692 my $partnum;
693
694 $partnum = file_read_firstline("${partnum_path}partition");
695
696 die "Partition does not exist\n" if !defined($partnum);
697
698 #untaint and ensure it is a int
699 if ($partnum =~ m/(\d+)/) {
700 $partnum = $1;
701 die "Partition number $partnum is invalid\n" if $partnum > 128;
702 } else {
703 die "Failed to get partition number\n";
704 }
705
706 return $partnum;
707 }
708
709 sub get_blockdev {
710 my ($part_path) = @_;
711
712 my $dev = $1 if $part_path =~ m|^/dev/(.*)$|;
713 my $link = readlink "/sys/class/block/$dev";
714 my $block_dev = $1 if $link =~ m|([^/]*)/$dev$|;
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;