github.hscsec.cn/u-root/u-root@v7.0.0+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(fstype string) error {
    18  	b, err := ioutil.ReadFile("/proc/filesystems")
    19  	if err != nil {
    20  		return err
    21  	}
    22  	return internalFindFileSystem(string(b), fstype)
    23  }
    24  
    25  func internalFindFileSystem(content string, fstype string) error {
    26  	for _, l := range strings.Split(content, "\n") {
    27  		f := strings.Fields(l)
    28  		if (len(f) > 1 && f[0] == "nodev" && f[1] == fstype) || (len(f) > 0 && f[0] != "nodev" && f[0] == fstype) {
    29  			return nil
    30  		}
    31  	}
    32  	return fmt.Errorf("file system type %q not found", fstype)
    33  }
    34  
    35  // GetBlockFilesystems returns the supported file systems for block devices.
    36  func GetBlockFilesystems() (fstypes []string, err error) {
    37  	return internalGetFilesystems("/proc/filesystems")
    38  }
    39  
    40  func internalGetFilesystems(file string) (fstypes []string, err error) {
    41  	var bytes []byte
    42  	if bytes, err = ioutil.ReadFile(file); err != nil {
    43  		return nil, fmt.Errorf("failed to read supported file systems: %v", err)
    44  	}
    45  	for _, line := range strings.Split(string(bytes), "\n") {
    46  		// len(fields)==1, 2 possibilites for fs: "nodev" fs and
    47  		// fs's. "nodev" fs cannot be mounted through devices.
    48  		// len(fields)==1 prevents this from occurring.
    49  		if fields := strings.Fields(line); len(fields) == 1 {
    50  			fstypes = append(fstypes, fields[0])
    51  		}
    52  	}
    53  	return fstypes, nil
    54  }