Skip to content

Commit

Permalink
new-design: Add Firecracker run support
Browse files Browse the repository at this point in the history
Add Firecracker template scripts and support in generating run scripts
and configuration files (JSON).

Signed-off-by: Razvan Deaconescu <[email protected]>
  • Loading branch information
razvand committed Sep 14, 2024
1 parent b47d398 commit b8bd606
Show file tree
Hide file tree
Showing 15 changed files with 448 additions and 9 deletions.
33 changes: 25 additions & 8 deletions utils/new-design/all.py
Original file line number Diff line number Diff line change
Expand Up @@ -631,8 +631,8 @@ def __init__(self, base_dir, config, target_config, app_config):
self.config = config
self.target_config = target_config
self.app_config = app_config
self.kernel_name = None
self.kernel_path = None
self.kernel_name = f"{self.app_config.config['name']}_{self.config['platform']}-{self.config['arch']}"
self.kernel_path = os.path.join(os.path.join(os.path.join(self.dir, ".unikraft"), "build"), self.kernel_name)

def get_build_tools(plat, arch):
return ["make", "kraft"]
Expand Down Expand Up @@ -912,8 +912,6 @@ def generate(self):
else:
self._generate_build_make()
self._generate_run_kraftfile()
self.kernel_name = f"{self.app_config.config['name']}_{self.config['platform']}-{self.config['arch']}"
self.kernel_path = os.path.join(os.path.join(os.path.join(self.dir, ".unikraft"), "build"), self.kernel_name)
else:
self._generate_fs()
elif self.config['build_tool'] == 'kraft':
Expand All @@ -933,7 +931,7 @@ def __init__(self, base_dir, config, target_config, build_config, app_config):
def get_run_tools(plat, arch):
return ["vmm", "kraft"]

def _generate_run_script_from_template(self, template_name):
def _generate_from_template(self, template_name, output_name):
with open(os.path.join(SCRIPT_DIR, template_name), "r", encoding="utf-8") as stream:
raw_content = stream.read()

Expand Down Expand Up @@ -962,12 +960,31 @@ def _generate_run_script_from_template(self, template_name):

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

with open(os.path.join(self.dir, "run"), "w", encoding="utf-8") as stream:
with open(os.path.join(self.dir, output_name), "w", encoding="utf-8") as stream:
stream.write(content)

def _generate_fc_config_from_template(self, template_name):
self._generate_from_template(template_name, "config.json")

def _generate_run_script_from_template(self, template_name):
self._generate_from_template(template_name, "run")
os.chmod(os.path.join(self.dir, "run"), 0o755)

def _generate_firecracker(self):
pass
if self.app_config.has_einitrd():
if self.config["networking"] == "none":
self._generate_fc_config_from_template(f"tpl_run_firecracker_nonet_noinitrd.json")
self._generate_run_script_from_template(f"tpl_run_firecracker_nonet_noinitrd.sh")
else:
self._generate_fc_config_from_template(f"tpl_run_firecracker_net_{self.config['networking']}_noinitrd.json")
self._generate_run_script_from_template(f"tpl_run_firecracker_net_{self.config['networking']}_noinitrd.sh")
else:
if self.config["networking"] == "none":
self._generate_fc_config_from_template(f"tpl_run_firecracker_nonet_initrd.json")
self._generate_run_script_from_template(f"tpl_run_firecracker_nonet_initrd.sh")
else:
self._generate_fc_config_from_template(f"tpl_run_firecracker_net_{self.config['networking']}_initrd.json")
self._generate_run_script_from_template(f"tpl_run_firecracker_net_{self.config['networking']}_initrd.sh")

def _generate_qemu(self):
if self.app_config.has_einitrd():
Expand All @@ -985,7 +1002,7 @@ def _generate_xen(self):
pass

