forked from mendersoftware/mender
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpartitions.go
231 lines (201 loc) · 6.66 KB
/
partitions.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
// Copyright 2018 Northern.tech AS
//
// 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 main
import (
"os"
"path"
"strings"
"syscall"
"github.com/mendersoftware/log"
"github.com/pkg/errors"
)
var (
RootPartitionDoesNotMatchMount = errors.New("Can not match active partition and any of mounted devices.")
ErrorNoMatchBootPartRootPart = errors.New("No match between boot and root partitions.")
ErrorPartitionNumberNotSet = errors.New("RootfsPartA and RootfsPartB settings are not both set.")
ErrorPartitionNumberSame = errors.New("RootfsPartA and RootfsPartB cannot be set to the same value.")
ErrorPartitionNoMatchActive = errors.New("Active root partition matches neither RootfsPartA nor RootfsPartB.")
)
type partitions struct {
StatCommander
BootEnvReadWriter
rootfsPartA string
rootfsPartB string
active string
inactive string
}
func (p *partitions) GetInactive() (string, error) {
if p.inactive != "" {
log.Debug("Inactive partition: ", p.inactive)
return p.inactive, nil
}
return p.getAndCacheInactivePartition()
}
func (p *partitions) GetActive() (string, error) {
if p.active != "" {
log.Debug("Active partition: ", p.active)
return p.active, nil
}
return p.getAndCacheActivePartition(isMountedRoot, getAllMountedDevices)
}
func (p *partitions) getAndCacheInactivePartition() (string, error) {
if p.rootfsPartA == "" || p.rootfsPartB == "" {
return "", ErrorPartitionNumberNotSet
}
if p.rootfsPartA == p.rootfsPartB {
return "", ErrorPartitionNumberSame
}
active, err := p.GetActive()
if err != nil {
return "", err
}
if active == p.rootfsPartA {
p.inactive = p.rootfsPartB
} else if active == p.rootfsPartB {
p.inactive = p.rootfsPartA
} else {
return "", ErrorPartitionNoMatchActive
}
log.Debugf("Detected inactive partition %s, based on active partition %s", p.inactive, active)
return p.inactive, nil
}
func getRootCandidateFromMount(data []byte) string {
for _, line := range strings.Split(string(data), "\n") {
fields := strings.Split(line, " ")
if len(fields) >= 3 && fields[2] == "/" {
// we just need the first one (in fact there should be ONLY one)
return fields[0]
}
}
return ""
}
func getRootDevice(sc StatCommander) *syscall.Stat_t {
rootStat, err := sc.Stat("/")
if err != nil {
// Seriously??
// Something is *very* wrong.
log.Error("Can not stat root device.")
return nil
}
return rootStat.Sys().(*syscall.Stat_t)
}
func getAllMountedDevices(devDir string) (names []string, err error) {
devFd, err := os.Open(devDir)
if err != nil {
return nil, err
}
defer devFd.Close()
names, err = devFd.Readdirnames(0)
if err != nil {
return nil, err
}
for i := 0; i < len(names); i++ {
names[i] = path.Join(devDir, names[i])
}
return names, nil
}
// There is a lot of system calls here so will be rather hard to test
func isMountedRoot(sc StatCommander, dev string, root *syscall.Stat_t) bool {
// Check if this is a device file and its device ID matches that of the
// root directory.
stat, err := sc.Stat(dev)
if err != nil ||
(stat.Mode()&os.ModeDevice) == 0 ||
stat.Sys().(*syscall.Stat_t).Rdev != root.Dev {
return false
}
return true
}
func getRootFromMountedDevices(sc StatCommander,
rootChecker func(StatCommander, string, *syscall.Stat_t) bool,
devices []string, root *syscall.Stat_t) (string, error) {
for _, device := range devices {
if rootChecker(sc, device, root) {
return device, nil
}
}
return "", RootPartitionDoesNotMatchMount
}
func (p *partitions) getAndCacheActivePartition(rootChecker func(StatCommander, string, *syscall.Stat_t) bool,
getMountedDevices func(string) ([]string, error)) (string, error) {
mountData, err := p.Command("mount").Output()
if err != nil {
return "", err
}
mountCandidate := getRootCandidateFromMount(mountData)
rootDevice := getRootDevice(p)
if rootDevice == nil {
return "", errors.New("Can not find root device")
}
// Fetch active partition from ENV
bootEnvBootPart, err := getBootEnvActivePartition(p.BootEnvReadWriter)
if err != nil {
return "", err
}
// First check if mountCandidate matches rootDevice
if mountCandidate != "" {
if rootChecker(p, mountCandidate, rootDevice) {
p.active = mountCandidate
log.Debugf("Setting active partition from mount candidate: %s", p.active)
return p.active, nil
}
// If mount candidate does not match root device check if we have a match in ENV
if checkBootEnvAndRootPartitionMatch(bootEnvBootPart, mountCandidate) {
p.active = mountCandidate
log.Debug("Setting active partition: ", mountCandidate)
return p.active, nil
}
// If not see if we are lucky somewhere else
}
const devDir string = "/dev"
mountedDevices, err := getMountedDevices(devDir)
if err != nil {
return "", err
}
activePartition, err := getRootFromMountedDevices(p, rootChecker, mountedDevices, rootDevice)
if err != nil {
// If we reach this point, we have not been able to find a match
// based on mounted device.
//
// Fall-back to configuration and environment only!
if checkBootEnvAndRootPartitionMatch(bootEnvBootPart, p.rootfsPartA) {
p.active = p.rootfsPartA
log.Debug("Setting active partition from configuration and environment: ", p.active)
return p.active, nil
}
if checkBootEnvAndRootPartitionMatch(bootEnvBootPart, p.rootfsPartB) {
p.active = p.rootfsPartB
log.Debug("Setting active partition from configuration and environment: ", p.active)
return p.active, nil
}
return "", err
}
if checkBootEnvAndRootPartitionMatch(bootEnvBootPart, activePartition) {
p.active = activePartition
log.Debug("Setting active partition: ", activePartition)
return p.active, nil
}
log.Error("Mounted root '" + activePartition + "' does not match boot environment mender_boot_part: " + bootEnvBootPart)
return "", ErrorNoMatchBootPartRootPart
}
func getBootEnvActivePartition(env BootEnvReadWriter) (string, error) {
bootEnv, err := env.ReadEnv("mender_boot_part")
if err != nil {
return "", errors.Wrapf(err, ErrorNoMatchBootPartRootPart.Error())
}
return bootEnv["mender_boot_part"], nil
}
func checkBootEnvAndRootPartitionMatch(bootPartNum string, rootPart string) bool {
return strings.HasSuffix(rootPart, bootPartNum)
}