-
Notifications
You must be signed in to change notification settings - Fork 36
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
rustvmm_gen: Introduce rustvmm_gen
#177
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool idea! I like having some centralized tools for dealing with all the bindings generation for our various crates. I do think the seccompiler-specific part should go into rust-vmm/seccompiler, however. What I'd find really useful would be to have some wrapper around bindgen-rs
that can be used to generate the bindings in crates like linux-loader and kvm-bindings (and I maybe xen-sys?) as well, built on top of the kernel header fetching you included here. Do you think that's a possible extension? :o
As a high level comment, maybe let's rename rustvmm_gen/
to just be scripts/
, or tools/
, and drop the lib_
prefix from the library files, instead moving them to some lib
subdirectory.
rustvmm_gen/lib_kernel_source.py
Outdated
def create_temp_dir(version): | ||
temp_dir = os.path.join("/tmp", f"linux-{version}-prepare") | ||
|
||
if os.path.exists(temp_dir): | ||
shutil.rmtree(temp_dir) | ||
|
||
try: | ||
os.makedirs(temp_dir) | ||
return temp_dir | ||
except OSError as e: | ||
raise RuntimeError(f"Failed to create temp directory: {e}") from e |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's use something that relies on mkdtemp(2)
here to avoid race conditions and the "delete if exists" dance: https://docs.python.org/3/library/tempfile.html#tempfile.TemporaryDirectory
rustvmm_gen/lib_kernel_source.py
Outdated
""" | ||
# Validate version format | ||
if not re.match(r"^\d+\.\d+(\.\d+)?$", version): | ||
return (False, "Invalid version format. Use X.Y or X.Y.Z") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this tuple of string and bool instead of just raising exceptions for errors, and returning void?
rustvmm_gen/lib_kernel_source.py
Outdated
|
||
|
||
def install_headers(src_dir, arch, install_path, cleanup=False): | ||
os.chdir(src_dir) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This never chdirs back. Maybe with contextlib.chdir(src_dir):
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed, changed to make -C
instead
rustvmm_gen/lib_syscall.py
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This script is specific to rust-vmm/seccompiler, right? If so it should probably live in that repository
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that lib file is specific to seccompiler
, and I'm going to write according lib file for kvm-bindings
and linux-loader
, all of them are going to need installed headers to continue, WDYT 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The generic parts, like downloading linux sources and installing headers, can live in rust-vmm-ci, and then there can be scripts in kvm-bindings/linux-leader/seccompiler that each call into the generic rust-vmm-ci scripts.
For kvm-bindings and linux-loader in particular there's probably some more common parts that can be moved into this repository (I was hoping some script that takes some .h files as input and produces .rs bindings from them?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we have the required headers, for kvm-bindings
and linux-loader
we are only one step away which is a invocation of bindgen
, and modify something (like for serde
support), IMO that's similar to seccompiler
while seccompiler
doesn't need bindgen
rustvmm_gen/lib_kernel_source.py
Outdated
print(f"Cleaning temporary files at {parent_dir}") | ||
shutil.rmtree(parent_dir) | ||
|
||
return True |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd also just drop the boolean return value here and simply raise an exception in the failure case
rustvmm_gen/lib_kernel_source.py
Outdated
stderr=subprocess.STDOUT, | ||
text=True, | ||
) | ||
print(result.stdout) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we just print result.stdout anyway, why not stdout=subprocess.PIPE
above?
rustvmm_gen/rustvmm_gen.py
Outdated
except Exception as e: | ||
print(f"\nError: {str(e)}") | ||
exit(1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not really a need for this try-catch + exit, I think. The call to this function is the final statement in main()
, so we hit an exception we'd exit with non-zero code anyway (and additionally printing "Error:" doesn't really add much imo), so can just drop this.
Sorry I forget to mark this as a draft, Thanks @roypat for reviewing! ❤️ |
Ahahhaah, so sorry for jumping the gun! Looking forward to it then :D |
99f5149
to
0686e7d
Compare
`kernel_source.py` contains functions used to check version of desired kernel, download, extract kernel source to specific directory and get the headers out of it. Signed-off-by: Ruoqing He <heruoqing@iscas.ac.cn>
`syscall.py` contains functions used to extract `syscall_table` from headers installed by `kernel_source.py` and generate according Rust code for specific architecture. Signed-off-by: Ruoqing He <heruoqing@iscas.ac.cn>
Add `__init__.py` for python programs outside `lib` to reference functions inside lib. Signed-off-by: Ruoqing He <heruoqing@iscas.ac.cn>
`rustvmm_gen.py` integrates kernel version checking, source extraction, compilation and header installation. Header files could be prepared with command: ```command ./rustvmm_gen/rustvmm_gen.py --arch <arm64/x86_64/riscv> --version <X.Y/X.Y.Z> prepare ``` Source code required by `seccompiler` could be generated with command: ```command ./rustvmm_gen/rustvmm_gen.py --arch <arm64/x86_64/riscv> --version <X.Y/X.Y.Z> generate_syscall ``` Signed-off-by: Ruoqing He <heruoqing@iscas.ac.cn>
0686e7d
to
d871f25
Compare
Summary of the PR
rustvmm_gen.py
integrates kernel version checking, source extraction, compilation and header installation. Header files could be prepared with command:Source code required by
seccompiler
could be generated with command:Requirements
Before submitting your PR, please make sure you addressed the following
requirements:
git commit -s
), and the commit message has max 60 characters for thesummary and max 75 characters for each description line.
test.
Release" section of CHANGELOG.md (if no such section exists, please create one).
unsafe
code is properly documented.