Skip to content

Commit b93e875

Browse files
authored
Merge pull request firecracker-microvm#531 from kzys/arm
Make integration tests work in aarch64
2 parents da69ac5 + ee9e78b commit b93e875

File tree

6 files changed

+153
-5
lines changed

6 files changed

+153
-5
lines changed

Makefile

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,18 @@ FIRECRACKER_CONTAINERD_BUILDER_IMAGE?=golang:1.13-buster
3232
export FIRECRACKER_CONTAINERD_TEST_IMAGE?=localhost/firecracker-containerd-test
3333
export GO_CACHE_VOLUME_NAME?=gocache
3434

35+
# This Makefile uses Firecracker's pre-build Linux kernels for x86_64 and aarch64.
36+
host_arch=$(shell arch)
37+
ifeq ($(host_arch),x86_64)
38+
kernel_sha256sum="bc7e2dbf12cf7038937abf42056f6bcd405d3eef4d27aaa3016f0ba15069ae4b"
39+
else ifeq ($(host_arch),aarch64)
40+
kernel_sha256sum="e2d7c3d6cc123de9e6052d1f434ca7b47635a1f630d63f7fcc1b7164958375e4"
41+
else
42+
$(error "$(host_arch) is not supported by Firecracker")
43+
endif
44+
FIRECRACKER_TARGET?=$(host_arch)-unknown-linux-musl
45+
3546
FIRECRACKER_DIR=$(SUBMODULES)/firecracker
36-
FIRECRACKER_TARGET?=x86_64-unknown-linux-musl
3747
FIRECRACKER_BIN=$(FIRECRACKER_DIR)/build/cargo_target/$(FIRECRACKER_TARGET)/release/firecracker
3848
FIRECRACKER_BUILDER_NAME?=firecracker-builder
3949
CARGO_CACHE_VOLUME_NAME?=cargocache
@@ -171,8 +181,8 @@ $(FIRECRACKER_CONTAINERD_RUNTIME_DIR) $(ETC_CONTAINERD):
171181
DEFAULT_VMLINUX_NAME?=default-vmlinux.bin
172182
$(DEFAULT_VMLINUX_NAME):
173183
curl --silent --show-error --retry 3 --max-time 30 --output $@ \
174-
"https://s3.amazonaws.com/spec.ccfc.min/img/quickstart_guide/x86_64/kernels/vmlinux.bin"
175-
echo "bc7e2dbf12cf7038937abf42056f6bcd405d3eef4d27aaa3016f0ba15069ae4b $@" | sha256sum -c -
184+
"https://s3.amazonaws.com/spec.ccfc.min/img/quickstart_guide/$(host_arch)/kernels/vmlinux.bin"
185+
echo "$(kernel_sha256sum) $@" | sha256sum -c -
176186
chmod 0400 $@
177187

178188
DEFAULT_VMLINUX_INSTALLPATH=$(FIRECRACKER_CONTAINERD_RUNTIME_DIR)/$(DEFAULT_VMLINUX_NAME)

config/config.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020

2121
"github.com/pkg/errors"
2222

23+
"github.com/firecracker-microvm/firecracker-containerd/internal"
2324
"github.com/firecracker-microvm/firecracker-containerd/internal/debug"
2425
"github.com/firecracker-microvm/firecracker-containerd/proto"
2526
models "github.com/firecracker-microvm/firecracker-go-sdk/client/models"
@@ -87,13 +88,20 @@ func LoadConfig(path string) (*Config, error) {
8788
KernelArgs: defaultKernelArgs,
8889
KernelImagePath: defaultKernelPath,
8990
RootDrive: defaultRootfsPath,
90-
CPUTemplate: string(defaultCPUTemplate),
9191
ShimBaseDir: defaultShimBaseDir,
9292
JailerConfig: JailerConfig{
9393
RuncConfigPath: runcConfigPath,
9494
},
9595
}
9696

97+
flag, err := internal.SupportCPUTemplate()
98+
if err != nil {
99+
return nil, err
100+
}
101+
if flag {
102+
cfg.CPUTemplate = string(defaultCPUTemplate)
103+
}
104+
97105
if err := json.Unmarshal(data, cfg); err != nil {
98106
return nil, errors.Wrapf(err, "failed to unmarshal config from %q", path)
99107
}

