Skip to content

Commit 709983d

Browse files
committed
Support FreeBSD guests in the QEMU test wrapper.
issue: #5
1 parent 64263de commit 709983d

10 files changed

+621
-133
lines changed

WORKSPACE

+36-2
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,49 @@ local_repository(
2929
)
3030

3131
load("@rust_fuse_cc_toolchains//:cc_toolchains.bzl", "cc_toolchains")
32-
load("//build:rust_toolchains.bzl", "rust_toolchains")
33-
load("//build/testutil:testutil.bzl", "busybox_multiarch")
3432

3533
cc_toolchains()
3634

35+
load("//build:rust_toolchains.bzl", "rust_toolchains")
36+
3737
rust_toolchains()
3838

39+
load(
40+
"//build/testutil:testutil.bzl",
41+
"busybox_multiarch",
42+
"freebsd_repository",
43+
"qemu_repository",
44+
)
45+
3946
busybox_multiarch(name = "busybox_multiarch")
4047

48+
freebsd_repository(
49+
name = "freebsd_amd64_v12.2",
50+
platform = "amd64/amd64",
51+
version = "12.2",
52+
)
53+
54+
qemu_repository(
55+
name = "qemu_v5.2.0",
56+
version = "5.2.0",
57+
)
58+
59+
http_archive(
60+
name = "rust_base64",
61+
build_file_content = """
62+
load("@io_bazel_rules_rust//rust:rust.bzl", "rust_library")
63+
rust_library(
64+
name = "base64",
65+
srcs = glob(["**/*.rs"]),
66+
visibility = ["//visibility:public"],
67+
)
68+
""",
69+
sha256 = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd",
70+
strip_prefix = "base64-0.13.0",
71+
type = "tar.gz",
72+
url = "https://crates.io/api/v1/crates/base64/0.13.0/download",
73+
)
74+
4175
http_archive(
4276
name = "rust_diff",
4377
build_file_content = """

build/rust_toolchains.bzl

+1
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,6 @@ def rust_toolchains():
4141
extra_target_triples = [
4242
"armv7-unknown-linux-musleabihf",
4343
"x86_64-unknown-linux-musl",
44+
"x86_64-unknown-freebsd",
4445
],
4546
)

build/testutil/BUILD

+32-23
Original file line numberDiff line numberDiff line change
@@ -3,49 +3,58 @@ load(
33
"rust_binary",
44
"rust_library",
55
)
6-
load(":testutil.bzl", "initrd", "qemu_exec")
6+
load(":testutil.bzl", "freebsd_rootfs", "linux_rootfs", "qemu_exec")
77

88
sh_binary(
9-
name = "build_initrd",
10-
srcs = ["build_initrd.sh"],
9+
name = "build_linux_rootfs",
10+
srcs = ["build_linux_rootfs.sh"],
1111
)
1212

13-
config_setting(
14-
name = "cfg_armv7-linux",
15-
constraint_values = [
16-
"@platforms//cpu:armv7",
17-
"@platforms//os:linux",
18-
],
13+
linux_rootfs(
14+
name = "linux_rootfs",
15+
busybox = select({
16+
"@platforms//cpu:armv7": "@busybox_multiarch//:busybox-armv7l",
17+
"@platforms//cpu:x86_64": "@busybox_multiarch//:busybox-x86_64",
18+
}),
19+
kernel = select({
20+
"@platforms//cpu:armv7": "@linux_kernel//:arch/arm/boot/bzImage",
21+
"@platforms//cpu:x86_64": "@linux_kernel//:arch/x86_64/boot/bzImage",
22+
}),
1923
)
2024

21-
config_setting(
22-
name = "cfg_x86_64-linux",
23-
constraint_values = [
24-
"@platforms//cpu:x86_64",
25-
"@platforms//os:linux",
26-
],
25+
sh_binary(
26+
name = "build_freebsd_rootfs",
27+
srcs = ["build_freebsd_rootfs.sh"],
2728
)
2829

29-
initrd(
30-
name = "initrd",
31-
busybox = select({
32-
"cfg_armv7-linux": "@busybox_multiarch//:busybox-armv7l",
33-
"cfg_x86_64-linux": "@busybox_multiarch//:busybox-x86_64",
30+
freebsd_rootfs(
31+
name = "freebsd_rootfs",
32+
freebsd = select({
33+
"@platforms//cpu:x86_64": "@freebsd_amd64_v12.2//:freebsd",
3434
}),
3535
)
3636

3737
qemu_exec(
3838
name = "qemu_exec",
39-
kernel = select({
40-
"cfg_armv7-linux": "@linux_kernel//:arch/arm/boot/bzImage",
41-
"cfg_x86_64-linux": "@linux_kernel//:arch/x86_64/boot/bzImage",
39+
cpu = select({
40+
"@platforms//cpu:armv7": "armv7",
41+
"@platforms//cpu:x86_64": "x86_64",
42+
}),
43+
os = select({
44+
"@platforms//os:linux": "linux",
45+
"@platforms//os:freebsd": "freebsd",
46+
}),
47+
rootfs = select({
48+
"@platforms//os:linux": ":linux_rootfs",
49+
"@platforms//os:freebsd": ":freebsd_rootfs",
4250
}),
4351
)
4452

4553
rust_binary(
4654
name = "qemu_exec_helper",
4755
srcs = ["qemu_exec_helper.rs"],
4856
deps = [
57+
"@rust_base64//:base64",
4958
"@rust_json//:json",
5059
],
5160
)
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/bin/sh
2+
set -eu
3+
4+
rootfs_path="$1"
5+
qemu_exec_helper_path="$2"
6+
freebsd_path="${PWD}/$3"
7+
8+
mkdir "${rootfs_path}/rust-fuse"
9+
cp "${qemu_exec_helper_path}" "${rootfs_path}/rust-fuse"
10+
11+
cd "${rootfs_path}"
12+
mkdir {bin,boot,boot/kernel,dev,etc,lib,libexec,rescue,sbin,tmp}
13+
mkdir rust-fuse/test_sandbox
14+
15+
touch dev/.keep
16+
touch rust-fuse/test_sandbox/.keep
17+
touch tmp/.keep
18+
19+
cp -L "${freebsd_path}/rescue/rescue" rescue/
20+
ln -s ../rescue/rescue bin/sh
21+
ln -s ../rescue/rescue sbin/init
22+
23+
cp -L "${freebsd_path}"/boot/loader_simp.efi boot/
24+
cp -L "${freebsd_path}"/boot/kernel/* boot/kernel/
25+
cp -L "${freebsd_path}"/lib/* lib/
26+
cp -L "${freebsd_path}"/libexec/* libexec/
27+
28+
cat >etc/rc <<EOF
29+
#!/bin/sh
30+
set +eu
31+
/rescue/rescue kldload cuse
32+
/rescue/rescue kldload fusefs
33+
/rescue/rescue kldload virtio_console
34+
35+
/rescue/rescue mdconfig -a -t swap -s 256m -u 0
36+
/rescue/rescue newfs -U md0
37+
/rescue/rescue mount /dev/md0 /rust-fuse/test_sandbox
38+
39+
/rescue/rescue mdconfig -a -t swap -s 256m -u 1
40+
/rescue/rescue newfs -U md1
41+
/rescue/rescue mount /dev/md1 /tmp
42+
43+
/rust-fuse/qemu_exec_helper
44+
EOF
45+
46+
chmod +x etc/rc

build/testutil/build_initrd.sh build/testutil/build_linux_rootfs.sh

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
#!/bin/sh
22
set -eu
33

4-
busybox_path="$1"
5-
qemu_exec_helper_path="$2"
6-
initrd_path="$3"
4+
rootfs_path="$1"
5+
busybox_path="$2"
6+
kernel_path="$3"
7+
qemu_exec_helper_path="$4"
8+
9+
mkdir "${rootfs_path}/boot"
10+
cp "${kernel_path}" "${rootfs_path}/boot/bzImage"
711

812
mkdir initrd-dir
913
cd initrd-dir
@@ -29,4 +33,4 @@ EOF
2933

3034
chmod +x bin/* etc/init.d/rcS
3135

32-
find . -print0 | cpio --null -ov --format=newc | gzip -9 > "../${initrd_path}"
36+
find . -print0 | cpio --null -ov --format=newc | gzip -9 > "../${rootfs_path}/boot/initrd.cpio.gz"

build/testutil/freebsd.bzl

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# Copyright 2021 John Millikin and the rust-fuse contributors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
# SPDX-License-Identifier: Apache-2.0
16+
17+
_CHECKSUMS = {
18+
"amd64/amd64/12.2-RELEASE/base.txz": "8bd49ce35c340a04029266fbbe82b1fdfeb914263e39579eecafb2e67d00693a",
19+
"amd64/amd64/12.2-RELEASE/kernel.txz": "729584a21f564cf9c1fa7d4a85ab6fa00a8c5370207396fa95d242b0bef750cb",
20+
}
21+
22+
_BUILD = """
23+
filegroup(
24+
name = "freebsd",
25+
srcs = glob(
26+
["**/*"],
27+
exclude = [
28+
"BUILD.bazel",
29+
"WORKSPACE",
30+
"base.tar.xz",
31+
"kernel.tar.xz",
32+
],
33+
),
34+
visibility = ["//visibility:public"],
35+
)
36+
"""
37+
38+
def _freebsd_repository(ctx):
39+
ctx.file("WORKSPACE", "workspace(name = {})\n".format(repr(ctx.name)))
40+
ctx.file("BUILD.bazel", _BUILD)
41+
42+
base_filename = "{}/{}-RELEASE/base.txz".format(ctx.attr.platform, ctx.attr.version)
43+
kernel_filename = "{}/{}-RELEASE/kernel.txz".format(ctx.attr.platform, ctx.attr.version)
44+
45+
ctx.download(
46+
url = ["https://download.freebsd.org/ftp/releases/" + base_filename],
47+
output = "base.tar.xz",
48+
sha256 = _CHECKSUMS[base_filename],
49+
)
50+
51+
ctx.download(
52+
url = ["https://download.freebsd.org/ftp/releases/" + kernel_filename],
53+
output = "kernel.tar.xz",
54+
sha256 = _CHECKSUMS[kernel_filename],
55+
)
56+
57+
ctx.execute(
58+
[
59+
"tar",
60+
"-xf",
61+
"kernel.tar.xz",
62+
"boot/kernel/kernel",
63+
"boot/kernel/cuse.ko",
64+
"boot/kernel/fusefs.ko",
65+
"boot/kernel/virtio_console.ko",
66+
],
67+
)
68+
69+
ctx.execute(
70+
[
71+
"tar",
72+
"-xf",
73+
"base.tar.xz",
74+
"boot/loader_simp.efi",
75+
"rescue/mt",
76+
"rescue/init",
77+
"rescue/rescue",
78+
"rescue/sh",
79+
"lib/libc.so.7",
80+
"lib/libgcc_s.so.1",
81+
"libexec/ld-elf.so.1",
82+
],
83+
)
84+
85+
freebsd_repository = repository_rule(
86+
implementation = _freebsd_repository,
87+
attrs = {
88+
"platform": attr.string(
89+
mandatory = True,
90+
values = [
91+
"amd64/amd64",
92+
],
93+
),
94+
"version": attr.string(
95+
mandatory = True,
96+
values = [
97+
"12.2",
98+
],
99+
),
100+
}
101+
)

build/testutil/qemu.bzl

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Copyright 2021 John Millikin and the rust-fuse contributors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
# SPDX-License-Identifier: Apache-2.0
16+
17+
_CHECKSUMS = {
18+
"v5.2.0/pc-bios/edk2-x86_64-code.fd.bz2": "8d9af6d88f51cfb6732a2542fefa50e7d5adb81aa12d0b79342e1bc905a368f1",
19+
}
20+
21+
_BUILD = """
22+
filegroup(
23+
name = "qemu",
24+
srcs = glob(
25+
["**/*"],
26+
exclude = ["BUILD.bazel", "WORKSPACE"],
27+
),
28+
visibility = ["//visibility:public"],
29+
)
30+
"""
31+
32+
def _qemu_repository(ctx):
33+
ctx.file("WORKSPACE", "workspace(name = {})\n".format(repr(ctx.name)))
34+
ctx.file("BUILD.bazel", _BUILD)
35+
36+
edk2_filename = "v{}/pc-bios/edk2-x86_64-code.fd.bz2".format(ctx.attr.version)
37+
38+
ctx.download(
39+
url = ["https://github.com/qemu/qemu/raw/" + edk2_filename],
40+
output = "pc-bios/edk2-x86_64-code.fd.bz2",
41+
sha256 = _CHECKSUMS[edk2_filename],
42+
)
43+
44+
ctx.execute(["bzip2", "-d", "pc-bios/edk2-x86_64-code.fd.bz2"])
45+
46+
qemu_repository = repository_rule(
47+
implementation = _qemu_repository,
48+
attrs = {
49+
"version": attr.string(
50+
mandatory = True,
51+
values = [
52+
"5.2.0",
53+
],
54+
),
55+
}
56+
)

0 commit comments

Comments
 (0)