-
Notifications
You must be signed in to change notification settings - Fork 38
/
machine.go
941 lines (787 loc) · 22.5 KB
/
machine.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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
//go:build linux && (arm64 || amd64)
package fakemachine
import (
"bufio"
"bytes"
"errors"
"fmt"
"github.com/alessio/shellescape"
"io"
"os"
"os/exec"
"path"
"path/filepath"
"regexp"
"runtime"
"strconv"
"strings"
"text/template"
writerhelper "github.com/go-debos/fakemachine/cpio"
)
func mergedUsrSystem() bool {
f, _ := os.Lstat("/bin")
return (f.Mode() & os.ModeSymlink) == os.ModeSymlink
}
// Parse modinfo output and return the value of module attributes
// There may be multiple row with same fieldname so []string
// is used to return all data.
func getModData(modname string, fieldname string, kernelRelease string) []string {
out, err := exec.Command("modinfo", "-k", kernelRelease, modname).Output()
if err != nil {
return nil
}
var fieldValue []string
scanner := bufio.NewScanner(strings.NewReader(string(out)))
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
field := strings.Split(strings.TrimSpace(scanner.Text()), ":")
if strings.TrimSpace(field[0]) == fieldname {
fieldValue = append(fieldValue, strings.TrimSpace(field[1]))
}
}
return fieldValue
}
// Get full path of module
func getModPath(modname string, kernelRelease string) string {
path := getModData(modname, "filename", kernelRelease)
if len(path) != 0 {
return path[0]
}
return ""
}
// Get all dependent module
func getModDepends(modname string, kernelRelease string) []string {
deplist := getModData(modname, "depends", kernelRelease)
var modlist []string
for _, v := range deplist {
if v != "" {
modlist = append(modlist, strings.Split(v, ",")...)
}
}
// Busybox expects a full dependency list for each module rather than just
// direct dependencies, so recurse the module dependency tree:
// https://github.com/mirror/busybox/blob/1dd2685dcc735496d7adde87ac60b9434ed4a04c/modutils/modprobe.c#L46-L49
var sublist []string
for _, mod := range modlist {
sublist = append(sublist, getModDepends(mod, kernelRelease)...)
}
modlist = append(modlist, sublist...)
return modlist
}
var suffixes = map[string]writerhelper.Transformer{
".ko": NullDecompressor,
".ko.gz": GzipDecompressor,
".ko.xz": XzDecompressor,
".ko.zst": ZstdDecompressor,
}
func (m *Machine) copyModules(w *writerhelper.WriterHelper, modname string, copiedModules map[string]bool) error {
release, _ := m.backend.KernelRelease()
modpath := getModPath(modname, release)
if modpath == "" {
return errors.New("modules path couldn't be determined")
}
if modpath == "(builtin)" || copiedModules[modname] {
return nil
}
found := false
for suffix, fn := range suffixes {
if strings.HasSuffix(modpath, suffix) {
// File must exist as-is on the filesystem. Aka do not
// fallback to other suffixes.
if _, err := os.Stat(modpath); err != nil {
return err
}
// The suffix is the complete thing - ".ko.foobar"
// Reinstate the required ".ko" part, after trimming.
dest := strings.TrimSuffix(modpath, suffix) + ".ko"
// Ensure destination has /usr prefix if running
// on merged-usr system.
if mergedUsrSystem() && !strings.HasPrefix(dest, "/usr") {
dest = "/usr" + dest
}
if err := w.TransformFileTo(modpath, dest, fn); err != nil {
return err
}
found = true
break
}
}
if !found {
return errors.New("module extension/suffix unknown")
}
copiedModules[modname] = true
deplist := getModDepends(modname, release)
for _, mod := range deplist {
if err := m.copyModules(w, mod, copiedModules); err != nil {
return err
}
}
return nil
}
// Evaluate any symbolic link, then return the path's directory. Returns an
// absolute path. Think of it as realpath(1) + dirname(1) in bash.
func realDir(path string) (string, error) {
var p string
var err error
if p, err = filepath.Abs(path); err != nil {
return "", err
}
if p, err = filepath.EvalSymlinks(p); err != nil {
return "", err
}
return filepath.Dir(p), nil
}
type Arch string
const (
Amd64 Arch = "amd64"
Arm64 Arch = "arm64"
)
var archMap = map[string]Arch{
"amd64": Amd64,
"arm64": Arm64,
}
var archDynamicLinker = map[Arch]string{
Amd64: "/lib64/ld-linux-x86-64.so.2",
Arm64: "/lib/ld-linux-aarch64.so.1",
}
type mountPoint struct {
hostDirectory string
machineDirectory string
label string
static bool
}
type image struct {
path string
label string
}
type Machine struct {
arch Arch
backend backend
mounts []mountPoint
count int
images []image
memory int
numcpus int
showBoot bool
quiet bool
Environ []string
scratchsize int64
scratchpath string
scratchfile string
scratchdev string
initrdpath string
}
// Create a new machine object with the auto backend
func NewMachine() (*Machine, error) {
return NewMachineWithBackend("auto")
}
// Create a new machine object
func NewMachineWithBackend(backendName string) (*Machine, error) {
var err error
m := &Machine{memory: 2048, numcpus: runtime.NumCPU()}
var ok bool
if m.arch, ok = archMap[runtime.GOARCH]; !ok {
return nil, fmt.Errorf("unsupported arch %s", runtime.GOARCH)
}
m.backend, err = newBackend(backendName, m)
if err != nil {
return nil, err
}
// usr is mounted by specific label via /init
m.addStaticVolume("/usr", "usr")
if !mergedUsrSystem() {
m.addStaticVolume("/sbin", "sbin")
m.addStaticVolume("/bin", "bin")
m.addStaticVolume("/lib", "lib")
}
// Mounts for ssl certificates
if _, err := os.Stat("/etc/ca-certificates"); err == nil {
m.AddVolume("/etc/ca-certificates")
}
if _, err := os.Stat("/etc/ssl"); err == nil {
m.AddVolume("/etc/ssl")
}
// Mounts for java VM configuration, especialy security policies
matches, _ := filepath.Glob("/etc/java*")
for _, path := range matches {
stat, err := os.Stat(path)
if err == nil && stat.IsDir() {
m.AddVolume(path)
}
}
// Dbus configuration
if _, err := os.Stat("/etc/dbus-1"); err == nil {
m.AddVolume("/etc/dbus-1")
}
// Debian alternative symlinks
if _, err := os.Stat("/etc/alternatives"); err == nil {
m.AddVolume("/etc/alternatives")
}
// Debians binfmt registry
if _, err := os.Stat("/var/lib/binfmts"); err == nil {
m.AddVolume("/var/lib/binfmts")
}
return m, nil
}
func InMachine() (ret bool) {
_, ret = os.LookupEnv("IN_FAKE_MACHINE")
return
}
// Check whether the auto backend is supported
func Supported() bool {
_, err := newBackend("auto", nil)
return err == nil
}
const initScript = `#!/bin/busybox sh
busybox mount -t proc proc /proc
busybox mount -t sysfs none /sys
# probe additional modules
{{ range $m := .Backend.InitModules }}
busybox modprobe {{ $m }}
{{ end }}
# mount static volumes
{{ range $point := StaticVolumes .Machine }}
{{ MountVolume $.Backend $point }}
{{ end }}
exec /lib/systemd/systemd
`
const networkdTemplate = `
[Match]
Type=ether
[Network]
DHCP=ipv4
# Disable link-local address to speedup boot
LinkLocalAddressing=no
IPv6AcceptRA=no
`
const networkdLinkTemplate = `
[Match]
Type=ether
[Link]
# Give the interface a static name
Name=ethernet0
`
const commandWrapper = `#!/bin/sh
/lib/systemd/systemd-networkd-wait-online -q --interface=ethernet0
if [ $? != 0 ]; then
echo "WARNING: Network setup failed"
echo "== Journal =="
journalctl -a --no-pager
echo "== networkd =="
networkctl status
networkctl list
echo 1 > /run/fakemachine/result
exit
fi
%[1]s
echo $? > /run/fakemachine/result
`
// The line 'Environment=%[2]s' is used for environment variables optionally
// configured using Machine.SetEnviron()
const serviceTemplate = `
[Unit]
Description=fakemachine runner
Conflicts=shutdown.target
Before=shutdown.target
Requires=basic.target
Wants=systemd-resolved.service binfmt-support.service systemd-networkd.service
After=basic.target systemd-resolved.service binfmt-support.service systemd-networkd.service
OnFailure=poweroff.target
[Service]
Environment=HOME=/root IN_FAKE_MACHINE=yes %[2]s
WorkingDirectory=-/scratch
ExecStart=/wrapper
ExecStopPost=/bin/sync
ExecStopPost=/bin/systemctl poweroff -q -ff
Type=idle
TTYPath=%[1]s
StandardInput=tty-force
StandardOutput=inherit
StandardError=inherit
KillMode=process
IgnoreSIGPIPE=no
SendSIGHUP=yes
LimitNOFILE=4096
`
// helper function to generate a mount command for a given mountpoint
func tmplMountVolume(b backend, m mountPoint) string {
fsType, options := b.MountParameters(m)
mntCommand := []string{"busybox", "mount", "-v"}
mntCommand = append(mntCommand, "-t", fsType)
if len(options) > 0 {
mntCommand = append(mntCommand, "-o", strings.Join(options, ","))
}
mntCommand = append(mntCommand, m.label)
mntCommand = append(mntCommand, m.machineDirectory)
return strings.Join(mntCommand, " ")
}
// helper function to return the static volumes from a machine, since the mounts variable is unexported
// include the extra static mounts from the backend
func tmplStaticVolumes(m Machine) []mountPoint {
mounts := []mountPoint{}
for _, mount := range append(m.mounts, m.backend.InitStaticVolumes()...) {
if mount.static {
mounts = append(mounts, mount)
}
}
return mounts
}
func executeInitScriptTemplate(m *Machine, b backend) ([]byte, error) {
helperFuncs := template.FuncMap{
"MountVolume": tmplMountVolume,
"StaticVolumes": tmplStaticVolumes,
}
type templateVars struct {
Machine *Machine
Backend backend
}
tmplVariables := templateVars{m, b}
tmpl := template.Must(template.New("init").Funcs(helperFuncs).Parse(initScript))
out := &bytes.Buffer{}
if err := tmpl.Execute(out, tmplVariables); err != nil {
return nil, err
}
return out.Bytes(), nil
}
func (m *Machine) addStaticVolume(directory, label string) {
m.mounts = append(m.mounts, mountPoint{directory, directory, label, true})
}
// AddVolumeAt mounts hostDirectory from the host at machineDirectory in the
// fake machine
func (m *Machine) AddVolumeAt(hostDirectory, machineDirectory string) {
label := fmt.Sprintf("virtfs-%d", m.count)
for _, mount := range m.mounts {
if mount.hostDirectory == hostDirectory && mount.machineDirectory == machineDirectory {
// Do not need to add already existing mount
return
}
}
m.mounts = append(m.mounts, mountPoint{hostDirectory, machineDirectory, label, false})
m.count = m.count + 1
}
// AddVolume mounts directory from the host at the same location in the
// fake machine
func (m *Machine) AddVolume(directory string) {
m.AddVolumeAt(directory, directory)
}
// CreateImageWithLabel creates an image file at path a given size and exposes
// it in the fake machine using the given label as the serial id. If size is -1
// then the image should already exist and the size isn't modified.
//
// label needs to be less then 20 characters due to limitations from qemu
//
// The returned string is the device path of the new image as seen inside
// fakemachine.
func (m *Machine) CreateImageWithLabel(path string, size int64, label string) (string,
error) {
if size < 0 {
_, err := os.Stat(path)
if err != nil {
return "", err
}
}
if len(label) >= 20 {
return "", fmt.Errorf("label '%s' too long; cannot be more then 20 characters", label)
}
for _, image := range m.images {
if image.label == label {
return "", fmt.Errorf("label '%s' already exists", label)
}
}
i, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
return "", err
}
if size >= 0 {
err = i.Truncate(size)
if err != nil {
return "", err
}
}
i.Close()
m.images = append(m.images, image{path, label})
return fmt.Sprintf("/dev/disk/by-fakemachine-label/%s", label), nil
}
// CreateImage does the same as CreateImageWithLabel but lets the library pick
// the label.
func (m *Machine) CreateImage(imagepath string, size int64) (string, error) {
label := fmt.Sprintf("fakedisk-%d", len(m.images))
return m.CreateImageWithLabel(imagepath, size, label)
}
// SetMemory sets the fakemachines amount of memory (in megabytes). Defaults to
// 2048 MB
func (m *Machine) SetMemory(memory int) {
m.memory = memory
}
// SetNumCPUs sets the number of CPUs exposed to the fakemachine. Defaults to
// the number of available cores in the system.
func (m *Machine) SetNumCPUs(numcpus int) {
m.numcpus = numcpus
}
// SetShowBoot sets whether to show boot/console messages from the fakemachine.
func (m *Machine) SetShowBoot(showBoot bool) {
m.showBoot = showBoot
}
// SetQuiet sets whether fakemachine should print additional information (e.g.
// the command to be ran) or just print the stdout/stderr of the command to be
// ran.
func (m *Machine) SetQuiet(quiet bool) {
m.quiet = quiet
}
// SetScratch sets the size and location of on-disk scratch space to allocate
// (sparsely) for /scratch. If not set /scratch will be backed by memory. If
// Path is "" then the working directory is used as a default storage location
func (m *Machine) SetScratch(scratchsize int64, path string) {
m.scratchsize = scratchsize
if path == "" {
m.scratchpath, _ = os.Getwd()
} else {
m.scratchpath = path
}
}
func (m Machine) generateFstab(w *writerhelper.WriterHelper, backend backend) error {
fstab := []string{"# Generated fstab file by fakemachine"}
if m.scratchfile == "" {
fstab = append(fstab, "none /scratch tmpfs size=95% 0 0")
} else {
fstab = append(fstab, fmt.Sprintf("%s /scratch ext4 defaults,relatime 0 0",
m.scratchdev))
}
for _, point := range m.mounts {
fstype, options := backend.MountParameters(point)
fstab = append(fstab,
fmt.Sprintf("%s %s %s %s 0 0",
point.label, point.machineDirectory, fstype, strings.Join(options, ",")))
}
fstab = append(fstab, "")
err := w.WriteFile("/etc/fstab", strings.Join(fstab, "\n"), 0755)
return err
}
func stripCompressionSuffix(module string) (string, error) {
for suffix := range suffixes {
if strings.HasSuffix(module, suffix) {
// The suffix is the complete thing - ".ko.foobar"
// Reinstate the required ".ko" part, after trimming.
return strings.TrimSuffix(module, suffix) + ".ko", nil
}
}
return "", errors.New("module extension/suffix unknown")
}
func (m *Machine) generateModulesDep(w *writerhelper.WriterHelper, moddir string, modules map[string]bool) error {
output := make([]string, len(modules))
release, _ := m.backend.KernelRelease()
i := 0
for mod := range modules {
modpath, _ := stripCompressionSuffix(getModPath(mod, release)) // CANNOT fail
deplist := getModDepends(mod, release) // CANNOT fail
deps := make([]string, len(deplist))
for j, dep := range deplist {
deppath, _ := stripCompressionSuffix(getModPath(dep, release)) // CANNOT fail
deps[j] = deppath
}
output[i] = fmt.Sprintf("%s: %s", modpath, strings.Join(deps, " "))
i += 1
}
path := path.Join(moddir, "modules.dep")
return w.WriteFile(path, strings.Join(output, "\n"), 0644)
}
func (m *Machine) SetEnviron(environ []string) {
m.Environ = environ
}
func (m *Machine) writerKernelModules(w *writerhelper.WriterHelper, moddir string, modules []string) error {
if len(modules) == 0 {
return nil
}
modfiles := []string{
"modules.builtin",
"modules.alias",
"modules.symbols"}
for _, v := range modfiles {
if err := w.CopyFile(moddir + "/" + v); err != nil {
return err
}
}
copiedModules := make(map[string]bool)
for _, modname := range modules {
if err := m.copyModules(w, modname, copiedModules); err != nil {
return err
}
}
return m.generateModulesDep(w, moddir, copiedModules)
}
func (m *Machine) setupscratch() error {
if m.scratchsize == 0 {
return nil
}
tmpfile, err := os.CreateTemp(m.scratchpath, "fake-scratch.img.")
if err != nil {
return err
}
m.scratchfile = tmpfile.Name()
m.scratchdev, err = m.CreateImageWithLabel(tmpfile.Name(), m.scratchsize, "fake-scratch")
if err != nil {
return err
}
mkfs := exec.Command("mkfs.ext4", "-q", tmpfile.Name())
err = mkfs.Run()
return err
}
func (m *Machine) cleanup() {
if m.scratchfile != "" {
os.Remove(m.scratchfile)
}
m.scratchfile = ""
}
// Start the machine running the given command and adding the extra content to
// the cpio. Extracontent is a list of {source, dest} tuples
func (m *Machine) startup(command string, extracontent [][2]string) (int, error) {
defer m.cleanup()
os.Setenv("PATH", os.Getenv("PATH")+":/sbin:/usr/sbin")
/* Sanity check mountpoints */
for _, v := range m.mounts {
/* Check the directory exists on the host */
stat, err := os.Stat(v.hostDirectory)
if err != nil || !stat.IsDir() {
return -1, fmt.Errorf("couldn't mount %s inside machine: expected a directory", v.hostDirectory)
}
/* Check for whitespace in the machine directory */
if regexp.MustCompile(`\s`).MatchString(v.machineDirectory) {
return -1, fmt.Errorf("couldn't mount %s inside machine: machine directory (%s) contains whitespace", v.hostDirectory, v.machineDirectory)
}
/* Check for whitespace in the label */
if regexp.MustCompile(`\s`).MatchString(v.label) {
return -1, fmt.Errorf("couldn't mount %s inside machine: label (%s) contains whitespace", v.hostDirectory, v.label)
}
}
tmpdir, err := os.MkdirTemp("", "fakemachine-")
if err != nil {
return -1, err
}
m.AddVolumeAt(tmpdir, "/run/fakemachine")
defer os.RemoveAll(tmpdir)
err = m.setupscratch()
if err != nil {
return -1, err
}
m.initrdpath = path.Join(tmpdir, "initramfs.cpio")
f, err := os.OpenFile(m.initrdpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0755)
if err != nil {
return -1, err
}
backend := m.backend
kernelModuleDir, err := backend.ModulePath()
if err != nil {
return -1, err
}
w := writerhelper.NewWriterHelper(f)
err = w.WriteDirectories([]writerhelper.WriteDirectory{
{Directory: "/scratch", Perm: 01777},
{Directory: "/var/tmp", Perm: 01777},
{Directory: "/var/lib/dbus", Perm: 0755},
{Directory: "/tmp", Perm: 01777},
{Directory: "/sys", Perm: 0755},
{Directory: "/proc", Perm: 0755},
{Directory: "/run", Perm: 0755},
{Directory: "/usr", Perm: 0755},
{Directory: "/usr/bin", Perm: 0755},
{Directory: "/lib64", Perm: 0755},
})
if err != nil {
return -1, err
}
err = w.WriteSymlink("/run", "/var/run", 0755)
if err != nil {
return -1, err
}
if mergedUsrSystem() {
err = w.WriteSymlinks([]writerhelper.WriteSymlink{
{Target: "/usr/sbin", Link: "/sbin", Perm: 0755},
{Target: "/usr/bin", Link: "/bin", Perm: 0755},
{Target: "/usr/lib", Link: "/lib", Perm: 0755},
{Target: "/usr/lib64", Link: "/lib64", Perm: 0755},
})
if err != nil {
return -1, err
}
} else {
err = w.WriteDirectories([]writerhelper.WriteDirectory{
{Directory: "/sbin", Perm: 0744},
{Directory: "/bin", Perm: 0755},
{Directory: "/lib", Perm: 0755},
})
if err != nil {
return -1, err
}
}
prefix := ""
if mergedUsrSystem() {
prefix = "/usr"
}
// search for busybox; in some distros it's located under /sbin
busybox, err := exec.LookPath("busybox")
if err != nil {
return -1, err
}
err = w.CopyFileTo(busybox, prefix+"/bin/busybox")
if err != nil {
return -1, err
}
/* Ensure systemd-resolved is available */
if _, err := os.Stat("/lib/systemd/systemd-resolved"); err != nil {
return -1, err
}
dynamicLinker := archDynamicLinker[m.arch]
err = w.CopyFile(prefix + dynamicLinker)
if err != nil {
return -1, err
}
/* C libraries */
libraryDir, err := realDir(dynamicLinker)
if err != nil {
return -1, err
}
err = w.CopyFile(libraryDir + "/libc.so.6")
if err != nil {
return -1, err
}
err = w.CopyFile(libraryDir + "/libresolv.so.2")
if err != nil {
return -1, err
}
err = w.WriteCharDevice("/dev/console", 5, 1, 0700)
if err != nil {
return -1, err
}
// Linker configuration
err = w.CopyFile("/etc/ld.so.conf")
if err != nil {
return -1, err
}
err = w.CopyTree("/etc/ld.so.conf.d")
if err != nil {
return -1, err
}
// Core system configuration
err = w.WriteFile("/etc/machine-id", "", 0444)
if err != nil {
return -1, err
}
err = w.WriteFile("/etc/hostname", "fakemachine", 0444)
if err != nil {
return -1, err
}
err = w.CopyFile("/etc/passwd")
if err != nil {
return -1, err
}
err = w.CopyFile("/etc/group")
if err != nil {
return -1, err
}
err = w.CopyFile("/etc/nsswitch.conf")
if err != nil {
return -1, err
}
// udev rules
udevRules := strings.Join(backend.UdevRules(), "\n") + "\n"
err = w.WriteFile("/etc/udev/rules.d/61-fakemachine.rules", udevRules, 0444)
if err != nil {
return -1, err
}
err = w.WriteFile("/etc/systemd/network/ethernet.network",
networkdTemplate, 0444)
if err != nil {
return -1, err
}
err = w.WriteFile("/etc/systemd/network/10-ethernet.link",
networkdLinkTemplate, 0444)
if err != nil {
return -1, err
}
err = w.WriteSymlink(
"/lib/systemd/resolv.conf",
"/etc/resolv.conf",
0755)
if err != nil {
return -1, err
}
err = m.writerKernelModules(w, kernelModuleDir, backend.InitModules())
if err != nil {
return -1, err
}
err = w.WriteFile("etc/systemd/system/fakemachine.service",
fmt.Sprintf(serviceTemplate, backend.JobOutputTTY(), strings.Join(m.Environ, " ")), 0644)
if err != nil {
return -1, err
}
err = w.WriteSymlink(
"/lib/systemd/system/serial-getty@ttyS0.service",
"/dev/null",
0755)
if err != nil {
return -1, err
}
err = w.WriteFile("/wrapper",
fmt.Sprintf(commandWrapper, command), 0755)
if err != nil {
return -1, err
}
init, err := executeInitScriptTemplate(m, backend)
if err != nil {
return -1, err
}
err = w.WriteFileRaw("/init", init, 0755)
if err != nil {
return -1, err
}
err = m.generateFstab(w, backend)
if err != nil {
return -1, err
}
for _, v := range extracontent {
err = w.CopyFileTo(v[0], v[1])
if err != nil {
return -1, err
}
}
w.Close()
f.Close()
if !m.quiet {
fmt.Printf("Running %s using %s backend\n", command, backend.Name())
}
success, err := backend.Start()
if !success || err != nil {
return -1, fmt.Errorf("error starting %s backend: %w", backend.Name(), err)
}
result, err := os.Open(path.Join(tmpdir, "result"))
if err != nil {
return -1, err
}
exitstr, _ := io.ReadAll(result)
exitcode, err := strconv.Atoi(strings.TrimSpace(string(exitstr)))
if err != nil {
return -1, err
}
return exitcode, nil
}
// Run creates the machine running the given command
func (m *Machine) Run(command string) (int, error) {
return m.startup(command, nil)
}
// RunInMachineWithArgs runs the caller binary inside the fakemachine with the
// specified commandline arguments
func (m *Machine) RunInMachineWithArgs(args []string) (int, error) {
name := path.Join("/", path.Base(os.Args[0]))
quotedArgs := shellescape.QuoteCommand(args)
command := strings.Join([]string{name, quotedArgs}, " ")
executable, err := exec.LookPath(os.Args[0])
if err != nil {
return -1, fmt.Errorf("failed to find executable: %w", err)
}
return m.startup(command, [][2]string{{executable, name}})
}
// RunInMachine runs the caller binary inside the fakemachine with the same
// commandline arguments as the parent
func (m *Machine) RunInMachine() (int, error) {
return m.RunInMachineWithArgs(os.Args[1:])
}