github.com/xyproto/u-root@v6.0.1-0.20200302025726-5528e0c77a3c+incompatible/pkg/mount/fs_linux.go (about)

     1  // Copyright 2014-2017 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package mount
     6  
     7  import (
     8  	"fmt"
     9  	"io/ioutil"
    10  	"strings"
    11  )
    12  
    13  // FindFileSystem returns nil if a file system is available for use.
    14  //
    15  // It rereads /proc/filesystems each time as the supported file systems can change
    16  // as modules are added and removed.
    17  func FindFileSystem(fs string) error {
    18  	b, err := ioutil.ReadFile("/proc/filesystems")
    19  	if err != nil {
    20  		return err
    21  	}
    22  	for _, l := range strings.Split(string(b), "\n") {
    23  		f := strings.Fields(l)
    24  		if (len(f) > 1 && f[0] == "nodev" && f[1] == fs) || (len(f) > 0 && f[0] != "nodev" && f[0] == fs) {
    25  			return nil
    26  		}
    27  	}
    28  	return fmt.Errorf("%s not found", fs)
    29  }
    30  
    31  // GetBlockFilesystems returns the supported file systems for block devices.
    32  func GetBlockFilesystems() (fstypes []string, err error) {
    33  	return internalGetFilesystems("/proc/filesystems")
    34  }
    35  
    36  func internalGetFilesystems(file string) (fstypes []string, err error) {
    37  	var bytes []byte
    38  	if bytes, err = ioutil.ReadFile(file); err != nil {
    39  		return nil, fmt.Errorf("failed to read supported file systems: %v", err)
    40  	}
    41  	for _, line := range strings.Split(string(bytes), "\n") {
    42  		// len(fields)==1, 2 possibilites for fs: "nodev" fs and
    43  		// fs's. "nodev" fs cannot be mounted through devices.
    44  		// len(fields)==1 prevents this from occurring.
    45  		if fields := strings.Fields(line); len(fields) == 1 {
    46  			fstypes = append(fstypes, fields[0])
    47  		}
    48  	}
    49  	return fstypes, nil
    50  }