-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
ramdisk_linux.go
35 lines (30 loc) · 995 Bytes
/
ramdisk_linux.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
package ramdisk
import (
"bytes"
"fmt"
"os/exec"
)
// LinuxPlatformImplementation is the implementation for Linux systems.
//
// The Linux implementation likely *requires sudo* to function on most distros.
// If you want a sudo-less option, you can simply use /dev/shm instead on most
// modern Linux platforms.
type LinuxPlatformImplementation struct{}
func init() {
implementation = LinuxPlatformImplementation{}
}
func (i LinuxPlatformImplementation) create(opts Options) (*RAMDisk, error) {
rd := RAMDisk{DevicePath: opts.MountPath, MountPath: opts.MountPath}
sizeFlag := fmt.Sprintf("size=%d", opts.Size)
cmd := exec.Command(
"mount", "-v", "-t", "tmpfs", "-o", sizeFlag, "tmpfs", opts.MountPath)
stdout, err := cmd.Output()
if err == nil && opts.Logger != nil {
opts.Logger.Printf("%s\n", bytes.TrimSpace(stdout))
}
return &rd, err
}
func (i LinuxPlatformImplementation) destroy(devicePath string) error {
cmd := exec.Command("umount", devicePath)
return cmd.Run()
}