internal/cpu_template.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"). You may
4+
// not use this file except in compliance with the License. A copy of the
5+
// License is located at
6+
//
7+
// http://aws.amazon.com/apache2.0/
8+
//
9+
// or in the "license" file accompanying this file. This file is distributed
10+
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+
// express or implied. See the License for the specific language governing
12+
// permissions and limitations under the License.
13+
14+
package internal
15+
16+
import (
17+
"bufio"
18+
"io"
19+
"os"
20+
"regexp"
21+
"runtime"
22+
"sync"
23+
)
24+
25+
var (
26+
isIntel bool
27+
isIntelOnce sync.Once
28+
)
29+
30+
// SupportCPUTemplate returns true if Firecracker supports CPU templates on
31+
// the current architecture.
32+
func SupportCPUTemplate() (bool, error) {
33+
if runtime.GOARCH != "amd64" {
34+
return false, nil
35+
}
36+
37+
var err error
38+
isIntelOnce.Do(func() {
39+
isIntel, err = checkIsIntel()
40+
})
41+
return isIntel, err
42+
}
43+
44+
var vendorID = regexp.MustCompile(`^vendor_id\s*:\s*(.+)$`)
45+
46+
func checkIsIntel() (bool, error) {
47+
f, err := os.Open("/proc/cpuinfo")
48+
if err != nil {
49+
return false, err
50+
}
51+
defer f.Close()
52+
53+
id, err := findFirstVendorID(f)
54+
if err != nil {
55+
return false, err
56+
}
57+
58+
return id == "GenuineIntel", nil
59+
}
60+
61+
func findFirstVendorID(r io.Reader) (string, error) {
62+
s := bufio.NewScanner(r)
63+
for s.Scan() {
64+
line := s.Text()
65+
matches := vendorID.FindStringSubmatch(line)
66+
if len(matches) == 2 {
67+
return matches[1], nil
68+
}
69+
}
70+
return "", nil
71+
}

internal/cpu_template_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"). You may
4+
// not use this file except in compliance with the License. A copy of the
5+
// License is located at
6+
//
7+
// http://aws.amazon.com/apache2.0/
8+
//
9+
// or in the "license" file accompanying this file. This file is distributed
10+
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+
// express or implied. See the License for the specific language governing
12+
// permissions and limitations under the License.
13+
14+
package internal
15+
16+
import (
17+
"strings"
18+
"testing"
19+
20+
"github.com/stretchr/testify/assert"
21+
"github.com/stretchr/testify/require"
22+
)
23+
24+
func TestFindFirstVendorID(t *testing.T) {
25+
cases := []struct {
26+
input string
27+
vendorID string
28+
}{
29+
{"vendor_id : GenuineIntel", "GenuineIntel"},
30+
{"vendor_id : AuthenticAMD", "AuthenticAMD"},
31+
32+
// aarch64 doesn't have vendor IDs.
33+
{"", ""},
34+
}
35+
for _, c := range cases {
36+
r := strings.NewReader(c.input)
37+
id, err := findFirstVendorID(r)
38+
require.NoError(t, err)
39+
assert.Equal(t, c.vendorID, id)
40+
}
41+
}

runtime/integ_test.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ var defaultRuntimeConfig = config.Config{
3838
KernelImagePath: "/var/lib/firecracker-containerd/runtime/default-vmlinux.bin",
3939
KernelArgs: "ro console=ttyS0 noapic reboot=k panic=1 pci=off nomodules systemd.journald.forward_to_console systemd.log_color=false systemd.unit=firecracker.target init=/sbin/overlay-init",
4040
RootDrive: "/var/lib/firecracker-containerd/runtime/default-rootfs.img",
41-
CPUTemplate: "T2",
4241
LogLevels: []string{"debug"},
4342
ShimBaseDir: shimBaseDir(),
4443
JailerConfig: config.JailerConfig{
@@ -47,6 +46,17 @@ var defaultRuntimeConfig = config.Config{
4746
},
4847
}
4948

49+
func init() {
50+
flag, err := internal.SupportCPUTemplate()
51+
if err != nil {
52+
panic(err)
53+
}
54+
55+
if flag {
56+
defaultRuntimeConfig.CPUTemplate = "T2"
57+
}
58+
}
59+
5060
func shimBaseDir() string {
5161
if v := os.Getenv(shimBaseDirEnvVar); v != "" {
5262
return v

runtime/service.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,14 @@ func (s *service) buildVMConfiguration(req *proto.CreateVMRequest) (*firecracker
967967
VMID: s.vmID,
968968
}
969969

970+
flag, err := internal.SupportCPUTemplate()
971+
if err != nil {
972+
return nil, err
973+
}
974+
if !flag {
975+
cfg.MachineCfg.CPUTemplate = ""
976+
}
977+
970978
logPath := s.shimDir.FirecrackerLogFifoPath()
971979
if req.LogFifoPath != "" {
972980
logPath = req.LogFifoPath

0 commit comments

Comments
 (0)