Skip to content

Commit

Permalink
new-design: Add fs build (init) script
Browse files Browse the repository at this point in the history
Signed-off-by: Razvan Deaconescu <[email protected]>
  • Loading branch information
razvand committed Jan 1, 2025
1 parent 4ee7d7e commit 7c11bf7
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 116 deletions.
75 changes: 40 additions & 35 deletions utils/new-design/all.py
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,35 @@ def _parse_app_config(self, app_config_file):
if "kconfig" in data["libraries"][l].keys():
self.config["libraries"][l]["kconfig"] = data["libraries"][l]["kconfig"]

def generate_init(self, tester_config):
if self.config['test_dir']:
test_dir = os.path.abspath(self.user_config['test_dir'])
else:
test_dir = os.path.abspath('.tests')
if self.config['rootfs']:
rootfs = os.path.join(os.getcwd(), self.config["rootfs"])
else:
rootfs = ""
init_dir = os.getcwd()

base = tester_config.config["source"]["base"]

name = self.config["name"]

if self.has_template():
app_dir = os.path.join(os.path.join(base, "apps"), self.config['template'])
else:
app_dir = os.getcwd()

with open(os.path.join(SCRIPT_DIR, "tpl_app_fs_init.sh"), "r", encoding="utf-8") as stream:
raw_content = stream.read()

content = raw_content.format(**locals())

with open(os.path.join(test_dir, "app_fs_init.sh"), "w", encoding="utf-8") as stream:
stream.write(content)
os.chmod(os.path.join(test_dir, "app_fs_init.sh"), 0o755)

def __init__(self, app_config="Kraftfile", user_config="config.yaml"):
self.config = {}
self._parse_user_config(user_config)
Expand Down Expand Up @@ -727,10 +756,6 @@ def _generate_makefile(self):
with open(os.path.join(SCRIPT_DIR, "tpl_Makefile"), "r", encoding="utf-8") as stream:
raw_content = stream.read()

if self.app_config.has_template():
app_dir = os.path.join(os.path.join(self.target_config["base"], "apps"), self.app_config.config['template'])
else:
app_dir = os.getcwd()
libs = ""
if 'libraries' in self.app_config.config.keys():
for l in self.app_config.config["libraries"].keys():
Expand All @@ -739,6 +764,11 @@ def _generate_makefile(self):
base = self.target_config["base"]
target_dir = self.dir

if self.app_config.has_template():
app_dir = os.path.join(os.path.join(self.target_config["base"], "apps"), self.app_config.config['template'])
else:
app_dir = os.getcwd()

content = raw_content.format(**locals())

with open(os.path.join(self.dir, "Makefile"), "w", encoding="utf-8") as stream:
Expand Down Expand Up @@ -892,10 +922,6 @@ def _generate_build_make_einitrd(self):
with open(os.path.join(SCRIPT_DIR, "tpl_build_make_einitrd.sh"), "r", encoding="utf-8") as stream:
raw_content = stream.read()

if self.app_config.has_template():
app_dir = os.path.join(os.path.join(self.target_config["base"], "apps"), self.app_config.config['template'])
else:
app_dir = os.getcwd()
base = self.target_config["base"]
target_dir = self.dir
rootfs = os.path.join(os.getcwd(), self.app_config.config["rootfs"])
Expand All @@ -910,28 +936,6 @@ def _generate_build_make_einitrd(self):
stream.write(content)
os.chmod(os.path.join(self.dir, "build"), 0o755)

def _generate_fs(self):
"""Generate file system for examples for Make-based build."""

with open(os.path.join(SCRIPT_DIR, "tpl_build_fs.sh"), "r", encoding="utf-8") as stream:
raw_content = stream.read()

if self.app_config.has_template():
app_dir = os.path.join(os.path.join(self.target_config["base"], "apps"), self.app_config.config['template'])
else:
app_dir = os.getcwd()
base = self.target_config["base"]
target_dir = self.dir
rootfs = os.path.join(os.getcwd(), self.app_config.config["rootfs"])
name = self.app_config.config["name"]
init_dir = os.getcwd()

content = raw_content.format(**locals())

with open(os.path.join(self.dir, "build"), "w", encoding="utf-8") as stream:
stream.write(content)
os.chmod(os.path.join(self.dir, "build"), 0o755)

def _generate_build_kraft(self):
"""Generate build script for Kraft-based build."""

