oshi.hardware.platform.linux.LinuxLogicalVolumeGroup Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2021-2024 The OSHI Project Contributors
* SPDX-License-Identifier: MIT
*/
package oshi.hardware.platform.linux;
import static oshi.software.os.linux.LinuxOperatingSystem.HAS_UDEV;
import java.io.File;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.sun.jna.platform.linux.Udev;
import oshi.hardware.LogicalVolumeGroup;
import oshi.hardware.common.AbstractLogicalVolumeGroup;
import oshi.util.ExecutingCommand;
import oshi.util.ParseUtil;
import oshi.util.Util;
import oshi.util.platform.linux.DevPath;
final class LinuxLogicalVolumeGroup extends AbstractLogicalVolumeGroup {
private static final Logger LOG = LoggerFactory.getLogger(LinuxLogicalVolumeGroup.class);
private static final String BLOCK = "block";
private static final String DM_UUID = "DM_UUID";
private static final String DM_VG_NAME = "DM_VG_NAME";
private static final String DM_LV_NAME = "DM_LV_NAME";
LinuxLogicalVolumeGroup(String name, Map> lvMap, Set pvSet) {
super(name, lvMap, pvSet);
}
static List getLogicalVolumeGroups() {
if (!HAS_UDEV) {
LOG.warn("Logical Volume Group information requires libudev, which is not present.");
return Collections.emptyList();
}
Map>> logicalVolumesMap = new HashMap<>();
Map> physicalVolumesMap = new HashMap<>();
// Populate pv map from pvs command
// This requires elevated permissions and may fail
for (String s : ExecutingCommand.runNative("pvs -o vg_name,pv_name")) {
String[] split = ParseUtil.whitespaces.split(s.trim());
if (split.length == 2 && split[1].startsWith(DevPath.DEV)) {
physicalVolumesMap.computeIfAbsent(split[0], k -> new HashSet<>()).add(split[1]);
}
}
// Populate lv map from udev
Udev.UdevContext udev = Udev.INSTANCE.udev_new();
try {
Udev.UdevEnumerate enumerate = udev.enumerateNew();
try {
enumerate.addMatchSubsystem(BLOCK);
enumerate.scanDevices();
for (Udev.UdevListEntry entry = enumerate.getListEntry(); entry != null; entry = entry.getNext()) {
String syspath = entry.getName();
Udev.UdevDevice device = udev.deviceNewFromSyspath(syspath);
if (device != null) {
try {
String devnode = device.getDevnode();
if (devnode != null && devnode.startsWith(DevPath.DM)) {
String uuid = device.getPropertyValue(DM_UUID);
if (uuid != null && uuid.startsWith("LVM-")) {
String vgName = device.getPropertyValue(DM_VG_NAME);
String lvName = device.getPropertyValue(DM_LV_NAME);
if (!Util.isBlank(vgName) && !Util.isBlank(lvName)) {
logicalVolumesMap.computeIfAbsent(vgName, k -> new HashMap<>());
Map> lvMapForGroup = logicalVolumesMap.get(vgName);
// Backup to add to pv set if pvs command failed
physicalVolumesMap.computeIfAbsent(vgName, k -> new HashSet<>());
Set pvSetForGroup = physicalVolumesMap.get(vgName);
File slavesDir = new File(syspath + "/slaves");
File[] slaves = slavesDir.listFiles();
if (slaves != null) {
for (File f : slaves) {
String pvName = f.getName();
lvMapForGroup.computeIfAbsent(lvName, k -> new HashSet<>())
.add(DevPath.DEV + pvName);
// Backup to add to pv set if pvs command failed
// Added /dev/ to remove duplicates like /dev/sda1 and sda1
pvSetForGroup.add(DevPath.DEV + pvName);
}
}
}
}
}
} finally {
device.unref();
}
}
}
} finally {
enumerate.unref();
}
} finally {
udev.unref();
}
return logicalVolumesMap.entrySet().stream()
.map(e -> new LinuxLogicalVolumeGroup(e.getKey(), e.getValue(), physicalVolumesMap.get(e.getKey())))
.collect(Collectors.toList());
}
}