-
Notifications
You must be signed in to change notification settings - Fork 206
/
initContainer.go
601 lines (488 loc) · 16.3 KB
/
initContainer.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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
/*
* Copyright © 2019 – 2020 Red Hat 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 cmd
import (
"errors"
"fmt"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"strings"
"github.com/containers/toolbox/pkg/shell"
"github.com/containers/toolbox/pkg/utils"
"github.com/fsnotify/fsnotify"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var (
initContainerFlags struct {
home string
homeLink bool
mediaLink bool
mntLink bool
monitorHost bool
shell string
uid int
user string
}
initContainerMounts = []struct {
containerPath string
source string
flags string
}{
{"/etc/machine-id", "/run/host/etc/machine-id", "ro"},
{"/run/libvirt", "/run/host/run/libvirt", ""},
{"/run/systemd/journal", "/run/host/run/systemd/journal", ""},
{"/run/udev/data", "/run/host/run/udev/data", ""},
{"/tmp", "/run/host/tmp", "rslave"},
{"/var/lib/flatpak", "/run/host/var/lib/flatpak", "ro"},
{"/var/lib/libvirt", "/run/host/var/lib/libvirt", ""},
{"/var/log/journal", "/run/host/var/log/journal", "ro"},
{"/var/mnt", "/run/host/var/mnt", "rslave"},
}
)
var initContainerCmd = &cobra.Command{
Use: "init-container",
Short: "Initialize a running container",
Hidden: true,
RunE: initContainer,
}
func init() {
flags := initContainerCmd.Flags()
flags.StringVar(&initContainerFlags.home,
"home",
"",
"Create a user inside the toolbox container whose login directory is HOME.")
initContainerCmd.MarkFlagRequired("home")
flags.BoolVar(&initContainerFlags.homeLink,
"home-link",
false,
"Make /home a symbolic link to /var/home.")
flags.BoolVar(&initContainerFlags.mediaLink,
"media-link",
false,
"Make /media a symbolic link to /run/media.")
flags.BoolVar(&initContainerFlags.mntLink, "mnt-link", false, "Make /mnt a symbolic link to /var/mnt.")
flags.BoolVar(&initContainerFlags.monitorHost,
"monitor-host",
false,
"Ensure that certain configuration files inside the toolbox container are in sync with the host.")
flags.StringVar(&initContainerFlags.shell,
"shell",
"",
"Create a user inside the toolbox container whose login shell is SHELL.")
initContainerCmd.MarkFlagRequired("shell")
flags.IntVar(&initContainerFlags.uid,
"uid",
0,
"Create a user inside the toolbox container whose numerical user ID is UID.")
initContainerCmd.MarkFlagRequired("uid")
flags.StringVar(&initContainerFlags.user,
"user",
"",
"Create a user inside the toolbox container whose login name is USER.")
initContainerCmd.MarkFlagRequired("user")
initContainerCmd.SetHelpFunc(initContainerHelp)
rootCmd.AddCommand(initContainerCmd)
}
func initContainer(cmd *cobra.Command, args []string) error {
if !utils.IsInsideContainer() {
var builder strings.Builder
fmt.Fprintf(&builder, "the 'init-container' command can only be used inside containers\n")
fmt.Fprintf(&builder, "Run '%s --help' for usage.", executableBase)
errMsg := builder.String()
return errors.New(errMsg)
}
runtimeDirectory := os.Getenv("XDG_RUNTIME_DIR")
if runtimeDirectory == "" {
logrus.Debug("XDG_RUNTIME_DIR is unset")
runtimeDirectory = fmt.Sprintf("/run/user/%d", initContainerFlags.uid)
os.Setenv("XDG_RUNTIME_DIR", runtimeDirectory)
logrus.Debugf("XDG_RUNTIME_DIR set to %s", runtimeDirectory)
}
logrus.Debug("Creating /run/.toolboxenv")
toolboxEnvFile, err := os.Create("/run/.toolboxenv")
if err != nil {
return errors.New("failed to create /run/.toolboxenv")
}
defer toolboxEnvFile.Close()
if initContainerFlags.monitorHost {
logrus.Debug("Monitoring host")
if utils.PathExists("/run/host/etc") {
logrus.Debug("Path /run/host/etc exists")
if _, err := os.Readlink("/etc/host.conf"); err != nil {
if err := redirectPath("/etc/host.conf",
"/run/host/etc/host.conf",
false); err != nil {
return err
}
}
if _, err := os.Readlink("/etc/hosts"); err != nil {
if err := redirectPath("/etc/hosts",
"/run/host/etc/hosts",
false); err != nil {
return err
}
}
if localtimeTarget, err := os.Readlink("/etc/localtime"); err != nil ||
localtimeTarget != "/run/host/etc/localtime" {
if err := redirectPath("/etc/localtime",
"/run/host/etc/localtime",
false); err != nil {
return err
}
}
if err := updateTimeZoneFromLocalTime(); err != nil {
return err
}
if _, err := os.Readlink("/etc/resolv.conf"); err != nil {
if err := redirectPath("/etc/resolv.conf",
"/run/host/etc/resolv.conf",
false); err != nil {
return err
}
}
for _, mount := range initContainerMounts {
if err := mountBind(mount.containerPath, mount.source, mount.flags); err != nil {
return err
}
}
if utils.PathExists("/sys/fs/selinux") {
if err := mountBind("/sys/fs/selinux", "/usr/share/empty", ""); err != nil {
return err
}
}
}
}
if initContainerFlags.mediaLink {
if _, err := os.Readlink("/media"); err != nil {
if err = redirectPath("/media", "/run/media", true); err != nil {
return err
}
}
}
if initContainerFlags.mntLink {
if _, err := os.Readlink("/mnt"); err != nil {
if err := redirectPath("/mnt", "/var/mnt", true); err != nil {
return err
}
}
}
if _, err := user.Lookup(initContainerFlags.user); err != nil {
if err := configureUsers(initContainerFlags.uid,
initContainerFlags.user,
initContainerFlags.home,
initContainerFlags.shell,
initContainerFlags.homeLink,
false); err != nil {
return err
}
} else {
if err := configureUsers(initContainerFlags.uid,
initContainerFlags.user,
initContainerFlags.home,
initContainerFlags.shell,
initContainerFlags.homeLink,
true); err != nil {
return err
}
}
if utils.PathExists("/etc/krb5.conf.d") && !utils.PathExists("/etc/krb5.conf.d/kcm_default_ccache") {
logrus.Debug("Setting KCM as the default Kerberos credential cache")
kcmConfigString := `# Written by Toolbox
# https://github.com/containers/toolbox
#
# # To disable the KCM credential cache, comment out the following lines.
[libdefaults]
default_ccache_name = KCM:
`
kcmConfigBytes := []byte(kcmConfigString)
if err := ioutil.WriteFile("/etc/krb5.conf.d/kcm_default_ccache",
kcmConfigBytes,
0644); err != nil {
return errors.New("failed to set KCM as the defult Kerberos credential cache")
}
}
logrus.Debug("Setting up watches for file system events")
watcherForHost, err := fsnotify.NewWatcher()
if err != nil {
return err
}
defer watcherForHost.Close()
if err := watcherForHost.Add("/run/host/etc"); err != nil {
return err
}
logrus.Debug("Finished initializing container")
toolboxRuntimeDirectory := runtimeDirectory + "/toolbox"
logrus.Debugf("Creating runtime directory %s", toolboxRuntimeDirectory)
if err := os.MkdirAll(toolboxRuntimeDirectory, 0700); err != nil {
return fmt.Errorf("failed to create runtime directory %s", toolboxRuntimeDirectory)
}
if err := os.Chown(toolboxRuntimeDirectory, initContainerFlags.uid, initContainerFlags.uid); err != nil {
return fmt.Errorf("failed to change ownership of the runtime directory %s",
toolboxRuntimeDirectory)
}
pid := os.Getpid()
initializedStamp := fmt.Sprintf("%s/container-initialized-%d", toolboxRuntimeDirectory, pid)
logrus.Debugf("Creating initialization stamp %s", initializedStamp)
initializedStampFile, err := os.Create(initializedStamp)
if err != nil {
return errors.New("failed to create initialization stamp")
}
defer initializedStampFile.Close()
if err := initializedStampFile.Chown(initContainerFlags.uid, initContainerFlags.uid); err != nil {
return errors.New("failed to change ownership of initialization stamp")
}
logrus.Debug("Listening to file system events")
for {
select {
case event := <-watcherForHost.Events:
handleFileSystemEvent(event)
case err := <-watcherForHost.Errors:
logrus.Warnf("Received an error from the file system watcher: %v", err)
}
}
return nil
}
func initContainerHelp(cmd *cobra.Command, args []string) {
if utils.IsInsideContainer() {
if !utils.IsInsideToolboxContainer() {
fmt.Fprintf(os.Stderr, "Error: this is not a toolbox container\n")
return
}
if _, err := utils.ForwardToHost(); err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
return
}
return
}
if err := utils.ShowManual("toolbox-init-container"); err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
return
}
}
func configureUsers(targetUserUid int,
targetUser, targetUserHome, targetUserShell string,
homeLink, targetUserExists bool) error {
if homeLink {
if err := redirectPath("/home", "/var/home", true); err != nil {
return err
}
}
sudoGroup, err := utils.GetGroupForSudo()
if err != nil {
return fmt.Errorf("failed to get group for sudo: %w", err)
}
if targetUserExists {
logrus.Debugf("Modifying user %s with UID %d:", targetUser, targetUserUid)
usermodArgs := []string{
"--append",
"--groups", sudoGroup,
"--home", targetUserHome,
"--shell", targetUserShell,
"--uid", fmt.Sprint(targetUserUid),
targetUser,
}
logrus.Debug("usermod")
for _, arg := range usermodArgs {
logrus.Debugf("%s", arg)
}
if err := shell.Run("usermod", nil, nil, nil, usermodArgs...); err != nil {
return fmt.Errorf("failed to modify user %s with UID %d", targetUser, targetUserUid)
}
} else {
logrus.Debugf("Adding user %s with UID %d:", targetUser, targetUserUid)
useraddArgs := []string{
"--groups", sudoGroup,
"--home-dir", targetUserHome,
"--no-create-home",
"--shell", targetUserShell,
"--uid", fmt.Sprint(targetUserUid),
targetUser,
}
logrus.Debug("useradd")
for _, arg := range useraddArgs {
logrus.Debugf("%s", arg)
}
if err := shell.Run("useradd", nil, nil, nil, useraddArgs...); err != nil {
return fmt.Errorf("failed to add user %s with UID %d", targetUser, targetUserUid)
}
}
logrus.Debugf("Removing password for user %s", targetUser)
if err := shell.Run("passwd", nil, nil, nil, "--delete", targetUser); err != nil {
return fmt.Errorf("failed to remove password for user %s", targetUser)
}
logrus.Debug("Removing password for user root")
if err := shell.Run("passwd", nil, nil, nil, "--delete", "root"); err != nil {
return errors.New("failed to remove password for root")
}
return nil
}
func handleFileSystemEvent(event fsnotify.Event) {
eventOpString := event.Op.String()
logrus.Debugf("Handling file system event: operation %s on %s", eventOpString, event.Name)
if event.Name == "/run/host/etc/localtime" {
if err := updateTimeZoneFromLocalTime(); err != nil {
logrus.Warnf("Failed to handle changes to the host's /etc/localtime: %v", err)
}
}
}
func mountBind(containerPath, source, flags string) error {
fi, err := os.Stat(source)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return fmt.Errorf("failed to stat %s", source)
}
if fi.IsDir() {
logrus.Debugf("Creating %s", containerPath)
if err := os.MkdirAll(containerPath, 0755); err != nil {
return fmt.Errorf("failed to create %s", containerPath)
}
}
logrus.Debugf("Binding %s to %s", containerPath, source)
args := []string{
"--rbind",
}
if flags != "" {
args = append(args, []string{"-o", flags}...)
}
args = append(args, []string{source, containerPath}...)
if err := shell.Run("mount", nil, nil, nil, args...); err != nil {
return fmt.Errorf("failed to bind %s to %s", containerPath, source)
}
return nil
}
// redirectPath serves for creating symbolic links for crucial system
// configuration files to their counterparts on the host's filesystem.
//
// containerPath and target must be absolute paths
//
// If the target itself is a symbolic link, redirectPath will evaluate the
// link. If it's valid then redirectPath will link containerPath to target.
// If it's not, then redirectPath will still proceed with the linking in two
// different ways depending whether target is an absolute or a relative link:
//
// * absolute - target's destination will be prepended with /run/host, and
// containerPath will be linked to the resulting path. This is
// an attempt to unbreak things, but if it doesn't work then
// it's the user's responsibility to fix it up.
//
// This is meant to address the common case where
// /etc/resolv.conf on the host (ie., /run/host/etc/resolv.conf
// inside the container) is an absolute symbolic link to
// something like /run/systemd/resolve/stub-resolv.conf. The
// container's /etc/resolv.conf will then get linked to
// /run/host/run/systemd/resolved/resolv-stub.conf.
//
// This is why properly configured hosts should use relative
// symbolic links, because they don't need to be adjusted in
// such scenarios.
//
// * relative - containerPath will be linked to the invalid target, and it's
// the user's responsibility to fix it up.
//
// folder signifies if the target is a folder
func redirectPath(containerPath, target string, folder bool) error {
if !filepath.IsAbs(containerPath) {
panic("containerPath must be an absolute path")
}
if !filepath.IsAbs(target) {
panic("target must be an absolute path")
}
logrus.Debugf("Preparing to redirect %s to %s", containerPath, target)
targetSanitized := sanitizeRedirectionTarget(target)
err := os.Remove(containerPath)
if folder {
if err != nil && !os.IsNotExist(err) {
return fmt.Errorf("failed to redirect %s to %s: %w", containerPath, target, err)
}
if err := os.MkdirAll(target, 0755); err != nil {
return fmt.Errorf("failed to redirect %s to %s: %w", containerPath, target, err)
}
}
logrus.Debugf("Redirecting %s to %s", containerPath, targetSanitized)
if err := os.Symlink(targetSanitized, containerPath); err != nil {
return fmt.Errorf("failed to redirect %s to %s: %w", containerPath, target, err)
}
return nil
}
func sanitizeRedirectionTarget(target string) string {
if !filepath.IsAbs(target) {
panic("target must be an absolute path")
}
fileInfo, err := os.Lstat(target)
if err != nil {
if os.IsNotExist(err) {
logrus.Warnf("%s not found", target)
} else {
logrus.Warnf("Failed to lstat %s: %v", target, err)
}
return target
}
fileMode := fileInfo.Mode()
if fileMode&os.ModeSymlink == 0 {
logrus.Debugf("%s isn't a symbolic link", target)
return target
}
logrus.Debugf("%s is a symbolic link", target)
_, err = filepath.EvalSymlinks(target)
if err == nil {
return target
}
logrus.Warnf("Failed to resolve %s: %v", target, err)
targetDestination, err := os.Readlink(target)
if err != nil {
logrus.Warnf("Failed to get the destination of %s: %v", target, err)
return target
}
logrus.Debugf("%s points to %s", target, targetDestination)
if filepath.IsAbs(targetDestination) {
logrus.Debugf("Prepending /run/host to %s", targetDestination)
targetGuess := filepath.Join("/run/host", targetDestination)
return targetGuess
}
return target
}
func updateTimeZoneFromLocalTime() error {
localTimeEvaled, err := filepath.EvalSymlinks("/etc/localtime")
if err != nil {
return fmt.Errorf("failed to resolve /etc/localtime: %w", err)
}
logrus.Debugf("Resolved /etc/localtime to %s", localTimeEvaled)
const zoneInfoRoot = "/run/host/usr/share/zoneinfo"
if !strings.HasPrefix(localTimeEvaled, zoneInfoRoot) {
return errors.New("/etc/localtime points to unknown location")
}
timeZone, err := filepath.Rel(zoneInfoRoot, localTimeEvaled)
if err != nil {
return fmt.Errorf("failed to extract time zone: %w", err)
}
const etcTimeZone = "/etc/timezone"
if err := os.Remove(etcTimeZone); err != nil {
if !os.IsNotExist(err) {
return fmt.Errorf("failed to remove old %s: %w", etcTimeZone, err)
}
}
timeZoneBytes := []byte(timeZone + "\n")
err = ioutil.WriteFile(etcTimeZone, timeZoneBytes, 0664)
if err != nil {
return fmt.Errorf("failed to create new %s: %w", etcTimeZone, err)
}
return nil
}