aboutsummaryrefslogtreecommitdiffhomepage
path: root/internal/rosa/busybox.go
blob: fcfe16480fa2c88d166dfccba911d5a89b898188 (plain)
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
package rosa

import (
	"fmt"
	"io"
	"net/http"
	"os"
	"time"

	"hakurei.app/fhs"
	"hakurei.app/internal/pkg"
)

// busyboxBin is a busybox binary distribution installed under bin/busybox.
type busyboxBin struct {
	// Underlying busybox binary.
	bin pkg.FileArtifact
}

// Kind returns the hardcoded [pkg.Kind] value.
func (a busyboxBin) Kind() pkg.Kind { return kindBusyboxBin }

// Params is a noop.
func (a busyboxBin) Params(*pkg.IContext) {}

// IsExclusive returns false: Cure performs a trivial filesystem write.
func (busyboxBin) IsExclusive() bool { return false }

// Dependencies returns the underlying busybox [pkg.File].
func (a busyboxBin) Dependencies() []pkg.Artifact {
	return []pkg.Artifact{a.bin}
}

func init() {
	pkg.Register(kindBusyboxBin, func(r *pkg.IRReader) pkg.Artifact {
		a := busyboxBin{r.Next().(pkg.FileArtifact)}
		if _, ok := r.Finalise(); ok {
			panic(pkg.ErrUnexpectedChecksum)
		}
		return a
	})
}

// String returns the reporting name of the underlying file prefixed with expand.
func (a busyboxBin) String() string {
	return "expand-" + a.bin.(fmt.Stringer).String()
}

// Cure installs the underlying busybox [pkg.File] to bin/busybox.
func (a busyboxBin) Cure(t *pkg.TContext) (err error) {
	var r io.ReadCloser
	if r, err = t.Open(a.bin); err != nil {
		return
	}
	defer func() {
		closeErr := r.Close()
		if err == nil {
			err = closeErr
		}
	}()

	binDir := t.GetWorkDir().Append("bin")
	if err = os.MkdirAll(binDir.String(), 0700); err != nil {
		return
	}

	var w *os.File
	if w, err = os.OpenFile(
		binDir.Append("busybox").String(),
		os.O_WRONLY|os.O_CREATE|os.O_EXCL,
		0500,
	); err != nil {
		return
	}
	defer func() {
		closeErr := w.Close()
		if err == nil {
			err = closeErr
		}
	}()

	_, err = io.Copy(w, r)
	return
}

// newBusyboxBin returns a [pkg.Artifact] containing a busybox installation from
// the https://busybox.net/downloads/binaries/ binary release.
func newBusyboxBin() pkg.Artifact {
	var version, url, checksum string
	switch arch {
	case "amd64":
		version = "1.35.0"
		url = "https://busybox.net/downloads/binaries/" +
			version + "-" + linuxArch() + "-linux-musl/busybox"
		checksum = "L7OBIsPu9enNHn7FqpBT1kOg_mCLNmetSeNMA3i4Y60Z5jTgnlX3qX3zcQtLx5AB"
	case "arm64":
		version = "1.31.0"
		url = "https://busybox.net/downloads/binaries/" +
			version + "-defconfig-multiarch-musl/busybox-armv8l"
		checksum = "npJjBO7iwhjW6Kx2aXeSxf8kXhVgTCDChOZTTsI8ZfFfa3tbsklxRiidZQdrVERg"

	default:
		panic("unsupported target " + arch)
	}

	return pkg.NewExec(
		"busybox-bin-"+version, arch, nil, pkg.ExecTimeoutMax, false,
		fhs.AbsRoot, []string{
			"PATH=/system/bin",
		},
		AbsSystem.Append("bin", "busybox"),
		[]string{"hush", "-c", "" +
			"busybox mkdir -p /work/system/bin/ && " +
			"busybox cp /system/bin/busybox /work/system/bin/ && " +
			"busybox --install -s /work/system/bin/"},
		pkg.Path(AbsSystem, true, busyboxBin{pkg.NewHTTPGet(
			&http.Client{Transport: &http.Transport{
				// busybox website is really slow to respond
				TLSHandshakeTimeout: 2 * time.Minute,
			}}, url,
			mustDecode(checksum),
		)}),
	)
}