]>
git.proxmox.com Git - pve-storage.git/blob - PVE/Diskmanage.pm
1 package PVE
::Diskmanage
;
9 use PVE
::Tools
qw(extract_param run_command file_get_contents file_read_firstline dir_glob_regex dir_glob_foreach trim);
11 my $SMARTCTL = "/usr/sbin/smartctl";
12 my $ZPOOL = "/sbin/zpool";
13 my $SGDISK = "/sbin/sgdisk";
14 my $PVS = "/sbin/pvs";
15 my $UDEVADM = "/bin/udevadm";
17 sub verify_blockdev_path
{
20 die "missing path" if !$rel_path;
21 my $path = abs_path
($rel_path);
22 die "failed to get absolute path to $rel_path\n" if !$path;
24 die "got unusual device path '$path'\n" if $path !~ m
|^/dev/(.*)$|;
26 $path = "/dev/$1"; # untaint
28 assert_blockdev
($path);
34 my ($dev, $noerr) = @_;
36 if ($dev !~ m
|^/dev/| || !(-b
$dev)) {
37 return undef if $noerr;
38 die "not a valid block device\n";
45 my ($disk, $uuid) = @_;
47 assert_blockdev
($disk);
49 # we should already have checked if it is in use in the api call
50 # but we check again for safety
51 die "disk $disk is already in use\n" if disk_is_used
($disk);
53 my $id = $uuid || 'R';
54 run_command
([$SGDISK, $disk, '-U', $id]);
64 my $disklist = get_disks
($dev, 1);
66 die "'$disk' is not a valid local disk\n" if !defined($disklist->{$dev});
67 return 1 if $disklist->{$dev}->{used
};
73 my ($disk, $healthonly) = @_;
75 assert_blockdev
($disk);
82 if $disk =~ m!^/dev/nvme\d+n\d+$!;
84 my $cmd = [$SMARTCTL, '-H'];
85 push @$cmd, '-A', '-f', 'brief' if !$healthonly;
89 $returncode = run_command
($cmd, noerr
=> 1, outfunc
=> sub{
92 # ATA SMART attributes, e.g.:
93 # ID# ATTRIBUTE_NAME FLAGS VALUE WORST THRESH FAIL RAW_VALUE
94 # 1 Raw_Read_Error_Rate POSR-K 100 100 000 - 0
96 # SAS and NVME disks, e.g.:
97 # Data Units Written: 5,584,952 [2.85 TB]
98 # Accumulated start-stop cycles: 34
100 if (defined($type) && $type eq 'ata' && $line =~ m/^([ \d]{2}\d)\s+(\S+)\s+(\S{6})\s+(\d+)\s+(\d+)\s+(\d+)\s+(\S+)\s+(.*)$/) {
102 $entry->{name
} = $2 if defined $2;
103 $entry->{flags
} = $3 if defined $3;
104 # the +0 makes a number out of the strings
105 $entry->{value
} = $4+0 if defined $4;
106 $entry->{worst
} = $5+0 if defined $5;
107 $entry->{threshold
} = $6+0 if defined $6;
108 $entry->{fail
} = $7 if defined $7;
109 $entry->{raw
} = $8 if defined $8;
110 $entry->{id
} = $1 if defined $1;
111 push @{$smartdata->{attributes
}}, $entry;
112 } elsif ($line =~ m/(?:Health Status|self\-assessment test result): (.*)$/ ) {
113 $smartdata->{health
} = $1;
114 } elsif ($line =~ m/Vendor Specific SMART Attributes with Thresholds:/) {
116 delete $smartdata->{text
};
117 } elsif ($line =~ m/=== START OF (READ )?SMART DATA SECTION ===/) {
119 } elsif (defined($type) && $type eq 'text') {
120 $smartdata->{text
} = '' if !defined $smartdata->{text
};
121 $smartdata->{text
} .= "$line\n";
122 } elsif ($line =~ m/SMART Disabled/) {
123 $smartdata->{health
} = "SMART Disabled";
129 # bit 0 and 1 mark an severe smartctl error
130 # all others are for disk status, so ignore them
132 if ((defined($returncode) && ($returncode & 0b00000011)) || $err) {
133 die "Error getting S.M.A.R.T. data: Exit code: $returncode\n";
136 $smartdata->{type
} = $type;
141 sub get_zfs_devices
{
144 # use zpool and parttype uuid,
145 # because log and cache do not have
148 run_command
([$ZPOOL, 'list', '-HPLv'], outfunc
=> sub {
151 if ($line =~ m
|^\t([^\t]+)\t|) {
158 # because maybe zfs tools are not installed
161 my $applezfsuuid = "6a898cc3-1dd2-11b2-99a6-080020736631";
162 my $bsdzfsuuid = "516e7cba-6ecf-11d6-8ff8-00022d09712b";
164 dir_glob_foreach
('/dev/disk/by-parttypeuuid', "($applezfsuuid|$bsdzfsuuid)\..+", sub {
166 my $real_dev = abs_path
("/dev/disk/by-parttypeuuid/$entry");
167 $list->{$real_dev} = 1;
173 sub get_lvm_devices
{
176 run_command
([$PVS, '--noheadings', '--readonly', '-o', 'pv_name'], outfunc
=> sub{
179 if ($line =~ m
|^/dev/|) {
185 # if something goes wrong, we do not want
186 # to give up, but indicate an error has occured
189 my $lvmuuid = "e6d6d379-f507-44c2-a23c-238f2a3df928";
191 dir_glob_foreach
('/dev/disk/by-parttypeuuid', "$lvmuuid\..+", sub {
193 my $real_dev = abs_path
("/dev/disk/by-parttypeuuid/$entry");
194 $list->{$real_dev} = 1;
200 sub get_ceph_journals
{
201 my $journalhash = {};
203 my $journal_uuid = '45b0969e-9b03-4f30-b4c6-b4b80ceff106';
205 dir_glob_foreach
('/dev/disk/by-parttypeuuid', "$journal_uuid\..+", sub {
207 my $real_dev = abs_path
("/dev/disk/by-parttypeuuid/$entry");
208 $journalhash->{$real_dev} = 1;
220 run_command
([$UDEVADM, 'info', '-p', $dev, '--query', 'all'], outfunc
=> sub {
226 return undef if !$info;
228 return undef if $info !~ m/^E: DEVTYPE=disk$/m;
229 return undef if $info =~ m/^E: ID_CDROM/m;
231 # we use this, because some disks are not simply in /dev
232 # e.g. /dev/cciss/c0d0
233 if ($info =~ m/^E: DEVNAME=(\S+)$/m) {
234 $data->{devpath
} = $1;
236 return if !defined($data->{devpath
});
238 $data->{serial
} = 'unknown';
239 if ($info =~ m/^E: ID_SERIAL_SHORT=(\S+)$/m) {
240 $data->{serial
} = $1;
244 if ($info =~ m/^E: ID_PART_TABLE_TYPE=gpt$/m) {
250 if ($info =~ m/^E: ID_ATA_ROTATION_RATE_RPM=(\d+)$/m) {
254 if ($info =~ m/^E: ID_BUS=usb$/m) {
258 $data->{wwn
} = 'unknown';
259 if ($info =~ m/^E: ID_WWN=(.*)$/m) {
266 sub get_sysdir_info
{
269 return undef if ! -d
"$sysdir/device";
273 my $size = file_read_firstline
("$sysdir/size");
274 return undef if !$size;
276 # linux always considers sectors to be 512 bytes,
277 # independently of real block size
278 $data->{size
} = $size * 512;
280 # dir/queue/rotational should be 1 for hdd, 0 for ssd
281 $data->{rotational
} = file_read_firstline
("$sysdir/queue/rotational");
283 $data->{vendor
} = file_read_firstline
("$sysdir/device/vendor") || 'unknown';
284 $data->{model
} = file_read_firstline
("$sysdir/device/model") || 'unknown';
289 sub get_wear_leveling_info
{
290 my ($attributes, $model) = @_;
302 # find target attr id
306 foreach my $vendor (keys %$vendormap) {
307 if ($model =~ m/$vendor/i) {
308 $attrid = $vendormap->{$vendor};
309 # found the attribute
315 $attrid = $vendormap->{default};
318 foreach my $attr (@$attributes) {
319 next if $attr->{id
} != $attrid;
320 $wearout = $attr->{value
};
328 my ($disk, $nosmart) = @_;
333 my $mounts = PVE
::ProcFSTools
::parse_proc_mounts
();
335 foreach my $mount (@$mounts) {
336 next if $mount->[0] !~ m
|^/dev/|;
337 $mounted->{abs_path
($mount->[0])} = $mount->[1];
340 my $dev_is_mounted = sub {
342 return $mounted->{$dev};
345 my $dir_is_empty = sub {
348 my $dh = IO
::Dir-
>new ($dir);
351 while (defined(my $tmp = $dh->read)) {
352 next if $tmp eq '.' || $tmp eq '..';
360 my $journalhash = get_ceph_journals
();
362 my $zfslist = get_zfs_devices
();
364 my $lvmlist = get_lvm_devices
();
366 dir_glob_foreach
('/sys/block', '.*', sub {
368 return if defined($disk) && $disk ne $dev;
369 # whitelisting following devices
370 # hdX: ide block device
371 # sdX: sd block device
372 # vdX: virtual block device
373 # xvdX: xen virtual block device
374 # nvmeXnY: nvme devices
375 # cciss!cXnY: cciss devices
376 return if $dev !~ m/^(h|s|x?v)d[a-z]+$/ &&
377 $dev !~ m/^nvme\d+n\d+$/ &&
378 $dev !~ m/^cciss\!c\d+d\d+$/;
380 my $data = get_udev_info
("/sys/block/$dev");
381 return if !defined($data);
382 my $devpath = $data->{devpath
};
384 my $sysdir = "/sys/block/$dev";
386 # we do not want iscsi devices
387 return if -l
$sysdir && readlink($sysdir) =~ m
|host
[^/]*/session
[^/]*|;
389 my $sysdata = get_sysdir_info
($sysdir);
390 return if !defined($sysdata);
392 my $type = 'unknown';
394 if ($sysdata->{rotational
} == 0) {
397 } elsif ($sysdata->{rotational
} == 1) {
398 if ($data->{rpm
} != -1) {
400 } elsif ($data->{usb
}) {
406 my $health = 'UNKNOWN';
411 my $smartdata = get_smart_data
($devpath, ($type ne 'ssd'));
412 $health = $smartdata->{health
} if $smartdata->{health
};
414 if ($type eq 'ssd') {
415 # if we have an ssd we try to get the wearout indicator
416 my $wearval = get_wear_leveling_info
($smartdata->{attributes
}, $sysdata->{model
});
417 $wearout = $wearval if $wearval;
424 $used = 'LVM' if $lvmlist->{$devpath};
426 $used = 'mounted' if &$dev_is_mounted($devpath);
428 $used = 'ZFS' if $zfslist->{$devpath};
430 $disklist->{$dev} = {
431 vendor
=> $sysdata->{vendor
},
432 model
=> $sysdata->{model
},
433 size
=> $sysdata->{size
},
434 serial
=> $data->{serial
},
446 my $journal_count = 0;
448 my $found_partitions;
450 my $found_mountpoints;
453 my $partpath = $devpath;
455 # remove part after last / to
456 # get the base path for the partitions
457 # e.g. from /dev/cciss/c0d0 get /dev/cciss
458 $partpath =~ s/\/[^\/]+$//;
460 dir_glob_foreach
("$sysdir", "$dev.+", sub {
463 $found_partitions = 1;
465 if (my $mp = &$dev_is_mounted("$partpath/$part")) {
466 $found_mountpoints = 1;
467 if ($mp =~ m
|^/var/lib
/ceph/osd/ceph-
(\d
+)$|) {
472 if ($lvmlist->{"$partpath/$part"}) {
476 if ($zfslist->{"$partpath/$part"}) {
480 $journal_count++ if $journalhash->{"$partpath/$part"};
482 if (!&$dir_is_empty("$sysdir/$part/holders") && !$found_lvm) {
487 $used = 'mounted' if $found_mountpoints && !$used;
488 $used = 'LVM' if $found_lvm && !$used;
489 $used = 'ZFS' if $found_zfs && !$used;
490 $used = 'Device Mapper' if $found_dm && !$used;
491 $used = 'partitions' if $found_partitions && !$used;
493 # multipath, software raid, etc.
494 # this check comes in last, to show more specific info
496 $used = 'Device Mapper' if !$used && !&$dir_is_empty("$sysdir/holders");
498 $disklist->{$dev}->{used
} = $used if $used;
499 $disklist->{$dev}->{osdid
} = $osdid;
500 $disklist->{$dev}->{journals
} = $journal_count;