-
Notifications
You must be signed in to change notification settings - Fork 115
/
ephemeral_device_partitioner.go
108 lines (85 loc) · 3.02 KB
/
ephemeral_device_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
package disk
import (
"strings"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
boshsys "github.com/cloudfoundry/bosh-utils/system"
)
type EphemeralDevicePartitioner struct {
partedPartitioner Partitioner
logger boshlog.Logger
logTag string
cmdRunner boshsys.CmdRunner
}
type Settings struct {
AgentID string `json:"agent_id"`
}
func NewEphemeralDevicePartitioner(
partedPartitioner Partitioner,
logger boshlog.Logger,
cmdRunner boshsys.CmdRunner,
) *EphemeralDevicePartitioner {
return &EphemeralDevicePartitioner{
partedPartitioner: partedPartitioner,
logger: logger,
logTag: "EphemeralDevicePartitioner",
cmdRunner: cmdRunner,
}
}
func (p *EphemeralDevicePartitioner) Partition(devicePath string, partitions []Partition) error {
existingPartitions, deviceFullSizeInBytes, err := p.partedPartitioner.GetPartitions(devicePath)
if err != nil {
return bosherr.WrapErrorf(err, "Getting existing partitions of `%s'", devicePath)
}
if p.matchPartitionNames(existingPartitions, partitions, deviceFullSizeInBytes) {
p.logger.Info(p.logTag, "%s already partitioned as expected, skipping", devicePath)
return nil
}
err = p.RemovePartitions(existingPartitions, devicePath)
if err != nil {
return bosherr.WrapErrorf(err, "Removing existing partitions of `%s'", devicePath)
}
err = p.ensureGPTPartition(devicePath)
if err != nil {
return bosherr.WrapErrorf(err, "Ensuring GPT table of `%s'", devicePath)
}
return p.partedPartitioner.Partition(devicePath, partitions)
}
func (p *EphemeralDevicePartitioner) GetDeviceSizeInBytes(devicePath string) (uint64, error) {
return p.partedPartitioner.GetDeviceSizeInBytes(devicePath)
}
func (p *EphemeralDevicePartitioner) GetPartitions(devicePath string) (partitions []ExistingPartition, deviceFullSizeInBytes uint64, err error) {
return p.partedPartitioner.GetPartitions(devicePath)
}
func (p *EphemeralDevicePartitioner) matchPartitionNames(existingPartitions []ExistingPartition, desiredPartitions []Partition, deviceSizeInBytes uint64) bool {
if len(existingPartitions) < len(desiredPartitions) {
return false
}
for index, partition := range desiredPartitions {
existingPartition := existingPartitions[index]
if !strings.HasPrefix(existingPartition.Name, partition.NamePrefix) {
return false
}
}
return true
}
func (p EphemeralDevicePartitioner) RemovePartitions(partitions []ExistingPartition, devicePath string) error {
return p.partedPartitioner.RemovePartitions(partitions, devicePath)
}
func (p EphemeralDevicePartitioner) ensureGPTPartition(devicePath string) (err error) {
stdout, _, _, err := p.cmdRunner.RunCommand("parted", "-m", devicePath, "unit", "B", "print")
if !strings.Contains(stdout, "gpt") {
p.logger.Debug(p.logTag, "Creating gpt table")
stdout, _, _, err = p.cmdRunner.RunCommand(
"parted",
"-s",
devicePath,
"mklabel",
"gpt",
)
if err != nil {
return bosherr.WrapErrorf(err, "Parted making label")
}
}
return nil
}