-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4e0e48d
commit 6b2f0fc
Showing
4 changed files
with
108 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
use anyhow::Ok; | ||
use anyhow::Result; | ||
|
||
use ostree::gio; | ||
use ostree_ext::ostree; | ||
use ostree_ext::ostree::Deployment; | ||
use crate::deploy::ImageState; | ||
use ostree_ext::prelude::FileExt; | ||
use ostree_ext::prelude::Cast; | ||
use ostree_ext::prelude::FileEnumeratorExt; | ||
|
||
use serde::Deserialize; | ||
|
||
#[derive(Deserialize)] | ||
struct Config { | ||
kargs: Vec<String>, | ||
supported: Supported, | ||
} | ||
|
||
#[derive(Deserialize)] | ||
struct Supported { | ||
architecture: String, | ||
} | ||
|
||
pub(crate) fn get_kargs(repo: &ostree::Repo, booted_deployment: &Deployment, fetched: &ImageState) -> Result<Vec<String>> { | ||
let cancellable = gio::Cancellable::NONE; | ||
let mut kargs: Vec<String> = vec![]; | ||
|
||
// Get the running kargs of the booted system | ||
match ostree::Deployment::bootconfig(booted_deployment) { | ||
Some(bootconfig) => { | ||
match ostree::BootconfigParser::get(&bootconfig, "options") { | ||
Some(options) => { | ||
let options: Vec<&str> = options.split_whitespace().collect(); | ||
let mut options: Vec<String> = options.into_iter().map(|s| s.to_string()).collect(); | ||
kargs.append(&mut options); | ||
}, | ||
None => () | ||
} | ||
}, | ||
None => () | ||
}; | ||
|
||
// Get the kargs in kargs.d of the booted system | ||
let mut existing_kargs: Vec<String> = vec![]; | ||
let fragments = liboverdrop::scan(&["/usr/lib"], "bootc/kargs.d", &["toml"], true); | ||
for (_name, path) in fragments { | ||
let s = std::fs::read_to_string(&path)?; | ||
let mut parsed_kargs = parse_file(s.clone())?; | ||
existing_kargs.append(&mut parsed_kargs); | ||
} | ||
|
||
// Get the kargs in kargs.d of the remote image | ||
let mut remote_kargs: Vec<String> = vec![]; | ||
let (fetched_tree, _) = repo.read_commit(fetched.ostree_commit.as_str(), cancellable)?; | ||
let fetched_tree = fetched_tree.resolve_relative_path("/usr/lib/bootc/kargs.d"); | ||
let fetched_tree = fetched_tree.downcast::<ostree::RepoFile>().expect("downcast"); | ||
match fetched_tree.query_exists(cancellable) { | ||
true => {} | ||
false => { | ||
return Ok(vec![]); | ||
} | ||
} | ||
let queryattrs = "standard::name,standard::type"; | ||
let queryflags = gio::FileQueryInfoFlags::NOFOLLOW_SYMLINKS; | ||
let fetched_iter = fetched_tree.enumerate_children(queryattrs, queryflags, cancellable)?; | ||
while let Some(fetched_info) = fetched_iter.next_file(cancellable)? { | ||
let fetched_child = fetched_iter.child(&fetched_info); | ||
let fetched_child = fetched_child.downcast::<ostree::RepoFile>().expect("downcast"); | ||
fetched_child.ensure_resolved()?; | ||
let fetched_contents_checksum = fetched_child.checksum(); | ||
let f = ostree::Repo::load_file(repo, fetched_contents_checksum.as_str(), cancellable)?; | ||
let file_content = f.0; | ||
let mut reader = ostree_ext::prelude::InputStreamExtManual::into_read(file_content.unwrap()); | ||
let s = std::io::read_to_string(&mut reader)?; | ||
let mut parsed_kargs = parse_file(s.clone())?; | ||
remote_kargs.append(&mut parsed_kargs); | ||
} | ||
|
||
// get the diff between the existing and remote kargs | ||
let mut added_kargs: Vec<String> = remote_kargs.clone().into_iter().filter(|item| !existing_kargs.contains(item)).collect(); | ||
let removed_kargs: Vec<String> = existing_kargs.clone().into_iter().filter(|item| !remote_kargs.contains(item)).collect(); | ||
|
||
// apply the diff to the system kargs | ||
kargs.retain(|x| !removed_kargs.contains(x)); | ||
kargs.append(&mut added_kargs); | ||
|
||
Ok(kargs) | ||
} | ||
|
||
pub fn parse_file(file_content: String) -> Result<Vec<String>> { | ||
let arch = std::env::consts::ARCH; | ||
let de: Config = toml::from_str(&file_content).unwrap(); | ||
let mut parsed_kargs: Vec<String> = vec![]; | ||
if de.supported.architecture == arch { | ||
parsed_kargs = de.kargs; | ||
} | ||
return Ok(parsed_kargs); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters