/
perform.go
412 lines (355 loc) · 11.6 KB
/
perform.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
// Copyright 2017 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 torcx
import (
"archive/tar"
"bufio"
"compress/gzip"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/coreos/torcx/internal/third_party/docker/pkg/loopback"
pkgtar "github.com/coreos/torcx/pkg/tar"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"golang.org/x/sys/unix"
)
var (
// ErrUnknownOSVersionID is the error returned on generic os-release parsing errors
ErrUnknownOSVersionID = errors.New(`unable to parse "VERSION_ID" from os-release`)
)
// ApplyProfile is called at boot-time to apply the configured profile
// system-wide. Apply operation is split in three phases:
// * unpack: all images are unpacked to their own dedicated path under UnpackDir
// * propagate: executable assets are propagated into the system;
// this includes symlinking binaries into BinDir and installing systemd
// transient units.
// * seal: system state is frozen, profile and metadata written to RunDir
func ApplyProfile(applyCfg *ApplyConfig) error {
var err error
if applyCfg == nil {
return errors.New("missing apply configuration")
}
err = setupPaths(applyCfg)
if err != nil {
return errors.Wrap(err, "profile setup")
}
images, err := mergeProfiles(applyCfg)
if err != nil {
return err
}
if len(images) > 0 {
if err := applyImages(applyCfg, images); err != nil {
return err
}
}
runProfile := ProfileManifestV0JSON{
Kind: ProfileManifestV0K,
Value: ImagesToJSONV0(images),
}
rpp, err := os.Create(applyCfg.RunProfile())
if err != nil {
return err
}
defer rpp.Close()
bufwr := bufio.NewWriter(rpp)
defer bufwr.Flush()
enc := json.NewEncoder(bufwr)
enc.SetIndent("", " ")
if err := enc.Encode(runProfile); err != nil {
return errors.Wrapf(err, "writing %q", applyCfg.RunProfile())
}
err = os.Chmod(applyCfg.RunProfile(), 0444)
if err != nil {
return err
}
logrus.WithFields(logrus.Fields{
"upper profile": applyCfg.UpperProfile,
"sealed profile": applyCfg.RunProfile(),
}).Debug("profile applied")
return nil
}
// applyImages unpacks and propagates assets from a list of images.
func applyImages(applyCfg *ApplyConfig, images []Image) error {
if applyCfg == nil {
return errors.New("missing apply configuration")
}
storeCache, err := NewStoreCache(applyCfg.StorePaths)
if err != nil {
return err
}
// Unpack all images, continuing on error
failedImages := []Image{}
for _, im := range images {
// Some log fields we keep using
logFields := logrus.Fields{
"image": im.Name,
"reference": im.Reference,
}
archive, err := storeCache.ArchiveFor(im)
if err != nil {
logrus.WithFields(logFields).Error(err)
failedImages = append(failedImages, im)
continue
}
var imageRoot string
switch archive.Format {
case ArchiveFormatTgz:
imageRoot, err = unpackTgz(applyCfg, archive.Filepath, im.Name)
case ArchiveFormatSquashfs:
imageRoot, err = mountSquashfs(applyCfg, archive.Filepath, im.Name)
default:
err = fmt.Errorf("unrecognized format for archive: %q", archive)
}
if err != nil {
failedImages = append(failedImages, im)
logrus.WithFields(logFields).Error("failed to unpack: ", err)
continue
}
logFields["path"] = imageRoot
logrus.WithFields(logFields).Debug("image unpacked")
assets, err := retrieveAssets(applyCfg, imageRoot)
if err != nil {
failedImages = append(failedImages, im)
logrus.WithFields(logFields).Error("failed retrieving assets from image: ", err)
continue
}
if len(assets.Binaries) > 0 {
if err := propagateBins(applyCfg, imageRoot, assets.Binaries); err != nil {
failedImages = append(failedImages, im)
logrus.WithFields(logFields).WithField("assets", assets.Binaries).Error("failed to propagate binaries: ", err)
continue
}
logrus.WithFields(logFields).WithField("assets", assets.Binaries).Debug("binaries propagated")
}
if len(assets.Network) > 0 {
if err := propagateNetworkdUnits(applyCfg, imageRoot, assets.Network); err != nil {
failedImages = append(failedImages, im)
logrus.WithFields(logFields).WithField("assets", assets.Network).Error("failed to propagate networkd units: ", err)
continue
}
logrus.WithFields(logFields).WithField("assets", assets.Network).Debug("networkd units propagated")
}
if len(assets.Units) > 0 {
if err := propagateSystemdUnits(applyCfg, imageRoot, assets.Units); err != nil {
failedImages = append(failedImages, im)
logrus.WithFields(logFields).WithField("assets", assets.Units).Error("failed to propagate systemd units: ", err)
continue
}
logrus.WithFields(logFields).WithField("assets", assets.Units).Debug("systemd units propagated")
}
if len(assets.Sysusers) > 0 {
if err := propagateSysusersUnits(applyCfg, imageRoot, assets.Sysusers); err != nil {
failedImages = append(failedImages, im)
logrus.WithFields(logFields).WithField("assets", assets.Sysusers).Error("failed to propagate sysusers: ", err)
continue
}
logrus.WithFields(logFields).WithField("assets", assets.Sysusers).Debug("sysusers propagated")
}
if len(assets.Tmpfiles) > 0 {
if err := propagateTmpfilesUnits(applyCfg, imageRoot, assets.Tmpfiles); err != nil {
failedImages = append(failedImages, im)
logrus.WithFields(logFields).WithField("assets", assets.Units).Error("failed to propagate tmpfiles: ", err)
continue
}
logrus.WithFields(logFields).WithField("assets", assets.Units).Debug("tmpfiles propagated")
}
if len(assets.UdevRules) > 0 {
if err := propagateUdevRules(applyCfg, imageRoot, assets.UdevRules); err != nil {
failedImages = append(failedImages, im)
logrus.WithFields(logFields).WithField("assets", assets.UdevRules).Error("failed to propagate udev rules: ", err)
continue
}
logrus.WithFields(logFields).WithField("assets", assets.UdevRules).Debug("udev rules propagated")
}
}
if len(failedImages) > 0 {
return fmt.Errorf("failed to install %d images", len(failedImages))
}
return nil
}
// SealSystemState is a one-time-op which seals the current state of the system,
// after a torcx profile has been applied to it.
func SealSystemState(applyCfg *ApplyConfig) error {
if applyCfg == nil {
return errors.New("missing apply configuration")
}
dirname := filepath.Dir(SealPath)
if _, err := os.Stat(dirname); err != nil && os.IsNotExist(err) {
if err := os.MkdirAll(dirname, 0755); err != nil {
return err
}
}
fp, err := os.Create(SealPath)
if err != nil {
return err
}
defer fp.Close()
content := []string{
fmt.Sprintf("%s=%q", SealLowerProfiles, strings.Join(applyCfg.LowerProfiles, ":")),
fmt.Sprintf("%s=%q", SealUpperProfile, applyCfg.UpperProfile),
fmt.Sprintf("%s=%q", SealRunProfilePath, applyCfg.RunProfile()),
fmt.Sprintf("%s=%q", SealBindir, applyCfg.RunBinDir()),
fmt.Sprintf("%s=%q", SealUnpackdir, applyCfg.RunUnpackDir()),
}
for _, line := range content {
_, err = fp.WriteString(line + "\n")
if err != nil {
return errors.Wrap(err, "writing seal content")
}
}
// Remount the unpackdir RO
if err := unix.Mount(applyCfg.RunUnpackDir(), applyCfg.RunUnpackDir(),
"", unix.MS_REMOUNT|unix.MS_RDONLY, ""); err != nil {
return errors.Wrap(err, "failed to remount read-only")
}
logrus.WithFields(logrus.Fields{
"path": SealPath,
"content": content,
}).Debug("system state sealed")
return nil
}
func setupPaths(applyCfg *ApplyConfig) error {
if applyCfg == nil {
return errors.New("missing apply configuration")
}
paths := []string{
// RunDir is the first path created, signaling that torcx run
applyCfg.RunDir,
applyCfg.BaseDir,
applyCfg.ConfDir,
applyCfg.RunBinDir(),
applyCfg.RunUnpackDir(),
applyCfg.UserProfileDir(),
}
for _, d := range paths {
if _, err := os.Stat(d); err != nil && os.IsNotExist(err) {
if err := os.MkdirAll(d, 0755); err != nil {
return err
}
}
}
// Now, mount a tmpfs directory to the unpack directory.
// We need to do this because "/run" is typically marked "noexec".
if err := unix.Mount("none", applyCfg.RunUnpackDir(), "tmpfs", 0, ""); err != nil {
return errors.Wrap(err, "failed to mount unpack dir")
}
// Default tmpfs permissions are 1777, which can trip up path auditing
if err := os.Chmod(applyCfg.RunUnpackDir(), 0755); err != nil {
return errors.Wrap(err, "failed to chmod unpack dir")
}
logrus.WithField("target", applyCfg.RunUnpackDir()).Debug("mounted tmpfs")
return nil
}
// unpackTgz renders a tgz rootfs, returning the target top directory.
func unpackTgz(applyCfg *ApplyConfig, tgzPath, imageName string) (string, error) {
if applyCfg == nil {
return "", errors.New("missing apply configuration")
}
if tgzPath == "" || imageName == "" {
return "", errors.New("missing unpack source")
}
topDir := filepath.Join(applyCfg.RunUnpackDir(), imageName)
if _, err := os.Stat(topDir); err != nil && os.IsNotExist(err) {
if err := os.MkdirAll(topDir, 0755); err != nil {
return "", err
}
}
fp, err := os.Open(tgzPath)
if err != nil {
return "", errors.Wrapf(err, "opening %q", tgzPath)
}
defer fp.Close()
gr, err := gzip.NewReader(fp)
if err != nil {
return "", err
}
defer gr.Close()
tr := tar.NewReader(gr)
untarCfg := pkgtar.ExtractCfg{}.Default()
untarCfg.XattrPrivileged = true
err = pkgtar.ChrootUntar(tr, topDir, untarCfg)
if err != nil {
return "", errors.Wrapf(err, "unpacking %q", tgzPath)
}
return topDir, nil
}
// mountSquashfs mounts a squashfs rootfs, returning the mounted directory.
func mountSquashfs(applyCfg *ApplyConfig, archivePath, imageName string) (string, error) {
if applyCfg == nil {
return "", errors.New("missing apply configuration")
}
if archivePath == "" || imageName == "" {
return "", errors.New("missing unpack source")
}
topDir := filepath.Join(applyCfg.RunUnpackDir(), imageName)
if _, err := os.Stat(topDir); err != nil && os.IsNotExist(err) {
if err := os.MkdirAll(topDir, 0755); err != nil {
return "", err
}
}
loopDev, err := loopback.AttachLoopDevice(archivePath)
if err != nil {
return "", err
}
defer loopDev.Close()
if err := unix.Mount(loopDev.Name(), topDir, "squashfs", unix.MS_RDONLY, ""); err != nil {
return "", err
}
return topDir, nil
}
// CurrentOsVersionID parses an os-release file to extract the VERSION_ID.
//
// For more details about the expect format of the os-release file, see
// https://www.freedesktop.org/software/systemd/man/os-release.html
func CurrentOsVersionID(path string) (string, error) {
if path == "" {
path = VendorOsReleasePath("/usr")
}
fp, err := os.Open(path)
if err != nil {
return "", errors.Wrapf(err, "failed to open %q", path)
}
defer fp.Close()
return parseOsVersionID(fp)
}
// parseOsVersionID is the parser for os-release.
func parseOsVersionID(rd io.Reader) (string, error) {
ver := ""
sc := bufio.NewScanner(rd)
for sc.Scan() {
line := sc.Text()
if line == "" {
continue
}
parts := strings.SplitN(line, "=", 2)
if len(parts) != 2 {
continue
}
if parts[0] == "VERSION_ID" {
ver = parts[1]
break
}
}
if sc.Err() != nil {
return "", errors.Wrap(sc.Err(), "failed to parse os-release file")
}
if ver == "" {
return "", ErrUnknownOSVersionID
}
return ver, nil
}