@@ -1851,30 +1851,58 @@ fn remove_all_in_dir_no_xdev(d: &Dir, mount_err: bool) -> Result<()> {
18511851 anyhow:: Ok ( ( ) )
18521852}
18531853
1854+ #[ context( "Removing boot directory content except loader dir on ostree" ) ]
1855+ fn remove_all_except_loader_dirs ( bootdir : & Dir , is_ostree : bool ) -> Result < ( ) > {
1856+ let entries = bootdir
1857+ . entries ( )
1858+ . context ( "Reading boot directory entries" ) ?;
1859+
1860+ for entry in entries {
1861+ let entry = entry. context ( "Reading directory entry" ) ?;
1862+ let file_name = entry. file_name ( ) ;
1863+ let file_name = if let Some ( n) = file_name. to_str ( ) {
1864+ n
1865+ } else {
1866+ anyhow:: bail!( "Invalid non-UTF8 filename: {file_name:?} in /boot" ) ;
1867+ } ;
1868+
1869+ // Only preserve loader on ostree
1870+ if is_ostree && file_name. starts_with ( "loader" ) {
1871+ continue ;
1872+ }
1873+
1874+ let etype = entry. file_type ( ) ?;
1875+ if etype == FileType :: dir ( ) {
1876+ // Open the directory and remove its contents
1877+ if let Some ( subdir) = bootdir. open_dir_noxdev ( & file_name) ? {
1878+ remove_all_in_dir_no_xdev ( & subdir, false )
1879+ . with_context ( || format ! ( "Removing directory contents: {}" , file_name) ) ?;
1880+ }
1881+ } else {
1882+ bootdir
1883+ . remove_file_optional ( & file_name)
1884+ . with_context ( || format ! ( "Removing file: {}" , file_name) ) ?;
1885+ }
1886+ }
1887+ Ok ( ( ) )
1888+ }
1889+
18541890#[ context( "Removing boot directory content" ) ]
18551891fn clean_boot_directories ( rootfs : & Dir , is_ostree : bool ) -> Result < ( ) > {
18561892 let bootdir =
18571893 crate :: utils:: open_dir_remount_rw ( rootfs, BOOT . into ( ) ) . context ( "Opening /boot" ) ?;
18581894
1859- if is_ostree {
1860- // On ostree systems, the boot directory already has our desired format, we should only
1861- // remove the bootupd-state.json file to avoid bootupctl complaining it already exists.
1862- bootdir
1863- . remove_file_optional ( "bootupd-state.json" )
1864- . context ( "removing bootupd-state.json" ) ?;
1865- } else {
1866- // This should not remove /boot/efi note.
1867- remove_all_in_dir_no_xdev ( & bootdir, false ) . context ( "Emptying /boot" ) ?;
1868- // TODO: Discover the ESP the same way bootupd does it; we should also
1869- // support not wiping the ESP.
1870- if ARCH_USES_EFI {
1871- if let Some ( efidir) = bootdir
1872- . open_dir_optional ( crate :: bootloader:: EFI_DIR )
1873- . context ( "Opening /boot/efi" ) ?
1874- {
1875- remove_all_in_dir_no_xdev ( & efidir, false )
1876- . context ( "Emptying EFI system partition" ) ?;
1877- }
1895+ // This should not remove /boot/efi note.
1896+ remove_all_except_loader_dirs ( & bootdir, is_ostree) . context ( "Emptying /boot" ) ?;
1897+
1898+ // TODO: Discover the ESP the same way bootupd does it; we should also
1899+ // support not wiping the ESP.
1900+ if ARCH_USES_EFI {
1901+ if let Some ( efidir) = bootdir
1902+ . open_dir_optional ( crate :: bootloader:: EFI_DIR )
1903+ . context ( "Opening /boot/efi" ) ?
1904+ {
1905+ remove_all_in_dir_no_xdev ( & efidir, false ) . context ( "Emptying EFI system partition" ) ?;
18781906 }
18791907 }
18801908
0 commit comments