New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
btrfs: auto-detect device #813
Merged
crosbymichael
merged 4 commits into
containerd:master
from
AkihiroSuda:btrfs-zero-config
May 10, 2017
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package mountinfo | ||
|
||
// Info reveals information about a particular mounted filesystem. This | ||
// struct is populated from the content in the /proc/<pid>/mountinfo file. | ||
type Info struct { | ||
// ID is a unique identifier of the mount (may be reused after umount). | ||
ID int | ||
|
||
// Parent indicates the ID of the mount parent (or of self for the top of the | ||
// mount tree). | ||
Parent int | ||
|
||
// Major indicates one half of the device ID which identifies the device class. | ||
Major int | ||
|
||
// Minor indicates one half of the device ID which identifies a specific | ||
// instance of device. | ||
Minor int | ||
|
||
// Root of the mount within the filesystem. | ||
Root string | ||
|
||
// Mountpoint indicates the mount point relative to the process's root. | ||
Mountpoint string | ||
|
||
// Options represents mount-specific options. | ||
Options string | ||
|
||
// Optional represents optional fields. | ||
Optional string | ||
|
||
// FSType indicates the type of filesystem, such as EXT3. | ||
FSType string | ||
|
||
// Source indicates filesystem specific information or "none". | ||
Source string | ||
|
||
// VFSOptions represents per super block options. | ||
VFSOptions string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package mountinfo | ||
|
||
/* | ||
#include <sys/param.h> | ||
#include <sys/ucred.h> | ||
#include <sys/mount.h> | ||
*/ | ||
import "C" | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"unsafe" | ||
) | ||
|
||
// Self retrieves a list of mounts for the current running process. | ||
func Self() ([]Info, error) { | ||
var rawEntries *C.struct_statfs | ||
|
||
count := int(C.getmntinfo(&rawEntries, C.MNT_WAIT)) | ||
if count == 0 { | ||
return nil, fmt.Errorf("Failed to call getmntinfo") | ||
} | ||
|
||
var entries []C.struct_statfs | ||
header := (*reflect.SliceHeader)(unsafe.Pointer(&entries)) | ||
header.Cap = count | ||
header.Len = count | ||
header.Data = uintptr(unsafe.Pointer(rawEntries)) | ||
|
||
var out []Info | ||
for _, entry := range entries { | ||
var mountinfo Info | ||
mountinfo.Mountpoint = C.GoString(&entry.f_mntonname[0]) | ||
mountinfo.Source = C.GoString(&entry.f_mntfromname[0]) | ||
mountinfo.FSType = C.GoString(&entry.f_fstypename[0]) | ||
out = append(out, mountinfo) | ||
} | ||
return out, nil | ||
} | ||
|
||
// PID collects the mounts for a specific process ID. | ||
func PID(pid int) ([]Info, error) { | ||
return nil, fmt.Errorf("mountinfo.PID is not implemented on freebsd") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// +build linux | ||
|
||
package mountinfo | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"io" | ||
"os" | ||
"strings" | ||
) | ||
|
||
const ( | ||
/* 36 35 98:0 /mnt1 /mnt2 rw,noatime master:1 - ext3 /dev/root rw,errors=continue | ||
(1)(2)(3) (4) (5) (6) (7) (8) (9) (10) (11) | ||
|
||
(1) mount ID: unique identifier of the mount (may be reused after umount) | ||
(2) parent ID: ID of parent (or of self for the top of the mount tree) | ||
(3) major:minor: value of st_dev for files on filesystem | ||
(4) root: root of the mount within the filesystem | ||
(5) mount point: mount point relative to the process's root | ||
(6) mount options: per mount options | ||
(7) optional fields: zero or more fields of the form "tag[:value]" | ||
(8) separator: marks the end of the optional fields | ||
(9) filesystem type: name of filesystem of the form "type[.subtype]" | ||
(10) mount source: filesystem specific information or "none" | ||
(11) super options: per super block options*/ | ||
mountinfoFormat = "%d %d %d:%d %s %s %s %s" | ||
) | ||
|
||
// Self retrieves a list of mounts for the current running process. | ||
func Self() ([]Info, error) { | ||
f, err := os.Open("/proc/self/mountinfo") | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer f.Close() | ||
|
||
return parseInfoFile(f) | ||
} | ||
|
||
func parseInfoFile(r io.Reader) ([]Info, error) { | ||
var ( | ||
s = bufio.NewScanner(r) | ||
out = []Info{} | ||
) | ||
|
||
for s.Scan() { | ||
if err := s.Err(); err != nil { | ||
return nil, err | ||
} | ||
|
||
var ( | ||
p = Info{} | ||
text = s.Text() | ||
optionalFields string | ||
) | ||
|
||
if _, err := fmt.Sscanf(text, mountinfoFormat, | ||
&p.ID, &p.Parent, &p.Major, &p.Minor, | ||
&p.Root, &p.Mountpoint, &p.Options, &optionalFields); err != nil { | ||
return nil, fmt.Errorf("Scanning '%s' failed: %s", text, err) | ||
} | ||
// Safe as mountinfo encodes mountpoints with spaces as \040. | ||
index := strings.Index(text, " - ") | ||
postSeparatorFields := strings.Fields(text[index+3:]) | ||
if len(postSeparatorFields) < 3 { | ||
return nil, fmt.Errorf("Error found less than 3 fields post '-' in %q", text) | ||
} | ||
|
||
if optionalFields != "-" { | ||
p.Optional = optionalFields | ||
} | ||
|
||
p.FSType = postSeparatorFields[0] | ||
p.Source = postSeparatorFields[1] | ||
p.VFSOptions = strings.Join(postSeparatorFields[2:], " ") | ||
out = append(out, p) | ||
} | ||
return out, nil | ||
} | ||
|
||
// PID collects the mounts for a specific process ID. If the process | ||
// ID is unknown, it is better to use `Self` which will inspect | ||
// "/proc/self/mountinfo" instead. | ||
func PID(pid int) ([]Info, error) { | ||
f, err := os.Open(fmt.Sprintf("/proc/%d/mountinfo", pid)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer f.Close() | ||
|
||
return parseInfoFile(f) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please link to the place you copypasted this from: https://www.kernel.org/doc/Documentation/filesystems/proc.txt.
Also, please be mindful of copy pasting from other sources without attribution.