go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/providers/os/resources/mount/manager.go (about)

     1  // Copyright (c) Mondoo, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package mount
     5  
     6  import (
     7  	"github.com/cockroachdb/errors"
     8  	"go.mondoo.com/cnquery/providers/os/connection/shared"
     9  )
    10  
    11  type MountPoint struct {
    12  	Device     string
    13  	MountPoint string
    14  	FSType     string
    15  	Options    map[string]string
    16  }
    17  
    18  type OperatingSystemMountManager interface {
    19  	Name() string
    20  	List() ([]MountPoint, error)
    21  }
    22  
    23  func ResolveManager(conn shared.Connection) (OperatingSystemMountManager, error) {
    24  	var mm OperatingSystemMountManager
    25  
    26  	pf := conn.Asset().Platform
    27  	if pf == nil {
    28  		return nil, errors.New("missing platform information")
    29  	}
    30  
    31  	if pf.IsFamily("linux") {
    32  		mm = &LinuxMountManager{conn: conn}
    33  	} else if pf.IsFamily("unix") {
    34  		mm = &UnixMountManager{conn: conn}
    35  	}
    36  
    37  	if mm == nil {
    38  		return nil, errors.New("could not detect suitable mount manager for platform: " + pf.Name)
    39  	}
    40  
    41  	return mm, nil
    42  }
    43  
    44  type LinuxMountManager struct {
    45  	conn shared.Connection
    46  }
    47  
    48  func (s *LinuxMountManager) Name() string {
    49  	return "Linux Mount Manager"
    50  }
    51  
    52  func (s *LinuxMountManager) List() ([]MountPoint, error) {
    53  	// TODO: not working via docker yet
    54  	// // try /proc
    55  	// f, err := s.motor.Provider.File("/proc/mount")
    56  	// if err == nil {
    57  	// 	defer f.Close()
    58  	// 	return ParseLinuxProcMount(f), nil
    59  	// }
    60  
    61  	if s.conn.Capabilities().Has(shared.Capability_RunCommand) {
    62  		cmd, err := s.conn.RunCommand("mount")
    63  		if err != nil {
    64  			return nil, errors.Wrap(err, "could not read mounts")
    65  		}
    66  		return ParseLinuxMountCmd(cmd.Stdout), nil
    67  	} else if s.conn.Capabilities().Has(shared.Capability_File) {
    68  		return mountsFromFSLinux(s.conn.FileSystem())
    69  	}
    70  
    71  	return nil, errors.New("mount not supported for provided transport")
    72  }
    73  
    74  type UnixMountManager struct {
    75  	conn shared.Connection
    76  }
    77  
    78  func (s *UnixMountManager) Name() string {
    79  	return "Unix Mount Manager"
    80  }
    81  
    82  func (s *UnixMountManager) List() ([]MountPoint, error) {
    83  	cmd, err := s.conn.RunCommand("mount")
    84  	if err != nil {
    85  		return nil, errors.Wrap(err, "could not read package list")
    86  	}
    87  
    88  	return ParseUnixMountCmd(cmd.Stdout), nil
    89  }