Skip to content

Commit b8225ef

Browse files
authored
chore: Reinstate linux_kernel_command_line package (#8240)
This package was removed in #7739 because there were no users, but it's actually a useful package that can be used to read kernel line arguments from Rust. Later PRs will use it. I did some optimization/simplification on the original code.
1 parent 7dc6083 commit b8225ef

File tree

8 files changed

+504
-16
lines changed

8 files changed

+504
-16
lines changed

Cargo.lock

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ members = [
180180
"rs/ic_os/guest_upgrade/server",
181181
"rs/ic_os/guest_upgrade/shared",
182182
"rs/ic_os/guest_upgrade/tests",
183+
"rs/ic_os/linux_kernel_command_line",
183184
"rs/ic_os/metrics_tool",
184185
"rs/ic_os/network",
185186
"rs/ic_os/nft_exporter",

rs/ic_os/dev_test_tools/setupos-disable-checks/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package(default_visibility = ["//rs:ic-os-pkg"])
55
DEPENDENCIES = [
66
# Keep sorted.
77
"//rs/ic_os/build_tools/partition_tools",
8+
"//rs/ic_os/linux_kernel_command_line",
89
"@crate_index//:anyhow",
910
"@crate_index//:clap",
1011
"@crate_index//:regex",

rs/ic_os/dev_test_tools/setupos-disable-checks/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ edition.workspace = true
77
anyhow = { workspace = true }
88
clap = { workspace = true }
99
indoc = { workspace = true }
10+
linux_kernel_command_line = { path = "../../linux_kernel_command_line" }
1011
partition_tools = { path = "../../build_tools/partition_tools" }
1112
regex = { workspace = true }
1213
tempfile = { workspace = true }

rs/ic_os/dev_test_tools/setupos-disable-checks/src/main.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
use anyhow::{Context, Error};
1+
use anyhow::{Context, Result};
22
use clap::Parser;
3+
use linux_kernel_command_line::KernelCommandLine;
34
use regex::Regex;
45
use std::fs;
56
use std::fs::Permissions;
67
use std::os::unix::fs::PermissionsExt;
78
use std::path::{Path, PathBuf};
9+
use std::str::FromStr;
810
use tempfile::NamedTempFile;
911

1012
use partition_tools::{Partition, ext::ExtPartition};
@@ -22,7 +24,7 @@ struct Cli {
2224
compat: bool,
2325
}
2426

25-
fn main() -> Result<(), Error> {
27+
fn main() -> Result<()> {
2628
let cli = Cli::parse();
2729
let boot_args_path = Path::new("/boot_args");
2830

@@ -39,7 +41,7 @@ fn main() -> Result<(), Error> {
3941
process_cmdline(
4042
std::str::from_utf8(&bootfs.read_file(boot_args_path)?)?,
4143
cli.compat,
42-
),
44+
)?,
4345
)
4446
.context("failed to write temporary boot args")?;
4547
fs::set_permissions(temp_boot_args.path(), Permissions::from_mode(0o755))?;
@@ -55,7 +57,7 @@ fn main() -> Result<(), Error> {
5557
}
5658

5759
/// Disable checks from the kernel command line
58-
fn process_cmdline(input: &str, compat: bool) -> String {
60+
fn process_cmdline(input: &str, compat: bool) -> Result<String> {
5961
let boot_args_re = Regex::new(r"(^|\n)BOOT_ARGS=(.*)(\s+#|\n|$)").unwrap();
6062

6163
let left;
@@ -82,33 +84,30 @@ fn process_cmdline(input: &str, compat: bool) -> String {
8284
}
8385
};
8486

85-
let requires_space = !boot_args.is_empty();
86-
let mut boot_args = format!(
87-
"{boot_args}{sep}ic.setupos.run_checks=0",
88-
sep = if requires_space { " " } else { "" }
89-
);
87+
let mut cmdline = KernelCommandLine::from_str(boot_args)?;
88+
cmdline.ensure_single_argument("ic.setupos.run_checks", Some("0"))?;
9089

9190
// TODO: Remove with NODE-1791
9291
// Disable old flags for temporary backwards compatibility
9392
if compat {
94-
boot_args = format!(
95-
"{boot_args} ic.setupos.check_hardware=0 ic.setupos.check_network=0 ic.setupos.check_age=0",
96-
);
93+
cmdline.ensure_single_argument("ic.setupos.check_hardware", Some("0"))?;
94+
cmdline.ensure_single_argument("ic.setupos.check_network", Some("0"))?;
95+
cmdline.ensure_single_argument("ic.setupos.check_age", Some("0"))?;
9796
}
9897

99-
format!(
100-
"# This file has been modified by setupos-disable-checks.\n{file_start}{indent}BOOT_ARGS=\"{boot_args}\"{tail}{file_end}",
98+
Ok(format!(
99+
"# This file has been modified by setupos-disable-checks.\n{file_start}{indent}BOOT_ARGS=\"{cmdline}\"{tail}{file_end}",
101100
file_start = &input[..left],
102101
file_end = &input[right..],
103-
)
102+
))
104103
}
105104

106105
#[cfg(test)]
107106
mod tests {
108107
use super::*;
109108

110109
fn test(input: &str, expected: &str) {
111-
let result = process_cmdline(input, false);
110+
let result = process_cmdline(input, false).unwrap();
112111

113112
assert_eq!(
114113
expected,
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test")
2+
3+
package(default_visibility = ["//visibility:public"])
4+
5+
rust_library(
6+
name = "linux_kernel_command_line",
7+
srcs = glob(["src/**"]),
8+
deps = [
9+
# Keep sorted.
10+
"@crate_index//:regex",
11+
],
12+
)
13+
14+
rust_test(
15+
name = "unit_tests",
16+
crate = ":linux_kernel_command_line",
17+
)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "linux_kernel_command_line"
3+
description = "Utilities to manipulate a kernel command line reliably."
4+
license = "Apache-2.0"
5+
version.workspace = true
6+
authors.workspace = true
7+
edition.workspace = true
8+
documentation.workspace = true
9+
10+
[dependencies]
11+
regex = { workspace = true }

0 commit comments

Comments
 (0)