@@ -130,26 +130,17 @@ where
130130 Ok ( ( ) )
131131}
132132
133- /// Updates kernel partition representations to match the GPT table
133+ /// Removes all kernel partitions for the specified block device
134134///
135135/// # Arguments
136136/// * `path` - Path to the block device
137137///
138138/// # Returns
139139/// `Result<(), Error>` indicating success or partition operation failure
140- pub fn sync_gpt_partitions < P : AsRef < Path > > ( path : P ) -> Result < ( ) , Error > {
141- info ! ( "Initiating GPT partition synchronization for {:?}" , path. as_ref( ) ) ;
140+ pub fn remove_kernel_partitions < P : AsRef < Path > > ( path : P ) -> Result < ( ) , Error > {
141+ debug ! ( "Beginning partition cleanup process for {:?}" , path. as_ref( ) ) ;
142142 let file = File :: open ( & path) ?;
143143
144- // Read GPT table
145- debug ! ( "Reading GPT partition table" ) ;
146- let gpt = gpt:: GptConfig :: new ( ) . writable ( false ) . open ( & path) ?;
147- let partitions = gpt. partitions ( ) ;
148- let block_size = 512 ;
149- info ! ( "Located {} partitions (block size: {})" , partitions. len( ) , block_size) ;
150-
151- debug ! ( "Beginning partition cleanup process" ) ;
152-
153144 // Find the disk for enumeration purposes
154145 let base_name = path
155146 . as_ref ( )
@@ -164,6 +155,28 @@ pub fn sync_gpt_partitions<P: AsRef<Path>>(path: P) -> Result<(), Error> {
164155 let _ = delete_partition ( file. as_raw_fd ( ) , partition. number as i32 ) ;
165156 }
166157
158+ info ! ( "Successfully removed all kernel partitions" ) ;
159+ Ok ( ( ) )
160+ }
161+
162+ /// Creates kernel partitions based on the current GPT table
163+ ///
164+ /// # Arguments
165+ /// * `path` - Path to the block device
166+ ///
167+ /// # Returns
168+ /// `Result<(), Error>` indicating success or partition operation failure
169+ pub fn create_kernel_partitions < P : AsRef < Path > > ( path : P ) -> Result < ( ) , Error > {
170+ info ! ( "Creating kernel partitions from GPT for {:?}" , path. as_ref( ) ) ;
171+ let file = File :: open ( & path) ?;
172+
173+ // Read GPT table
174+ debug ! ( "Reading GPT partition table" ) ;
175+ let gpt = gpt:: GptConfig :: new ( ) . writable ( false ) . open ( & path) ?;
176+ let partitions = gpt. partitions ( ) ;
177+ let block_size = 512 ;
178+ info ! ( "Located {} partitions (block size: {})" , partitions. len( ) , block_size) ;
179+
167180 // Add partitions from GPT
168181 debug ! ( "Beginning partition creation from GPT table" ) ;
169182 for ( i, partition) in partitions. iter ( ) {
@@ -175,6 +188,23 @@ pub fn sync_gpt_partitions<P: AsRef<Path>>(path: P) -> Result<(), Error> {
175188 ) ?;
176189 }
177190
191+ info ! ( "GPT partition creation completed successfully" ) ;
192+ Ok ( ( ) )
193+ }
194+
195+ /// Updates kernel partition representations to match the GPT table
196+ ///
197+ /// # Arguments
198+ /// * `path` - Path to the block device
199+ ///
200+ /// # Returns
201+ /// `Result<(), Error>` indicating success or partition operation failure
202+ pub fn sync_gpt_partitions < P : AsRef < Path > > ( path : P ) -> Result < ( ) , Error > {
203+ info ! ( "Initiating GPT partition synchronization for {:?}" , path. as_ref( ) ) ;
204+
205+ remove_kernel_partitions ( & path) ?;
206+ create_kernel_partitions ( & path) ?;
207+
178208 info ! ( "GPT partition synchronization completed successfully" ) ;
179209 Ok ( ( ) )
180210}
0 commit comments