-
Notifications
You must be signed in to change notification settings - Fork 164
/
Copy pathzboot.go
548 lines (464 loc) · 14.8 KB
/
zboot.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
// Copyright (c) 2017 Zededa, Inc.
// SPDX-License-Identifier: Apache-2.0
// zboot APIs for IMGA & IMGB
package zboot
import (
"context"
"errors"
"fmt"
"os"
"os/exec"
"strings"
"sync"
"syscall"
"time"
"github.com/lf-edge/edge-containers/pkg/registry"
"github.com/lf-edge/eve/pkg/pillar/base"
"github.com/lf-edge/eve/pkg/pillar/cas"
"github.com/lf-edge/eve/pkg/pillar/types"
"github.com/sirupsen/logrus" // Used for log.Fatal only
)
// MountFlags used in zbootMount calls
type MountFlags uint
const (
// MountFlagRDONLY readOnly mount
MountFlagRDONLY MountFlags = 0x01
casClientType = "containerd"
)
// mutex for zboot/dd APIs
// XXX not bullet proof since this can be invoked by different agents/processes
var zbootMutex *sync.Mutex
func init() {
zbootMutex = new(sync.Mutex)
if zbootMutex == nil {
logrus.Fatal("Mutex Init")
}
}
// Reset routine
// it runs reboot -n -f
func Reset(log *base.LogObject) {
_, err := execWithRetry(log, "zboot", "reset")
if err != nil {
logrus.Fatalf("zboot reset: err %v\n", err)
}
}
// Poweroff routine
// it runs poweroff -f
func Poweroff(log *base.LogObject) {
_, err := execWithRetry(log, "zboot", "poweroff")
if err != nil {
logrus.Fatalf("zboot poweroff: err %v\n", err)
}
}
// If log is nil there is no logging
func execWithRetry(log *base.LogObject, command string, args ...string) ([]byte, error) {
for {
out, done, err := execWithTimeout(log, command, args...)
if err != nil {
return out, err
}
if done {
return out, nil
}
if log != nil {
log.Errorf("Retrying %s %v", command, args)
}
}
}
// If log is nil there is no logging
func execWithTimeout(log *base.LogObject, command string, args ...string) ([]byte, bool, error) {
if log != nil {
log.Functionf("Waiting for zbootMutex.lock for %s %+v\n",
command, args)
}
zbootMutex.Lock()
if log != nil {
log.Functionf("Got zbootMutex.lock. Executing %s %+v\n",
command, args)
}
// initialize context after getting lock
// it uses the current time on initialization step
// Handle slow zboot due to bad disks by waiting for a while
ctx, cancel := context.WithTimeout(context.Background(),
30*time.Second)
defer cancel()
cmd := exec.CommandContext(ctx, command, args...)
out, err := cmd.Output()
zbootMutex.Unlock()
if log != nil {
log.Functionf("Released zbootMutex.lock for %s %+v\n",
command, args)
}
if ctx.Err() == context.DeadlineExceeded {
return nil, false, nil
}
return out, true, err
}
// Cache since it never changes on a running system
// XXX lsblk seems to hang in kernel so avoid calling zboot curpart more
// than once per process.
var currentPartition string
// SetCurpart set current partition to curpart
func SetCurpart(curpart string) {
currentPartition = curpart
}
// partition routines
// GetCurrentPartition determine current active partition
func GetCurrentPartition() string {
if currentPartition != "" {
return currentPartition
}
ret, err := execWithRetry(nil, "zboot", "curpart")
if err != nil {
logrus.Fatalf("zboot curpart: err %v\n", err)
}
partName := string(ret)
partName = strings.TrimSpace(partName)
validatePartitionName(partName)
currentPartition = partName
return partName
}
// GetOtherPartition get the partition that is not currently active
func GetOtherPartition() string {
partName := GetCurrentPartition()
switch partName {
case "IMGA":
partName = "IMGB"
case "IMGB":
partName = "IMGA"
default:
logrus.Fatalf("GetOtherPartition unknown partName %s\n", partName)
}
return partName
}
func validatePartitionName(partName string) {
if partName == "IMGA" || partName == "IMGB" {
return
}
errStr := fmt.Sprintf("invalid partition %s", partName)
logrus.Fatal(errStr)
}
func validatePartitionState(partState string) {
if partState == "active" || partState == "inprogress" ||
partState == "unused" || partState == "updating" {
return
}
errStr := fmt.Sprintf("invalid partition state %s", partState)
logrus.Fatal(errStr)
}
// IsCurrentPartition determine if partName is the currently active partition
func IsCurrentPartition(partName string) bool {
validatePartitionName(partName)
curPartName := GetCurrentPartition()
return curPartName == partName
}
// IsOtherPartition determine if partName is the other, not-currently-active, partition
func IsOtherPartition(partName string) bool {
validatePartitionName(partName)
otherPartName := GetOtherPartition()
return otherPartName == partName
}
// get/set api routines
// GetPartitionState get the state of partition partName
func GetPartitionState(partName string) string {
validatePartitionName(partName)
ret, err := execWithRetry(nil, "zboot", "partstate", partName)
if err != nil {
logrus.Fatalf("zboot partstate %s: err %v\n", partName, err)
}
partState := string(ret)
partState = strings.TrimSpace(partState)
return partState
}
// IsPartitionState determine if partition partName is in the state partState
func IsPartitionState(partName string, partState string) bool {
validatePartitionName(partName)
validatePartitionState(partState)
curPartState := GetPartitionState(partName)
res := curPartState == partState
return res
}
func setPartitionState(log *base.LogObject, partName string, partState string) {
log.Functionf("setPartitionState(%s, %s)\n", partName, partState)
validatePartitionName(partName)
validatePartitionState(partState)
_, err := execWithRetry(log, "zboot", "set_partstate",
partName, partState)
if err != nil {
logrus.Fatalf("zboot set_partstate %s %s: err %v\n",
partName, partState, err)
}
}
// Cache - doesn't change in running system
var partDev = make(map[string]string)
// GetPartitionDevname get the device name for partition partName
func GetPartitionDevname(partName string) string {
validatePartitionName(partName)
dev, ok := partDev[partName]
if ok {
return dev
}
ret, err := execWithRetry(nil, "zboot", "partdev", partName)
if err != nil {
logrus.Fatalf("zboot partdev %s: err %v\n", partName, err)
}
devName := string(ret)
devName = strings.TrimSpace(devName)
partDev[partName] = devName
return devName
}
// set routines
func setPartitionStateActive(log *base.LogObject, partName string) {
setPartitionState(log, partName, "active")
}
func setPartitionStateUnused(log *base.LogObject, partName string) {
setPartitionState(log, partName, "unused")
}
func setPartitionStateUpdating(log *base.LogObject, partName string) {
setPartitionState(log, partName, "updating")
}
// check routines, for current partition
// IsCurrentPartitionStateActive determine if the current partition is active
func IsCurrentPartitionStateActive() bool {
partName := GetCurrentPartition()
return IsPartitionState(partName, "active")
}
// IsCurrentPartitionStateInProgress determine if the current partition state is in progress
func IsCurrentPartitionStateInProgress() bool {
partName := GetCurrentPartition()
return IsPartitionState(partName, "inprogress")
}
// IsCurrentPartitionStateUpdating determine if the current partition state is updating
func IsCurrentPartitionStateUpdating() bool {
partName := GetCurrentPartition()
return IsPartitionState(partName, "updating")
}
// check routines, for other partition
// IsOtherPartitionStateActive determine if the other partition state is active
func IsOtherPartitionStateActive() bool {
partName := GetOtherPartition()
return IsPartitionState(partName, "active")
}
// IsOtherPartitionStateInProgress determine if the other partition state is in progress
func IsOtherPartitionStateInProgress() bool {
partName := GetOtherPartition()
return IsPartitionState(partName, "inprogress")
}
// IsOtherPartitionStateUnused determine if the other partition state is in unused
func IsOtherPartitionStateUnused() bool {
partName := GetOtherPartition()
return IsPartitionState(partName, "unused")
}
// IsOtherPartitionStateUpdating determine if the other partition state is updating
func IsOtherPartitionStateUpdating() bool {
partName := GetOtherPartition()
return IsPartitionState(partName, "updating")
}
func setCurrentPartitionStateActive(log *base.LogObject) {
partName := GetCurrentPartition()
setPartitionState(log, partName, "active")
}
func setCurrentPartitionStateUpdating(log *base.LogObject) {
partName := GetCurrentPartition()
setPartitionState(log, partName, "updating")
}
func setCurrentPartitionStateUnused(log *base.LogObject) {
partName := GetCurrentPartition()
setPartitionState(log, partName, "unused")
}
// set routines, for other partition
func setOtherPartitionStateActive(log *base.LogObject) {
partName := GetOtherPartition()
setPartitionState(log, partName, "active")
}
// SetOtherPartitionStateUpdating set the other partition state to updating
func SetOtherPartitionStateUpdating(log *base.LogObject) {
partName := GetOtherPartition()
setPartitionState(log, partName, "updating")
}
// SetOtherPartitionStateUnused set the other partition state to unused
func SetOtherPartitionStateUnused(log *base.LogObject) {
partName := GetOtherPartition()
setPartitionState(log, partName, "unused")
}
// GetCurrentPartitionDevName get the device name for the current partition
func GetCurrentPartitionDevName() string {
partName := GetCurrentPartition()
return GetPartitionDevname(partName)
}
// GetOtherPartitionDevName get the device name for the other partition
func GetOtherPartitionDevName() string {
partName := GetOtherPartition()
return GetPartitionDevname(partName)
}
// WriteToPartition write the image to partition partName
func WriteToPartition(log *base.LogObject, image string, partName string) error {
var (
casClient cas.CAS
err error
)
if !IsOtherPartition(partName) {
errStr := fmt.Sprintf("not other partition %s", partName)
log.Errorf("WriteToPartition failed %s\n", errStr)
return errors.New(errStr)
}
devName := GetPartitionDevname(partName)
if devName == "" {
errStr := fmt.Sprintf("null devname for partition %s", partName)
log.Errorf("WriteToPartition failed %s\n", errStr)
return errors.New(errStr)
}
log.Functionf("WriteToPartition %s, %s: %v\n", partName, devName, image)
// use the edge-containers library to extract the data we need
puller := registry.Puller{
Image: image,
}
if casClient, err = cas.NewCAS(casClientType); err != nil {
err = fmt.Errorf("Run: exception while initializing CAS client: %s", err.Error())
log.Fatal(err)
}
defer casClient.CloseClient()
ctrdCtx, done := casClient.CtrNewUserServicesCtx()
defer done()
resolver, err := casClient.Resolver(ctrdCtx)
if err != nil {
errStr := fmt.Sprintf("error getting CAS resolver: %v", err)
log.Error(errStr)
return errors.New(errStr)
}
// Make sure we have nothing mounted on the target
for {
if err := syscall.Unmount(devName, 0); err != nil {
break
}
log.Warnf("Successfully umounted %s", devName)
}
// create a writer for the file where we want
// Avoid holding the lock since this can take a long time.
f, err := os.OpenFile(devName,
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
errStr := fmt.Sprintf("error writing to partition device at %s: %v", devName, err)
log.Error(errStr)
return errors.New(errStr)
}
defer f.Close()
if _, _, err := puller.Pull(®istry.FilesTarget{Root: f, AcceptHash: true}, 0, false, os.Stderr, resolver); err != nil {
errStr := fmt.Sprintf("error pulling %s from containerd: %v", image, err)
log.Error(errStr)
return errors.New(errStr)
}
return nil
}
// MarkCurrentPartitionStateActive transition current from inprogress to active, and other from active/inprogress
// to unused
func MarkCurrentPartitionStateActive(log *base.LogObject) error {
curPart := GetCurrentPartition()
otherPart := GetOtherPartition()
log.Functionf("Check current partition %s, for inProgress state\n", curPart)
if ret := IsCurrentPartitionStateInProgress(); ret == false {
errStr := fmt.Sprintf("Current partition %s, is not inProgress",
curPart)
return errors.New(errStr)
}
log.Functionf("Mark the current partition %s, active\n", curPart)
setCurrentPartitionStateActive(log)
log.Functionf("Check other partition %s for active state or inprogress\n",
otherPart)
state := GetPartitionState(otherPart)
switch state {
case "active":
// Normal case
case "inprogress":
// Activated what was already on the other partition
default:
errStr := fmt.Sprintf("Other partition %s, is %s not active/inprogress",
otherPart, state)
return errors.New(errStr)
}
log.Functionf("Mark other partition %s, unused\n", otherPart)
SetOtherPartitionStateUnused(log)
return nil
}
// XXX known pathnames for the version file and the zededa-tools container
const (
otherPartVersionFile = "/etc/eve-release"
)
// GetShortVersion get short form of version for partName
func GetShortVersion(log *base.LogObject, partName string) (string, error) {
ver, err := getVersion(log, partName, types.EveVersionFile)
return ver, err
}
// GetLongVersion get long version of partition part
// XXX add longversion once we have a filename above
func GetLongVersion(part string) string {
return ""
}
// XXX explore a loopback mount to be able to read version
// from a downloaded image file
func getVersion(log *base.LogObject, part string, verFilename string) (string, error) {
validatePartitionName(part)
if part == GetCurrentPartition() {
filename := verFilename
version, err := os.ReadFile(filename)
if err != nil {
log.Errorln(err)
return "", err
}
versionStr := string(version)
versionStr = strings.TrimSpace(versionStr)
log.Functionf("%s, readCurVersion %s\n", part, versionStr)
return versionStr, nil
} else {
verFilename = otherPartVersionFile
devname := GetPartitionDevname(part)
target, err := os.MkdirTemp("/run/baseosmgr", "tmpmnt")
if err != nil {
log.Errorln(err)
return "", err
}
defer func() {
log.Noticef("Remove(%s)", target)
if err := os.Remove(target); err != nil {
log.Errorf("Remove(%s) failed %s", target, err)
}
}()
// Mount failure is ok; might not have a filesystem in the
// other partition
// XXX hardcoded file system type squashfs
mountFlags := MountFlagRDONLY
err = zbootMount(devname, target, "squashfs", mountFlags, "")
if err != nil {
errStr := fmt.Sprintf("Mount of %s failed: %s", devname, err)
log.Errorln(errStr)
return "", errors.New(errStr)
}
log.Noticef("Mounted %s on %s", devname, target)
defer func() {
log.Noticef("Unmount(%s)", target)
for i := 0; i < 10; i++ {
err := zbootUnmount(target, i > 0)
if err != nil {
errStr := fmt.Sprintf("Unmount of %s %d failed: %s",
target, i, err)
logrus.Error(errStr)
time.Sleep(time.Second)
} else {
log.Noticef("Unmounted %s", target)
break
}
}
}()
filename := fmt.Sprintf("%s/%s",
target, verFilename)
version, err := os.ReadFile(filename)
if err != nil {
log.Warn(err)
return "", err
}
versionStr := string(version)
versionStr = strings.TrimSpace(versionStr)
log.Functionf("%s, readOtherVersion %s\n", part, versionStr)
return versionStr, nil
}
}