forked from cloudfoundry-attic/bosh-init
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linux_formatter.go
53 lines (45 loc) · 1.36 KB
/
linux_formatter.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
package disk
import (
"fmt"
bosherr "github.com/cloudfoundry/bosh-agent/errors"
boshsys "github.com/cloudfoundry/bosh-agent/system"
"strings"
)
type linuxFormatter struct {
runner boshsys.CmdRunner
fs boshsys.FileSystem
}
func NewLinuxFormatter(runner boshsys.CmdRunner, fs boshsys.FileSystem) (formatter linuxFormatter) {
formatter.runner = runner
formatter.fs = fs
return
}
func (f linuxFormatter) Format(partitionPath string, fsType FileSystemType) (err error) {
if f.partitionHasGivenType(partitionPath, fsType) {
return
}
switch fsType {
case FileSystemSwap:
_, _, _, err = f.runner.RunCommand("mkswap", partitionPath)
if err != nil {
err = bosherr.WrapError(err, "Shelling out to mkswap")
}
case FileSystemExt4:
if f.fs.FileExists("/sys/fs/ext4/features/lazy_itable_init") {
_, _, _, err = f.runner.RunCommand("mke2fs", "-t", "ext4", "-j", "-E", "lazy_itable_init=1", partitionPath)
} else {
_, _, _, err = f.runner.RunCommand("mke2fs", "-t", "ext4", "-j", partitionPath)
}
if err != nil {
err = bosherr.WrapError(err, "Shelling out to mke2fs")
}
}
return
}
func (f linuxFormatter) partitionHasGivenType(partitionPath string, fsType FileSystemType) bool {
stdout, _, _, err := f.runner.RunCommand("blkid", "-p", partitionPath)
if err != nil {
return false
}
return strings.Contains(stdout, fmt.Sprintf(` TYPE="%s"`, fsType))
}