]> git.proxmox.com Git - pve-storage.git/blob - PVE/Diskmanage.pm
bee5d99d2a973e68dd166466b87b8da49f33bfb5
[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
9 use PVE::Tools qw(extract_param run_command file_get_contents file_read_firstline dir_glob_regex dir_glob_foreach trim);
10
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";
16
17 sub verify_blockdev_path {
18 my ($rel_path) = @_;
19
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;
23
24 die "got unusual device path '$path'\n" if $path !~ m|^/dev/(.*)$|;
25
26 $path = "/dev/$1"; # untaint
27
28 assert_blockdev($path);
29
30 return $path;
31 }
32
33 sub assert_blockdev {
34 my ($dev, $noerr) = @_;
35
36 if ($dev !~ m|^/dev/| || !(-b $dev)) {
37 return undef if $noerr;
38 die "not a valid block device\n";
39 }
40
41 return 1;
42 }
43
44 sub init_disk {
45 my ($disk, $uuid) = @_;
46
47 assert_blockdev($disk);
48
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);
52
53 my $id = $uuid || 'R';
54 run_command([$SGDISK, $disk, '-U', $id]);
55 return 1;
56 }
57
58 sub disk_is_used {
59 my ($disk) = @_;
60
61 my $dev = $disk;
62 $dev =~ s|^/dev/||;
63
64 my $disklist = get_disks($dev);
65
66 die "'$disk' is not a valid local disk\n" if !defined($disklist->{$dev});
67 return 1 if $disklist->{$dev}->{used};
68
69 return 0;
70 }
71
72 sub get_smart_data {
73 my ($disk) = @_;
74
75 assert_blockdev($disk);
76 my $smartdata = {};
77 my $datastarted = 0;
78
79 my $returncode = 0;
80 eval {
81 $returncode = run_command([$SMARTCTL, '-a', '-f', 'brief', $disk], noerr => 1, outfunc => sub{
82 my ($line) = @_;
83
84 if ($datastarted && $line =~ m/^[ \d]{2}\d/) {
85 $line = trim($line);
86 my @data = split /\s+/, $line;
87 my $entry = {};
88 $entry->{name} = $data[1];
89 $entry->{flags} = $data[2];
90 # the +0 makes a number out of the strings
91 $entry->{value} = $data[3] + 0;
92 $entry->{worst} = $data[4] + 0;
93 $entry->{threshold} = $data[5] + 0;
94 $entry->{fail} = $data[6];
95 $entry->{raw} = $data[7];
96 $entry->{id} = $data[0];
97 push @{$smartdata->{attributes}}, $entry;
98 } elsif ($line =~ m/self\-assessment test result: (.*)$/) {
99 $smartdata->{health} = $1;
100 } elsif ($line =~ m/Vendor Specific SMART Attributes with Thresholds:/) {
101 $datastarted = 1;
102 }
103 });
104 };
105 my $err = $@;
106
107 # bit 0 and 1 mark an severe smartctl error
108 # all others are for disk status, so ignore them
109 # see smartctl(8)
110 if ((defined($returncode) && ($returncode & 0b00000011)) || $err) {
111 die "Error getting S.M.A.R.T. data: Exit code: $returncode\n";
112 }
113 $smartdata->{health} = 'UNKOWN' if !defined $smartdata->{health};
114 return $smartdata;
115 }
116
117 sub get_smart_health {
118 my ($disk) = @_;
119
120 return "NOT A DEVICE" if !assert_blockdev($disk, 1);
121
122 my $message = "UNKOWN";
123
124 eval {
125 run_command([$SMARTCTL, '-H', $disk], noerr => 1, outfunc => sub {
126 my ($line) = @_;
127
128 if ($line =~ m/test result: (.*)$/) {
129 $message = $1;
130 } elsif ($line =~ m/open device: (.*) failed: (.*)$/) {
131 $message = "FAILED TO OPEN";
132 } elsif ($line =~ m/^SMART Disabled/) {
133 $message = "SMART DISABLED";
134 }
135 });
136 };
137 # we ignore errors here because by default we want to return UNKNOWN
138
139 return $message;
140 }
141
142 sub get_zfs_devices {
143 my $list = {};
144
145 # use zpool and parttype uuid,
146 # because log and cache do not have
147 # zfs type uuid
148 eval {
149 run_command([$ZPOOL, 'list', '-HPLv'], outfunc => sub {
150 my ($line) = @_;
151
152 if ($line =~ m|^\t([^\t]+)\t|) {
153 $list->{$1} = 1;
154 }
155 });
156 };
157
158 # only warn here,
159 # because maybe zfs tools are not installed
160 warn "$@\n" if $@;
161
162 my $applezfsuuid = "6a898cc3-1dd2-11b2-99a6-080020736631";
163 my $bsdzfsuuid = "516e7cba-6ecf-11d6-8ff8-00022d09712b";
164
165 dir_glob_foreach('/dev/disk/by-parttypeuuid', "($applezfsuuid|$bsdzfsuuid)\..+", sub {
166 my ($entry) = @_;
167 my $real_dev = abs_path("/dev/disk/by-parttypeuuid/$entry");
168 $list->{$real_dev} = 1;
169 });
170
171 return $list;
172 }
173
174 sub get_lvm_devices {
175 my $list = {};
176 eval {
177 run_command([$PVS, '--noheadings', '--readonly', '-o', 'pv_name'], outfunc => sub{
178 my ($line) = @_;
179 $line = trim($line);
180 if ($line =~ m|^/dev/|) {
181 $list->{$line} = 1;
182 }
183 });
184 };
185
186 # if something goes wrong, we do not want
187 # to give up, but indicate an error has occured
188 warn "$@\n" if $@;
189
190 my $lvmuuid = "e6d6d379-f507-44c2-a23c-238f2a3df928";
191
192 dir_glob_foreach('/dev/disk/by-parttypeuuid', "$lvmuuid\..+", sub {
193 my ($entry) = @_;
194 my $real_dev = abs_path("/dev/disk/by-parttypeuuid/$entry");
195 $list->{$real_dev} = 1;
196 });
197
198 return $list;
199 }
200
201 sub get_ceph_journals {
202 my $journalhash = {};
203
204 my $journal_uuid = '45b0969e-9b03-4f30-b4c6-b4b80ceff106';
205
206 dir_glob_foreach('/dev/disk/by-parttypeuuid', "$journal_uuid\..+", sub {
207 my ($entry) = @_;
208 my $real_dev = abs_path("/dev/disk/by-parttypeuuid/$entry");
209 $journalhash->{$real_dev} = 1;
210 });
211
212 return $journalhash;
213 }
214
215 sub get_udev_info {
216 my ($dev) = @_;
217
218 my $info = "";
219 my $data = {};
220 eval {
221 run_command([$UDEVADM, 'info', '-n', $dev, '--query', 'all'], outfunc => sub {
222 my ($line) = @_;
223 $info .= "$line\n";
224 });
225 };
226 warn $@ if $@;
227 return undef if !$info;
228
229 return undef if $info !~ m/^E: DEVTYPE=disk$/m;
230 return undef if $info =~ m/^E: ID_CDROM/m;
231
232 # we use this, because some disks are not simply in /dev
233 # e.g. /dev/cciss/c0d0
234 if ($info =~ m/^E: DEVNAME=(\S+)$/m) {
235 $data->{devpath} = $1;
236 }
237 return if !defined($data->{devpath});
238
239 $data->{serial} = 'unknown';
240 if ($info =~ m/^E: ID_SERIAL_SHORT=(\S+)$/m) {
241 $data->{serial} = $1;
242 }
243
244 $data->{gpt} = 0;
245 if ($info =~ m/^E: ID_PART_TABLE_TYPE=gpt$/m) {
246 $data->{gpt} = 1;
247 }
248
249 # detect SSD
250 $data->{rpm} = -1;
251 if ($info =~ m/^E: ID_ATA_ROTATION_RATE_RPM=(\d+)$/m) {
252 $data->{rpm} = $1;
253 }
254
255 if ($info =~ m/^E: ID_BUS=usb$/m) {
256 $data->{usb} = 1;
257 }
258
259 $data->{wwn} = 'unknown';
260 if ($info =~ m/^E: ID_WWN=(.*)$/m) {
261 $data->{wwn} = $1;
262 }
263
264 return $data;
265 }
266
267 sub get_sysdir_info {
268 my ($sysdir) = @_;
269
270 my $data = {};
271
272 my $size = file_read_firstline("$sysdir/size");
273 return undef if !$size;
274
275 # linux always considers sectors to be 512 bytes,
276 # independently of real block size
277 $data->{size} = $size * 512;
278
279 # dir/queue/rotational should be 1 for hdd, 0 for ssd
280 $data->{rotational} = file_read_firstline("$sysdir/queue/rotational");
281
282 $data->{vendor} = file_read_firstline("$sysdir/device/vendor") || 'unknown';
283 $data->{model} = file_read_firstline("$sysdir/device/model") || 'unknown';
284
285 return $data;
286 }
287
288 sub get_disks {
289 my ($disk) = @_;
290 my $disklist = {};
291
292 my $mounted = {};
293
294 my $mounts = PVE::ProcFSTools::parse_proc_mounts();
295
296 foreach my $mount (@$mounts) {
297 next if $mount->[0] !~ m|^/dev/|;
298 $mounted->{abs_path($mount->[0])} = $mount->[1];
299 };
300
301 my $dev_is_mounted = sub {
302 my ($dev) = @_;
303 return $mounted->{$dev};
304 };
305
306 my $dir_is_empty = sub {
307 my ($dir) = @_;
308
309 my $dh = IO::Dir->new ($dir);
310 return 1 if !$dh;
311
312 while (defined(my $tmp = $dh->read)) {
313 next if $tmp eq '.' || $tmp eq '..';
314 $dh->close;
315 return 0;
316 }
317 $dh->close;
318 return 1;
319 };
320
321 my $journalhash = get_ceph_journals();
322
323 my $zfslist = get_zfs_devices();
324
325 my $lvmlist = get_lvm_devices();
326
327 dir_glob_foreach('/sys/block', '.*', sub {
328 my ($dev) = @_;
329 return if defined($disk) && $disk ne $dev;
330 # whitelisting following devices
331 # hdX: ide block device
332 # sdX: sd block device
333 # vdX: virtual block device
334 # xvdX: xen virtual block device
335 # nvmeXnY: nvme devices
336 # cXnY: cciss devices
337 return if $dev !~ m/^(h|s|x?v)d[a-z]+$/ &&
338 $dev !~ m/^nvme\d+n\d+$/ &&
339 $dev !~ m/^c\d+d\d+$/;
340
341 my $data = get_udev_info($dev);
342 return if !defined($data);
343 my $devpath = $data->{devpath};
344
345 my $sysdir = "/sys/block/$dev";
346
347 return if ! -d "$sysdir/device";
348
349 # we do not want iscsi devices
350 return if readlink($sysdir) =~ m|host[^/]*/session[^/]*|;
351
352 my $sysdata = get_sysdir_info($sysdir);
353 return if !defined($sysdata);
354
355 my $type = 'unknown';
356
357 if ($sysdata->{rotational} == 0) {
358 $type = 'ssd';
359 $data->{rpm} = 0;
360 } elsif ($sysdata->{rotational} == 1) {
361 if ($data->{rpm} != -1) {
362 $type = 'hdd';
363 } elsif ($data->{usb}) {
364 $type = 'usb';
365 $data->{rpm} = 0;
366 }
367 }
368
369 my $health;
370 my $wearout;
371 if ($type eq 'ssd' && !defined($disk)) {
372 # if we have an ssd we try to get the wearout indicator
373 my $smartdata = get_smart_data($devpath);
374 $health = $smartdata->{health};
375 foreach my $attr (@{$smartdata->{attributes}}) {
376 # ID 233 is media wearout indicator on intel and sandisk
377 # ID 177 is media wearout indicator on samsung
378 next if ($attr->{id} != 233 && $attr->{id} != 177);
379 next if ($attr->{name} !~ m/wear/i);
380 $wearout = $attr->{value};
381
382 # prefer the 233 value
383 last if ($attr->{id} == 233);
384 }
385
386 $wearout = 'N/A' if !defined($wearout);
387 } elsif (!defined($disk)) {
388 # we do not need smart data if we check a single disk
389 # because this functionality is only for disk_is_used
390 $health = get_smart_health($devpath) if !defined($disk);
391 }
392
393 my $used;
394
395 $used = 'LVM' if $lvmlist->{$devpath};
396
397 $used = 'mounted' if &$dev_is_mounted($devpath);
398
399 $used = 'ZFS' if $zfslist->{$devpath};
400
401 $disklist->{$dev} = {
402 vendor => $sysdata->{vendor},
403 model => $sysdata->{model},
404 size => $sysdata->{size},
405 serial => $data->{serial},
406 gpt => $data->{gpt},
407 rpm => $data->{rpm},
408 type => $type,
409 wwn => $data->{wwn},
410 health => $health,
411 devpath => $devpath,
412 wearout => $wearout,
413 };
414
415 my $osdid = -1;
416
417 my $journal_count = 0;
418
419 my $found_partitions;
420 my $found_lvm;
421 my $found_mountpoints;
422 my $found_zfs;
423 my $found_dm;
424 my $partpath = $devpath;
425
426 # remove part after last / to
427 # get the base path for the partitions
428 # e.g. from /dev/cciss/c0d0 get /dev/cciss
429 $partpath =~ s/\/[^\/]+$//;
430
431 dir_glob_foreach("$sysdir", "$dev.+", sub {
432 my ($part) = @_;
433
434 $found_partitions = 1;
435
436 if (my $mp = &$dev_is_mounted("$partpath/$part")) {
437 $found_mountpoints = 1;
438 if ($mp =~ m|^/var/lib/ceph/osd/ceph-(\d+)$|) {
439 $osdid = $1;
440 }
441 }
442
443 if ($lvmlist->{"$partpath/$part"}) {
444 $found_lvm = 1;
445 }
446
447 if ($zfslist->{"$partpath/$part"}) {
448 $found_zfs = 1;
449 }
450
451 $journal_count++ if $journalhash->{"$partpath/$part"};
452
453 if (!&$dir_is_empty("$sysdir/$part/holders") && !$found_lvm) {
454 $found_dm = 1;
455 }
456 });
457
458 $used = 'mounted' if $found_mountpoints && !$used;
459 $used = 'LVM' if $found_lvm && !$used;
460 $used = 'ZFS' if $found_zfs && !$used;
461 $used = 'Device Mapper' if $found_dm && !$used;
462 $used = 'partitions' if $found_partitions && !$used;
463
464 # multipath, software raid, etc.
465 # this check comes in last, to show more specific info
466 # if we have it
467 $used = 'Device Mapper' if !$used && !&$dir_is_empty("$sysdir/holders");
468
469 $disklist->{$dev}->{used} = $used if $used;
470 $disklist->{$dev}->{osdid} = $osdid;
471 $disklist->{$dev}->{journals} = $journal_count;
472 });
473
474 return $disklist;
475
476 }
477
478 1;