@@ -1862,30 +1862,58 @@ fn remove_all_in_dir_no_xdev(d: &Dir, mount_err: bool) -> Result<()> {
18621862 anyhow:: Ok ( ( ) )
18631863}
18641864
1865+ #[ context( "Removing boot directory content except loader dir on ostree" ) ]
1866+ fn remove_all_except_loader_dirs ( bootdir : & Dir , is_ostree : bool ) -> Result < ( ) > {
1867+ let entries = bootdir
1868+ . entries ( )
1869+ . context ( "Reading boot directory entries" ) ?;
1870+
1871+ for entry in entries {
1872+ let entry = entry. context ( "Reading directory entry" ) ?;
1873+ let file_name = entry. file_name ( ) ;
1874+ let file_name = if let Some ( n) = file_name. to_str ( ) {
1875+ n
1876+ } else {
1877+ anyhow:: bail!( "Invalid non-UTF8 filename: {file_name:?} in /boot" ) ;
1878+ } ;
1879+
1880+ // Only preserve loader on ostree
1881+ if is_ostree && file_name. starts_with ( "loader" ) {
1882+ continue ;
1883+ }
1884+
1885+ let etype = entry. file_type ( ) ?;
1886+ if etype == FileType :: dir ( ) {
1887+ // Open the directory and remove its contents
1888+ if let Some ( subdir) = bootdir. open_dir_noxdev ( & file_name) ? {
1889+ remove_all_in_dir_no_xdev ( & subdir, false )
1890+ . with_context ( || format ! ( "Removing directory contents: {}" , file_name) ) ?;
1891+ }
1892+ } else {
1893+ bootdir
1894+ . remove_file_optional ( & file_name)
1895+ . with_context ( || format ! ( "Removing file: {}" , file_name) ) ?;
1896+ }
1897+ }
1898+ Ok ( ( ) )
1899+ }
1900+
18651901#[ context( "Removing boot directory content" ) ]
18661902fn clean_boot_directories ( rootfs : & Dir , is_ostree : bool ) -> Result < ( ) > {
18671903 let bootdir =
18681904 crate :: utils:: open_dir_remount_rw ( rootfs, BOOT . into ( ) ) . context ( "Opening /boot" ) ?;
18691905
1870- if is_ostree {
1871- // On ostree systems, the boot directory already has our desired format, we should only
1872- // remove the bootupd-state.json file to avoid bootupctl complaining it already exists.
1873- bootdir
1874- . remove_file_optional ( "bootupd-state.json" )
1875- . context ( "removing bootupd-state.json" ) ?;
1876- } else {
1877- // This should not remove /boot/efi note.
1878- remove_all_in_dir_no_xdev ( & bootdir, false ) . context ( "Emptying /boot" ) ?;
1879- // TODO: Discover the ESP the same way bootupd does it; we should also
1880- // support not wiping the ESP.
1881- if ARCH_USES_EFI {
1882- if let Some ( efidir) = bootdir
1883- . open_dir_optional ( crate :: bootloader:: EFI_DIR )
1884- . context ( "Opening /boot/efi" ) ?
1885- {
1886- remove_all_in_dir_no_xdev ( & efidir, false )
1887- . context ( "Emptying EFI system partition" ) ?;
1888- }
1906+ // This should not remove /boot/efi note.
1907+ remove_all_except_loader_dirs ( & bootdir, is_ostree) . context ( "Emptying /boot" ) ?;
1908+
1909+ // TODO: Discover the ESP the same way bootupd does it; we should also
1910+ // support not wiping the ESP.
1911+ if ARCH_USES_EFI {
1912+ if let Some ( efidir) = bootdir
1913+ . open_dir_optional ( crate :: bootloader:: EFI_DIR )
1914+ . context ( "Opening /boot/efi" ) ?
1915+ {
1916+ remove_all_in_dir_no_xdev ( & efidir, false ) . context ( "Emptying EFI system partition" ) ?;
18891917 }
18901918 }
18911919
0 commit comments