Expand Down Expand Up @@ -963,9 +967,6 @@ def generate(self):
else:
self._generate_build_make()
self._generate_run_kraftfile()
else:
if self.app_config.config["rootfs"]:
self._generate_fs()
elif self.config['build_tool'] == 'kraft':
self._generate_kraftfile()
self._generate_build_kraft()
Expand Down Expand Up @@ -1016,7 +1017,11 @@ def _generate_from_template(self, template_name, output_name):
if self.config["networking"] == "nat" and arch == "arm64":
name = ""

print(template_name)
if self.app_config.has_template():
app_dir = os.path.join(os.path.join(self.target_config["base"], "apps"), self.app_config.config['template'])
else:
app_dir = os.getcwd()

content = raw_content.format(**locals())

with open(os.path.join(self.dir, output_name), "w", encoding="utf-8") as stream:
Expand Down Expand Up @@ -1120,6 +1125,7 @@ def main():

t = TesterConfig(sys.argv[1])
a = AppConfig()
a.generate_init(t)
s = SystemConfig()

copy_common()
Expand All @@ -1128,6 +1134,5 @@ def main():
for t in targets:
t.generate()


if __name__ == "__main__":
sys.exit(main())
27 changes: 27 additions & 0 deletions utils/new-design/tpl_app_fs_init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/sh

init_rootfs="{rootfs}"
testing_rootfs="{init_dir}/.rootfs-for-testing"

# If no root filesystem, exit.
if test -z "$init_rootfs"; then
exit 0
fi

# Use $testing_rootfs as the rootfs for testing.
rm -fr "$testing_rootfs"
mkdir "$testing_rootfs"

