diff --git a/utils/new-design/all.py b/utils/new-design/all.py index 3359ec69..b9d094ab 100644 --- a/utils/new-design/all.py +++ b/utils/new-design/all.py @@ -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) @@ -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(): @@ -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: @@ -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"]) @@ -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.""" @@ -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() @@ -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: @@ -1120,6 +1125,7 @@ def main(): t = TesterConfig(sys.argv[1]) a = AppConfig() + a.generate_init(t) s = SystemConfig() copy_common() @@ -1128,6 +1134,5 @@ def main(): for t in targets: t.generate() - if __name__ == "__main__": sys.exit(main()) diff --git a/utils/new-design/tpl_app_fs_init.sh b/utils/new-design/tpl_app_fs_init.sh new file mode 100644 index 00000000..178b2c74 --- /dev/null +++ b/utils/new-design/tpl_app_fs_init.sh @@ -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" diff --git a/utils/new-design/tpl_build_fs.sh b/utils/new-design/tpl_build_fs.sh deleted file mode 100644 index c6fe5db7..00000000 --- a/utils/new-design/tpl_build_fs.sh +++ /dev/null @@ -1,23 +0,0 @@ -#!/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 - rootfs="$init_rootfs" - cp -r "$init_rootfs"/* "$rootfs" -fi - -# Create CPIO archive to be used as the embedded initrd. -{base}/unikraft/support/scripts/mkcpio {app_dir}/initrd.cpio "$rootfs" diff --git a/utils/new-design/tpl_build_kraft.sh b/utils/new-design/tpl_build_kraft.sh index 969181e5..28f5dc73 100644 --- a/utils/new-design/tpl_build_kraft.sh +++ b/utils/new-design/tpl_build_kraft.sh @@ -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} diff --git a/utils/new-design/tpl_build_make_einitrd.sh b/utils/new-design/tpl_build_make_einitrd.sh index 484f2233..f6b3b42f 100644 --- a/utils/new-design/tpl_build_make_einitrd.sh +++ b/utils/new-design/tpl_build_make_einitrd.sh @@ -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 diff --git a/utils/new-design/tpl_run_firecracker_net_bridge_initrd.sh b/utils/new-design/tpl_run_firecracker_net_bridge_initrd.sh index 5658e71c..78af186c 100644 --- a/utils/new-design/tpl_run_firecracker_net_bridge_initrd.sh +++ b/utils/new-design/tpl_run_firecracker_net_bridge_initrd.sh @@ -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 diff --git a/utils/new-design/tpl_run_firecracker_net_tap_initrd.json b/utils/new-design/tpl_run_firecracker_net_tap_initrd.json index adcab17a..0e56ec0c 100644 --- a/utils/new-design/tpl_run_firecracker_net_tap_initrd.json +++ b/utils/new-design/tpl_run_firecracker_net_tap_initrd.json @@ -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": {{ diff --git a/utils/new-design/tpl_run_firecracker_nonet_initrd.json b/utils/new-design/tpl_run_firecracker_nonet_initrd.json index f4447463..9b65e88c 100644 --- a/utils/new-design/tpl_run_firecracker_nonet_initrd.json +++ b/utils/new-design/tpl_run_firecracker_nonet_initrd.json @@ -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": {{ diff --git a/utils/new-design/tpl_run_firecracker_nonet_initrd.sh b/utils/new-design/tpl_run_firecracker_nonet_initrd.sh index 46426c1d..45242f54 100644 --- a/utils/new-design/tpl_run_firecracker_nonet_initrd.sh +++ b/utils/new-design/tpl_run_firecracker_nonet_initrd.sh @@ -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 diff --git a/utils/new-design/tpl_run_qemu_net_bridge_initrd.sh b/utils/new-design/tpl_run_qemu_net_bridge_initrd.sh index 0a36399b..9beefc5c 100644 --- a/utils/new-design/tpl_run_qemu_net_bridge_initrd.sh +++ b/utils/new-design/tpl_run_qemu_net_bridge_initrd.sh @@ -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} \ @@ -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 diff --git a/utils/new-design/tpl_run_qemu_net_nat_initrd.sh b/utils/new-design/tpl_run_qemu_net_nat_initrd.sh index 65d181d9..9264f8cd 100644 --- a/utils/new-design/tpl_run_qemu_net_nat_initrd.sh +++ b/utils/new-design/tpl_run_qemu_net_nat_initrd.sh @@ -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} \ @@ -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 diff --git a/utils/new-design/tpl_run_qemu_nonet_initrd.sh b/utils/new-design/tpl_run_qemu_nonet_initrd.sh index d823daaf..2c596859 100644 --- a/utils/new-design/tpl_run_qemu_nonet_initrd.sh +++ b/utils/new-design/tpl_run_qemu_nonet_initrd.sh @@ -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} \ @@ -26,5 +22,5 @@ rootfs={target_dir}/rootfs -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