def _generate_kraft(self):
if self.config["networking"] == "none" or self.config["networking"] == "tap":
if self.config["networking"] == "none":
self._generate_run_script_from_template(f"tpl_run_kraft_nonet.sh")
else:
self._generate_run_script_from_template(f"tpl_run_kraft_net_{self.config['networking']}.sh")
Expand Down
7 changes: 6 additions & 1 deletion utils/new-design/tester.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ variants:
- bridge
- tap
exclude_variants:
- networking: bridge
- networking: nat
platform: fc
- networking: tap
platform: not fc
Expand All @@ -55,6 +55,11 @@ exclude_variants:
- platform: fc
rootfs: 9pfs
- bootloader: uefi
- networking: nat
run_tool: kraft
platform: not qemu
- networking: tap
run_tool: kraft
- debug:
- info
- err
Expand Down
33 changes: 33 additions & 0 deletions utils/new-design/tpl_run_firecracker_net_bridge_initrd.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{{
"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"
}},
"drives": [],
"machine-config": {{
"vcpu_count": 1,
"mem_size_mib": 8,
"smt": false,
"track_dirty_pages": false
}},
"cpu-config": null,
"balloon": null,
"network-interfaces": [
{{
"iface_id": "virbr0",
"guest_mac": "06:00:ac:10:00:02",
"host_dev_name": "virbr0@if0"
}}
],
"vsock": null,
"logger": {{
"log_path": "/tmp/firecracker.log",
"level": "Debug",
"show_level": true,
"show_log_origin": true
}},
"metrics": null,
"mmds-config": null,
"entropy": null
}}
46 changes: 46 additions & 0 deletions utils/new-design/tpl_run_firecracker_net_bridge_initrd.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/bin/sh

config={run_dir}/config.json

{{
# Clean up any previous instances.
sudo pkill -f qemu-system
sudo pkill -f firecracker
kraft stop --all
kraft rm --all
sudo KRAFTKIT_NO_WARN_SUDO=1 kraft stop --all
sudo KRAFTKIT_NO_WARN_SUDO=1 kraft rm --all
}} > /dev/null 2>&1

{{
# Remove previously created network interfaces.
sudo ip link set dev tap0 down
sudo ip link del dev tap0
sudo ip link set dev virbr0 down
sudo ip link del dev virbr0
}} > /dev/null 2>&1

# Create bridge and tap interface for Firecracker networking.
sudo ip link add dev virbr0 type bridge
sudo ip link set dev virbr0 up
sudo ip tuntap add dev virbr0@if0 mode tap
sudo ip link set dev virbr0@if0 up

# Add tap interface to bridge.
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={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
sudo rm -f /tmp/firecracker.socket
sudo {vmm} \
--api-sock /tmp/firecracker.socket \
--config-file "$config"
32 changes: 32 additions & 0 deletions utils/new-design/tpl_run_firecracker_net_bridge_noinitrd.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{{
"boot-source": {{
"kernel_image_path": "{kernel}",
"boot_args": "{name} netdev.ip=172.44.0.2/24:172.44.0.1::: -- {cmd}"
}},
"drives": [],
"machine-config": {{
"vcpu_count": 1,
"mem_size_mib": 8,
"smt": false,
"track_dirty_pages": false
}},
"cpu-config": null,
"balloon": null,
"network-interfaces": [
{{
"iface_id": "virbr0",
"guest_mac": "06:00:ac:10:00:02",
"host_dev_name": "virbr0@if0"
}}
],
"vsock": null,
"logger": {{
"log_path": "/tmp/firecracker.log",
"level": "Debug",
"show_level": true,
"show_log_origin": true
}},
"metrics": null,
"mmds-config": null,
"entropy": null
}}
41 changes: 41 additions & 0 deletions utils/new-design/tpl_run_firecracker_net_bridge_noinitrd.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/bin/sh

config={run_dir}/config.json

{{
# Clean up any previous instances.
sudo pkill -f qemu-system
sudo pkill -f firecracker
kraft stop --all
kraft rm --all
sudo KRAFTKIT_NO_WARN_SUDO=1 kraft stop --all
sudo KRAFTKIT_NO_WARN_SUDO=1 kraft rm --all
}} > /dev/null 2>&1

{{
# Remove previously created network interfaces.
sudo ip link set dev tap0 down
sudo ip link del dev tap0
sudo ip link set dev virbr0 down
sudo ip link del dev virbr0
}} > /dev/null 2>&1

# Create bridge and tap interface for Firecracker networking.
sudo ip link add dev virbr0 type bridge
sudo ip link set dev virbr0 up
sudo ip tuntap add dev virbr0@if0 mode tap
sudo ip link set dev virbr0@if0 up

# Add tap interface to bridge.
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

# Remove previously created files.
sudo rm -f /tmp/firecracker.log
touch /tmp/firecracker.log
sudo rm -f /tmp/firecracker.socket
sudo {vmm} \
--api-sock /tmp/firecracker.socket \
--config-file "$config"
33 changes: 33 additions & 0 deletions utils/new-design/tpl_run_firecracker_net_tap_initrd.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{{
"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"
}},
"drives": [],
"machine-config": {{
"vcpu_count": 1,
"mem_size_mib": 8,
"smt": false,
"track_dirty_pages": false
}},
"cpu-config": null,
"balloon": null,
"network-interfaces": [
{{
"iface_id": "net1",
"guest_mac": "06:00:ac:10:00:02",
"host_dev_name": "tap0"
}}
],
"vsock": null,
"logger": {{
"log_path": "/tmp/firecracker.log",
"level": "Debug",
"show_level": true,
"show_log_origin": true
}},
"metrics": null,
"mmds-config": null,
"entropy": null
}}
34 changes: 34 additions & 0 deletions utils/new-design/tpl_run_firecracker_net_tap_initrd.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/sh

