]>
git.proxmox.com Git - pve-storage.git/blob - PVE/Diskmanage.pm
36a5b5b7cbac26ea1255143dc902c2a755710a22
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
};
75 assert_blockdev
($disk);
82 if $disk =~ m!^/dev/nvme\d+n\d+$!;
85 $returncode = run_command
([$SMARTCTL, '-H', '-A', '-f', 'brief', $disk], noerr
=> 1, outfunc
=> sub{
88 # ATA SMART attributes, e.g.:
89 # ID# ATTRIBUTE_NAME FLAGS VALUE WORST THRESH FAIL RAW_VALUE
90 # 1 Raw_Read_Error_Rate POSR-K 100 100 000 - 0
92 # SAS and NVME disks, e.g.:
93 # Data Units Written: 5,584,952 [2.85 TB]
94 # Accumulated start-stop cycles: 34
96 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+(.*)$/) {
98 $entry->{name
} = $2 if defined $2;
99 $entry->{flags
} = $3 if defined $3;
100 # the +0 makes a number out of the strings
101 $entry->{value
} = $4+0 if defined $4;
102 $entry->{worst
} = $5+0 if defined $5;
103 $entry->{threshold
} = $6+0 if defined $6;
104 $entry->{fail
} = $7 if defined $7;
105 $entry->{raw
} = $8 if defined $8;
106 $entry->{id
} = $1 if defined $1;
107 push @{$smartdata->{attributes
}}, $entry;
108 } elsif ($line =~ m/(?:Health Status|self\-assessment test result): (.*)$/ ) {
109 $smartdata->{health
} = $1;
110 } elsif ($line =~ m/Vendor Specific SMART Attributes with Thresholds:/) {
112 delete $smartdata->{text
};
113 } elsif ($line =~ m/=== START OF (READ )?SMART DATA SECTION ===/) {
115 } elsif (defined($type) && $type eq 'text') {
116 $smartdata->{text
} = '' if !defined $smartdata->{text
};
117 $smartdata->{text
} .= "$line\n";
123 # bit 0 and 1 mark an severe smartctl error
124 # all others are for disk status, so ignore them
126 if ((defined($returncode) && ($returncode & 0b00000011)) || $err) {
127 die "Error getting S.M.A.R.T. data: Exit code: $returncode\n";
130 $smartdata->{type
} = $type;
135 sub get_smart_health
{
138 return "NOT A DEVICE" if !assert_blockdev
($disk, 1);
142 if $disk =~ m!^/dev/nvme\d+n\d+$!;
144 run_command
([$SMARTCTL, '-H', $disk], noerr
=> 1, outfunc
=> sub {
147 if ($line =~ m/test result: (.*)$/) {
149 } elsif ($line =~ m/open device: (.*) failed: (.*)$/) {
150 $message = "FAILED TO OPEN";
151 } elsif ($line =~ m/^SMART Disabled/) {
152 $message = "SMART DISABLED";
159 sub get_zfs_devices
{
162 # use zpool and parttype uuid,
163 # because log and cache do not have
166 run_command
([$ZPOOL, 'list', '-HPLv'], outfunc
=> sub {
169 if ($line =~ m
|^\t([^\t]+)\t|) {
176 # because maybe zfs tools are not installed
179 my $applezfsuuid = "6a898cc3-1dd2-11b2-99a6-080020736631";
180 my $bsdzfsuuid = "516e7cba-6ecf-11d6-8ff8-00022d09712b";
182 dir_glob_foreach
('/dev/disk/by-parttypeuuid', "($applezfsuuid|$bsdzfsuuid)\..+", sub {
184 my $real_dev = abs_path
("/dev/disk/by-parttypeuuid/$entry");
185 $list->{$real_dev} = 1;
191 sub get_lvm_devices
{
194 run_command
([$PVS, '--noheadings', '--readonly', '-o', 'pv_name'], outfunc
=> sub{
197 if ($line =~ m
|^/dev/|) {
203 # if something goes wrong, we do not want
204 # to give up, but indicate an error has occured
207 my $lvmuuid = "e6d6d379-f507-44c2-a23c-238f2a3df928";
209 dir_glob_foreach
('/dev/disk/by-parttypeuuid', "$lvmuuid\..+", sub {
211 my $real_dev = abs_path
("/dev/disk/by-parttypeuuid/$entry");
212 $list->{$real_dev} = 1;
218 sub get_ceph_journals
{
219 my $journalhash = {};
221 my $journal_uuid = '45b0969e-9b03-4f30-b4c6-b4b80ceff106';
223 dir_glob_foreach
('/dev/disk/by-parttypeuuid', "$journal_uuid\..+", sub {
225 my $real_dev = abs_path
("/dev/disk/by-parttypeuuid/$entry");
226 $journalhash->{$real_dev} = 1;
238 run_command
([$UDEVADM, 'info', '-n', $dev, '--query', 'all'], outfunc
=> sub {
244 return undef if !$info;
246 return undef if $info !~ m/^E: DEVTYPE=disk$/m;
247 return undef if $info =~ m/^E: ID_CDROM/m;
249 # we use this, because some disks are not simply in /dev
250 # e.g. /dev/cciss/c0d0
251 if ($info =~ m/^E: DEVNAME=(\S+)$/m) {
252 $data->{devpath
} = $1;
254 return if !defined($data->{devpath
});
256 $data->{serial
} = 'unknown';
257 if ($info =~ m/^E: ID_SERIAL_SHORT=(\S+)$/m) {
258 $data->{serial
} = $1;
262 if ($info =~ m/^E: ID_PART_TABLE_TYPE=gpt$/m) {
268 if ($info =~ m/^E: ID_ATA_ROTATION_RATE_RPM=(\d+)$/m) {
272 if ($info =~ m/^E: ID_BUS=usb$/m) {
276 $data->{wwn
} = 'unknown';
277 if ($info =~ m/^E: ID_WWN=(.*)$/m) {
284 sub get_sysdir_info
{
289 my $size = file_read_firstline
("$sysdir/size");
290 return undef if !$size;
292 # linux always considers sectors to be 512 bytes,
293 # independently of real block size
294 $data->{size
} = $size * 512;
296 # dir/queue/rotational should be 1 for hdd, 0 for ssd
297 $data->{rotational
} = file_read_firstline
("$sysdir/queue/rotational");
299 $data->{vendor
} = file_read_firstline
("$sysdir/device/vendor") || 'unknown';
300 $data->{model
} = file_read_firstline
("$sysdir/device/model") || 'unknown';
306 my ($disk, $nosmart) = @_;
311 my $mounts = PVE
::ProcFSTools
::parse_proc_mounts
();
313 foreach my $mount (@$mounts) {
314 next if $mount->[0] !~ m
|^/dev/|;
315 $mounted->{abs_path
($mount->[0])} = $mount->[1];
318 my $dev_is_mounted = sub {
320 return $mounted->{$dev};
323 my $dir_is_empty = sub {
326 my $dh = IO
::Dir-
>new ($dir);
329 while (defined(my $tmp = $dh->read)) {
330 next if $tmp eq '.' || $tmp eq '..';
338 my $journalhash = get_ceph_journals
();
340 my $zfslist = get_zfs_devices
();
342 my $lvmlist = get_lvm_devices
();
344 dir_glob_foreach
('/sys/block', '.*', sub {
346 return if defined($disk) && $disk ne $dev;
347 # whitelisting following devices
348 # hdX: ide block device
349 # sdX: sd block device
350 # vdX: virtual block device
351 # xvdX: xen virtual block device
352 # nvmeXnY: nvme devices
353 # cXnY: cciss devices
354 return if $dev !~ m/^(h|s|x?v)d[a-z]+$/ &&
355 $dev !~ m/^nvme\d+n\d+$/ &&
356 $dev !~ m/^c\d+d\d+$/;
358 my $data = get_udev_info
($dev);
359 return if !defined($data);
360 my $devpath = $data->{devpath
};
362 my $sysdir = "/sys/block/$dev";
364 return if ! -d
"$sysdir/device";
366 # we do not want iscsi devices
367 return if readlink($sysdir) =~ m
|host
[^/]*/session
[^/]*|;
369 my $sysdata = get_sysdir_info
($sysdir);
370 return if !defined($sysdata);
372 my $type = 'unknown';
374 if ($sysdata->{rotational
} == 0) {
377 } elsif ($sysdata->{rotational
} == 1) {
378 if ($data->{rpm
} != -1) {
380 } elsif ($data->{usb
}) {
386 my $health = 'UNKNOWN';
391 if ($type eq 'ssd') {
392 # if we have an ssd we try to get the wearout indicator
394 my $smartdata = get_smart_data
($devpath);
395 $health = $smartdata->{health
};
396 foreach my $attr (@{$smartdata->{attributes
}}) {
397 # ID 233 is media wearout indicator on intel and sandisk
398 # ID 177 is media wearout indicator on samsung
399 next if ($attr->{id
} != 233 && $attr->{id
} != 177);
400 next if ($attr->{name
} !~ m/wear/i);
401 $wearout = $attr->{value
};
403 # prefer the 233 value
404 last if ($attr->{id
} == 233);
407 # else we just get the health
408 $health = get_smart_health
($devpath);
415 $used = 'LVM' if $lvmlist->{$devpath};
417 $used = 'mounted' if &$dev_is_mounted($devpath);
419 $used = 'ZFS' if $zfslist->{$devpath};
421 $disklist->{$dev} = {
422 vendor
=> $sysdata->{vendor
},
423 model
=> $sysdata->{model
},
424 size
=> $sysdata->{size
},
425 serial
=> $data->{serial
},
437 my $journal_count = 0;
439 my $found_partitions;
441 my $found_mountpoints;
444 my $partpath = $devpath;
446 # remove part after last / to
447 # get the base path for the partitions
448 # e.g. from /dev/cciss/c0d0 get /dev/cciss
449 $partpath =~ s/\/[^\/]+$//;
451 dir_glob_foreach
("$sysdir", "$dev.+", sub {
454 $found_partitions = 1;
456 if (my $mp = &$dev_is_mounted("$partpath/$part")) {
457 $found_mountpoints = 1;
458 if ($mp =~ m
|^/var/lib
/ceph/osd/ceph-
(\d
+)$|) {
463 if ($lvmlist->{"$partpath/$part"}) {
467 if ($zfslist->{"$partpath/$part"}) {
471 $journal_count++ if $journalhash->{"$partpath/$part"};
473 if (!&$dir_is_empty("$sysdir/$part/holders") && !$found_lvm) {
478 $used = 'mounted' if $found_mountpoints && !$used;
479 $used = 'LVM' if $found_lvm && !$used;
480 $used = 'ZFS' if $found_zfs && !$used;
481 $used = 'Device Mapper' if $found_dm && !$used;
482 $used = 'partitions' if $found_partitions && !$used;
484 # multipath, software raid, etc.
485 # this check comes in last, to show more specific info
487 $used = 'Device Mapper' if !$used && !&$dir_is_empty("$sysdir/holders");
489 $disklist->{$dev}->{used
} = $used if $used;
490 $disklist->{$dev}->{osdid
} = $osdid;
491 $disklist->{$dev}->{journals
} = $journal_count;