-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
195 lines (154 loc) · 4.16 KB
/
main.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
package main
import (
"io/fs"
"os"
"strings"
"syscall"
"github.com/charmbracelet/log"
"github.com/opencontainers/runc/libcontainer/system"
"github.com/opencontainers/runc/libcontainer/user"
"golang.org/x/sys/unix"
)
func init() {
log.SetOutput(os.Stderr)
log.SetLevel(log.DebugLevel)
}
func main() {
log.Info("started init")
defer func() {
if err := reboot(); err != nil {
log.Fatal(err)
}
}()
log.Debug("decoding run.json file")
config, err := DecodeMachine("/thi/run.json")
if err != nil {
log.Fatalf("could not parse run.json file %v", err)
}
if err := mkdir("/dev", perm0755); err != nil {
log.Fatal(err)
}
initialMnts := MakeInitialMounts(config.RootDevice)
if err := initialMnts.Mount(); err != nil {
log.Fatal(err)
}
log.Debug("Removing /thi to save space")
if err := os.RemoveAll("/thi"); err != nil {
log.Fatal(err)
}
log.Debug("Switching root")
if err := os.Chdir("/newroot"); err != nil {
log.Fatal(err)
}
if err := mount(".", "/", "", unix.MS_MOVE, ""); err != nil {
log.Fatal(err)
}
if err := unix.Chroot("."); err != nil {
log.Fatal(err)
}
if err := os.Chdir("/"); err != nil {
log.Fatal(err)
}
mnts := MakeMounts()
if err := mnts.Mount(); err != nil {
log.Fatal(err)
}
if err := mkdir("/run/lock", fs.FileMode(^uint32(0))); err != nil {
log.Fatalf("could not create /run/lock directory: %v", err)
}
if err := unix.Symlinkat("/proc/self/fd", 0, "/dev/fd"); err != nil {
log.Fatal(err)
}
if err := unix.Symlinkat("/proc/self/fd/0", 0, "/dev/stdin"); err != nil {
log.Fatal(err)
}
if err := unix.Symlinkat("/proc/self/fd/1", 0, "/dev/stdout"); err != nil {
log.Fatal(err)
}
if err := unix.Symlinkat("/proc/self/fd/2", 0, "/dev/stderr"); err != nil {
log.Fatal(err)
}
if err := mkdir("/root", unix.S_IRWXU); err != nil {
log.Fatalf("could not create /root dir: %v", err)
}
cgroupMnt := MakeCgroupMounts()
if err := cgroupMnt.Mount(); err != nil {
log.Fatal(err)
}
if err := unix.Setrlimit(0, &unix.Rlimit{Cur: 10240, Max: 10240}); err != nil {
log.Fatal(err)
}
// parse user and group names
username := config.ImageConfig.User
if username == "" {
username = "root"
}
usrSplit := strings.Split(username, ":")
var group user.Group
if len(usrSplit) < 1 {
log.Fatal("no username set, something is terribly wrong!")
} else if len(usrSplit) >= 2 {
group, err = user.LookupGroup(usrSplit[1])
if err != nil {
log.Fatalf("group %s not found: %v", usrSplit[1], err)
}
}
_ = group
nixUser, err := user.LookupUser(usrSplit[0])
if err != nil {
log.Fatalf("user %s not found: %v", username, err)
}
if err := system.Setgid(nixUser.Gid); err != nil {
log.Fatalf("unable to set group id: %v", err)
}
if err := system.Setuid(nixUser.Uid); err != nil {
log.Fatalf("unable to set group id: %v", err)
}
if err := populateProcessEnv(config.ImageConfig.Env); err != nil {
log.Fatal(err)
}
// set the home dir if not already set
if envHome := os.Getenv("HOME"); envHome == "" {
if err := os.Setenv("HOME", nixUser.Home); err != nil {
log.Fatal("unable to set user home directory")
}
}
if err := MountAdditionalDrives(config.Mounts, nixUser.Uid, nixUser.Gid); err != nil {
log.Fatalf("error mounting drives: %v", err)
}
if err := unix.Sethostname([]byte(config.Hostname)); err != nil {
log.Fatalf("error setting hostname: %v", err)
}
if err := mkdir("/etc", perm0755); err != nil {
log.Fatalf("could not create /etc dir: %v", err)
}
if err := os.WriteFile("/etc/hostname", []byte(config.Hostname+"\n"), perm0755); err != nil {
log.Fatalf("error writing /etc/hostname: %v", err)
}
if err := WriteEtcResolv(config.EtcResolv); err != nil {
log.Fatal(err)
}
if err := WriteEtcHost(config.EtcHost); err != nil {
log.Fatal(err)
}
if err := NetworkSetup(); err != nil {
log.Fatal(err)
}
p, err := NewProcess(config)
if err != nil {
log.Fatal(err)
}
if err = p.Run(); err != nil {
log.Printf("error running process: %v", err)
}
}
func reboot() error {
return syscall.Reboot(syscall.LINUX_REBOOT_CMD_RESTART)
}
func mkdir(name string, perm fs.FileMode) error {
err := os.Mkdir(name, perm)
if err != nil && !os.IsExist(err) {
return err
}
return nil
}