Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tools/sandbox changes #4609

Merged
merged 5 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions tests/framework/microvm_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ class MicrovmHelpers:

# keep track of assigned subnets
shared_subnet_ctr = 0
_supernet = ipaddress.IPv4Network("10.0.0.0/16")
# Try not to collide with anything by using the last /16 of the 10.x.x.x
# private block
_supernet = ipaddress.IPv4Network("10.255.0.0/16")
_subnets_gen = _supernet.subnets(new_prefix=30)

def __init__(self, vm):
Expand Down Expand Up @@ -110,9 +112,11 @@ def how_to_ssh(self):
ip = self.vm.iface["eth0"]["iface"].guest_ip
return f"{self.vm.netns.cmd_prefix()} ssh -o StrictHostKeyChecking=no -i {self.vm.ssh_key} root@{ip}"

def tmux_ssh(self):
def tmux_ssh(self, cmd=""):
"""Open a tmux window with an SSH session to the VM"""
return self.tmux_neww(self.how_to_ssh())
if len(cmd) > 0:
cmd = f" {cmd}"
return self.tmux_neww(self.how_to_ssh() + cmd)

def enable_console(self):
"""Helper method to attach a console, before the machine boots"""
Expand All @@ -138,9 +142,6 @@ def how_to_docker(self):

def enable_ip_forwarding(self, iface="eth0"):
"""Enables IP forwarding in the guest"""
if DOCKER.in_docker:
docker_apt_install("iptables")

i = MicrovmHelpers.shared_subnet_ctr
MicrovmHelpers.shared_subnet_ctr += 1
netns = self.vm.netns.id
Expand Down
27 changes: 25 additions & 2 deletions tools/devtool
Original file line number Diff line number Diff line change
Expand Up @@ -413,8 +413,11 @@ cmd_help() {
test_debug [-- [<pytest args>]]
Run tests in a debugging environment

test_sandbox
Run Firecracker in an IPython REPL
sandbox
Run Firecracker in an IPython REPL (in devctr)

sandbox_native
Run Firecracker in an IPython REPL (AL2023/Ubuntu)

mkdocs
Use 'cargo doc' to generate rustdoc documentation
Expand Down Expand Up @@ -851,6 +854,26 @@ cmd_sandbox() {
cmd_sh "tmux new env PYTEST_ADDOPTS=--pdbcls=IPython.terminal.debugger:TerminalPdb PYTHONPATH=tests IPYTHONDIR=\$PWD/.ipython ipython -i ./tools/sandbox.py $@"
}

cmd_sandbox_native() {
cmd_build --release

source /etc/os-release
case $ID-$VERSION_ID in
ubuntu-22.04)
sudo apt install python3-pip python3.11-dev gcc tmux
;;
al2023)
sudo yum -y install python3.11-pip python3.11-devel gcc tmux
;;
esac
python3.11 -m venv sandbox
source sandbox/bin/activate
pip3.11 install ipython requests requests_unixsocket "urllib3<2" psutil tenacity filelock
pip3.11 install packaging pytest
ensure_ci_artifacts
tmux neww sudo --preserve-env=HOME,PATH,TMUX env PYTHONPATH=tests IPYTHONDIR=\$PWD/.ipython ipython -i ./tools/sandbox.py $@
}

cmd_test_debug() {
cmd_sh "tmux new ./tools/test.sh --pdb $@"
}
Expand Down
12 changes: 11 additions & 1 deletion tools/sandbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"""

import argparse
import json
import re
from pathlib import Path

Expand Down Expand Up @@ -36,12 +37,14 @@ def parse_byte_size(param):
parser = argparse.ArgumentParser()
parser.add_argument(
"--kernel",
type=Path,
choices=kernels,
default=kernels[-1],
help=f"Kernel to use. [{kernels[-1]}]",
)
parser.add_argument(
"--rootfs",
type=Path,
choices=rootfs,
default=rootfs[-1],
help=f"Rootfs to use. [{rootfs[-1]}]",
Expand All @@ -54,6 +57,7 @@ def parse_byte_size(param):
)
parser.add_argument("--rootfs-size", type=parse_byte_size, default=1 * 2**30) # 1GB
parser.add_argument("--binary-dir", help="Path to the firecracker binaries")
parser.add_argument("--cpu-template-path", help="CPU template to use", type=Path)
args = parser.parse_args()
print(args)

Expand All @@ -65,13 +69,19 @@ def parse_byte_size(param):
bins = get_firecracker_binaries()

print("This step may take a while to compile Firecracker ...")
cpu_template = None
if args.cpu_template_path is not None:
cpu_template = json.loads(args.cpu_template_path.read_text())
vmfcty = MicroVMFactory(*bins)
uvm = vmfcty.build(args.kernel, args.rootfs)
uvm.help.enable_console()
uvm.help.resize_disk(uvm.rootfs_file, args.rootfs_size)
uvm.spawn()
uvm.spawn(log_show_level=True)
uvm.help.print_log()
uvm.add_net_iface()
uvm.basic_config(vcpu_count=args.vcpus, mem_size_mib=args.guest_mem_size // 2**20)
if cpu_template is not None:
uvm.api.cpu_config.put(**cpu_template)
print(cpu_template)
uvm.start()
uvm.get_all_metrics()
Loading