Skip to main content

Overview

The PartitionStat struct contains information about a disk partition, including its device path, mount point, filesystem type, and mount options. This information is useful for discovering available filesystems and their configurations.

Struct Definition

type PartitionStat struct {
    Device     string   `json:"device"`
    Mountpoint string   `json:"mountpoint"`
    Fstype     string   `json:"fstype"`
    Opts       []string `json:"opts"`
}

Fields

Device

  • Type: string
  • Description: Device path or device name
  • Examples:
    • Linux: /dev/sda1, /dev/nvme0n1p1, /dev/mapper/vg0-root
    • Windows: C:\, D:\, \\?\Volume{guid}
    • macOS: /dev/disk1s1
    • Network: server:/export/home (NFS), //server/share (SMB)
  • Note: This is typically a physical device path, but can be a virtual device, network path, or special identifier

Mountpoint

  • Type: string
  • Description: Directory where the filesystem is mounted (accessible path)
  • Examples:
    • Linux/macOS: /, /home, /mnt/data, /media/usb
    • Windows: C:\, D:\, E:\
  • Important: Use this field (not Device) when calling disk.Usage()

Fstype

  • Type: string
  • Description: Filesystem type
  • Common values:
    • Linux: ext4, xfs, btrfs, ext3, ext2, vfat, ntfs, tmpfs, nfs, cifs
    • Windows: NTFS, FAT32, exFAT, ReFS
    • macOS: apfs, hfs, msdos, exfat
    • BSD: ufs, zfs, ffs
    • Special: tmpfs, devtmpfs, sysfs, proc, devfs

Opts

  • Type: []string
  • Description: Mount options as a slice of strings
  • Common options:
    • Linux/Unix:
      • rw / ro - Read-write / Read-only
      • nodev - Do not interpret device files
      • nosuid - Do not allow set-user-ID or set-group-ID bits
      • noexec - Do not allow direct execution of binaries
      • relatime - Update access times relative to modify time
      • defaults - Use default mount options
    • Windows:
      • rw / ro
      • compressed - NTFS compression enabled
      • encrypted - NTFS encryption enabled
  • Example: ["rw", "nosuid", "nodev", "relatime"]

Methods

String

func (d PartitionStat) String() string
Returns a JSON string representation of the struct. Example output:
{
  "device": "/dev/sda1",
  "mountpoint": "/",
  "fstype": "ext4",
  "opts": ["rw", "relatime", "errors=remount-ro"]
}

Partitions Function

func Partitions(all bool) ([]PartitionStat, error)
func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, error)
Parameters:
  • all - Controls which filesystems are returned:
    • false - Returns only physical devices (hard disks, SSDs, USB drives, CD-ROM)
    • true - Returns all filesystems including virtual/pseudo filesystems (tmpfs, sysfs, proc, devfs, etc.)
Note: On BSD systems, the all parameter is ignored due to platform limitations. See psutil issue #906. Returns:
  • []PartitionStat - Slice of partition information
  • error - Error if partitions cannot be enumerated

Usage Examples

List All Physical Partitions

package main

import (
    "fmt"
    "log"
    
    "github.com/shirou/gopsutil/v4/disk"
)

func main() {
    // Get only physical partitions
    partitions, err := disk.Partitions(false)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Printf("Found %d physical partition(s):\n\n", len(partitions))
    
    for _, partition := range partitions {
        fmt.Printf("Device: %s\n", partition.Device)
        fmt.Printf("  Mount Point: %s\n", partition.Mountpoint)
        fmt.Printf("  Filesystem: %s\n", partition.Fstype)
        fmt.Printf("  Options: %v\n", partition.Opts)
        fmt.Println()
    }
}

List All Filesystems (Including Virtual)

package main

import (
    "fmt"
    "log"
    
    "github.com/shirou/gopsutil/v4/disk"
)

func main() {
    // Get all filesystems including virtual ones
    partitions, err := disk.Partitions(true)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Printf("Found %d filesystem(s):\n\n", len(partitions))
    
    for _, partition := range partitions {
        fmt.Printf("%s (%s) mounted at %s\n", 
            partition.Device, partition.Fstype, partition.Mountpoint)
    }
}

Get Usage for Each Partition

package main

import (
    "fmt"
    "log"
    
    "github.com/shirou/gopsutil/v4/disk"
)

func main() {
    partitions, err := disk.Partitions(false)
    if err != nil {
        log.Fatal(err)
    }

    for _, partition := range partitions {
        // IMPORTANT: Use Mountpoint, not Device
        usage, err := disk.Usage(partition.Mountpoint)
        if err != nil {
            log.Printf("Error getting usage for %s: %v\n", partition.Mountpoint, err)
            continue
        }

        fmt.Printf("%s [%s]\n", partition.Mountpoint, partition.Fstype)
        fmt.Printf("  Device: %s\n", partition.Device)
        fmt.Printf("  Total: %v GB\n", usage.Total/1024/1024/1024)
        fmt.Printf("  Used: %v GB (%.2f%%)\n", 
            usage.Used/1024/1024/1024, usage.UsedPercent)
        fmt.Printf("  Free: %v GB\n", usage.Free/1024/1024/1024)
        fmt.Println()
    }
}

Filter Partitions by Filesystem Type

package main

import (
    "fmt"
    "log"
    
    "github.com/shirou/gopsutil/v4/disk"
)

