View Javadoc
1   /*
2    * Copyright 2020-2022 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.driver.unix.solaris.disk;
6   
7   import java.util.HashMap;
8   import java.util.List;
9   import java.util.Map;
10  
11  import oshi.annotation.concurrent.ThreadSafe;
12  import oshi.util.ExecutingCommand;
13  import oshi.util.ParseUtil;
14  
15  /**
16   * Utility to query lshal
17   */
18  @ThreadSafe
19  public final class Lshal {
20  
21      private static final String LSHAL_CMD = "lshal";
22  
23      private Lshal() {
24      }
25  
26      /**
27       * Query lshal to get device major
28       *
29       * @return A map with disk names as the key and block device major as the value if lshal is installed; empty map
30       *         otherwise
31       */
32      public static Map<String, Integer> queryDiskToMajorMap() {
33          Map<String, Integer> majorMap = new HashMap<>();
34          List<String> lshal = ExecutingCommand.runNative(LSHAL_CMD);
35          String diskName = null;
36          for (String line : lshal) {
37              if (line.startsWith("udi ")) {
38                  String udi = ParseUtil.getSingleQuoteStringValue(line);
39                  diskName = udi.substring(udi.lastIndexOf('/') + 1);
40              } else {
41                  line = line.trim();
42                  if (line.startsWith("block.major") && diskName != null) {
43                      majorMap.put(diskName, ParseUtil.getFirstIntValue(line));
44                  }
45              }
46          }
47          return majorMap;
48      }
49  }