Skip to content

feat: add quirk feature #18

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 2 commits into
base: master
Choose a base branch
from
Open
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
40 changes: 40 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ disk = { path = "./disk" }
install = { path = "./install" }

[workspace]
members = ["disk", "install"]
members = ["disk", "install", "quirk"]

[patch.crates-io]
loopdev = { git = "https://github.com/eatradish/loopdev", rev = "0dde43a15320cf84148e57fed8aec6683755c04f" }
Expand Down
1 change: 1 addition & 0 deletions install/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ faster-hex = "0.10.0"
serde_json = "1.0.128"
num_enum = "0.7.3"
snafu = "0.8.5"
quirk = { path = "../quirk" }

[features]
default = []
Expand Down
67 changes: 65 additions & 2 deletions install/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::{
collections::HashSet,
fmt::{Display, Formatter},
fs::{self, create_dir_all, read_dir},
io::{self, Write},
Expand Down Expand Up @@ -26,6 +27,7 @@ use grub::RunGrubError;
use locale::SetHwclockError;
use mount::{mount_root_path, UmountError};
use num_enum::IntoPrimitive;
use quirk::get_matches_quirk;
use rustix::{
fs::sync,
io::Errno,
Expand Down Expand Up @@ -350,7 +352,7 @@ macro_rules! cancel_install_exit {
};
}

#[derive(Clone, IntoPrimitive)]
#[derive(Clone, IntoPrimitive, PartialEq, Eq)]
#[repr(u8)]
enum InstallationStage {
SetupPartition = 1,
Expand All @@ -368,6 +370,7 @@ enum InstallationStage {
UmountInnerPath,
UmountEFIPath,
UmountRootPath,
Quirk,
Done,
}

Expand Down Expand Up @@ -395,6 +398,7 @@ impl Display for InstallationStage {
Self::UmountInnerPath => "umount inner path",
Self::UmountEFIPath => "umount EFI path",
Self::UmountRootPath => "umount root path",
Self::Quirk => "run quirk",
Self::Done => "done",
};

Expand All @@ -419,7 +423,8 @@ impl InstallationStage {
Self::CopyLog => Self::UmountInnerPath,
Self::UmountInnerPath => Self::UmountEFIPath,
Self::UmountEFIPath => Self::UmountRootPath,
Self::UmountRootPath => Self::Done,
Self::UmountRootPath => Self::Quirk,
Self::Quirk => Self::Done,
Self::Done => Self::Done,
}
}
Expand All @@ -436,6 +441,37 @@ impl InstallConfig {
) -> Result<bool, InstallErr> {
debug!("Install config: {:#?}", self);

let quirks = get_matches_quirk("/usr/share/deploykit-backend/quirks");

let mut no_run = HashSet::new();
let mut quirk_commands = vec![];

for quirk in quirks {
if let Some(skip_stages) = quirk.skip_stages {
no_run.extend(skip_stages);
}
quirk_commands.push(quirk.command);
}

let no_run = no_run
.iter()
.flat_map(|x| match x.as_str() {
"SetupPartition" => Some(InstallationStage::SetupPartition),
"DownloadSquashfs" => Some(InstallationStage::DownloadSquashfs),
"ExtractSquashfs" => Some(InstallationStage::ExtractSquashfs),
"GenerateFstab" => Some(InstallationStage::GenerateFstab),
"Dracut" => Some(InstallationStage::Dracut),
"InstallGrub" => Some(InstallationStage::InstallGrub),
"GenerateSshKey" => Some(InstallationStage::GenerateSshKey),
"ConfigureSystem" => Some(InstallationStage::ConfigureSystem),
"SwapOff" => Some(InstallationStage::SwapOff),
x => {
error!("Unsupport skip step: {x}");
None
}
})
.collect::<Vec<_>>();

let root_fd = get_dir_fd(Path::new("/")).context(GetDirFdSnafu)?;

let mut stage = InstallationStage::default();
Expand Down Expand Up @@ -464,11 +500,17 @@ impl InstallConfig {
InstallationStage::UmountInnerPath => 8,
InstallationStage::UmountEFIPath => 8,
InstallationStage::UmountRootPath => 8,
InstallationStage::Quirk => 8,
InstallationStage::Done => 8,
};

step.store(num, Ordering::SeqCst);

if no_run.contains(&stage) {
stage = stage.get_next_stage();
continue;
}

let res = match stage {
InstallationStage::SetupPartition => self
.setup_partition(&progress, &tmp_mount_path, &cancel_install)
Expand Down Expand Up @@ -538,6 +580,27 @@ impl InstallConfig {
.context(UmountSnafu)
.context(PostInstallationSnafu)
.map(|_| true),
InstallationStage::Quirk => {
for cmd in &quirk_commands {
let out = match Command::new("bash").arg("-c").arg(cmd).output() {
Ok(out) => out,
Err(e) => {
error!("Run {} failed: {}", cmd, e);
continue;
}
};

if !out.status.success() {
error!(
"Run {} failed: stderr: {}",
cmd,
String::from_utf8_lossy(&out.stderr)
)
}
}

Ok(true)
}
InstallationStage::Done => break,
};

Expand Down
13 changes: 13 additions & 0 deletions quirk/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "quirk"
version = "0.1.0"
edition = "2024"

[dependencies]
toml = "0.8"
serde = { version = "1", features = ["derive"] }
walkdir = "2"
fancy-regex = "0.14.0"
glob = "0.3"
snafu = "0.8"
tracing = "0.1.40"
23 changes: 23 additions & 0 deletions quirk/examples/test_dmi_is_match.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use std::{env::args, process::exit};

use quirk::{dmi_is_match, QuirkError};

fn main() -> Result<(), QuirkError> {
let args = args().skip(1).collect::<Vec<_>>();

if args.len() != 2 {
eprintln!("Usage: DMI_PATTERN MODALIAS");
exit(1);
}

let result = dmi_is_match(&args[1], &args[0])?;

if result {
println!("Match.");
} else {
println!("Not match.");
exit(1);
}

Ok(())
}
Loading