-
Notifications
You must be signed in to change notification settings - Fork 601
/
Copy pathexport_test.go
328 lines (287 loc) · 9.06 KB
/
export_test.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
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2017 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package bootloader
import (
"os"
"path/filepath"
. "gopkg.in/check.v1"
"github.com/snapcore/snapd/bootloader/lkenv"
"github.com/snapcore/snapd/bootloader/ubootenv"
"github.com/snapcore/snapd/osutil/disks"
"github.com/snapcore/snapd/osutil/kcmdline"
"github.com/snapcore/snapd/snap"
)
// creates a new Androidboot bootloader object
func NewAndroidBoot(rootdir string) Bootloader {
return newAndroidBoot(rootdir, nil)
}
func MockAndroidBootFile(c *C, rootdir string, mode os.FileMode) {
f := &androidboot{rootdir: rootdir}
err := os.MkdirAll(f.dir(), 0755)
c.Assert(err, IsNil)
err = os.WriteFile(f.configFile(), nil, mode)
c.Assert(err, IsNil)
}
func NewUboot(rootdir string, blOpts *Options) ExtractedRecoveryKernelImageBootloader {
return newUboot(rootdir, blOpts).(ExtractedRecoveryKernelImageBootloader)
}
func MockUbootFiles(c *C, rootdir string, blOpts *Options) {
u := &uboot{rootdir: rootdir}
u.setDefaults()
u.processBlOpts(blOpts)
err := os.MkdirAll(u.dir(), 0755)
c.Assert(err, IsNil)
// ensure that we have a valid uboot.env too
env, err := ubootenv.Create(u.envFile(), 4096, ubootenv.CreateOptions{HeaderFlagByte: true})
c.Assert(err, IsNil)
err = env.Save()
c.Assert(err, IsNil)
}
func NewGrub(rootdir string, opts *Options) RecoveryAwareBootloader {
return newGrub(rootdir, opts).(RecoveryAwareBootloader)
}
func MockGrubFiles(c *C, rootdir string) {
err := os.MkdirAll(filepath.Join(rootdir, "/boot/grub"), 0755)
c.Assert(err, IsNil)
err = os.WriteFile(filepath.Join(rootdir, "/boot/grub/grub.cfg"), nil, 0644)
c.Assert(err, IsNil)
}
func NewLk(rootdir string, opts *Options) ExtractedRecoveryKernelImageBootloader {
if opts == nil {
opts = &Options{
Role: RoleSole,
}
}
return newLk(rootdir, opts).(ExtractedRecoveryKernelImageBootloader)
}
// LkConfigFile returns the primary lk bootloader environment file.
func LkConfigFile(b Bootloader) (string, error) {
lk := b.(*lk)
return lk.envBackstore(primaryStorage)
}
func UbootConfigFile(b Bootloader) string {
u := b.(*uboot)
return u.envFile()
}
func MockLkFiles(c *C, rootdir string, opts *Options) (restore func()) {
var cleanups []func()
if opts == nil {
// default to v1, uc16/uc18 version for test simplicity
opts = &Options{
Role: RoleSole,
}
}
l := &lk{rootdir: rootdir}
l.processOpts(opts)
var version lkenv.Version
switch opts.Role {
case RoleSole:
version = lkenv.V1
case RoleRunMode:
version = lkenv.V2Run
case RoleRecovery:
version = lkenv.V2Recovery
}
// setup some role specific things
if opts.Role == RoleRunMode || opts.Role == RoleRecovery {
// then we need to setup some additional files - namely the kernel
// command line and a mock disk for that
lkBootDisk := &disks.MockDiskMapping{
// mock the partition labels, since these structures won't have
// filesystems, but they will have partition labels
Structure: []disks.Partition{
{
PartitionLabel: "snapbootsel",
PartitionUUID: "snapbootsel-partuuid",
},
{
PartitionLabel: "snapbootselbak",
PartitionUUID: "snapbootselbak-partuuid",
},
{
PartitionLabel: "snaprecoverysel",
PartitionUUID: "snaprecoverysel-partuuid",
},
{
PartitionLabel: "snaprecoveryselbak",
PartitionUUID: "snaprecoveryselbak-partuuid",
},
// for run mode kernel snaps
{
PartitionLabel: "boot_a",
PartitionUUID: "boot-a-partuuid",
},
{
PartitionLabel: "boot_b",
PartitionUUID: "boot-b-partuuid",
},
// for recovery system kernel snaps
{
PartitionLabel: "boot_ra",
PartitionUUID: "boot-ra-partuuid",
},
{
PartitionLabel: "boot_rb",
PartitionUUID: "boot-rb-partuuid",
},
},
DiskHasPartitions: true,
DevNum: "lk-boot-disk-dev-num",
}
m := map[string]*disks.MockDiskMapping{
"lk-boot-disk": lkBootDisk,
}
// mock the disk
r := disks.MockDeviceNameToDiskMapping(m)
cleanups = append(cleanups, r)
// now mock the kernel command line
cmdLine := filepath.Join(c.MkDir(), "cmdline")
os.WriteFile(cmdLine, []byte("snapd_lk_boot_disk=lk-boot-disk"), 0644)
r = kcmdline.MockProcCmdline(cmdLine)
cleanups = append(cleanups, r)
}
// next create empty env file
buf := make([]byte, 4096)
f, err := l.envBackstore(primaryStorage)
c.Assert(err, IsNil)
c.Assert(os.MkdirAll(filepath.Dir(f), 0755), IsNil)
err = os.WriteFile(f, buf, 0660)
c.Assert(err, IsNil)
// now write env in it with correct crc
env := lkenv.NewEnv(f, "", version)
if version == lkenv.V2Recovery {
env.InitializeBootPartitions("boot_ra", "boot_rb")
} else {
env.InitializeBootPartitions("boot_a", "boot_b")
}
err = env.Save()
c.Assert(err, IsNil)
// also make the empty files for the boot_a and boot_b partitions
if opts.Role == RoleRunMode || opts.Role == RoleRecovery {
// for uc20 roles we need to mock the files in /dev/disk/by-partuuid
// and we also need to mock the snapbootselbak file (the snapbootsel
// was created above when we created envFile())
for _, label := range []string{"boot_a", "boot_b", "boot_ra", "boot_rb", "snapbootselbak"} {
disk, err := disks.DiskFromDeviceName("lk-boot-disk")
c.Assert(err, IsNil)
partUUID, err := disk.FindMatchingPartitionUUIDWithPartLabel(label)
c.Assert(err, IsNil)
bootFile := filepath.Join(rootdir, "/dev/disk/by-partuuid", partUUID)
c.Assert(os.MkdirAll(filepath.Dir(bootFile), 0755), IsNil)
c.Assert(os.WriteFile(bootFile, nil, 0755), IsNil)
}
} else {
// for non-uc20 roles just mock the files in /dev/disk/by-partlabel
for _, partName := range []string{"boot_a", "boot_b"} {
mockPart := filepath.Join(rootdir, "/dev/disk/by-partlabel/", partName)
err := os.MkdirAll(filepath.Dir(mockPart), 0755)
c.Assert(err, IsNil)
err = os.WriteFile(mockPart, nil, 0600)
c.Assert(err, IsNil)
}
}
return func() {
for _, r := range cleanups {
r()
}
}
}
func LkRuntimeMode(b Bootloader) bool {
lk := b.(*lk)
return !lk.prepareImageTime
}
func MockAddBootloaderToFind(blConstructor func(string, *Options) Bootloader) (restore func()) {
oldLen := len(bootloaders)
bootloaders = append(bootloaders, blConstructor)
return func() {
bootloaders = bootloaders[:oldLen]
}
}
func NewPiboot(rootdir string, opts *Options) ExtractedRecoveryKernelImageBootloader {
return newPiboot(rootdir, opts).(ExtractedRecoveryKernelImageBootloader)
}
func MockPibootFiles(c *C, rootdir string, blOpts *Options) func() {
oldSeedPartDir := ubuntuSeedDir
ubuntuSeedDir = rootdir
p := &piboot{rootdir: rootdir}
p.setDefaults()
p.processBlOpts(blOpts)
err := os.MkdirAll(p.dir(), 0755)
c.Assert(err, IsNil)
// ensure that we have a valid piboot.conf
env, err := ubootenv.Create(p.envFile(), 4096, ubootenv.CreateOptions{HeaderFlagByte: true})
c.Assert(err, IsNil)
err = env.Save()
c.Assert(err, IsNil)
// Create configuration files expected to come from the gadget
cmdLineFile, err := os.Create(filepath.Join(rootdir, "cmdline.txt"))
c.Assert(err, IsNil)
cmdLineFile.Close()
cfgFile, err := os.Create(filepath.Join(rootdir, "config.txt"))
c.Assert(err, IsNil)
cfgFile.Close()
return func() { ubuntuSeedDir = oldSeedPartDir }
}
func MockRPi4Files(c *C, rootdir string, rpiRevisionCode, eepromTimeStamp []byte) func() {
oldRevCodePath := rpi4RevisionCodesPath
oldEepromTs := rpi4EepromTimeStampPath
rpi4RevisionCodesPath = filepath.Join(rootdir, "linux,revision")
rpi4EepromTimeStampPath = filepath.Join(rootdir, "build-timestamp")
files := []struct {
path string
data []byte
}{
{
path: rpi4RevisionCodesPath,
data: rpiRevisionCode,
},
{
path: rpi4EepromTimeStampPath,
data: eepromTimeStamp,
},
}
for _, file := range files {
if len(file.data) == 0 {
continue
}
fd, err := os.Create(file.path)
c.Assert(err, IsNil)
defer fd.Close()
written, err := fd.Write(file.data)
c.Assert(err, IsNil)
c.Assert(written, Equals, len(file.data))
}
return func() {
rpi4RevisionCodesPath = oldRevCodePath
rpi4EepromTimeStampPath = oldEepromTs
}
}
func PibootConfigFile(b Bootloader) string {
p := b.(*piboot)
return p.envFile()
}
func LayoutKernelAssetsToDir(b Bootloader, snapf snap.Container, dstDir string) error {
p := b.(*piboot)
return p.layoutKernelAssetsToDir(snapf, dstDir)
}
var (
EditionFromDiskConfigAsset = editionFromDiskConfigAsset
EditionFromConfigAsset = editionFromConfigAsset
ConfigAssetFrom = configAssetFrom
StaticCommandLineForGrubAssetEdition = staticCommandLineForGrubAssetEdition
)