-
Notifications
You must be signed in to change notification settings - Fork 11
/
mount.go
51 lines (46 loc) · 1.39 KB
/
mount.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
package main
import (
"log"
"os"
"strings"
)
// Remount a filesystem. Grub mounts / as ro during the boot process, and this will get it to
// be readwrite (assuming it's rw in /etc/fstab)
func Remount(dir string) {
if err := run("mount", "-o", "remount", dir); err != nil {
log.Println(err)
}
}
// Mounts a filesystem, creating the mount point if it doesn't exist.
func Mount(typ, device, dir, opts string) {
if _, err := os.Stat(dir); os.IsNotExist(err) {
if err := os.Mkdir(dir, 0775); err != nil {
log.Printf("Could not create mount point %v: %v\n", dir, err)
}
}
if err := run("mount", "-t", typ, device, dir, "-o", opts); err != nil {
log.Println(err)
}
}
// Mounts all filesystems from /etc/fstab, except for ones of the type passed in the
// except parameter
func MountAllExcept(except []string) {
noexcept := make([]string, len(except))
for i, val := range except {
noexcept[i] = "no" + val
}
if err := run("mount", "-a", "-t", strings.Join(noexcept, ","), "-O", "no_netdev"); err != nil {
log.Println(err)
}
}
// Unmounts all filesystems, except for netdev filesystems and those passed in the except
// parameter
func UnmountAllExcept(except []string) {
noexcept := make([]string, len(except))
for i, val := range except {
noexcept[i] = "no" + val
}
if err := run("umount", "-a", "-t", strings.Join(noexcept, ","), "-O", "no_netdev"); err != nil {
log.Println(err)
}
}