Forum Stats

  • 3,815,605 Users
  • 2,259,059 Discussions
  • 7,893,185 Comments

Discussions

Java - Exclude Linux Drives

frank.anellia
frank.anellia Member Posts: 104
edited Nov 29, 2017 2:04PM in Java Programming

Hello,

I have the following code that reports on mounted filesystems from /etc/mstab in Linux:

import java.io.IOException;import java.nio.file.FileStore;import java.nio.file.FileSystems;public class linuxMounts {    public static void main(String[] args) throws IOException {      for (FileStore store : FileSystems.getDefault().getFileStores()) {         long total = store.getTotalSpace() / 1024;         long used = (store.getTotalSpace() - store.getUnallocatedSpace()) / 1024;         long avail = store.getUsableSpace() / 1024;         System.out.format("%-20s %12d %12d %12d%n", store, total, used, avail);      }   }}

The output is as follows:

/ (/dev/mapper/rootvg-rootlv)     15350768      3111056     11453280/proc (proc)                    0            0            0/sys (sysfs)                    0            0            0/dev/pts (devpts)               0            0            0/dev/shm (tmpfs)          8160264       649396      7510868/boot (/dev/sda1)          243823       107889       123134/home (/dev/mapper/rootvg-homelv)      5029504      1222208      3545152/opt (/dev/mapper/rootvg-optlv)      3997376       628816      3158848/tmp (/dev/mapper/rootvg-tmplv)      3997376       205540      3582124/usr (/dev/mapper/rootvg-usrlv)     15350768      5281180      9283156/var (/dev/mapper/rootvg-varlv)      3997376      2841840       945824/var/crash (/dev/mapper/rootvg-crashlv)     12254384        30704     11594536/u01 (/dev/mapper/mpathgp1)    103079868     83741420     14098936/backups (/dev/mapper/mpathip1)    103079868     68240572     29596488/proc/sys/fs/binfmt_misc (none)            0            0            0/dev/oracleasm (oracleasmfs)            0            0            0/var/lib/nfs/rpc_pipefs (sunrpc)            0            0            0

I'd like to exclude most of these filesystem and keep only a certain few like '/u01' and '/backups'.  I'd also only like to return '/backups' instead of the full path.

I'm new to Java programming and I've been using Google for several days without any luck.

Thanks in advanced for your help!

Frank

mNemrpc1

Best Answer

  • mNem
    mNem Member Posts: 1,380 Gold Trophy
    edited Nov 29, 2017 8:47AM Answer ✓

    You were able to get your output from

    System.out.format("%-20s %12d %12d %12d%n", store, total, used, avail);  

    where store is passed directly to a placeholder which expects a string value.

    In this case the store.toString() method gets called implicitly.

«13

Answers

  • karianna
    karianna Member Posts: 205 Blue Ribbon
    edited Nov 28, 2017 10:52AM

    Can a moderator move this to the Java language community (Path / Files API / perhaps NIO)

    @Frank take a look at the Paths and FIles API that come with Java 9+ :-)

  • frank.anellia
    frank.anellia Member Posts: 104
    edited Nov 28, 2017 10:58AM

    Sure, it can be moved.

    I'll check out those APIs. Thx!

  • morgalr
    morgalr Member Posts: 457
    edited Nov 28, 2017 3:44PM

    It's easily done, just manipulate the "store" variable to check to see if it contains what you want, and it if does, then output it.  If you want a shortened name, then make a String value in the place of "store" in your System.out statement.

  • frank.anellia
    frank.anellia Member Posts: 104
    edited Nov 29, 2017 6:44AM

    I made the following changes so the code will search for a "u01" filesystem using the "store" variable:

         for (FileStore store : FileSystems.getDefault().getFileStores()) {         String path = store.name();         if (path.contains("u01"))         {         long total = store.getTotalSpace() / 1024 / 1024;         long used = (store.getTotalSpace() - store.getUnallocatedSpace()) / 1024 / 1024;         long avail = store.getUsableSpace() / 1024 / 1024;         System.out.format("%-50s %20d %20d %20d %n", store, total, used, avail);         } else {                System.out.println("Junk");

    However, when I run it nothing is returned.  The problem is either "store.name" or "store.path" on Linux is not returning the correct filesystem name. Here is the output from above code:

    Filesystem                                              Total Size (MB)      Used Space (MB) Available Space (MB)JunkJunkJunkJunkJunk

    How can I use only the "store" variable?  Every attempt to use it failed for me.

    Thanks

    Frank

  • mNem
    mNem Member Posts: 1,380 Gold Trophy
    edited Nov 29, 2017 9:07AM

    You could try something like this ...

    import java.io.IOException;

    import java.nio.file.FileStore;

    import java.nio.file.FileSystems;

    import java.util.HashMap;

    public class linuxMounts {

       public static void main(String[] args) throws IOException {

          HashMap<String, String> lkp = new HashMap<String, String>();

         

          //what to match and what to output

          //sample here ... tweak them to your need

          lkp.put("/boot/efi (/dev/sda1)", "Line Item - 1");

          lkp.put("/run/user/1001 (tmpfs)", "Line Item - 2");

          for (FileStore store : FileSystems.getDefault().getFileStores()) {

             long total = store.getTotalSpace() / 1024;

             long used = (store.getTotalSpace() - store.getUnallocatedSpace()) / 1024;

             long avail = store.getUsableSpace() / 1024;

             String strPath = store.toString();

             if (total != 0 && lkp.containsKey(strPath)) {

                System.out.format("%-40s %12d %12d %12d%n", lkp.get(strPath), total, used, avail);

             }

          }

       }

    }

    EDIT: changed the variable name strName to strPath.

  • frank.anellia
    frank.anellia Member Posts: 104
    edited Nov 29, 2017 7:47AM

    Thanks for the feedback but I'd like to stick with using the "store" variable if that's possible?

  • frank.anellia
    frank.anellia Member Posts: 104
    edited Nov 29, 2017 8:22AM

    When I attempt:

    String path = store();

    I receive the following compilation error:

    linuxMounts.java:18: error: cannot find symbol        String path = store();                      ^  symbol:  method store()  location: class linuxMounts
  • mNem
    mNem Member Posts: 1,380 Gold Trophy
    edited Nov 29, 2017 8:38AM
    String path = store();  

    store is a variable of type java.nio.file.FileStore and not a method available in linuxMounts (static method to be exact).

    So, need to remove the brackets and call an appropriate method available from java.nio.file.FileStore.

  • frank.anellia
    frank.anellia Member Posts: 104
    edited Nov 29, 2017 8:41AM

    I tried that with:

    String path = store.name(); String path = store.type();

    However, these two methods don't return the values I'm looking to pull from the Linux filesystems.  The output from my original post shows a bunch of filesystems that are being returned.  I'd like to narrow that down to only a few.

  • mNem
    mNem Member Posts: 1,380 Gold Trophy
    edited Nov 29, 2017 8:47AM Answer ✓

    You were able to get your output from

    System.out.format("%-20s %12d %12d %12d%n", store, total, used, avail);  

    where store is passed directly to a placeholder which expects a string value.

    In this case the store.toString() method gets called implicitly.

This discussion has been closed.