-
Notifications
You must be signed in to change notification settings - Fork 115
/
sfdisk_partitioner.go
231 lines (190 loc) · 6.71 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package disk
import (
"fmt"
"regexp"
"strconv"
"strings"
"code.cloudfoundry.org/clock"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
boshretry "github.com/cloudfoundry/bosh-utils/retrystrategy"
boshsys "github.com/cloudfoundry/bosh-utils/system"
)
type sfdiskPartitioner struct {
logger boshlog.Logger
cmdRunner boshsys.CmdRunner
logTag string
timeService clock.Clock
}
func NewSfdiskPartitioner(logger boshlog.Logger, cmdRunner boshsys.CmdRunner, timeService clock.Clock) Partitioner {
return sfdiskPartitioner{
logger: logger,
cmdRunner: cmdRunner,
logTag: "SfdiskPartitioner",
timeService: timeService,
}
}
func (p sfdiskPartitioner) Partition(devicePath string, partitions []Partition) error {
partitionMatches, err := p.diskMatchesPartitions(devicePath, partitions)
if err != nil {
return err
}
if partitionMatches {
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)
}
partitionRetryable := boshretry.NewRetryable(func() (bool, error) {
_, _, _, err := p.cmdRunner.RunCommandWithInput(sfdiskInput, "sfdisk", "-uM", devicePath)
if err != nil {
p.logger.Error(p.logTag, "Failed with an error: %s", err)
return true, bosherr.WrapError(err, "Shelling out to sfdisk")
}
p.logger.Info(p.logTag, "Succeeded in partitioning %s with %s", devicePath, sfdiskInput)
return false, nil
})
partitionRetryStrategy := NewPartitionStrategy(partitionRetryable, p.timeService, p.logger)
err = partitionRetryStrategy.Try()
if err != nil {
return err
}
if strings.Contains(devicePath, "/dev/mapper/") {
_, _, _, err = p.cmdRunner.RunCommand("/etc/init.d/open-iscsi", "restart")
if err != nil {
return bosherr.WrapError(err, "Shelling out to restart open-iscsi")
}
_, _, _, err = p.cmdRunner.RunCommand("/etc/init.d/multipath-tools", "restart")
if err != nil {
return bosherr.WrapError(err, "Restarting multipath after restarting open-iscsi")
}
detectPartitionRetryable := boshretry.NewRetryable(func() (bool, error) {
output, _, _, err := p.cmdRunner.RunCommand("dmsetup", "ls")
if err != nil {
return true, bosherr.WrapError(err, "Shelling out to dmsetup ls")
}
if strings.Contains(output, "No devices found") {
return true, bosherr.Errorf("No devices found")
}
device := strings.TrimPrefix(devicePath, "/dev/mapper/")
lines := strings.Split(strings.Trim(output, "\n"), "\n")
for i := 0; i < len(lines); i++ {
if match, _ := regexp.MatchString("-part1", lines[i]); match {
if strings.Contains(lines[i], device) {
p.logger.Info(p.logTag, "Succeeded in detecting partition %s", devicePath+"-part1")
return false, nil
}
}
}
return true, bosherr.Errorf("Partition %s does not show up", devicePath+"-part1")
})
detectPartitionRetryStrategy := NewPartitionStrategy(detectPartitionRetryable, p.timeService, p.logger)
err := detectPartitionRetryStrategy.Try()
if err != nil {
return err
}
}
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 when getting device size")
}
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) (bool, error) {
existingPartitions, err := p.getPartitions(devicePath)
if err != nil {
return false, err
}
if len(existingPartitions) < len(partitionsToMatch) {
return false, nil
}
remainingDiskSpace, err := p.GetDeviceSizeInBytes(devicePath)
if err != nil {
return false, err
}
for index, partitionToMatch := range partitionsToMatch {
if index == len(partitionsToMatch)-1 {
partitionToMatch.SizeInBytes = remainingDiskSpace
}
existingPartition := existingPartitions[index]
switch {
case existingPartition.Type != partitionToMatch.Type:
return false, nil
case !withinDelta(existingPartition.SizeInBytes, partitionToMatch.SizeInBytes, p.convertFromMbToBytes(deltaSize)):
return false, nil
}
remainingDiskSpace = remainingDiskSpace - partitionToMatch.SizeInBytes
}
return true, nil
}
func (p sfdiskPartitioner) getPartitions(devicePath string) ([]Partition, error) {
stdout, _, _, err := p.cmdRunner.RunCommand("sfdisk", "-d", devicePath)
if err != nil {
return nil, bosherr.WrapError(err, "Shelling out to sfdisk when getting partitions")
}
partitions := []Partition{}
allLines := strings.Split(stdout, "\n")
if len(allLines) < 4 {
return partitions, nil
}
partitionLines := allLines[3 : len(allLines)-1]
for _, partitionLine := range partitionLines {
partitionPath, partitionType := extractPartitionPathAndType(partitionLine)
if partitionType == PartitionTypeGPT {
return nil, ErrGPTPartitionEncountered
}
partition := Partition{Type: partitionType}
if partition.Type != PartitionTypeEmpty {
if strings.Contains(partitionPath, "/dev/mapper/") {
partitionPath = partitionPath[0:len(partitionPath)-1] + "-part1"
}
size, err := p.GetDeviceSizeInBytes(partitionPath)
if err == nil {
partition.SizeInBytes = size
}
}
partitions = append(partitions, partition)
}
return partitions, nil
}
var partitionTypesMap = map[string]PartitionType{
"82": PartitionTypeSwap,
"83": PartitionTypeLinux,
"0": PartitionTypeEmpty,
"ee": PartitionTypeGPT,
}
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
}