|
| 1 | +package ephem |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "regexp" |
| 6 | + "strconv" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/moby/sys/mountinfo" |
| 10 | +) |
| 11 | + |
| 12 | +var specialFsRegex = regexp.MustCompile(`^(/proc|/dev|/sys|/run|/var/lib/docker|/var/lib/nfs/rpc_pipefs).*`) |
| 13 | + |
| 14 | +// MountInfo represents the information about a mount point. |
| 15 | +// It is just a type alias for mountinfo.Info to allow us to add JSON marshalling. |
| 16 | +type MountInfo struct { |
| 17 | + mountinfo.Info |
| 18 | +} |
| 19 | + |
| 20 | +func (i MountInfo) MarshalJSON() ([]byte, error) { |
| 21 | + return json.Marshal(map[string]string{ |
| 22 | + "id": strconv.Itoa(i.ID), |
| 23 | + "parent_id": strconv.Itoa(i.Parent), |
| 24 | + "major": strconv.Itoa(i.Major), |
| 25 | + "minor": strconv.Itoa(i.Minor), |
| 26 | + "root": i.Root, |
| 27 | + "mount_point": i.Mountpoint, |
| 28 | + "mount_options": i.Options, |
| 29 | + "optional": i.Optional, |
| 30 | + "filesystem_type": i.FSType, |
| 31 | + "mount_source": i.Source, |
| 32 | + "super_options": i.VFSOptions, |
| 33 | + }) |
| 34 | +} |
| 35 | + |
| 36 | +func Mounts() ([]*MountInfo, error) { |
| 37 | + info, err := mountinfo.GetMounts(mountFilter) |
| 38 | + if err != nil { |
| 39 | + return nil, err |
| 40 | + } |
| 41 | + var result []*MountInfo |
| 42 | + for idx := range info { |
| 43 | + result = append(result, &MountInfo{Info: *info[idx]}) |
| 44 | + } |
| 45 | + return result, nil |
| 46 | +} |
| 47 | + |
| 48 | +func mountFilter(info *mountinfo.Info) (skip, stop bool) { |
| 49 | + if skip = specialFsRegex.MatchString(info.Mountpoint); skip { |
| 50 | + return true, false |
| 51 | + } |
| 52 | + |
| 53 | + // skip the read-only bind-mount on /nix/store |
| 54 | + // https://github.com/NixOS/nixpkgs/blob/dac9cdf8c930c0af98a63cbfe8005546ba0125fb/nixos/modules/installer/tools/nixos-generate-config.pl#L395 |
| 55 | + if info.Mountpoint == "/nix/store" && strings.Contains(info.VFSOptions, "rw") && strings.Contains(info.Options, "ro") { |
| 56 | + return true, false |
| 57 | + } |
| 58 | + |
| 59 | + return false, false |
| 60 | +} |
0 commit comments