forked from cloudfoundry-attic/bosh-init
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sfdisk_partitioner.go
164 lines (131 loc) · 4.49 KB
/
sfdisk_partitioner.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package disk
import (
"fmt"
bosherr "github.com/cloudfoundry/bosh-agent/errors"
boshlog "github.com/cloudfoundry/bosh-agent/logger"
boshsys "github.com/cloudfoundry/bosh-agent/system"
"strconv"
"strings"
)
type sfdiskPartitioner struct {
logger boshlog.Logger
cmdRunner boshsys.CmdRunner
logTag string
}
func NewSfdiskPartitioner(logger boshlog.Logger, cmdRunner boshsys.CmdRunner) (partitioner sfdiskPartitioner) {
partitioner.logger = logger
partitioner.cmdRunner = cmdRunner
partitioner.logTag = "SfdiskPartitioner"
return
}
func (p sfdiskPartitioner) Partition(devicePath string, partitions []Partition) error {
if p.diskMatchesPartitions(devicePath, partitions) {
p.logger.Info(p.logTag, "%s already partitioned as expected, skipping", devicePath)
return nil
}
sfdiskPartitionTypes := map[PartitionType]string{
PartitionTypeSwap: "S",
PartitionTypeLinux: "L",
}
sfdiskInput := ""
for index, partition := range partitions {
sfdiskPartitionType := sfdiskPartitionTypes[partition.Type]
partitionSize := fmt.Sprintf("%d", p.convertFromBytesToMb(partition.SizeInBytes))
if index == len(partitions)-1 {
partitionSize = ""
}
sfdiskInput = sfdiskInput + fmt.Sprintf(",%s,%s\n", partitionSize, sfdiskPartitionType)
}
p.logger.Info(p.logTag, "Partitioning %s with %s", devicePath, sfdiskInput)
_, _, _, err := p.cmdRunner.RunCommandWithInput(sfdiskInput, "sfdisk", "-uM", devicePath)
if err != nil {
return bosherr.WrapError(err, "Shelling out to sfdisk")
}
return nil
}
func (p sfdiskPartitioner) GetDeviceSizeInBytes(devicePath string) (uint64, error) {
stdout, _, _, err := p.cmdRunner.RunCommand("sfdisk", "-s", devicePath)
if err != nil {
return 0, bosherr.WrapError(err, "Shelling out to sfdisk")
}
sizeInKb, err := strconv.ParseUint(strings.Trim(stdout, "\n"), 10, 64)
if err != nil {
return 0, bosherr.WrapError(err, "Converting disk size to integer")
}
return p.convertFromKbToBytes(sizeInKb), nil
}
func (p sfdiskPartitioner) diskMatchesPartitions(devicePath string, partitionsToMatch []Partition) (result bool) {
existingPartitions, err := p.getPartitions(devicePath)
if err != nil {
err = bosherr.WrapErrorf(err, "Getting partitions for %s", devicePath)
return
}
if len(existingPartitions) < len(partitionsToMatch) {
return
}
remainingDiskSpace, err := p.GetDeviceSizeInBytes(devicePath)
if err != nil {
err = bosherr.WrapErrorf(err, "Getting device size for %s", devicePath)
return
}
for index, partitionToMatch := range partitionsToMatch {
if index == len(partitionsToMatch)-1 {
partitionToMatch.SizeInBytes = remainingDiskSpace
}
existingPartition := existingPartitions[index]
switch {
case existingPartition.Type != partitionToMatch.Type:
return
case !withinDelta(existingPartition.SizeInBytes, partitionToMatch.SizeInBytes, p.convertFromMbToBytes(20)):
return
}
remainingDiskSpace = remainingDiskSpace - partitionToMatch.SizeInBytes
}
return true
}
func (p sfdiskPartitioner) getPartitions(devicePath string) (partitions []Partition, err error) {
stdout, _, _, err := p.cmdRunner.RunCommand("sfdisk", "-d", devicePath)
if err != nil {
err = bosherr.WrapError(err, "Shelling out to sfdisk")
return
}
allLines := strings.Split(stdout, "\n")
if len(allLines) < 4 {
return
}
partitionLines := allLines[3 : len(allLines)-1]
for _, partitionLine := range partitionLines {
partitionPath, partitionType := extractPartitionPathAndType(partitionLine)
partition := Partition{Type: partitionType}
if partition.Type != PartitionTypeEmpty {
size, err := p.GetDeviceSizeInBytes(partitionPath)
if err == nil {
partition.SizeInBytes = size
}
}
partitions = append(partitions, partition)
}
return
}
var partitionTypesMap = map[string]PartitionType{
"82": PartitionTypeSwap,
"83": PartitionTypeLinux,
"0": PartitionTypeEmpty,
}
func extractPartitionPathAndType(line string) (partitionPath string, partitionType PartitionType) {
partitionFields := strings.Fields(line)
lastField := partitionFields[len(partitionFields)-1]
sfdiskPartitionType := strings.Replace(lastField, "Id=", "", 1)
partitionPath = partitionFields[0]
partitionType = partitionTypesMap[sfdiskPartitionType]
return
}
func (p sfdiskPartitioner) convertFromBytesToMb(sizeInBytes uint64) uint64 {
return sizeInBytes / (1024 * 1024)
}
func (p sfdiskPartitioner) convertFromMbToBytes(sizeInMb uint64) uint64 {
return sizeInMb * 1024 * 1024
}
func (p sfdiskPartitioner) convertFromKbToBytes(sizeInKb uint64) uint64 {
return sizeInKb * 1024
}