-
Notifications
You must be signed in to change notification settings - Fork 244
/
disk.go
134 lines (119 loc) · 4.01 KB
/
disk.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
// Copyright 2016 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package types
import (
"github.com/coreos/ignition/v2/config/shared/errors"
"github.com/coreos/vcontext/path"
"github.com/coreos/vcontext/report"
)
func (d Disk) Key() string {
return d.Device
}
func (n Disk) Validate(c path.ContextPath) (r report.Report) {
if len(n.Device) == 0 {
r.AddOnError(c.Append("device"), errors.ErrDiskDeviceRequired)
return
}
r.AddOnError(c.Append("device"), validatePath(n.Device))
if collides, p := n.partitionNumbersCollide(); collides {
r.AddOnError(c.Append("partitions", p), errors.ErrPartitionNumbersCollide)
}
if overlaps, p := n.partitionsOverlap(); overlaps {
r.AddOnError(c.Append("partitions", p), errors.ErrPartitionsOverlap)
}
if n.partitionsMixZeroesAndNonexistence() {
r.AddOnError(c.Append("partitions"), errors.ErrZeroesWithShouldNotExist)
}
if collides, p := n.partitionLabelsCollide(); collides {
r.AddOnError(c.Append("partitions", p), errors.ErrDuplicateLabels)
}
return
}
// partitionNumbersCollide returns true if partition numbers in n.Partitions are not unique. It also returns the
// index of the colliding partition
func (n Disk) partitionNumbersCollide() (bool, int) {
m := map[int][]int{} // from partition number to index into array
for i, p := range n.Partitions {
if p.Number != 0 {
// a number of 0 means next available number, multiple devices can specify this
m[p.Number] = append(m[p.Number], i)
}
}
for _, n := range m {
if len(n) > 1 {
// TODO(vc): return information describing the collision for logging
return true, n[1]
}
}
return false, 0
}
func (d Disk) partitionLabelsCollide() (bool, int) {
m := map[string]struct{}{}
for i, p := range d.Partitions {
if p.Label != nil {
// a number of 0 means next available number, multiple devices can specify this
if _, exists := m[*p.Label]; exists {
return true, i
}
m[*p.Label] = struct{}{}
}
}
return false, 0
}
// end returns the last sector of a partition. Only used by partitionsOverlap. Requires non-nil Start and Size.
func (p Partition) end() int {
if *p.SizeMiB == 0 {
// a size of 0 means "fill available", just return the start as the end for those.
return *p.StartMiB
}
return *p.StartMiB + *p.SizeMiB - 1
}
// partitionsOverlap returns true if any explicitly dimensioned partitions overlap. It also returns the index of
// the overlapping partition
func (n Disk) partitionsOverlap() (bool, int) {
for _, p := range n.Partitions {
// Starts of 0 are placed by sgdisk into the "largest available block" at that time.
// We aren't going to check those for overlap since we don't have the disk geometry.
if p.StartMiB == nil || p.SizeMiB == nil || *p.StartMiB == 0 {
continue
}
for i, o := range n.Partitions {
if o.StartMiB == nil || o.SizeMiB == nil || p == o || *o.StartMiB == 0 {
continue
}
// is p.StartMiB within o?
if *p.StartMiB >= *o.StartMiB && *p.StartMiB <= o.end() {
return true, i
}
// is p.end() within o?
if p.end() >= *o.StartMiB && p.end() <= o.end() {
return true, i
}
// do p.StartMiB and p.end() straddle o?
if *p.StartMiB < *o.StartMiB && p.end() > o.end() {
return true, i
}
}
}
return false, 0
}
func (n Disk) partitionsMixZeroesAndNonexistence() bool {
hasZero := false
hasShouldNotExist := false
for _, p := range n.Partitions {
hasShouldNotExist = hasShouldNotExist || (p.ShouldExist != nil && !*p.ShouldExist)
hasZero = hasZero || (p.Number == 0)
}
return hasZero && hasShouldNotExist
}