config={run_dir}/config.json

{{
# Clean up any previous instances.
sudo pkill -f qemu-system
sudo pkill -f firecracker
kraft stop --all
kraft rm --all
sudo KRAFTKIT_NO_WARN_SUDO=1 kraft stop --all
sudo KRAFTKIT_NO_WARN_SUDO=1 kraft rm --all
}} > /dev/null 2>&1

{{
# Remove previously created network interfaces.
sudo ip link set dev tap0 down
sudo ip link del dev tap0
sudo ip link set dev virbr0 down
sudo ip link del dev virbr0
}} > /dev/null 2>&1

# Create tap interface for Firecracker networking.
sudo ip tuntap add dev tap0 mode tap
sudo ip address add 172.44.0.1/24 dev tap0
sudo ip link set dev tap0 up

# Remove previously created files.
sudo rm -f /tmp/firecracker.log
touch /tmp/firecracker.log
sudo rm -f /tmp/firecracker.socket
sudo {vmm} \
--api-sock /tmp/firecracker.socket \
--config-file "$config"
32 changes: 32 additions & 0 deletions utils/new-design/tpl_run_firecracker_net_tap_noinitrd.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{{
"boot-source": {{
"kernel_image_path": "{kernel}",
"boot_args": "{name} netdev.ip=172.44.0.2/24:172.44.0.1::: -- {cmd}"
}},
"drives": [],
"machine-config": {{
"vcpu_count": 1,
"mem_size_mib": 8,
"smt": false,
"track_dirty_pages": false
}},
"cpu-config": null,
"balloon": null,
"network-interfaces": [
{{
"iface_id": "net1",
"guest_mac": "06:00:ac:10:00:02",
"host_dev_name": "tap0"
}}
],
"vsock": null,
"logger": {{
"log_path": "/tmp/firecracker.log",
"level": "Debug",
"show_level": true,
"show_log_origin": true
}},
"metrics": null,
"mmds-config": null,
"entropy": null
}}
34 changes: 34 additions & 0 deletions utils/new-design/tpl_run_firecracker_net_tap_noinitrd.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/sh

config={run_dir}/config.json

{{
# Clean up any previous instances.
sudo pkill -f qemu-system
sudo pkill -f firecracker
kraft stop --all
kraft rm --all
sudo KRAFTKIT_NO_WARN_SUDO=1 kraft stop --all
sudo KRAFTKIT_NO_WARN_SUDO=1 kraft rm --all
}} > /dev/null 2>&1

{{
# Remove previously created network interfaces.
sudo ip link set dev tap0 down
sudo ip link del dev tap0
sudo ip link set dev virbr0 down
sudo ip link del dev virbr0
}} > /dev/null 2>&1

# Create tap interface for Firecracker networking.
sudo ip tuntap add dev tap0 mode tap
sudo ip address add 172.44.0.1/24 dev tap0
sudo ip link set dev tap0 up

# Remove previously created files.
sudo rm -f /tmp/firecracker.log
touch /tmp/firecracker.log
sudo rm -f /tmp/firecracker.socket
sudo {vmm} \
--api-sock /tmp/firecracker.socket \
--config-file "$config"
Loading

0 comments on commit b8bd606

Please sign in to comment.