11use crate :: helpers:: {
22 CheckGroup , CheckGroupResult , CheckResult ,
3- CheckResultValue :: { Errored , Failed , Passed } , host_executor:: HostNamespaceExecutor ,
3+ CheckResultValue :: { Errored , Failed , Passed } ,
4+ host_executor:: HostNamespaceExecutor ,
45} ;
56
67use anyhow:: { Result , bail} ;
78use async_trait:: async_trait;
9+ use futures:: { FutureExt , future:: join_all} ;
810use log:: debug;
911use procfs:: { Current , sys:: kernel} ;
1012use std:: { fs, path:: PathBuf , process:: Command } ;
11- use futures:: { future:: join_all, FutureExt } ;
1213
1314const GROUP_IDENTIFIER : & str = "KernelChecks" ;
1415const NAME : & str = "Kernel Checks" ;
1516// TODO (bml) assemble actual list
16- const REQUIRED_MODULES : & [ & str ] = & [
17- "nf_tables" ,
18- ] ;
17+ const REQUIRED_MODULES : & [ & str ] = & [ "nf_tables" ] ;
1918
2019pub struct KernelChecks {
2120 host_executor : HostNamespaceExecutor ,
@@ -29,12 +28,7 @@ impl KernelChecks {
2928 /// Run all the checkers asynchronously, then
3029 /// join and collect the results.
3130 pub async fn run_all ( & self ) -> CheckGroupResult {
32- let results = join_all ( [
33- self . has_modules ( ) . boxed ( ) ,
34- self . version_is_good ( ) . boxed ( ) ,
35- ] )
36- . await ;
37-
31+ let results = join_all ( [ self . has_modules ( ) . boxed ( ) , self . version_is_good ( ) . boxed ( ) ] ) . await ;
3832
3933 let mut group_result = Passed ;
4034 for res in results. iter ( ) {
@@ -60,9 +54,11 @@ impl KernelChecks {
6054 let mut result = Passed ;
6155
6256 // Get host kernel version
63- let current = self . host_executor . spawn_in_host_ns ( async {
64- kernel:: Version :: current ( )
65- } ) . await . expect ( "error spawning in host" ) ;
57+ let current = self
58+ . host_executor
59+ . spawn_in_host_ns ( async { kernel:: Version :: current ( ) } )
60+ . await
61+ . expect ( "error spawning in host" ) ;
6662
6763 if let Err ( e) = current {
6864 return CheckResult :: new ( & name, Errored ( e. to_string ( ) ) ) ;
@@ -80,18 +76,23 @@ impl KernelChecks {
8076 let name = String :: from ( "Host Has Necessary Modules" ) ;
8177 let mut result = Passed ;
8278
83- let required_modules: Vec < String > = REQUIRED_MODULES . iter ( ) . map ( |s| s. to_string ( ) ) . collect ( ) ;
79+ let required_modules: Vec < String > =
80+ REQUIRED_MODULES . iter ( ) . map ( |s| s. to_string ( ) ) . collect ( ) ;
8481
8582 // Search builtin modules
8683 let remaining = match self . find_builtins ( & required_modules) . await {
8784 Ok ( r) => r,
88- Err ( e) => return CheckResult :: new ( & name, Errored ( format ! ( "getting kernel builtins {e}" ) ) ) ,
85+ Err ( e) => {
86+ return CheckResult :: new ( & name, Errored ( format ! ( "getting kernel builtins {e}" ) ) ) ;
87+ }
8988 } ;
9089
9190 // Search loaded modules
9291 let remaining = match self . find_loaded ( & remaining) . await {
9392 Ok ( r) => r,
94- Err ( e) => return CheckResult :: new ( & name, Errored ( format ! ( "getting kernel modules {e}" ) ) ) ,
93+ Err ( e) => {
94+ return CheckResult :: new ( & name, Errored ( format ! ( "getting kernel modules {e}" ) ) ) ;
95+ }
9596 } ;
9697 if !remaining. is_empty ( ) {
9798 result = Failed ( format ! ( "missing {:?}" , remaining) )
@@ -107,20 +108,21 @@ impl KernelChecks {
107108 let mut modules_to_find: Vec < String > = required_modules. clone ( ) ;
108109
109110 // read host builtins
110- let builtins = self . host_executor . spawn_in_host_ns ( async move {
111- // Get kernel version
112- let output = Command :: new ( "uname" )
113- . arg ( "-r" )
114- . output ( ) ?;
115-
116- if !output. status . success ( ) {
117- let error_message = String :: from_utf8_lossy ( & output. stderr ) ;
118- bail ! ( "{}" , error_message. to_string( ) ) ;
119- }
120- let kernel_version = String :: from_utf8_lossy ( & output. stdout ) . trim ( ) . to_string ( ) ;
121- let path = PathBuf :: from ( format ! ( "/lib/modules/{kernel_version}/modules.builtin" ) ) ;
122- fs:: read_to_string ( path) . map_err ( |e| anyhow:: anyhow!( e) )
123- } ) . await ??;
111+ let builtins = self
112+ . host_executor
113+ . spawn_in_host_ns ( async move {
114+ // Get kernel version
115+ let output = Command :: new ( "uname" ) . arg ( "-r" ) . output ( ) ?;
116+
117+ if !output. status . success ( ) {
118+ let error_message = String :: from_utf8_lossy ( & output. stderr ) ;
119+ bail ! ( "{}" , error_message) ;
120+ }
121+ let kernel_version = String :: from_utf8_lossy ( & output. stdout ) . trim ( ) . to_string ( ) ;
122+ let path = PathBuf :: from ( format ! ( "/lib/modules/{kernel_version}/modules.builtin" ) ) ;
123+ fs:: read_to_string ( path) . map_err ( |e| anyhow:: anyhow!( e) )
124+ } )
125+ . await ??;
124126
125127 for builtin in builtins. lines ( ) {
126128 let found = modules_to_find
@@ -142,16 +144,15 @@ impl KernelChecks {
142144 async fn find_loaded ( & self , required_modules : & Vec < String > ) -> Result < Vec < String > > {
143145 let mut modules_to_find: Vec < String > = required_modules. clone ( ) ;
144146
145- let modules = self . host_executor . spawn_in_host_ns ( async move {
146- procfs:: KernelModules :: current ( )
147- } ) . await ?;
147+ let modules = self
148+ . host_executor
149+ . spawn_in_host_ns ( async move { procfs:: KernelModules :: current ( ) } )
150+ . await ?;
148151
149152 let modules = modules. unwrap ( ) ;
150153
151154 for ( name, _) in modules. 0 . iter ( ) {
152- let found = modules_to_find
153- . iter ( )
154- . position ( |required| required == name) ;
155+ let found = modules_to_find. iter ( ) . position ( |required| required == name) ;
155156
156157 if let Some ( index) = found {
157158 debug ! ( "module {}" , modules_to_find[ index] ) ;
0 commit comments