-
Notifications
You must be signed in to change notification settings - Fork 1
/
podman.go
91 lines (80 loc) · 2.11 KB
/
podman.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
// Binary podman is a gokrazy wrapper program that runs the bundled podman
// executable in /usr/local/bin/podman after doing any necessary runtime system
// setup.
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"syscall"
)
func isMounted(mountpoint string) (bool, error) {
b, err := ioutil.ReadFile("/proc/self/mountinfo")
if err != nil {
if os.IsNotExist(err) {
return false, nil // platform does not have /proc/self/mountinfo, fall back to not verifying
}
return false, err
}
for _, line := range strings.Split(strings.TrimSpace(string(b)), "\n") {
parts := strings.Fields(line)
if len(parts) < 5 {
continue
}
if parts[4] == mountpoint {
return true, nil
}
}
return false, nil
}
func makeWritable(dir string) error {
mounted, err := isMounted(dir)
if err != nil {
return err
}
if mounted {
// Nothing to do, directory is already mounted.
return nil
}
// Read all regular files in this directory.
regularFiles := make(map[string]string)
fis, err := os.ReadDir(dir)
if err != nil {
return err
}
for _, fi := range fis {
b, err := os.ReadFile(filepath.Join(dir, fi.Name()))
if err != nil {
return err
}
regularFiles[fi.Name()] = string(b)
}
if err := syscall.Mount("tmpfs", dir, "tmpfs", 0, ""); err != nil {
return fmt.Errorf("tmpfs on %s: %v", dir, err)
}
// Write all regular files from memory back to new tmpfs.
for name, contents := range regularFiles {
if err := os.WriteFile(filepath.Join(dir, name), []byte(contents), 0644); err != nil {
return err
}
}
return nil
}
func main() {
// Workaround for podman ≤ v4.2.1, which failed on read-only /etc:
// https://github.com/containers/common/commit/50c2c97c3b828f908f1a22f6967c1136163dcefd
//
// The fix was imported into podman as part of commit
// https://github.com/containers/podman/commit/0f739355635d5bc4d538cf88009d7af533e7c289
//
// TODO: drop this entire binary with podman > v4.2.1
if err := makeWritable("/etc/cni/net.d/"); err != nil {
log.Fatal(err)
}
if err := syscall.Exec("/usr/local/bin/podman", os.Args, os.Environ()); err != nil {
log.Fatal(err)
}
}