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