-
Notifications
You must be signed in to change notification settings - Fork 42
/
filesystem.go
78 lines (64 loc) · 2.15 KB
/
filesystem.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
73
74
75
76
77
78
package storage
import (
"fmt"
"os"
"os/exec"
log "github.com/sirupsen/logrus"
)
// FileSystemCreate handles the creation of filesystems.
func FileSystemCreate(f Filesystem) error {
var cmd *exec.Cmd
var debugCMD string
switch f.Mount.Format {
case "swap":
cmd = exec.Command("/sbin/mkswap", f.Mount.Device)
debugCMD = fmt.Sprintf("%s %s", "/sbin/mkswap", f.Mount.Device)
case "ext4", "ext3", "ext2":
// Add filesystem flags
f.Mount.Create.Options = append(f.Mount.Create.Options, "-t")
f.Mount.Create.Options = append(f.Mount.Create.Options, f.Mount.Format)
// Add force
f.Mount.Create.Options = append(f.Mount.Create.Options, "-F")
// Add Device to formate
f.Mount.Create.Options = append(f.Mount.Create.Options, f.Mount.Device)
// Format disk
cmd = exec.Command("/sbin/mke2fs", f.Mount.Create.Options...)
// Build command for error message
debugCMD := "/sbin/mke2fs"
for i := range f.Mount.Create.Options {
debugCMD = fmt.Sprintf("%s %s", debugCMD, f.Mount.Create.Options[i])
}
case "vfat":
f.Mount.Create.Options = append(f.Mount.Create.Options, f.Mount.Device)
cmd = exec.Command("/sbin/mkfs.fat", f.Mount.Create.Options...)
// Build command for error message
debugCMD := "/sbin/mkfs.fat"
for i := range f.Mount.Create.Options {
debugCMD = fmt.Sprintf("%s %s", debugCMD, f.Mount.Create.Options[i])
}
case "xfs":
// Add force
f.Mount.Create.Options = append(f.Mount.Create.Options, "-f")
// Add Device to formate
f.Mount.Create.Options = append(f.Mount.Create.Options, f.Mount.Device)
// Format disk
cmd = exec.Command("/sbin/mkfs.xfs", f.Mount.Create.Options...)
// Build command for error message
debugCMD := "/sbin/mkfs.xfs"
for i := range f.Mount.Create.Options {
debugCMD = fmt.Sprintf("%s %s", debugCMD, f.Mount.Create.Options[i])
}
default:
log.Warnf("unknown filesystem type [%s]", f.Mount.Format)
}
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
err := cmd.Start()
if err != nil {
return fmt.Errorf("command [%s] Filesystem [%w]", debugCMD, err)
}
err = cmd.Wait()
if err != nil {
return fmt.Errorf("command [%s] Filesystem [%w]", debugCMD, err)
}
return nil
}