-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuilder.go
332 lines (271 loc) · 9.16 KB
/
builder.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
// rootfsbuilder - A simple tool to build Debian/Ubuntu rootfs tarballs
// Copyright (C) 2023 Hugo Melder
//
// SPDX-License-Identifier: MIT
//
package main
import (
"fmt"
"io"
"os"
"os/exec"
"path"
"strings"
"time"
)
const ()
type Builder struct {
config *ConfigurationV1
// The host's architecture (debian naming scheme)
hostDebArch string
needsQemu bool
// Path to the qemu static binary, if needed
absoluteQemuPath string
qemuBinaryName string
outDir string
loggerOut io.Writer
loggerErr io.Writer
rootfs string
}
func NewBuilder(config *ConfigurationV1, hostDebArch string, outDir string, loggerOut io.Writer, loggerErr io.Writer) *Builder {
return &Builder{
config: config,
hostDebArch: hostDebArch,
outDir: outDir,
loggerOut: loggerOut,
loggerErr: loggerErr,
}
}
func (b *Builder) Build() ([]string, error) {
args := []string{}
artefacts := []string{}
curDate := time.Now().Unix()
// Qemu static availability check
if b.config.Architecture != b.hostDebArch {
b.needsQemu = true
fmt.Fprintf(b.loggerErr, "Architecture '%s' is not the same as the host architecture '%s', using qemu-static\n", b.config.Architecture, b.hostDebArch)
path, binName, err := checkQemuStaticAvailability(b.config.Architecture)
if err != nil {
return nil, fmt.Errorf("qemu-static availability check failed: %w", err)
}
b.absoluteQemuPath = path
b.qemuBinaryName = binName
}
// Create temporary directory
dir, err := os.MkdirTemp(os.TempDir(), "rootfsbuilder-")
if err != nil {
return nil, fmt.Errorf("error while creating temporary directory: %w", err)
}
b.rootfs = dir
// Defer the removal of the temporary directory
defer os.RemoveAll(dir)
if b.config.Variant != "" {
args = append(args, "--variant="+b.config.Variant)
}
args = append(args, "--arch="+b.config.Architecture)
if b.config.AdditionalPackages != nil {
args = append(args, "--include="+strings.Join(b.config.AdditionalPackages, ","))
}
if b.config.ExcludedPackages != nil {
args = append(args, "--exclude="+strings.Join(b.config.ExcludedPackages, ","))
}
if b.config.Components != nil {
args = append(args, "--components="+strings.Join(b.config.Components, ","))
}
args = append(args, b.config.Release)
args = append(args, b.rootfs)
args = append(args, b.config.Mirror)
cmd := exec.Command("debootstrap", args...)
// Set loggers
cmd.Stdout = b.loggerOut
cmd.Stderr = b.loggerErr
fmt.Fprintf(b.loggerErr, "Running debootstrap with args: %s\n", strings.Join(cmd.Args, " "))
if err = cmd.Run(); err != nil {
return nil, fmt.Errorf("error while running debootstrap: %w", err)
}
// Extract the optional payload
if b.config.Payload != "" {
fmt.Fprint(b.loggerErr, "Extracting payload\n")
err = b.extractPayload()
if err != nil {
return nil, fmt.Errorf("error while extracting payload: %w", err)
}
}
needsMount := b.config.PostInstallCommand != "" || b.config.UseHostsResolvConf
if needsMount {
err = b.mountOperations()
if err != nil {
return nil, fmt.Errorf("error while mounting operations: %w", err)
}
}
// Create tarball
if b.config.TarballType != "" {
withGzip := b.config.TarballType == TarballTypeTarGz
tarballPath := fmt.Sprintf("%s/%s-%s-%s-%d.%s",
b.outDir, b.config.Distribution,
b.config.Release, b.config.Architecture, curDate, b.config.TarballType)
flags := "-cpf"
if withGzip {
flags = "-czpf"
}
// Create a tarball of the rootfs. We do not want a leading directory, and
// we want to preserve all file attributes, and permissions.
cmd = exec.Command("tar", "--xattrs", "--acls", flags, tarballPath, "-C", dir, ".")
// Set loggers
cmd.Stdout = b.loggerOut
cmd.Stderr = b.loggerErr
fmt.Fprintf(b.loggerErr, "Running tar with args: %s\n", strings.Join(cmd.Args, " "))
if err = cmd.Run(); err != nil {
return nil, fmt.Errorf("error while running tar: %w", err)
}
artefacts = append(artefacts, tarballPath)
}
if b.config.SquashFS {
squashfsPath := fmt.Sprintf("%s/%s-%s-%s-%d.squashfs",
b.outDir, b.config.Distribution, b.config.Release, b.config.Architecture, curDate)
// We use the default arguments if none are provided
if b.config.SquashFSArgs == nil {
b.config.SquashFSArgs = []string{dir, squashfsPath, "-noappend"}
} else {
b.config.SquashFSArgs = append(b.config.SquashFSArgs, dir, squashfsPath)
}
// Create a squashfs of the rootfs
cmd = exec.Command("mksquashfs", b.config.SquashFSArgs...)
// Set loggers
cmd.Stdout = b.loggerOut
cmd.Stderr = b.loggerErr
fmt.Fprintf(b.loggerErr, "Running mksquashfs with args: %s\n", strings.Join(cmd.Args, " "))
if err = cmd.Run(); err != nil {
return nil, fmt.Errorf("error while running mksquashfs: %w", err)
}
artefacts = append(artefacts, squashfsPath)
}
return artefacts, nil
}
// RootFS manipulation
func (b *Builder) mountOperations() error {
fmt.Fprintf(b.loggerErr, "Mounting filesystems for chroot\n")
err := b.mountAux()
if err != nil {
return fmt.Errorf("error while mounting aux: %w", err)
}
if b.config.UseHostsResolvConf {
fmt.Fprintf(b.loggerErr, "Copying resolv.conf\n")
// Copy the host's resolv.conf to the rootfs
cmd := exec.Command("cp", "/etc/resolv.conf", b.rootfs+"/etc/resolv.conf")
if err = cmd.Run(); err != nil {
_ = b.unmountRootfs()
return fmt.Errorf("error while copying resolv.conf: %w", err)
}
}
if b.config.PostInstallCommand != "" {
if b.needsQemu {
fmt.Fprintf(b.loggerErr, "Copying qemu-static into rootfs for script execution\n")
// Copy qemu-static into the rootfs /usr/bin directory
cmd := exec.Command("cp", b.absoluteQemuPath, b.rootfs+"/usr/bin/"+b.qemuBinaryName)
if err = cmd.Run(); err != nil {
_ = b.unmountRootfs()
return fmt.Errorf("error while copying qemu-static: %w", err)
}
}
err = b.runInRoofs(b.config.PostInstallCommand)
if err != nil {
// We do not want to leave the rootfs mounted
_ = b.unmountRootfs()
return fmt.Errorf("error while running post install command: %w", err)
}
// Remove qemu-static from the rootfs
if b.needsQemu {
fmt.Fprintf(b.loggerErr, "Removing qemu-static from rootfs...\n")
err = os.Remove(b.rootfs + "/usr/bin/" + b.qemuBinaryName)
if err != nil {
return fmt.Errorf("error while removing qemu-static from rootfs: %w", err)
}
}
}
fmt.Fprintf(b.loggerErr, "Unmounting filesystems\n")
err = b.unmountRootfs()
if err != nil {
return fmt.Errorf("error while unmounting rootfs: %w", err)
}
return nil
}
func (b *Builder) extractPayload() error {
flags := "-xpf"
if b.config.PayloadType == PayloadTypeTarGz {
flags = "-xzpf"
}
absolutePayloadPath := path.Dir(b.config.absoluteConfigPath) + "/" + b.config.Payload
cmd := exec.Command("tar", flags, absolutePayloadPath, "-C", b.rootfs)
if err := cmd.Run(); err != nil {
return fmt.Errorf("error while extracting payload: %w", err)
}
return nil
}
// mount -t proc none "$ROOTFS_PATH/proc"
// mount -t sysfs none "$ROOTFS_PATH/sys"
// mount -o bind /dev "$ROOTFS_PATH/dev"
func (b *Builder) mountAux() error {
cmd := exec.Command("mount", "-t", "proc", "none", b.rootfs+"/proc")
if err := cmd.Run(); err != nil {
return fmt.Errorf("mounting proc: %w", err)
}
cmd = exec.Command("mount", "-t", "sysfs", "none", b.rootfs+"/sys")
if err := cmd.Run(); err != nil {
return fmt.Errorf("mounting sysfs: %w", err)
}
cmd = exec.Command("mount", "-o", "bind", "/dev", b.rootfs+"/dev")
if err := cmd.Run(); err != nil {
return fmt.Errorf("mounting /dev: %w", err)
}
return nil
}
func (b *Builder) runInRoofs(command string, args ...string) error {
// TODO: Set PATH as we use the host's PATH env which may be incorrect
innerCmd := fmt.Sprintf("%s %s", command, strings.Join(args, " "))
// Construct the command arguments for chroot
cmdArgs := []string{}
cmdArgs = append(cmdArgs, b.rootfs)
// Use qemu-static if needed
if b.needsQemu {
cmdArgs = append(cmdArgs, "/usr/bin/"+b.qemuBinaryName)
}
cmdArgs = append(cmdArgs, "/bin/sh", "-c", innerCmd)
cmd := exec.Command("chroot", cmdArgs...)
cmd.Stdout = b.loggerOut
cmd.Stderr = b.loggerErr
fmt.Fprintf(b.loggerErr, "Running command '%s' in rootfs 'chroot %s'\n", command, strings.Join(cmdArgs, " "))
if err := cmd.Run(); err != nil {
return fmt.Errorf("error while running command '%s': %w", command, err)
}
return nil
}
// Check if qemu-static is available for the given architecture.
// Returns the absolute path to the binary.
func checkQemuStaticAvailability(debArch string) (string, string, error) {
arch, err := DebToQemuArch(debArch)
if err != nil {
return "", "", err
}
binaryName := "qemu-" + arch + "-static"
binaryPath, err := exec.LookPath(binaryName)
if err != nil {
return "", "", fmt.Errorf("binary 'qemu-%s-static' not found in PATH", arch)
}
return binaryPath, binaryName, nil
}
func (b *Builder) unmountRootfs() error {
cmd := exec.Command("umount", b.rootfs+"/proc")
if err := cmd.Run(); err != nil {
return fmt.Errorf("unmounting proc: %w", err)
}
cmd = exec.Command("umount", b.rootfs+"/sys")
if err := cmd.Run(); err != nil {
return fmt.Errorf("unmounting sysfs: %w", err)
}
cmd = exec.Command("umount", b.rootfs+"/dev")
if err := cmd.Run(); err != nil {
return fmt.Errorf("unmounting /dev: %w", err)
}
return nil
}