-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathos.go
72 lines (61 loc) · 1.46 KB
/
os.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main
import (
"bytes"
"fmt"
"os"
"os/exec"
"github.com/docker/docker/pkg/mount"
"github.com/sirupsen/logrus"
)
var supportedFileystemTypes = [...]string{"ext4", "xfs", "ext3", "ext2"}
func getMounts() (map[string]string, error) {
mounts, err := mount.GetMounts()
if err != nil {
return nil, err
}
mountsMap := make(map[string]string, len(mounts))
for _, mount := range mounts {
mountsMap[mount.Source] = mount.Mountpoint
}
return mountsMap, nil
}
func mkfs(dev, fstype string) error {
mkfsExec := fmt.Sprintf("/sbin/mkfs.%s", fstype)
cmd := exec.Command(mkfsExec, dev)
var stderr bytes.Buffer
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
logrus.Errorf("mkfs stderr: %s", stderr.String())
return err
}
return nil
}
func setPermissions(dev, fstype string, uid int, gid int) (err error) {
tmpDir, err := os.MkdirTemp(os.TempDir(), "mnt-*")
if err != nil {
return fmt.Errorf("creating temp dir for chmod: %w", err)
}
if err := mount.Mount(
dev,
tmpDir,
fstype,
"",
); err != nil {
// nothing to clean up yet
return fmt.Errorf("mounting: %w", err)
}
defer func() {
// clean up
if unmountErr := mount.Unmount(tmpDir); err == nil && unmountErr != nil {
err = fmt.Errorf("unmounting after chown: %w", unmountErr)
return
}
if rmErr := os.Remove(tmpDir); err == nil && rmErr != nil {
err = rmErr
}
}()
if err := os.Chown(tmpDir, uid, gid); err != nil {
return fmt.Errorf("chowning: %w", err)
}
return nil
}