10000 virtme-ng-init: add some useful info when PID>1 by jimc · Pull Request #256 · arighi/virtme-ng · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

virtme-ng-init: add some useful info when PID>1 #256

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
8000
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ debian/virtme-ng.postinst.debhelper
debian/virtme-ng.prerm.debhelper
debian/virtme-ng.substvars
virtme_ng_init/target
virtme_ng_init/target/version.rs
virtme/guest/.crate*
virtme/guest/bin
6 changes: 6 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python3

import os
import pathlib
import platform
import subprocess
import sys
Expand Down Expand Up @@ -50,6 +51,11 @@
"RUSTFLAGS", ""
)

# Generate Rust version file
version_rs_path = pathlib.Path("virtme_ng_init/target/version.rs")
version_rs_path.parent.mkdir(parents=True, exist_ok=True)
version_rs_path.write_text(f'pub const VERSION: &str = "{VERSION}";\n')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will it write this info in the source code, modifying the tracked file?

That's a good idea to pass the version, but can we not pass the info differently? e.g. passing a var when compiling the program?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. setup.py writes the whole file.

I fiddled with trying to get env at compile-time, but that env isnt a hash, so I cant just inspect it for useful datums.
Some AI showed me a long list of things, not many looked useful. I settled on 3 as a POC.

FWIW, the placeholder version serves as a sentinel,
making clear that something is off with the build process.
But it does muddle the meaning of "clean source"

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If that's not easily possible, an env var could be passed to the VM.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

following up on this, I asked the LLM:

Ive done the placeholder version like you suggested, but it opens another problem: the build process now alters the checked-out code, so it no longer would match a checksum baseline. Some CI might barf on that.

Heres its (slightly solicitous) response: It sounds plausible to me, but this is like week 2 of my rust world. Anyone foresee any troubles ??

You've hit upon a very important point! Creating a placeholder version.rs and then having the build process overwrite it does indeed introduce the problem of altering checked-out code, which can cause checksum mismatches and CI failures.

Let's address this issue with a solution that avoids modifying checked-out code:

The Solution: Generate version.rs in the target Directory

Instead of generating version.rs in the src directory, we can generate it in the target directory, which is already ignored by Git and intended for build artifacts.

Here's how to implement this solution:

Modify setup.py:

Change the version_rs_path to point to the target directory:

Python

version_rs_path = pathlib.Path("virtme_ng_init/target/version.rs")
Ensure the directory exists:

Python

version_rs_path.parent.mkdir(parents=True, exist_ok=True)
Generate the version.rs file in the target directory:

Python

version_rs_path.write_text(f'pub const VERSION: &str = "{VERSION}";\n')
Modify virtme_ng_init/src/main.rs:

Change the mod version; declaration to point to the target directory:

Rust

mod version;
use crate::versio 8000 n::VERSION;
Update .gitignore:

Ensure that the virtme_ng_init/target/version.rs file is ignored by Git.
The virtme_ng_init/target directory is already ignored, so there should be nothing to do.
Explanation

By generating version.rs in the target directory, we avoid modifying checked-out code.
The target directory is intended for build artifacts and is already ignored by Git, so it won't affect checksums or CI.
We change the mod declaration to mod version; and then use crate::version::VERSION to tell rust to look for the module in the same crate, which is the default behavior.
Benefits

No Modification of Checked-Out Code: This resolves the checksum and CI issues.
Clean Build: The build process generates files in the appropriate location.
Compatibility: This approach should be compatible with most CI systems.
By generating version.rs in the target directory, you can resolve the pre-commit rejection and avoid modifying checked-out code.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know Rust well, (maybe @arighi can help here? :) ) but I think the idea is to add a build.rs file in virtme_ng_init/ that will execute python3 virtme_ng/version.py and set a build env var:

// build.rs
use std::process::Command;
fn main() {
    // note: add error checking yourself.
    let output = Command::new("python3").args(&["virtme_ng/version.py"]).output().unwrap();
    let version = String::from_utf8(output.stdout).unwrap();
    println!("cargo:rustc-env=VNG_VERSION={}", version);
}

Then in main.rs, you can use env!("VNG_VERSION")

Source: https://stackoverflow.com/a/44407625



class BuildPy(build_py):
def run(self):
Expand Down
16 changes: 15 additions & 1 deletion virtme_ng_init/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,22 @@ const SYSTEM_MOUNTS: &[MountInfo] = &[

const USER_SCRIPT: &str = "/run/tmp/.virtme-script";

mod version {
include!("../target/version.rs");
}
use crate::version::VERSION;

fn print_build_info() {
log!("Build Information:");
log!(" Package Name: {}", env!("CARGO_PKG_NAME"));
log!(" Package Version: {}", VERSION);
log!(" Manifest Directory: {}", env!("CARGO_MANIFEST_DIR"));
}

fn check_init_pid() {
if id() != 1 {
log!("must be run as PID 1");
print_build_info();
exit(1);
}
}
Expand Down Expand Up @@ -1101,7 +1114,8 @@ fn print_logo() {
\_/ |_|_| \__|_| |_| |_|\___| |_| |_|\__ |
|___/"#;
println!("{}", logo.trim_start_matches('\n'));
println!(" kernel version: {}", get_kernel_version(true));
println!(" version: {}", VERSION);
println!(" kernel: {}", get_kernel_version(true));
println!(" (CTRL+d to exit)\n");
}

Expand Down
Loading
0