@@ -1924,30 +1924,58 @@ fn remove_all_in_dir_no_xdev(d: &Dir, mount_err: bool) -> Result<()> {
19241924 anyhow:: Ok ( ( ) )
19251925}
19261926
1927+ #[ context( "Removing boot directory content except loader dir on ostree" ) ]
1928+ fn remove_all_except_loader_dirs ( bootdir : & Dir , is_ostree : bool ) -> Result < ( ) > {
1929+ let entries = bootdir
1930+ . entries ( )
1931+ . context ( "Reading boot directory entries" ) ?;
1932+
1933+ for entry in entries {
1934+ let entry = entry. context ( "Reading directory entry" ) ?;
1935+ let file_name = entry. file_name ( ) ;
1936+ let file_name = if let Some ( n) = file_name. to_str ( ) {
1937+ n
1938+ } else {
1939+ anyhow:: bail!( "Invalid non-UTF8 filename: {file_name:?} in /boot" ) ;
1940+ } ;
1941+
1942+ // Only preserve loader on ostree
1943+ if is_ostree && file_name. starts_with ( "loader" ) {
1944+ continue ;
1945+ }
1946+
1947+ let etype = entry. file_type ( ) ?;
1948+ if etype == FileType :: dir ( ) {
1949+ // Open the directory and remove its contents
1950+ if let Some ( subdir) = bootdir. open_dir_noxdev ( & file_name) ? {
1951+ remove_all_in_dir_no_xdev ( & subdir, false )
1952+ . with_context ( || format ! ( "Removing directory contents: {}" , file_name) ) ?;
1953+ }
1954+ } else {
1955+ bootdir
1956+ . remove_file_optional ( & file_name)
1957+ . with_context ( || format ! ( "Removing file: {}" , file_name) ) ?;
1958+ }
1959+ }
1960+ Ok ( ( ) )
1961+ }
1962+
19271963#[ context( "Removing boot directory content" ) ]
19281964fn clean_boot_directories ( rootfs : & Dir , is_ostree : bool ) -> Result < ( ) > {
19291965 let bootdir =
19301966 crate :: utils:: open_dir_remount_rw ( rootfs, BOOT . into ( ) ) . context ( "Opening /boot" ) ?;
19311967
1932- if is_ostree {
1933- // On ostree systems, the boot directory already has our desired format, we should only
1934- // remove the bootupd-state.json file to avoid bootupctl complaining it already exists.
1935- bootdir
1936- . remove_file_optional ( "bootupd-state.json" )
1937- . context ( "removing bootupd-state.json" ) ?;
1938- } else {
1939- // This should not remove /boot/efi note.
1940- remove_all_in_dir_no_xdev ( & bootdir, false ) . context ( "Emptying /boot" ) ?;
1941- // TODO: Discover the ESP the same way bootupd does it; we should also
1942- // support not wiping the ESP.
1943- if ARCH_USES_EFI {
1944- if let Some ( efidir) = bootdir
1945- . open_dir_optional ( crate :: bootloader:: EFI_DIR )
1946- . context ( "Opening /boot/efi" ) ?
1947- {
1948- remove_all_in_dir_no_xdev ( & efidir, false )
1949- . context ( "Emptying EFI system partition" ) ?;
1950- }
1968+ // This should not remove /boot/efi note.
1969+ remove_all_except_loader_dirs ( & bootdir, is_ostree) . context ( "Emptying /boot" ) ?;
1970+
1971+ // TODO: Discover the ESP the same way bootupd does it; we should also
1972+ // support not wiping the ESP.
1973+ if ARCH_USES_EFI {
1974+ if let Some ( efidir) = bootdir
1975+ . open_dir_optional ( crate :: bootloader:: EFI_DIR )
1976+ . context ( "Opening /boot/efi" ) ?
1977+ {
1978+ remove_all_in_dir_no_xdev ( & efidir, false ) . context ( "Emptying EFI system partition" ) ?;
19511979 }
19521980 }
19531981
0 commit comments