1
2
3
4
5 package oshi.driver.unix.aix;
6
7 import java.util.ArrayList;
8 import java.util.Collections;
9 import java.util.Comparator;
10 import java.util.HashMap;
11 import java.util.List;
12 import java.util.Map;
13 import java.util.Map.Entry;
14 import java.util.concurrent.ConcurrentHashMap;
15 import java.util.stream.Collectors;
16
17 import oshi.annotation.concurrent.ThreadSafe;
18 import oshi.hardware.HWPartition;
19 import oshi.util.ExecutingCommand;
20 import oshi.util.ParseUtil;
21 import oshi.util.tuples.Pair;
22
23
24
25
26 @ThreadSafe
27 public final class Lspv {
28
29
30
31
32
33 private static final Map<String, List<HWPartition>> PARTITION_CACHE = new ConcurrentHashMap<>();
34
35 private Lspv() {
36 }
37
38
39
40
41
42
43
44
45
46 public static List<HWPartition> queryLogicalVolumes(String device, Map<String, Pair<Integer, Integer>> majMinMap) {
47 return PARTITION_CACHE.computeIfAbsent(device,
48 d -> Collections.unmodifiableList(computeLogicalVolumes(d, majMinMap).stream()
49 .sorted(Comparator.comparing(HWPartition::getMinor).thenComparing(HWPartition::getName))
50 .collect(Collectors.toList())));
51 }
52
53 private static List<HWPartition> computeLogicalVolumes(String device,
54 Map<String, Pair<Integer, Integer>> majMinMap) {
55 List<HWPartition> partitions = new ArrayList<>();
56
57
58
59
60
61
62
63
64
65
66
67
68
69 String stateMarker = "PV STATE:";
70 String sizeMarker = "PP SIZE:";
71 long ppSize = 0L;
72 for (String s : ExecutingCommand.runNative("lspv -L " + device)) {
73 if (s.startsWith(stateMarker)) {
74 if (!s.contains("active")) {
75 return partitions;
76 }
77 } else if (s.contains(sizeMarker)) {
78 ppSize = ParseUtil.getFirstIntValue(s);
79 }
80 }
81 if (ppSize == 0L) {
82 return partitions;
83 }
84
85 ppSize <<= 20;
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114 Map<String, String> mountMap = new HashMap<>();
115 Map<String, String> typeMap = new HashMap<>();
116 Map<String, Integer> ppMap = new HashMap<>();
117 for (String s : ExecutingCommand.runNative("lspv -p " + device)) {
118 String[] split = ParseUtil.whitespaces.split(s.trim());
119 if (split.length >= 6 && "used".equals(split[1])) {
120
121 String name = split[split.length - 3];
122 mountMap.put(name, split[split.length - 1]);
123 typeMap.put(name, split[split.length - 2]);
124 int ppCount = 1 + ParseUtil.getNthIntValue(split[0], 2) - ParseUtil.getNthIntValue(split[0], 1);
125 ppMap.put(name, ppCount + ppMap.getOrDefault(name, 0));
126 }
127 }
128 for (Entry<String, String> entry : mountMap.entrySet()) {
129 String mount = "N/A".equals(entry.getValue()) ? "" : entry.getValue();
130
131 String name = entry.getKey();
132 String type = typeMap.get(name);
133 long size = ppSize * ppMap.get(name);
134 Pair<Integer, Integer> majMin = majMinMap.get(name);
135 int major = majMin == null ? ParseUtil.getFirstIntValue(name) : majMin.getA();
136 int minor = majMin == null ? ParseUtil.getFirstIntValue(name) : majMin.getB();
137 partitions.add(new HWPartition(name, name, type, "", size, major, minor, mount));
138 }
139 return partitions;
140 }
141 }