7

Wondering if there has been anyone experimenting with low-level disk I/O, such as reading raw sectors, MBR, etc. I've done some digging around myself, but haven't been able to find anything mentioned about it. Most of it is dead ends where someone is talking about Go's native io package.

Any leads would be appreciated.

10
  • On *nix you'd read/write the raw device files in /dev/
    – nos
    Commented Jan 9, 2014 at 22:26
  • Yeah, but it would be nice if there were a cross-platform way of doing it since Go is cross-platform... without having to code everything in C, though possible if required. The /dev/ files would be fine with me, since I run *NIX on everything. I know they don't exist in Windows, though.
    – Sly
    Commented Jan 9, 2014 at 22:28
  • 4
    no such cross platform API exists, at least not for Go. btw windows have special files in the \\.\ namespace, e.g. \\.\PhysicalDisk0, or \Device\XXX\
    – nos
    Commented Jan 9, 2014 at 22:36
  • 1
    Most likely you will find such universal API in nearest future. Anyway accessing disk on Windows is similar to *NIX. \\.\PhysicalDriveN or \\.\X: support.microsoft.com/kb/100027 (You posted before me)
    – Tema
    Commented Jan 9, 2014 at 22:38
  • 1
    @FUZxxl Yeah, I'm aware it's all platform dependent. I wanted to know if someone has experimented with Go having a native ability to do low-level disk I/O without depending on anything external. I figured, being a compiled language, that it may have something helpful hidden that I haven't had a chance to see yet. From the comments here, it doesn't seem like there's a way to do it without depending on something external.
    – Sly
    Commented Jan 10, 2014 at 10:05

2 Answers 2

10

I am still new to go so my example is not particularly elegant, but I think this is what you want:

package main

import (
    "syscall"
    "fmt"
)

func main() {
    disk := "/dev/sda"
    var fd, numread int
    var err error

    fd, err = syscall.Open(disk, syscall.O_RDONLY, 0777)

    if err != nil {
        fmt.Print(err.Error(), "\n")
        return
    }

    buffer := make([]byte, 10, 100)

    numread, err = syscall.Read(fd, buffer)

    if err != nil {
        fmt.Print(err.Error(), "\n")
    }

    fmt.Printf("Numbytes read: %d\n", numread)
    fmt.Printf("Buffer: %b\n", buffer)

    err = syscall.Close(fd)

    if err != nil {
        fmt.Print(err.Error(), "\n")
    }
}

Here is a link to the syscall package documentation: http://golang.org/pkg/syscall/

According to this page, this package attempts to be compatible with as many different platforms as possible but it kinda seems to my novice eye like the main target is the Linux API with, of course, go idioms to simplify things. I hope this answers your question!

2
  • Haven't had a chance to check this out yet, but +1 anyway for the effort. :D
    – Sly
    Commented Jan 15, 2014 at 17:07
  • I gave it some more thought, and I actually don't think this is particularly following the Linux API except in the sense that these system call functions are named similarly--I really don't know enough about other OSes to comment on whether or not the names match up. For example, the syscall.Open function always requires mode argument even if the call is not actually creating a file.
    – waynr
    Commented Jan 16, 2014 at 17:36
0

It seems it is already late, but it might be interesting for somebody else. There is an interesting example of how to work with MBR on Windows: GoogleCloudPlatform/compute-image-tools

and they use package golang.org/x/sys/windows

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.