8000 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

Conversation

jimc
Copy link
Contributor
@jimc jimc commented Mar 19, 2025

report vng-init version info, as written to src/version.rs, by ../setup.py, using version.py's notion of VERSION.

this yields:

[jimc@gandalf virtme-ng]$ ./virtme/guest/bin/virtme-ng-init virtme-ng-init: must be run as PID 1
virtme-ng-init: Build Information:
virtme-ng-init: Package Name: virtme-ng-init
virtme-ng-init: Package Version: 1.33+33.g5893a4b.dirty
virtme-ng-init: Manifest Directory: /home/jimc/projects/virtme-ng/virtme_ng_init

or:
_ _
__ ()_ | | _ __ ___ ___ _ __ __ _
\ \ / / | | | _ _ \ / _ ____| _ \ / _ |
\ V /| | | | |
| | | | | | /
| | | | (| |
_/ |
|| _|| || |_|_
| || ||__ |
|___/
kernel version: 6.14.0-rc7-dd-00059-gb4d7289f3f54 x86_64
virtme-ng-init version: 1.33+34.g723e552.dirty
(CTRL+d to exit)

So far, its only useful for blaming, but theres room for more details, if/when any useful ones become apparent.

@jimc jimc force-pushed the pid-gt-1 branch 2 times, most recently from 66bd4a8 to 0e4a0df Compare March 19, 2025 05:01
@jimc
Copy link
Contributor Author
jimc commented Mar 20, 2025

ok. this is probably no go.
setyp.py writes version.rs into the subdir.
so its a build artifact.
but then its not in the repo for clean builds everwhere.

@jimc jimc force-pushed the pid-gt-1 branch 3 times, most recently from c3ec9be to 8061617 Compare March 28, 2025 04:10
# Generate Rust version file
version_rs_path = pathlib.Path("virtme_ng_init/src/version.rs")
version_rs_path.parent.mkdir(parents=True, exist_ok=True)
version_rs_path.write_text(f'pub const VERSION: &str = "{VERSION}";\n')
8000 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::version::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

@jimc jimc force-pushed the pid-gt-1 branch 2 times, most recently from c60e63d to cd8d9ad Compare April 7, 2025 14:46
report virtme-ng-init version info, as written to target/version.rs,
by ../setup.py, using version.py's notion of VERSION.

this yields:

[jimc@gandalf virtme-ng]$ ./virtme/guest/bin/virtme-ng-init
virtme-ng-init: must be run as PID 1
virtme-ng-init: Build Information:
virtme-ng-init:   Package Name: virtme-ng-init
virtme-ng-init:   Package Version: 1.33+33.g5893a4b.dirty
virtme-ng-init:   Manifest Directory: /home/jimc/projects/virtme-ng/virtme_ng_init

or:
          _      _
   __   _(_)_ __| |_ _ __ ___   ___       _ __   __ _
   \ \ / / |  __| __|  _   _ \ / _ \_____|  _ \ / _  |
    \ V /| | |  | |_| | | | | |  __/_____| | | | (_| |
     \_/ |_|_|   \__|_| |_| |_|\___|     |_| |_|\__  |
                                                |___/
   version: 1.33+34.g723e552.dirty
   kernel:  6.14.0-rc7-dd-00059-gb4d7289f3f54 x86_64
   (CTRL+d to exit)

So far, the info is mostly useful for blaming, but theres room for
more details, if/when any other useful ones become apparent.

Signed-off-by: Jim Cromie <jim.cromie@gmail.com>
---
v2 - version in target, to avoid repo/checksum/build/CI conflicts
@jimc
Copy link
Contributor Author
jimc commented Apr 7, 2025

So it appears that setup.py is not run 1st as part of CI

it writes the target/version.rs file, which must be there for the compile to succeed.

Can someone conjure a fix ?

# Generate Rust version file
version_rs_path = pathlib.Path("virtme_ng_init/src/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.

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

@jimc
Copy link
Contributor Author
jimc commented Apr 7, 2025

can we depend upon package variables ?

use std::env;
use std::fs;
use std::path::Path;

fn main() {
    // This is a simplified example - you'd need a robust way
    // to get the VERSION from your Python setup.py

    // For demonstration, let's hardcode a version or get it from an env var
    let version = env::var("CARGO_PKG_VERSION").unwrap_or("0.1.0");
    let out_dir = env::var("OUT_DIR").unwrap();
    let dest_path = Path::new(&out_dir).join("version.rs");

    fs::write(
        &dest_path,
        format!("pub const VERSION: &str = \"{}\";\n", version),
    )
    .unwrap();

    println!("cargo:rustc-env=BUILD_VERSION={}", version);
    println!("cargo:rustc-cfg=build_version=\"{}\"", version);
}

@jimc
Copy link
Contributor Author
jimc commented Apr 7, 2025 via email

@matttbe
Copy link
Collaborator
matttbe commented Apr 7, 2025

can we depend upon package variables ?

That's different, that will be the version of the init program, no?

Maybe we could also set an env var containing VERSION at build time in setup.py and use it in build.rs?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0