# If rootfs is Dockerfile, create directory from Dockerfile.
if test "$(basename "$init_rootfs")" = "Dockerfile"; then
image_name="uk-{name}"
d=$(pwd)
cd {init_dir}
docker build -o "$testing_rootfs" -f "$init_rootfs" -t "$image_name" .
cd "$d"
else
cp -r "$init_rootfs"/* "$testing_rootfs"
fi

# Create CPIO archive to be used as the embedded initrd.
{base}/unikraft/support/scripts/mkcpio {app_dir}/initrd.cpio "$testing_rootfs"
23 changes: 0 additions & 23 deletions utils/new-design/tpl_build_fs.sh

This file was deleted.

10 changes: 0 additions & 10 deletions utils/new-design/tpl_build_kraft.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
#!/bin/sh

init_rootfs="{rootfs}"
rootfs="{target_dir}/rootfs"

if test ! -z "$rootfs"; then
if test ! "$(basename "$init_rootfs")" = "Dockerfile"; then
rm -fr "$rootfs"
cp -r "$init_rootfs" "$rootfs"
fi
fi

rm -fr {target_dir}/.unikraft/build
rm -f {target_dir}/.config.*
kraft build --log-level debug --log-type basic --no-cache --no-update --plat {plat} --arch {arch} {target_dir}
21 changes: 0 additions & 21 deletions utils/new-design/tpl_build_make_einitrd.sh
Original file line number Diff line number Diff line change
@@ -1,26 +1,5 @@
#!/bin/sh

init_rootfs="{rootfs}"

# Use "rootfs" as the actual rootfs directory.
rootfs="{target_dir}/rootfs"
rm -fr "$rootfs"
mkdir "$rootfs"

# If rootfs is Dockerfile, create directory from Dockerfile.
if test "$(basename "$init_rootfs")" = "Dockerfile"; then
image_name="uk-{name}"
d=$(pwd)
cd {init_dir}
docker build -o "$rootfs" -f "$init_rootfs" -t "$image_name" .
cd "$d"
else
cp -r "$init_rootfs"/* "$rootfs"
fi

# Create CPIO archive to be used as the embedded initrd.
{base}/unikraft/support/scripts/mkcpio {target_dir}/initrd.cpio "$rootfs"

test -d {target_dir}/.unikraft/build || mkdir -p {target_dir}/.unikraft/build
make -f {target_dir}/Makefile distclean
UK_DEFCONFIG={target_dir}/defconfig make -f {target_dir}/Makefile defconfig
Expand Down
5 changes: 0 additions & 5 deletions utils/new-design/tpl_run_firecracker_net_bridge_initrd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ sudo ip link set dev virbr0@if0 master virbr0
# Add IP address to bridge interface
sudo ip address add 172.44.0.1/24 dev virbr0

rootfs={target_dir}/rootfs

# Create CPIO archive to be used as the initrd.
{base}/unikraft/support/scripts/mkcpio {run_dir}/initrd.cpio "$rootfs"

# Remove previously created files.
sudo rm -f /tmp/firecracker.log
touch /tmp/firecracker.log
Expand Down
2 changes: 1 addition & 1 deletion utils/new-design/tpl_run_firecracker_net_tap_initrd.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"boot-source": {{
"kernel_image_path": "{kernel}",
"boot_args": "{name} netdev.ip=172.44.0.2/24:172.44.0.1::: vfs.fstab=[ \"initrd0:/:extract::ramfs=1:\" ] -- {cmd}",
"initrd_path": "{run_dir}/initrd.cpio"
"initrd_path": "{app_dir}/initrd.cpio"
}},
"drives": [],
"machine-config": {{
Expand Down
2 changes: 1 addition & 1 deletion utils/new-design/tpl_run_firecracker_nonet_initrd.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"boot-source": {{
"kernel_image_path": "{kernel}",
"boot_args": "{name} vfs.fstab=[ \"initrd0:/:extract::ramfs=1:\" ] -- {cmd}",
"initrd_path": "{run_dir}/initrd.cpio"
"initrd_path": "{app_dir}/initrd.cpio"
}},
"drives": [],
"machine-config": {{
Expand Down
5 changes: 0 additions & 5 deletions utils/new-design/tpl_run_firecracker_nonet_initrd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ sudo KRAFTKIT_NO_WARN_SUDO=1 kraft stop --all
sudo KRAFTKIT_NO_WARN_SUDO=1 kraft rm --all
}} > /dev/null 2>&1

rootfs={target_dir}/rootfs

# Create CPIO archive to be used as the initrd.
{base}/unikraft/support/scripts/mkcpio {run_dir}/initrd.cpio "$rootfs"

# Remove previously created files.
sudo rm -f /tmp/firecracker.log
touch /tmp/firecracker.log
Expand Down
6 changes: 1 addition & 5 deletions utils/new-design/tpl_run_qemu_net_bridge_initrd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ sudo ip link add dev virbr0 type bridge
sudo ip address add 172.44.0.1/24 dev virbr0
sudo ip link set dev virbr0 up

rootfs={target_dir}/rootfs

# Create CPIO archive to be used as the initrd.
{base}/unikraft/support/scripts/mkcpio {run_dir}/initrd.cpio "$rootfs"
sudo {vmm} \
{hypervisor_option} \
{machine} \
Expand All @@ -42,5 +38,5 @@ sudo {vmm} \
-m {memory}M \
-netdev bridge,id=en0,br=virbr0 -device virtio-net-pci,netdev=en0 \
-append "{name} netdev.ip=172.44.0.2/24:172.44.0.1::: vfs.fstab=[ \"initrd0:/:extract::ramfs=1:\" ] -- $cmd" \
-initrd {run_dir}/initrd.cpio \
-initrd {app_dir}/initrd.cpio \
-cpu max
6 changes: 1 addition & 5 deletions utils/new-design/tpl_run_qemu_net_nat_initrd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ kraft stop --all
kraft rm --all
}} > /dev/null 2>&1

rootfs={target_dir}/rootfs

# Create CPIO archive to be used as the initrd.
{base}/unikraft/support/scripts/mkcpio {run_dir}/initrd.cpio "$rootfs"
{vmm} \
{hypervisor_option} \
{machine} \
Expand All @@ -28,5 +24,5 @@ rootfs={target_dir}/rootfs
-device virtio-net-pci,mac=02:b0:b0:1d:be:01,netdev=hostnet0 \
-netdev user,id=hostnet0,hostfwd=tcp::{port_ext}-:{port_int} \
-append "{name} vfs.fstab=[ \"initrd0:/:extract::ramfs=1:\" ] -- $cmd" \
-initrd {run_dir}/initrd.cpio \
-initrd {app_dir}/initrd.cpio \
-cpu max
6 changes: 1 addition & 5 deletions utils/new-design/tpl_run_qemu_nonet_initrd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,12 @@ kraft stop --all
kraft rm --all
}} > /dev/null 2>&1

rootfs={target_dir}/rootfs

# Create CPIO archive to be used as the initrd.
{base}/unikraft/support/scripts/mkcpio {run_dir}/initrd.cpio "$rootfs"
{vmm} \
{hypervisor_option} \
{machine} \
-kernel "$kernel" \
-nographic \
-m {memory}M \
-append "{name} vfs.fstab=[ \"initrd0:/:extract::ramfs=1:\" ] -- $cmd" \
-initrd {run_dir}/initrd.cpio \
-initrd {app_dir}/initrd.cpio \
-cpu max

0 comments on commit 7c11bf7

Please sign in to comment.