func main() {
    partitions, err := disk.Partitions(true)
    if err != nil {
        log.Fatal(err)
    }

    // Find all ext4 filesystems
    fmt.Println("ext4 filesystems:")
    for _, partition := range partitions {
        if partition.Fstype == "ext4" {
            fmt.Printf("  %s mounted at %s\n", 
                partition.Device, partition.Mountpoint)
        }
    }

    // Find all network filesystems
    fmt.Println("\nNetwork filesystems:")
    for _, partition := range partitions {
        if partition.Fstype == "nfs" || partition.Fstype == "cifs" {
            fmt.Printf("  %s (%s) mounted at %s\n",
                partition.Device, partition.Fstype, partition.Mountpoint)
        }
    }
}

Check Mount Options

package main

import (
    "fmt"
    "log"
    "strings"
    
    "github.com/shirou/gopsutil/v4/disk"
)

func hasOption(opts []string, option string) bool {
    for _, opt := range opts {
        if opt == option || strings.HasPrefix(opt, option+"=") {
            return true
        }
    }
    return false
}

func main() {
    partitions, err := disk.Partitions(false)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Println("Read-only filesystems:")
    for _, partition := range partitions {
        if hasOption(partition.Opts, "ro") {
            fmt.Printf("  %s\n", partition.Mountpoint)
        }
    }

    fmt.Println("\nFilesystems with noexec:")
    for _, partition := range partitions {
        if hasOption(partition.Opts, "noexec") {
            fmt.Printf("  %s\n", partition.Mountpoint)
        }
    }
}

Find Specific Mount Point

package main

import (
    "fmt"
    "log"
    
    "github.com/shirou/gopsutil/v4/disk"
)

func findPartition(mountpoint string) (*disk.PartitionStat, error) {
    partitions, err := disk.Partitions(true)
    if err != nil {
        return nil, err
    }

    for _, partition := range partitions {
        if partition.Mountpoint == mountpoint {
            return &partition, nil
        }
    }
    
    return nil, fmt.Errorf("mount point not found: %s", mountpoint)
}

func main() {
    partition, err := findPartition("/home")
    if err != nil {
        log.Fatal(err)
    }

    fmt.Printf("Found /home:\n")
    fmt.Printf("  Device: %s\n", partition.Device)
    fmt.Printf("  Filesystem: %s\n", partition.Fstype)
    fmt.Printf("  Options: %v\n", partition.Opts)
}

Platform-Specific Behavior

Linux

  • Reads from /proc/mounts or /etc/mtab
  • Both physical and virtual filesystems available
  • all=false filters out pseudo filesystems like proc, sysfs, tmpfs, devtmpfs, etc.
  • Network filesystems (NFS, CIFS) included when mounted

Windows

  • Uses GetLogicalDrives and GetVolumeInformation APIs
  • Returns drive letters as both device and mountpoint (e.g., C:\)
  • all parameter typically has less effect since Windows has fewer virtual filesystems
  • Network drives included when mapped

macOS

  • Uses getmntinfo system call
  • Returns both physical and virtual filesystems
  • APFS container structure may result in multiple entries for a single physical device
  • Network mounts (AFP, SMB, NFS) included

BSD (FreeBSD, OpenBSD)

  • Uses getmntinfo system call
  • Important: The all parameter is ignored on BSD systems
  • All mounted filesystems are returned regardless of the all value
  • Application must filter pseudo filesystems if needed

Common Filesystem Types

Linux

  • Physical: ext4, ext3, ext2, xfs, btrfs, f2fs, ntfs, vfat
  • Virtual: tmpfs, sysfs, proc, devtmpfs, cgroup, cgroup2
  • Network: nfs, nfs4, cifs, smbfs
  • Special: fuse, overlay, aufs

Windows

  • Local: NTFS, FAT32, exFAT, ReFS
  • Network: CIFS, SMB

macOS

  • Local: apfs, hfs, jhfs+ (journaled HFS+), msdos, exfat
  • Network: nfs, smbfs, afpfs
  • Virtual: devfs, autofs

BSD

  • Local: ufs, zfs, ffs, ext2fs
  • Network: nfs
  • Virtual: devfs, procfs, tmpfs

Common Mount Options

Read/Write Permissions

  • rw - Read-write (default)
  • ro - Read-only

Security Options

  • nosuid - Ignore set-user-ID and set-group-ID bits
  • nodev - Do not interpret character or block special devices
  • noexec - Do not allow direct execution of binaries

Access Time Options

  • relatime - Update access times relative to modify/change time
  • noatime - Do not update access times
  • strictatime - Always update access times

Linux-Specific

  • errors=remount-ro - Remount read-only on errors (ext filesystems)
  • errors=continue - Continue on errors
  • data=ordered - Write data before metadata (ext filesystems)
  • discard - Enable TRIM for SSDs

Windows-Specific

  • compressed - NTFS compression enabled
  • encrypted - NTFS encryption enabled

Important Notes

When using partition information with disk.Usage(), always use the Mountpoint field, never the Device field:
// Correct
usage, err := disk.Usage(partition.Mountpoint)

// Wrong - will fail!
usage, err := disk.Usage(partition.Device)
On BSD systems, the all parameter to Partitions() is ignored. You’ll always get all mounted filesystems. Filter the results in your application if you only want physical devices.
Network filesystems (NFS, CIFS, SMB) will appear in the partition list when mounted. Accessing them may be slower, especially if the network connection is poor or the server is unresponsive.

See Also

Build docs developers (and LLMs) love